pastebin - collaborative debugging tool
rovema.kpaste.net RSS


x86 RDTSCP instruction test code 1
Posted by Anonymous on Mon 17th Oct 2022 17:19
raw | new post
view followups (newest first): x86 RDTSCP instruction test code 1 by Anonymous
modification of post by Anonymous (view diff)

  1. /*
  2.  * x86_rdtscp1.c - test x86 RDTSCP instruction to get the current
  3.  * CPU number
  4.  *
  5.  * Compile with:
  6.  * $ gcc -std=gnu17 -m64 x86_rdtscp1.c -lpthread
  7.  *
  8.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  9.  *
  10.  */
  11.  
  12. #define _XOPEN_SOURCE 700
  13. #define _GNU_SOURCE 1
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. #include <pthread.h>
  19. #include <sched.h>
  20. #include <x86intrin.h>
  21.  
  22.  
  23. int main(int ac, char *av[])
  24. {
  25.         (void)puts("#start.");
  26.        
  27.         unsigned int A = 0;
  28.         unsigned long long retval = 0;
  29.         int cpu;
  30.        
  31.         /*
  32.          * The x86 rdtscp instruction and |sched_getcpu()| syscall can
  33.          * return different results when the scheduler reschedules the
  34.          * running thread, usually at the syscall boundary.
  35.          *
  36.          * Notes:
  37.          * - Using the FIFO scheduler can reduce the instances where
  38.          * this happens, but even then the scheduler can get active:
  39.          * $ time chrt --fifo 20 ksh93 -c 'while true ; do \
  40.          *      x="$(/home/rmainz/tmp/x86_rdtscp/a.out)" ; \
  41.          *      [[ "$x" == *match* ]] && break ; done ; \
  42.          * printf "res=%s\n" "$x"'
  43.          * res=#start.
  44.          * _rdtscp:        A=7, retval=43db24d4804f4e
  45.          * sched_getcpu(): cpu=3
  46.          * # mismatch A!=cpu
  47.          * #done.
  48.          * real    5m27.782s
  49.          * user    3m53.786s
  50.          * sys     1m40.138s
  51.          *
  52.          * - The only useable solution is to pin the thread to one
  53.          * cpu strand, as shown in the |USE_FIXED_CPU_NUMBER| codepath
  54.          * below...
  55.          *
  56.          * - Read https://unix4lyfe.org/benchmarking/
  57.          *
  58.          * - On some machines the cpu (strand) number is always |0|,
  59.          * independent from which CPU/strand the thread is really
  60.          * executing on... ;-(
  61.          */
  62.  
  63. #define USE_FIXED_CPU_NUMBER 1
  64. #if USE_FIXED_CPU_NUMBER
  65.         cpu_set_t cpuset;
  66.         pthread_t thread;
  67.        
  68.         thread = pthread_self();
  69.         cpu = sched_getcpu();
  70.  
  71.         CPU_ZERO(&cpuset);
  72.         CPU_SET(cpu, &cpuset);
  73.  
  74.         if (pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset) == 0) {
  75.                 (void)printf("# pthread_setaffinity_np(), "
  76.                         "thread fixed to cpu %d\n",
  77.                         cpu);
  78.         }
  79.         else {
  80.                 /*
  81.                  * If you specify a CPU number beyond the maximum number
  82.                  * of CPUs, then |pthread_setaffinity_np()| can have a
  83.                  * |errno==0
  84.                  */
  85.                 perror("pthread_setaffinity_np");
  86.         }
  87. #endif /* USE_FIXED_CPU_NUMBER */
  88.  
  89.         retval = _rdtscp(&A);
  90.         cpu = sched_getcpu();
  91.  
  92.         (void)printf("_rdtscp:\tA=%x, ", A);
  93.         (void)printf("retval=%llx\n", retval);
  94.         (void)printf("sched_getcpu():\tcpu=%x\n", cpu);
  95.        
  96.         if (cpu != A)
  97.                 (void)puts("# mismatch A!=cpu");
  98.         (void)puts("#done.");
  99.  
  100.         return EXIT_SUCCESS;
  101. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at