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

Commit fcb8666e authored by T.J. Mercier's avatar T.J. Mercier
Browse files

libprocessgroup: Rename CgroupController -> CgroupControllerWrapper

So that the name is not overloaded with libcgrouprc_format's
CgroupController, which can be confusing.

Bug: 349105928
Test: m
Change-Id: I39df9814c500de68fd20139e661363ba51ea3543
parent 9da55b8c
Loading
Loading
Loading
Loading
+17 −17
Original line number Diff line number Diff line
@@ -38,26 +38,26 @@ static constexpr const char* CGROUP_PROCS_FILE = "/cgroup.procs";
static constexpr const char* CGROUP_TASKS_FILE = "/tasks";
static constexpr const char* CGROUP_TASKS_FILE_V2 = "/cgroup.threads";

uint32_t CgroupController::version() const {
uint32_t CgroupControllerWrapper::version() const {
    CHECK(HasValue());
    return ACgroupController_getVersion(controller_);
}

const char* CgroupController::name() const {
const char* CgroupControllerWrapper::name() const {
    CHECK(HasValue());
    return ACgroupController_getName(controller_);
}

const char* CgroupController::path() const {
const char* CgroupControllerWrapper::path() const {
    CHECK(HasValue());
    return ACgroupController_getPath(controller_);
}

bool CgroupController::HasValue() const {
bool CgroupControllerWrapper::HasValue() const {
    return controller_ != nullptr;
}

bool CgroupController::IsUsable() {
bool CgroupControllerWrapper::IsUsable() {
    if (!HasValue()) return false;

    if (state_ == UNKNOWN) {
@@ -72,7 +72,7 @@ bool CgroupController::IsUsable() {
    return state_ == USABLE;
}

std::string CgroupController::GetTasksFilePath(const std::string& rel_path) const {
std::string CgroupControllerWrapper::GetTasksFilePath(const std::string& rel_path) const {
    std::string tasks_path = path();

    if (!rel_path.empty()) {
@@ -81,7 +81,7 @@ std::string CgroupController::GetTasksFilePath(const std::string& rel_path) cons
    return (version() == 1) ? tasks_path + CGROUP_TASKS_FILE : tasks_path + CGROUP_TASKS_FILE_V2;
}

std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_t uid,
std::string CgroupControllerWrapper::GetProcsFilePath(const std::string& rel_path, uid_t uid,
                                                      pid_t pid) const {
    std::string proc_path(path());
    proc_path.append("/").append(rel_path);
@@ -91,7 +91,7 @@ std::string CgroupController::GetProcsFilePath(const std::string& rel_path, uid_
    return proc_path.append(CGROUP_PROCS_FILE);
}

bool CgroupController::GetTaskGroup(pid_t tid, std::string* group) const {
bool CgroupControllerWrapper::GetTaskGroup(pid_t tid, std::string* group) const {
    std::string file_name = StringPrintf("/proc/%d/cgroup", tid);
    std::string content;
    if (!android::base::ReadFileToString(file_name, &content)) {
@@ -175,40 +175,40 @@ void CgroupMap::Print() const {
    }
}

CgroupController CgroupMap::FindController(const std::string& name) const {
CgroupControllerWrapper CgroupMap::FindController(const std::string& name) const {
    if (!loaded_) {
        LOG(ERROR) << "CgroupMap::FindController called for [" << getpid()
                   << "] failed, RC file was not initialized properly";
        return CgroupController(nullptr);
        return CgroupControllerWrapper(nullptr);
    }

    auto controller_count = ACgroupFile_getControllerCount();
    for (uint32_t i = 0; i < controller_count; ++i) {
        const ACgroupController* controller = ACgroupFile_getController(i);
        if (name == ACgroupController_getName(controller)) {
            return CgroupController(controller);
            return CgroupControllerWrapper(controller);
        }
    }

    return CgroupController(nullptr);
    return CgroupControllerWrapper(nullptr);
}

CgroupController CgroupMap::FindControllerByPath(const std::string& path) const {
CgroupControllerWrapper 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);
        return CgroupControllerWrapper(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 CgroupControllerWrapper(controller);
        }
    }

    return CgroupController(nullptr);
    return CgroupControllerWrapper(nullptr);
}

int CgroupMap::ActivateControllers(const std::string& path) const {
+4 −4
Original line number Diff line number Diff line
@@ -23,10 +23,10 @@
#include <android/cgrouprc.h>

// Convenient wrapper of an ACgroupController pointer.
class CgroupController {
class CgroupControllerWrapper {
  public:
    // Does not own controller
    explicit CgroupController(const ACgroupController* controller)
    explicit CgroupControllerWrapper(const ACgroupController* controller)
        : controller_(controller) {}

    uint32_t version() const;
@@ -57,8 +57,8 @@ class CgroupMap {
    static bool SetupCgroups();

    static CgroupMap& GetInstance();
    CgroupController FindController(const std::string& name) const;
    CgroupController FindControllerByPath(const std::string& path) const;
    CgroupControllerWrapper FindController(const std::string& name) const;
    CgroupControllerWrapper FindControllerByPath(const std::string& path) const;
    int ActivateControllers(const std::string& path) const;

  private:
+1 −1
Original line number Diff line number Diff line
@@ -94,7 +94,7 @@ static bool CgroupKillAvailable() {
}

static bool CgroupGetMemcgAppsPath(std::string* path) {
    CgroupController controller = CgroupMap::GetInstance().FindController("memory");
    CgroupControllerWrapper controller = CgroupMap::GetInstance().FindController("memory");

    if (!controller.HasValue()) {
        return false;
+3 −3
Original line number Diff line number Diff line
@@ -123,8 +123,8 @@ const std::string& ProfileAttribute::file_name() const {
    return file_name_;
}

void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name,
                             const std::string& file_v2_name) {
void ProfileAttribute::Reset(const CgroupControllerWrapper& controller,
                             const std::string& file_name, const std::string& file_v2_name) {
    controller_ = controller;
    file_name_ = file_name;
    file_v2_name_ = file_v2_name;
@@ -333,7 +333,7 @@ bool SetAttributeAction::IsValidForTask(pid_t tid) const {
    return optional_;
}

SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p)
SetCgroupAction::SetCgroupAction(const CgroupControllerWrapper& c, const std::string& p)
    : controller_(c), path_(p) {
    FdCacheHelper::Init(controller_.GetTasksFilePath(path_), fd_[ProfileAction::RCT_TASK]);
    // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them
+9 −9
Original line number Diff line number Diff line
@@ -32,9 +32,9 @@
class IProfileAttribute {
  public:
    virtual ~IProfileAttribute() = 0;
    virtual void Reset(const CgroupController& controller, const std::string& file_name,
    virtual void Reset(const CgroupControllerWrapper& controller, const std::string& file_name,
                       const std::string& file_v2_name) = 0;
    virtual const CgroupController* controller() const = 0;
    virtual const CgroupControllerWrapper* controller() const = 0;
    virtual const std::string& file_name() const = 0;
    virtual bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const = 0;
    virtual bool GetPathForTask(pid_t tid, std::string* path) const = 0;
@@ -46,14 +46,14 @@ class ProfileAttribute : public IProfileAttribute {
    // Cgroup attributes may have different names in the v1 and v2 hierarchies. If `file_v2_name` is
    // not empty, `file_name` is the name for the v1 hierarchy and `file_v2_name` is the name for
    // the v2 hierarchy. If `file_v2_name` is empty, `file_name` is used for both hierarchies.
    ProfileAttribute(const CgroupController& controller, const std::string& file_name,
    ProfileAttribute(const CgroupControllerWrapper& controller, const std::string& file_name,
                     const std::string& file_v2_name)
        : controller_(controller), file_name_(file_name), file_v2_name_(file_v2_name) {}
    ~ProfileAttribute() = default;

    const CgroupController* controller() const override { return &controller_; }
    const CgroupControllerWrapper* controller() const override { return &controller_; }
    const std::string& file_name() const override;
    void Reset(const CgroupController& controller, const std::string& file_name,
    void Reset(const CgroupControllerWrapper& controller, const std::string& file_name,
               const std::string& file_v2_name) override;

    bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const override;
@@ -61,7 +61,7 @@ class ProfileAttribute : public IProfileAttribute {
    bool GetPathForUID(uid_t uid, std::string* path) const override;

  private:
    CgroupController controller_;
    CgroupControllerWrapper controller_;
    std::string file_name_;
    std::string file_v2_name_;
};
@@ -142,7 +142,7 @@ class SetAttributeAction : public ProfileAction {
// Set cgroup profile element
class SetCgroupAction : public ProfileAction {
  public:
    SetCgroupAction(const CgroupController& c, const std::string& p);
    SetCgroupAction(const CgroupControllerWrapper& c, const std::string& p);

    const char* Name() const override { return "SetCgroup"; }
    bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
@@ -152,10 +152,10 @@ class SetCgroupAction : public ProfileAction {
    bool IsValidForProcess(uid_t uid, pid_t pid) const override;
    bool IsValidForTask(pid_t tid) const override;

    const CgroupController* controller() const { return &controller_; }
    const CgroupControllerWrapper* controller() const { return &controller_; }

  private:
    CgroupController controller_;
    CgroupControllerWrapper controller_;
    std::string path_;
    android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
    mutable std::mutex fd_mutex_;
Loading