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