diff options
author | Suren Baghdasaryan <surenb@google.com> | 2018-01-17 13:57:32 -0800 |
---|---|---|
committer | Suren Baghdasaryan <surenb@google.com> | 2018-01-17 16:41:57 -0800 |
commit | f58f6e8dd037a494ba90be9a89ec6bac4e749013 (patch) | |
tree | 7e037143fa1e31e0fced6ccffa512bd985e8d67d /alloc-stress | |
parent | 597f95eae7813532d1b99d92c788a170e6bed25f (diff) | |
download | extras-f58f6e8dd037a494ba90be9a89ec6bac4e749013.tar.gz |
alloc-stress: add cgroup usage flag and replace Android.mk with Android.bp
Add -g command-line flag to enable or disable memory cgroup usage. By
default cgroups are not used.
Replace Android.mk with Android.bp.
Change-Id: I3c181b323b3d90b3b12bca33103cfa6b8ce07a7e
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Diffstat (limited to 'alloc-stress')
-rw-r--r-- | alloc-stress/Android.bp | 43 | ||||
-rw-r--r-- | alloc-stress/Android.mk | 24 | ||||
-rw-r--r-- | alloc-stress/alloc-stress.cpp | 44 |
3 files changed, 78 insertions, 33 deletions
diff --git a/alloc-stress/Android.bp b/alloc-stress/Android.bp new file mode 100644 index 00000000..896db5ef --- /dev/null +++ b/alloc-stress/Android.bp @@ -0,0 +1,43 @@ +// +// Copyright (C) 2017 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 +// +// 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. +// + +cc_binary { + name: "alloc-stress", + srcs: ["alloc-stress.cpp"], + shared_libs: [ + "libhardware", + "libcutils", + ], + cppflags: [ + "-g", + "-Wall", + "-Werror", + "-Wno-missing-field-initializers", + "-Wno-sign-compare" + ] +} + +cc_binary { + name: "mem-pressure", + srcs: ["mem-pressure.cpp"], + cppflags: [ + "-g", + "-Wall", + "-Werror", + "-Wno-missing-field-initializers", + "-Wno-sign-compare" + ] +} diff --git a/alloc-stress/Android.mk b/alloc-stress/Android.mk deleted file mode 100644 index ad46f262..00000000 --- a/alloc-stress/Android.mk +++ /dev/null @@ -1,24 +0,0 @@ -LOCAL_PATH:= $(call my-dir) - -include $(CLEAR_VARS) -LOCAL_MODULE := alloc-stress -LOCAL_CFLAGS += -g -Wall -Werror -Wno-missing-field-initializers -Wno-sign-compare -ifneq ($(ENABLE_MEM_CGROUPS),) - LOCAL_CFLAGS += -DENABLE_MEM_CGROUPS -endif -LOCAL_C_INCLUDES += $(LOCAL_PATH)/../include -LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. -LOCAL_SHARED_LIBRARIES := libhardware libcutils -LOCAL_SRC_FILES := \ - alloc-stress.cpp -include $(BUILD_EXECUTABLE) - -include $(CLEAR_VARS) -LOCAL_MODULE := mem-pressure -LOCAL_CFLAGS += -g -Wall -Werror -Wno-missing-field-initializers -Wno-sign-compare -ifneq ($(ENABLE_MEM_CGROUPS),) - LOCAL_CFLAGS += -DENABLE_MEM_CGROUPS -endif -LOCAL_SRC_FILES := \ - mem-pressure.cpp -include $(BUILD_EXECUTABLE) diff --git a/alloc-stress/alloc-stress.cpp b/alloc-stress/alloc-stress.cpp index 7fd42b44..03cb655c 100644 --- a/alloc-stress/alloc-stress.cpp +++ b/alloc-stress/alloc-stress.cpp @@ -109,7 +109,8 @@ public: } }; -pid_t createProcess(Pipe pipe, const char *exName, const char *arg) +pid_t createProcess(Pipe pipe, const char *exName, + const char *arg, bool use_memcg) { pipe.preserveOverFork(true); pid_t pid = fork(); @@ -119,7 +120,8 @@ pid_t createProcess(Pipe pipe, const char *exName, const char *arg) char writeFdStr[16]; snprintf(readFdStr, sizeof(readFdStr), "%d", pipe.getReadFd()); snprintf(writeFdStr, sizeof(writeFdStr), "%d", pipe.getWriteFd()); - execl(exName, exName, "--worker", arg, readFdStr, writeFdStr, nullptr); + execl(exName, exName, "--worker", arg, readFdStr, writeFdStr, + use_memcg ? "1" : "0", nullptr); ASSERT_TRUE(0); } // parent process @@ -157,7 +159,6 @@ static void write_oomadj_to_lmkd(int oomadj) { cout << "Wrote " << written << " bytes to lmkd control socket." << endl; } -#ifdef ENABLE_MEM_CGROUPS static void create_memcg() { char buf[256]; uid_t uid = getuid(); @@ -187,16 +188,26 @@ static void create_memcg() { write(tasks, buf, strlen(buf)); close(tasks); } -#endif + +void usage() { + cout << "Application allocates memory until it's killed." << endl + << "It starts at max oom_score_adj and gradually " + << "decreases it to 0." << endl + << "Usage: alloc-stress [-g | --cgroup]" << endl + << "\t-g | --cgroup\tcreates memory cgroup for the process" << endl; +} size_t s = 4 * (1 << 20); void *gptr; int main(int argc, char *argv[]) { + bool use_memcg = false; + if ((argc > 1) && (std::string(argv[1]) == "--worker")) { -#ifdef ENABLE_MEM_CGROUPS - create_memcg(); -#endif + if (std::string(argv[5]) == "1") { + create_memcg(); + } + write_oomadj_to_lmkd(atoi(argv[2])); Pipe p{atoi(argv[3]), atoi(argv[4])}; @@ -216,7 +227,21 @@ int main(int argc, char *argv[]) allocCount += s; } } else { - cout << "parent:" << argc << endl; + if (argc == 2) { + if (std::string(argv[1]) == "--help" || + std::string(argv[1]) == "-h") { + usage(); + return 0; + } + + if (std::string(argv[1]) == "--cgroup" || + std::string(argv[1]) == "-g") { + use_memcg = true; + } + } + + cout << "Memory cgroups are " + << (use_memcg ? "used" : "not used") << endl; write_oomadj_to_lmkd(-1000); for (int i = 1000; i >= 0; i -= 100) { @@ -224,7 +249,8 @@ int main(int argc, char *argv[]) char arg[16]; pid_t ch_pid; snprintf(arg, sizeof(arg), "%d", i); - ch_pid = createProcess(std::move(std::get<1>(pipes)), argv[0], arg); + ch_pid = createProcess(std::move(std::get<1>(pipes)), + argv[0], arg, use_memcg); Pipe &p = std::get<0>(pipes); size_t t = 0; |