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