• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef MEMORY_DATA_PLUGIN_H
17 #define MEMORY_DATA_PLUGIN_H
18 
19 #include <algorithm>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <iomanip>
24 #include <sstream>
25 #include <string>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <unordered_map>
30 #include <utility>
31 #include <v1_0/imemory_tracker_interface.h>
32 
33 #include "buffer_splitter.h"
34 #include "logging.h"
35 #include "memory_plugin_config.pb.h"
36 #include "memory_plugin_result.pb.h"
37 #include "plugin_module_api.h"
38 #include "smaps_stats.h"
39 
40 struct Proto2StrMapping {
41     int protobufid;
42     const char* procstr;
43 };
44 
45 constexpr Proto2StrMapping meminfoMapping[] = {
46     {SysMeminfoType::PMEM_UNSPECIFIED, "MemUnspecified"},
47     {SysMeminfoType::PMEM_MEM_TOTAL, "MemTotal"},
48     {SysMeminfoType::PMEM_MEM_FREE, "MemFree"},
49     {SysMeminfoType::PMEM_MEM_AVAILABLE, "MemAvailable"},
50     {SysMeminfoType::PMEM_BUFFERS, "Buffers"},
51     {SysMeminfoType::PMEM_CACHED, "Cached"},
52     {SysMeminfoType::PMEM_SWAP_CACHED, "SwapCached"},
53     {SysMeminfoType::PMEM_ACTIVE, "Active"},
54     {SysMeminfoType::PMEM_INACTIVE, "Inactive"},
55     {SysMeminfoType::PMEM_ACTIVE_ANON, "Active(anon)"},
56     {SysMeminfoType::PMEM_INACTIVE_ANON, "Inactive(anon)"},
57     {SysMeminfoType::PMEM_ACTIVE_FILE, "Active(file)"},
58     {SysMeminfoType::PMEM_INACTIVE_FILE, "Inactive(file)"},
59     {SysMeminfoType::PMEM_ACTIVE_PURG, "Active(purg)"},
60     {SysMeminfoType::PMEM_INACTIVE_PURG, "Inactive(purg)"},
61     {SysMeminfoType::PMEM_PINED_PURG, "Pined(purg)"},
62     {SysMeminfoType::PMEM_UNEVICTABLE, "Unevictable"},
63     {SysMeminfoType::PMEM_MLOCKED, "Mlocked"},
64     {SysMeminfoType::PMEM_SWAP_TOTAL, "SwapTotal"},
65     {SysMeminfoType::PMEM_SWAP_FREE, "SwapFree"},
66     {SysMeminfoType::PMEM_DIRTY, "Dirty"},
67     {SysMeminfoType::PMEM_WRITEBACK, "Writeback"},
68     {SysMeminfoType::PMEM_ANON_PAGES, "AnonPages"},
69     {SysMeminfoType::PMEM_MAPPED, "Mapped"},
70     {SysMeminfoType::PMEM_SHMEM, "Shmem"},
71     {SysMeminfoType::PMEM_SLAB, "Slab"},
72     {SysMeminfoType::PMEM_SLAB_RECLAIMABLE, "SReclaimable"},
73     {SysMeminfoType::PMEM_SLAB_UNRECLAIMABLE, "SUnreclaim"},
74     {SysMeminfoType::PMEM_KERNEL_STACK, "KernelStack"},
75     {SysMeminfoType::PMEM_PAGE_TABLES, "PageTables"},
76     {SysMeminfoType::PMEM_COMMIT_LIMIT, "CommitLimit"},
77     {SysMeminfoType::PMEM_COMMITED_AS, "Committed_AS"},
78     {SysMeminfoType::PMEM_VMALLOC_TOTAL, "VmallocTotal"},
79     {SysMeminfoType::PMEM_VMALLOC_USED, "VmallocUsed"},
80     {SysMeminfoType::PMEM_VMALLOC_CHUNK, "VmallocChunk"},
81     {SysMeminfoType::PMEM_CMA_TOTAL, "CmaTotal"},
82     {SysMeminfoType::PMEM_CMA_FREE, "CmaFree"},
83     {SysMeminfoType::PMEM_KERNEL_RECLAIMABLE, "KReclaimable"},
84 };
85 
86 constexpr Proto2StrMapping vmeminfoMapping[] = {
87     {SysVMeminfoType::VMEMINFO_UNSPECIFIED, "unspecified"},
88     {SysVMeminfoType::VMEMINFO_NR_FREE_PAGES, "nr_free_pages"},
89     {SysVMeminfoType::VMEMINFO_NR_ALLOC_BATCH, "nr_alloc_batch"},
90     {SysVMeminfoType::VMEMINFO_NR_INACTIVE_ANON, "nr_inactive_anon"},
91     {SysVMeminfoType::VMEMINFO_NR_ACTIVE_ANON, "nr_active_anon"},
92     {SysVMeminfoType::VMEMINFO_NR_INACTIVE_FILE, "nr_inactive_file"},
93     {SysVMeminfoType::VMEMINFO_NR_ACTIVE_FILE, "nr_active_file"},
94     {SysVMeminfoType::VMEMINFO_NR_UNEVICTABLE, "nr_unevictable"},
95     {SysVMeminfoType::VMEMINFO_NR_MLOCK, "nr_mlock"},
96     {SysVMeminfoType::VMEMINFO_NR_ANON_PAGES, "nr_anon_pages"},
97     {SysVMeminfoType::VMEMINFO_NR_MAPPED, "nr_mapped"},
98     {SysVMeminfoType::VMEMINFO_NR_FILE_PAGES, "nr_file_pages"},
99     {SysVMeminfoType::VMEMINFO_NR_DIRTY, "nr_dirty"},
100     {SysVMeminfoType::VMEMINFO_NR_WRITEBACK, "nr_writeback"},
101     {SysVMeminfoType::VMEMINFO_NR_SLAB_RECLAIMABLE, "nr_slab_reclaimable"},
102     {SysVMeminfoType::VMEMINFO_NR_SLAB_UNRECLAIMABLE, "nr_slab_unreclaimable"},
103     {SysVMeminfoType::VMEMINFO_NR_PAGE_TABLE_PAGES, "nr_page_table_pages"},
104     {SysVMeminfoType::VMEMINFO_NR_KERNEL_STACK, "nr_kernel_stack"},
105     {SysVMeminfoType::VMEMINFO_NR_OVERHEAD, "nr_overhead"},
106     {SysVMeminfoType::VMEMINFO_NR_UNSTABLE, "nr_unstable"},
107     {SysVMeminfoType::VMEMINFO_NR_BOUNCE, "nr_bounce"},
108     {SysVMeminfoType::VMEMINFO_NR_VMSCAN_WRITE, "nr_vmscan_write"},
109     {SysVMeminfoType::VMEMINFO_NR_VMSCAN_IMMEDIATE_RECLAIM, "nr_vmscan_immediate_reclaim"},
110     {SysVMeminfoType::VMEMINFO_NR_WRITEBACK_TEMP, "nr_writeback_temp"},
111     {SysVMeminfoType::VMEMINFO_NR_ISOLATED_ANON, "nr_isolated_anon"},
112     {SysVMeminfoType::VMEMINFO_NR_ISOLATED_FILE, "nr_isolated_file"},
113     {SysVMeminfoType::VMEMINFO_NR_SHMEM, "nr_shmem"},
114     {SysVMeminfoType::VMEMINFO_NR_DIRTIED, "nr_dirtied"},
115     {SysVMeminfoType::VMEMINFO_NR_WRITTEN, "nr_written"},
116     {SysVMeminfoType::VMEMINFO_NR_PAGES_SCANNED, "nr_pages_scanned"},
117     {SysVMeminfoType::VMEMINFO_WORKINGSET_REFAULT, "workingset_refault"},
118     {SysVMeminfoType::VMEMINFO_WORKINGSET_ACTIVATE, "workingset_activate"},
119     {SysVMeminfoType::VMEMINFO_WORKINGSET_NODERECLAIM, "workingset_nodereclaim"},
120     {SysVMeminfoType::VMEMINFO_NR_ANON_TRANSPARENT_HUGEPAGES, "nr_anon_transparent_hugepages"},
121     {SysVMeminfoType::VMEMINFO_NR_FREE_CMA, "nr_free_cma"},
122     {SysVMeminfoType::VMEMINFO_NR_SWAPCACHE, "nr_swapcache"},
123     {SysVMeminfoType::VMEMINFO_NR_DIRTY_THRESHOLD, "nr_dirty_threshold"},
124     {SysVMeminfoType::VMEMINFO_NR_DIRTY_BACKGROUND_THRESHOLD, "nr_dirty_background_threshold"},
125     {SysVMeminfoType::VMEMINFO_PGPGIN, "pgpgin"},
126     {SysVMeminfoType::VMEMINFO_PGPGOUT, "pgpgout"},
127     {SysVMeminfoType::VMEMINFO_PGPGOUTCLEAN, "pgpgoutclean"},
128     {SysVMeminfoType::VMEMINFO_PSWPIN, "pswpin"},
129     {SysVMeminfoType::VMEMINFO_PSWPOUT, "pswpou"},
130     {SysVMeminfoType::VMEMINFO_PGALLOC_DMA, "pgalloc_dma"},
131     {SysVMeminfoType::VMEMINFO_PGALLOC_NORMAL, "pgalloc_normal"},
132     {SysVMeminfoType::VMEMINFO_PGALLOC_MOVABLE, "pgalloc_movable"},
133     {SysVMeminfoType::VMEMINFO_PGFREE, "pgfree"},
134     {SysVMeminfoType::VMEMINFO_PGACTIVATE, "pgactivate"},
135     {SysVMeminfoType::VMEMINFO_PGDEACTIVATE, "pgdeactivate"},
136     {SysVMeminfoType::VMEMINFO_PGFAULT, "pgfault"},
137     {SysVMeminfoType::VMEMINFO_PGMAJFAULT, "pgmajfault"},
138     {SysVMeminfoType::VMEMINFO_PGREFILL_DMA, "pgrefill_dma"},
139     {SysVMeminfoType::VMEMINFO_PGREFILL_NORMAL, "pgrefill_normal"},
140     {SysVMeminfoType::VMEMINFO_PGREFILL_MOVABLE, "pgrefill_movable"},
141     {SysVMeminfoType::VMEMINFO_PGSTEAL_KSWAPD_DMA, "pgsteal_kswapd_dma"},
142     {SysVMeminfoType::VMEMINFO_PGSTEAL_KSWAPD_NORMAL, "pgsteal_kswapd_normal"},
143     {SysVMeminfoType::VMEMINFO_PGSTEAL_KSWAPD_MOVABLE, "pgsteal_kswapd_movable"},
144     {SysVMeminfoType::VMEMINFO_PGSTEAL_DIRECT_DMA, "pgsteal_direct_dma"},
145     {SysVMeminfoType::VMEMINFO_PGSTEAL_DIRECT_NORMAL, "pgsteal_direct_normal"},
146     {SysVMeminfoType::VMEMINFO_PGSTEAL_DIRECT_MOVABLE, "pgsteal_direct_movable"},
147     {SysVMeminfoType::VMEMINFO_PGSCAN_KSWAPD_DMA, "pgscan_kswapd_dma"},
148     {SysVMeminfoType::VMEMINFO_PGSCAN_KSWAPD_NORMAL, "pgscan_kswapd_normal"},
149     {SysVMeminfoType::VMEMINFO_PGSCAN_KSWAPD_MOVABLE, "pgscan_kswapd_movable"},
150     {SysVMeminfoType::VMEMINFO_PGSCAN_DIRECT_DMA, "pgscan_direct_dma"},
151     {SysVMeminfoType::VMEMINFO_PGSCAN_DIRECT_NORMAL, "pgscan_direct_normal"},
152     {SysVMeminfoType::VMEMINFO_PGSCAN_DIRECT_MOVABLE, "pgscan_direct_movable"},
153     {SysVMeminfoType::VMEMINFO_PGSCAN_DIRECT_THROTTLE, "pgscan_direct_throttle"},
154     {SysVMeminfoType::VMEMINFO_PGINODESTEAL, "pginodesteal"},
155     {SysVMeminfoType::VMEMINFO_SLABS_SCANNED, "slabs_scanned"},
156     {SysVMeminfoType::VMEMINFO_KSWAPD_INODESTEAL, "kswapd_inodesteal"},
157     {SysVMeminfoType::VMEMINFO_KSWAPD_LOW_WMARK_HIT_QUICKLY, "kswapd_low_wmark_hit_quickly"},
158     {SysVMeminfoType::VMEMINFO_KSWAPD_HIGH_WMARK_HIT_QUICKLY, "kswapd_high_wmark_hit_quickly"},
159     {SysVMeminfoType::VMEMINFO_PAGEOUTRUN, "pageoutrun"},
160     {SysVMeminfoType::VMEMINFO_ALLOCSTALL, "allocstall"},
161     {SysVMeminfoType::VMEMINFO_PGROTATED, "pgrotated"},
162     {SysVMeminfoType::VMEMINFO_DROP_PAGECACHE, "drop_pagecache"},
163     {SysVMeminfoType::VMEMINFO_DROP_SLAB, "drop_slab"},
164     {SysVMeminfoType::VMEMINFO_PGMIGRATE_SUCCESS, "pgmigrate_success"},
165     {SysVMeminfoType::VMEMINFO_PGMIGRATE_FAIL, "pgmigrate_fail"},
166     {SysVMeminfoType::VMEMINFO_COMPACT_MIGRATE_SCANNED, "compact_migrate_scanned"},
167     {SysVMeminfoType::VMEMINFO_COMPACT_FREE_SCANNED, "compact_free_scanned"},
168     {SysVMeminfoType::VMEMINFO_COMPACT_ISOLATED, "compact_isolated"},
169     {SysVMeminfoType::VMEMINFO_COMPACT_STALL, "compact_stall"},
170     {SysVMeminfoType::VMEMINFO_COMPACT_FAIL, "compact_fail"},
171     {SysVMeminfoType::VMEMINFO_COMPACT_SUCCESS, "compact_success"},
172     {SysVMeminfoType::VMEMINFO_COMPACT_DAEMON_WAKE, "compact_daemon_wake"},
173     {SysVMeminfoType::VMEMINFO_UNEVICTABLE_PGS_CULLED, "unevictable_pgs_culled"},
174     {SysVMeminfoType::VMEMINFO_UNEVICTABLE_PGS_SCANNED, "unevictable_pgs_scanned"},
175     {SysVMeminfoType::VMEMINFO_UNEVICTABLE_PGS_RESCUED, "unevictable_pgs_rescued"},
176     {SysVMeminfoType::VMEMINFO_UNEVICTABLE_PGS_MLOCKED, "unevictable_pgs_mlocked"},
177     {SysVMeminfoType::VMEMINFO_UNEVICTABLE_PGS_MUNLOCKED, "unevictable_pgs_munlocked"},
178     {SysVMeminfoType::VMEMINFO_UNEVICTABLE_PGS_CLEARED, "unevictable_pgs_cleared"},
179     {SysVMeminfoType::VMEMINFO_UNEVICTABLE_PGS_STRANDED, "unevictable_pgs_stranded"},
180     {SysVMeminfoType::VMEMINFO_NR_ZSPAGES, "nr_zspages"},
181     {SysVMeminfoType::VMEMINFO_NR_ION_HEAP, "nr_ion_heap"},
182     {SysVMeminfoType::VMEMINFO_NR_GPU_HEAP, "nr_gpu_heap"},
183     {SysVMeminfoType::VMEMINFO_ALLOCSTALL_DMA, "allocstall_dma"},
184     {SysVMeminfoType::VMEMINFO_ALLOCSTALL_MOVABLE, "allocstall_movable"},
185     {SysVMeminfoType::VMEMINFO_ALLOCSTALL_NORMAL, "allocstall_normal"},
186     {SysVMeminfoType::VMEMINFO_COMPACT_DAEMON_FREE_SCANNED, "compact_daemon_free_scanned"},
187     {SysVMeminfoType::VMEMINFO_COMPACT_DAEMON_MIGRATE_SCANNED, "compact_daemon_migrate_scanned"},
188     {SysVMeminfoType::VMEMINFO_NR_FASTRPC, "nr_fastrpc"},
189     {SysVMeminfoType::VMEMINFO_NR_INDIRECTLY_RECLAIMABLE, "nr_indirectly_reclaimable"},
190     {SysVMeminfoType::VMEMINFO_NR_ION_HEAP_POOL, "nr_ion_heap_pool"},
191     {SysVMeminfoType::VMEMINFO_NR_KERNEL_MISC_RECLAIMABLE, "nr_kernel_misc_reclaimable"},
192     {SysVMeminfoType::VMEMINFO_NR_SHADOW_CALL_STACK_BYTES, "nr_shadow_call_stack_bytes"},
193     {SysVMeminfoType::VMEMINFO_NR_SHMEM_HUGEPAGES, "nr_shmem_hugepages"},
194     {SysVMeminfoType::VMEMINFO_NR_SHMEM_PMDMAPPED, "nr_shmem_pmdmapped"},
195     {SysVMeminfoType::VMEMINFO_NR_UNRECLAIMABLE_PAGES, "nr_unreclaimable_pages"},
196     {SysVMeminfoType::VMEMINFO_NR_ZONE_ACTIVE_ANON, "nr_zone_active_anon"},
197     {SysVMeminfoType::VMEMINFO_NR_ZONE_ACTIVE_FILE, "nr_zone_active_file"},
198     {SysVMeminfoType::VMEMINFO_NR_ZONE_INACTIVE_ANON, "nr_zone_inactive_anon"},
199     {SysVMeminfoType::VMEMINFO_NR_ZONE_INACTIVE_FILE, "nr_zone_inactive_file"},
200     {SysVMeminfoType::VMEMINFO_NR_ZONE_UNEVICTABLE, "nr_zone_unevictable"},
201     {SysVMeminfoType::VMEMINFO_NR_ZONE_WRITE_PENDING, "nr_zone_write_pending"},
202     {SysVMeminfoType::VMEMINFO_OOM_KILL, "oom_kill"},
203     {SysVMeminfoType::VMEMINFO_PGLAZYFREE, "pglazyfree"},
204     {SysVMeminfoType::VMEMINFO_PGLAZYFREED, "pglazyfreed"},
205     {SysVMeminfoType::VMEMINFO_PGREFILL, "pgrefill"},
206     {SysVMeminfoType::VMEMINFO_PGSCAN_DIRECT, "pgscan_direct"},
207     {SysVMeminfoType::VMEMINFO_PGSCAN_KSWAPD, "pgscan_kswapd"},
208     {SysVMeminfoType::VMEMINFO_PGSKIP_DMA, "pgskip_dma"},
209     {SysVMeminfoType::VMEMINFO_PGSKIP_MOVABLE, "pgskip_movable"},
210     {SysVMeminfoType::VMEMINFO_PGSKIP_NORMAL, "pgskip_normal"},
211     {SysVMeminfoType::VMEMINFO_PGSTEAL_DIRECT, "pgsteal_direct"},
212     {SysVMeminfoType::VMEMINFO_PGSTEAL_KSWAPD, "pgsteal_kswapd"},
213     {SysVMeminfoType::VMEMINFO_SWAP_RA, "swap_ra"},
214     {SysVMeminfoType::VMEMINFO_SWAP_RA_HIT, "swap_ra_hit"},
215     {SysVMeminfoType::VMEMINFO_WORKINGSET_RESTORE, "workingset_restore"},
216 };
217 
218 struct ProcStatusMapping {
219     int procid;
220     const char* procstr;
221 };
222 
223 enum StatusType {
224     PRO_TGID = 1,
225     PRO_NAME,
226     PRO_VMSIZE,
227     PRO_VMRSS,
228     PRO_RSSANON,
229     PRO_RSSFILE,
230     PRO_RSSSHMEM,
231     PRO_VMSWAP,
232     PRO_VMLCK,
233     PRO_VMHWM,
234     PRO_PURGSUM,
235     PRO_PURGPIN,
236 };
237 
238 constexpr ProcStatusMapping procStatusMapping[] = {
239     {StatusType::PRO_TGID, "Tgid"},         {StatusType::PRO_NAME, "Name"},       {StatusType::PRO_VMSIZE, "VmSize"},
240     {StatusType::PRO_VMRSS, "VmRSS"},       {StatusType::PRO_RSSANON, "RssAnon"}, {StatusType::PRO_RSSFILE, "RssFile"},
241     {StatusType::PRO_RSSSHMEM, "RssShmem"}, {StatusType::PRO_VMSWAP, "VmSwap"},   {StatusType::PRO_VMLCK, "VmLck"},
242     {StatusType::PRO_VMHWM, "VmHWM"},       {StatusType::PRO_PURGSUM, "PurgSum"},
243     {StatusType::PRO_PURGPIN, "PurgPin"},
244 };
245 
246 enum ErrorType {
247     RET_NULL_ADDR,
248     RET_IVALID_PID,
249     RET_TGID_VALUE_NULL,
250     RET_FAIL = -1,
251     RET_SUCC = 0,
252 };
253 
254 enum FileType {
255     FILE_STATUS = 0,
256     FILE_OOM,
257     FILE_SMAPS,
258 };
259 
260 struct ProcfdMapping {
261     int procid;
262     const char* file;
263 };
264 
265 constexpr ProcfdMapping procfdMapping[] = {
266     {FileType::FILE_STATUS, "status"},
267     {FileType::FILE_OOM, "oom_score_adj"},
268     {FileType::FILE_SMAPS, "smaps"},
269 };
270 
271 enum AshmemType {
272     ASHMEM_PROCESS_NAME = 1,
273     ASHMEM_PID,
274     ASHMEM_FD,
275     ASHMEM_ADJ,
276     ASHMEM_NAME,
277     ASHMEM_SIZE,
278     ASHMEM_ID,
279     ASHMEM_TIME,
280     ASHMEM_REF_COUNT,
281     ASHMEM_PURGED,
282 };
283 
284 enum DmaType {
285     DMA_NAME = 1,
286     DMA_PID,
287     DMA_FD,
288     DMA_SIZE,
289     DMA_INO,
290     DMA_EXP_PID,
291     DMA_EXP_TASK_COMM,
292     DMA_BUF_NAME,
293     DMA_EXP_NAME,
294 };
295 
296 enum GpuMemoryType {
297     GPU_NAME = 1,
298     GPU_SIZE,
299 };
300 
301 enum GpuCtxType {
302     GPU_ADDR = 1,
303     GPU_TID,
304     GPU_PID,
305     GPU_USED_SIZE,
306 };
307 
308 enum RSImageDumpType {
309     RS_SIZE = 1,
310     RS_TYPE,
311     RS_PID,
312     RS_SURFACENAME = 5,
313 };
314 
315 enum WinMgrSvcType {
316     WINDOW_NAME = 1,
317     WINDOW_PID = 3,
318 };
319 
320 struct GraphicsMemory {
321     uint64_t gl = 0;
322     uint64_t graph = 0;
323 };
324 
325 const std::vector<std::pair<OHOS::HDI::Memorytracker::V1_0::MemoryTrackerType, std::string>> MEMORY_TRACKER_TYPES = {
326     {OHOS::HDI::Memorytracker::V1_0::MEMORY_TRACKER_TYPE_GL, "GL"},
327     {OHOS::HDI::Memorytracker::V1_0::MEMORY_TRACKER_TYPE_GRAPH, "Graph"},
328     {OHOS::HDI::Memorytracker::V1_0::MEMORY_TRACKER_TYPE_OTHER, "Other"}
329 };
330 
331 class MemoryDataPlugin {
332 public:
333     MemoryDataPlugin();
334     ~MemoryDataPlugin();
335     int Start(const uint8_t* configData, uint32_t configSize);
336     int Report(uint8_t* data, uint32_t dataSize);
337     int ReportOptimize(RandomWriteCtx* randomWrite);
338     int Stop();
SetPath(char * path)339     void SetPath(char* path)
340     {
341         testpath_ = path;
342     };
343 
344     // for UT
SetProtoConfig(MemoryConfig protoConfig)345     void SetProtoConfig(MemoryConfig protoConfig)
346     {
347         protoConfig_ = protoConfig;
348     };
349 
350     template <typename T> void WriteProcesseList(T& memoryData);
351 
352     template <typename T> void WriteProcinfoByPidfds(T& processMemoryInfo, int32_t pid);
353 
354     DIR* OpenDestDir(const char* dirPath);
355     int32_t GetValidPid(DIR* dirp);
356     // for test change static
357     int ParseNumber(std::string line);
358 
359 private:
360     /* data */
361     MemoryConfig protoConfig_;
362 
363     std::unique_ptr<uint8_t[]> buffer_;
364 
365     int meminfoFd_ = -1;
366     int vmstatFd_ = -1;
367     std::map<std::string, int> meminfoCounters_ = {};
368     std::map<std::string, int> vmstatCounters_ = {};
369 
370     void InitProto2StrVector();
371     std::vector<const char*> meminfoStrList_ = {};
372     std::vector<const char*> vmstatStrList_ = {};
373 
374     // SmapsStats *
375     template <typename T> void WriteVmstat(T& memoryData);
376 
377     template <typename T> void WriteMeminfo(T& memoryData);
378 
379     template <typename T> void WriteZramData(T& memoryData);
380 
381     std::unordered_map<int32_t, std::vector<int>> pidFds_ = {};
382     std::vector<int32_t> seenPids_ = {};
383     char* testpath_;
384     int32_t err_ = -1;
385     int32_t ReadFile(int fd);
386     std::string ReadFile(const std::string& path);
387     std::vector<int> OpenProcPidFiles(int32_t pid);
388     int32_t ReadProcPidFile(int32_t pid, const char* pFileName);
389 
390     template <typename T> void WriteProcessInfo(T& memoryData, int32_t pid);
391 
392     template <typename T> void WriteOomInfo(T& processMemoryInfo, int32_t pid);
393 
394     template <typename T> void WriteProcess(T& processMemoryInfo, const char* pFile, uint32_t fileLen, int32_t pid);
395 
396     template <typename T> void WriteAppsummary(T& processMemoryInfo, SmapsStats& smapInfo);
397 
398     template <typename T> void SetProcessInfo(T& processMemoryInfo, int key, const char* word);
399 
400     bool StringToUll(const char* word, uint64_t& value);
401 
402     bool BufnCmp(const char* src, int srcLen, const char* key, int keyLen);
403     bool addPidBySort(int32_t pid);
404     int GetProcStatusId(const char* src, int srcLen);
405 
ParseMemInfo(const char * data,T & processMemoryInfo)406     template <typename T> bool ParseMemInfo(const char* data, T& processMemoryInfo)
407     {
408         bool ready = false;
409         bool done = false;
410         std::istringstream ss(data);
411         std::string line;
412 
413         auto memsummary = processMemoryInfo.mutable_memsummary();
414         while (std::getline(ss, line)) {
415             std::string s(line);
416             if (s.find("App Summary") != s.npos) {
417                 ready = true;
418                 continue;
419             }
420 
421             if (ready) {
422                 if (s.find("Java Heap:") != s.npos) {
423                     memsummary->set_java_heap(ParseNumber(s));
424                     continue;
425                 }
426                 if (s.find("Native Heap:") != s.npos) {
427                     memsummary->set_native_heap(ParseNumber(s));
428                     continue;
429                 }
430                 if (s.find("Code:") != s.npos) {
431                     memsummary->set_code(ParseNumber(s));
432                     continue;
433                 }
434                 if (s.find("Stack:") != s.npos) {
435                     memsummary->set_stack(ParseNumber(s));
436                     continue;
437                 }
438                 if (s.find("Graphics:") != s.npos) {
439                     memsummary->set_graphics(ParseNumber(s));
440                     continue;
441                 }
442                 if (s.find("Private Other:") != s.npos) {
443                     memsummary->set_private_other(ParseNumber(s));
444                     continue;
445                 }
446                 if (s.find("System:") != s.npos) {
447                     memsummary->set_system(ParseNumber(s));
448                     done = true;
449                     break;
450                 }
451             }
452         }
453         return done;
454     }
455 
456     template <typename T> bool GetMemInfoByMemoryService(uint32_t pid, T& processMemoryInfo);
457 
458     int InitMemVmemFd();
459 
460     template <typename T, typename S> void WriteMemoryData(T& memoryDataProto, S smapsInfo);
461 
462     std::string RunCommand(const std::string& cmd);
463     int GetIndentNum(const std::string& line);
464     uint64_t SizeToBytes(const std::string& sizeStr, const std::string& type);
465     bool isRenderService(int pid);
466     bool GetRenderServiceGlSize(int32_t pid, GraphicsMemory& graphicsMemory);
467 
468     template <typename T> void WriteAshmemInfo(T& dataProto);
469     template <typename T> void WriteDmaInfo(T& dataProto);
470     template <typename T> void WriteGpuMemInfo(T& dataProto);
471     template <typename T> void WriteGpuDumpInfo(T& dataProto);
472     template <typename T> void WriteDumpProcessInfo(T& dataProto);
473     template <typename T> void WriteManagerServiceInfo(T& dataProto);
474     template <typename T> void WriteProfileMemInfo(T& dataProto);
475 
476     template <typename T> void SetAshmemInfo(T& ashmemInfo, int key, const char* word);
477     template <typename T> void SetDmaInfo(T& dmaInfo, int key, const char* word);
478     template <typename T> void SetGpuProcessInfo(T& gpuProcessInfo, int key, const char* word);
479     template <typename T> bool SetGpuDumpInfo(T& gpuDumpInfo, BufferSplitter& totalBuffer);
480     template <typename T> void SetRSImageDumpInfo(T& rsDumpInfo, int key, const char* word);
481     template <typename T> void SetManagerServiceInfo(T& windowInfo, int key, const char* word);
482 };
483 
484 #endif
485