Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4a8f4548 authored by Suren Baghdasaryan's avatar Suren Baghdasaryan Committed by Gerrit Code Review
Browse files

Merge "libprocessgroup: Add CgroupGetControllerFromPath API function"

parents 9ac28517 9e3ace52
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -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();
+1 −0
Original line number Diff line number Diff line
@@ -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:
+1 −0
Original line number Diff line number Diff line
@@ -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);

+14 −0
Original line number Diff line number Diff line
@@ -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);