task_profiles.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2019 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. #pragma once
  17. #include <sys/cdefs.h>
  18. #include <sys/types.h>
  19. #include <map>
  20. #include <mutex>
  21. #include <string>
  22. #include <vector>
  23. #include <android-base/unique_fd.h>
  24. #include <cgroup_map.h>
  25. class ProfileAttribute {
  26. public:
  27. ProfileAttribute(const CgroupController& controller, const std::string& file_name)
  28. : controller_(controller), file_name_(file_name) {}
  29. const CgroupController* controller() const { return &controller_; }
  30. const std::string& file_name() const { return file_name_; }
  31. bool GetPathForTask(int tid, std::string* path) const;
  32. private:
  33. CgroupController controller_;
  34. std::string file_name_;
  35. };
  36. // Abstract profile element
  37. class ProfileAction {
  38. public:
  39. virtual ~ProfileAction() {}
  40. // Default implementations will fail
  41. virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
  42. virtual bool ExecuteForTask(int) const { return false; };
  43. virtual void EnableResourceCaching() {}
  44. };
  45. // Profile actions
  46. class SetClampsAction : public ProfileAction {
  47. public:
  48. SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
  49. virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
  50. virtual bool ExecuteForTask(int tid) const;
  51. protected:
  52. int boost_;
  53. int clamp_;
  54. };
  55. // To avoid issues in sdk_mac build
  56. #if defined(__ANDROID__)
  57. class SetTimerSlackAction : public ProfileAction {
  58. public:
  59. SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
  60. virtual bool ExecuteForTask(int tid) const;
  61. private:
  62. unsigned long slack_;
  63. static bool IsTimerSlackSupported(int tid);
  64. };
  65. #else
  66. class SetTimerSlackAction : public ProfileAction {
  67. public:
  68. SetTimerSlackAction(unsigned long) noexcept {}
  69. virtual bool ExecuteForTask(int) const { return true; }
  70. };
  71. #endif
  72. // Set attribute profile element
  73. class SetAttributeAction : public ProfileAction {
  74. public:
  75. SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
  76. : attribute_(attribute), value_(value) {}
  77. virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
  78. virtual bool ExecuteForTask(int tid) const;
  79. private:
  80. const ProfileAttribute* attribute_;
  81. std::string value_;
  82. };
  83. // Set cgroup profile element
  84. class SetCgroupAction : public ProfileAction {
  85. public:
  86. SetCgroupAction(const CgroupController& c, const std::string& p);
  87. virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
  88. virtual bool ExecuteForTask(int tid) const;
  89. virtual void EnableResourceCaching();
  90. const CgroupController* controller() const { return &controller_; }
  91. std::string path() const { return path_; }
  92. private:
  93. enum FdState {
  94. FDS_INACCESSIBLE = -1,
  95. FDS_APP_DEPENDENT = -2,
  96. FDS_NOT_CACHED = -3,
  97. };
  98. CgroupController controller_;
  99. std::string path_;
  100. android::base::unique_fd fd_;
  101. mutable std::mutex fd_mutex_;
  102. static bool IsAppDependentPath(const std::string& path);
  103. static bool AddTidToCgroup(int tid, int fd);
  104. bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; }
  105. };
  106. class TaskProfile {
  107. public:
  108. TaskProfile() : res_cached_(false) {}
  109. void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
  110. bool ExecuteForProcess(uid_t uid, pid_t pid) const;
  111. bool ExecuteForTask(int tid) const;
  112. void EnableResourceCaching();
  113. private:
  114. bool res_cached_;
  115. std::vector<std::unique_ptr<ProfileAction>> elements_;
  116. };
  117. class TaskProfiles {
  118. public:
  119. // Should be used by all users
  120. static TaskProfiles& GetInstance();
  121. TaskProfile* GetProfile(const std::string& name) const;
  122. const ProfileAttribute* GetAttribute(const std::string& name) const;
  123. private:
  124. std::map<std::string, std::unique_ptr<TaskProfile>> profiles_;
  125. std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
  126. TaskProfiles();
  127. bool Load(const CgroupMap& cg_map, const std::string& file_name);
  128. };