• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef ANDROID_DVR_PERFORMANCED_PERFORMANCE_SERVICE_H_
2 #define ANDROID_DVR_PERFORMANCED_PERFORMANCE_SERVICE_H_
3 
4 #include <functional>
5 #include <string>
6 #include <unordered_map>
7 
8 #include <pdx/service.h>
9 
10 #include "cpu_set.h"
11 #include "task.h"
12 
13 namespace android {
14 namespace dvr {
15 
16 // PerformanceService manages compute partitions usings cpusets. Different
17 // cpusets are assigned specific purposes and performance characteristics;
18 // clients may request for threads to be moved into these cpusets to help
19 // achieve system performance goals.
20 class PerformanceService : public pdx::ServiceBase<PerformanceService> {
21  public:
22   pdx::Status<void> HandleMessage(pdx::Message& message) override;
23   bool IsInitialized() const override;
24 
25   std::string DumpState(size_t max_length) override;
26 
27  private:
28   friend BASE;
29 
30   PerformanceService();
31 
32   pdx::Status<void> OnSetSchedulerPolicy(pdx::Message& message, pid_t task_id,
33                                          const std::string& scheduler_class);
34 
35   pdx::Status<void> OnSetCpuPartition(pdx::Message& message, pid_t task_id,
36                                       const std::string& partition);
37   pdx::Status<void> OnSetSchedulerClass(pdx::Message& message, pid_t task_id,
38                                         const std::string& scheduler_class);
39   pdx::Status<std::string> OnGetCpuPartition(pdx::Message& message,
40                                              pid_t task_id);
41 
42   CpuSetManager cpuset_;
43 
44   int sched_fifo_min_priority_;
45   int sched_fifo_max_priority_;
46 
47   struct SchedulerPolicyConfig {
48     unsigned long timer_slack;
49     int scheduler_policy;
50     int priority;
51     std::function<bool(const pdx::Message& message, const Task& task)>
52         permission_check;
53     std::string cpuset;
54 
55     // Check the permisison of the given task to use this scheduler class. If a
56     // permission check function is not set then operations are only allowed on
57     // tasks in the sender's process.
IsAllowedSchedulerPolicyConfig58     bool IsAllowed(const pdx::Message& sender, const Task& task) const {
59       if (permission_check)
60         return permission_check(sender, task);
61       else if (!task || task.thread_group_id() != sender.GetProcessId())
62         return false;
63       else
64         return true;
65     }
66   };
67 
68   std::unordered_map<std::string, SchedulerPolicyConfig> scheduler_policies_;
69 
70   std::function<bool(const pdx::Message& message, const Task& task)>
71       partition_permission_check_;
72 
73   PerformanceService(const PerformanceService&) = delete;
74   void operator=(const PerformanceService&) = delete;
75 };
76 
77 }  // namespace dvr
78 }  // namespace android
79 
80 #endif  // ANDROID_DVR_PERFORMANCED_PERFORMANCE_SERVICE_H_
81