1 /*
2  * Copyright (C) 2019 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 SRC_TRACE_PROCESSOR_TABLES_PROFILER_TABLES_H_
18 #define SRC_TRACE_PROCESSOR_TABLES_PROFILER_TABLES_H_
19 
20 #include "src/trace_processor/tables/macros.h"
21 #include "src/trace_processor/tables/track_tables.h"
22 
23 namespace perfetto {
24 namespace trace_processor {
25 namespace tables {
26 
27 // The profiler smaps contains the memory stats for virtual memory ranges
28 // captured by the [heap profiler](/docs/data-sources/native-heap-profiler.md).
29 // @param upid The UniquePID of the process {@joinable process.upid}.
30 // @param ts   Timestamp of the snapshot. Multiple rows will have the same
31 //             timestamp.
32 // @param path The mmaped file, as per /proc/pid/smaps.
33 // @param size_kb Total size of the mapping.
34 // @param private_dirty_kb KB of this mapping that are private dirty  RSS.
35 // @param swap_kb KB of this mapping that are in swap.
36 // @param file_name
37 // @param file_name_iid
38 // @param path_iid
39 // @param start_address
40 // @param module_timestamp
41 // @param module_debugid
42 // @param module_debug_path
43 // @param protection_flags
44 // @param private_clean_resident_kb
45 // @param shared_dirty_resident_kb
46 // @param shared_clean_resident_kb
47 // @param locked_kb
48 // @param proportional_resident_kb
49 // @tablegroup Callstack profilers
50 #define PERFETTO_TP_PROFILER_SMAPS_DEF(NAME, PARENT, C) \
51   NAME(ProfilerSmapsTable, "profiler_smaps")            \
52   PERFETTO_TP_ROOT_TABLE(PARENT, C)                     \
53   C(uint32_t, upid)                                     \
54   C(int64_t, ts)                                        \
55   C(StringPool::Id, path)                               \
56   C(int64_t, size_kb)                                   \
57   C(int64_t, private_dirty_kb)                          \
58   C(int64_t, swap_kb)                                   \
59   C(StringPool::Id, file_name)                          \
60   C(int64_t, start_address)                             \
61   C(int64_t, module_timestamp)                          \
62   C(StringPool::Id, module_debugid)                     \
63   C(StringPool::Id, module_debug_path)                  \
64   C(int64_t, protection_flags)                          \
65   C(int64_t, private_clean_resident_kb)                 \
66   C(int64_t, shared_dirty_resident_kb)                  \
67   C(int64_t, shared_clean_resident_kb)                  \
68   C(int64_t, locked_kb)                                 \
69   C(int64_t, proportional_resident_kb)
70 
71 PERFETTO_TP_TABLE(PERFETTO_TP_PROFILER_SMAPS_DEF);
72 
73 // Metadata about packages installed on the system.
74 // This is generated by the packages_list data-source.
75 // @param package_name name of the package, e.g. com.google.android.gm.
76 // @param uid UID processes of this package run as.
77 // @param debuggable bool whether this app is debuggable.
78 // @param profileable_from_shell bool whether this app is profileable.
79 // @param version_code versionCode from the APK.
80 #define PERFETTO_TP_PACKAGES_LIST_DEF(NAME, PARENT, C) \
81   NAME(PackageListTable, "package_list")               \
82   PERFETTO_TP_ROOT_TABLE(PARENT, C)                    \
83   C(StringPool::Id, package_name)                      \
84   C(int64_t, uid)                                      \
85   C(int32_t, debuggable)                               \
86   C(int32_t, profileable_from_shell)                   \
87   C(int64_t, version_code)
88 
89 PERFETTO_TP_TABLE(PERFETTO_TP_PACKAGES_LIST_DEF);
90 
91 // A mapping (binary / library) in a process.
92 // This is generated by the stack profilers: heapprofd and traced_perf.
93 // @param build_id hex-encoded Build ID of the binary / library.
94 // @param start start of the mapping in the process' address space.
95 // @param end end of the mapping in the process' address space.
96 // @param name filename of the binary / library {@joinable profiler_smaps.path}.
97 // @tablegroup Callstack profilers
98 #define PERFETTO_TP_STACK_PROFILE_MAPPING_DEF(NAME, PARENT, C) \
99   NAME(StackProfileMappingTable, "stack_profile_mapping")      \
100   PERFETTO_TP_ROOT_TABLE(PARENT, C)                            \
101   C(StringPool::Id, build_id)                                  \
102   C(int64_t, exact_offset)                                     \
103   C(int64_t, start_offset)                                     \
104   C(int64_t, start)                                            \
105   C(int64_t, end)                                              \
106   C(int64_t, load_bias)                                        \
107   C(StringPool::Id, name)
108 
109 PERFETTO_TP_TABLE(PERFETTO_TP_STACK_PROFILE_MAPPING_DEF);
110 
111 // A frame on the callstack. This is a location in a program.
112 // This is generated by the stack profilers: heapprofd and traced_perf.
113 // @param name name of the function this location is in.
114 // @param mapping the mapping (library / binary) this location is in.
115 // @param rel_pc the program counter relative to the start of the mapping.
116 // @param symbol_set_id if the profile was offline symbolized, the offline
117 //        symbol information of this frame.
118 //        {@joinable stack_profile_symbol.symbol_set_id}
119 // @tablegroup Callstack profilers
120 #define PERFETTO_TP_STACK_PROFILE_FRAME_DEF(NAME, PARENT, C) \
121   NAME(StackProfileFrameTable, "stack_profile_frame")        \
122   PERFETTO_TP_ROOT_TABLE(PARENT, C)                          \
123   C(StringPool::Id, name)                                    \
124   C(StackProfileMappingTable::Id, mapping)                   \
125   C(int64_t, rel_pc)                                         \
126   C(base::Optional<uint32_t>, symbol_set_id)                 \
127   C(base::Optional<StringPool::Id>, deobfuscated_name)
128 
129 PERFETTO_TP_TABLE(PERFETTO_TP_STACK_PROFILE_FRAME_DEF);
130 
131 // A callsite. This is a list of frames that were on the stack.
132 // This is generated by the stack profilers: heapprofd and traced_perf.
133 // @param depth distance from the bottom-most frame of the callstack.
134 // @param parent_id parent frame on the callstack. NULL for the bottom-most.
135 // @param frame_id frame at this position in the callstack.
136 // @tablegroup Callstack profilers
137 #define PERFETTO_TP_STACK_PROFILE_CALLSITE_DEF(NAME, PARENT, C) \
138   NAME(StackProfileCallsiteTable, "stack_profile_callsite")     \
139   PERFETTO_TP_ROOT_TABLE(PARENT, C)                             \
140   C(uint32_t, depth)                                            \
141   C(base::Optional<StackProfileCallsiteTable::Id>, parent_id)   \
142   C(StackProfileFrameTable::Id, frame_id)
143 
144 PERFETTO_TP_TABLE(PERFETTO_TP_STACK_PROFILE_CALLSITE_DEF);
145 
146 // TODO(rsavitski): rethink what to do with the root table now that only chrome
147 // callstacks use it.
148 
149 // Root table for timestamped stack samples.
150 // @param ts timestamp of the sample.
151 // @param callsite_id unwound callstack.
152 // @tablegroup Callstack profilers
153 #define PERFETTO_TP_STACK_SAMPLE_DEF(NAME, PARENT, C) \
154   NAME(StackSampleTable, "stack_sample")              \
155   PERFETTO_TP_ROOT_TABLE(PARENT, C)                   \
156   C(int64_t, ts, Column::Flag::kSorted)               \
157   C(StackProfileCallsiteTable::Id, callsite_id)
158 
159 PERFETTO_TP_TABLE(PERFETTO_TP_STACK_SAMPLE_DEF);
160 
161 // Samples from the Chromium stack sampler.
162 // @param ts timestamp this sample was taken at.
163 // @param utid thread that was active when the sample was taken.
164 // @param callsite_id callstack in active thread at time of sample.
165 // @tablegroup Callstack profilers
166 #define PERFETTO_TP_CPU_PROFILE_STACK_SAMPLE_DEF(NAME, PARENT, C) \
167   NAME(CpuProfileStackSampleTable, "cpu_profile_stack_sample")    \
168   PARENT(PERFETTO_TP_STACK_SAMPLE_DEF, C)                         \
169   C(uint32_t, utid)                                               \
170   C(int32_t, process_priority)
171 
172 PERFETTO_TP_TABLE(PERFETTO_TP_CPU_PROFILE_STACK_SAMPLE_DEF);
173 
174 // Samples from the traced_perf perf sampler.
175 //
176 // The table currently provides no means of discriminating between multiple data
177 // sources producing samples within a single trace.
178 // @param ts timestamp of the sample.
179 // @param utid sampled thread. {@joinable thread.utid}.
180 // @param cpu the core the sampled thread was running on.
181 // @param cpu_mode execution state (userspace/kernelspace) of the sampled
182 //        thread.
183 // @param callsite_id if set, unwound callstack of the sampled thread.
184 // @param unwind_error if set, indicates that the unwinding for this sample
185 //        encountered an error. Such samples still reference the best-effort
186 //        result via the callsite_id (with a synthetic error frame at the point
187 //        where unwinding stopped).
188 // @param perf_session_id distinguishes samples from different profiling
189 //        streams (i.e. multiple data sources).
190 //        {@joinable perf_counter_track.perf_session_id}
191 // @tablegroup Callstack profilers
192 #define PERFETTO_TP_PERF_SAMPLE_DEF(NAME, PARENT, C)            \
193   NAME(PerfSampleTable, "perf_sample")                          \
194   PERFETTO_TP_ROOT_TABLE(PARENT, C)                             \
195   C(int64_t, ts, Column::Flag::kSorted)                         \
196   C(uint32_t, utid)                                             \
197   C(uint32_t, cpu)                                              \
198   C(StringPool::Id, cpu_mode)                                   \
199   C(base::Optional<StackProfileCallsiteTable::Id>, callsite_id) \
200   C(base::Optional<StringPool::Id>, unwind_error)               \
201   C(uint32_t, perf_session_id)
202 
203 PERFETTO_TP_TABLE(PERFETTO_TP_PERF_SAMPLE_DEF);
204 
205 // Symbolization data for a frame. Rows with the same symbol_set_id describe
206 // one callframe, with the most-inlined symbol having id == symbol_set_id.
207 //
208 // For instance, if the function foo has an inlined call to the function bar,
209 // which has an inlined call to baz, the stack_profile_symbol table would look
210 // like this.
211 //
212 // ```
213 // |id|symbol_set_id|name         |source_file|line_number|
214 // |--|-------------|-------------|-----------|-----------|
215 // |1 |      1      |baz          |foo.cc     | 36        |
216 // |2 |      1      |bar          |foo.cc     | 30        |
217 // |3 |      1      |foo          |foo.cc     | 60        |
218 // ```
219 // @param name name of the function.
220 // @param source_file name of the source file containing the function.
221 // @param line_number line number of the frame in the source file. This is the
222 // exact line for the corresponding program counter, not the beginning of the
223 // function.
224 // @tablegroup Callstack profilers
225 #define PERFETTO_TP_SYMBOL_DEF(NAME, PARENT, C) \
226   NAME(SymbolTable, "stack_profile_symbol")     \
227   PERFETTO_TP_ROOT_TABLE(PARENT, C)             \
228   C(uint32_t, symbol_set_id)                    \
229   C(StringPool::Id, name)                       \
230   C(StringPool::Id, source_file)                \
231   C(uint32_t, line_number)
232 
233 PERFETTO_TP_TABLE(PERFETTO_TP_SYMBOL_DEF);
234 
235 // Allocations that happened at a callsite.
236 // This is generated by heapprofd.
237 // @param ts the timestamp the allocations happened at. heapprofd batches
238 // allocations and frees, and all data from a dump will have the same
239 // timestamp.
240 // @param upid the UniquePID of the allocating process.
241 //        {@joinable process.upid}
242 // @param callsite_id the callsite the allocation happened at.
243 // @param count if positive: number of allocations that happened at this
244 // callsite. if negative: number of allocations that happened at this callsite
245 // that were freed.
246 // @param size if positive: size of allocations that happened at this
247 // callsite. if negative: size of allocations that happened at this callsite
248 // that were freed.
249 // @tablegroup Callstack profilers
250 #define PERFETTO_TP_HEAP_PROFILE_ALLOCATION_DEF(NAME, PARENT, C) \
251   NAME(HeapProfileAllocationTable, "heap_profile_allocation")    \
252   PERFETTO_TP_ROOT_TABLE(PARENT, C)                              \
253   C(int64_t, ts, Column::Flag::kSorted)                          \
254   C(uint32_t, upid)                                              \
255   C(StringPool::Id, heap_name)                                   \
256   C(StackProfileCallsiteTable::Id, callsite_id)                  \
257   C(int64_t, count)                                              \
258   C(int64_t, size)
259 
260 PERFETTO_TP_TABLE(PERFETTO_TP_HEAP_PROFILE_ALLOCATION_DEF);
261 
262 // Table used to render flamegraphs. This gives cumulative sizes of nodes in
263 // the flamegraph.
264 //
265 // WARNING: This is experimental and the API is subject to change.
266 // @tablegroup Callstack profilers
267 #define PERFETTO_TP_EXPERIMENTAL_FLAMEGRAPH_NODES(NAME, PARENT, C)        \
268   NAME(ExperimentalFlamegraphNodesTable, "experimental_flamegraph_nodes") \
269   PERFETTO_TP_ROOT_TABLE(PARENT, C)                                       \
270   C(int64_t, ts, Column::Flag::kSorted | Column::Flag::kHidden)           \
271   C(uint32_t, upid, Column::Flag::kHidden)                                \
272   C(StringPool::Id, profile_type, Column::Flag::kHidden)                  \
273   C(StringPool::Id, focus_str, Column::Flag::kHidden)                     \
274   C(uint32_t, depth)                                                      \
275   C(StringPool::Id, name)                                                 \
276   C(StringPool::Id, map_name)                                             \
277   C(int64_t, count)                                                       \
278   C(int64_t, cumulative_count)                                            \
279   C(int64_t, size)                                                        \
280   C(int64_t, cumulative_size)                                             \
281   C(int64_t, alloc_count)                                                 \
282   C(int64_t, cumulative_alloc_count)                                      \
283   C(int64_t, alloc_size)                                                  \
284   C(int64_t, cumulative_alloc_size)                                       \
285   C(base::Optional<ExperimentalFlamegraphNodesTable::Id>, parent_id)
286 
287 PERFETTO_TP_TABLE(PERFETTO_TP_EXPERIMENTAL_FLAMEGRAPH_NODES);
288 
289 // @param name (potentially obfuscated) name of the class.
290 // @param deobfuscated_name if class name was obfuscated and deobfuscation map
291 // for it provided, the deobfuscated name.
292 // @param location the APK / Dex / JAR file the class is contained in.
293 // @tablegroup ART Heap Graphs
294 //
295 // classloader_id should really be HeapGraphObject::id, but that would
296 // create a loop, which is currently not possible.
297 // TODO(lalitm): resolve this
298 #define PERFETTO_TP_HEAP_GRAPH_CLASS_DEF(NAME, PARENT, C)   \
299   NAME(HeapGraphClassTable, "heap_graph_class")             \
300   PERFETTO_TP_ROOT_TABLE(PARENT, C)                         \
301   C(StringPool::Id, name)                                   \
302   C(base::Optional<StringPool::Id>, deobfuscated_name)      \
303   C(base::Optional<StringPool::Id>, location)               \
304   C(base::Optional<HeapGraphClassTable::Id>, superclass_id) \
305   C(base::Optional<uint32_t>, classloader_id)               \
306   C(StringPool::Id, kind)
307 
308 PERFETTO_TP_TABLE(PERFETTO_TP_HEAP_GRAPH_CLASS_DEF);
309 
310 // The objects on the Dalvik heap.
311 //
312 // All rows with the same (upid, graph_sample_ts) are one dump.
313 // @param upid UniquePid of the target {@joinable process.upid}.
314 // @param graph_sample_ts timestamp this dump was taken at.
315 // @param self_size size this object uses on the Java Heap.
316 // @param reference_set_id join key with heap_graph_reference containing all
317 //        objects referred in this object's fields.
318 //        {@joinable heap_graph_reference.reference_set_id}
319 // @param reachable bool whether this object is reachable from a GC root. If
320 // false, this object is uncollected garbage.
321 // @param type_id class this object is an instance of.
322 // @param root_type if not NULL, this object is a GC root.
323 // @tablegroup ART Heap Graphs
324 #define PERFETTO_TP_HEAP_GRAPH_OBJECT_DEF(NAME, PARENT, C)            \
325   NAME(HeapGraphObjectTable, "heap_graph_object")                     \
326   PERFETTO_TP_ROOT_TABLE(PARENT, C)                                   \
327   C(uint32_t, upid)                                                   \
328   C(int64_t, graph_sample_ts)                                         \
329   C(int64_t, self_size)                                               \
330   C(base::Optional<uint32_t>, reference_set_id, Column::Flag::kDense) \
331   C(int32_t, reachable)                                               \
332   C(HeapGraphClassTable::Id, type_id)                                 \
333   C(base::Optional<StringPool::Id>, root_type)                        \
334   C(int32_t, root_distance, Column::Flag::kHidden)
335 
336 PERFETTO_TP_TABLE(PERFETTO_TP_HEAP_GRAPH_OBJECT_DEF);
337 
338 // Many-to-many mapping between heap_graph_object.
339 //
340 // This associates the object with given reference_set_id with the objects
341 // that are referred to by its fields.
342 // @param reference_set_id join key to heap_graph_object.
343 // @param owner_id id of object that has this reference_set_id.
344 // @param owned_id id of object that is referred to.
345 // @param field_name the field that refers to the object. E.g. Foo.name.
346 // @param field_type_name the static type of the field. E.g. java.lang.String.
347 // @param deobfuscated_field_name if field_name was obfuscated and a
348 // deobfuscation mapping was provided for it, the deobfuscated name.
349 // @tablegroup ART Heap Graphs
350 #define PERFETTO_TP_HEAP_GRAPH_REFERENCE_DEF(NAME, PARENT, C) \
351   NAME(HeapGraphReferenceTable, "heap_graph_reference")       \
352   PERFETTO_TP_ROOT_TABLE(PARENT, C)                           \
353   C(uint32_t, reference_set_id, Column::Flag::kSorted)        \
354   C(HeapGraphObjectTable::Id, owner_id)                       \
355   C(base::Optional<HeapGraphObjectTable::Id>, owned_id)       \
356   C(StringPool::Id, field_name)                               \
357   C(StringPool::Id, field_type_name)                          \
358   C(base::Optional<StringPool::Id>, deobfuscated_field_name)
359 
360 PERFETTO_TP_TABLE(PERFETTO_TP_HEAP_GRAPH_REFERENCE_DEF);
361 
362 // @param arg_set_id {@joinable args.arg_set_id}
363 #define PERFETTO_TP_VULKAN_MEMORY_ALLOCATIONS_DEF(NAME, PARENT, C) \
364   NAME(VulkanMemoryAllocationsTable, "vulkan_memory_allocations")  \
365   PERFETTO_TP_ROOT_TABLE(PARENT, C)                                \
366   C(StringPool::Id, source)                                        \
367   C(StringPool::Id, operation)                                     \
368   C(int64_t, timestamp)                                            \
369   C(base::Optional<uint32_t>, upid)                                \
370   C(base::Optional<int64_t>, device)                               \
371   C(base::Optional<int64_t>, device_memory)                        \
372   C(base::Optional<uint32_t>, memory_type)                         \
373   C(base::Optional<uint32_t>, heap)                                \
374   C(base::Optional<StringPool::Id>, function_name)                 \
375   C(base::Optional<int64_t>, object_handle)                        \
376   C(base::Optional<int64_t>, memory_address)                       \
377   C(base::Optional<int64_t>, memory_size)                          \
378   C(StringPool::Id, scope)                                         \
379   C(base::Optional<uint32_t>, arg_set_id)
380 
381 PERFETTO_TP_TABLE(PERFETTO_TP_VULKAN_MEMORY_ALLOCATIONS_DEF);
382 
383 #define PERFETTO_TP_GPU_COUNTER_GROUP_DEF(NAME, PARENT, C) \
384   NAME(GpuCounterGroupTable, "gpu_counter_group")          \
385   PERFETTO_TP_ROOT_TABLE(PARENT, C)                        \
386   C(int32_t, group_id)                                     \
387   C(TrackTable::Id, track_id)
388 
389 PERFETTO_TP_TABLE(PERFETTO_TP_GPU_COUNTER_GROUP_DEF);
390 
391 }  // namespace tables
392 }  // namespace trace_processor
393 }  // namespace perfetto
394 
395 #endif  // SRC_TRACE_PROCESSOR_TABLES_PROFILER_TABLES_H_
396