1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include <algorithm>
19 #include <cstring>
20 #include <fstream>
21 #include <iostream>
22 #include <jni.h>
23 #include <numeric>
24 #include <sstream>
25 #include <string>
26 #include <tuple>
27 #include <unistd.h>
28 
29 #include <android/log.h>
30 #define LOG(...) __android_log_write(ANDROID_LOG_INFO, "ALLOC-STRESS", __VA_ARGS__)
31 
32 using namespace std;
33 
34 size_t s = 4 * (1 << 20); // 4 MB
35 void *gptr;
36 extern "C"
37 JNIEXPORT void JNICALL
Java_com_android_server_cts_device_statsd_StatsdCtsBackgroundService_cmain(JNIEnv *,jobject)38 Java_com_android_server_cts_device_statsd_StatsdCtsBackgroundService_cmain(JNIEnv* , jobject /* this */)
39 {
40     long long allocCount = 0;
41     while (1) {
42         char *ptr = (char *)malloc(s);
43         memset(ptr, (int)allocCount >> 10, s);
44         for (int i = 0; i < s; i += 4096) {
45             *((long long *)&ptr[i]) = allocCount + i;
46         }
47         std::stringstream ss;
48         ss << "total alloc: " << allocCount / (1 << 20);
49         LOG(ss.str().c_str());
50         gptr = ptr;
51         allocCount += s;
52     }
53 }
54