Merge "libprocessgroup: Add CgroupGetControllerFromPath API function"

This commit is contained in:
Suren Baghdasaryan 2021-11-10 16:48:28 +00:00 committed by Gerrit Code Review
commit 4a8f4548cb
4 changed files with 36 additions and 0 deletions

View file

@ -34,6 +34,7 @@
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <cgroup_map.h>
#include <json/reader.h>
@ -41,6 +42,7 @@
#include <processgroup/processgroup.h>
using android::base::GetBoolProperty;
using android::base::StartsWith;
using android::base::StringPrintf;
using android::base::unique_fd;
using android::base::WriteStringToFile;
@ -204,6 +206,24 @@ CgroupController CgroupMap::FindController(const std::string& name) const {
return CgroupController(nullptr);
}
CgroupController CgroupMap::FindControllerByPath(const std::string& path) const {
if (!loaded_) {
LOG(ERROR) << "CgroupMap::FindControllerByPath called for [" << getpid()
<< "] failed, RC file was not initialized properly";
return CgroupController(nullptr);
}
auto controller_count = ACgroupFile_getControllerCount();
for (uint32_t i = 0; i < controller_count; ++i) {
const ACgroupController* controller = ACgroupFile_getController(i);
if (StartsWith(path, ACgroupController_getPath(controller))) {
return CgroupController(controller);
}
}
return CgroupController(nullptr);
}
int CgroupMap::ActivateControllers(const std::string& path) const {
if (__builtin_available(android 30, *)) {
auto controller_count = ACgroupFile_getControllerCount();

View file

@ -62,6 +62,7 @@ class CgroupMap {
static CgroupMap& GetInstance();
CgroupController FindController(const std::string& name) const;
CgroupController FindControllerByPath(const std::string& path) const;
int ActivateControllers(const std::string& path) const;
private:

View file

@ -26,6 +26,7 @@ __BEGIN_DECLS
static constexpr const char* CGROUPV2_CONTROLLER_NAME = "cgroup2";
bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path);
bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name);
bool CgroupGetAttributePath(const std::string& attr_name, std::string* path);
bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path);

View file

@ -69,6 +69,20 @@ bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path)
return true;
}
bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name) {
auto controller = CgroupMap::GetInstance().FindControllerByPath(path);
if (!controller.HasValue()) {
return false;
}
if (cgroup_name) {
*cgroup_name = controller.name();
}
return true;
}
bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {
const TaskProfiles& tp = TaskProfiles::GetInstance();
const ProfileAttribute* attr = tp.GetAttribute(attr_name);