The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

os/time: Add time getting and conversion functions

For Linux it is based on CLOCK_MONOTONIC.

+44
+44
src/xrt/auxiliary/os/os_time.h
··· 15 15 16 16 #ifdef XRT_OS_LINUX 17 17 #include <time.h> 18 + #include <sys/time.h> 18 19 #else 19 20 #error "No time support on non-Linux platforms yet." 20 21 #endif ··· 22 23 #ifdef __cplusplus 23 24 extern "C" { 24 25 #endif 26 + 25 27 26 28 /*! 27 29 * Write an output report to the given device. ··· 37 39 nanosleep(&spec, NULL); 38 40 #endif 39 41 } 42 + 43 + #ifdef XRT_OS_LINUX 44 + /*! 45 + * Convert a timespec struct to nanoseconds. 46 + */ 47 + XRT_MAYBE_UNUSED static inline uint64_t 48 + os_timespec_to_ns(struct timespec *spec) 49 + { 50 + uint64_t ns = 0; 51 + ns += (uint64_t)spec->tv_sec * 1000 * 1000 * 1000; 52 + ns += (uint64_t)spec->tv_nsec; 53 + return ns; 54 + } 55 + 56 + /*! 57 + * Convert a timeval struct to nanoseconds. 58 + */ 59 + XRT_MAYBE_UNUSED static inline uint64_t 60 + os_timeval_to_ns(struct timeval *val) 61 + { 62 + uint64_t ns = 0; 63 + ns += (uint64_t)val->tv_sec * 1000 * 1000 * 1000; 64 + ns += (uint64_t)val->tv_usec * 1000; 65 + return ns; 66 + } 67 + 68 + /*! 69 + * Return a monotonic clock in nanoseconds. 70 + */ 71 + XRT_MAYBE_UNUSED static inline uint64_t 72 + os_monotonic_get_ns(void) 73 + { 74 + struct timespec ts; 75 + int ret = clock_gettime(CLOCK_MONOTONIC, &ts); 76 + if (ret != 0) { 77 + return 0; 78 + } 79 + 80 + return os_timespec_to_ns(&ts); 81 + } 82 + #endif 83 + 40 84 41 85 #ifdef __cplusplus 42 86 }