2 * Tests for prctl(PR_GET_TSC, ...) / prctl(PR_SET_TSC, ...)
4 * Tests if the control register is updated correctly
5 * when set with prctl()
7 * Warning: this test will cause a very high load for a few seconds
19 #include <sys/prctl.h>
20 #include <linux/prctl.h>
22 /* Get/set the process' ability to use the timestamp counter instruction */
26 # define PR_TSC_ENABLE 1 /* allow the use of the timestamp counter */
27 # define PR_TSC_SIGSEGV 2 /* throw a SIGSEGV instead of reading the TSC */
30 /* snippet from wikipedia :-) */
32 static uint64_t rdtsc(void)
35 /* We cannot use "=A", since this would use %rax on x86_64 */
36 __asm__
__volatile__ ("rdtsc" : "=a" (lo
), "=d" (hi
));
37 return (uint64_t)hi
<< 32 | lo
;
42 static void sigsegv_cb(int sig
)
46 fprintf(stderr
, "FATAL ERROR, rdtsc() failed while enabled\n");
49 if (prctl(PR_SET_TSC
, PR_TSC_ENABLE
) < 0)
59 static void task(void)
61 signal(SIGSEGV
, sigsegv_cb
);
68 fprintf(stderr
, "FATAL ERROR, rdtsc() succeeded while disabled\n");
71 if (prctl(PR_SET_TSC
, PR_TSC_SIGSEGV
) < 0)
81 int main(int argc
, char **argv
)
85 fprintf(stderr
, "[No further output means we're allright]\n");
87 for (i
=0; i
<n_tasks
; i
++)
91 for (i
=0; i
<n_tasks
; i
++)