Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(860)

Side by Side Diff: mojo/common/trace_provider_impl.cc

Issue 1334213002: Support for tracing app initialization. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/common/trace_provider_impl.h" 5 #include "mojo/common/trace_provider_impl.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/weak_ptr.h"
8 #include "base/trace_event/trace_config.h" 9 #include "base/trace_event/trace_config.h"
9 #include "base/trace_event/trace_event.h" 10 #include "base/trace_event/trace_event.h"
10 #include "mojo/public/cpp/application/application_connection.h" 11 #include "mojo/public/cpp/application/application_connection.h"
11 #include "mojo/public/cpp/application/application_impl.h" 12 #include "mojo/public/cpp/application/application_impl.h"
12 13
13 namespace mojo { 14 namespace mojo {
14 15 namespace {
15 TraceProviderImpl::TraceProviderImpl( 16 void StopIfForced(base::WeakPtr<TraceProviderImpl> impl) {
16 InterfaceRequest<tracing::TraceProvider> request) 17 if (impl.get()) {
17 : tracing_already_started_(false), binding_(this, request.Pass()) { 18 impl->StopIfForced();
19 }
18 } 20 }
19 21
20 TraceProviderImpl::~TraceProviderImpl() { 22 void DelayedStop(base::WeakPtr<TraceProviderImpl> impl) {
23 // We use this indirection to account for cases where the Initialize app
24 // method (within which TraceProviderImpl is created) takes more than one
25 // second to finish; thus we start the countdown only when the current thread
26 // is unblocked.
27 base::MessageLoop::current()->PostDelayedTask(
28 FROM_HERE, base::Bind(&StopIfForced, impl),
29 base::TimeDelta::FromSeconds(1));
30 }
31
32 } // namespace
33
34 TraceProviderImpl::TraceProviderImpl()
35 : binding_(this), tracing_forced_(false), weak_factory_(this) {}
36
37 TraceProviderImpl::~TraceProviderImpl() {}
38
39 void TraceProviderImpl::Bind(InterfaceRequest<tracing::TraceProvider> request) {
40 if (!binding_.is_bound()) {
41 binding_.Bind(request.Pass());
42 } else {
43 LOG(ERROR) << "Cannot accept two connections to TraceProvider.";
44 }
21 } 45 }
22 46
23 void TraceProviderImpl::StartTracing(const String& categories, 47 void TraceProviderImpl::StartTracing(const String& categories,
24 tracing::TraceRecorderPtr collector) { 48 tracing::TraceRecorderPtr collector) {
25 DCHECK(!recorder_.get()); 49 DCHECK(!recorder_.get());
26 recorder_ = collector.Pass(); 50 recorder_ = collector.Pass();
27 if (!tracing_already_started_) { 51 tracing_forced_ = false;
52 if (!base::trace_event::TraceLog::GetInstance()->IsEnabled()) {
28 std::string categories_str = categories.To<std::string>(); 53 std::string categories_str = categories.To<std::string>();
29 base::trace_event::TraceLog::GetInstance()->SetEnabled( 54 base::trace_event::TraceLog::GetInstance()->SetEnabled(
30 base::trace_event::TraceConfig(categories_str, 55 base::trace_event::TraceConfig(categories_str,
31 base::trace_event::RECORD_UNTIL_FULL), 56 base::trace_event::RECORD_UNTIL_FULL),
32 base::trace_event::TraceLog::RECORDING_MODE); 57 base::trace_event::TraceLog::RECORDING_MODE);
33 } 58 }
34 } 59 }
35 60
36 void TraceProviderImpl::StopTracing() { 61 void TraceProviderImpl::StopTracing() {
37 DCHECK(recorder_); 62 DCHECK(recorder_);
38 base::trace_event::TraceLog::GetInstance()->SetDisabled(); 63 base::trace_event::TraceLog::GetInstance()->SetDisabled();
39 64
40 base::trace_event::TraceLog::GetInstance()->Flush( 65 base::trace_event::TraceLog::GetInstance()->Flush(
41 base::Bind(&TraceProviderImpl::SendChunk, base::Unretained(this))); 66 base::Bind(&TraceProviderImpl::SendChunk, base::Unretained(this)));
42 } 67 }
43 68
69 void TraceProviderImpl::EnableTracingNow() {
70 base::trace_event::TraceLog::GetInstance()->SetEnabled(
71 base::trace_event::TraceConfig("*", base::trace_event::RECORD_UNTIL_FULL),
72 base::trace_event::TraceLog::RECORDING_MODE);
73 tracing_forced_ = true;
74 base::MessageLoop::current()->PostTask(
75 FROM_HERE, base::Bind(&DelayedStop, weak_factory_.GetWeakPtr()));
76 }
77
78 void TraceProviderImpl::StopIfForced() {
79 if (!tracing_forced_) {
80 return;
81 }
82 base::trace_event::TraceLog::GetInstance()->SetDisabled();
83 base::trace_event::TraceLog::GetInstance()->Flush(
84 base::Callback<void(const scoped_refptr<base::RefCountedString>&,
85 bool)>());
86 }
87
44 void TraceProviderImpl::SendChunk( 88 void TraceProviderImpl::SendChunk(
45 const scoped_refptr<base::RefCountedString>& events_str, 89 const scoped_refptr<base::RefCountedString>& events_str,
46 bool has_more_events) { 90 bool has_more_events) {
47 DCHECK(recorder_); 91 DCHECK(recorder_);
48 // The string will be empty if an error eccured or there were no trace 92 // The string will be empty if an error eccured or there were no trace
49 // events. Empty string is not a valid chunk to record so skip in this case. 93 // events. Empty string is not a valid chunk to record so skip in this case.
50 if (events_str->data().length()) { 94 if (events_str->data().length()) {
51 recorder_->Record(mojo::String(events_str->data())); 95 recorder_->Record(mojo::String(events_str->data()));
52 } 96 }
53 if (!has_more_events) { 97 if (!has_more_events) {
54 recorder_.reset(); 98 recorder_.reset();
55 } 99 }
56 } 100 }
57 101
58 } // namespace mojo 102 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698