• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <sys/types.h>
20 
21 #include <string>
22 #include <vector>
23 
24 #include "meminfo.h"
25 
26 namespace android {
27 namespace meminfo {
28 
29 using VmaCallback = std::function<void(const Vma&)>;
30 
31 class ProcMemInfo final {
32     // Per-process memory accounting
33   public:
34     // Reset the working set accounting of the process via /proc/<pid>/clear_refs
35     static bool ResetWorkingSet(pid_t pid);
36 
37     ProcMemInfo(pid_t pid, bool get_wss = false, uint64_t pgflags = 0, uint64_t pgflags_mask = 0);
38 
39     const std::vector<Vma>& Maps();
40     const MemUsage& Usage();
41     const MemUsage& Wss();
42 
43     // Same as Maps() except, only valid for reading working set using CONFIG_IDLE_PAGE_TRACKING
44     // support in kernel. If the kernel support doesn't exist, the function will return an empty
45     // vector.
46     const std::vector<Vma>& MapsWithPageIdle();
47 
48     // Collect all 'vma' or 'maps' from /proc/<pid>/smaps and store them in 'maps_'. Returns a
49     // constant reference to the vma vector after the collection is done.
50     //
51     // Each 'struct Vma' is *fully* populated by this method (unlike SmapsOrRollup).
52     const std::vector<Vma>& Smaps(const std::string& path = "");
53 
54     // This method reads /proc/<pid>/smaps and calls the callback() for each
55     // vma or map that it finds. The map is converted to 'struct Vma' object which is then
56     // passed to the callback.
57     // Returns 'false' if the file is malformed.
58     bool ForEachVma(const VmaCallback& callback);
59 
60     // Used to parse either of /proc/<pid>/{smaps, smaps_rollup} and record the process's
61     // Pss and Private memory usage in 'stats'.  In particular, the method only populates the fields
62     // of the MemUsage structure that are intended to be used by Android's periodic Pss collection.
63     //
64     // The method populates the following statistics in order to be fast an efficient.
65     //   Pss
66     //   Rss
67     //   Uss
68     //   private_clean
69     //   private_dirty
70     //   SwapPss
71     // All other fields of MemUsage are zeroed.
72     bool SmapsOrRollup(MemUsage* stats) const;
73 
74     // Used to parse either of /proc/<pid>/{smaps, smaps_rollup} and record the process's
75     // Pss.
76     // Returns 'true' on success and the value of Pss in the out parameter.
77     bool SmapsOrRollupPss(uint64_t* pss) const;
78 
79     const std::vector<uint16_t>& SwapOffsets();
80 
81     // Reads /proc/<pid>/pagemap for this process for each page within
82     // the 'vma' and stores that in 'pagemap'. It is assumed that the 'vma'
83     // is obtained by calling Maps() or 'ForEachVma' for the same object. No special checks
84     // are made to see if 'vma' is *valid*.
85     // Returns false if anything goes wrong, 'true' otherwise.
86     bool PageMap(const Vma& vma, std::vector<uint64_t>* pagemap);
87 
88     ~ProcMemInfo() = default;
89 
90   private:
91     bool ReadMaps(bool get_wss, bool use_pageidle = false);
92     bool ReadVmaStats(int pagemap_fd, Vma& vma, bool get_wss, bool use_pageidle);
93 
94     pid_t pid_;
95     bool get_wss_;
96     uint64_t pgflags_;
97     uint64_t pgflags_mask_;
98 
99     std::vector<Vma> maps_;
100 
101     MemUsage usage_;
102     std::vector<uint16_t> swap_offsets_;
103 };
104 
105 // Makes callback for each 'vma' or 'map' found in file provided. The file is expected to be in the
106 // same format as /proc/<pid>/smaps. Returns 'false' if the file is malformed.
107 bool ForEachVmaFromFile(const std::string& path, const VmaCallback& callback);
108 
109 // Returns if the kernel supports /proc/<pid>/smaps_rollup. Assumes that the
110 // calling process has access to the /proc/<pid>/smaps_rollup.
111 // Returns 'false' if the calling process has no permission to read the file if it exists
112 // of if the file doesn't exist.
113 bool IsSmapsRollupSupported(pid_t pid);
114 
115 // Same as ProcMemInfo::SmapsOrRollup but reads the statistics directly
116 // from a file. The file MUST be in the same format as /proc/<pid>/smaps
117 // or /proc/<pid>/smaps_rollup
118 bool SmapsOrRollupFromFile(const std::string& path, MemUsage* stats);
119 
120 // Same as ProcMemInfo::SmapsOrRollupPss but reads the statistics directly
121 // from a file and returns total Pss in kB. The file MUST be in the same format
122 // as /proc/<pid>/smaps or /proc/<pid>/smaps_rollup
123 bool SmapsOrRollupPssFromFile(const std::string& path, uint64_t* pss);
124 
125 }  // namespace meminfo
126 }  // namespace android
127