summaryrefslogtreecommitdiffstats
path: root/chrome/browser/metrics/leak_detector_controller.cc
blob: aebd8a708591ef42e78f5a0b38d482cc075e6faf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/metrics/leak_detector_controller.h"

#include <algorithm>

#include "base/logging.h"

namespace metrics {

namespace {

// Default parameters for LeakDetector.
const float kSamplingRate = 1.0f / 256;
const int kMaxStackDepth = 4;
const uint64_t kAnalysisIntervalBytes = 32 * 1024 * 1024;
const int kSizeSuspicionThreshold = 4;
const int kCallStackSuspicionThreshold = 4;

}  // namespace

LeakDetectorController::LeakDetectorController() {
  LeakDetector* detector = LeakDetector::GetInstance();
  detector->AddObserver(this);
  detector->Init(kSamplingRate, kMaxStackDepth, kAnalysisIntervalBytes,
                 kSizeSuspicionThreshold, kCallStackSuspicionThreshold);
}

LeakDetectorController::~LeakDetectorController() {
  DCHECK(thread_checker_.CalledOnValidThread());
  LeakDetector::GetInstance()->RemoveObserver(this);
}

void LeakDetectorController::OnLeakFound(
    const LeakDetector::LeakReport& report) {
  DCHECK(thread_checker_.CalledOnValidThread());

  stored_reports_.push_back(MemoryLeakReportProto());
  MemoryLeakReportProto* proto = &stored_reports_.back();

  // Copy the contents of the report to the protobuf.
  proto->set_size_bytes(report.alloc_size_bytes);
  proto->mutable_call_stack()->Reserve(report.call_stack.size());
  for (uintptr_t call_stack_entry : report.call_stack)
    proto->mutable_call_stack()->Add(call_stack_entry);

  // Store the LeakDetector parameters in the protobuf.
  proto->set_sampling_rate(kSamplingRate);
  proto->set_max_stack_depth(kMaxStackDepth);
  proto->set_analysis_interval_bytes(kAnalysisIntervalBytes);
  proto->set_size_suspicion_threshold(kSizeSuspicionThreshold);
  proto->set_call_stack_suspicion_threshold(kCallStackSuspicionThreshold);
}

void LeakDetectorController::GetLeakReports(
    std::vector<MemoryLeakReportProto>* reports) {
  DCHECK(thread_checker_.CalledOnValidThread());
  *reports = std::move(stored_reports_);
}

}  // namespace metrics