summaryrefslogtreecommitdiff
path: root/alloc-stress
diff options
context:
space:
mode:
authorThiƩbaud Weksteen <tweek@google.com>2020-10-23 16:35:37 +0200
committerThiƩbaud Weksteen <tweek@google.com>2020-10-26 15:51:07 +0100
commitd637ec3e1a7d00cb04de92629eb3b0a192c3b5a8 (patch)
tree1ad8c803b23d3c84a0fa8480824d134645477576 /alloc-stress
parent7ebc9e6cd61d5b167ccccbea85f42387370f51a7 (diff)
downloadextras-d637ec3e1a7d00cb04de92629eb3b0a192c3b5a8.tar.gz
alloc-stress: format files
Format *.cpp according to the new .clang-format. The following command was used to generate this change: $ find . \( -name \*.cpp -o -name \*.h \) -exec clang-format \ --style=file -i {} \; Test: mm Bug: 171699326 Change-Id: I98b1cd32663fa4b8ffae3c5e74d2db0ec862e144
Diffstat (limited to 'alloc-stress')
-rw-r--r--alloc-stress/alloc-stress.cpp126
-rw-r--r--alloc-stress/mem-pressure.cpp38
2 files changed, 72 insertions, 92 deletions
diff --git a/alloc-stress/alloc-stress.cpp b/alloc-stress/alloc-stress.cpp
index 1cd8fddf..33c12635 100644
--- a/alloc-stress/alloc-stress.cpp
+++ b/alloc-stress/alloc-stress.cpp
@@ -1,44 +1,46 @@
#include <arpa/inet.h>
-#include <iostream>
-#include <chrono>
#include <cutils/sockets.h>
-#include <hardware/gralloc.h>
-#include <vector>
-#include <tuple>
-#include <algorithm>
-#include <tuple>
-#include <numeric>
#include <fcntl.h>
-#include <string>
-#include <fstream>
+#include <hardware/gralloc.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
+#include <algorithm>
+#include <chrono>
+#include <fstream>
+#include <iostream>
+#include <numeric>
+#include <string>
+#include <tuple>
+#include <vector>
//#define TRACE_CHILD_LIFETIME
#ifdef TRACE_CHILD_LIFETIME
#define ATRACE_TAG ATRACE_TAG_ALWAYS
#include <utils/Trace.h>
-#endif // TRACE_CHILD_LIFETIME
+#endif // TRACE_CHILD_LIFETIME
using namespace std;
-#define ASSERT_TRUE(cond) \
-do { \
- if (!(cond)) {\
- cerr << __func__ << "( " << getpid() << "):" << __LINE__ << " condition:" << #cond << " failed\n" << endl; \
- exit(EXIT_FAILURE); \
- } \
-} while (0)
+#define ASSERT_TRUE(cond) \
+ do { \
+ if (!(cond)) { \
+ cerr << __func__ << "( " << getpid() << "):" << __LINE__ << " condition:" << #cond \
+ << " failed\n" \
+ << endl; \
+ exit(EXIT_FAILURE); \
+ } \
+ } while (0)
class Pipe {
int m_readFd;
int m_writeFd;
- Pipe(const Pipe &) = delete;
- Pipe& operator=(const Pipe &) = delete;
- Pipe& operator=(const Pipe &&) = delete;
-public:
+ Pipe(const Pipe&) = delete;
+ Pipe& operator=(const Pipe&) = delete;
+ Pipe& operator=(const Pipe&&) = delete;
+
+ public:
Pipe(int readFd, int writeFd) : m_readFd{readFd}, m_writeFd{writeFd} {
fcntl(m_readFd, F_SETFD, FD_CLOEXEC);
fcntl(m_writeFd, F_SETFD, FD_CLOEXEC);
@@ -50,26 +52,20 @@ public:
rval.m_writeFd = 0;
}
~Pipe() {
- if (m_readFd)
- close(m_readFd);
- if (m_writeFd)
- close(m_writeFd);
+ if (m_readFd) close(m_readFd);
+ if (m_writeFd) close(m_writeFd);
}
void preserveOverFork(bool preserve) {
if (preserve) {
fcntl(m_readFd, F_SETFD, 0);
- fcntl(m_writeFd, F_SETFD,0);
+ fcntl(m_writeFd, F_SETFD, 0);
} else {
fcntl(m_readFd, F_SETFD, FD_CLOEXEC);
fcntl(m_writeFd, F_SETFD, FD_CLOEXEC);
}
}
- int getReadFd() {
- return m_readFd;
- }
- int getWriteFd() {
- return m_writeFd;
- }
+ int getReadFd() { return m_readFd; }
+ int getWriteFd() { return m_writeFd; }
void signal() {
bool val = true;
int error = write(m_writeFd, &val, sizeof(val));
@@ -85,17 +81,17 @@ public:
int error = read(m_readFd, &val, sizeof(val));
return (error != 1);
}
- template <typename T> void send(const T& v) {
+ template <typename T>
+ void send(const T& v) {
int error = write(m_writeFd, &v, sizeof(T));
ASSERT_TRUE(error >= 0);
}
- template <typename T> void recv(T& v) {
+ template <typename T>
+ void recv(T& v) {
int error = read(m_readFd, &v, sizeof(T));
ASSERT_TRUE(error >= 0);
}
- static Pipe makePipeFromFds(int readFd, int writeFd) {
- return Pipe(readFd, writeFd);
- }
+ static Pipe makePipeFromFds(int readFd, int writeFd) { return Pipe(readFd, writeFd); }
static tuple<Pipe, Pipe> createPipePair() {
int a[2];
int b[2];
@@ -109,9 +105,7 @@ public:
}
};
-pid_t createProcess(Pipe pipe, const char *exName,
- const char *arg, bool use_memcg)
-{
+pid_t createProcess(Pipe pipe, const char* exName, const char* arg, bool use_memcg) {
pipe.preserveOverFork(true);
pid_t pid = fork();
// child proc
@@ -123,33 +117,30 @@ pid_t createProcess(Pipe pipe, const char *exName,
char exPath[PATH_MAX];
ssize_t exPathLen = readlink("/proc/self/exe", exPath, sizeof(exPath));
bool isExPathAvailable =
- exPathLen != -1 && exPathLen < static_cast<ssize_t>(sizeof(exPath));
+ exPathLen != -1 && exPathLen < static_cast<ssize_t>(sizeof(exPath));
if (isExPathAvailable) {
- exPath[exPathLen] = '\0';
+ exPath[exPathLen] = '\0';
}
execl(isExPathAvailable ? exPath : exName, exName, "--worker", arg, readFdStr, writeFdStr,
- use_memcg ? "1" : "0", nullptr);
+ use_memcg ? "1" : "0", nullptr);
ASSERT_TRUE(0);
}
// parent process
else if (pid > 0) {
pipe.preserveOverFork(false);
- }
- else {
+ } else {
ASSERT_TRUE(0);
}
return pid;
}
-
static void write_oomadj_to_lmkd(int oomadj) {
// Connect to lmkd and store our oom_adj
int lmk_procprio_cmd[4];
int sock;
int tries = 10;
- while ((sock = socket_local_client("lmkd",
- ANDROID_SOCKET_NAMESPACE_RESERVED,
- SOCK_SEQPACKET)) < 0) {
+ while ((sock = socket_local_client("lmkd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET)) <
+ 0) {
usleep(100000);
if (tries-- < 0) break;
}
@@ -198,16 +189,15 @@ static void create_memcg() {
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;
+ << "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[])
-{
+void* gptr;
+int main(int argc, char* argv[]) {
bool use_memcg = false;
if ((argc > 1) && (std::string(argv[1]) == "--worker")) {
@@ -221,34 +211,31 @@ int main(int argc, char *argv[])
long long allocCount = 0;
while (1) {
p.wait();
- char *ptr = (char*)malloc(s);
+ char* ptr = (char*)malloc(s);
memset(ptr, (int)allocCount >> 10, s);
- for (int i = 0; i < s; i+= 4096) {
+ for (int i = 0; i < s; i += 4096) {
*((long long*)&ptr[i]) = allocCount + i;
}
usleep(10 * 1000);
gptr = ptr;
- //cout << "total alloc: " << allocCount / (1<<20)<< " adj: " << argv[2]<< endl;;
- //cout << "ptr: " << (long long)(void*)ptr << endl;;
+ // cout << "total alloc: " << allocCount / (1<<20)<< " adj: " << argv[2]<< endl;;
+ // cout << "ptr: " << (long long)(void*)ptr << endl;;
p.signal();
allocCount += s;
}
} else {
if (argc == 2) {
- if (std::string(argv[1]) == "--help" ||
- std::string(argv[1]) == "-h") {
+ 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") {
+ if (std::string(argv[1]) == "--cgroup" || std::string(argv[1]) == "-g") {
use_memcg = true;
}
}
- cout << "Memory cgroups are "
- << (use_memcg ? "used" : "not used") << endl;
+ cout << "Memory cgroups are " << (use_memcg ? "used" : "not used") << endl;
write_oomadj_to_lmkd(-1000);
for (int i = 1000; i >= 0; i -= 100) {
@@ -256,9 +243,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, use_memcg);
- Pipe &p = std::get<0>(pipes);
+ ch_pid = createProcess(std::move(std::get<1>(pipes)), argv[0], arg, use_memcg);
+ Pipe& p = std::get<0>(pipes);
size_t t = 0;
diff --git a/alloc-stress/mem-pressure.cpp b/alloc-stress/mem-pressure.cpp
index 82b2273c..b0920e69 100644
--- a/alloc-stress/mem-pressure.cpp
+++ b/alloc-stress/mem-pressure.cpp
@@ -8,8 +8,8 @@
#include <sys/wait.h>
#include <unistd.h>
-void *alloc_set(size_t size) {
- void *addr = NULL;
+void* alloc_set(size_t size) {
+ void* addr = NULL;
addr = malloc(size);
if (!addr) {
@@ -20,15 +20,14 @@ void *alloc_set(size_t size) {
return addr;
}
-void add_pressure(size_t *shared, size_t size, size_t step_size,
- size_t duration, const char *oom_score) {
+void add_pressure(size_t* shared, size_t size, size_t step_size, size_t duration,
+ const char* oom_score) {
int fd, ret;
fd = open("/proc/self/oom_score_adj", O_WRONLY);
ret = write(fd, oom_score, strlen(oom_score));
if (ret < 0) {
- printf("Writing oom_score_adj failed with err %s\n",
- strerror(errno));
+ printf("Writing oom_score_adj failed with err %s\n", strerror(errno));
}
close(fd);
@@ -43,31 +42,27 @@ void add_pressure(size_t *shared, size_t size, size_t step_size,
}
}
-void usage()
-{
+void usage() {
printf("Usage: [OPTIONS]\n\n"
" -d N: Duration in microsecond to sleep between each allocation.\n"
" -i N: Number of iterations to run the alloc process.\n"
" -o N: The oom_score to set the child process to before alloc.\n"
- " -s N: Number of bytes to allocate in an alloc process loop.\n"
- );
+ " -s N: Number of bytes to allocate in an alloc process loop.\n");
}
-int main(int argc, char *argv[])
-{
+int main(int argc, char* argv[]) {
pid_t pid;
- size_t *shared;
+ size_t* shared;
int c, i = 0;
size_t duration = 1000;
int iterations = 0;
- const char *oom_score = "899";
- size_t step_size = 2 * 1024 * 1024; // 2 MB
+ const char* oom_score = "899";
+ size_t step_size = 2 * 1024 * 1024; // 2 MB
size_t size = step_size;
while ((c = getopt(argc, argv, "hi:d:o:s:")) != -1) {
- switch (c)
- {
+ switch (c) {
case 'i':
iterations = atoi(optarg);
break;
@@ -85,11 +80,11 @@ int main(int argc, char *argv[])
abort();
default:
abort();
- }
+ }
}
- shared = (size_t *)mmap(NULL, sizeof(size_t), PROT_READ | PROT_WRITE,
- MAP_ANONYMOUS | MAP_SHARED, 0, 0);
+ shared = (size_t*)mmap(NULL, sizeof(size_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED,
+ 0, 0);
while (iterations == 0 || i < iterations) {
*shared = 0;
@@ -101,8 +96,7 @@ int main(int argc, char *argv[])
exit(0);
} else {
wait(NULL);
- printf("Child %d allocated %zd MB\n", i,
- *shared / 1024 / 1024);
+ printf("Child %d allocated %zd MB\n", i, *shared / 1024 / 1024);
size = *shared / 2;
}
i++;