Commit 60104eda authored by Sigurd Schneider's avatar Sigurd Schneider Committed by Commit Bot

[cleanup] Introduce since_origin helper to TimeBase

TimeTicks cannot convert to other timeunits, only TimeDelta can
do that. Chrome's version of TimeBase introduced a `since_origin`
helper that essentially converts a TimeTicks object in to a
TimeDelta with the corresponding duration.

This CL ports that helper to V8 and cleans up a couple of places
where we used `timestamp - 0` to convert a TimeTicks object to
a TimeDelta.

Change-Id: I540ced876ac3e727dfdedf7b838a272cc40d6954
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1993282
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Reviewed-by: 's avatarSimon Zünd <szuend@chromium.org>
Reviewed-by: 's avatarClemens Backes <clemensb@chromium.org>
Reviewed-by: 's avatarPeter Marshall <petermarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65820}
parent c0ee1e28
......@@ -10364,18 +10364,17 @@ const CpuProfileNode* CpuProfile::GetSample(int index) const {
int64_t CpuProfile::GetSampleTimestamp(int index) const {
const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
return (profile->sample(index).timestamp - base::TimeTicks())
.InMicroseconds();
return profile->sample(index).timestamp.since_origin().InMicroseconds();
}
int64_t CpuProfile::GetStartTime() const {
const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
return (profile->start_time() - base::TimeTicks()).InMicroseconds();
return profile->start_time().since_origin().InMicroseconds();
}
int64_t CpuProfile::GetEndTime() const {
const i::CpuProfile* profile = reinterpret_cast<const i::CpuProfile*>(this);
return (profile->end_time() - base::TimeTicks()).InMicroseconds();
return profile->end_time().since_origin().InMicroseconds();
}
int CpuProfile::GetSamplesCount() const {
......
......@@ -268,6 +268,16 @@ class TimeBase : public TimeConstants {
// provided operators.
int64_t ToInternalValue() const { return us_; }
// The amount of time since the origin (or "zero") point. This is a syntactic
// convenience to aid in code readability, mainly for debugging/testing use
// cases.
//
// Warning: While the Time subclass has a fixed origin point, the origin for
// the other subclasses can vary each time the application is restarted.
constexpr TimeDelta since_origin() const {
return TimeDelta::FromMicroseconds(us_);
}
TimeClass& operator=(TimeClass other) {
us_ = other.us_;
return *(static_cast<TimeClass*>(this));
......
......@@ -299,8 +299,7 @@ Response V8ProfilerAgentImpl::startPreciseCoverage(Maybe<bool> callCount,
double* out_timestamp) {
if (!m_enabled) return Response::Error("Profiler is not enabled");
*out_timestamp =
(v8::base::TimeTicks::HighResolutionNow() - v8::base::TimeTicks())
.InSecondsF();
v8::base::TimeTicks::HighResolutionNow().since_origin().InSecondsF();
bool callCountValue = callCount.fromMaybe(false);
bool detailedValue = detailed.fromMaybe(false);
m_state->setBoolean(ProfilerAgentState::preciseCoverageStarted, true);
......@@ -411,8 +410,7 @@ Response V8ProfilerAgentImpl::takePreciseCoverage(
v8::HandleScope handle_scope(m_isolate);
v8::debug::Coverage coverage = v8::debug::Coverage::CollectPrecise(m_isolate);
*out_timestamp =
(v8::base::TimeTicks::HighResolutionNow() - v8::base::TimeTicks())
.InSecondsF();
v8::base::TimeTicks::HighResolutionNow().since_origin().InSecondsF();
return coverageToProtocol(m_session->inspector(), coverage, out_result);
}
......
......@@ -497,8 +497,7 @@ CpuProfile::CpuProfile(CpuProfiler* profiler, const char* title,
streaming_next_sample_(0),
id_(++last_id_) {
auto value = TracedValue::Create();
value->SetDouble("startTime",
(start_time_ - base::TimeTicks()).InMicroseconds());
value->SetDouble("startTime", start_time_.since_origin().InMicroseconds());
TRACE_EVENT_SAMPLE_WITH_ID1(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"),
"Profile", id_, "data", std::move(value));
......@@ -639,7 +638,7 @@ void CpuProfile::FinishProfile() {
context_filter_ = nullptr;
StreamPendingTraceEvents();
auto value = TracedValue::Create();
value->SetDouble("endTime", (end_time_ - base::TimeTicks()).InMicroseconds());
value->SetDouble("endTime", end_time_.since_origin().InMicroseconds());
TRACE_EVENT_SAMPLE_WITH_ID1(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"),
"ProfileChunk", id_, "data", std::move(value));
}
......
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