-
Notifications
You must be signed in to change notification settings - Fork 287
/
timespec.h
207 lines (177 loc) · 6.42 KB
/
timespec.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __TIMESPEC_UTIL_H__
#define __TIMESPEC_UTIL_H__
#include <time.h>
#include <stdint.h>
#include <stdio.h>
#include "logging.h"
/**
* Retrieve a timestamp of the current system time.
* @ingroup time
*/
inline void timestamp( timespec* timestampOut ) { if(!timestampOut) return; timestampOut->tv_sec=0; timestampOut->tv_nsec=0; clock_gettime(CLOCK_REALTIME, timestampOut); }
/**
* Retrieve a timestamp of the current system time.
* @ingroup time
*/
inline timespec timestamp() { timespec t; timestamp(&t); return t; }
/**
* Return a blank timespec that's been zero'd.
* @ingroup time
*/
inline timespec timeZero() { timespec t; t.tv_sec=0; t.tv_nsec=0; return t; }
/**
* Return an initialized `timespec`
* @ingroup time
*/
inline timespec timeNew( time_t seconds, long int nanoseconds ) { timespec t; t.tv_sec=seconds; t.tv_nsec=nanoseconds; return t; }
/**
* Return an initialized `timespec`
* @ingroup time
*/
inline timespec timeNew( long int nanoseconds ) { const time_t sec=nanoseconds/1e+9; return timeNew(sec, nanoseconds-sec*1e+9); }
/**
* Add two times together.
* @ingroup time
*/
inline timespec timeAdd( const timespec& a, const timespec& b ) { timespec t; t.tv_sec=a.tv_sec+b.tv_sec; t.tv_nsec=a.tv_nsec+b.tv_nsec; const time_t sec=t.tv_nsec/1e+9; t.tv_sec+=sec; t.tv_nsec-=sec*1e+9; return t; }
/**
* Find the difference between two timestamps.
* @ingroup time
*/
inline void timeDiff( const timespec& start, const timespec& end, timespec* result )
{
if ((end.tv_nsec-start.tv_nsec)<0) {
result->tv_sec = end.tv_sec-start.tv_sec-1;
result->tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
result->tv_sec = end.tv_sec-start.tv_sec;
result->tv_nsec = end.tv_nsec-start.tv_nsec;
}
}
/**
* Find the difference between two timestamps.
* @ingroup time
*/
inline timespec timeDiff( const timespec& start, const timespec& end )
{
timespec result;
timeDiff(start, end, &result);
return result;
}
/**
* Compare two timestamps.
*
* @return 0, if timestamp A equals timestamp B
* >0, if timestamp A is greater than timestamp B
* <0, if timestamp A is less than timestamp B
*
* @ingroup time
*/
inline int timeCmp( const timespec& a, const timespec& b )
{
if( a.tv_sec < b.tv_sec )
return -1;
else if( a.tv_sec > b.tv_sec )
return 1;
else
{
if( a.tv_nsec < b.tv_nsec )
return -1;
else if( a.tv_nsec > b.tv_nsec )
return 1;
else
return 0;
}
}
/**
* @internal Reference timestamp of when the process started.
* @ingroup time
*/
extern const timespec __apptime_begin__;
/**
* Retrieve the elapsed time since the process started.
* @ingroup time
*/
inline void apptime( timespec* a ) { timespec t; timestamp(&t); timeDiff(__apptime_begin__, t, a); }
/**
* Retrieve the elapsed time since the process started (in nanoseconds).
* @ingroup time
*/
inline uint64_t apptime_nano() { timespec t; apptime(&t); return (uint64_t)t.tv_sec * uint64_t(1000000000) + (uint64_t)t.tv_nsec; }
/**
* Retrieve the elapsed time since the process started (in seconds).
* @ingroup time
*/
inline float apptime() { timespec t; apptime(&t); return t.tv_sec + t.tv_nsec * 0.000000001f; }
/**
* Convert to 32-bit float (in milliseconds).
* @ingroup time
*/
inline float timeFloat( const timespec& a ) { return a.tv_sec * 1000.0f + a.tv_nsec * 0.000001f; }
/**
* Convert to 64-bit double (in milliseconds).
* @ingroup time
*/
inline double timeDouble( const timespec& a ) { return a.tv_sec * 1000.0 + a.tv_nsec * 0.000001; }
/**
* Get current timestamp as 64-bit double (in milliseconds).
* @ingroup time
*/
inline double timeDouble() { return timeDouble(timestamp()); }
/**
* Produce a text representation of the timestamp.
* @ingroup time
*/
inline char* timeStr( const timespec& timestamp, char* strOut ) { sprintf(strOut, "%lus + %010luns", (uint64_t)timestamp.tv_sec, (uint64_t)timestamp.tv_nsec); return strOut; }
/**
* Print the time to stdout.
* @ingroup time
*/
inline void timePrint( const timespec& timestamp, const char* text=NULL ) { LogInfo("%s %lus + %010luns\n", text, (uint64_t)timestamp.tv_sec, (uint64_t)timestamp.tv_nsec); }
/**
* Put the current thread to sleep for a specified time.
* @ingroup time
*/
inline void sleepTime( const timespec& duration ) { nanosleep(&duration, NULL); }
/**
* Put the current thread to sleep for a specified time.
* @ingroup time
*/
inline void sleepTime( time_t seconds, long int nanoseconds ) { sleepTime(timeNew(seconds,nanoseconds)); }
/**
* Put the current thread to sleep for a specified number of milliseconds.
* @ingroup time
*/
inline void sleepMs( uint64_t milliseconds ) { sleepTime(timeNew(0, milliseconds * 1000 * 1000)); }
/**
* Put the current thread to sleep for a specified number of microseconds.
* @ingroup time
*/
inline void sleepUs( uint64_t microseconds ) { sleepTime(timeNew(0, microseconds * 1000)); }
/**
* Put the current thread to sleep for a specified number of nanoseconds.
* @ingroup time
*/
inline void sleepNs( uint64_t nanoseconds ) { sleepTime(timeNew(0, nanoseconds)); }
#endif