blob: 8bd7ef23bc6b38298bf4ebbfd89f07748d65300e [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
Chinglin Yu0d4368d2018-10-12 13:54:41 +080011#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
Eric Caruso0b241882018-04-04 13:43:46 -070015#include <base/files/scoped_file.h>
Ben Chan657bed82014-09-02 20:40:51 -070016#include <base/macros.h>
Chinglin Yu0d4368d2018-10-12 13:54:41 +080017#include <base/optional.h>
18#include <brillo/asynchronous_signal_handler.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070019#include <brillo/errors/error.h>
Simon Glass2b1da092020-05-21 12:24:16 -060020#include <brillo/process/process_reaper.h>
Chinglin Yu0d4368d2018-10-12 13:54:41 +080021
22#include "debugd/src/sandboxed_process.h"
Ahmad Sharifae1714d2013-01-17 11:29:37 -080023
24namespace debugd {
25
26class PerfTool {
27 public:
28 PerfTool();
Qijiang Fan6bc59e12020-11-11 02:51:06 +090029 PerfTool(const PerfTool&) = delete;
30 PerfTool& operator=(const PerfTool&) = delete;
31
Ben Chan78f89532014-08-29 09:35:09 -070032 ~PerfTool() = default;
Ahmad Sharifae1714d2013-01-17 11:29:37 -080033
David Sharp4324e9a2015-08-05 14:23:41 -070034 // Runs the perf tool with the request command for |duration_secs| seconds
35 // and returns either a perf_data or perf_stat protobuf in serialized form.
Eric Caruso56eacb22017-08-04 11:00:37 -070036 bool GetPerfOutput(uint32_t duration_secs,
37 const std::vector<std::string>& perf_args,
38 std::vector<uint8_t>* perf_data,
39 std::vector<uint8_t>* perf_stat,
40 int32_t* status,
41 brillo::ErrorPtr* error);
Simon Que795327b2015-03-16 17:42:35 -070042
David Sharp16d35652016-04-05 17:20:08 -070043 // Runs the perf tool with the request command for |duration_secs| seconds
44 // and returns either a perf_data or perf_stat protobuf in serialized form
45 // over the passed stdout_fd file descriptor, or nothing if there was an
Chinglin Yu0d4368d2018-10-12 13:54:41 +080046 // error. |session_id| is returned to the client to optionally stop the perf
47 // tool before it runs for the full duration.
Eric Caruso56eacb22017-08-04 11:00:37 -070048 bool GetPerfOutputFd(uint32_t duration_secs,
David Sharp16d35652016-04-05 17:20:08 -070049 const std::vector<std::string>& perf_args,
Eric Caruso0b241882018-04-04 13:43:46 -070050 const base::ScopedFD& stdout_fd,
Chinglin Yu0d4368d2018-10-12 13:54:41 +080051 uint64_t* session_id,
Eric Carusocc7106c2017-04-27 14:22:42 -070052 brillo::ErrorPtr* error);
David Sharp16d35652016-04-05 17:20:08 -070053
Chinglin Yu0d4368d2018-10-12 13:54:41 +080054 // Stops the perf tool that was previously launched using GetPerfOutputFd()
55 // and gathers perf output right away.
56 bool StopPerf(uint64_t session_id, brillo::ErrorPtr* error);
57
Ahmad Sharifae1714d2013-01-17 11:29:37 -080058 private:
Chinglin Yu0d4368d2018-10-12 13:54:41 +080059 void OnQuipperProcessExited(const siginfo_t& siginfo);
60
61 base::Optional<uint64_t> profiler_session_id_;
62 std::unique_ptr<SandboxedProcess> quipper_process_;
Anand K Mistrya8b97f92020-04-24 10:32:18 +100063 base::ScopedFD quipper_process_output_fd_;
Chinglin Yu0d4368d2018-10-12 13:54:41 +080064 brillo::AsynchronousSignalHandler signal_handler_;
65 brillo::ProcessReaper process_reaper_;
Ahmad Sharifae1714d2013-01-17 11:29:37 -080066};
67
Ben Chana0011d82014-05-13 00:19:29 -070068} // namespace debugd
Ahmad Sharifae1714d2013-01-17 11:29:37 -080069
Alex Vakulenko262be3f2014-07-30 15:25:50 -070070#endif // DEBUGD_SRC_PERF_TOOL_H_