Use chrono for timing

Summary: Since we depend on C++11, we might as well use it for timing, instead of this platform-depended code.

Test Plan: Ran autovector_test, which reports time and confirmed that output is similar to master

Reviewers: ljin, sdong, yhchiang, rven, dhruba

Reviewed By: dhruba

Subscribers: dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D25587
main
Igor Canadi 10 years ago
parent 240ed0cd7b
commit 001ce64dc7
  1. 21
      util/env_posix.cc

@ -7,6 +7,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors. // found in the LICENSE file. See the AUTHORS file for names of contributors.
#include <chrono>
#include <deque> #include <deque>
#include <set> #include <set>
#include <dirent.h> #include <dirent.h>
@ -1350,25 +1351,13 @@ class PosixEnv : public Env {
} }
virtual uint64_t NowMicros() { virtual uint64_t NowMicros() {
struct timeval tv; return std::chrono::duration_cast<std::chrono::milliseconds>(
// TODO(kailiu) MAC DON'T HAVE THIS std::chrono::steady_clock::now().time_since_epoch()).count();
gettimeofday(&tv, nullptr);
return static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
} }
virtual uint64_t NowNanos() { virtual uint64_t NowNanos() {
#ifdef OS_LINUX return std::chrono::duration_cast<std::chrono::nanoseconds>(
struct timespec ts; std::chrono::steady_clock::now().time_since_epoch()).count();
clock_gettime(CLOCK_MONOTONIC, &ts);
return static_cast<uint64_t>(ts.tv_sec) * 1000000000 + ts.tv_nsec;
#elif __MACH__
clock_serv_t cclock;
mach_timespec_t ts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &ts);
mach_port_deallocate(mach_task_self(), cclock);
#endif
return static_cast<uint64_t>(ts.tv_sec) * 1000000000 + ts.tv_nsec;
} }
virtual void SleepForMicroseconds(int micros) { virtual void SleepForMicroseconds(int micros) {

Loading…
Cancel
Save