modified: n.fq
[GalaxyCodeBases.git] / c_cpp / coverages / timer.h
blob8adb6d7a590a78e42b59af27802985fa4c5f21ad
1 // by Hu Xuesong
2 #ifndef _G_TIMER_H
3 #define _G_TIMER_H
5 #include <sys/time.h> //getrusage, gettimeofday
6 #include <sys/resource.h>
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
12 struct timeval __g_timeofday_start, __g_timeofday_end, __g_timeofday_diff,\
13 __g_resource_usage_tmp,__g_resource_usage_other_time;
14 struct rusage __g_resource_usage;
16 #define G_TIMER_START \
17 gettimeofday(&__g_timeofday_start,NULL)
19 #define G_TIMER_END \
20 do {\
21 getrusage(RUSAGE_SELF, &__g_resource_usage);\
22 gettimeofday(&__g_timeofday_end,NULL);\
23 timersub(&__g_timeofday_end,&__g_timeofday_start,&__g_timeofday_diff);\
24 timersub(&__g_timeofday_diff,&__g_resource_usage.ru_utime,&__g_resource_usage_tmp);\
25 timersub(&__g_resource_usage_tmp,&__g_resource_usage.ru_stime,&__g_resource_usage_other_time);\
26 } while (0)
28 #define __G_TIMER_PRINT(__sustype__) \
29 fprintf(stderr,"\n--------------------------------------------------------------------------------\n"\
30 "Resource Usage Measures:\n"\
31 " User: %ld."__sustype__"(s), System: %ld."__sustype__"(s). Real: %ld."__sustype__"(s).\n"\
32 " Sleep: %ld."__sustype__"(s). Block I/O times: %ld/%ld. MaxRSS: %ld kiB.\n"\
33 " Wait(s): %ld(nvcsw) + %ld(nivcsw). "\
34 "Page Fault(s): %ld(minflt) + %ld(majflt).\n",\
35 __g_resource_usage.ru_utime.tv_sec, __g_resource_usage.ru_utime.tv_usec,\
36 __g_resource_usage.ru_stime.tv_sec, __g_resource_usage.ru_stime.tv_usec,\
37 __g_timeofday_diff.tv_sec, __g_timeofday_diff.tv_usec,\
38 __g_resource_usage_other_time.tv_sec, __g_resource_usage_other_time.tv_usec,\
39 __g_resource_usage.ru_inblock, __g_resource_usage.ru_oublock,\
40 __g_resource_usage.ru_maxrss,\
41 __g_resource_usage.ru_nvcsw, __g_resource_usage.ru_nivcsw,\
42 __g_resource_usage.ru_minflt, __g_resource_usage.ru_majflt)
44 #ifdef __linux__
45 #define G_TIMER_PRINT __G_TIMER_PRINT("%06ld")
46 #elif __APPLE__
47 #define G_TIMER_PRINT __G_TIMER_PRINT("%06d")
48 #else
49 # error "Unknown compiler !"
50 #endif
52 #ifdef __cplusplus
54 #endif
56 #endif /* timer.h */