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

Side by Side Diff: mojo/shell/standalone/tracer.cc

Issue 1877753003: Move mojo\shell to services\shell (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@62scan
Patch Set: . Created 4 years, 8 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
« no previous file with comments | « mojo/shell/standalone/tracer.h ('k') | mojo/shell/switches.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "mojo/shell/standalone/tracer.h"
6
7 #include <stddef.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <utility>
11
12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h"
16 #include "base/trace_event/trace_config.h"
17 #include "base/trace_event/trace_event.h"
18
19 namespace mojo {
20 namespace shell {
21
22 Tracer::Tracer()
23 : tracing_(false), first_chunk_written_(false), trace_file_(nullptr) {}
24
25 Tracer::~Tracer() {
26 StopAndFlushToFile();
27 }
28
29 void Tracer::Start(const std::string& categories,
30 const std::string& duration_seconds_str,
31 const std::string& filename) {
32 tracing_ = true;
33 trace_filename_ = filename;
34 categories_ = categories;
35 base::trace_event::TraceConfig config(categories,
36 base::trace_event::RECORD_UNTIL_FULL);
37 base::trace_event::TraceLog::GetInstance()->SetEnabled(
38 config, base::trace_event::TraceLog::RECORDING_MODE);
39
40 int trace_duration_secs = 5;
41 if (!duration_seconds_str.empty()) {
42 CHECK(base::StringToInt(duration_seconds_str, &trace_duration_secs))
43 << "Could not parse --trace-startup-duration value "
44 << duration_seconds_str;
45 }
46 base::MessageLoop::current()->PostDelayedTask(
47 FROM_HERE,
48 base::Bind(&Tracer::StopAndFlushToFile, base::Unretained(this)),
49 base::TimeDelta::FromSeconds(trace_duration_secs));
50 }
51
52 void Tracer::StartCollectingFromTracingService(
53 tracing::TraceCollectorPtr coordinator) {
54 coordinator_ = std::move(coordinator);
55 mojo::DataPipe data_pipe;
56 coordinator_->Start(std::move(data_pipe.producer_handle), categories_);
57 drainer_.reset(new mojo::common::DataPipeDrainer(
58 this, std::move(data_pipe.consumer_handle)));
59 }
60
61 void Tracer::StopAndFlushToFile() {
62 if (tracing_)
63 StopTracingAndFlushToDisk();
64 }
65
66 void Tracer::ConnectToProvider(
67 mojo::InterfaceRequest<tracing::TraceProvider> request) {
68 trace_provider_impl_.Bind(std::move(request));
69 }
70
71 void Tracer::StopTracingAndFlushToDisk() {
72 tracing_ = false;
73 trace_file_ = fopen(trace_filename_.c_str(), "w+");
74 PCHECK(trace_file_);
75 static const char kStart[] = "{\"traceEvents\":[";
76 PCHECK(fwrite(kStart, 1, strlen(kStart), trace_file_) == strlen(kStart));
77
78 // At this point we might be connected to the tracing service, in which case
79 // we want to tell it to stop tracing and we will send the data we've
80 // collected in process to it.
81 if (coordinator_) {
82 coordinator_->StopAndFlush();
83 } else {
84 // Or we might not be connected. If we aren't connected to the tracing
85 // service we want to collect the tracing data gathered ourselves and flush
86 // it to disk. We do this in a blocking fashion (for this thread) so we can
87 // gather as much data as possible on shutdown.
88 base::trace_event::TraceLog::GetInstance()->SetDisabled();
89 {
90 base::WaitableEvent flush_complete_event(false, false);
91 // TraceLog::Flush requires a message loop but we've already shut ours
92 // down.
93 // Spin up a new thread to flush things out.
94 base::Thread flush_thread("mojo_runner_trace_event_flush");
95 flush_thread.Start();
96 flush_thread.message_loop()->PostTask(
97 FROM_HERE,
98 base::Bind(&Tracer::EndTraceAndFlush, base::Unretained(this),
99 trace_filename_,
100 base::Bind(&base::WaitableEvent::Signal,
101 base::Unretained(&flush_complete_event))));
102 base::trace_event::TraceLog::GetInstance()
103 ->SetCurrentThreadBlocksMessageLoop();
104 flush_complete_event.Wait();
105 }
106 }
107 }
108
109 void Tracer::WriteFooterAndClose() {
110 static const char kEnd[] = "]}";
111 PCHECK(fwrite(kEnd, 1, strlen(kEnd), trace_file_) == strlen(kEnd));
112 PCHECK(fclose(trace_file_) == 0);
113 trace_file_ = nullptr;
114 LOG(ERROR) << "Wrote trace data to " << trace_filename_;
115 }
116
117 void Tracer::EndTraceAndFlush(const std::string& filename,
118 const base::Closure& done_callback) {
119 base::trace_event::TraceLog::GetInstance()->SetDisabled();
120 base::trace_event::TraceLog::GetInstance()->Flush(base::Bind(
121 &Tracer::WriteTraceDataCollected, base::Unretained(this), done_callback));
122 }
123
124 void Tracer::WriteTraceDataCollected(
125 const base::Closure& done_callback,
126 const scoped_refptr<base::RefCountedString>& events_str,
127 bool has_more_events) {
128 if (events_str->size()) {
129 WriteCommaIfNeeded();
130
131 PCHECK(fwrite(events_str->data().c_str(), 1, events_str->data().length(),
132 trace_file_) == events_str->data().length());
133 }
134
135 if (!has_more_events && !done_callback.is_null())
136 done_callback.Run();
137 }
138
139 void Tracer::OnDataAvailable(const void* data, size_t num_bytes) {
140 const char* chars = static_cast<const char*>(data);
141 trace_service_data_.append(chars, num_bytes);
142 }
143
144 void Tracer::OnDataComplete() {
145 if (!trace_service_data_.empty()) {
146 WriteCommaIfNeeded();
147 const char* const chars = trace_service_data_.data();
148 size_t num_bytes = trace_service_data_.length();
149 PCHECK(fwrite(chars, 1, num_bytes, trace_file_) == num_bytes);
150 trace_service_data_ = std::string();
151 }
152 drainer_.reset();
153 coordinator_.reset();
154 WriteFooterAndClose();
155 }
156
157 void Tracer::WriteCommaIfNeeded() {
158 if (first_chunk_written_)
159 PCHECK(fwrite(",", 1, 1, trace_file_) == 1);
160 first_chunk_written_ = true;
161 }
162
163 } // namespace shell
164 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/standalone/tracer.h ('k') | mojo/shell/switches.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698