Skip to content

Commit 8aff385

Browse files
authored
udpate_now() to force update now (#473)
* udpate_now() to force update `now` and make it compilable under macos + arm
1 parent 76e032d commit 8aff385

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ elseif (${ARCH} STREQUAL aarch64)
7171
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=generic+crc -fsigned-char -fno-stack-protector -fomit-frame-pointer")
7272
endif ()
7373

74-
check_cxx_compiler_flag(-mcrc32 COMPILER_HAS_MCRC32_FLAG)
75-
if (COMPILER_HAS_MCRC32_FLAG)
76-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcrc32")
74+
if (${ARCH} STREQUAL x86_64)
75+
check_cxx_compiler_flag(-mcrc32 COMPILER_HAS_MCRC32_FLAG)
76+
if (COMPILER_HAS_MCRC32_FLAG)
77+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcrc32")
78+
endif ()
7779
endif ()
7880

7981
set(CMAKE_C_FLAGS ${CMAKE_CXX_FLAGS})

common/alog.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void LogFormatter::put_integer_dec(ALogBuffer& buf, ALogInteger x)
206206
__attribute__((constructor)) static void __initial_timezone() { tzset(); }
207207
static time_t dayid = 0;
208208
static struct tm alog_time = {0};
209-
struct tm* alog_update_time(time_t now)
209+
static struct tm* alog_update_time(time_t now)
210210
{
211211
auto now0 = now;
212212
int sec = now % 60; now /= 60;
@@ -478,36 +478,22 @@ int log_output_file_close() {
478478
return 0;
479479
}
480480

481-
namespace photon
482-
{
483-
struct thread;
484-
extern __thread thread* CURRENT;
485-
}
486-
487-
static inline ALogInteger DEC_W2P0(uint64_t x)
488-
{
489-
return DEC(x).width(2).padding('0');
490-
}
491-
492-
namespace photon {
493-
struct timeval alog_update_now();
494-
}
495-
496481
LogBuffer& operator << (LogBuffer& log, const Prologue& pro)
497482
{
498483
#ifdef LOG_BENCHMARK
499484
auto t = &alog_time;
500485
#else
501-
auto ts = photon::alog_update_now();
502-
auto t = alog_update_time(ts.tv_sec - timezone);
486+
auto ts = photon::__update_now();
487+
auto t = alog_update_time(ts.sec() - timezone);
503488
#endif
489+
#define DEC_W2P0(x) DEC(x).width(2).padding('0')
504490
log.printf(t->tm_year, '/');
505491
log.printf(DEC_W2P0(t->tm_mon), '/');
506492
log.printf(DEC_W2P0(t->tm_mday), ' ');
507493
log.printf(DEC_W2P0(t->tm_hour), ':');
508494
log.printf(DEC_W2P0(t->tm_min), ':');
509495
log.printf(DEC_W2P0(t->tm_sec), '.');
510-
log.printf(DEC(ts.tv_usec).width(6).padding('0'));
496+
log.printf(DEC(ts.usec()).width(6).padding('0'));
511497

512498
static const char levels[] = "|DEBUG|th=|INFO |th=|WARN |th=|ERROR|th=|FATAL|th=|TEMP |th=|AUDIT|th=";
513499
log.reserved = pro.level;

thread/thread.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,11 @@ R"(
847847
)"
848848
);
849849

850+
#ifdef __clang__
851+
#pragma GCC diagnostic push
852+
#pragma GCC diagnostic ignored "-Winline-asm"
853+
#endif
854+
850855
inline void switch_context(thread* from, thread* to) {
851856
prepare_switch(from, to);
852857
auto _t_ = to->stack.pointer_ref();
@@ -881,6 +886,9 @@ R"(
881886
"x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16",
882887
"x17", "x18");
883888
}
889+
#ifdef __clang__
890+
#pragma GCC diagnostic pop
891+
#endif
884892

885893
#endif // x86 or arm
886894

@@ -1033,19 +1041,18 @@ R"(
10331041

10341042
volatile uint64_t now;
10351043
static std::atomic<pthread_t> ts_updater(0);
1036-
static inline struct timeval update_now()
1044+
static inline NowTime update_now()
10371045
{
10381046
#if defined(__x86_64__) && defined(__linux__) && defined(ENABLE_MIMIC_VDSO)
10391047
if (likely(__mimic_vdso_time_x86))
10401048
return photon::now = __mimic_vdso_time_x86.get_now();
10411049
#endif
10421050
struct timeval tv;
10431051
gettimeofday(&tv, NULL);
1044-
uint64_t nnow = tv.tv_sec;
1045-
nnow *= 1000 * 1000;
1046-
nnow += tv.tv_usec;
1052+
uint64_t nnow = tv.tv_sec * 1000ul * 1000ul + tv.tv_usec;
10471053
now = nnow;
1048-
return tv;
1054+
assert(tv.tv_sec <= UINT32_MAX && tv.tv_usec < 1000000);
1055+
return {nnow, ((uint64_t)tv.tv_sec << 32) | (uint32_t)tv.tv_usec};
10491056
}
10501057
__attribute__((always_inline))
10511058
static inline uint32_t _rdtsc()
@@ -1089,7 +1096,7 @@ R"(
10891096
update_now();
10901097
}
10911098
}
1092-
struct timeval alog_update_now() {
1099+
NowTime __update_now() {
10931100
last_tsc = _rdtsc();
10941101
return update_now();
10951102
}

thread/thread.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ namespace photon
3636

3737
struct thread;
3838
extern __thread thread* CURRENT;
39-
extern volatile uint64_t now;
39+
extern volatile uint64_t now; // a coarse-grained timestamp in unit of us
40+
struct NowTime {
41+
uint64_t now, _sec_usec;
42+
uint32_t sec() { return _sec_usec >> 32; }
43+
uint32_t usec() { auto p = (uint32_t*)&_sec_usec; return *p; }
44+
};
45+
NowTime __update_now(); // update `now`
4046

4147
enum states
4248
{

0 commit comments

Comments
 (0)