summaryrefslogtreecommitdiffstats
path: root/chrome/browser/mac/mac_startup_profiler.cc
blob: 91a8719f67560f4e13a95aecbab014ae2b6bfc07 (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
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2014 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/mac/mac_startup_profiler.h"

#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"

// static
MacStartupProfiler* MacStartupProfiler::GetInstance() {
  return base::Singleton<MacStartupProfiler>::get();
}

MacStartupProfiler::MacStartupProfiler() : recorded_metrics_(false) {
}

MacStartupProfiler::~MacStartupProfiler() {
}

void MacStartupProfiler::Profile(Location location) {
  profiled_ticks_[location] = base::TimeTicks::Now();
}

void MacStartupProfiler::RecordMetrics() {
  const base::TimeTicks main_entry_ticks =
      startup_metric_utils::MainEntryPointTicks();
  DCHECK(!main_entry_ticks.is_null());
  DCHECK(!recorded_metrics_);

  recorded_metrics_ = true;

  for (const std::pair<Location, base::TimeTicks>& entry : profiled_ticks_)
    RecordHistogram(entry.first, entry.second - main_entry_ticks);
}

const std::string MacStartupProfiler::HistogramName(Location location) {
  std::string prefix("Startup.OSX.");
  switch (location) {
    case PRE_MAIN_MESSAGE_LOOP_START:
      return prefix + "PreMainMessageLoopStart";
    case AWAKE_FROM_NIB:
      return prefix + "AwakeFromNib";
    case POST_MAIN_MESSAGE_LOOP_START:
      return prefix + "PostMainMessageLoopStart";
    case PRE_PROFILE_INIT:
      return prefix + "PreProfileInit";
    case POST_PROFILE_INIT:
      return prefix + "PostProfileInit";
    case WILL_FINISH_LAUNCHING:
      return prefix + "WillFinishLaunching";
    case DID_FINISH_LAUNCHING:
      return prefix + "DockIconWillFinishBouncing";
  }
}

void MacStartupProfiler::RecordHistogram(Location location,
                                         const base::TimeDelta& delta) {
  const std::string name(HistogramName(location));
  base::TimeDelta min = base::TimeDelta::FromMilliseconds(10);
  base::TimeDelta max = base::TimeDelta::FromMinutes(1);
  int bucket_count = 100;

  // No need to cache the histogram pointers, since each invocation of this
  // method will be the first and only usage of a histogram with that given
  // name.
  base::HistogramBase* histogram = base::Histogram::FactoryTimeGet(
      name,
      min,
      max,
      bucket_count,
      base::HistogramBase::kUmaTargetedHistogramFlag);
  histogram->AddTime(delta);
}