Home | History | Annotate | Download | only in performance_monitor
      1 // Copyright (c) 2012 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_PERFORMANCE_MONITOR_STARTUP_TIMER_H_
      6 #define CHROME_BROWSER_PERFORMANCE_MONITOR_STARTUP_TIMER_H_
      7 
      8 #include "base/time/time.h"
      9 #include "content/public/browser/notification_observer.h"
     10 #include "content/public/browser/notification_registrar.h"
     11 
     12 namespace performance_monitor {
     13 
     14 // This class is responsible for recording the startup and session restore times
     15 // (if applicable) for PerformanceMonitor. This allows us to initialize this
     16 // relatively small object early in the startup process, and start the whole of
     17 // PerformanceMonitor at a later time. StartupTimer will record the times and
     18 // insert them into PerformanceMonitor's database.
     19 class StartupTimer : public content::NotificationObserver {
     20  public:
     21   // Indicates the type of startup; i.e. either a normal startup or a testing
     22   // environment.
     23   enum StartupType {
     24     STARTUP_NORMAL,
     25     STARTUP_TEST
     26   };
     27 
     28   StartupTimer();
     29   virtual ~StartupTimer();
     30 
     31   // Inform StartupTimer that the startup process has been completed; stop the
     32   // timer. Returns false if the timer has already stopped.
     33   bool SignalStartupComplete(StartupType startup_type);
     34 
     35   // Pauses the timer until UnpauseTimer() is called; any time spent within a
     36   // pause does not count towards the measured startup time. This will DCHECK if
     37   // PauseTimer() is called while paused or UnpauseTimer() is called while
     38   // unpaused.
     39   static void PauseTimer();
     40   static void UnpauseTimer();
     41 
     42   // content::NotificationObserver
     43   // We keep track of whether or not PerformanceMonitor has been started via
     44   // the PERFORMANCE_MONITOR_INITIALIZED notification; we need to know this so
     45   // we know when to insert startup data into the database. We either insert
     46   // data as we gather it (if PerformanceMonitor is started prior to data
     47   // collection) or at the notification (if PerformanceMonitor is started
     48   // later).
     49   virtual void Observe(int type,
     50                        const content::NotificationSource& source,
     51                        const content::NotificationDetails& details) OVERRIDE;
     52 
     53   static void SetElapsedSessionRestoreTime(
     54       const base::TimeDelta& elapsed_session_restore_time);
     55 
     56  private:
     57   // Insert the elapsed time measures into PerformanceMonitor's database.
     58   void InsertElapsedStartupTime();
     59   void InsertElapsedSessionRestoreTime();
     60 
     61   // The time at which the startup process begins (the creation of
     62   // ChromeBrowserMain).
     63   base::TimeTicks startup_begin_;
     64 
     65   // The time at which the timer was most recently paused, or null if the timer
     66   // is not currently paused.
     67   base::TimeTicks pause_started_;
     68 
     69   // The total duration for which the timer has been paused.
     70   base::TimeDelta total_pause_;
     71 
     72   // A flag of whether or not this was a "normal" startup (e.g. whether or not
     73   // this was in a testing environment, which would change the startup time
     74   // values). If it is not a normal startup, we use a different metric.
     75   StartupType startup_type_;
     76 
     77   // The total duration of the startup process, minus any pauses.
     78   base::TimeDelta elapsed_startup_time_;
     79 
     80   // The total duration of the session restore(s), if any occurred. This is
     81   // independent of the startup time, because:
     82   // - If the user has auto-restore on, the restore is synchronous, and we pause
     83   //   the startup timer during the session restore; the restore will not
     84   //   interfere with startup timing.
     85   // - If Chrome crashed and the user chooses to restore the crashed session,
     86   //   then the startup is already completed; the restore will not interfere
     87   //   with startup timing.
     88   std::vector<base::TimeDelta> elapsed_session_restore_times_;
     89 
     90   // Flag whether or not PerformanceMonitor has been fully started.
     91   bool performance_monitor_initialized_;
     92 
     93   content::NotificationRegistrar registrar_;
     94 
     95   // The singleton of this class.
     96   static StartupTimer* g_startup_timer_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(StartupTimer);
     99 };
    100 
    101 }  // namespace performance_monitor
    102 
    103 #endif  // CHROME_BROWSER_PERFORMANCE_MONITOR_STARTUP_TIMER_H_
    104