Advertisement
teknoraver

ctx.c

Oct 3rd, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. /*
  2.  * ctx_time Copyright (C) 2018 Matteo Croce <mcroce@redhat.com>
  3.  * a tool measure the context switch time in clock cycles
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <limits.h>
  10. #include <string.h>
  11. #include <x86intrin.h>
  12.  
  13. int main (int argc, char *argv[])
  14. {
  15.     int i;
  16.     unsigned long long min = ULLONG_MAX;
  17.     unsigned long long time;
  18.     unsigned junk;
  19.  
  20.     for (i = 0; i < 10000000; i++) {
  21.         time = __rdtscp(&junk);
  22.         /* a syscalls that does basically nothing but returning a number */
  23.         getuid();
  24.         time = __rdtscp(&junk) - time;
  25.  
  26.         /* the OS has always better things to do than take care of
  27.          * our benchmarks, get the best value obtained
  28.          */
  29.         if (time < min)
  30.             min = time;
  31.     }
  32.     printf("ctx: %llu clocks\n", min);
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement