Home | History | Annotate | Download | only in metrics
      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_METRICS_PERF_PROVIDER_CHROMEOS_H_
      6 #define CHROME_BROWSER_METRICS_PERF_PROVIDER_CHROMEOS_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/threading/non_thread_safe.h"
     12 #include "base/time/time.h"
     13 #include "base/timer/timer.h"
     14 #include "chrome/common/metrics/proto/perf_data.pb.h"
     15 
     16 namespace metrics {
     17 
     18 class WindowedIncognitoObserver;
     19 
     20 // Provides access to ChromeOS perf data. perf aka "perf events" is a
     21 // performance profiling infrastructure built into the linux kernel. For more
     22 // information, see: https://perf.wiki.kernel.org/index.php/Main_Page.
     23 class PerfProvider : public base::NonThreadSafe {
     24  public:
     25   PerfProvider();
     26   ~PerfProvider();
     27 
     28   // Gets the collected perf data protobuf and writes it to |perf_data_proto|.
     29   // Returns true if it wrote to |perf_data_proto|.
     30   bool GetPerfData(PerfDataProto* perf_data_proto);
     31 
     32  private:
     33   enum PerfDataState {
     34     // Indicates that we are ready to collect perf data.
     35     READY_TO_COLLECT,
     36 
     37     // Indicates that we are ready to upload perf data.
     38     READY_TO_UPLOAD,
     39   };
     40 
     41   // Starts an internal timer to start collecting perf data. The timer is set to
     42   // trigger |interval| after this function call.
     43   void ScheduleCollection(const base::TimeDelta& interval);
     44 
     45   // Collects perf data if it has not been consumed by calling GetPerfData()
     46   // (see above).
     47   void CollectIfNecessary();
     48 
     49   // Collects perf data by calling CollectIfNecessary() and reschedules it to be
     50   // collected again.
     51   void CollectIfNecessaryAndReschedule();
     52 
     53   // Parses a protobuf from the |data| passed in only if the
     54   // |incognito_observer| indicates that no incognito window had been opened
     55   // during the profile collection period.
     56   void ParseProtoIfValid(
     57       scoped_ptr<WindowedIncognitoObserver> incognito_observer,
     58       const std::vector<uint8>& data);
     59 
     60   // The internal state can be one of the enum values above.
     61   PerfDataState state_;
     62 
     63   // Protobuf that has the perf data.
     64   PerfDataProto perf_data_proto_;
     65 
     66   // For scheduling collection of perf data.
     67   base::OneShotTimer<PerfProvider> timer_;
     68 
     69   // To pass around the "this" pointer across threads safely.
     70   base::WeakPtrFactory<PerfProvider> weak_factory_;
     71 
     72   DISALLOW_COPY_AND_ASSIGN(PerfProvider);
     73 };
     74 
     75 }  // namespace system
     76 
     77 #endif  // CHROME_BROWSER_METRICS_PERF_PROVIDER_CHROMEOS_H_
     78