blob: a043b2860a8592bfaedc7e3f569e3911e35d61cd [file] [log] [blame]
Ahmad Sharifae1714d2013-01-17 11:29:37 -08001// Copyright (c) 2012 The Chromium OS 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
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#ifndef DEBUGD_SRC_PERF_TOOL_H_
6#define DEBUGD_SRC_PERF_TOOL_H_
Ahmad Sharifae1714d2013-01-17 11:29:37 -08007
Ben Chan49d264c2014-08-06 17:38:16 -07008#include <stdint.h>
9
Simon Que795327b2015-03-16 17:42:35 -070010#include <memory>
Ahmad Sharifae1714d2013-01-17 11:29:37 -080011#include <string>
Ben Chana0011d82014-05-13 00:19:29 -070012#include <vector>
Ahmad Sharifae1714d2013-01-17 11:29:37 -080013
Ben Chan657bed82014-09-02 20:40:51 -070014#include <base/macros.h>
Ahmad Sharifae1714d2013-01-17 11:29:37 -080015#include <dbus-c++/dbus.h>
16
17namespace debugd {
18
Simon Que795327b2015-03-16 17:42:35 -070019class RandomSelector;
20
Ahmad Sharifae1714d2013-01-17 11:29:37 -080021class PerfTool {
22 public:
Simon Que795327b2015-03-16 17:42:35 -070023 // Class that returns the CPU arch and model. This is in a separate class so
24 // it can be mocked out for testing.
25 class CPUInfoReader {
26 public:
27 CPUInfoReader();
28 virtual ~CPUInfoReader() {}
29
30 // Accessors
31 const std::string& arch() const {
32 return arch_;
33 }
34 const std::string& model() const {
35 return model_;
36 }
37
38 protected:
39 // The CPU arch and model info.
40 std::string arch_;
41 std::string model_;
42 };
43
Ahmad Sharifae1714d2013-01-17 11:29:37 -080044 PerfTool();
Simon Que795327b2015-03-16 17:42:35 -070045 // This is a special constructor for testing that takes in CPUInfoReader and
46 // RandomSelector args. In particular, it takes ownership of
47 // |random_selector|.
48 PerfTool(const CPUInfoReader& cpu_info, RandomSelector* random_selector);
Ben Chan78f89532014-08-29 09:35:09 -070049 ~PerfTool() = default;
Ahmad Sharifae1714d2013-01-17 11:29:37 -080050
Ahmad Shariff5597f62013-04-25 12:25:41 -070051 // Randomly runs the perf tool in various modes and collects various events
52 // for |duration_secs| seconds and returns a protobuf containing the collected
53 // data.
Ben Chan49d264c2014-08-06 17:38:16 -070054 std::vector<uint8_t> GetRichPerfData(const uint32_t& duration_secs,
55 DBus::Error* error);
Simon Que795327b2015-03-16 17:42:35 -070056
Simon Queac5f9cf2015-06-20 13:50:22 -070057 // Randomly runs the perf tool in various modes and collects various events
58 // for |duration_secs| seconds and returns either a perf_data or perf_stat
59 // protobuf in binary form.
60 int GetRandomPerfOutput(const uint32_t& duration_secs,
61 std::vector<uint8_t>* perf_data,
62 std::vector<uint8_t>* perf_stat,
63 DBus::Error* error);
64
Ahmad Sharifae1714d2013-01-17 11:29:37 -080065 private:
Ahmad Shariff5597f62013-04-25 12:25:41 -070066 // Helper function that runs perf for a given |duration_secs| returning the
Simon Queac5f9cf2015-06-20 13:50:22 -070067 // collected data in |data_string|. Return value is the status from running
68 // perf.
69 int GetPerfOutputHelper(const uint32_t& duration_secs,
70 const std::vector<std::string>& perf_args,
71 DBus::Error* error,
72 std::string* data_string);
Ahmad Shariff5597f62013-04-25 12:25:41 -070073
Simon Que795327b2015-03-16 17:42:35 -070074 std::unique_ptr<RandomSelector> random_selector_;
Ahmad Shariff5597f62013-04-25 12:25:41 -070075
Ahmad Sharifae1714d2013-01-17 11:29:37 -080076 DISALLOW_COPY_AND_ASSIGN(PerfTool);
77};
78
Ben Chana0011d82014-05-13 00:19:29 -070079} // namespace debugd
Ahmad Sharifae1714d2013-01-17 11:29:37 -080080
Alex Vakulenko262be3f2014-07-30 15:25:50 -070081#endif // DEBUGD_SRC_PERF_TOOL_H_