1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/scheduler/begin_frame_tracker.h"
9 BeginFrameTracker::BeginFrameTracker(const tracked_objects::Location
& location
)
10 : location_(location
),
11 location_string_(location
.ToString()),
12 current_updated_at_(),
14 current_finished_at_(base::TimeTicks::FromInternalValue(-1)) {
17 BeginFrameTracker::~BeginFrameTracker() {
20 void BeginFrameTracker::Start(BeginFrameArgs new_args
) {
21 // Trace the frame time being passed between BeginFrameTrackers.
22 TRACE_EVENT_FLOW_STEP0(
23 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"), "BeginFrameArgs",
24 new_args
.frame_time
.ToInternalValue(), location_string_
);
26 // Trace this specific begin frame tracker Start/Finish times.
27 TRACE_EVENT_ASYNC_BEGIN2(
28 TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"),
29 location_string_
.c_str(), new_args
.frame_time
.ToInternalValue(),
30 "new args", new_args
.AsValue(), "current args", current_args_
.AsValue());
32 // Check the new BeginFrameArgs are valid and monotonically increasing.
33 DCHECK(new_args
.IsValid());
34 DCHECK_LE(current_args_
.frame_time
, new_args
.frame_time
);
37 << "Tried to start a new frame before finishing an existing frame.";
38 current_updated_at_
= base::TimeTicks::NowFromSystemTraceTime();
39 current_args_
= new_args
;
40 current_finished_at_
= base::TimeTicks();
42 // TODO(mithro): Add UMA tracking of delta between current_updated_at_ time
43 // and the new_args.frame_time argument. This will give us how long after a
44 // BeginFrameArgs message was created before we started processing it.
47 const BeginFrameArgs
& BeginFrameTracker::Current() const {
48 DCHECK(!HasFinished())
49 << "Tried to use BeginFrameArgs after marking the frame finished.";
50 DCHECK(current_args_
.IsValid())
51 << "Tried to use BeginFrameArgs before starting a frame!";
55 void BeginFrameTracker::Finish() {
56 DCHECK(!HasFinished()) << "Tried to finish an already finished frame";
57 current_finished_at_
= base::TimeTicks::NowFromSystemTraceTime();
58 TRACE_EVENT_ASYNC_END0(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"),
59 location_string_
.c_str(),
60 current_args_
.frame_time
.ToInternalValue());
63 const BeginFrameArgs
& BeginFrameTracker::Last() const {
64 DCHECK(current_args_
.IsValid())
65 << "Tried to use last BeginFrameArgs before starting a frame!";
67 << "Tried to use last BeginFrameArgs before the frame is finished.";
71 base::TimeDelta
BeginFrameTracker::Interval() const {
72 base::TimeDelta interval
= current_args_
.interval
;
73 // Normal interval will be ~16ms, 200Hz (5ms) screens are the fastest
74 // easily available so anything less than that is likely an error.
75 if (interval
< base::TimeDelta::FromMilliseconds(1)) {
76 interval
= BeginFrameArgs::DefaultInterval();
81 void BeginFrameTracker::AsValueInto(
83 base::trace_event::TracedValue
* state
) const {
84 state
->SetInteger("updated_at_us", current_updated_at_
.ToInternalValue());
85 state
->SetInteger("finished_at_us", current_finished_at_
.ToInternalValue());
87 state
->SetString("state", "FINISHED");
88 state
->BeginDictionary("current_args_");
90 state
->SetString("state", "USING");
91 state
->BeginDictionary("last_args_");
93 current_args_
.AsValueInto(state
);
94 state
->EndDictionary();
96 base::TimeTicks frame_time
= current_args_
.frame_time
;
97 base::TimeTicks deadline
= current_args_
.deadline
;
98 base::TimeDelta interval
= current_args_
.interval
;
99 state
->BeginDictionary("major_timestamps_in_ms");
100 state
->SetDouble("0_interval", interval
.InMillisecondsF());
101 state
->SetDouble("1_now_to_deadline", (deadline
- now
).InMillisecondsF());
102 state
->SetDouble("2_frame_time_to_now", (now
- frame_time
).InMillisecondsF());
103 state
->SetDouble("3_frame_time_to_deadline",
104 (deadline
- frame_time
).InMillisecondsF());
105 state
->SetDouble("4_now", (now
- base::TimeTicks()).InMillisecondsF());
106 state
->SetDouble("5_frame_time",
107 (frame_time
- base::TimeTicks()).InMillisecondsF());
108 state
->SetDouble("6_deadline",
109 (deadline
- base::TimeTicks()).InMillisecondsF());
110 state
->EndDictionary();
113 const BeginFrameArgs
& BeginFrameTracker::DangerousMethodCurrentOrLast() const {
114 if (!HasFinished()) {