blob: 364279414336e598e1511fb6e09796eb18929c15 [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
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080021#include <string>
22
Yifan Hong53e0deb2019-03-22 17:01:08 -070023#include <android/cgrouprc.h>
24
25// Convenient wrapper of an ACgroupController pointer.
T.J. Mercierfcb86662024-08-01 20:52:30 +000026class CgroupControllerWrapper {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080027 public:
Yifan Hong53e0deb2019-03-22 17:01:08 -070028 // Does not own controller
T.J. Mercierfcb86662024-08-01 20:52:30 +000029 explicit CgroupControllerWrapper(const ACgroupController* controller)
T.J. Mercierdf671072024-05-30 00:18:30 +000030 : controller_(controller) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080031
Yifan Hong53e0deb2019-03-22 17:01:08 -070032 uint32_t version() const;
33 const char* name() const;
34 const char* path() const;
35
36 bool HasValue() const;
Suren Baghdasaryan25eb1bf2019-06-26 11:08:50 -070037 bool IsUsable();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080038
39 std::string GetTasksFilePath(const std::string& path) const;
40 std::string GetProcsFilePath(const std::string& path, uid_t uid, pid_t pid) const;
T.J. Mercier1c007992024-01-25 16:29:54 +000041 bool GetTaskGroup(pid_t tid, std::string* group) const;
42
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080043 private:
Suren Baghdasaryan25eb1bf2019-06-26 11:08:50 -070044 enum ControllerState {
45 UNKNOWN = 0,
46 USABLE = 1,
47 MISSING = 2,
48 };
49
Yifan Hong53e0deb2019-03-22 17:01:08 -070050 const ACgroupController* controller_ = nullptr;
T.J. Mercierdf671072024-05-30 00:18:30 +000051 ControllerState state_ = ControllerState::UNKNOWN;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080052};
53
54class CgroupMap {
55 public:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080056 static CgroupMap& GetInstance();
T.J. Mercierfcb86662024-08-01 20:52:30 +000057 CgroupControllerWrapper FindController(const std::string& name) const;
58 CgroupControllerWrapper FindControllerByPath(const std::string& path) const;
Suren Baghdasaryan25ad3f92021-07-30 13:42:39 -070059 int ActivateControllers(const std::string& path) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080060
61 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070062 bool loaded_ = false;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080063 CgroupMap();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080064 bool LoadRcFile();
Wei Wangd71d3012019-03-07 11:59:12 -080065 void Print() const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080066};