xref: /llvm-project/compiler-rt/test/sanitizer_common/TestCases/Posix/getcpuclockid.c (revision 975fa725063fe33aba02164a53c4ef66662e68d8)
1 // RUN: %clang -pthread %s -Wl,-as-needed -o %t && %run %t
2 //
3 // UNSUPPORTED: darwin, target={{.*solaris.*}}
4 
5 #include <time.h>
6 #include <unistd.h>
7 #include <assert.h>
8 #include <pthread.h>
9 
cpu_ns()10 long cpu_ns() {
11   clockid_t clk;
12   struct timespec ts;
13   int res = clock_getcpuclockid(getpid(), &clk);
14   assert(!res);
15   res = clock_gettime(clk, &ts);
16   assert(!res);
17   return ts.tv_nsec;
18 }
19 
th_cpu_ns()20 long th_cpu_ns() {
21   clockid_t clk;
22   struct timespec ts;
23   int res = pthread_getcpuclockid(pthread_self(), &clk);
24   assert(!res);
25   res = clock_gettime(clk, &ts);
26   assert(!res);
27   return ts.tv_nsec;
28 }
29 
main()30 int main() {
31   long cpuns = cpu_ns();
32   asm volatile ("" :: "r"(cpuns));
33   cpuns = th_cpu_ns();
34   asm volatile ("" :: "r"(cpuns));
35   return 0;
36 }
37