| 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 | |
| 10 | long cpu_ns() { |
| 11 | clockid_t clk; |
| 12 | struct timespec ts; |
| 13 | int res = clock_getcpuclockid(pid: getpid(), clock_id: &clk); |
| 14 | assert(!res); |
| 15 | res = clock_gettime(clock_id: clk, tp: &ts); |
| 16 | assert(!res); |
| 17 | return ts.tv_nsec; |
| 18 | } |
| 19 | |
| 20 | long th_cpu_ns() { |
| 21 | clockid_t clk; |
| 22 | struct timespec ts; |
| 23 | int res = pthread_getcpuclockid(thread_id: pthread_self(), clock_id: &clk); |
| 24 | assert(!res); |
| 25 | res = clock_gettime(clock_id: clk, tp: &ts); |
| 26 | assert(!res); |
| 27 | return ts.tv_nsec; |
| 28 | } |
| 29 | |
| 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 |
