1
#include "util/easy_time.h"
10
int easy_localtime(const time_t *t, struct tm *tp)
12
static const unsigned short int mon_yday[2][13] = {
14
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
16
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
19
#define SECS_PER_HOUR (60 * 60)
20
#define SECS_PER_DAY (SECS_PER_HOUR * 24)
21
#define ISLEAP(year) ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
22
#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
23
#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
25
long int days, rem, y;
26
const unsigned short int *ip;
28
days = *t / SECS_PER_DAY;
29
rem = *t % SECS_PER_DAY;
37
while (rem >= SECS_PER_DAY) {
42
tp->tm_hour = rem / SECS_PER_HOUR;
44
tp->tm_min = rem / 60;
45
tp->tm_sec = rem % 60;
47
tp->tm_wday = (4 + days) % 7;
54
while (days < 0 || days >= (ISLEAP(y) ? 366 : 365)) {
56
long int yg = y + days / 365 - (days % 365 < 0);
59
days -= ((yg - y) * 365
60
+ LEAPS_THRU_END_OF(yg - 1)
61
- LEAPS_THRU_END_OF(y - 1));
65
tp->tm_year = y - 1900;
67
if (tp->tm_year != y - 1900) {
72
ip = mon_yday[ISLEAP(y)];
74
for (y = 11; days < (long int) ip[y]; --y)
79
tp->tm_mday = days + 1;
83
void __attribute__((constructor)) easy_time_start_()
88
int64_t easy_time_now()
92
return __INT64_C(1000000) * tv.tv_sec + tv.tv_usec;
96
int is_support_invariant_tsc()
100
#if defined(__x86_64__)
101
unsigned int cpu_info[4];
103
__get_cpuid(0x80000007, cpu_info, cpu_info + 1, cpu_info + 2, cpu_info + 3);
104
if (cpu_info[3] & 0x100) {
114
typedef struct ObTscTimestampStruct {
122
__thread ObTscTimestamp tsc_obj;
124
#if defined(__x86_64__)
125
static __inline__ uint64_t rdtsc()
128
__asm__ __volatile__("rdtsc" : "=a"(rax), "=d"(rdx) :: "%rcx");
129
return (rdx << 32) + rax;
132
static __inline__ uint64_t rdtscp()
135
__asm__ __volatile__("rdtscp" : "=a"(rax), "=d"(rdx) :: "%rcx");
136
return (rdx << 32) + rax;
139
static __inline__ uint64_t rdtscp()
141
int64_t virtual_timer_value;
142
asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
143
return virtual_timer_value;
145
static __inline__ uint64_t rdtsc()
152
#if defined(__x86_64__)
154
uint64_t get_cpufreq_khz()
158
double freq_mhz = 0.0;
159
uint64_t freq_khz = 0;
161
stream = fopen("/proc/cpuinfo", "r");
162
if (NULL == stream) {
164
while (fgets(line, sizeof(line), stream)) {
165
if (sscanf(line, "cpu MHz\t: %lf", &freq_mhz) == 1) {
166
freq_khz = (uint64_t)(freq_mhz * 1000UL);
176
uint64_t get_cpufreq_khz(void)
178
uint64_t timer_frequency;
179
asm volatile("mrs %0, cntfrq_el0":"=r"(timer_frequency));
180
return timer_frequency / 1000;
185
void build_tsc_timestamp()
187
if (!tsc_obj.is_inited) {
188
if (is_support_invariant_tsc()) {
189
int64_t cpu_freq_khz = get_cpufreq_khz();
191
gettimeofday(&tv, NULL);
192
tsc_obj.start_us = tv.tv_sec * 1000000 + tv.tv_usec;
193
tsc_obj.tsc_count = rdtscp();
194
if (tsc_obj.tsc_count > 0 && cpu_freq_khz > 0) {
195
tsc_obj.scale = (1000 << 20) / cpu_freq_khz;
200
tsc_obj.is_inited = 1;
204
#define LIKELY(x) __builtin_expect(!!(x),!!1)
206
int64_t fast_current_time()
208
if (LIKELY(tsc_obj.use_tsc)) {
209
uint64_t current_tsc = rdtsc();
210
return ((current_tsc - tsc_obj.tsc_count) * tsc_obj.scale >> 20) + tsc_obj.start_us;
212
if (!tsc_obj.is_inited) {
213
build_tsc_timestamp();
216
gettimeofday(&tv, NULL);
217
return tv.tv_sec * 1000000 + tv.tv_usec;