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 | |
| 28 | class Workload { |
| 29 | private: |
| 30 | enum WorkState { |
| 31 | NotYetCreateNewProcess, |
| 32 | NotYetStartNewProcess, |
| 33 | Started, |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 34 | Finished, |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | public: |
| 38 | static std::unique_ptr<Workload> CreateWorkload(const std::vector<std::string>& args); |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 39 | static std::unique_ptr<Workload> CreateWorkload(const std::function<void ()>& function); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 40 | 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] | 41 | |
Yabin Cui | 7d4904d | 2015-06-09 13:38:42 -0700 | [diff] [blame] | 42 | ~Workload(); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 43 | |
| 44 | bool Start(); |
Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 45 | bool IsStarted() { |
| 46 | return work_state_ == Started; |
| 47 | } |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 48 | pid_t GetPid() { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 49 | return work_pid_; |
| 50 | } |
| 51 | |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 52 | bool WaitChildProcess(int* exit_code); |
| 53 | |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 54 | // Set the function used to kill the workload process in ~Workload(). |
| 55 | void SetKillFunction(const std::function<void (pid_t)>& kill_function) { |
| 56 | kill_function_ = kill_function; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 59 | private: |
| 60 | explicit Workload(const std::vector<std::string>& args, const std::function<void ()>& function); |
| 61 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 62 | bool CreateNewProcess(); |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 63 | void ChildProcessFn(int start_signal_fd, int exec_child_fd); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 64 | bool WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 65 | |
| 66 | WorkState work_state_; |
Yabin Cui | 5be914d | 2016-11-29 15:21:13 -0800 | [diff] [blame] | 67 | // The child process either executes child_proc_args or run child_proc_function. |
| 68 | std::vector<std::string> child_proc_args_; |
| 69 | std::function<void ()> child_proc_function_; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 70 | pid_t work_pid_; |
| 71 | int start_signal_fd_; // The parent process writes 1 to start workload in the child process. |
| 72 | int exec_child_fd_; // The child process writes 1 to notify that execvp() failed. |
Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 73 | std::function<void (pid_t)> kill_function_; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 74 | |
| 75 | DISALLOW_COPY_AND_ASSIGN(Workload); |
| 76 | }; |
| 77 | |
| 78 | #endif // SIMPLE_PERF_WORKLOAD_H_ |