1 #include <arpa/inet.h>
2 #include <cutils/sockets.h>
3 #include <fcntl.h>
4 #include <hardware/gralloc.h>
5 #include <sys/stat.h>
6 #include <sys/wait.h>
7 #include <unistd.h>
8 #include <algorithm>
9 #include <chrono>
10 #include <fstream>
11 #include <iostream>
12 #include <numeric>
13 #include <string>
14 #include <tuple>
15 #include <vector>
16
17 #include <processgroup/processgroup.h>
18
19 //#define TRACE_CHILD_LIFETIME
20
21 #ifdef TRACE_CHILD_LIFETIME
22 #define ATRACE_TAG ATRACE_TAG_ALWAYS
23 #include <utils/Trace.h>
24 #endif // TRACE_CHILD_LIFETIME
25
26 using namespace std;
27
28 #define ASSERT_TRUE(cond) \
29 do { \
30 if (!(cond)) { \
31 cerr << __func__ << "( " << getpid() << "):" << __LINE__ << " condition:" << #cond \
32 << " failed\n" \
33 << endl; \
34 exit(EXIT_FAILURE); \
35 } \
36 } while (0)
37
38 class Pipe {
39 int m_readFd;
40 int m_writeFd;
41 Pipe(const Pipe&) = delete;
42 Pipe& operator=(const Pipe&) = delete;
43 Pipe& operator=(const Pipe&&) = delete;
44
45 public:
Pipe(int readFd,int writeFd)46 Pipe(int readFd, int writeFd) : m_readFd{readFd}, m_writeFd{writeFd} {
47 fcntl(m_readFd, F_SETFD, FD_CLOEXEC);
48 fcntl(m_writeFd, F_SETFD, FD_CLOEXEC);
49 }
Pipe(Pipe && rval)50 Pipe(Pipe&& rval) noexcept {
51 m_readFd = rval.m_readFd;
52 m_writeFd = rval.m_writeFd;
53 rval.m_readFd = 0;
54 rval.m_writeFd = 0;
55 }
~Pipe()56 ~Pipe() {
57 if (m_readFd) close(m_readFd);
58 if (m_writeFd) close(m_writeFd);
59 }
preserveOverFork(bool preserve)60 void preserveOverFork(bool preserve) {
61 if (preserve) {
62 fcntl(m_readFd, F_SETFD, 0);
63 fcntl(m_writeFd, F_SETFD, 0);
64 } else {
65 fcntl(m_readFd, F_SETFD, FD_CLOEXEC);
66 fcntl(m_writeFd, F_SETFD, FD_CLOEXEC);
67 }
68 }
getReadFd()69 int getReadFd() { return m_readFd; }
getWriteFd()70 int getWriteFd() { return m_writeFd; }
signal()71 void signal() {
72 bool val = true;
73 int error = write(m_writeFd, &val, sizeof(val));
74 ASSERT_TRUE(error == sizeof(val));
75 };
wait()76 void wait() {
77 bool val = false;
78 int error = read(m_readFd, &val, sizeof(val));
79 ASSERT_TRUE(error == sizeof(val));
80 }
wait_ret_error()81 bool wait_ret_error() {
82 bool val = false;
83 int error = read(m_readFd, &val, sizeof(val));
84 return (error != 1);
85 }
86 template <typename T>
send(const T & v)87 void send(const T& v) {
88 int error = write(m_writeFd, &v, sizeof(T));
89 ASSERT_TRUE(error >= 0);
90 }
91 template <typename T>
recv(T & v)92 void recv(T& v) {
93 int error = read(m_readFd, &v, sizeof(T));
94 ASSERT_TRUE(error >= 0);
95 }
makePipeFromFds(int readFd,int writeFd)96 static Pipe makePipeFromFds(int readFd, int writeFd) { return Pipe(readFd, writeFd); }
createPipePair()97 static tuple<Pipe, Pipe> createPipePair() {
98 int a[2];
99 int b[2];
100
101 int error1 = pipe(a);
102 int error2 = pipe(b);
103 ASSERT_TRUE(error1 >= 0);
104 ASSERT_TRUE(error2 >= 0);
105
106 return make_tuple(Pipe(a[0], b[1]), Pipe(b[0], a[1]));
107 }
108 };
109
createProcess(Pipe pipe,const char * exName,const char * arg,bool use_memcg)110 pid_t createProcess(Pipe pipe, const char* exName, const char* arg, bool use_memcg) {
111 pipe.preserveOverFork(true);
112 pid_t pid = fork();
113 // child proc
114 if (pid == 0) {
115 char readFdStr[16];
116 char writeFdStr[16];
117 snprintf(readFdStr, sizeof(readFdStr), "%d", pipe.getReadFd());
118 snprintf(writeFdStr, sizeof(writeFdStr), "%d", pipe.getWriteFd());
119 char exPath[PATH_MAX];
120 ssize_t exPathLen = readlink("/proc/self/exe", exPath, sizeof(exPath));
121 bool isExPathAvailable =
122 exPathLen != -1 && exPathLen < static_cast<ssize_t>(sizeof(exPath));
123 if (isExPathAvailable) {
124 exPath[exPathLen] = '\0';
125 }
126 execl(isExPathAvailable ? exPath : exName, exName, "--worker", arg, readFdStr, writeFdStr,
127 use_memcg ? "1" : "0", nullptr);
128 ASSERT_TRUE(0);
129 }
130 // parent process
131 else if (pid > 0) {
132 pipe.preserveOverFork(false);
133 } else {
134 ASSERT_TRUE(0);
135 }
136 return pid;
137 }
138
write_oomadj_to_lmkd(int oomadj)139 static void write_oomadj_to_lmkd(int oomadj) {
140 // Connect to lmkd and store our oom_adj
141 int lmk_procprio_cmd[4];
142 int sock;
143 int tries = 10;
144 while ((sock = socket_local_client("lmkd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET)) <
145 0) {
146 usleep(100000);
147 if (tries-- < 0) break;
148 }
149 if (sock < 0) {
150 cout << "Failed to connect to lmkd, errno " << errno << endl;
151 exit(1);
152 }
153 lmk_procprio_cmd[0] = htonl(1);
154 lmk_procprio_cmd[1] = htonl(getpid());
155 lmk_procprio_cmd[2] = htonl(getuid());
156 lmk_procprio_cmd[3] = htonl(oomadj);
157
158 int written = write(sock, lmk_procprio_cmd, sizeof(lmk_procprio_cmd));
159 cout << "Wrote " << written << " bytes to lmkd control socket." << endl;
160 }
161
usage()162 void usage() {
163 cout << "Application allocates memory until it's killed." << endl
164 << "It starts at max oom_score_adj and gradually "
165 << "decreases it to 0." << endl
166 << "Usage: alloc-stress [-g | --cgroup]" << endl
167 << "\t-g | --cgroup\tcreates memory cgroup for the process" << endl;
168 }
169
170 size_t s = 4 * (1 << 20);
171 void* gptr;
main(int argc,char * argv[])172 int main(int argc, char* argv[]) {
173 bool use_memcg = false;
174
175 if ((argc > 1) && (std::string(argv[1]) == "--worker")) {
176 if (std::string(argv[5]) == "1") {
177 createProcessGroup(getuid(), getpid(), true);
178 }
179
180 write_oomadj_to_lmkd(atoi(argv[2]));
181 Pipe p{atoi(argv[3]), atoi(argv[4])};
182
183 long long allocCount = 0;
184 while (1) {
185 p.wait();
186 char* ptr = (char*)malloc(s);
187 memset(ptr, (int)allocCount >> 10, s);
188 for (int i = 0; i < s; i += 4096) {
189 *((long long*)&ptr[i]) = allocCount + i;
190 }
191 usleep(10 * 1000);
192 gptr = ptr;
193 // cout << "total alloc: " << allocCount / (1<<20)<< " adj: " << argv[2]<< endl;;
194 // cout << "ptr: " << (long long)(void*)ptr << endl;;
195 p.signal();
196 allocCount += s;
197 }
198 } else {
199 if (argc == 2) {
200 if (std::string(argv[1]) == "--help" || std::string(argv[1]) == "-h") {
201 usage();
202 return 0;
203 }
204
205 if (std::string(argv[1]) == "--cgroup" || std::string(argv[1]) == "-g") {
206 use_memcg = true;
207 }
208 }
209
210 cout << "Memory cgroups are " << (use_memcg ? "used" : "not used") << endl;
211
212 write_oomadj_to_lmkd(-1000);
213 for (int i = 1000; i >= 0; i -= 100) {
214 auto pipes = Pipe::createPipePair();
215 char arg[16];
216 pid_t ch_pid;
217 snprintf(arg, sizeof(arg), "%d", i);
218 ch_pid = createProcess(std::move(std::get<1>(pipes)), argv[0], arg, use_memcg);
219 Pipe& p = std::get<0>(pipes);
220
221 size_t t = 0;
222
223 #ifdef TRACE_CHILD_LIFETIME
224 char trace_str[64];
225 snprintf(trace_str, sizeof(trace_str), "alloc-stress, adj=%d, pid=%u", i, ch_pid);
226 ATRACE_INT(trace_str, i);
227 #endif
228 while (1) {
229 //;cout << getpid() << ":" << "parent signal" << endl;
230 p.signal();
231 if (p.wait_ret_error()) {
232 int status;
233 waitpid(0, &status, 0);
234 break;
235 }
236 t += s;
237 }
238 cout << "pid: " << ch_pid << " adj: " << i << " sz: " << t / (1 << 20) << endl;
239 #ifdef TRACE_CHILD_LIFETIME
240 ATRACE_INT(trace_str, 0);
241 #endif
242 }
243 }
244 return 0;
245 }
246