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

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

libprocessgroup: Remove PRODUCT_MEMCG_V2_FORCE_ENABLED

Commit f8901767 ("Add build flag to force memcg to the v2 cgroup
hierarchy") added the ability to ignore cgroups.json for the memory
controller, and force it to v2 when the PRODUCT_MEMCG_V2_FORCE_ENABLED
build flag is true. However the memory controller is now v2 by default
since commit 5fa48a84 ("libprocessgroup: memcg default version 1 -> 2")
so this build flag is no longer needed.

Bug: 327480673
Bug: 377579705
Change-Id: I46f45a44c01b2244eed2232ac091ef20c42cac41
parent 661afc25
Loading
Loading
Loading
Loading
+0 −6
Original line number Original line Diff line number Diff line
@@ -7,7 +7,6 @@ soong_config_module_type {
    module_type: "cc_defaults",
    module_type: "cc_defaults",
    config_namespace: "ANDROID",
    config_namespace: "ANDROID",
    bool_variables: [
    bool_variables: [
        "memcg_v2_force_enabled",
        "cgroup_v2_sys_app_isolation",
        "cgroup_v2_sys_app_isolation",
    ],
    ],
    properties: [
    properties: [
@@ -19,11 +18,6 @@ libprocessgroup_flag_aware_cc_defaults {
    name: "libprocessgroup_build_flags_cc",
    name: "libprocessgroup_build_flags_cc",
    cpp_std: "gnu++23",
    cpp_std: "gnu++23",
    soong_config_variables: {
    soong_config_variables: {
        memcg_v2_force_enabled: {
            cflags: [
                "-DMEMCG_V2_FORCE_ENABLED=true",
            ],
        },
        cgroup_v2_sys_app_isolation: {
        cgroup_v2_sys_app_isolation: {
            cflags: [
            cflags: [
                "-DCGROUP_V2_SYS_APP_ISOLATION=true",
                "-DCGROUP_V2_SYS_APP_ISOLATION=true",
+0 −8
Original line number Original line Diff line number Diff line
@@ -16,20 +16,12 @@


#pragma once
#pragma once


#ifndef MEMCG_V2_FORCE_ENABLED
#define MEMCG_V2_FORCE_ENABLED false
#endif

#ifndef CGROUP_V2_SYS_APP_ISOLATION
#ifndef CGROUP_V2_SYS_APP_ISOLATION
#define CGROUP_V2_SYS_APP_ISOLATION false
#define CGROUP_V2_SYS_APP_ISOLATION false
#endif
#endif


namespace android::libprocessgroup_flags {
namespace android::libprocessgroup_flags {


inline consteval bool force_memcg_v2() {
    return MEMCG_V2_FORCE_ENABLED;
}

inline consteval bool cgroup_v2_sys_app_isolation() {
inline consteval bool cgroup_v2_sys_app_isolation() {
    return CGROUP_V2_SYS_APP_ISOLATION;
    return CGROUP_V2_SYS_APP_ISOLATION;
}
}
+0 −47
Original line number Original line Diff line number Diff line
@@ -27,9 +27,6 @@
#include <sys/types.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>


#include <optional>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/logging.h>
#include <processgroup/cgroup_descriptor.h>
#include <processgroup/cgroup_descriptor.h>
#include <processgroup/processgroup.h>
#include <processgroup/processgroup.h>
@@ -260,39 +257,6 @@ void CgroupDescriptor::set_mounted(bool mounted) {
    controller_.set_flags(flags);
    controller_.set_flags(flags);
}
}


static std::optional<bool> MGLRUDisabled() {
    const std::string file_name = "/sys/kernel/mm/lru_gen/enabled";
    std::string content;
    if (!android::base::ReadFileToString(file_name, &content)) {
        PLOG(ERROR) << "Failed to read MGLRU state from " << file_name;
        return {};
    }

    return content == "0x0000";
}

static std::optional<bool> MEMCGDisabled(const CgroupDescriptorMap& descriptors) {
    std::string cgroup_v2_root = CGROUP_V2_ROOT_DEFAULT;
    const auto it = descriptors.find(CGROUPV2_HIERARCHY_NAME);
    if (it == descriptors.end()) {
        LOG(WARNING) << "No Cgroups2 path found in cgroups.json. Vendor has modified Android, and "
                     << "kernel memory use will be higher than intended.";
    } else if (it->second.controller()->path() != cgroup_v2_root) {
        cgroup_v2_root = it->second.controller()->path();
    }

    const std::string file_name = cgroup_v2_root + "/cgroup.controllers";
    std::string content;
    if (!android::base::ReadFileToString(file_name, &content)) {
        PLOG(ERROR) << "Failed to read cgroup controllers from " << file_name;
        return {};
    }

    // If we've forced memcg to v2 and it's not available, then it could only have been disabled
    // on the kernel command line (GKI sets CONFIG_MEMCG).
    return content.find("memory") == std::string::npos;
}

static bool CreateV2SubHierarchy(const std::string& path, const CgroupDescriptorMap& descriptors) {
static bool CreateV2SubHierarchy(const std::string& path, const CgroupDescriptorMap& descriptors) {
    const auto cgv2_iter = descriptors.find(CGROUPV2_HIERARCHY_NAME);
    const auto cgv2_iter = descriptors.find(CGROUPV2_HIERARCHY_NAME);
    if (cgv2_iter == descriptors.end()) return false;
    if (cgv2_iter == descriptors.end()) return false;
@@ -335,17 +299,6 @@ bool CgroupSetup() {
        }
        }
    }
    }


    if (android::libprocessgroup_flags::force_memcg_v2()) {
        if (MGLRUDisabled().value_or(false)) {
            LOG(WARNING) << "Memcg forced to v2 hierarchy with MGLRU disabled! "
                         << "Global reclaim performance will suffer.";
        }
        if (MEMCGDisabled(descriptors).value_or(false)) {
            LOG(WARNING) << "Memcg forced to v2 hierarchy while memcg is disabled by kernel "
                         << "command line!";
        }
    }

    // System / app isolation.
    // System / app isolation.
    // This really belongs in early-init in init.rc, but we cannot use the flag there.
    // This really belongs in early-init in init.rc, but we cannot use the flag there.
    if (android::libprocessgroup_flags::cgroup_v2_sys_app_isolation()) {
    if (android::libprocessgroup_flags::cgroup_v2_sys_app_isolation()) {
+1 −1
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@ package {


cc_library_static {
cc_library_static {
    name: "libprocessgroup_util",
    name: "libprocessgroup_util",
    cpp_std: "gnu++23",
    vendor_available: true,
    vendor_available: true,
    product_available: true,
    product_available: true,
    ramdisk_available: true,
    ramdisk_available: true,
@@ -47,7 +48,6 @@ cc_library_static {
    static_libs: [
    static_libs: [
        "libjsoncpp",
        "libjsoncpp",
    ],
    ],
    defaults: ["libprocessgroup_build_flags_cc"],
}
}


cc_test {
cc_test {
+0 −19
Original line number Original line Diff line number Diff line
@@ -111,7 +111,6 @@ void MergeCgroupToDescriptors(CgroupDescriptorMap* descriptors, const Json::Valu
}
}


bool ReadDescriptorsFromFile(const std::string& file_name, CgroupDescriptorMap* descriptors) {
bool ReadDescriptorsFromFile(const std::string& file_name, CgroupDescriptorMap* descriptors) {
    static constexpr bool force_memcg_v2 = android::libprocessgroup_flags::force_memcg_v2();
    std::vector<CgroupDescriptor> result;
    std::vector<CgroupDescriptor> result;
    std::string json_doc;
    std::string json_doc;


@@ -133,14 +132,10 @@ bool ReadDescriptorsFromFile(const std::string& file_name, CgroupDescriptorMap*
        const Json::Value& cgroups = root["Cgroups"];
        const Json::Value& cgroups = root["Cgroups"];
        for (Json::Value::ArrayIndex i = 0; i < cgroups.size(); ++i) {
        for (Json::Value::ArrayIndex i = 0; i < cgroups.size(); ++i) {
            std::string name = cgroups[i]["Controller"].asString();
            std::string name = cgroups[i]["Controller"].asString();

            if (force_memcg_v2 && name == "memory") continue;

            MergeCgroupToDescriptors(descriptors, cgroups[i], name, "", 1);
            MergeCgroupToDescriptors(descriptors, cgroups[i], name, "", 1);
        }
        }
    }
    }


    bool memcgv2_present = false;
    std::string root_path;
    std::string root_path;
    if (root.isMember("Cgroups2")) {
    if (root.isMember("Cgroups2")) {
        const Json::Value& cgroups2 = root["Cgroups2"];
        const Json::Value& cgroups2 = root["Cgroups2"];
@@ -150,24 +145,10 @@ bool ReadDescriptorsFromFile(const std::string& file_name, CgroupDescriptorMap*
        const Json::Value& childGroups = cgroups2["Controllers"];
        const Json::Value& childGroups = cgroups2["Controllers"];
        for (Json::Value::ArrayIndex i = 0; i < childGroups.size(); ++i) {
        for (Json::Value::ArrayIndex i = 0; i < childGroups.size(); ++i) {
            std::string name = childGroups[i]["Controller"].asString();
            std::string name = childGroups[i]["Controller"].asString();

            if (force_memcg_v2 && name == "memory") memcgv2_present = true;

            MergeCgroupToDescriptors(descriptors, childGroups[i], name, root_path, 2);
            MergeCgroupToDescriptors(descriptors, childGroups[i], name, root_path, 2);
        }
        }
    }
    }


    if (force_memcg_v2 && !memcgv2_present) {
        LOG(INFO) << "Forcing memcg to v2 hierarchy";
        Json::Value memcgv2;
        memcgv2["Controller"] = "memory";
        memcgv2["NeedsActivation"] = true;
        memcgv2["Path"] = ".";
        memcgv2["Optional"] = true;  // In case of cgroup_disabled=memory, so we can still boot
        MergeCgroupToDescriptors(descriptors, memcgv2, "memory",
                                 root_path.empty() ? CGROUP_V2_ROOT_DEFAULT : root_path, 2);
    }

    return true;
    return true;
}
}