blob: 72cce60175e0a51004175838771c296aaa4a00b9 [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>
Chinglin Yu0d4368d2018-10-12 13:54:41 +080020#include <brillo/process_reaper.h>
21
22#include "debugd/src/sandboxed_process.h"
Ahmad Sharifae1714d2013-01-17 11:29:37 -080023
24namespace debugd {
25
26class PerfTool {
27 public:
28 PerfTool();
Ben Chan78f89532014-08-29 09:35:09 -070029 ~PerfTool() = default;
Ahmad Sharifae1714d2013-01-17 11:29:37 -080030
David Sharp4324e9a2015-08-05 14:23:41 -070031 // Runs the perf tool with the request command for |duration_secs| seconds
32 // and returns either a perf_data or perf_stat protobuf in serialized form.
Eric Caruso56eacb22017-08-04 11:00:37 -070033 bool GetPerfOutput(uint32_t duration_secs,
34 const std::vector<std::string>& perf_args,
35 std::vector<uint8_t>* perf_data,
36 std::vector<uint8_t>* perf_stat,
37 int32_t* status,
38 brillo::ErrorPtr* error);
Simon Que795327b2015-03-16 17:42:35 -070039
David Sharp16d35652016-04-05 17:20:08 -070040 // Runs the perf tool with the request command for |duration_secs| seconds
41 // and returns either a perf_data or perf_stat protobuf in serialized form
42 // over the passed stdout_fd file descriptor, or nothing if there was an
Chinglin Yu0d4368d2018-10-12 13:54:41 +080043 // error. |session_id| is returned to the client to optionally stop the perf
44 // tool before it runs for the full duration.
Eric Caruso56eacb22017-08-04 11:00:37 -070045 bool GetPerfOutputFd(uint32_t duration_secs,
David Sharp16d35652016-04-05 17:20:08 -070046 const std::vector<std::string>& perf_args,
Eric Caruso0b241882018-04-04 13:43:46 -070047 const base::ScopedFD& stdout_fd,
Chinglin Yu0d4368d2018-10-12 13:54:41 +080048 uint64_t* session_id,
Eric Carusocc7106c2017-04-27 14:22:42 -070049 brillo::ErrorPtr* error);
David Sharp16d35652016-04-05 17:20:08 -070050
Chinglin Yu0d4368d2018-10-12 13:54:41 +080051 // Stops the perf tool that was previously launched using GetPerfOutputFd()
52 // and gathers perf output right away.
53 bool StopPerf(uint64_t session_id, brillo::ErrorPtr* error);
54
Ahmad Sharifae1714d2013-01-17 11:29:37 -080055 private:
Chinglin Yu0d4368d2018-10-12 13:54:41 +080056 void OnQuipperProcessExited(const siginfo_t& siginfo);
57
58 base::Optional<uint64_t> profiler_session_id_;
59 std::unique_ptr<SandboxedProcess> quipper_process_;
Anand K Mistrya8b97f92020-04-24 10:32:18 +100060 base::ScopedFD quipper_process_output_fd_;
Chinglin Yu0d4368d2018-10-12 13:54:41 +080061 brillo::AsynchronousSignalHandler signal_handler_;
62 brillo::ProcessReaper process_reaper_;
63
Ahmad Sharifae1714d2013-01-17 11:29:37 -080064 DISALLOW_COPY_AND_ASSIGN(PerfTool);
65};
66
Ben Chana0011d82014-05-13 00:19:29 -070067} // namespace debugd
Ahmad Sharifae1714d2013-01-17 11:29:37 -080068
Alex Vakulenko262be3f2014-07-30 15:25:50 -070069#endif // DEBUGD_SRC_PERF_TOOL_H_