1*288bf522SAndroid Build Coastguard Worker /* 2*288bf522SAndroid Build Coastguard Worker * Copyright 2013 The Android Open Source Project 3*288bf522SAndroid Build Coastguard Worker * 4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*288bf522SAndroid Build Coastguard Worker * 8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*288bf522SAndroid Build Coastguard Worker * 10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*288bf522SAndroid Build Coastguard Worker * limitations under the License. 15*288bf522SAndroid Build Coastguard Worker */ 16*288bf522SAndroid Build Coastguard Worker 17*288bf522SAndroid Build Coastguard Worker #ifndef __MEMTRACK_H__ 18*288bf522SAndroid Build Coastguard Worker #define __MEMTRACK_H__ 19*288bf522SAndroid Build Coastguard Worker 20*288bf522SAndroid Build Coastguard Worker #include <sys/types.h> 21*288bf522SAndroid Build Coastguard Worker 22*288bf522SAndroid Build Coastguard Worker #include <map> 23*288bf522SAndroid Build Coastguard Worker #include <string> 24*288bf522SAndroid Build Coastguard Worker #include <vector> 25*288bf522SAndroid Build Coastguard Worker 26*288bf522SAndroid Build Coastguard Worker #define DEFAULT_SLEEP_DELAY_SECONDS 5 27*288bf522SAndroid Build Coastguard Worker #ifndef NS_PER_SEC 28*288bf522SAndroid Build Coastguard Worker #define NS_PER_SEC 1000000000ULL 29*288bf522SAndroid Build Coastguard Worker #endif 30*288bf522SAndroid Build Coastguard Worker 31*288bf522SAndroid Build Coastguard Worker class FileData { 32*288bf522SAndroid Build Coastguard Worker public: 33*288bf522SAndroid Build Coastguard Worker FileData(char *filename, char *buffer, size_t buffer_len); 34*288bf522SAndroid Build Coastguard Worker ~FileData(); 35*288bf522SAndroid Build Coastguard Worker 36*288bf522SAndroid Build Coastguard Worker // Get the PSS information from the file data. If there are no more 37*288bf522SAndroid Build Coastguard Worker // PSS values to be found, return false. 38*288bf522SAndroid Build Coastguard Worker bool getPss(size_t *pss); 39*288bf522SAndroid Build Coastguard Worker 40*288bf522SAndroid Build Coastguard Worker // Check if there is at least bytes available in the file data. 41*288bf522SAndroid Build Coastguard Worker bool isAvail(size_t bytes); 42*288bf522SAndroid Build Coastguard Worker 43*288bf522SAndroid Build Coastguard Worker private: 44*288bf522SAndroid Build Coastguard Worker int fd_; 45*288bf522SAndroid Build Coastguard Worker char *data_; 46*288bf522SAndroid Build Coastguard Worker size_t max_; 47*288bf522SAndroid Build Coastguard Worker size_t cur_idx_; 48*288bf522SAndroid Build Coastguard Worker size_t len_; 49*288bf522SAndroid Build Coastguard Worker bool read_complete_; 50*288bf522SAndroid Build Coastguard Worker }; 51*288bf522SAndroid Build Coastguard Worker 52*288bf522SAndroid Build Coastguard Worker typedef struct { 53*288bf522SAndroid Build Coastguard Worker std::string name; 54*288bf522SAndroid Build Coastguard Worker 55*288bf522SAndroid Build Coastguard Worker size_t max_num_pids; 56*288bf522SAndroid Build Coastguard Worker 57*288bf522SAndroid Build Coastguard Worker size_t num_samples; 58*288bf522SAndroid Build Coastguard Worker double avg_pss_kb; 59*288bf522SAndroid Build Coastguard Worker size_t min_pss_kb; 60*288bf522SAndroid Build Coastguard Worker size_t max_pss_kb; 61*288bf522SAndroid Build Coastguard Worker size_t last_pss_kb; 62*288bf522SAndroid Build Coastguard Worker 63*288bf522SAndroid Build Coastguard Worker std::vector<int> pids; 64*288bf522SAndroid Build Coastguard Worker } process_info_t; 65*288bf522SAndroid Build Coastguard Worker typedef std::map<std::string, process_info_t> processes_t; 66*288bf522SAndroid Build Coastguard Worker 67*288bf522SAndroid Build Coastguard Worker typedef struct { 68*288bf522SAndroid Build Coastguard Worker size_t pss_kb; 69*288bf522SAndroid Build Coastguard Worker 70*288bf522SAndroid Build Coastguard Worker std::vector<int> pids; 71*288bf522SAndroid Build Coastguard Worker } cur_process_info_t; 72*288bf522SAndroid Build Coastguard Worker typedef std::map<std::string, cur_process_info_t> cur_processes_t; 73*288bf522SAndroid Build Coastguard Worker 74*288bf522SAndroid Build Coastguard Worker class ProcessInfo { 75*288bf522SAndroid Build Coastguard Worker public: 76*288bf522SAndroid Build Coastguard Worker ProcessInfo(); 77*288bf522SAndroid Build Coastguard Worker ~ProcessInfo(); 78*288bf522SAndroid Build Coastguard Worker 79*288bf522SAndroid Build Coastguard Worker // Get the information about a single process. 80*288bf522SAndroid Build Coastguard Worker bool getInformation(int pid, char *pid_str, size_t pid_str_len); 81*288bf522SAndroid Build Coastguard Worker 82*288bf522SAndroid Build Coastguard Worker // Scan all of the running processes. 83*288bf522SAndroid Build Coastguard Worker void scan(); 84*288bf522SAndroid Build Coastguard Worker 85*288bf522SAndroid Build Coastguard Worker // Dump the information about all of the processes in the system to the log. 86*288bf522SAndroid Build Coastguard Worker void dumpToLog(); 87*288bf522SAndroid Build Coastguard Worker 88*288bf522SAndroid Build Coastguard Worker private: 89*288bf522SAndroid Build Coastguard Worker static const size_t kBufferLen = 4096; 90*288bf522SAndroid Build Coastguard Worker static const size_t kCmdNameLen = 1024; 91*288bf522SAndroid Build Coastguard Worker 92*288bf522SAndroid Build Coastguard Worker static const char *kProc; 93*288bf522SAndroid Build Coastguard Worker static const size_t kProcLen = 6; 94*288bf522SAndroid Build Coastguard Worker 95*288bf522SAndroid Build Coastguard Worker static const char *kCmdline; 96*288bf522SAndroid Build Coastguard Worker static const size_t kCmdlineLen = 9; // Includes \0 at end of string. 97*288bf522SAndroid Build Coastguard Worker 98*288bf522SAndroid Build Coastguard Worker static const char *kSmaps; 99*288bf522SAndroid Build Coastguard Worker static const size_t kSmapsLen = 7; // Includes \0 at end of string. 100*288bf522SAndroid Build Coastguard Worker 101*288bf522SAndroid Build Coastguard Worker static const char *kStatus; 102*288bf522SAndroid Build Coastguard Worker static const size_t kStatusLen = 8; // Includes \0 at end of string. 103*288bf522SAndroid Build Coastguard Worker 104*288bf522SAndroid Build Coastguard Worker static const size_t kInitialEntries = 1000; 105*288bf522SAndroid Build Coastguard Worker 106*288bf522SAndroid Build Coastguard Worker char proc_file_[PATH_MAX]; 107*288bf522SAndroid Build Coastguard Worker char buffer_[kBufferLen]; 108*288bf522SAndroid Build Coastguard Worker 109*288bf522SAndroid Build Coastguard Worker char cmd_name_[kCmdNameLen]; 110*288bf522SAndroid Build Coastguard Worker 111*288bf522SAndroid Build Coastguard Worker // Minimize a need for a lot of allocations by keeping our maps and 112*288bf522SAndroid Build Coastguard Worker // lists in this object. 113*288bf522SAndroid Build Coastguard Worker processes_t all_; 114*288bf522SAndroid Build Coastguard Worker cur_processes_t cur_; 115*288bf522SAndroid Build Coastguard Worker std::vector<const process_info_t *> list_; 116*288bf522SAndroid Build Coastguard Worker 117*288bf522SAndroid Build Coastguard Worker // Compute a running average. computeAvg(double * running_avg,size_t cur_avg,size_t num_samples)118*288bf522SAndroid Build Coastguard Worker static inline void computeAvg(double *running_avg, size_t cur_avg, 119*288bf522SAndroid Build Coastguard Worker size_t num_samples) { 120*288bf522SAndroid Build Coastguard Worker *running_avg = (*running_avg/(num_samples+1))*num_samples 121*288bf522SAndroid Build Coastguard Worker + (double)cur_avg/(num_samples+1); 122*288bf522SAndroid Build Coastguard Worker } 123*288bf522SAndroid Build Coastguard Worker }; 124*288bf522SAndroid Build Coastguard Worker 125*288bf522SAndroid Build Coastguard Worker #endif // __MEMTRACK_H__ 126