1 // Copyright 2014 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 #ifndef CHROME_BROWSER_MAC_MAC_STARTUP_PROFILER_H_
6 #define CHROME_BROWSER_MAC_MAC_STARTUP_PROFILER_H_
7 
8 #include <map>
9 
10 #include "base/macros.h"
11 #include "base/memory/singleton.h"
12 #include "base/time/time.h"
13 
14 // A lightweight profiler of startup performance. Records UMA metrics for the
15 // time delta between Chrome's launch and major initialization phases.
16 class MacStartupProfiler {
17  public:
18   // Returns pointer to the singleton instance for the current process.
19   static MacStartupProfiler* GetInstance();
20 
21   MacStartupProfiler();
22   ~MacStartupProfiler();
23 
24   // These locations correspond to major phases of Chrome startup.
25   // Profiling of these locations should occur at the beginning of the method
26   // indicated by the enum name.
27   // The ordering of the enum matches the order in which the initialization
28   // phases are reached.
29   enum Location {
30     PRE_MAIN_MESSAGE_LOOP_START,
31     AWAKE_FROM_NIB,
32     POST_MAIN_MESSAGE_LOOP_START,
33     PRE_PROFILE_INIT,
34     POST_PROFILE_INIT,
35     WILL_FINISH_LAUNCHING,
36     DID_FINISH_LAUNCHING,
37   };
38 
39   // Record timestamp for the given location event.
40   void Profile(Location location);
41 
42   // Call once to record all UMA metrics for all profiled locations.
43   void RecordMetrics();
44 
45  private:
46   friend struct base::DefaultSingletonTraits<MacStartupProfiler>;
47 
48   // Returns the name of the histogram for the given location.
49   const std::string HistogramName(Location location);
50 
51   // Records UMA metrics for a specific location.
52   void RecordHistogram(Location location, const base::TimeDelta& delta);
53 
54   // Keeps track of the time at which each initialization phase was reached.
55   std::map<Location, base::TimeTicks> profiled_ticks_;
56 
57   // Whether UMA metrics have been recorded. Only record UMA metrics once.
58   bool recorded_metrics_;
59 
60   DISALLOW_COPY_AND_ASSIGN(MacStartupProfiler);
61 };
62 
63 #endif  // CHROME_BROWSER_MAC_MAC_STARTUP_PROFILER_H_
64