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