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

Commit 5d6aa920 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "libprocessgroup: Remove PRODUCT_MEMCG_V2_FORCE_ENABLED" into main

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

#pragma once

#ifndef MEMCG_V2_FORCE_ENABLED
#define MEMCG_V2_FORCE_ENABLED false
#endif

#ifndef CGROUP_V2_SYS_APP_ISOLATION
#define CGROUP_V2_SYS_APP_ISOLATION false
#endif

namespace android::libprocessgroup_flags {

inline consteval bool force_memcg_v2() {
    return MEMCG_V2_FORCE_ENABLED;
}

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

#include <optional>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <processgroup/cgroup_descriptor.h>
#include <processgroup/processgroup.h>
@@ -260,39 +257,6 @@ void CgroupDescriptor::set_mounted(bool mounted) {
    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) {
    const auto cgv2_iter = descriptors.find(CGROUPV2_HIERARCHY_NAME);
    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.
    // 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()) {
+1 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ package {

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

cc_test {
+0 −19
Original line number 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) {
    static constexpr bool force_memcg_v2 = android::libprocessgroup_flags::force_memcg_v2();
    std::vector<CgroupDescriptor> result;
    std::string json_doc;

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

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

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

    bool memcgv2_present = false;
    std::string root_path;
    if (root.isMember("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"];
        for (Json::Value::ArrayIndex i = 0; i < childGroups.size(); ++i) {
            std::string name = childGroups[i]["Controller"].asString();

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

            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;
}