Skip to content

Commit

Permalink
Fix potential ambiguities in/around port/sys_time.h (facebook#10045)
Browse files Browse the repository at this point in the history
Summary:
There are some time-related POSIX APIs that are not available on Windows
(e.g. `localtime_r`), which we have worked around by providing our own
implementations in `port/sys_time.h`. This workaround actually relies on
some ambiguity: on Windows, a call to `localtime_r` calls
`ROCKSDB_NAMESPACE::port::localtime_r` (which is pulled into
`ROCKSDB_NAMESPACE` by a using-declaration), while on other platforms
it calls the global `localtime_r`. This works fine as long as there is only one
candidate function; however, it breaks down when there is more than one
`localtime_r` visible in a scope.

The patch fixes this by introducing `ROCKSDB_NAMESPACE::port::{TimeVal, GetTimeOfDay, LocalTimeR}`
to eliminate any ambiguity.

Pull Request resolved: facebook#10045

Test Plan: `make check`

Reviewed By: riversand963

Differential Revision: D36639372

Pulled By: ltamasi

fbshipit-source-id: fc13dbfa421b7c8918111a6d9e24ce77e91a7c50
  • Loading branch information
ltamasi authored and facebook-github-bot committed May 25, 2022
1 parent a96a4a2 commit af7ae91
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 39 deletions.
6 changes: 3 additions & 3 deletions env/env_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class PosixClock : public SystemClock {
const char* NickName() const override { return kClassName(); }

uint64_t NowMicros() override {
struct timeval tv;
gettimeofday(&tv, nullptr);
port::TimeVal tv;
port::GetTimeOfDay(&tv, nullptr);
return static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
}

Expand Down Expand Up @@ -200,7 +200,7 @@ class PosixClock : public SystemClock {
dummy.reserve(maxsize);
dummy.resize(maxsize);
char* p = &dummy[0];
localtime_r(&seconds, &t);
port::LocalTimeR(&seconds, &t);
snprintf(p, maxsize, "%04d/%02d/%02d-%02d:%02d:%02d ", t.tm_year + 1900,
t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
return dummy;
Expand Down
10 changes: 5 additions & 5 deletions env/env_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1604,9 +1604,9 @@ class TestLogger : public Logger {

if (new_format[0] == '[') {
// "[DEBUG] "
ASSERT_TRUE(n <= 56 + (512 - static_cast<int>(sizeof(struct timeval))));
ASSERT_TRUE(n <= 56 + (512 - static_cast<int>(sizeof(port::TimeVal))));
} else {
ASSERT_TRUE(n <= 48 + (512 - static_cast<int>(sizeof(struct timeval))));
ASSERT_TRUE(n <= 48 + (512 - static_cast<int>(sizeof(port::TimeVal))));
}
va_end(backup_ap);
}
Expand Down Expand Up @@ -1674,9 +1674,9 @@ class TestLogger2 : public Logger {
va_copy(backup_ap, ap);
int n = vsnprintf(new_format, sizeof(new_format) - 1, format, backup_ap);
// 48 bytes for extra information + bytes allocated
ASSERT_TRUE(
n <= 48 + static_cast<int>(max_log_size_ - sizeof(struct timeval)));
ASSERT_TRUE(n > static_cast<int>(max_log_size_ - sizeof(struct timeval)));
ASSERT_TRUE(n <=
48 + static_cast<int>(max_log_size_ - sizeof(port::TimeVal)));
ASSERT_TRUE(n > static_cast<int>(max_log_size_ - sizeof(port::TimeVal)));
va_end(backup_ap);
}
}
Expand Down
6 changes: 3 additions & 3 deletions env/mock_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,13 @@ class TestMemLogger : public Logger {
char* p = base;
char* limit = base + bufsize;

struct timeval now_tv;
gettimeofday(&now_tv, nullptr);
port::TimeVal now_tv;
port::GetTimeOfDay(&now_tv, nullptr);
const time_t seconds = now_tv.tv_sec;
struct tm t;
memset(&t, 0, sizeof(t));
struct tm* ret __attribute__((__unused__));
ret = localtime_r(&seconds, &t);
ret = port::LocalTimeR(&seconds, &t);
assert(ret);
p += snprintf(p, limit - p, "%04d/%02d/%02d-%02d:%02d:%02d.%06d ",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour,
Expand Down
6 changes: 3 additions & 3 deletions logging/env_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ class EnvLogger : public Logger {
char* p = base;
char* limit = base + bufsize;

struct timeval now_tv;
gettimeofday(&now_tv, nullptr);
port::TimeVal now_tv;
port::GetTimeOfDay(&now_tv, nullptr);
const time_t seconds = now_tv.tv_sec;
struct tm t;
localtime_r(&seconds, &t);
port::LocalTimeR(&seconds, &t);
p += snprintf(p, limit - p, "%04d/%02d/%02d-%02d:%02d:%02d.%06d %llx ",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour,
t.tm_min, t.tm_sec, static_cast<int>(now_tv.tv_usec),
Expand Down
4 changes: 2 additions & 2 deletions logging/log_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void LogBuffer::AddLogToBuffer(size_t max_log_size, const char* format,
char* limit = alloc_mem + max_log_size - 1;

// store the time
gettimeofday(&(buffered_log->now_tv), nullptr);
port::GetTimeOfDay(&(buffered_log->now_tv), nullptr);

// Print the message
if (p < limit) {
Expand Down Expand Up @@ -60,7 +60,7 @@ void LogBuffer::FlushBufferToLog() {
for (BufferedLog* log : logs_) {
const time_t seconds = log->now_tv.tv_sec;
struct tm t;
if (localtime_r(&seconds, &t) != nullptr) {
if (port::LocalTimeR(&seconds, &t) != nullptr) {
Log(log_level_, info_log_,
"(Original Log Time %04d/%02d/%02d-%02d:%02d:%02d.%06d) %s",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min,
Expand Down
2 changes: 1 addition & 1 deletion logging/log_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LogBuffer {
private:
// One log entry with its timestamp
struct BufferedLog {
struct timeval now_tv; // Timestamp of the log
port::TimeVal now_tv; // Timestamp of the log
char message[1]; // Beginning of log message
};

Expand Down
6 changes: 3 additions & 3 deletions logging/posix_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ class PosixLogger : public Logger {
char* p = base;
char* limit = base + bufsize;

struct timeval now_tv;
gettimeofday(&now_tv, nullptr);
port::TimeVal now_tv;
port::GetTimeOfDay(&now_tv, nullptr);
const time_t seconds = now_tv.tv_sec;
struct tm t;
localtime_r(&seconds, &t);
port::LocalTimeR(&seconds, &t);
p += snprintf(p, limit - p, "%04d/%02d/%02d-%02d:%02d:%02d.%06d %llu ",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour,
t.tm_min, t.tm_sec, static_cast<int>(now_tv.tv_usec),
Expand Down
38 changes: 27 additions & 11 deletions port/sys_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,52 @@

#pragma once

#if defined(OS_WIN) && defined(_MSC_VER)
#include "rocksdb/rocksdb_namespace.h"

#include <time.h>
#if defined(OS_WIN) && (defined(_MSC_VER) || defined(__MINGW32__))

#include "rocksdb/rocksdb_namespace.h"
#include <time.h>

namespace ROCKSDB_NAMESPACE {

namespace port {

// Avoid including winsock2.h for this definition
struct timeval {
struct TimeVal {
long tv_sec;
long tv_usec;
};

void gettimeofday(struct timeval* tv, struct timezone* tz);
void GetTimeOfDay(TimeVal* tv, struct timezone* tz);

inline struct tm* localtime_r(const time_t* timep, struct tm* result) {
inline struct tm* LocalTimeR(const time_t* timep, struct tm* result) {
errno_t ret = localtime_s(result, timep);
return (ret == 0) ? result : NULL;
}
}

using port::timeval;
using port::gettimeofday;
using port::localtime_r;
} // namespace port

} // namespace ROCKSDB_NAMESPACE

#else
#include <time.h>
#include <sys/time.h>

namespace ROCKSDB_NAMESPACE {

namespace port {

using TimeVal = struct timeval;

inline void GetTimeOfDay(TimeVal* tv, struct timezone* tz) {
gettimeofday(tv, tz);
}

inline struct tm* LocalTimeR(const time_t* timep, struct tm* result) {
return localtime_r(timep, result);
}

} // namespace port

} // namespace ROCKSDB_NAMESPACE

#endif
2 changes: 1 addition & 1 deletion port/win/port_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ std::wstring utf8_to_utf16(const std::string& utf8) {
}
#endif

void gettimeofday(struct timeval* tv, struct timezone* /* tz */) {
void GetTimeOfDay(TimeVal* tv, struct timezone* /* tz */) {
std::chrono::microseconds usNow(
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch()));
Expand Down
4 changes: 2 additions & 2 deletions port/win/win_logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ void WinLogger::Logv(const char* format, va_list ap) {
char* p = base;
char* limit = base + bufsize;

struct timeval now_tv;
gettimeofday(&now_tv, nullptr);
port::TimeVal now_tv;
port::GetTimeOfDay(&now_tv, nullptr);
const time_t seconds = now_tv.tv_sec;
struct tm t;
localtime_s(&t, &seconds);
Expand Down
2 changes: 1 addition & 1 deletion util/string_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ std::string TimeToHumanString(int unixtime) {
char time_buffer[80];
time_t rawtime = unixtime;
struct tm tInfo;
struct tm* timeinfo = localtime_r(&rawtime, &tInfo);
struct tm* timeinfo = port::LocalTimeR(&rawtime, &tInfo);
assert(timeinfo == &tInfo);
strftime(time_buffer, 80, "%c", timeinfo);
return std::string(time_buffer);
Expand Down
10 changes: 6 additions & 4 deletions utilities/persistent_cache/hash_table_bench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
int main() { fprintf(stderr, "Please install gflags to run tools\n"); }
#else

#include <sys/time.h>
#include <unistd.h>

#include <atomic>
#include <functional>
#include <string>
#include <unordered_map>
#include <unistd.h>
#include <sys/time.h>

#include "port/port_posix.h"
#include "port/sys_time.h"
#include "rocksdb/env.h"
#include "util/gflags_compat.h"
#include "util/mutexlock.h"
Expand Down Expand Up @@ -152,8 +154,8 @@ class HashTableBenchmark {
}

static uint64_t NowInMillSec() {
timeval tv;
gettimeofday(&tv, /*tz=*/nullptr);
port::TimeVal tv;
port::GetTimeOfDay(&tv, /*tz=*/nullptr);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

Expand Down

0 comments on commit af7ae91

Please sign in to comment.