Go to the documentation of this file.
2 #define _TICK_TIME_H_ "$Id: tick_time.h 421 2020-07-14 17:11:44Z fargos $"
9 #if defined(_MSC_VER) || (__MINGW32__ == 1)
16 #ifndef CLOCK_REALTIME
17 #if (_MSC_VER >= 1900) || (__MINGW32__ == 1)
26 #define CLOCK_REALTIME 1
42 #define USE_INTERPOLATED_TIME 1
50 #define get_CPU_ticks() __rdtsc()
53 #if defined(__x86_64__) || defined(__i386__)
56 #define rdtscll(val) do { \
58 __asm__ __volatile__("rdtsc" : "=a" (__a), "=d" (__d)); \
59 (val) = ((uint64_t)__a) | (((uint64_t)__d)<<32); \
62 #elif defined(__sparc__)
64 inline uint64_t readSPARCtick()
66 unsigned long long result;
73 inline uint64_t readSPARCtick()
75 unsigned long long result;
84 #elif defined(__s390__) // Covers both s390 and s390x.
85 inline uint64_t readS390timestamp()
89 asm(
"stck %0" :
"=Q"(tsc) : :
"cc");
92 #elif defined(__aarch64__) // 64-bit ARM
93 inline uint64_t readAARCH64timer()
99 int64_t virtual_timer_value;
100 asm volatile(
"mrs %0, cntvct_el0" :
"=r"(virtual_timer_value));
101 return (virtual_timer_value);
103 #elif defined(__ARM_ARCH)
106 #if (__ARM_ARCH >= 6)
107 inline uint64_t readARMperfCounter()
113 asm volatile(
"mrc p15, 0, %0, c9, c14, 0" :
"=r"(pmuseren));
115 asm volatile(
"mrc p15, 0, %0, c9, c12, 1" :
"=r"(pmcntenset));
116 if (pmcntenset & 0x80000000ul) {
117 asm volatile(
"mrc p15, 0, %0, c9, c13, 0" :
"=r"(pmccntr));
119 return static_cast<int64_t
>(pmccntr) * 64;
125 #elif defined(__linux__)
127 #include <linux/perf_event.h>
128 #include <asm/unistd.h>
129 #include <sys/ioctl.h>
131 extern int initPerfEvent();
132 extern uint64_t getCPUcyclesPerfEvent();
133 extern int perfEvent_FD;
145 #if defined(__i386__) || defined(__x86_64__)
147 #elif defined(__sparc__)
148 result = readSPARCtick();
149 #elif defined(_WIN32)
151 #elif defined(__s390__)
152 result = readS390timestamp();
153 #elif defined(__aarch64__) // 64-bit ARM
154 result = readAARCH64timer();
155 #elif defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_8A)
156 result = readARMperfCounter();
157 #elif defined(__linux__)
158 # warning "CPU type not yet supported by OMEgetCPUticks()\n";
159 if (perfEvent_FD == -1) {
160 int rc = initPerfEvent();
166 result = getCPUcyclesPerfEvent();
169 asm volatile(
"mrc p15, 0, %0, c9, c13, 0" :
"=r"(r));
173 # error "CPU type not supported by get_CPU_ticks()\n";
223 uint64_t get_clock_rate_estimate(
bool setGlobalRate);
286 #if USE_INTERPOLATED_TIME == 1
300 #define clock_gettime_via_tick(r,f) clock_gettime(CLOCK_REALTIME, r)
int clock_gettime_via_tick(struct timespec *result, bool force) OME_ALWAYS_INLINE OME_ALWAYS_OPTIMIZE("-O3")
Definition: tick_time.h:290
ClockProfileState
Definition: tick_time.cpp:214
@ CLOCK_NOT_PROFILED
Definition: tick_time.cpp:214
void set_max_clock_sample_interval(uint64_t nanoseconds)
Set maximum number of nanoseconds between forced retrieval of time from hardware clock when clock_get...
Definition: tick_time.cpp:238
const char s[]
Definition: t.cpp:4
int(* Clock_GetTime_InterfaceFP)(struct timespec *result, bool force)
Defines the interface for clock_gettime_via_tick() or equivalent.
Definition: tick_time.h:239
#define stdout
Definition: tmp.o.cpp:3117
#define CLOCK_REALTIME
Definition: tmp.o.cpp:50
#define stderr
Definition: tmp.o.cpp:3115
uint64_t profile_clock_rate(bool setGlobalRate)
Normal initialization routine to determine tick rate of underlying hardware.
Definition: tick_time.cpp:258
@ CLOCK_SECOND_SAMPLE
Definition: tick_time.cpp:215
#define OME_ALWAYS_OPTIMIZE(level)
Mark a function to be compiled with a specific level of optimization.
Definition: compiler_hints.h:406
struct timespec first_sample_time
Definition: tick_time.cpp:222
void set_tick_rate(double ticksPerNanosecond)
Manually set CPU tick rate per nanoseconds.
Definition: tick_time.cpp:226
int simulated_clock_gettime(struct timespec *result, bool force)
Alternate of interpolated_clock_gettime() that can use an external RDTSC counter.
Definition: tick_time.cpp:377
@ CLOCK_PROFILE_COMPLETE
Definition: tick_time.cpp:215
struct timespec last_clock_gettime
Definition: tick_time.cpp:224
const char srcID[]
Definition: catSym.c:17
#define NANOSECONDS_IN_1_SEC
Definition: tick_time.cpp:27
#define ENOENT
Definition: tmp.o.cpp:114
#define OME_EXPECT_TRUE(expr)
Annotation macro for conditional expression expected to be true.
Definition: compiler_hints.h:541
@ CLOCK_FIRST_SAMPLE
Definition: tick_time.cpp:214
void set_external_rdtsc_variable(uint64_t *rdtsc_ptr)
Sets the storage location from which the current time in nanoseconds will be read.
Definition: tick_time.cpp:390
Clock_GetTime_InterfaceFP set_clock_function(Clock_GetTime_InterfaceFP func)
Set clock routine implementation.
Definition: tick_time.cpp:396
const char srcID[] OME_USED
Definition: tick_time.cpp:24
Clock_GetTime_InterfaceFP clockRoutine_function
Definition: tick_time.cpp:388
int interpolated_clock_gettime(struct timespec *result, bool force)
Get time with nanosecond precision using interpolation of elapsed CPU ticks.
Definition: tick_time.cpp:315
@ CLOCK_RATE_ESTIMATED
Definition: tick_time.cpp:215
int errno
Definition: ethers.c:41
Compiler-specific macros to provide performance-related hints.
#define OME_EXPECT_FALSE(expr)
Annotation macro for conditional expression expected to be false.
Definition: compiler_hints.h:540
#define OME_ALWAYS_INLINE
Tell the compiler to alway inline a function, regardless of optimization level.
Definition: compiler_hints.h:364
uint64_t get_CPU_ticks() OME_ALWAYS_INLINE OME_ALWAYS_OPTIMIZE("-O3")
Return value of CPU tick register.
Definition: tick_time.h:141
FARGOS CPU tick routines.
unsigned __int64 rdtsc()
Definition: IAPerf.h:101
int main(int argc, const char *argv[])
Definition: tick_test.cpp:5
int fd
Definition: ethers.c:41