GCS  0.2.3
gu_time.h
1 // Copyright (C) 2008 Codership Oy <info@codership.com>
2 
9 #ifndef _gu_time_h_
10 #define _gu_time_h_
11 
12 #include <sys/time.h>
13 #include <time.h>
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif /* __cplusplus */
18 
20 static inline double
21 gu_timeval_diff (struct timeval* left, struct timeval* right)
22 {
23  register long long diff = left->tv_sec;
24  diff = ((diff - right->tv_sec)*1000000LL) + left->tv_usec - right->tv_usec;
25  return (((double)diff) * 1.0e-06);
26 }
27 
28 static inline void
29 gu_timeval_add (struct timeval* t, double s)
30 {
31  double ret = (double)t->tv_sec + ((double)t->tv_usec) * 1.0e-06 + s;
32 
33  t->tv_sec = (long)ret;
34  t->tv_usec = (long)((ret - (double)t->tv_sec) * 1.0e+06);
35 }
36 
37 static const double SEC_PER_CLOCK = ((double)1.0)/CLOCKS_PER_SEC;
38 
40 static inline double
41 gu_clock_diff (clock_t left, clock_t right)
42 {
43  return ((double)(left - right)) * SEC_PER_CLOCK;
44 }
45 
46 #include "gu_limits.h" // for GU_LONG_LONG_MAX
47 #include <unistd.h>
48 
55 /* Maximum date representable by long long and compatible with timespec */
56 #define GU_TIME_ETERNITY 9223372035999999999LL
57 
58 #if defined(__APPLE__) /* synced with linux/time.h */
59 # define CLOCK_REALTIME 0
60 # define CLOCK_MONOTONIC 1
61 typedef int clockid_t;
62 int clock_gettime (clockid_t clk_id, struct timespec * tp);
63 #endif /* __APPLE__ */
64 
65 static inline long long
66 gu_time_getres()
67 {
68 #if _POSIX_TIMERS > 0
69  struct timespec tmp;
70  clock_getres (CLOCK_REALTIME, &tmp);
71  return ((tmp.tv_sec * 1000000000LL) + tmp.tv_nsec);
72 #else
73  return 1000LL; // assumed resolution of gettimeofday() in nanoseconds
74 #endif
75 }
76 
77 static inline long long
78 gu_time_calendar()
79 {
80 #if _POSIX_TIMERS > 0 || defined(__APPLE__)
81  struct timespec tmp;
82  clock_gettime (CLOCK_REALTIME, &tmp);
83  return ((tmp.tv_sec * 1000000000LL) + tmp.tv_nsec);
84 #else
85  struct timeval tmp;
86  gettimeofday (&tmp, NULL);
87  return ((tmp.tv_sec * 1000000000LL) + (tmp.tv_usec * 1000LL));
88 #endif
89 }
90 
91 static inline long long
92 gu_time_monotonic()
93 {
94 #if defined(_POSIX_MONOTONIC_CLOCK) || defined(__APPLE__)
95  struct timespec tmp;
96  clock_gettime (CLOCK_MONOTONIC, &tmp);
97  return ((tmp.tv_sec * 1000000000LL) + tmp.tv_nsec);
98 #else
99  struct timeval tmp;
100  gettimeofday (&tmp, NULL);
101  return ((tmp.tv_sec * 1000000000LL) + (tmp.tv_usec * 1000LL));
102 #endif
103 }
104 
105 #ifdef CLOCK_PROCESS_CPUTIME_ID
106 static inline long long
107 gu_time_process_cputime()
108 {
109 #if _POSIX_TIMERS > 0
110  struct timespec tmp;
111  clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &tmp);
112  return ((tmp.tv_sec * 1000000000LL) + tmp.tv_nsec);
113 #else
114  return -1;
115 #endif
116 }
117 #endif /* CLOCK_PROCESS_CPUTIME_ID */
118 
119 
120 static inline long long
121 gu_time_thread_cputime()
122 {
123 #if _POSIX_TIMERS > 0
124  struct timespec tmp;
125  clock_gettime (CLOCK_THREAD_CPUTIME_ID, &tmp);
126  return ((tmp.tv_sec * 1000000000LL) + tmp.tv_nsec);
127 #else
128  return -1;
129 #endif
130 }
131 
132 #ifdef __cplusplus
133 }
134 #endif /* __cplusplus */
135 
136 
137 #endif /* _gu_time_h_ */