Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 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 | |
| 17 | #pragma once |
| 18 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 21 | #include <cstdint> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 22 | #include <string> |
| 23 | |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 24 | #include <processgroup/cgroup_controller.h> |
| 25 | #include <processgroup/util.h> |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 26 | |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 27 | // Convenient wrapper of a CgroupController pointer. |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 28 | class CgroupControllerWrapper { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 29 | public: |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 30 | // Does not own controller |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 31 | explicit CgroupControllerWrapper(const CgroupController* controller) |
T.J. Mercier | df67107 | 2024-05-30 00:18:30 +0000 | [diff] [blame] | 32 | : controller_(controller) {} |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 33 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 34 | uint32_t version() const; |
| 35 | const char* name() const; |
| 36 | const char* path() const; |
| 37 | |
| 38 | bool HasValue() const; |
Suren Baghdasaryan | 25eb1bf | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 39 | bool IsUsable(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 40 | |
| 41 | std::string GetTasksFilePath(const std::string& path) const; |
| 42 | std::string GetProcsFilePath(const std::string& path, uid_t uid, pid_t pid) const; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 43 | bool GetTaskGroup(pid_t tid, std::string* group) const; |
| 44 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 45 | private: |
Suren Baghdasaryan | 25eb1bf | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 46 | enum ControllerState { |
| 47 | UNKNOWN = 0, |
| 48 | USABLE = 1, |
| 49 | MISSING = 2, |
| 50 | }; |
| 51 | |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 52 | const CgroupController* controller_ = nullptr; // CgroupMap owns the object behind this pointer |
T.J. Mercier | df67107 | 2024-05-30 00:18:30 +0000 | [diff] [blame] | 53 | ControllerState state_ = ControllerState::UNKNOWN; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | class CgroupMap { |
| 57 | public: |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 58 | static CgroupMap& GetInstance(); |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 59 | CgroupControllerWrapper FindController(const std::string& name) const; |
| 60 | CgroupControllerWrapper FindControllerByPath(const std::string& path) const; |
T.J. Mercier | 787ddbc | 2024-10-08 23:41:34 +0000 | [diff] [blame] | 61 | bool ActivateControllers(const std::string& path) const; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 62 | |
| 63 | private: |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 64 | bool loaded_ = false; |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 65 | CgroupDescriptorMap descriptors_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 66 | CgroupMap(); |
T.J. Mercier | c76b6ad | 2024-10-08 23:41:31 +0000 | [diff] [blame] | 67 | bool LoadDescriptors(); |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 68 | void Print() const; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 69 | }; |