diff --git a/libprocessgroup/Android.mk b/libprocessgroup/Android.mk index ee6ba58686a8baa891920f647dd78d19e81c25bd..1885fa5ddf98e3ca8e83f2f1a24a6f928c17482d 100644 --- a/libprocessgroup/Android.mk +++ b/libprocessgroup/Android.mk @@ -7,14 +7,4 @@ LOCAL_SHARED_LIBRARIES := liblog libutils LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include LOCAL_CFLAGS := -Wall -Werror -LOCAL_REQUIRED_MODULE := processgroup_cleanup include $(BUILD_SHARED_LIBRARY) - -include $(CLEAR_VARS) -LOCAL_SRC_FILES := cleanup.cpp -LOCAL_MODULE := processgroup_cleanup -LOCAL_C_INCLUDES := $(LOCAL_PATH)/include -LOCAL_CFLAGS := -Wall -Werror -LOCAL_FORCE_STATIC_EXECUTABLE := true -LOCAL_STATIC_LIBRARIES := libc libcutils -include $(BUILD_EXECUTABLE) diff --git a/libprocessgroup/cleanup.cpp b/libprocessgroup/cleanup.cpp deleted file mode 100644 index cca8dc4287e37049a01112f3138a97604d0b91da..0000000000000000000000000000000000000000 --- a/libprocessgroup/cleanup.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2014 Google, Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include <string.h> -#include <unistd.h> -#include <sys/syslimits.h> - -#include "processgroup_priv.h" - -int main(int argc, char **argv) -{ - char buf[PATH_MAX]; - if (argc != 2) - return -1; - - memcpy(buf, PROCESSGROUP_CGROUP_PATH, sizeof(PROCESSGROUP_CGROUP_PATH)); - strlcat(buf, argv[1], sizeof(buf)); - return rmdir(buf); -} diff --git a/libprocessgroup/processgroup.cpp b/libprocessgroup/processgroup.cpp index ad0500daba808f0f68cc8b6ca7d2cf25359c95ed..f160ac17d75a93bc0794da5aa1cc7ba779d1246f 100644 --- a/libprocessgroup/processgroup.cpp +++ b/libprocessgroup/processgroup.cpp @@ -22,6 +22,7 @@ #include <errno.h> #include <fcntl.h> #include <inttypes.h> +#include <mutex> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> @@ -35,7 +36,26 @@ #include <utils/SystemClock.h> #include <processgroup/processgroup.h> -#include "processgroup_priv.h" + +#define MEM_CGROUP_PATH "/dev/memcg/apps" +#define ACCT_CGROUP_PATH "/acct" + +#define PROCESSGROUP_UID_PREFIX "uid_" +#define PROCESSGROUP_PID_PREFIX "pid_" +#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs" +#define PROCESSGROUP_MAX_UID_LEN 11 +#define PROCESSGROUP_MAX_PID_LEN 11 +#define PROCESSGROUP_MAX_PATH_LEN \ + ((sizeof(MEM_CGROUP_PATH) > sizeof(ACCT_CGROUP_PATH) ? \ + sizeof(MEM_CGROUP_PATH) : sizeof(ACCT_CGROUP_PATH)) + \ + sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \ + PROCESSGROUP_MAX_UID_LEN + \ + sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \ + PROCESSGROUP_MAX_PID_LEN + \ + sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \ + 1) + +std::once_flag init_path_flag; struct ctx { bool initialized; @@ -45,10 +65,18 @@ struct ctx { size_t buf_len; }; +static const char* getCgroupRootPath() { + static const char* cgroup_root_path = NULL; + std::call_once(init_path_flag, [&]() { + cgroup_root_path = access(MEM_CGROUP_PATH, W_OK) ? ACCT_CGROUP_PATH : MEM_CGROUP_PATH; + }); + return cgroup_root_path; +} + static int convertUidToPath(char *path, size_t size, uid_t uid) { return snprintf(path, size, "%s/%s%d", - PROCESSGROUP_CGROUP_PATH, + getCgroupRootPath(), PROCESSGROUP_UID_PREFIX, uid); } @@ -56,7 +84,7 @@ static int convertUidToPath(char *path, size_t size, uid_t uid) static int convertUidPidToPath(char *path, size_t size, uid_t uid, int pid) { return snprintf(path, size, "%s/%s%d/%s%d", - PROCESSGROUP_CGROUP_PATH, + getCgroupRootPath(), PROCESSGROUP_UID_PREFIX, uid, PROCESSGROUP_PID_PREFIX, @@ -188,9 +216,10 @@ static void removeUidProcessGroups(const char *uid_path) void removeAllProcessGroups() { SLOGV("removeAllProcessGroups()"); - DIR *root = opendir(PROCESSGROUP_CGROUP_PATH); + const char *cgroup_root_path = getCgroupRootPath(); + DIR *root = opendir(cgroup_root_path); if (root == NULL) { - SLOGE("failed to open %s: %s", PROCESSGROUP_CGROUP_PATH, strerror(errno)); + SLOGE("failed to open %s: %s", cgroup_root_path, strerror(errno)); } else { struct dirent cur; struct dirent *dir; @@ -204,7 +233,7 @@ void removeAllProcessGroups() continue; } - snprintf(path, sizeof(path), "%s/%s", PROCESSGROUP_CGROUP_PATH, dir->d_name); + snprintf(path, sizeof(path), "%s/%s", cgroup_root_path, dir->d_name); removeUidProcessGroups(path); SLOGV("removing %s\n", path); rmdir(path); diff --git a/libprocessgroup/processgroup_priv.h b/libprocessgroup/processgroup_priv.h deleted file mode 100644 index 1895bf9d55f63d3b5ea9f457771e86b0ba278cbf..0000000000000000000000000000000000000000 --- a/libprocessgroup/processgroup_priv.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2014 Google, Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _PROCESSGROUP_PRIV_H_ -#define _PROCESSGROUP_PRIV_H_ - -#define PROCESSGROUP_CGROUP_PATH "/acct" -#define PROCESSGROUP_UID_PREFIX "uid_" -#define PROCESSGROUP_PID_PREFIX "pid_" -#define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs" -#define PROCESSGROUP_MAX_UID_LEN 11 -#define PROCESSGROUP_MAX_PID_LEN 11 -#define PROCESSGROUP_MAX_PATH_LEN \ - (sizeof(PROCESSGROUP_CGROUP_PATH) + \ - sizeof(PROCESSGROUP_UID_PREFIX) + 1 + \ - PROCESSGROUP_MAX_UID_LEN + \ - sizeof(PROCESSGROUP_PID_PREFIX) + 1 + \ - PROCESSGROUP_MAX_PID_LEN + \ - sizeof(PROCESSGROUP_CGROUP_PROCS_FILE) + \ - 1) - -#endif diff --git a/rootdir/init.rc b/rootdir/init.rc index 441f28b10b888a2b29f2833da0fa6913d8197fea..11c859eb004f91b0d590377ade0c524928fcaebf 100644 --- a/rootdir/init.rc +++ b/rootdir/init.rc @@ -43,19 +43,6 @@ on init mount cgroup none /acct cpuacct mkdir /acct/uid - # Create cgroup mount point for memory - mount tmpfs none /sys/fs/cgroup mode=0750,uid=0,gid=1000 - mkdir /sys/fs/cgroup/memory 0750 root system - mount cgroup none /sys/fs/cgroup/memory memory - write /sys/fs/cgroup/memory/memory.move_charge_at_immigrate 1 - chown root system /sys/fs/cgroup/memory/tasks - chmod 0660 /sys/fs/cgroup/memory/tasks - mkdir /sys/fs/cgroup/memory/sw 0750 root system - write /sys/fs/cgroup/memory/sw/memory.swappiness 100 - write /sys/fs/cgroup/memory/sw/memory.move_charge_at_immigrate 1 - chown root system /sys/fs/cgroup/memory/sw/tasks - chmod 0660 /sys/fs/cgroup/memory/sw/tasks - # Create energy-aware scheduler tuning nodes mkdir /sys/fs/cgroup/stune mount cgroup none /sys/fs/cgroup/stune schedtune @@ -95,9 +82,11 @@ on init symlink /storage/self/primary /sdcard symlink /mnt/user/0/primary /mnt/runtime/default/self/primary - # memory control cgroup + # root memory control cgroup, used by lmkd mkdir /dev/memcg 0700 root system mount cgroup none /dev/memcg memory + # app mem cgroups, used by activity manager and lmkd + mkdir /dev/memcg/apps/ 0755 system system write /proc/sys/kernel/panic_on_oops 1 write /proc/sys/kernel/hung_task_timeout_secs 0