blob: 4cfd9efb4332403476dfc46e8d36415181888a49 [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
David Sharp4324e9a2015-08-05 14:23:41 -070055 // Runs the perf tool with the request command for |duration_secs| seconds
56 // and returns either a perf_data or perf_stat protobuf in serialized form.
57 int GetPerfOutput(const uint32_t& duration_secs,
58 const std::vector<std::string>& perf_args,
59 std::vector<uint8_t>* perf_data,
60 std::vector<uint8_t>* perf_stat,
61 DBus::Error* error);
Simon Que795327b2015-03-16 17:42:35 -070062
Simon Queac5f9cf2015-06-20 13:50:22 -070063 // Randomly runs the perf tool in various modes and collects various events
64 // for |duration_secs| seconds and returns either a perf_data or perf_stat
65 // protobuf in binary form.
66 int GetRandomPerfOutput(const uint32_t& duration_secs,
67 std::vector<uint8_t>* perf_data,
68 std::vector<uint8_t>* perf_stat,
69 DBus::Error* error);
70
David Sharp4324e9a2015-08-05 14:23:41 -070071 // Randomly runs the perf tool in various modes and collects various events
72 // for |duration_secs| seconds and returns a protobuf containing the collected
73 // data.
74 std::vector<uint8_t> GetRichPerfData(const uint32_t& duration_secs,
75 DBus::Error* error);
David Sharp61901312015-08-05 13:32:07 -070076
Ahmad Sharifae1714d2013-01-17 11:29:37 -080077 private:
Ahmad Shariff5597f62013-04-25 12:25:41 -070078 // Helper function that runs perf for a given |duration_secs| returning the
Simon Queac5f9cf2015-06-20 13:50:22 -070079 // collected data in |data_string|. Return value is the status from running
80 // perf.
81 int GetPerfOutputHelper(const uint32_t& duration_secs,
82 const std::vector<std::string>& perf_args,
83 DBus::Error* error,
84 std::string* data_string);
Ahmad Shariff5597f62013-04-25 12:25:41 -070085
Simon Que795327b2015-03-16 17:42:35 -070086 std::unique_ptr<RandomSelector> random_selector_;
Ahmad Shariff5597f62013-04-25 12:25:41 -070087
Ahmad Sharifae1714d2013-01-17 11:29:37 -080088 DISALLOW_COPY_AND_ASSIGN(PerfTool);
89};
90
Ben Chana0011d82014-05-13 00:19:29 -070091} // namespace debugd
Ahmad Sharifae1714d2013-01-17 11:29:37 -080092
Alex Vakulenko262be3f2014-07-30 15:25:50 -070093#endif // DEBUGD_SRC_PERF_TOOL_H_