blob: 5ad59bddf0a5d92a08e41fe978b63bffa54fd38f [file] [log] [blame]
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -08001/*
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 Baghdasaryan82b72a52018-12-21 11:41:50 -080019#include <sys/types.h>
20
T.J. Mercierc76b6ad2024-10-08 23:41:31 +000021#include <cstdint>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080022#include <string>
23
T.J. Mercierc76b6ad2024-10-08 23:41:31 +000024#include <processgroup/cgroup_controller.h>
25#include <processgroup/util.h>
Yifan Hong53e0deb2019-03-22 17:01:08 -070026
T.J. Mercierc76b6ad2024-10-08 23:41:31 +000027// Convenient wrapper of a CgroupController pointer.
T.J. Mercierfcb86662024-08-01 20:52:30 +000028class CgroupControllerWrapper {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080029 public:
Yifan Hong53e0deb2019-03-22 17:01:08 -070030 // Does not own controller
T.J. Mercierc76b6ad2024-10-08 23:41:31 +000031 explicit CgroupControllerWrapper(const CgroupController* controller)
T.J. Mercierdf671072024-05-30 00:18:30 +000032 : controller_(controller) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080033
Yifan Hong53e0deb2019-03-22 17:01:08 -070034 uint32_t version() const;
35 const char* name() const;
36 const char* path() const;
37
38 bool HasValue() const;
Suren Baghdasaryan25eb1bf2019-06-26 11:08:50 -070039 bool IsUsable();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080040
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. Mercier1c007992024-01-25 16:29:54 +000043 bool GetTaskGroup(pid_t tid, std::string* group) const;
44
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080045 private:
Suren Baghdasaryan25eb1bf2019-06-26 11:08:50 -070046 enum ControllerState {
47 UNKNOWN = 0,
48 USABLE = 1,
49 MISSING = 2,
50 };
51
T.J. Mercierc76b6ad2024-10-08 23:41:31 +000052 const CgroupController* controller_ = nullptr; // CgroupMap owns the object behind this pointer
T.J. Mercierdf671072024-05-30 00:18:30 +000053 ControllerState state_ = ControllerState::UNKNOWN;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080054};
55
56class CgroupMap {
57 public:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080058 static CgroupMap& GetInstance();
T.J. Mercierfcb86662024-08-01 20:52:30 +000059 CgroupControllerWrapper FindController(const std::string& name) const;
60 CgroupControllerWrapper FindControllerByPath(const std::string& path) const;
Priyanka Advani (xWF)0fa49252024-10-08 18:54:37 +000061 int ActivateControllers(const std::string& path) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080062
63 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070064 bool loaded_ = false;
T.J. Mercierc76b6ad2024-10-08 23:41:31 +000065 CgroupDescriptorMap descriptors_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080066 CgroupMap();
T.J. Mercierc76b6ad2024-10-08 23:41:31 +000067 bool LoadDescriptors();
Wei Wangd71d3012019-03-07 11:59:12 -080068 void Print() const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080069};