blob: 7b1552f462241302f2f84e290a2d3da809db9e63 [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 }
David Sharpe01f7252015-06-30 16:24:05 -070034 const std::string& model_name() const {
35 return model_name_;
36 }
37 const std::string& intel_family_model() const {
38 return intel_family_model_;
Simon Que795327b2015-03-16 17:42:35 -070039 }
40
41 protected:
42 // The CPU arch and model info.
43 std::string arch_;
David Sharpe01f7252015-06-30 16:24:05 -070044 std::string model_name_; // e.g. "Intel(R) Celeron(R) 2955U @ 1.40GHz"
45 std::string intel_family_model_; // e.g. "06_45"
Simon Que795327b2015-03-16 17:42:35 -070046 };
47
Ahmad Sharifae1714d2013-01-17 11:29:37 -080048 PerfTool();
Simon Que795327b2015-03-16 17:42:35 -070049 // This is a special constructor for testing that takes in CPUInfoReader and
50 // RandomSelector args. In particular, it takes ownership of
51 // |random_selector|.
52 PerfTool(const CPUInfoReader& cpu_info, RandomSelector* random_selector);
Ben Chan78f89532014-08-29 09:35:09 -070053 ~PerfTool() = default;
Ahmad Sharifae1714d2013-01-17 11:29:37 -080054
Ahmad Shariff5597f62013-04-25 12:25:41 -070055 // Randomly runs the perf tool in various modes and collects various events
56 // for |duration_secs| seconds and returns a protobuf containing the collected
57 // data.
Ben Chan49d264c2014-08-06 17:38:16 -070058 std::vector<uint8_t> GetRichPerfData(const uint32_t& duration_secs,
59 DBus::Error* error);
Simon Que795327b2015-03-16 17:42:35 -070060
Simon Queac5f9cf2015-06-20 13:50:22 -070061 // Randomly runs the perf tool in various modes and collects various events
62 // for |duration_secs| seconds and returns either a perf_data or perf_stat
63 // protobuf in binary form.
64 int GetRandomPerfOutput(const uint32_t& duration_secs,
65 std::vector<uint8_t>* perf_data,
66 std::vector<uint8_t>* perf_stat,
67 DBus::Error* error);
68
David Sharp61901312015-08-05 13:32:07 -070069 int GetPerfOutput(const uint32_t& duration_secs,
70 const std::vector<std::string>& perf_args,
71 std::vector<uint8_t>* perf_data,
72 std::vector<uint8_t>* perf_stat,
73 DBus::Error* error);
74
Ahmad Sharifae1714d2013-01-17 11:29:37 -080075 private:
Ahmad Shariff5597f62013-04-25 12:25:41 -070076 // Helper function that runs perf for a given |duration_secs| returning the
Simon Queac5f9cf2015-06-20 13:50:22 -070077 // collected data in |data_string|. Return value is the status from running
78 // perf.
79 int GetPerfOutputHelper(const uint32_t& duration_secs,
80 const std::vector<std::string>& perf_args,
81 DBus::Error* error,
82 std::string* data_string);
Ahmad Shariff5597f62013-04-25 12:25:41 -070083
Simon Que795327b2015-03-16 17:42:35 -070084 std::unique_ptr<RandomSelector> random_selector_;
Ahmad Shariff5597f62013-04-25 12:25:41 -070085
Ahmad Sharifae1714d2013-01-17 11:29:37 -080086 DISALLOW_COPY_AND_ASSIGN(PerfTool);
87};
88
Ben Chana0011d82014-05-13 00:19:29 -070089} // namespace debugd
Ahmad Sharifae1714d2013-01-17 11:29:37 -080090
Alex Vakulenko262be3f2014-07-30 15:25:50 -070091#endif // DEBUGD_SRC_PERF_TOOL_H_