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