Commit 11ba497c authored by Sergei D's avatar Sergei D Committed by Commit Bot

Delegate getting current wall-clock time to the Platform interface.

To enable executing code in a context of a particular time or date (e.g. when
codepath depends on whether it's say evening or New Year) there is a need for
a way to provide it bypassing actual system time.

Bug: chromium:751993
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: Iee35d97b74345f63fff814a65a6f134d7c970341
Reviewed-on: https://chromium-review.googlesource.com/598666
Commit-Queue: Sergei Datsenko <dats@chromium.org>
Reviewed-by: 's avatarJaroslav Sevcik <jarin@chromium.org>
Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Reviewed-by: 's avatarRoss McIlroy <rmcilroy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47700}
parent 46473f82
......@@ -209,10 +209,7 @@ class Platform {
* Current wall-clock time in milliseconds since epoch.
* This function is expected to return at least millisecond-precision values.
*/
virtual double CurrentClockTimeMillis() {
// TODO(dats): Make pure virtual after V8 roll in Chromium.
return 0.0;
}
virtual double CurrentClockTimeMillis() = 0;
typedef void (*StackTracePrinter)();
......
......@@ -355,8 +355,9 @@ void GraphC1Visualizer::PrintCompilation(const CompilationInfo* info) {
PrintStringProperty("name", name.get());
PrintStringProperty("method", "stub");
}
PrintLongProperty("date",
static_cast<int64_t>(base::OS::TimeCurrentMillis()));
PrintLongProperty(
"date",
static_cast<int64_t>(V8::GetCurrentPlatform()->CurrentClockTimeMillis()));
}
......
......@@ -228,6 +228,10 @@ class PredictablePlatform : public Platform {
return synthetic_time_in_sec_ += 0.00001;
}
double CurrentClockTimeMillis() override {
return MonotonicallyIncreasingTime() * base::Time::kMillisecondsPerSecond;
}
v8::TracingController* GetTracingController() override {
return platform_->GetTracingController();
}
......
......@@ -278,6 +278,10 @@ double DefaultPlatform::MonotonicallyIncreasingTime() {
static_cast<double>(base::Time::kMicrosecondsPerSecond);
}
double DefaultPlatform::CurrentClockTimeMillis() {
return base::OS::TimeCurrentMillis();
}
TracingController* DefaultPlatform::GetTracingController() {
return tracing_controller_.get();
}
......
......@@ -57,6 +57,7 @@ class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
void CallIdleOnForegroundThread(Isolate* isolate, IdleTask* task) override;
bool IdleTasksEnabled(Isolate* isolate) override;
double MonotonicallyIncreasingTime() override;
double CurrentClockTimeMillis() override;
v8::TracingController* GetTracingController() override;
StackTracePrinter GetStackTracePrinter() override;
......
......@@ -1358,7 +1358,7 @@ void Logger::ResourceEvent(const char* name, const char* tag) {
if (base::OS::GetUserTime(&sec, &usec) != -1) {
msg.Append("%d,%d,", sec, usec);
}
msg.Append("%.0f", base::OS::TimeCurrentMillis());
msg.Append("%.0f", V8::GetCurrentPlatform()->CurrentClockTimeMillis());
msg.WriteToLogFile();
}
......@@ -1389,7 +1389,7 @@ void Logger::HeapSampleBeginEvent(const char* space, const char* kind) {
// Using non-relative system time in order to be able to synchronize with
// external memory profiling events (e.g. DOM memory size).
msg.Append("heap-sample-begin,\"%s\",\"%s\",%.0f", space, kind,
base::OS::TimeCurrentMillis());
V8::GetCurrentPlatform()->CurrentClockTimeMillis());
msg.WriteToLogFile();
}
......@@ -1818,7 +1818,8 @@ static void PrepareLogFileName(std::ostream& os, // NOLINT
break;
case 't':
// %t expands to the current time in milliseconds.
os << static_cast<int64_t>(base::OS::TimeCurrentMillis());
os << static_cast<int64_t>(
V8::GetCurrentPlatform()->CurrentClockTimeMillis());
break;
case '%':
// %% expands (contracts really) to %.
......
......@@ -18737,9 +18737,7 @@ double JSDate::CurrentTimeValue(Isolate* isolate) {
// the number in a Date object representing a particular instant in
// time is milliseconds. Therefore, we floor the result of getting
// the OS time.
return Floor(FLAG_verify_predictable
? isolate->heap()->MonotonicallyIncreasingTimeInMs()
: base::OS::TimeCurrentMillis());
return Floor(V8::GetCurrentPlatform()->CurrentClockTimeMillis());
}
......
......@@ -398,7 +398,8 @@ void PerfJitLogger::LogWriteHeader() {
header.reserved_ = 0xdeadbeef;
header.process_id_ = base::OS::GetCurrentProcessId();
header.time_stamp_ =
static_cast<uint64_t>(base::OS::TimeCurrentMillis() * 1000.0);
static_cast<uint64_t>(V8::GetCurrentPlatform()->CurrentClockTimeMillis() *
base::Time::kMicrosecondsPerMillisecond);
header.flags_ = 0;
LogWriteBytes(reinterpret_cast<const char*>(&header), sizeof(header));
......
......@@ -677,6 +677,10 @@ class TestPlatform : public v8::Platform {
return old_platform_->MonotonicallyIncreasingTime();
}
double CurrentClockTimeMillis() override {
return old_platform_->CurrentClockTimeMillis();
}
void CallIdleOnForegroundThread(v8::Isolate* isolate,
v8::IdleTask* task) override {
old_platform_->CallIdleOnForegroundThread(isolate, task);
......
......@@ -786,11 +786,12 @@ class TestApiCallbacks {
private:
void Wait() {
if (is_warming_up_) return;
double start = v8::base::OS::TimeCurrentMillis();
v8::Platform* platform = v8::internal::V8::GetCurrentPlatform();
double start = platform->CurrentClockTimeMillis();
double duration = 0;
while (duration < min_duration_ms_) {
v8::base::OS::Sleep(v8::base::TimeDelta::FromMilliseconds(1));
duration = v8::base::OS::TimeCurrentMillis() - start;
duration = platform->CurrentClockTimeMillis() - start;
}
}
......
......@@ -359,7 +359,7 @@ void IsolateData::SetCurrentTimeMS(double time) {
double IsolateData::currentTimeMS() {
if (current_time_set_) return current_time_;
return v8::base::OS::TimeCurrentMillis();
return v8::internal::V8::GetCurrentPlatform()->CurrentClockTimeMillis();
}
void IsolateData::SetMemoryInfo(v8::Local<v8::Value> memory_info) {
......
......@@ -160,6 +160,10 @@ class MockPlatform : public v8::Platform {
return time_;
}
double CurrentClockTimeMillis() override {
return time_ * base::Time::kMillisecondsPerSecond;
}
v8::TracingController* GetTracingController() override {
return tracing_controller_;
}
......
......@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "src/libplatform/default-platform.h"
#include "src/base/platform/time.h"
#include "testing/gmock/include/gmock/gmock.h"
using testing::InSequence;
......@@ -30,6 +31,9 @@ class DefaultPlatformWithMockTime : public DefaultPlatform {
DefaultPlatformWithMockTime()
: DefaultPlatform(IdleTaskSupport::kEnabled), time_(0) {}
double MonotonicallyIncreasingTime() override { return time_; }
double CurrentClockTimeMillis() override {
return time_ * base::Time::kMillisecondsPerSecond;
}
void IncreaseTime(double seconds) { time_ += seconds; }
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment