4 #include <cutils/sockets.h>
5 #include <hardware/gralloc.h>
19 #define ASSERT_TRUE(cond) \
22 cerr << __func__ << "( " << getpid() << "):" << __LINE__ << " condition:" << #cond << " failed\n" << endl; \
30 Pipe(const Pipe &) = delete;
31 Pipe& operator=(const Pipe &) = delete;
32 Pipe& operator=(const Pipe &&) = delete;
34 Pipe(int readFd, int writeFd) : m_readFd{readFd}, m_writeFd{writeFd} {
35 fcntl(m_readFd, F_SETFD, FD_CLOEXEC);
36 fcntl(m_writeFd, F_SETFD, FD_CLOEXEC);
38 Pipe(Pipe&& rval) noexcept {
39 m_readFd = rval.m_readFd;
40 m_writeFd = rval.m_writeFd;
50 void preserveOverFork(bool preserve) {
52 fcntl(m_readFd, F_SETFD, 0);
53 fcntl(m_writeFd, F_SETFD,0);
55 fcntl(m_readFd, F_SETFD, FD_CLOEXEC);
56 fcntl(m_writeFd, F_SETFD, FD_CLOEXEC);
67 int error = write(m_writeFd, &val, sizeof(val));
68 ASSERT_TRUE(error == sizeof(val));
72 int error = read(m_readFd, &val, sizeof(val));
73 ASSERT_TRUE(error == sizeof(val));
75 bool wait_ret_error() {
77 int error = read(m_readFd, &val, sizeof(val));
80 template <typename T> void send(const T& v) {
81 int error = write(m_writeFd, &v, sizeof(T));
82 ASSERT_TRUE(error >= 0);
84 template <typename T> void recv(T& v) {
85 int error = read(m_readFd, &v, sizeof(T));
86 ASSERT_TRUE(error >= 0);
88 static Pipe makePipeFromFds(int readFd, int writeFd) {
89 return Pipe(readFd, writeFd);
91 static tuple<Pipe, Pipe> createPipePair() {
97 ASSERT_TRUE(error1 >= 0);
98 ASSERT_TRUE(error2 >= 0);
100 return make_tuple(Pipe(a[0], b[1]), Pipe(b[0], a[1]));
104 void createProcess(Pipe pipe, const char *exName, const char *arg)
106 pipe.preserveOverFork(true);
112 snprintf(readFdStr, sizeof(readFdStr), "%d", pipe.getReadFd());
113 snprintf(writeFdStr, sizeof(writeFdStr), "%d", pipe.getWriteFd());
114 execl(exName, exName, "--worker", arg, readFdStr, writeFdStr, 0);
119 pipe.preserveOverFork(false);
128 static void write_oomadj_to_lmkd(int oomadj) {
129 // Connect to lmkd and store our oom_adj
130 int lmk_procprio_cmd[4];
133 while ((sock = socket_local_client("lmkd",
134 ANDROID_SOCKET_NAMESPACE_RESERVED,
135 SOCK_SEQPACKET)) < 0) {
137 if (tries-- < 0) break;
140 cout << "Failed to connect to lmkd, errno " << errno << endl;
143 lmk_procprio_cmd[0] = htonl(1);
144 lmk_procprio_cmd[1] = htonl(getpid());
145 lmk_procprio_cmd[2] = htonl(getuid());
146 lmk_procprio_cmd[3] = htonl(oomadj);
148 int written = write(sock, lmk_procprio_cmd, sizeof(lmk_procprio_cmd));
149 cout << "Wrote " << written << " bytes to lmkd control socket." << endl;
152 #ifdef ENABLE_MEM_CGROUPS
153 static void create_memcg() {
155 pid_t pid = getpid();
156 snprintf(buf, sizeof(buf), "/dev/memctl/apps/%u", pid);
158 int tasks = mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
160 cout << "Failed to create memory cgroup" << endl;
163 snprintf(buf, sizeof(buf), "/dev/memctl/apps/%u/tasks", pid);
164 tasks = open(buf, O_WRONLY);
166 cout << "Unable to add process to memory cgroup" << endl;
169 snprintf(buf, sizeof(buf), "%u", pid);
170 write(tasks, buf, strlen(buf));
175 size_t s = 4 * (1 << 20);
177 int main(int argc, char *argv[])
179 if ((argc > 1) && (std::string(argv[1]) == "--worker")) {
180 #ifdef ENABLE_MEM_CGROUPS
183 write_oomadj_to_lmkd(atoi(argv[2]));
184 Pipe p{atoi(argv[3]), atoi(argv[4])};
186 long long allocCount = 0;
189 char *ptr = (char*)malloc(s);
190 memset(ptr, (int)allocCount >> 10, s);
191 for (int i = 0; i < s; i+= 4096) {
192 *((long long*)&ptr[i]) = allocCount + i;
196 //cout << "total alloc: " << allocCount / (1<<20)<< " adj: " << argv[2]<< endl;;
197 //cout << "ptr: " << (long long)(void*)ptr << endl;;
202 cout << "parent:" << argc << endl;
204 write_oomadj_to_lmkd(-1000);
205 for (int i = 1000; i >= 0; i -= 100) {
206 auto pipes = Pipe::createPipePair();
208 snprintf(arg, sizeof(arg), "%d", i);
209 createProcess(std::move(std::get<1>(pipes)), argv[0], arg);
210 Pipe &p = std::get<0>(pipes);
214 //;cout << getpid() << ":" << "parent signal" << endl;
216 if (p.wait_ret_error()) {
218 waitpid(0, &status, 0);
223 cout << "adj: " << i << " sz: " << t / (1 << 20) << endl;