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

Side by Side Diff: content/browser/tracing/background_tracing_manager_impl.cc

Issue 1181213002: Slow Reports - Embed Metadata in Traces (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments addressed. Created 5 years, 6 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 "content/browser/tracing/background_tracing_manager_impl.h" 5 #include "content/browser/tracing/background_tracing_manager_impl.h"
6 6
7 #include "base/json/json_writer.h"
7 #include "base/macros.h" 8 #include "base/macros.h"
8 #include "base/metrics/histogram_macros.h" 9 #include "base/metrics/histogram_macros.h"
9 #include "base/time/time.h" 10 #include "base/time/time.h"
10 #include "content/public/browser/background_tracing_preemptive_config.h" 11 #include "content/public/browser/background_tracing_preemptive_config.h"
11 #include "content/public/browser/background_tracing_reactive_config.h" 12 #include "content/public/browser/background_tracing_reactive_config.h"
12 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/content_browser_client.h" 14 #include "content/public/browser/content_browser_client.h"
14 #include "content/public/browser/tracing_delegate.h" 15 #include "content/public/browser/tracing_delegate.h"
15 #include "content/public/common/content_client.h" 16 #include "content/public/common/content_client.h"
16 17
17 namespace content { 18 namespace content {
18 19
19 namespace { 20 namespace {
20 21
21 base::LazyInstance<BackgroundTracingManagerImpl>::Leaky g_controller = 22 base::LazyInstance<BackgroundTracingManagerImpl>::Leaky g_controller =
22 LAZY_INSTANCE_INITIALIZER; 23 LAZY_INSTANCE_INITIALIZER;
23 24
25 const char kMetaDataConfigKey[] = "config";
26 const char kMetaDataVersionKey[] = "product_version";
27
24 // These values are used for a histogram. Do not reorder. 28 // These values are used for a histogram. Do not reorder.
25 enum BackgroundTracingMetrics { 29 enum BackgroundTracingMetrics {
26 SCENARIO_ACTIVATION_REQUESTED = 0, 30 SCENARIO_ACTIVATION_REQUESTED = 0,
27 SCENARIO_ACTIVATED_SUCCESSFULLY = 1, 31 SCENARIO_ACTIVATED_SUCCESSFULLY = 1,
28 RECORDING_ENABLED = 2, 32 RECORDING_ENABLED = 2,
29 PREEMPTIVE_TRIGGERED = 3, 33 PREEMPTIVE_TRIGGERED = 3,
30 REACTIVE_TRIGGERED = 4, 34 REACTIVE_TRIGGERED = 4,
31 FINALIZATION_ALLOWED = 5, 35 FINALIZATION_ALLOWED = 5,
32 FINALIZATION_DISALLOWED = 6, 36 FINALIZATION_DISALLOWED = 6,
33 FINALIZATION_STARTED = 7, 37 FINALIZATION_STARTED = 7,
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 } 367 }
364 368
365 void BackgroundTracingManagerImpl::OnFinalizeStarted( 369 void BackgroundTracingManagerImpl::OnFinalizeStarted(
366 scoped_refptr<base::RefCountedString> file_contents) { 370 scoped_refptr<base::RefCountedString> file_contents) {
367 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 371 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
368 372
369 RecordBackgroundTracingMetric(FINALIZATION_STARTED); 373 RecordBackgroundTracingMetric(FINALIZATION_STARTED);
370 UMA_HISTOGRAM_MEMORY_KB("Tracing.Background.FinalizingTraceSizeInKB", 374 UMA_HISTOGRAM_MEMORY_KB("Tracing.Background.FinalizingTraceSizeInKB",
371 file_contents->size() / 1024); 375 file_contents->size() / 1024);
372 376
373 if (!receive_callback_.is_null()) 377 if (!receive_callback_.is_null()) {
374 receive_callback_.Run( 378 receive_callback_.Run(
375 file_contents, 379 file_contents, GenerateMetadataDict(),
376 base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete, 380 base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete,
377 base::Unretained(this))); 381 base::Unretained(this)));
382 }
378 } 383 }
379 384
380 void BackgroundTracingManagerImpl::OnFinalizeComplete() { 385 void BackgroundTracingManagerImpl::OnFinalizeComplete() {
381 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { 386 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
382 BrowserThread::PostTask( 387 BrowserThread::PostTask(
383 BrowserThread::UI, FROM_HERE, 388 BrowserThread::UI, FROM_HERE,
384 base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete, 389 base::Bind(&BackgroundTracingManagerImpl::OnFinalizeComplete,
385 base::Unretained(this))); 390 base::Unretained(this)));
386 return; 391 return;
387 } 392 }
388 393
389 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 394 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
390 395
391 is_gathering_ = false; 396 is_gathering_ = false;
392 397
393 if (!idle_callback_.is_null()) 398 if (!idle_callback_.is_null())
394 idle_callback_.Run(); 399 idle_callback_.Run();
395 400
396 // Now that a trace has completed, we may need to enable recording again. 401 // Now that a trace has completed, we may need to enable recording again.
397 EnableRecordingIfConfigNeedsIt(); 402 EnableRecordingIfConfigNeedsIt();
398 RecordBackgroundTracingMetric(FINALIZATION_COMPLETE); 403 RecordBackgroundTracingMetric(FINALIZATION_COMPLETE);
399 } 404 }
400 405
406 scoped_ptr<base::DictionaryValue>
407 BackgroundTracingManagerImpl::GenerateMetadataDict() const {
408 // Grab the product version.
409 std::string product_version = GetContentClient()->GetProduct();
410
411 // Serialize the config into json.
412 scoped_ptr<base::DictionaryValue> config_dict(new base::DictionaryValue());
413
414 BackgroundTracingConfig::IntoDict(config_.get(), config_dict.get());
415
416 scoped_ptr<base::DictionaryValue> metadata_dict(new base::DictionaryValue());
417 metadata_dict->Set(kMetaDataConfigKey, config_dict.Pass());
418 metadata_dict->SetString(kMetaDataVersionKey, product_version);
419
420 return metadata_dict.Pass();
421 }
422
401 void BackgroundTracingManagerImpl::BeginFinalizing( 423 void BackgroundTracingManagerImpl::BeginFinalizing(
402 StartedFinalizingCallback callback) { 424 StartedFinalizingCallback callback) {
403 is_gathering_ = true; 425 is_gathering_ = true;
404 is_tracing_ = false; 426 is_tracing_ = false;
405 427
406 bool is_allowed_finalization = 428 bool is_allowed_finalization =
407 !delegate_ || (config_ && 429 !delegate_ || (config_ &&
408 delegate_->IsAllowedToEndBackgroundScenario( 430 delegate_->IsAllowedToEndBackgroundScenario(
409 *config_.get(), requires_anonymized_data_)); 431 *config_.get(), requires_anonymized_data_));
410 432
411 scoped_refptr<TracingControllerImpl::TraceDataSink> trace_data_sink; 433 scoped_refptr<TracingControllerImpl::TraceDataSink> trace_data_sink;
412 if (is_allowed_finalization) { 434 if (is_allowed_finalization) {
413 trace_data_sink = content::TracingController::CreateCompressedStringSink( 435 trace_data_sink = content::TracingController::CreateCompressedStringSink(
414 data_endpoint_wrapper_); 436 data_endpoint_wrapper_);
415 RecordBackgroundTracingMetric(FINALIZATION_ALLOWED); 437 RecordBackgroundTracingMetric(FINALIZATION_ALLOWED);
438
439 if (auto metadata_dict = GenerateMetadataDict()) {
440 std::string results;
441 if (base::JSONWriter::Write(*metadata_dict.get(), &results))
442 trace_data_sink->SetMetadata(results);
443 }
416 } else { 444 } else {
417 RecordBackgroundTracingMetric(FINALIZATION_DISALLOWED); 445 RecordBackgroundTracingMetric(FINALIZATION_DISALLOWED);
418 } 446 }
419 447
420 content::TracingController::GetInstance()->DisableRecording(trace_data_sink); 448 content::TracingController::GetInstance()->DisableRecording(trace_data_sink);
421 449
422 if (!callback.is_null()) 450 if (!callback.is_null())
423 callback.Run(is_allowed_finalization); 451 callback.Run(is_allowed_finalization);
424 } 452 }
425 453
426 std::string 454 std::string
427 BackgroundTracingManagerImpl::GetCategoryFilterStringForCategoryPreset( 455 BackgroundTracingManagerImpl::GetCategoryFilterStringForCategoryPreset(
428 BackgroundTracingConfig::CategoryPreset preset) const { 456 BackgroundTracingConfig::CategoryPreset preset) const {
429 switch (preset) { 457 switch (preset) {
430 case BackgroundTracingConfig::CategoryPreset::BENCHMARK: 458 case BackgroundTracingConfig::CategoryPreset::BENCHMARK:
431 return "benchmark," 459 return "benchmark,"
432 "disabled-by-default-toplevel.flow," 460 "disabled-by-default-toplevel.flow,"
433 "disabled-by-default-ipc.flow"; 461 "disabled-by-default-ipc.flow";
434 case BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP: 462 case BackgroundTracingConfig::CategoryPreset::BENCHMARK_DEEP:
435 return "*,disabled-by-default-blink.debug.layout"; 463 return "*,disabled-by-default-blink.debug.layout";
436 } 464 }
437 NOTREACHED(); 465 NOTREACHED();
438 return ""; 466 return "";
439 } 467 }
440 468
441 } // namspace content 469 } // namspace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698