1 /*
2 * Copyright (C) 2018 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 #define LOG_TAG "IonWatcher"
18
19 #include <android/log.h>
20 #include <stdio.h>
21 #include <unistd.h>
22
23 #include <fstream>
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #define ATRACE_TAG ATRACE_TAG_NNAPI
28 #include <utils/Trace.h>
29
parseMemInfo(const char * name)30 int parseMemInfo(const char* name) {
31 std::ifstream meminfoStream("/proc/meminfo");
32 if (!meminfoStream.good()) {
33 perror("Failed to open /proc/meminfo");
34 return -1;
35 }
36 std::string line;
37 while (std::getline(meminfoStream, line)) {
38 if (line.find(name) != std::string::npos) {
39 std::istringstream lineStream(line);
40 std::string name;
41 int size;
42 lineStream >> name;
43 lineStream >> size;
44 return size;
45 }
46 }
47 std::cerr << "Failed to find " << name << " in /proc/meminfo\n";
48 return -1;
49 }
50
main(void)51 int main(void) {
52 if (!(atrace_get_enabled_tags() & ATRACE_TAG)) {
53 std::cerr << "systrace not running, logcat output only\n";
54 }
55 int size = 0;
56 while (true) {
57 const int newSize = parseMemInfo("ION_heap");
58 if (newSize < 0) {
59 return newSize;
60 }
61 if (newSize != size) {
62 size = newSize;
63 std::cout << size << "\n";
64 ATRACE_INT("ION_heap", size);
65 __android_log_print(ANDROID_LOG_INFO, "ion", "ION_heap %d", size);
66 }
67 usleep(10);
68 }
69 }
70