• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <pthread.h>
5 
6 #define POW10_9                 1000000000
7 
8 #define assert(_Expression) (void)( (!!(_Expression)) || (_my_assert(#_Expression, __FILE__, __LINE__), 0) )
9 
_my_assert(char * message,char * file,unsigned int line)10 static __inline void _my_assert(char *message, char *file, unsigned int line)
11 {
12     fprintf(stderr, "Assertion failed: %s , file %s, line %u\n", message, file, line);
13     exit(1);
14 }
15 
sub_and_div(const struct timespec * t1,const struct timespec * t2,const struct timespec * r)16 double sub_and_div(const struct timespec *t1, const struct timespec *t2, const struct timespec *r)
17 {
18     __int64 diff = (t2->tv_sec - t1->tv_sec) * POW10_9 + (t2->tv_nsec - t1->tv_nsec);
19     return diff / (double) (r->tv_sec * POW10_9 + r->tv_nsec);
20 }
21 
test_clock_getres(char * name,int id)22 void test_clock_getres(char *name, int id)
23 {
24     int rc;
25     double d;
26     struct timespec tp, t1, t2;
27 
28     rc = clock_getres(id, &tp);
29     assert(rc == 0);
30     printf("%s resolution: %d.%09d sec\n", name, (int) tp.tv_sec, (int) tp.tv_nsec);
31 
32     rc = clock_gettime(id, &t1);
33     assert(rc == 0);
34     printf("%s time: %d.%09d sec\n", name, (int) t1.tv_sec, (int) t1.tv_nsec);
35 
36     if (id == CLOCK_REALTIME || id == CLOCK_MONOTONIC) {
37         struct timespec request = {1, 0};
38         clock_nanosleep(CLOCK_REALTIME, 0, &request, NULL);
39     } else {
40         long i;
41         for (i = 0; i < 100000000; i++) {
42             rand();
43         }
44     }
45 
46     rc = clock_gettime(id, &t2);
47     assert(rc == 0);
48     printf("%s time: %d.%09d sec\n", name, (int) t2.tv_sec, (int) t2.tv_nsec);
49 
50     d = sub_and_div(&t1, &t2, &tp);
51     printf("sub_and_div: %7.3lf\n", d);
52     printf("\n");
53 }
54 
main(int argc,char * argv[])55 int main(int argc, char *argv[])
56 {
57     test_clock_getres("          CLOCK_REALTIME", CLOCK_REALTIME);
58     test_clock_getres("         CLOCK_MONOTONIC", CLOCK_MONOTONIC);
59     test_clock_getres("CLOCK_PROCESS_CPUTIME_ID", CLOCK_PROCESS_CPUTIME_ID);
60     test_clock_getres(" CLOCK_THREAD_CPUTIME_ID", CLOCK_THREAD_CPUTIME_ID);
61 
62     return 0;
63 }
64