Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / Documentation / prctl / disable-tsc-on-off-stress-test.c
bloba06f027e9d1642a0c8b12591dae0bfe05e4ec2da
1 /*
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
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <signal.h>
15 #include <inttypes.h>
16 #include <wait.h>
19 #include <sys/prctl.h>
20 #include <linux/prctl.h>
22 /* Get/set the process' ability to use the timestamp counter instruction */
23 #ifndef PR_GET_TSC
24 #define PR_GET_TSC 25
25 #define PR_SET_TSC 26
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 */
28 #endif
30 /* snippet from wikipedia :-) */
32 static uint64_t rdtsc(void)
34 uint32_t lo, hi;
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;
40 int should_segv = 0;
42 static void sigsegv_cb(int sig)
44 if (!should_segv)
46 fprintf(stderr, "FATAL ERROR, rdtsc() failed while enabled\n");
47 exit(0);
49 if (prctl(PR_SET_TSC, PR_TSC_ENABLE) < 0)
51 perror("prctl");
52 exit(0);
54 should_segv = 0;
56 rdtsc();
59 static void task(void)
61 signal(SIGSEGV, sigsegv_cb);
62 alarm(10);
63 for(;;)
65 rdtsc();
66 if (should_segv)
68 fprintf(stderr, "FATAL ERROR, rdtsc() succeeded while disabled\n");
69 exit(0);
71 if (prctl(PR_SET_TSC, PR_TSC_SIGSEGV) < 0)
73 perror("prctl");
74 exit(0);
76 should_segv = 1;
81 int main(void)
83 int n_tasks = 100, i;
85 fprintf(stderr, "[No further output means we're allright]\n");
87 for (i=0; i<n_tasks; i++)
88 if (fork() == 0)
89 task();
91 for (i=0; i<n_tasks; i++)
92 wait(NULL);
94 exit(0);