1 #ifndef ANDROID_DVR_PERFORMANCED_TASK_H_
2 #define ANDROID_DVR_PERFORMANCED_TASK_H_
3 
4 #include <sys/types.h>
5 
6 #include <array>
7 #include <cstdio>
8 #include <memory>
9 #include <string>
10 #include <unordered_map>
11 #include <vector>
12 
13 #include <android-base/unique_fd.h>
14 
15 #include "unique_file.h"
16 
17 namespace android {
18 namespace dvr {
19 
20 // Task provides access to task-related information from the procfs
21 // pseudo-filesystem.
22 class Task {
23  public:
24   explicit Task(pid_t task_id);
25 
IsValid()26   bool IsValid() const { return task_fd_.get() >= 0; }
27   explicit operator bool() const { return IsValid(); }
28 
task_id()29   pid_t task_id() const { return task_id_; }
name()30   std::string name() const { return name_; }
thread_group_id()31   pid_t thread_group_id() const { return thread_group_id_; }
parent_process_id()32   pid_t parent_process_id() const { return parent_process_id_; }
thread_count()33   size_t thread_count() const { return thread_count_; }
cpus_allowed_mask()34   uint32_t cpus_allowed_mask() const { return cpus_allowed_mask_; }
cpus_allowed_list()35   const std::string& cpus_allowed_list() const { return cpus_allowed_list_; }
user_id()36   const std::array<int, 4>& user_id() const { return user_id_; }
group_id()37   const std::array<int, 4>& group_id() const { return group_id_; }
38 
39   // Indices into user and group id arrays.
40   enum {
41     kUidReal = 0,
42     kUidEffective,
43     kUidSavedSet,
44     kUidFilesystem,
45   };
46 
47   std::string GetCpuSetPath() const;
48 
49  private:
50   pid_t task_id_;
51   base::unique_fd task_fd_;
52 
53   // Fields read from /proc/<task_id_>/status.
54   std::string name_;
55   pid_t thread_group_id_;
56   pid_t parent_process_id_;
57   std::array<int, 4> user_id_;
58   std::array<int, 4> group_id_;
59   size_t thread_count_;
60   uint32_t cpus_allowed_mask_;
61   std::string cpus_allowed_list_;
62 
63   // Opens the file /proc/<task_id_>/|name| and returns the open file
64   // descriptor.
65   base::unique_fd OpenTaskFile(const std::string& name) const;
66 
67   // Similar to OpenTaskFile() but returns a file pointer.
68   UniqueFile OpenTaskFilePointer(const std::string& name) const;
69 
70   // Reads the field named |field| from /proc/<task_id_>/status.
71   std::string GetStatusField(const std::string& field) const;
72 
73   // Reads a subset of the fields in /proc/<task_id_>/status.
74   void ReadStatusFields();
75 
76   Task(const Task&) = delete;
77   void operator=(const Task&) = delete;
78 };
79 
80 }  // namespace dvr
81 }  // namespace android
82 
83 #endif  // ANDROID_DVR_PERFORMANCED_TASK_H_
84