Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 1 | // 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 Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #ifndef DEBUGD_SRC_PERF_TOOL_H_ |
| 6 | #define DEBUGD_SRC_PERF_TOOL_H_ |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 7 | |
Ben Chan | 49d264c | 2014-08-06 17:38:16 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 10 | #include <memory> |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 11 | #include <string> |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 12 | #include <vector> |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 13 | |
Ben Chan | 657bed8 | 2014-09-02 20:40:51 -0700 | [diff] [blame] | 14 | #include <base/macros.h> |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 15 | #include <dbus-c++/dbus.h> |
| 16 | |
| 17 | namespace debugd { |
| 18 | |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 19 | class RandomSelector; |
| 20 | |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 21 | class PerfTool { |
| 22 | public: |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 23 | // 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 Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 34 | 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 Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | protected: |
| 42 | // The CPU arch and model info. |
| 43 | std::string arch_; |
David Sharp | e01f725 | 2015-06-30 16:24:05 -0700 | [diff] [blame] | 44 | std::string model_name_; // e.g. "Intel(R) Celeron(R) 2955U @ 1.40GHz" |
| 45 | std::string intel_family_model_; // e.g. "06_45" |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 48 | PerfTool(); |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 49 | // 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 Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 53 | ~PerfTool() = default; |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 54 | |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 55 | // 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 Chan | 49d264c | 2014-08-06 17:38:16 -0700 | [diff] [blame] | 58 | std::vector<uint8_t> GetRichPerfData(const uint32_t& duration_secs, |
| 59 | DBus::Error* error); |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 60 | |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 61 | // 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 | |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 69 | private: |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 70 | // Helper function that runs perf for a given |duration_secs| returning the |
Simon Que | ac5f9cf | 2015-06-20 13:50:22 -0700 | [diff] [blame] | 71 | // collected data in |data_string|. Return value is the status from running |
| 72 | // perf. |
| 73 | int GetPerfOutputHelper(const uint32_t& duration_secs, |
| 74 | const std::vector<std::string>& perf_args, |
| 75 | DBus::Error* error, |
| 76 | std::string* data_string); |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 77 | |
Simon Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 78 | std::unique_ptr<RandomSelector> random_selector_; |
Ahmad Sharif | f5597f6 | 2013-04-25 12:25:41 -0700 | [diff] [blame] | 79 | |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 80 | DISALLOW_COPY_AND_ASSIGN(PerfTool); |
| 81 | }; |
| 82 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 83 | } // namespace debugd |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 84 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 85 | #endif // DEBUGD_SRC_PERF_TOOL_H_ |