1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include <pthread.h>
6
7 #define assert(_Expression) (void)( (!!(_Expression)) || (_my_assert(#_Expression, __FILE__, __LINE__), 0) )
8
_my_assert(char * message,char * file,unsigned int line)9 static __inline void _my_assert(char *message, char *file, unsigned int line)
10 {
11 fprintf(stderr, "Assertion failed: %s , file %s, line %u\n", message, file, line);
12 exit(1);
13 }
14
test_clock_gettime()15 void test_clock_gettime()
16 {
17 int rc;
18 struct timespec tp, request = { 1, 0 }, remain;
19
20 rc = clock_gettime(CLOCK_REALTIME, &tp);
21 assert(rc == 0);
22 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_REALTIME)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
23
24 rc = clock_gettime(CLOCK_MONOTONIC, &tp);
25 assert(rc == 0);
26 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_MONOTONIC)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
27
28 rc = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
29 assert(rc == 0);
30 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_PROCESS_CPUTIME_ID)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
31
32 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp);
33 assert(rc == 0);
34 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_THREAD_CPUTIME_ID)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
35
36 rc = clock_nanosleep(CLOCK_REALTIME, 0, &request, &remain);
37 assert(rc == 0);
38
39 rc = clock_gettime(CLOCK_REALTIME, &tp);
40 assert(rc == 0);
41 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_REALTIME)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
42
43 rc = clock_gettime(CLOCK_MONOTONIC, &tp);
44 assert(rc == 0);
45 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_MONOTONIC)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
46
47 rc = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
48 assert(rc == 0);
49 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_PROCESS_CPUTIME_ID)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
50
51 rc = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp);
52 assert(rc == 0);
53 printf("[%10"PRId64".%09d] clock_gettime (CLOCK_THREAD_CPUTIME_ID)\n", (__int64) tp.tv_sec, (int) tp.tv_nsec);
54
55 }
main(int argc,char * argv[])56 int main(int argc, char *argv[])
57 {
58 test_clock_gettime();
59
60 return 0;
61 }
62