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> |
David Sharp | 0001e25 | 2015-08-27 15:59:31 -0700 | [diff] [blame] | 9 | #include <sys/utsname.h> |
Ben Chan | 49d264c | 2014-08-06 17:38:16 -0700 | [diff] [blame] | 10 | |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 11 | #include <memory> |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 12 | #include <string> |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 13 | #include <vector> |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 14 | |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 15 | #include <base/files/scoped_file.h> |
Ben Chan | 657bed8 | 2014-09-02 20:40:51 -0700 | [diff] [blame] | 16 | #include <base/macros.h> |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 17 | #include <base/optional.h> |
| 18 | #include <brillo/asynchronous_signal_handler.h> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 19 | #include <brillo/errors/error.h> |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame] | 20 | #include <brillo/process/process_reaper.h> |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 21 | |
| 22 | #include "debugd/src/sandboxed_process.h" |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 23 | |
| 24 | namespace debugd { |
| 25 | |
| 26 | class PerfTool { |
| 27 | public: |
| 28 | PerfTool(); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 29 | PerfTool(const PerfTool&) = delete; |
| 30 | PerfTool& operator=(const PerfTool&) = delete; |
| 31 | |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 32 | ~PerfTool() = default; |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 33 | |
David Sharp | 4324e9a | 2015-08-05 14:23:41 -0700 | [diff] [blame] | 34 | // 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 Caruso | 56eacb2 | 2017-08-04 11:00:37 -0700 | [diff] [blame] | 36 | 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 Que | 795327b | 2015-03-16 17:42:35 -0700 | [diff] [blame] | 42 | |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 43 | // 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 Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 46 | // error. |session_id| is returned to the client to optionally stop the perf |
| 47 | // tool before it runs for the full duration. |
Eric Caruso | 56eacb2 | 2017-08-04 11:00:37 -0700 | [diff] [blame] | 48 | bool GetPerfOutputFd(uint32_t duration_secs, |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 49 | const std::vector<std::string>& perf_args, |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 50 | const base::ScopedFD& stdout_fd, |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 51 | uint64_t* session_id, |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 52 | brillo::ErrorPtr* error); |
David Sharp | 16d3565 | 2016-04-05 17:20:08 -0700 | [diff] [blame] | 53 | |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 54 | // 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 Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 58 | private: |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 59 | void OnQuipperProcessExited(const siginfo_t& siginfo); |
| 60 | |
| 61 | base::Optional<uint64_t> profiler_session_id_; |
| 62 | std::unique_ptr<SandboxedProcess> quipper_process_; |
Anand K Mistry | a8b97f9 | 2020-04-24 10:32:18 +1000 | [diff] [blame] | 63 | base::ScopedFD quipper_process_output_fd_; |
Chinglin Yu | 0d4368d | 2018-10-12 13:54:41 +0800 | [diff] [blame] | 64 | brillo::AsynchronousSignalHandler signal_handler_; |
| 65 | brillo::ProcessReaper process_reaper_; |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 68 | } // namespace debugd |
Ahmad Sharif | ae1714d | 2013-01-17 11:29:37 -0800 | [diff] [blame] | 69 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 70 | #endif // DEBUGD_SRC_PERF_TOOL_H_ |