Skip to content

Commit

Permalink
Making use of GetSystemTimePreciseAsFileTime dynamic to not
Browse files Browse the repository at this point in the history
break compatibility with Windows 7. The issue with rotated logs
was fixed other way.
  • Loading branch information
koldat committed Feb 2, 2016
1 parent 9c2cf94 commit 502d41f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
13 changes: 11 additions & 2 deletions db/auto_roll_logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,17 @@ Status AutoRollLogger::ResetLogger() {
}

void AutoRollLogger::RollLogFile() {
std::string old_fname = OldInfoLogFileName(
dbname_, env_->NowMicros(), db_absolute_path_, db_log_dir_);
uint64_t now = env_->NowMicros();
std::string old_fname;
// Try to check target name only 10 times at most
for (int i = 0; i < 10; i++) {
old_fname = OldInfoLogFileName(
dbname_, now, db_absolute_path_, db_log_dir_);
if (!env_->FileExists(old_fname).ok()) {
break;
}
now++;
};
env_->RenameFile(log_fname_, old_fname);
}

Expand Down
59 changes: 40 additions & 19 deletions port/win/env_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,8 @@ void WinthreadCall(const char* label, std::error_code result) {
}
}

typedef VOID(WINAPI * FnGetSystemTimePreciseAsFileTime)(LPFILETIME);

class WinEnv : public Env {
public:
WinEnv();
Expand Down Expand Up @@ -1676,25 +1678,29 @@ class WinEnv : public Env {
}

virtual uint64_t NowMicros() override {
// all std::chrono clocks on windows proved to return
// values that may repeat that is not good enough for some uses.
const int64_t c_UnixEpochStartTicks = 116444736000000000i64;
const int64_t c_FtToMicroSec = 10;

// This interface needs to return system time and not
// just any microseconds because it is often used as an argument
// to TimedWait() on condition variable
FILETIME ftSystemTime;
GetSystemTimePreciseAsFileTime(&ftSystemTime);

LARGE_INTEGER li;
li.LowPart = ftSystemTime.dwLowDateTime;
li.HighPart = ftSystemTime.dwHighDateTime;
// Subtract unix epoch start
li.QuadPart -= c_UnixEpochStartTicks;
// Convert to microsecs
li.QuadPart /= c_FtToMicroSec;
return li.QuadPart;
if (GetSystemTimePreciseAsFileTime_ != NULL) {
// all std::chrono clocks on windows proved to return
// values that may repeat that is not good enough for some uses.
const int64_t c_UnixEpochStartTicks = 116444736000000000i64;
const int64_t c_FtToMicroSec = 10;

// This interface needs to return system time and not
// just any microseconds because it is often used as an argument
// to TimedWait() on condition variable
FILETIME ftSystemTime;
GetSystemTimePreciseAsFileTime_(&ftSystemTime);

LARGE_INTEGER li;
li.LowPart = ftSystemTime.dwLowDateTime;
li.HighPart = ftSystemTime.dwHighDateTime;
// Subtract unix epoch start
li.QuadPart -= c_UnixEpochStartTicks;
// Convert to microsecs
li.QuadPart /= c_FtToMicroSec;
return li.QuadPart;
}
using namespace std::chrono;
return duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
}

virtual uint64_t NowNanos() override {
Expand Down Expand Up @@ -2104,15 +2110,30 @@ class WinEnv : public Env {
std::vector<ThreadPool> thread_pools_;
mutable std::mutex mu_;
std::vector<std::thread> threads_to_join_;
static FnGetSystemTimePreciseAsFileTime GetSystemTimePreciseAsFileTime_;
static bool GetSystemTimePreciseAsFileTimeInitialized_;
};

FnGetSystemTimePreciseAsFileTime WinEnv::GetSystemTimePreciseAsFileTime_ = NULL;
bool WinEnv::GetSystemTimePreciseAsFileTimeInitialized_ = false;

WinEnv::WinEnv()
: checkedDiskForMmap_(false),
forceMmapOff(false),
page_size_(4 * 1012),
allocation_granularity_(page_size_),
perf_counter_frequency_(0),
thread_pools_(Priority::TOTAL) {

if (!GetSystemTimePreciseAsFileTimeInitialized_) {
HMODULE module = GetModuleHandle("kernel32.dll");
if (module != NULL) {
GetSystemTimePreciseAsFileTime_ = (FnGetSystemTimePreciseAsFileTime)GetProcAddress(
module, "GetSystemTimePreciseAsFileTime");
}
GetSystemTimePreciseAsFileTimeInitialized_ = true;
}

SYSTEM_INFO sinfo;
GetSystemInfo(&sinfo);

Expand Down

0 comments on commit 502d41f

Please sign in to comment.