1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef SRC_SYSTEM_WRAPPERS_SOURCE_CPU_MEASUREMENT_HARNESS_H_
12#define SRC_SYSTEM_WRAPPERS_SOURCE_CPU_MEASUREMENT_HARNESS_H_
13
14#include "system_wrappers/interface/scoped_ptr.h"
15
16namespace webrtc {
17
18class CpuWrapper;
19class EventWrapper;
20class ThreadWrapper;
21
22// This abstract class provides an interface that should be passed to
23// CpuMeasurementHarness. CpuMeasurementHarness will call it with the
24// frequency requested and measure the CPU usage for all calls.
25class CpuTarget {
26 public:
27  // Callback function for which the CPU usage should be calculated.
28  virtual bool DoWork() = 0;
29
30 protected:
31  CpuTarget() {}
32  virtual ~CpuTarget() {}
33};
34
35class CpuMeasurementHarness {
36 public:
37  static CpuMeasurementHarness* Create(CpuTarget* target,
38                                       int work_period_ms,
39                                       int work_iterations_per_period,
40                                       int duration_ms);
41  ~CpuMeasurementHarness();
42  bool Run();
43  int AverageCpu();
44
45 protected:
46  CpuMeasurementHarness(CpuTarget* target, int work_period_ms,
47                        int work_iterations_per_period, int duration_ms);
48
49 private:
50  bool WaitForCpuInit();
51  void Measure();
52  bool DoWork();
53
54  CpuTarget* cpu_target_;
55  const int work_period_ms_;
56  const int work_iterations_per_period_;
57  const int duration_ms_;
58  int cpu_sum_;
59  int cpu_iterations_;
60  scoped_ptr<CpuWrapper> cpu_;
61  scoped_ptr<EventWrapper> event_;
62};
63
64}  // namespace webrtc
65
66#endif  // SRC_SYSTEM_WRAPPERS_SOURCE_CPU_MEASUREMENT_HARNESS_H_
67