blob: 23ae792df813c0348d22f115166b615115e7b1ea [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>
David Sharp0001e252015-08-27 15:59:31 -07009#include <sys/utsname.h>
Ben Chan49d264c2014-08-06 17:38:16 -070010
Simon Que795327b2015-03-16 17:42:35 -070011#include <memory>
Ahmad Sharifae1714d2013-01-17 11:29:37 -080012#include <string>
Ben Chana0011d82014-05-13 00:19:29 -070013#include <vector>
Ahmad Sharifae1714d2013-01-17 11:29:37 -080014
Ben Chan657bed82014-09-02 20:40:51 -070015#include <base/macros.h>
Ahmad Sharifae1714d2013-01-17 11:29:37 -080016#include <dbus-c++/dbus.h>
17
David Sharp0001e252015-08-27 15:59:31 -070018#include "debugd/src/cpu_info_parser.h"
19
Ahmad Sharifae1714d2013-01-17 11:29:37 -080020namespace debugd {
21
Simon Que795327b2015-03-16 17:42:35 -070022class RandomSelector;
23
Ahmad Sharifae1714d2013-01-17 11:29:37 -080024class PerfTool {
25 public:
David Sharp0001e252015-08-27 15:59:31 -070026 // For injection of uname(2)
27 typedef std::function<int(struct utsname*)> UnameFunc;
Simon Que795327b2015-03-16 17:42:35 -070028
Ahmad Sharifae1714d2013-01-17 11:29:37 -080029 PerfTool();
David Sharp0001e252015-08-27 15:59:31 -070030 // This is a special constructor for testing that takes in CPUInfoParser and
31 // RandomSelector args and allows injection of uname(2). It takes ownership of
Simon Que795327b2015-03-16 17:42:35 -070032 // |random_selector|.
David Sharp0001e252015-08-27 15:59:31 -070033 PerfTool(const CPUInfoParser& cpu_info,
34 RandomSelector* random_selector,
35 UnameFunc uname_func);
Ben Chan78f89532014-08-29 09:35:09 -070036 ~PerfTool() = default;
Ahmad Sharifae1714d2013-01-17 11:29:37 -080037
David Sharp4324e9a2015-08-05 14:23:41 -070038 // Runs the perf tool with the request command for |duration_secs| seconds
39 // and returns either a perf_data or perf_stat protobuf in serialized form.
40 int GetPerfOutput(const uint32_t& duration_secs,
41 const std::vector<std::string>& perf_args,
42 std::vector<uint8_t>* perf_data,
43 std::vector<uint8_t>* perf_stat,
44 DBus::Error* error);
Simon Que795327b2015-03-16 17:42:35 -070045
Simon Queac5f9cf2015-06-20 13:50:22 -070046 // Randomly runs the perf tool in various modes and collects various events
47 // for |duration_secs| seconds and returns either a perf_data or perf_stat
48 // protobuf in binary form.
49 int GetRandomPerfOutput(const uint32_t& duration_secs,
50 std::vector<uint8_t>* perf_data,
51 std::vector<uint8_t>* perf_stat,
52 DBus::Error* error);
53
David Sharp4324e9a2015-08-05 14:23:41 -070054 // Randomly runs the perf tool in various modes and collects various events
55 // for |duration_secs| seconds and returns a protobuf containing the collected
56 // data.
57 std::vector<uint8_t> GetRichPerfData(const uint32_t& duration_secs,
58 DBus::Error* error);
David Sharp61901312015-08-05 13:32:07 -070059
Ahmad Sharifae1714d2013-01-17 11:29:37 -080060 private:
Ahmad Shariff5597f62013-04-25 12:25:41 -070061 // Helper function that runs perf for a given |duration_secs| returning the
Simon Queac5f9cf2015-06-20 13:50:22 -070062 // collected data in |data_string|. Return value is the status from running
63 // perf.
64 int GetPerfOutputHelper(const uint32_t& duration_secs,
65 const std::vector<std::string>& perf_args,
66 DBus::Error* error,
67 std::string* data_string);
Ahmad Shariff5597f62013-04-25 12:25:41 -070068
Simon Que795327b2015-03-16 17:42:35 -070069 std::unique_ptr<RandomSelector> random_selector_;
Ahmad Shariff5597f62013-04-25 12:25:41 -070070
Ahmad Sharifae1714d2013-01-17 11:29:37 -080071 DISALLOW_COPY_AND_ASSIGN(PerfTool);
72};
73
Ben Chana0011d82014-05-13 00:19:29 -070074} // namespace debugd
Ahmad Sharifae1714d2013-01-17 11:29:37 -080075
Alex Vakulenko262be3f2014-07-30 15:25:50 -070076#endif // DEBUGD_SRC_PERF_TOOL_H_