| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 "chrome/browser/metrics/leak_detector_controller.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "components/variations/variations_associated_data.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 | |
| 14 namespace metrics { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Reads parameters for the field trial variation. Any parameters not present in | |
| 19 // the variation info or that cannot be parsed will be filled in with default | |
| 20 // values. Returns a MemoryLeakReportProto with the parameter fields filled in. | |
| 21 MemoryLeakReportProto::Params GetVariationParameters() { | |
| 22 // Variation parameter names. | |
| 23 const char kFieldTrialName[] = "RuntimeMemoryLeakDetector"; | |
| 24 const char kSamplingRateParam[] = "sampling_rate"; | |
| 25 const char kMaxStackDepthParam[] = "max_stack_depth"; | |
| 26 const char kAnalysisIntervalKbParam[] = "analysis_interval_kb"; | |
| 27 const char kSizeSuspicionThresholdParam[] = "size_suspicion_threshold"; | |
| 28 const char kCallStackSuspicionThresholdParam[] = | |
| 29 "call_stack_suspicion_threshold"; | |
| 30 | |
| 31 // Default parameter values. | |
| 32 double kDefaultSamplingRate = 1.0f / 256; | |
| 33 size_t kDefaultMaxStackDepth = 4; | |
| 34 uint64_t kDefaultAnalysisIntervalKb = 32768; | |
| 35 uint32_t kDefaultSizeSuspicionThreshold = 4; | |
| 36 uint32_t kDefaultCallStackSuspicionThreshold = 4; | |
| 37 | |
| 38 double sampling_rate = 0; | |
| 39 size_t max_stack_depth = 0; | |
| 40 uint64_t analysis_interval_kb = 0; | |
| 41 uint32_t size_suspicion_threshold = 0; | |
| 42 uint32_t call_stack_suspicion_threshold = 0; | |
| 43 | |
| 44 // Get a mapping of param names to param values. | |
| 45 std::map<std::string, std::string> params; | |
| 46 variations::GetVariationParams(kFieldTrialName, ¶ms); | |
| 47 | |
| 48 // Even if the variation param data does not exist and |params| ends up empty, | |
| 49 // the below code will assign default values. | |
| 50 auto iter = params.find(kSamplingRateParam); | |
| 51 if (iter == params.end() || | |
| 52 !base::StringToDouble(iter->second, &sampling_rate)) { | |
| 53 sampling_rate = kDefaultSamplingRate; | |
| 54 } | |
| 55 | |
| 56 iter = params.find(kMaxStackDepthParam); | |
| 57 if (iter == params.end() || | |
| 58 !base::StringToSizeT(iter->second, &max_stack_depth)) { | |
| 59 max_stack_depth = kDefaultMaxStackDepth; | |
| 60 } | |
| 61 | |
| 62 iter = params.find(kAnalysisIntervalKbParam); | |
| 63 if (iter == params.end() || | |
| 64 !base::StringToUint64(iter->second, &analysis_interval_kb)) { | |
| 65 analysis_interval_kb = kDefaultAnalysisIntervalKb; | |
| 66 } | |
| 67 | |
| 68 iter = params.find(kSizeSuspicionThresholdParam); | |
| 69 if (iter == params.end() || | |
| 70 !base::StringToUint(iter->second, &size_suspicion_threshold)) { | |
| 71 size_suspicion_threshold = kDefaultSizeSuspicionThreshold; | |
| 72 } | |
| 73 | |
| 74 iter = params.find(kCallStackSuspicionThresholdParam); | |
| 75 if (iter == params.end() || | |
| 76 !base::StringToUint(iter->second, &call_stack_suspicion_threshold)) { | |
| 77 call_stack_suspicion_threshold = kDefaultCallStackSuspicionThreshold; | |
| 78 } | |
| 79 | |
| 80 MemoryLeakReportProto_Params result; | |
| 81 result.set_sampling_rate(sampling_rate); | |
| 82 result.set_max_stack_depth(max_stack_depth); | |
| 83 result.set_analysis_interval_bytes(analysis_interval_kb * 1024); | |
| 84 result.set_size_suspicion_threshold(size_suspicion_threshold); | |
| 85 result.set_call_stack_suspicion_threshold(call_stack_suspicion_threshold); | |
| 86 return result; | |
| 87 } | |
| 88 | |
| 89 } // namespace | |
| 90 | |
| 91 LeakDetectorController::LeakDetectorController() | |
| 92 : params_(GetVariationParameters()) { | |
| 93 LeakDetector* detector = LeakDetector::GetInstance(); | |
| 94 detector->AddObserver(this); | |
| 95 | |
| 96 // Leak detector parameters are stored in |params_|. | |
| 97 detector->Init(params_, content::BrowserThread::GetMessageLoopProxyForThread( | |
| 98 content::BrowserThread::UI)); | |
| 99 } | |
| 100 | |
| 101 LeakDetectorController::~LeakDetectorController() { | |
| 102 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 103 LeakDetector::GetInstance()->RemoveObserver(this); | |
| 104 } | |
| 105 | |
| 106 void LeakDetectorController::OnLeaksFound( | |
| 107 const std::vector<MemoryLeakReportProto>& reports) { | |
| 108 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 109 | |
| 110 for (const auto& report : reports) { | |
| 111 // Store the report and insert stored parameters. | |
| 112 stored_reports_.push_back(report); | |
| 113 stored_reports_.back().mutable_params()->CopyFrom(params_); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void LeakDetectorController::GetLeakReports( | |
| 118 std::vector<MemoryLeakReportProto>* reports) { | |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 120 *reports = std::move(stored_reports_); | |
| 121 } | |
| 122 | |
| 123 } // namespace metrics | |
| OLD | NEW |