blob: 86b2892438175c25930767a84ce1ed792dbfb451 [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -07001/*
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 Cui5be914d2016-11-29 15:21:13 -080022#include <functional>
Yabin Cui323e9452015-04-20 18:07:17 -070023#include <string>
24#include <vector>
25
Elliott Hughes66dd09e2015-12-04 14:00:57 -080026#include <android-base/macros.h>
Yabin Cui323e9452015-04-20 18:07:17 -070027
Yabin Cuifaa7b922021-01-11 17:35:57 -080028namespace simpleperf {
29
Yabin Cui323e9452015-04-20 18:07:17 -070030class Workload {
31 private:
32 enum WorkState {
33 NotYetCreateNewProcess,
34 NotYetStartNewProcess,
35 Started,
Yabin Cuia80f8f72017-07-12 15:50:20 -070036 Finished,
Yabin Cui323e9452015-04-20 18:07:17 -070037 };
38
39 public:
40 static std::unique_ptr<Workload> CreateWorkload(const std::vector<std::string>& args);
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020041 static std::unique_ptr<Workload> CreateWorkload(const std::function<void()>& function);
Yabin Cuia80f8f72017-07-12 15:50:20 -070042 static bool RunCmd(const std::vector<std::string>& args, bool report_error = true);
Yabin Cui323e9452015-04-20 18:07:17 -070043
Yabin Cui7d4904d2015-06-09 13:38:42 -070044 ~Workload();
Yabin Cui323e9452015-04-20 18:07:17 -070045
Yabin6b771a22022-08-18 17:01:54 -070046 bool SetCpuAffinity(int cpu);
Yabin Cui323e9452015-04-20 18:07:17 -070047 bool Start();
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020048 bool IsStarted() { return work_state_ == Started; }
49 pid_t GetPid() { return work_pid_; }
Yabin Cui323e9452015-04-20 18:07:17 -070050
Yabin Cui248ef5e2021-12-16 14:09:39 -080051 bool WaitChildProcess(bool wait_forever, int* exit_code);
Yabin Cuia80f8f72017-07-12 15:50:20 -070052
Yabin Cui616b3a02017-07-14 15:59:56 -070053 // Set the function used to kill the workload process in ~Workload().
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020054 void SetKillFunction(const std::function<void(pid_t)>& kill_function) {
Yabin Cui616b3a02017-07-14 15:59:56 -070055 kill_function_ = kill_function;
Yabin Cui323e9452015-04-20 18:07:17 -070056 }
57
Yabin Cui616b3a02017-07-14 15:59:56 -070058 private:
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020059 explicit Workload(const std::vector<std::string>& args, const std::function<void()>& function);
Yabin Cui616b3a02017-07-14 15:59:56 -070060
Yabin Cui323e9452015-04-20 18:07:17 -070061 bool CreateNewProcess();
Yabin Cui5be914d2016-11-29 15:21:13 -080062 void ChildProcessFn(int start_signal_fd, int exec_child_fd);
Yabin Cuia80f8f72017-07-12 15:50:20 -070063 bool WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code);
Yabin Cui323e9452015-04-20 18:07:17 -070064
65 WorkState work_state_;
Yabin Cui5be914d2016-11-29 15:21:13 -080066 // The child process either executes child_proc_args or run child_proc_function.
67 std::vector<std::string> child_proc_args_;
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020068 std::function<void()> child_proc_function_;
Yabin Cui323e9452015-04-20 18:07:17 -070069 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 Weksteen4848ee02020-10-23 16:06:59 +020072 std::function<void(pid_t)> kill_function_;
Yabin Cui323e9452015-04-20 18:07:17 -070073
74 DISALLOW_COPY_AND_ASSIGN(Workload);
75};
76
Yabin Cuifaa7b922021-01-11 17:35:57 -080077} // namespace simpleperf
78
Yabin Cui323e9452015-04-20 18:07:17 -070079#endif // SIMPLE_PERF_WORKLOAD_H_