Jetson Inference
DNN Vision Library
timespec.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __TIMESPEC_UTIL_H__
24 #define __TIMESPEC_UTIL_H__
25 
26 #include <time.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 
30 #include "logging.h"
31 
32 
37 inline void timestamp( timespec* timestampOut ) { if(!timestampOut) return; timestampOut->tv_sec=0; timestampOut->tv_nsec=0; clock_gettime(CLOCK_REALTIME, timestampOut); }
38 
43 inline timespec timestamp() { timespec t; timestamp(&t); return t; }
44 
49 inline timespec timeZero() { timespec t; t.tv_sec=0; t.tv_nsec=0; return t; }
50 
55 inline timespec timeNew( time_t seconds, long int nanoseconds ) { timespec t; t.tv_sec=seconds; t.tv_nsec=nanoseconds; return t; }
56 
61 inline timespec timeNew( long int nanoseconds ) { const time_t sec=nanoseconds/1e+9; return timeNew(sec, nanoseconds-sec*1e+9); }
62 
67 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; }
68 
73 inline void timeDiff( const timespec& start, const timespec& end, timespec* result )
74 {
75  if ((end.tv_nsec-start.tv_nsec)<0) {
76  result->tv_sec = end.tv_sec-start.tv_sec-1;
77  result->tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
78  } else {
79  result->tv_sec = end.tv_sec-start.tv_sec;
80  result->tv_nsec = end.tv_nsec-start.tv_nsec;
81  }
82 }
83 
88 inline timespec timeDiff( const timespec& start, const timespec& end )
89 {
90  timespec result;
91  timeDiff(start, end, &result);
92  return result;
93 }
94 
104 inline int timeCmp( const timespec& a, const timespec& b )
105 {
106  if( a.tv_sec < b.tv_sec )
107  return -1;
108  else if( a.tv_sec > b.tv_sec )
109  return 1;
110  else
111  {
112  if( a.tv_nsec < b.tv_nsec )
113  return -1;
114  else if( a.tv_nsec > b.tv_nsec )
115  return 1;
116  else
117  return 0;
118  }
119 }
120 
125 extern const timespec __apptime_begin__;
126 
131 inline void apptime( timespec* a ) { timespec t; timestamp(&t); timeDiff(__apptime_begin__, t, a); }
132 
137 inline uint64_t apptime_nano() { timespec t; apptime(&t); return (uint64_t)t.tv_sec * uint64_t(1000000000) + (uint64_t)t.tv_nsec; }
138 
143 inline float apptime() { timespec t; apptime(&t); return t.tv_sec + t.tv_nsec * 0.000000001f; }
144 
149 inline float timeFloat( const timespec& a ) { return a.tv_sec * 1000.0f + a.tv_nsec * 0.000001f; }
150 
155 inline double timeDouble( const timespec& a ) { return a.tv_sec * 1000.0 + a.tv_nsec * 0.000001; }
156 
161 inline double timeDouble() { return timeDouble(timestamp()); }
162 
167 inline char* timeStr( const timespec& timestamp, char* strOut ) { sprintf(strOut, "%lus + %010luns", (uint64_t)timestamp.tv_sec, (uint64_t)timestamp.tv_nsec); return strOut; }
168 
173 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); }
174 
179 inline void sleepTime( const timespec& duration ) { nanosleep(&duration, NULL); }
180 
185 inline void sleepTime( time_t seconds, long int nanoseconds ) { sleepTime(timeNew(seconds,nanoseconds)); }
186 
191 inline void sleepMs( uint64_t milliseconds ) { sleepTime(timeNew(0, milliseconds * 1000 * 1000)); }
192 
197 inline void sleepUs( uint64_t microseconds ) { sleepTime(timeNew(0, microseconds * 1000)); }
198 
203 inline void sleepNs( uint64_t nanoseconds ) { sleepTime(timeNew(0, nanoseconds)); }
204 
205 
206 #endif
207 
timeAdd
timespec timeAdd(const timespec &a, const timespec &b)
Add two times together.
Definition: timespec.h:67
timeDouble
double timeDouble(const timespec &a)
Convert to 64-bit double (in milliseconds).
Definition: timespec.h:155
LogInfo
#define LogInfo(format, args...)
Log a printf-style info message (Log::INFO)
Definition: logging.h:168
timeFloat
float timeFloat(const timespec &a)
Convert to 32-bit float (in milliseconds).
Definition: timespec.h:149
timestamp
void timestamp(timespec *timestampOut)
Retrieve a timestamp of the current system time.
Definition: timespec.h:37
timePrint
void timePrint(const timespec &timestamp, const char *text=NULL)
Print the time to stdout.
Definition: timespec.h:173
__apptime_begin__
const timespec __apptime_begin__
timeZero
timespec timeZero()
Return a blank timespec that's been zero'd.
Definition: timespec.h:49
apptime_nano
uint64_t apptime_nano()
Retrieve the elapsed time since the process started (in nanoseconds).
Definition: timespec.h:137
sleepNs
void sleepNs(uint64_t nanoseconds)
Put the current thread to sleep for a specified number of nanoseconds.
Definition: timespec.h:203
timeNew
timespec timeNew(time_t seconds, long int nanoseconds)
Return an initialized timespec
Definition: timespec.h:55
sleepMs
void sleepMs(uint64_t milliseconds)
Put the current thread to sleep for a specified number of milliseconds.
Definition: timespec.h:191
apptime
void apptime(timespec *a)
Retrieve the elapsed time since the process started.
Definition: timespec.h:131
timeCmp
int timeCmp(const timespec &a, const timespec &b)
Compare two timestamps.
Definition: timespec.h:104
logging.h
timeStr
char * timeStr(const timespec &timestamp, char *strOut)
Produce a text representation of the timestamp.
Definition: timespec.h:167
sleepTime
void sleepTime(const timespec &duration)
Put the current thread to sleep for a specified time.
Definition: timespec.h:179
sleepUs
void sleepUs(uint64_t microseconds)
Put the current thread to sleep for a specified number of microseconds.
Definition: timespec.h:197
timeDiff
void timeDiff(const timespec &start, const timespec &end, timespec *result)
Find the difference between two timestamps.
Definition: timespec.h:73