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 float apptime() { timespec t; apptime(&t); return t.tv_sec + t.tv_nsec * 0.000000001f; }
138 
143 inline float timeFloat( const timespec& a ) { return a.tv_sec * 1000.0f + a.tv_nsec * 0.000001f; }
144 
149 inline double timeDouble( const timespec& a ) { return a.tv_sec * 1000.0 + a.tv_nsec * 0.000001; }
150 
155 inline double timeDouble() { return timeDouble(timestamp()); }
156 
161 inline char* timeStr( const timespec& timestamp, char* strOut ) { sprintf(strOut, "%lus + %010luns", (uint64_t)timestamp.tv_sec, (uint64_t)timestamp.tv_nsec); return strOut; }
162 
167 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); }
168 
173 inline void sleepTime( const timespec& duration ) { nanosleep(&duration, NULL); }
174 
179 inline void sleepTime( time_t seconds, long int nanoseconds ) { sleepTime(timeNew(seconds,nanoseconds)); }
180 
185 inline void sleepMs( uint64_t milliseconds ) { sleepTime(timeNew(0, milliseconds * 1000 * 1000)); }
186 
191 inline void sleepUs( uint64_t microseconds ) { sleepTime(timeNew(0, microseconds * 1000)); }
192 
197 inline void sleepNs( uint64_t nanoseconds ) { sleepTime(timeNew(0, nanoseconds)); }
198 
199 
200 #endif
201 
float timeFloat(const timespec &a)
Convert to 32-bit float (in milliseconds).
Definition: timespec.h:143
void sleepUs(uint64_t microseconds)
Put the current thread to sleep for a specified number of microseconds.
Definition: timespec.h:191
void timePrint(const timespec &timestamp, const char *text=NULL)
Print the time to stdout.
Definition: timespec.h:167
#define LogInfo(format, args...)
Log a printf-style info message (Log::INFO)
Definition: logging.h:168
void timestamp(timespec *timestampOut)
Retrieve a timestamp of the current system time.
Definition: timespec.h:37
const timespec __apptime_begin__
void sleepMs(uint64_t milliseconds)
Put the current thread to sleep for a specified number of milliseconds.
Definition: timespec.h:185
void timeDiff(const timespec &start, const timespec &end, timespec *result)
Find the difference between two timestamps.
Definition: timespec.h:73
void sleepNs(uint64_t nanoseconds)
Put the current thread to sleep for a specified number of nanoseconds.
Definition: timespec.h:197
timespec timeAdd(const timespec &a, const timespec &b)
Add two times together.
Definition: timespec.h:67
timespec timeNew(time_t seconds, long int nanoseconds)
Return an initialized timespec
Definition: timespec.h:55
void apptime(timespec *a)
Retrieve the elapsed time since the process started.
Definition: timespec.h:131
double timeDouble(const timespec &a)
Convert to 64-bit double (in milliseconds).
Definition: timespec.h:149
void sleepTime(const timespec &duration)
Put the current thread to sleep for a specified time.
Definition: timespec.h:173
int timeCmp(const timespec &a, const timespec &b)
Compare two timestamps.
Definition: timespec.h:104
timespec timeZero()
Return a blank timespec that&#39;s been zero&#39;d.
Definition: timespec.h:49
char * timeStr(const timespec &timestamp, char *strOut)
Produce a text representation of the timestamp.
Definition: timespec.h:161