Skip to content

Commit

Permalink
udpate_now() to force update now (#473)
Browse files Browse the repository at this point in the history
* udpate_now() to force update `now`

and make it compilable under macos + arm
  • Loading branch information
lihuiba authored May 9, 2024
1 parent 76e032d commit 8aff385
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ elseif (${ARCH} STREQUAL aarch64)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=generic+crc -fsigned-char -fno-stack-protector -fomit-frame-pointer")
endif ()

check_cxx_compiler_flag(-mcrc32 COMPILER_HAS_MCRC32_FLAG)
if (COMPILER_HAS_MCRC32_FLAG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcrc32")
if (${ARCH} STREQUAL x86_64)
check_cxx_compiler_flag(-mcrc32 COMPILER_HAS_MCRC32_FLAG)
if (COMPILER_HAS_MCRC32_FLAG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcrc32")
endif ()
endif ()

set(CMAKE_C_FLAGS ${CMAKE_CXX_FLAGS})
Expand Down
24 changes: 5 additions & 19 deletions common/alog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void LogFormatter::put_integer_dec(ALogBuffer& buf, ALogInteger x)
__attribute__((constructor)) static void __initial_timezone() { tzset(); }
static time_t dayid = 0;
static struct tm alog_time = {0};
struct tm* alog_update_time(time_t now)
static struct tm* alog_update_time(time_t now)
{
auto now0 = now;
int sec = now % 60; now /= 60;
Expand Down Expand Up @@ -478,36 +478,22 @@ int log_output_file_close() {
return 0;
}

namespace photon
{
struct thread;
extern __thread thread* CURRENT;
}

static inline ALogInteger DEC_W2P0(uint64_t x)
{
return DEC(x).width(2).padding('0');
}

namespace photon {
struct timeval alog_update_now();
}

LogBuffer& operator << (LogBuffer& log, const Prologue& pro)
{
#ifdef LOG_BENCHMARK
auto t = &alog_time;
#else
auto ts = photon::alog_update_now();
auto t = alog_update_time(ts.tv_sec - timezone);
auto ts = photon::__update_now();
auto t = alog_update_time(ts.sec() - timezone);
#endif
#define DEC_W2P0(x) DEC(x).width(2).padding('0')
log.printf(t->tm_year, '/');
log.printf(DEC_W2P0(t->tm_mon), '/');
log.printf(DEC_W2P0(t->tm_mday), ' ');
log.printf(DEC_W2P0(t->tm_hour), ':');
log.printf(DEC_W2P0(t->tm_min), ':');
log.printf(DEC_W2P0(t->tm_sec), '.');
log.printf(DEC(ts.tv_usec).width(6).padding('0'));
log.printf(DEC(ts.usec()).width(6).padding('0'));

static const char levels[] = "|DEBUG|th=|INFO |th=|WARN |th=|ERROR|th=|FATAL|th=|TEMP |th=|AUDIT|th=";
log.reserved = pro.level;
Expand Down
19 changes: 13 additions & 6 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,11 @@ R"(
)"
);

#ifdef __clang__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winline-asm"
#endif

inline void switch_context(thread* from, thread* to) {
prepare_switch(from, to);
auto _t_ = to->stack.pointer_ref();
Expand Down Expand Up @@ -881,6 +886,9 @@ R"(
"x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16",
"x17", "x18");
}
#ifdef __clang__
#pragma GCC diagnostic pop
#endif

#endif // x86 or arm

Expand Down Expand Up @@ -1033,19 +1041,18 @@ R"(

volatile uint64_t now;
static std::atomic<pthread_t> ts_updater(0);
static inline struct timeval update_now()
static inline NowTime update_now()
{
#if defined(__x86_64__) && defined(__linux__) && defined(ENABLE_MIMIC_VDSO)
if (likely(__mimic_vdso_time_x86))
return photon::now = __mimic_vdso_time_x86.get_now();
#endif
struct timeval tv;
gettimeofday(&tv, NULL);
uint64_t nnow = tv.tv_sec;
nnow *= 1000 * 1000;
nnow += tv.tv_usec;
uint64_t nnow = tv.tv_sec * 1000ul * 1000ul + tv.tv_usec;
now = nnow;
return tv;
assert(tv.tv_sec <= UINT32_MAX && tv.tv_usec < 1000000);
return {nnow, ((uint64_t)tv.tv_sec << 32) | (uint32_t)tv.tv_usec};
}
__attribute__((always_inline))
static inline uint32_t _rdtsc()
Expand Down Expand Up @@ -1089,7 +1096,7 @@ R"(
update_now();
}
}
struct timeval alog_update_now() {
NowTime __update_now() {
last_tsc = _rdtsc();
return update_now();
}
Expand Down
8 changes: 7 additions & 1 deletion thread/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ namespace photon

struct thread;
extern __thread thread* CURRENT;
extern volatile uint64_t now;
extern volatile uint64_t now; // a coarse-grained timestamp in unit of us
struct NowTime {
uint64_t now, _sec_usec;
uint32_t sec() { return _sec_usec >> 32; }
uint32_t usec() { auto p = (uint32_t*)&_sec_usec; return *p; }
};
NowTime __update_now(); // update `now`

enum states
{
Expand Down

0 comments on commit 8aff385

Please sign in to comment.