Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SIMPLE_PERF_WORKLOAD_H_ |
| 18 | #define SIMPLE_PERF_WORKLOAD_H_ |
| 19 | |
| 20 | #include <sys/types.h> |
| 21 | #include <chrono> |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 22 | #include <functional> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 26 | #include <android-base/macros.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 27 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 28 | namespace simpleperf { |
| 29 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 30 | class Workload { |
| 31 | private: |
| 32 | enum WorkState { |
| 33 | NotYetCreateNewProcess, |
| 34 | NotYetStartNewProcess, |
| 35 | Started, |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 36 | Finished, |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | public: |
| 40 | static std::unique_ptr<Workload> CreateWorkload(const std::vector<std::string>& args); |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 41 | static std::unique_ptr<Workload> CreateWorkload(const std::function<void()>& function); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 42 | static bool RunCmd(const std::vector<std::string>& args, bool report_error = true); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 43 | |
Yabin Cui | 7d4904d | 2015-06-09 13:38:42 -0700 | [diff] [blame] | 44 | ~Workload(); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 45 | |
Yabin | 6b771a2 | 2022-08-18 17:01:54 -0700 | [diff] [blame] | 46 | bool SetCpuAffinity(int cpu); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 47 | bool Start(); |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 48 | bool IsStarted() { return work_state_ == Started; } |
| 49 | pid_t GetPid() { return work_pid_; } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 50 | |
Yabin Cui | 248ef5e | 2021-12-16 14:09:39 -0800 | [diff] [blame] | 51 | bool WaitChildProcess(bool wait_forever, int* exit_code); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 52 | |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 53 | // Set the function used to kill the workload process in ~Workload(). |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 54 | void SetKillFunction(const std::function<void(pid_t)>& kill_function) { |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 55 | kill_function_ = kill_function; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 58 | private: |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 59 | explicit Workload(const std::vector<std::string>& args, const std::function<void()>& function); |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 60 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 61 | bool CreateNewProcess(); |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 62 | void ChildProcessFn(int start_signal_fd, int exec_child_fd); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 63 | bool WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 64 | |
| 65 | WorkState work_state_; |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 66 | // The child process either executes child_proc_args or run child_proc_function. |
| 67 | std::vector<std::string> child_proc_args_; |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 68 | std::function<void()> child_proc_function_; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 69 | pid_t work_pid_; |
| 70 | int start_signal_fd_; // The parent process writes 1 to start workload in the child process. |
| 71 | int exec_child_fd_; // The child process writes 1 to notify that execvp() failed. |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 72 | std::function<void(pid_t)> kill_function_; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 73 | |
| 74 | DISALLOW_COPY_AND_ASSIGN(Workload); |
| 75 | }; |
| 76 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 77 | } // namespace simpleperf |
| 78 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 79 | #endif // SIMPLE_PERF_WORKLOAD_H_ |