1 // Copyright 2016 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.
4
5 #include "base/trace_event/auto_open_close_event.h"
6
7 #include "base/macros.h"
8 #include "base/time/time.h"
9 #include "base/trace_event/trace_event.h"
10
11 namespace base {
12 namespace trace_event {
13
AutoOpenCloseEvent(AutoOpenCloseEvent::Type type,const char * category,const char * event_name)14 AutoOpenCloseEvent::AutoOpenCloseEvent(AutoOpenCloseEvent::Type type,
15 const char* category, const char* event_name):
16 category_(category),
17 event_name_(event_name),
18 weak_factory_(this) {
19 base::trace_event::TraceLog::GetInstance()->AddAsyncEnabledStateObserver(
20 weak_factory_.GetWeakPtr());
21 }
22
~AutoOpenCloseEvent()23 AutoOpenCloseEvent::~AutoOpenCloseEvent() {
24 DCHECK(thread_checker_.CalledOnValidThread());
25 base::trace_event::TraceLog::GetInstance()->RemoveAsyncEnabledStateObserver(
26 this);
27 }
28
Begin()29 void AutoOpenCloseEvent::Begin() {
30 DCHECK(thread_checker_.CalledOnValidThread());
31 start_time_ = TRACE_TIME_TICKS_NOW();
32 TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(
33 category_, event_name_, static_cast<void*>(this), start_time_);
34 }
35
End()36 void AutoOpenCloseEvent::End() {
37 DCHECK(thread_checker_.CalledOnValidThread());
38 TRACE_EVENT_ASYNC_END0(category_, event_name_, static_cast<void*>(this));
39 start_time_ = base::TimeTicks();
40 }
41
OnTraceLogEnabled()42 void AutoOpenCloseEvent::OnTraceLogEnabled() {
43 DCHECK(thread_checker_.CalledOnValidThread());
44 if (start_time_.ToInternalValue() != 0)
45 TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(
46 category_, event_name_, static_cast<void*>(this), start_time_);
47 }
48
OnTraceLogDisabled()49 void AutoOpenCloseEvent::OnTraceLogDisabled() {}
50
51 } // namespace trace_event
52 } // namespace base
53