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