Home | History | Annotate | Download | only in memtrack
      1 /*
      2  * Copyright 2013 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 #ifndef __MEMTRACK_H__
     18 #define __MEMTRACK_H__
     19 
     20 #include <sys/types.h>
     21 
     22 #include <map>
     23 #include <string>
     24 #include <vector>
     25 
     26 #define DEFAULT_SLEEP_DELAY_SECONDS 5
     27 #ifndef NS_PER_SEC
     28 #define NS_PER_SEC 1000000000ULL
     29 #endif
     30 
     31 class FileData {
     32 public:
     33   FileData(char *filename, char *buffer, size_t buffer_len);
     34   ~FileData();
     35 
     36   // Get the PSS information from the file data. If there are no more
     37   // PSS values to be found, return false.
     38   bool getPss(size_t *pss);
     39 
     40   // Check if there is at least bytes available in the file data.
     41   bool isAvail(size_t bytes);
     42 
     43 private:
     44   int fd_;
     45   char *data_;
     46   size_t max_;
     47   size_t cur_idx_;
     48   size_t len_;
     49   bool read_complete_;
     50 };
     51 
     52 typedef struct {
     53   std::string name;
     54 
     55   size_t max_num_pids;
     56 
     57   size_t num_samples;
     58   double avg_pss_kb;
     59   size_t min_pss_kb;
     60   size_t max_pss_kb;
     61   size_t last_pss_kb;
     62 
     63   std::vector<int> pids;
     64 } process_info_t;
     65 typedef std::map<std::string, process_info_t> processes_t;
     66 
     67 typedef struct {
     68   size_t pss_kb;
     69 
     70   std::vector<int> pids;
     71 } cur_process_info_t;
     72 typedef std::map<std::string, cur_process_info_t> cur_processes_t;
     73 
     74 class ProcessInfo {
     75 public:
     76   ProcessInfo();
     77   ~ProcessInfo();
     78 
     79   // Get the information about a single process.
     80   bool getInformation(int pid, char *pid_str, size_t pid_str_len);
     81 
     82   // Scan all of the running processes.
     83   void scan();
     84 
     85   // Dump the information about all of the processes in the system to the log.
     86   void dumpToLog();
     87 
     88 private:
     89   static const size_t kBufferLen = 4096;
     90   static const size_t kCmdNameLen = 1024;
     91 
     92   static const char *kProc;
     93   static const size_t kProcLen = 6;
     94 
     95   static const char *kCmdline;
     96   static const size_t kCmdlineLen = 9;  // Includes \0 at end of string.
     97 
     98   static const char *kSmaps;
     99   static const size_t kSmapsLen = 7;  // Includes \0 at end of string.
    100 
    101   static const char *kStatus;
    102   static const size_t kStatusLen = 8;  // Includes \0 at end of string.
    103 
    104   static const size_t kInitialEntries = 1000;
    105 
    106   char proc_file_[PATH_MAX];
    107   char buffer_[kBufferLen];
    108 
    109   char cmd_name_[kCmdNameLen];
    110 
    111   // Minimize a need for a lot of allocations by keeping our maps and
    112   // lists in this object.
    113   processes_t all_;
    114   cur_processes_t cur_;
    115   std::vector<const process_info_t *> list_;
    116 
    117   // Compute a running average.
    118   static inline void computeAvg(double *running_avg, size_t cur_avg,
    119                                 size_t num_samples) {
    120     *running_avg = (*running_avg/(num_samples+1))*num_samples
    121                    + (double)cur_avg/(num_samples+1);
    122   }
    123 };
    124 
    125 #endif  // __MEMTRACK_H__
    126