OSDN Git Service

9d9d5952ffb76b03ea10345d1c12734c4e30e031
[android-x86/system-extras.git] / simpleperf / workload.h
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>
22 #include <functional>
23 #include <string>
24 #include <vector>
25
26 #include <android-base/macros.h>
27
28 class Workload {
29  private:
30   enum WorkState {
31     NotYetCreateNewProcess,
32     NotYetStartNewProcess,
33     Started,
34   };
35
36  public:
37   static std::unique_ptr<Workload> CreateWorkload(const std::vector<std::string>& args);
38   static std::unique_ptr<Workload> CreateWorkload(const std::function<void ()>& function);
39
40   ~Workload();
41
42   bool Start();
43   bool IsStarted() {
44     return work_state_ == Started;
45   }
46   pid_t GetPid() {
47     return work_pid_;
48   }
49
50  private:
51   explicit Workload(const std::vector<std::string>& args,
52                     const std::function<void ()>& function)
53       : work_state_(NotYetCreateNewProcess),
54         child_proc_args_(args),
55         child_proc_function_(function),
56         work_pid_(-1),
57         start_signal_fd_(-1),
58         exec_child_fd_(-1) {
59   }
60
61   bool CreateNewProcess();
62   void ChildProcessFn(int start_signal_fd, int exec_child_fd);
63   bool WaitChildProcess(bool wait_forever, bool is_child_killed);
64
65   WorkState work_state_;
66   // The child process either executes child_proc_args or run child_proc_function.
67   std::vector<std::string> child_proc_args_;
68   std::function<void ()> child_proc_function_;
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.
72
73   DISALLOW_COPY_AND_ASSIGN(Workload);
74 };
75
76 #endif  // SIMPLE_PERF_WORKLOAD_H_