blob: 4ca93a42be72045976448c8d2a3e249e2d41ba99 [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
28class Workload {
29 private:
30 enum WorkState {
31 NotYetCreateNewProcess,
32 NotYetStartNewProcess,
33 Started,
Yabin Cuia80f8f72017-07-12 15:50:20 -070034 Finished,
Yabin Cui323e9452015-04-20 18:07:17 -070035 };
36
37 public:
38 static std::unique_ptr<Workload> CreateWorkload(const std::vector<std::string>& args);
Yabin Cui5be914d2016-11-29 15:21:13 -080039 static std::unique_ptr<Workload> CreateWorkload(const std::function<void ()>& function);
Yabin Cuia80f8f72017-07-12 15:50:20 -070040 static bool RunCmd(const std::vector<std::string>& args, bool report_error = true);
Yabin Cui323e9452015-04-20 18:07:17 -070041
Yabin Cui7d4904d2015-06-09 13:38:42 -070042 ~Workload();
Yabin Cui323e9452015-04-20 18:07:17 -070043
44 bool Start();
Yabin Cui26968e62017-01-30 11:34:24 -080045 bool IsStarted() {
46 return work_state_ == Started;
47 }
Yabin Cui9759e1b2015-04-28 15:54:13 -070048 pid_t GetPid() {
Yabin Cui323e9452015-04-20 18:07:17 -070049 return work_pid_;
50 }
51
Yabin Cuia80f8f72017-07-12 15:50:20 -070052 bool WaitChildProcess(int* exit_code);
53
Yabin Cui616b3a02017-07-14 15:59:56 -070054 // 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 Cui323e9452015-04-20 18:07:17 -070057 }
58
Yabin Cui616b3a02017-07-14 15:59:56 -070059 private:
60 explicit Workload(const std::vector<std::string>& args, const std::function<void ()>& function);
61
Yabin Cui323e9452015-04-20 18:07:17 -070062 bool CreateNewProcess();
Yabin Cui5be914d2016-11-29 15:21:13 -080063 void ChildProcessFn(int start_signal_fd, int exec_child_fd);
Yabin Cuia80f8f72017-07-12 15:50:20 -070064 bool WaitChildProcess(bool wait_forever, bool is_child_killed, int* exit_code);
Yabin Cui323e9452015-04-20 18:07:17 -070065
66 WorkState work_state_;
Yabin Cui5be914d2016-11-29 15:21:13 -080067 // 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 Cui323e9452015-04-20 18:07:17 -070070 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 Cui616b3a02017-07-14 15:59:56 -070073 std::function<void (pid_t)> kill_function_;
Yabin Cui323e9452015-04-20 18:07:17 -070074
75 DISALLOW_COPY_AND_ASSIGN(Workload);
76};
77
78#endif // SIMPLE_PERF_WORKLOAD_H_