Attachment 'tls_benchmark.c'

Download

   1 #include <stdio.h>
   2 #include <time.h>
   3 
   4 #define LOOPS 10000000
   5 
   6 static __thread int tlsvar __attribute__((tls_model ("initial-exec")));
   7 int globalvar __attribute__ ((visibility ("hidden")));
   8 //int globalvar;
   9 
  10 int tls_access(void)
  11 {
  12   return tlsvar;
  13 }
  14 
  15 void set_tls(int v)
  16 {
  17   tlsvar = v;
  18 }
  19 
  20 int global_access(void)
  21 {
  22   return globalvar;
  23 }
  24 
  25 int main(void)
  26 {
  27   struct timespec start, end;
  28   unsigned int i, loops = LOOPS;
  29   double tls_elapsed, global_elapsed;
  30 
  31   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
  32   for (i = 0; i < LOOPS; i++)
  33     {
  34       tls_access();
  35     }
  36   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
  37 
  38   tls_elapsed = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) * 1e-9;
  39 
  40   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
  41   for (i = 0; i < LOOPS; i++)
  42     {
  43       global_access();
  44     }
  45   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
  46 
  47   global_elapsed = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) * 1e-9;
  48 
  49   printf("TLS ticks per 1000 loops: %.10f Global ticks per 1000 loops: %.10f\n",
  50          (tls_elapsed / loops) * 1000, (global_elapsed / loops) * 1000);
  51 
  52 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.