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 #ifndef INCLUDE_PERFETTO_EXT_TRACED_SYS_STATS_COUNTERS_H_
18 #define INCLUDE_PERFETTO_EXT_TRACED_SYS_STATS_COUNTERS_H_
19 
20 #include "perfetto/ext/base/utils.h"
21 #include "protos/perfetto/common/sys_stats_counters.pbzero.h"
22 
23 #include <vector>
24 
25 namespace perfetto {
26 
27 struct KeyAndId {
28   const char* str;
29   int id;
30 };
31 
32 constexpr KeyAndId kMeminfoKeys[] = {
33     {"MemUnspecified", protos::pbzero::MeminfoCounters::MEMINFO_UNSPECIFIED},
34     {"MemTotal", protos::pbzero::MeminfoCounters::MEMINFO_MEM_TOTAL},
35     {"MemFree", protos::pbzero::MeminfoCounters::MEMINFO_MEM_FREE},
36     {"MemAvailable", protos::pbzero::MeminfoCounters::MEMINFO_MEM_AVAILABLE},
37     {"Buffers", protos::pbzero::MeminfoCounters::MEMINFO_BUFFERS},
38     {"Cached", protos::pbzero::MeminfoCounters::MEMINFO_CACHED},
39     {"SwapCached", protos::pbzero::MeminfoCounters::MEMINFO_SWAP_CACHED},
40     {"Active", protos::pbzero::MeminfoCounters::MEMINFO_ACTIVE},
41     {"Inactive", protos::pbzero::MeminfoCounters::MEMINFO_INACTIVE},
42     {"Active(anon)", protos::pbzero::MeminfoCounters::MEMINFO_ACTIVE_ANON},
43     {"Inactive(anon)", protos::pbzero::MeminfoCounters::MEMINFO_INACTIVE_ANON},
44     {"Active(file)", protos::pbzero::MeminfoCounters::MEMINFO_ACTIVE_FILE},
45     {"Inactive(file)", protos::pbzero::MeminfoCounters::MEMINFO_INACTIVE_FILE},
46     {"Unevictable", protos::pbzero::MeminfoCounters::MEMINFO_UNEVICTABLE},
47     {"Mlocked", protos::pbzero::MeminfoCounters::MEMINFO_MLOCKED},
48     {"SwapTotal", protos::pbzero::MeminfoCounters::MEMINFO_SWAP_TOTAL},
49     {"SwapFree", protos::pbzero::MeminfoCounters::MEMINFO_SWAP_FREE},
50     {"Dirty", protos::pbzero::MeminfoCounters::MEMINFO_DIRTY},
51     {"Writeback", protos::pbzero::MeminfoCounters::MEMINFO_WRITEBACK},
52     {"AnonPages", protos::pbzero::MeminfoCounters::MEMINFO_ANON_PAGES},
53     {"Mapped", protos::pbzero::MeminfoCounters::MEMINFO_MAPPED},
54     {"Shmem", protos::pbzero::MeminfoCounters::MEMINFO_SHMEM},
55     {"Slab", protos::pbzero::MeminfoCounters::MEMINFO_SLAB},
56     {"SReclaimable", protos::pbzero::MeminfoCounters::MEMINFO_SLAB_RECLAIMABLE},
57     {"SUnreclaim", protos::pbzero::MeminfoCounters::MEMINFO_SLAB_UNRECLAIMABLE},
58     {"KernelStack", protos::pbzero::MeminfoCounters::MEMINFO_KERNEL_STACK},
59     {"PageTables", protos::pbzero::MeminfoCounters::MEMINFO_PAGE_TABLES},
60     {"CommitLimit", protos::pbzero::MeminfoCounters::MEMINFO_COMMIT_LIMIT},
61     {"Committed_AS", protos::pbzero::MeminfoCounters::MEMINFO_COMMITED_AS},
62     {"VmallocTotal", protos::pbzero::MeminfoCounters::MEMINFO_VMALLOC_TOTAL},
63     {"VmallocUsed", protos::pbzero::MeminfoCounters::MEMINFO_VMALLOC_USED},
64     {"VmallocChunk", protos::pbzero::MeminfoCounters::MEMINFO_VMALLOC_CHUNK},
65     {"CmaTotal", protos::pbzero::MeminfoCounters::MEMINFO_CMA_TOTAL},
66     {"CmaFree", protos::pbzero::MeminfoCounters::MEMINFO_CMA_FREE},
67 };
68 
69 const KeyAndId kVmstatKeys[] = {
70     {"VmstatUnspecified", protos::pbzero::VmstatCounters::VMSTAT_UNSPECIFIED},
71     {"nr_free_pages", protos::pbzero::VmstatCounters::VMSTAT_NR_FREE_PAGES},
72     {"nr_alloc_batch", protos::pbzero::VmstatCounters::VMSTAT_NR_ALLOC_BATCH},
73     {"nr_inactive_anon",
74      protos::pbzero::VmstatCounters::VMSTAT_NR_INACTIVE_ANON},
75     {"nr_active_anon", protos::pbzero::VmstatCounters::VMSTAT_NR_ACTIVE_ANON},
76     {"nr_inactive_file",
77      protos::pbzero::VmstatCounters::VMSTAT_NR_INACTIVE_FILE},
78     {"nr_active_file", protos::pbzero::VmstatCounters::VMSTAT_NR_ACTIVE_FILE},
79     {"nr_unevictable", protos::pbzero::VmstatCounters::VMSTAT_NR_UNEVICTABLE},
80     {"nr_mlock", protos::pbzero::VmstatCounters::VMSTAT_NR_MLOCK},
81     {"nr_anon_pages", protos::pbzero::VmstatCounters::VMSTAT_NR_ANON_PAGES},
82     {"nr_mapped", protos::pbzero::VmstatCounters::VMSTAT_NR_MAPPED},
83     {"nr_file_pages", protos::pbzero::VmstatCounters::VMSTAT_NR_FILE_PAGES},
84     {"nr_dirty", protos::pbzero::VmstatCounters::VMSTAT_NR_DIRTY},
85     {"nr_writeback", protos::pbzero::VmstatCounters::VMSTAT_NR_WRITEBACK},
86     {"nr_slab_reclaimable",
87      protos::pbzero::VmstatCounters::VMSTAT_NR_SLAB_RECLAIMABLE},
88     {"nr_slab_unreclaimable",
89      protos::pbzero::VmstatCounters::VMSTAT_NR_SLAB_UNRECLAIMABLE},
90     {"nr_page_table_pages",
91      protos::pbzero::VmstatCounters::VMSTAT_NR_PAGE_TABLE_PAGES},
92     {"nr_kernel_stack", protos::pbzero::VmstatCounters::VMSTAT_NR_KERNEL_STACK},
93     {"nr_overhead", protos::pbzero::VmstatCounters::VMSTAT_NR_OVERHEAD},
94     {"nr_unstable", protos::pbzero::VmstatCounters::VMSTAT_NR_UNSTABLE},
95     {"nr_bounce", protos::pbzero::VmstatCounters::VMSTAT_NR_BOUNCE},
96     {"nr_vmscan_write", protos::pbzero::VmstatCounters::VMSTAT_NR_VMSCAN_WRITE},
97     {"nr_vmscan_immediate_reclaim",
98      protos::pbzero::VmstatCounters::VMSTAT_NR_VMSCAN_IMMEDIATE_RECLAIM},
99     {"nr_writeback_temp",
100      protos::pbzero::VmstatCounters::VMSTAT_NR_WRITEBACK_TEMP},
101     {"nr_isolated_anon",
102      protos::pbzero::VmstatCounters::VMSTAT_NR_ISOLATED_ANON},
103     {"nr_isolated_file",
104      protos::pbzero::VmstatCounters::VMSTAT_NR_ISOLATED_FILE},
105     {"nr_shmem", protos::pbzero::VmstatCounters::VMSTAT_NR_SHMEM},
106     {"nr_dirtied", protos::pbzero::VmstatCounters::VMSTAT_NR_DIRTIED},
107     {"nr_written", protos::pbzero::VmstatCounters::VMSTAT_NR_WRITTEN},
108     {"nr_pages_scanned",
109      protos::pbzero::VmstatCounters::VMSTAT_NR_PAGES_SCANNED},
110     {"workingset_refault",
111      protos::pbzero::VmstatCounters::VMSTAT_WORKINGSET_REFAULT},
112     {"workingset_activate",
113      protos::pbzero::VmstatCounters::VMSTAT_WORKINGSET_ACTIVATE},
114     {"workingset_nodereclaim",
115      protos::pbzero::VmstatCounters::VMSTAT_WORKINGSET_NODERECLAIM},
116     {"nr_anon_transparent_hugepages",
117      protos::pbzero::VmstatCounters::VMSTAT_NR_ANON_TRANSPARENT_HUGEPAGES},
118     {"nr_free_cma", protos::pbzero::VmstatCounters::VMSTAT_NR_FREE_CMA},
119     {"nr_swapcache", protos::pbzero::VmstatCounters::VMSTAT_NR_SWAPCACHE},
120     {"nr_dirty_threshold",
121      protos::pbzero::VmstatCounters::VMSTAT_NR_DIRTY_THRESHOLD},
122     {"nr_dirty_background_threshold",
123      protos::pbzero::VmstatCounters::VMSTAT_NR_DIRTY_BACKGROUND_THRESHOLD},
124     {"pgpgin", protos::pbzero::VmstatCounters::VMSTAT_PGPGIN},
125     {"pgpgout", protos::pbzero::VmstatCounters::VMSTAT_PGPGOUT},
126     {"pgpgoutclean", protos::pbzero::VmstatCounters::VMSTAT_PGPGOUTCLEAN},
127     {"pswpin", protos::pbzero::VmstatCounters::VMSTAT_PSWPIN},
128     {"pswpout", protos::pbzero::VmstatCounters::VMSTAT_PSWPOUT},
129     {"pgalloc_dma", protos::pbzero::VmstatCounters::VMSTAT_PGALLOC_DMA},
130     {"pgalloc_normal", protos::pbzero::VmstatCounters::VMSTAT_PGALLOC_NORMAL},
131     {"pgalloc_movable", protos::pbzero::VmstatCounters::VMSTAT_PGALLOC_MOVABLE},
132     {"pgfree", protos::pbzero::VmstatCounters::VMSTAT_PGFREE},
133     {"pgactivate", protos::pbzero::VmstatCounters::VMSTAT_PGACTIVATE},
134     {"pgdeactivate", protos::pbzero::VmstatCounters::VMSTAT_PGDEACTIVATE},
135     {"pgfault", protos::pbzero::VmstatCounters::VMSTAT_PGFAULT},
136     {"pgmajfault", protos::pbzero::VmstatCounters::VMSTAT_PGMAJFAULT},
137     {"pgrefill_dma", protos::pbzero::VmstatCounters::VMSTAT_PGREFILL_DMA},
138     {"pgrefill_normal", protos::pbzero::VmstatCounters::VMSTAT_PGREFILL_NORMAL},
139     {"pgrefill_movable",
140      protos::pbzero::VmstatCounters::VMSTAT_PGREFILL_MOVABLE},
141     {"pgsteal_kswapd_dma",
142      protos::pbzero::VmstatCounters::VMSTAT_PGSTEAL_KSWAPD_DMA},
143     {"pgsteal_kswapd_normal",
144      protos::pbzero::VmstatCounters::VMSTAT_PGSTEAL_KSWAPD_NORMAL},
145     {"pgsteal_kswapd_movable",
146      protos::pbzero::VmstatCounters::VMSTAT_PGSTEAL_KSWAPD_MOVABLE},
147     {"pgsteal_direct_dma",
148      protos::pbzero::VmstatCounters::VMSTAT_PGSTEAL_DIRECT_DMA},
149     {"pgsteal_direct_normal",
150      protos::pbzero::VmstatCounters::VMSTAT_PGSTEAL_DIRECT_NORMAL},
151     {"pgsteal_direct_movable",
152      protos::pbzero::VmstatCounters::VMSTAT_PGSTEAL_DIRECT_MOVABLE},
153     {"pgscan_kswapd_dma",
154      protos::pbzero::VmstatCounters::VMSTAT_PGSCAN_KSWAPD_DMA},
155     {"pgscan_kswapd_normal",
156      protos::pbzero::VmstatCounters::VMSTAT_PGSCAN_KSWAPD_NORMAL},
157     {"pgscan_kswapd_movable",
158      protos::pbzero::VmstatCounters::VMSTAT_PGSCAN_KSWAPD_MOVABLE},
159     {"pgscan_direct_dma",
160      protos::pbzero::VmstatCounters::VMSTAT_PGSCAN_DIRECT_DMA},
161     {"pgscan_direct_normal",
162      protos::pbzero::VmstatCounters::VMSTAT_PGSCAN_DIRECT_NORMAL},
163     {"pgscan_direct_movable",
164      protos::pbzero::VmstatCounters::VMSTAT_PGSCAN_DIRECT_MOVABLE},
165     {"pgscan_direct_throttle",
166      protos::pbzero::VmstatCounters::VMSTAT_PGSCAN_DIRECT_THROTTLE},
167     {"pginodesteal", protos::pbzero::VmstatCounters::VMSTAT_PGINODESTEAL},
168     {"slabs_scanned", protos::pbzero::VmstatCounters::VMSTAT_SLABS_SCANNED},
169     {"kswapd_inodesteal",
170      protos::pbzero::VmstatCounters::VMSTAT_KSWAPD_INODESTEAL},
171     {"kswapd_low_wmark_hit_quickly",
172      protos::pbzero::VmstatCounters::VMSTAT_KSWAPD_LOW_WMARK_HIT_QUICKLY},
173     {"kswapd_high_wmark_hit_quickly",
174      protos::pbzero::VmstatCounters::VMSTAT_KSWAPD_HIGH_WMARK_HIT_QUICKLY},
175     {"pageoutrun", protos::pbzero::VmstatCounters::VMSTAT_PAGEOUTRUN},
176     {"allocstall", protos::pbzero::VmstatCounters::VMSTAT_ALLOCSTALL},
177     {"pgrotated", protos::pbzero::VmstatCounters::VMSTAT_PGROTATED},
178     {"drop_pagecache", protos::pbzero::VmstatCounters::VMSTAT_DROP_PAGECACHE},
179     {"drop_slab", protos::pbzero::VmstatCounters::VMSTAT_DROP_SLAB},
180     {"pgmigrate_success",
181      protos::pbzero::VmstatCounters::VMSTAT_PGMIGRATE_SUCCESS},
182     {"pgmigrate_fail", protos::pbzero::VmstatCounters::VMSTAT_PGMIGRATE_FAIL},
183     {"compact_migrate_scanned",
184      protos::pbzero::VmstatCounters::VMSTAT_COMPACT_MIGRATE_SCANNED},
185     {"compact_free_scanned",
186      protos::pbzero::VmstatCounters::VMSTAT_COMPACT_FREE_SCANNED},
187     {"compact_isolated",
188      protos::pbzero::VmstatCounters::VMSTAT_COMPACT_ISOLATED},
189     {"compact_stall", protos::pbzero::VmstatCounters::VMSTAT_COMPACT_STALL},
190     {"compact_fail", protos::pbzero::VmstatCounters::VMSTAT_COMPACT_FAIL},
191     {"compact_success", protos::pbzero::VmstatCounters::VMSTAT_COMPACT_SUCCESS},
192     {"compact_daemon_wake",
193      protos::pbzero::VmstatCounters::VMSTAT_COMPACT_DAEMON_WAKE},
194     {"unevictable_pgs_culled",
195      protos::pbzero::VmstatCounters::VMSTAT_UNEVICTABLE_PGS_CULLED},
196     {"unevictable_pgs_scanned",
197      protos::pbzero::VmstatCounters::VMSTAT_UNEVICTABLE_PGS_SCANNED},
198     {"unevictable_pgs_rescued",
199      protos::pbzero::VmstatCounters::VMSTAT_UNEVICTABLE_PGS_RESCUED},
200     {"unevictable_pgs_mlocked",
201      protos::pbzero::VmstatCounters::VMSTAT_UNEVICTABLE_PGS_MLOCKED},
202     {"unevictable_pgs_munlocked",
203      protos::pbzero::VmstatCounters::VMSTAT_UNEVICTABLE_PGS_MUNLOCKED},
204     {"unevictable_pgs_cleared",
205      protos::pbzero::VmstatCounters::VMSTAT_UNEVICTABLE_PGS_CLEARED},
206     {"unevictable_pgs_stranded",
207      protos::pbzero::VmstatCounters::VMSTAT_UNEVICTABLE_PGS_STRANDED},
208     {"nr_zspages", protos::pbzero::VmstatCounters::VMSTAT_NR_ZSPAGES},
209     {"nr_ion_heap", protos::pbzero::VmstatCounters::VMSTAT_NR_ION_HEAP},
210     {"nr_gpu_heap", protos::pbzero::VmstatCounters::VMSTAT_NR_GPU_HEAP},
211 };
212 
213 // Returns a lookup table of meminfo counter names addressable by counter id.
BuildMeminfoCounterNames()214 inline std::vector<const char*> BuildMeminfoCounterNames() {
215   int max_id = 0;
216   for (size_t i = 0; i < base::ArraySize(kMeminfoKeys); i++)
217     max_id = std::max(max_id, kMeminfoKeys[i].id);
218   std::vector<const char*> v;
219   v.resize(static_cast<size_t>(max_id) + 1);
220   for (size_t i = 0; i < base::ArraySize(kMeminfoKeys); i++)
221     v[static_cast<size_t>(kMeminfoKeys[i].id)] = kMeminfoKeys[i].str;
222   return v;
223 }
224 
BuildVmstatCounterNames()225 inline std::vector<const char*> BuildVmstatCounterNames() {
226   int max_id = 0;
227   for (size_t i = 0; i < base::ArraySize(kVmstatKeys); i++)
228     max_id = std::max(max_id, kVmstatKeys[i].id);
229   std::vector<const char*> v;
230   v.resize(static_cast<size_t>(max_id) + 1);
231   for (size_t i = 0; i < base::ArraySize(kVmstatKeys); i++)
232     v[static_cast<size_t>(kVmstatKeys[i].id)] = kVmstatKeys[i].str;
233   return v;
234 }
235 
236 }  // namespace perfetto
237 
238 #endif  // INCLUDE_PERFETTO_EXT_TRACED_SYS_STATS_COUNTERS_H_
239