| OLD | NEW |
| 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 #include "content/browser/tracing/tracing_controller_impl.h" | 4 #include "content/browser/tracing/tracing_controller_impl.h" |
| 5 | 5 |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "third_party/zlib/zlib.h" | 10 #include "third_party/zlib/zlib.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 class StringTraceDataSink : public TracingController::TraceDataSink { | 111 class StringTraceDataSink : public TracingController::TraceDataSink { |
| 112 public: | 112 public: |
| 113 explicit StringTraceDataSink( | 113 explicit StringTraceDataSink( |
| 114 scoped_refptr<TracingController::TraceDataEndpoint> endpoint) | 114 scoped_refptr<TracingController::TraceDataEndpoint> endpoint) |
| 115 : endpoint_(endpoint) {} | 115 : endpoint_(endpoint) {} |
| 116 | 116 |
| 117 void AddTraceChunk(const std::string& chunk) override { | 117 void AddTraceChunk(const std::string& chunk) override { |
| 118 std::string trace_string; | 118 std::string trace_string; |
| 119 if (trace_.empty()) | 119 if (trace_.empty()) { |
| 120 trace_string = "{\"traceEvents\":["; | 120 trace_string = "{\"" + |
| 121 else | 121 std::string(base::trace_event::TracingAgent::kChromeTraceLabel) + |
| 122 "\":["; |
| 123 } else { |
| 122 trace_string = ","; | 124 trace_string = ","; |
| 125 } |
| 123 trace_string += chunk; | 126 trace_string += chunk; |
| 124 | 127 |
| 125 AddTraceChunkAndPassToEndpoint(trace_string); | 128 AddTraceChunkAndPassToEndpoint(trace_string); |
| 126 } | 129 } |
| 127 | 130 |
| 128 void AddTraceChunkAndPassToEndpoint(const std::string& chunk) { | 131 void AddTraceChunkAndPassToEndpoint(const std::string& chunk) { |
| 129 trace_ += chunk; | 132 trace_ += chunk; |
| 130 | 133 |
| 131 endpoint_->ReceiveTraceChunk(chunk); | 134 endpoint_->ReceiveTraceChunk(chunk); |
| 132 } | 135 } |
| 133 | 136 |
| 134 void SetSystemTrace(const std::string& data) override { | |
| 135 system_trace_ = data; | |
| 136 } | |
| 137 | |
| 138 void SetPowerTrace(const std::string& data) override { power_trace_ = data; } | |
| 139 | |
| 140 void Close() override { | 137 void Close() override { |
| 141 AddTraceChunkAndPassToEndpoint("]"); | 138 AddTraceChunkAndPassToEndpoint("]"); |
| 142 if (!system_trace_.empty()) | 139 |
| 143 AddTraceChunkAndPassToEndpoint(",\"systemTraceEvents\": " + | 140 for (auto const &it : GetAdditionalTracingAgentTrace()) |
| 144 system_trace_); | 141 AddTraceChunkAndPassToEndpoint(",\"" + it.first + "\": " + it.second); |
| 142 |
| 145 std::string metadataJSON; | 143 std::string metadataJSON; |
| 146 if (base::JSONWriter::Write(GetMetadata(), &metadataJSON) && | 144 if (base::JSONWriter::Write(GetMetadata(), &metadataJSON) && |
| 147 !metadataJSON.empty()) | 145 !metadataJSON.empty()) { |
| 148 AddTraceChunkAndPassToEndpoint(",\"metadata\": " + metadataJSON); | 146 AddTraceChunkAndPassToEndpoint(",\"" + |
| 149 if (!power_trace_.empty()) { | 147 std::string(base::trace_event::TracingAgent::kMetadataTraceLabel) + |
| 150 AddTraceChunkAndPassToEndpoint(",\"powerTraceAsString\": " + | 148 "\": " + metadataJSON); |
| 151 power_trace_); | |
| 152 } | 149 } |
| 153 | 150 |
| 154 AddTraceChunkAndPassToEndpoint("}"); | 151 AddTraceChunkAndPassToEndpoint("}"); |
| 155 | 152 |
| 156 scoped_ptr<const base::DictionaryValue> metadata(GetMetadata().DeepCopy()); | 153 scoped_ptr<const base::DictionaryValue> metadata(GetMetadata().DeepCopy()); |
| 157 endpoint_->ReceiveTraceFinalContents(metadata.Pass(), trace_); | 154 endpoint_->ReceiveTraceFinalContents(metadata.Pass(), trace_); |
| 158 } | 155 } |
| 159 | 156 |
| 160 private: | 157 private: |
| 161 ~StringTraceDataSink() override {} | 158 ~StringTraceDataSink() override {} |
| 162 | 159 |
| 163 scoped_refptr<TracingController::TraceDataEndpoint> endpoint_; | 160 scoped_refptr<TracingController::TraceDataEndpoint> endpoint_; |
| 164 std::string trace_; | 161 std::string trace_; |
| 165 std::string system_trace_; | |
| 166 std::string power_trace_; | |
| 167 | 162 |
| 168 DISALLOW_COPY_AND_ASSIGN(StringTraceDataSink); | 163 DISALLOW_COPY_AND_ASSIGN(StringTraceDataSink); |
| 169 }; | 164 }; |
| 170 | 165 |
| 171 class CompressedStringTraceDataSink : public TracingController::TraceDataSink { | 166 class CompressedStringTraceDataSink : public TracingController::TraceDataSink { |
| 172 public: | 167 public: |
| 173 explicit CompressedStringTraceDataSink( | 168 explicit CompressedStringTraceDataSink( |
| 174 scoped_refptr<TracingController::TraceDataEndpoint> endpoint) | 169 scoped_refptr<TracingController::TraceDataEndpoint> endpoint) |
| 175 : endpoint_(endpoint), already_tried_open_(false) {} | 170 : endpoint_(endpoint), already_tried_open_(false) {} |
| 176 | 171 |
| 177 void AddTraceChunk(const std::string& chunk) override { | 172 void AddTraceChunk(const std::string& chunk) override { |
| 178 std::string tmp = chunk; | 173 std::string tmp = chunk; |
| 179 scoped_refptr<base::RefCountedString> chunk_ptr = | 174 scoped_refptr<base::RefCountedString> chunk_ptr = |
| 180 base::RefCountedString::TakeString(&tmp); | 175 base::RefCountedString::TakeString(&tmp); |
| 181 BrowserThread::PostTask( | 176 BrowserThread::PostTask( |
| 182 BrowserThread::FILE, FROM_HERE, | 177 BrowserThread::FILE, FROM_HERE, |
| 183 base::Bind(&CompressedStringTraceDataSink::AddTraceChunkOnFileThread, | 178 base::Bind(&CompressedStringTraceDataSink::AddTraceChunkOnFileThread, |
| 184 this, chunk_ptr)); | 179 this, chunk_ptr)); |
| 185 } | 180 } |
| 186 | 181 |
| 187 void SetSystemTrace(const std::string& data) override { | |
| 188 system_trace_ = data; | |
| 189 } | |
| 190 | |
| 191 void SetPowerTrace(const std::string& data) override { power_trace_ = data; } | |
| 192 | |
| 193 void Close() override { | 182 void Close() override { |
| 194 BrowserThread::PostTask( | 183 BrowserThread::PostTask( |
| 195 BrowserThread::FILE, FROM_HERE, | 184 BrowserThread::FILE, FROM_HERE, |
| 196 base::Bind(&CompressedStringTraceDataSink::CloseOnFileThread, this)); | 185 base::Bind(&CompressedStringTraceDataSink::CloseOnFileThread, this)); |
| 197 } | 186 } |
| 198 | 187 |
| 199 private: | 188 private: |
| 200 ~CompressedStringTraceDataSink() override {} | 189 ~CompressedStringTraceDataSink() override {} |
| 201 | 190 |
| 202 bool OpenZStreamOnFileThread() { | 191 bool OpenZStreamOnFileThread() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 219 MAX_WBITS + 16, | 208 MAX_WBITS + 16, |
| 220 8, // memLevel = 8 is default. | 209 8, // memLevel = 8 is default. |
| 221 Z_DEFAULT_STRATEGY); | 210 Z_DEFAULT_STRATEGY); |
| 222 return result == 0; | 211 return result == 0; |
| 223 } | 212 } |
| 224 | 213 |
| 225 void AddTraceChunkOnFileThread( | 214 void AddTraceChunkOnFileThread( |
| 226 const scoped_refptr<base::RefCountedString> chunk_ptr) { | 215 const scoped_refptr<base::RefCountedString> chunk_ptr) { |
| 227 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 216 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 228 std::string trace; | 217 std::string trace; |
| 229 if (compressed_trace_data_.empty()) | 218 if (compressed_trace_data_.empty()) { |
| 230 trace = "{\"traceEvents\":["; | 219 trace = "{\"" + |
| 231 else | 220 std::string(base::trace_event::TracingAgent::kChromeTraceLabel) + |
| 221 "\":["; |
| 222 } else { |
| 232 trace = ","; | 223 trace = ","; |
| 224 } |
| 233 trace += chunk_ptr->data(); | 225 trace += chunk_ptr->data(); |
| 234 AddTraceChunkAndCompressOnFileThread(trace, false); | 226 AddTraceChunkAndCompressOnFileThread(trace, false); |
| 235 } | 227 } |
| 236 | 228 |
| 237 void AddTraceChunkAndCompressOnFileThread(const std::string& chunk, | 229 void AddTraceChunkAndCompressOnFileThread(const std::string& chunk, |
| 238 bool finished) { | 230 bool finished) { |
| 239 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 231 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 240 if (!OpenZStreamOnFileThread()) | 232 if (!OpenZStreamOnFileThread()) |
| 241 return; | 233 return; |
| 242 | 234 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 262 endpoint_->ReceiveTraceChunk(compressed_chunk); | 254 endpoint_->ReceiveTraceChunk(compressed_chunk); |
| 263 } | 255 } |
| 264 } while (stream_->avail_out == 0); | 256 } while (stream_->avail_out == 0); |
| 265 } | 257 } |
| 266 | 258 |
| 267 void CloseOnFileThread() { | 259 void CloseOnFileThread() { |
| 268 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 260 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 269 if (!OpenZStreamOnFileThread()) | 261 if (!OpenZStreamOnFileThread()) |
| 270 return; | 262 return; |
| 271 | 263 |
| 272 if (compressed_trace_data_.empty()) | 264 if (compressed_trace_data_.empty()) { |
| 273 AddTraceChunkAndCompressOnFileThread("{\"traceEvents\":[", false); | 265 AddTraceChunkAndCompressOnFileThread( |
| 266 "{\"" + |
| 267 std::string(base::trace_event::TracingAgent::kChromeTraceLabel) + |
| 268 "\":[", |
| 269 false); |
| 270 } |
| 271 AddTraceChunkAndCompressOnFileThread("]", false); |
| 274 | 272 |
| 275 AddTraceChunkAndCompressOnFileThread("]", false); | 273 for (auto const &it : GetAdditionalTracingAgentTrace()) { |
| 276 if (!system_trace_.empty()) { | |
| 277 AddTraceChunkAndCompressOnFileThread( | 274 AddTraceChunkAndCompressOnFileThread( |
| 278 ",\"systemTraceEvents\": " + system_trace_, false); | 275 ",\"" + it.first + "\": " + it.second, false); |
| 279 } | 276 } |
| 277 |
| 280 std::string metadataJSON; | 278 std::string metadataJSON; |
| 281 if (base::JSONWriter::Write(GetMetadata(), &metadataJSON) && | 279 if (base::JSONWriter::Write(GetMetadata(), &metadataJSON) && |
| 282 !metadataJSON.empty()) { | 280 !metadataJSON.empty()) { |
| 283 AddTraceChunkAndCompressOnFileThread(",\"metadata\": " + metadataJSON, | |
| 284 false); | |
| 285 } | |
| 286 if (!power_trace_.empty()) { | |
| 287 AddTraceChunkAndCompressOnFileThread( | 281 AddTraceChunkAndCompressOnFileThread( |
| 288 ",\"powerTraceAsString\": " + power_trace_, false); | 282 ",\"" + |
| 283 std::string(base::trace_event::TracingAgent::kMetadataTraceLabel) + |
| 284 "\": " + metadataJSON, |
| 285 false); |
| 289 } | 286 } |
| 290 AddTraceChunkAndCompressOnFileThread("}", true); | 287 AddTraceChunkAndCompressOnFileThread("}", true); |
| 291 | 288 |
| 292 deflateEnd(stream_.get()); | 289 deflateEnd(stream_.get()); |
| 293 stream_.reset(); | 290 stream_.reset(); |
| 294 | 291 |
| 295 scoped_ptr<const base::DictionaryValue> metadata(GetMetadata().DeepCopy()); | 292 scoped_ptr<const base::DictionaryValue> metadata(GetMetadata().DeepCopy()); |
| 296 endpoint_->ReceiveTraceFinalContents(metadata.Pass(), | 293 endpoint_->ReceiveTraceFinalContents(metadata.Pass(), |
| 297 compressed_trace_data_); | 294 compressed_trace_data_); |
| 298 } | 295 } |
| 299 | 296 |
| 300 scoped_refptr<TracingController::TraceDataEndpoint> endpoint_; | 297 scoped_refptr<TracingController::TraceDataEndpoint> endpoint_; |
| 301 scoped_ptr<z_stream> stream_; | 298 scoped_ptr<z_stream> stream_; |
| 302 bool already_tried_open_; | 299 bool already_tried_open_; |
| 303 std::string compressed_trace_data_; | 300 std::string compressed_trace_data_; |
| 304 std::string system_trace_; | |
| 305 std::string power_trace_; | |
| 306 | 301 |
| 307 DISALLOW_COPY_AND_ASSIGN(CompressedStringTraceDataSink); | 302 DISALLOW_COPY_AND_ASSIGN(CompressedStringTraceDataSink); |
| 308 }; | 303 }; |
| 309 | 304 |
| 310 } // namespace | 305 } // namespace |
| 311 | 306 |
| 307 TracingController::TraceDataSink::TraceDataSink() {} |
| 308 |
| 309 TracingController::TraceDataSink::~TraceDataSink() {} |
| 310 |
| 311 void TracingController::TraceDataSink::AddAgentTrace( |
| 312 const std::string& trace_label, |
| 313 const std::string& trace_data) { |
| 314 DCHECK(additional_tracing_agent_trace_.find(trace_label) == |
| 315 additional_tracing_agent_trace_.end()); |
| 316 additional_tracing_agent_trace_[trace_label] = trace_data; |
| 317 } |
| 318 |
| 319 const std::map<std::string, std::string>& |
| 320 TracingController::TraceDataSink::GetAdditionalTracingAgentTrace() const { |
| 321 return additional_tracing_agent_trace_; |
| 322 } |
| 323 |
| 312 void TracingController::TraceDataSink::AddMetadata( | 324 void TracingController::TraceDataSink::AddMetadata( |
| 313 const base::DictionaryValue& data) { | 325 const base::DictionaryValue& data) { |
| 314 metadata_.MergeDictionary(&data); | 326 metadata_.MergeDictionary(&data); |
| 315 } | 327 } |
| 316 | 328 |
| 317 const base::DictionaryValue& | 329 const base::DictionaryValue& |
| 318 TracingController::TraceDataSink::GetMetadata() const { | 330 TracingController::TraceDataSink::GetMetadata() const { |
| 319 return metadata_; | 331 return metadata_; |
| 320 } | 332 } |
| 321 | 333 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 346 return new StringTraceDataEndpoint(callback); | 358 return new StringTraceDataEndpoint(callback); |
| 347 } | 359 } |
| 348 | 360 |
| 349 scoped_refptr<TracingController::TraceDataEndpoint> | 361 scoped_refptr<TracingController::TraceDataEndpoint> |
| 350 TracingController::CreateFileEndpoint(const base::FilePath& file_path, | 362 TracingController::CreateFileEndpoint(const base::FilePath& file_path, |
| 351 const base::Closure& callback) { | 363 const base::Closure& callback) { |
| 352 return new FileTraceDataEndpoint(file_path, callback); | 364 return new FileTraceDataEndpoint(file_path, callback); |
| 353 } | 365 } |
| 354 | 366 |
| 355 } // namespace content | 367 } // namespace content |
| OLD | NEW |