diff options
| -rw-r--r-- | cmds/rss_hwm_reset/Android.bp | 28 | ||||
| -rw-r--r-- | cmds/rss_hwm_reset/rss_hwm_reset.cc | 72 | ||||
| -rw-r--r-- | cmds/rss_hwm_reset/rss_hwm_reset.rc | 26 |
3 files changed, 126 insertions, 0 deletions
diff --git a/cmds/rss_hwm_reset/Android.bp b/cmds/rss_hwm_reset/Android.bp new file mode 100644 index 0000000000..15f10efdee --- /dev/null +++ b/cmds/rss_hwm_reset/Android.bp @@ -0,0 +1,28 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// 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 +// +// https://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. + +cc_binary { + name: "rss_hwm_reset", + + srcs: [ + "rss_hwm_reset.cc", + ], + + shared_libs: [ + "libbase", + "liblog", + ], + + init_rc: ["rss_hwm_reset.rc"], +} diff --git a/cmds/rss_hwm_reset/rss_hwm_reset.cc b/cmds/rss_hwm_reset/rss_hwm_reset.cc new file mode 100644 index 0000000000..1626e7ec50 --- /dev/null +++ b/cmds/rss_hwm_reset/rss_hwm_reset.cc @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * 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 + * + * https://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. + */ + + /* + * rss_hwm_reset clears the RSS high-water mark counters for all currently + * running processes. It writes "5" to /proc/PID/clear_refs for every PID. + * + * It runs in its own process becuase dac_override capability is required + * in order to write to other processes' clear_refs. + * + * It is invoked from a system service by flipping sys.rss_hwm_reset.on + * property to "1". + */ + +#define LOG_TAG "rss_hwm_reset" + +#include <dirent.h> + +#include <string> + +#include <android-base/file.h> +#include <android-base/stringprintf.h> +#include <log/log.h> + +namespace { +// Resets RSS HWM counter for the selected process by writing 5 to +// /proc/PID/clear_refs. +void reset_rss_hwm(const char* pid) { + std::string clear_refs_path = + ::android::base::StringPrintf("/proc/%s/clear_refs", pid); + ::android::base::WriteStringToFile("5", clear_refs_path); +} +} + +// Clears RSS HWM counters for all currently running processes. +int main(int /* argc */, char** /* argv[] */) { + DIR* dirp = opendir("/proc"); + if (dirp == nullptr) { + ALOGE("unable to read /proc"); + return 1; + } + struct dirent* entry; + while ((entry = readdir(dirp)) != nullptr) { + // Skip entries that are not directories. + if (entry->d_type != DT_DIR) continue; + // Skip entries that do not contain only numbers. + const char* pid = entry->d_name; + while (*pid) { + if (*pid < '0' || *pid > '9') break; + pid++; + } + if (*pid != 0) continue; + + pid = entry->d_name; + reset_rss_hwm(pid); + } + closedir(dirp); + return 0; +} diff --git a/cmds/rss_hwm_reset/rss_hwm_reset.rc b/cmds/rss_hwm_reset/rss_hwm_reset.rc new file mode 100644 index 0000000000..fbbc8200b2 --- /dev/null +++ b/cmds/rss_hwm_reset/rss_hwm_reset.rc @@ -0,0 +1,26 @@ +# Copyright (C) 2018 The Android Open Source Project +# +# 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 +# +# https://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. + +service rss_hwm_reset /system/bin/rss_hwm_reset + class late_start + disabled + oneshot + user nobody + group nobody readproc + writepid /dev/cpuset/system-background/tasks + capabilities DAC_OVERRIDE + +on property:sys.rss_hwm_reset.on=1 + start rss_hwm_reset + setprop sys.rss_hwm_reset.on 0 |