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 
28 #include <stdint.h>
29 #include <stdio.h>
30 
31 
36 inline void timestamp( timespec* timestampOut ) { if(!timestampOut) return; timestampOut->tv_sec=0; timestampOut->tv_nsec=0; clock_gettime(CLOCK_REALTIME, timestampOut); }
37 
38 
43 inline timespec timestamp() { timespec t; timestamp(&t); return t; }
44 
45 
50 inline timespec timeZero() { timespec t; t.tv_sec=0; t.tv_nsec=0; return t; }
51 
52 
57 inline timespec timeNew( time_t seconds, long int nanoseconds ) { timespec t; t.tv_sec=seconds; t.tv_nsec=nanoseconds; return t; }
58 
59 
64 inline timespec timeNew( long int nanoseconds ) { const time_t sec=nanoseconds/1e-9; return timeNew(sec, nanoseconds-sec*1e-9); }
65 
66 
71 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; }
72 
73 
78 inline void timeDiff( const timespec& start, const timespec& end, timespec* result )
79 {
80  if ((end.tv_nsec-start.tv_nsec)<0) {
81  result->tv_sec = end.tv_sec-start.tv_sec-1;
82  result->tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
83  } else {
84  result->tv_sec = end.tv_sec-start.tv_sec;
85  result->tv_nsec = end.tv_nsec-start.tv_nsec;
86  }
87 }
88 
89 
94 inline timespec timeDiff( const timespec& start, const timespec& end )
95 {
96  timespec result;
97  timeDiff(start, end, &result);
98  return result;
99 }
100 
101 
111 inline int timeCmp( const timespec& a, const timespec& b )
112 {
113  if( a.tv_sec < b.tv_sec )
114  return -1;
115  else if( a.tv_sec > b.tv_sec )
116  return 1;
117  else
118  {
119  if( a.tv_nsec < b.tv_nsec )
120  return -1;
121  else if( a.tv_nsec > b.tv_nsec )
122  return 1;
123  else
124  return 0;
125  }
126 }
127 
132 inline float timeFloat( const timespec& a ) { return a.tv_sec * 1000.0f + a.tv_nsec * 0.000001f; }
133 
134 
139 inline float timeDouble( const timespec& a ) { return a.tv_sec * 1000.0 + a.tv_nsec * 0.000001; }
140 
141 
146 inline char* timeStr( const timespec& timestamp, char* strOut ) { sprintf(strOut, "%lu s %lu ns", (uint64_t)timestamp.tv_sec, (uint64_t)timestamp.tv_nsec); return strOut; }
147 
148 
153 inline void timePrint( const timespec& timestamp, const char* text=NULL ) { printf("%s %lus %010luns\n", text, (uint64_t)timestamp.tv_sec, (uint64_t)timestamp.tv_nsec); }
154 
155 
160 inline void sleepTime( const timespec& duration ) { nanosleep(&duration, NULL); }
161 
162 
167 inline void sleepTime( time_t seconds, long int nanoseconds ) { sleepTime(timeNew(seconds,nanoseconds)); }
168 
169 
174 inline void sleepMs( uint64_t milliseconds ) { sleepTime(timeNew(0, milliseconds * 1000 * 1000)); }
175 
176 
181 inline void sleepUs( uint64_t microseconds ) { sleepTime(timeNew(0, microseconds * 1000)); }
182 
183 
188 inline void sleepNs( uint64_t nanoseconds ) { sleepTime(timeNew(0, nanoseconds)); }
189 
190 
191 #endif
float timeFloat(const timespec &a)
Convert to 32-bit float (in milliseconds).
Definition: timespec.h:132
void sleepUs(uint64_t microseconds)
Put the current thread to sleep for a specified number of microseconds.
Definition: timespec.h:181
float timeDouble(const timespec &a)
Convert to 64-bit double (in milliseconds).
Definition: timespec.h:139
void timePrint(const timespec &timestamp, const char *text=NULL)
Print the time to stdout.
Definition: timespec.h:153
void timestamp(timespec *timestampOut)
Retrieve a timestamp of the current system time.
Definition: timespec.h:36
void sleepMs(uint64_t milliseconds)
Put the current thread to sleep for a specified number of milliseconds.
Definition: timespec.h:174
void timeDiff(const timespec &start, const timespec &end, timespec *result)
Find the difference between two timestamps.
Definition: timespec.h:78
void sleepNs(uint64_t nanoseconds)
Put the current thread to sleep for a specified number of nanoseconds.
Definition: timespec.h:188
timespec timeAdd(const timespec &a, const timespec &b)
Add two times together.
Definition: timespec.h:71
timespec timeNew(time_t seconds, long int nanoseconds)
Return an initialized timespec
Definition: timespec.h:57
void sleepTime(const timespec &duration)
Put the current thread to sleep for a specified time.
Definition: timespec.h:160
int timeCmp(const timespec &a, const timespec &b)
Compare two timestamps.
Definition: timespec.h:111
timespec timeZero()
Return a blank timespec that&#39;s been zero&#39;d.
Definition: timespec.h:50
char * timeStr(const timespec &timestamp, char *strOut)
Produce a text representation of the timestamp.
Definition: timespec.h:146