xref: /aosp_15_r20/external/perfetto/src/profiling/perf/common_types.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2020 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_PROFILING_PERF_COMMON_TYPES_H_
18 #define SRC_PROFILING_PERF_COMMON_TYPES_H_
19 
20 #include <memory>
21 #include <vector>
22 
23 #include <linux/perf_event.h>
24 #include <stdint.h>
25 
26 #include <unwindstack/Error.h>
27 #include <unwindstack/Regs.h>
28 
29 #include "src/profiling/common/unwind_support.h"
30 
31 namespace perfetto {
32 namespace profiling {
33 
34 // Data present in all types of samples.
35 struct CommonSampleData {
36   uint16_t cpu_mode = PERF_RECORD_MISC_CPUMODE_UNKNOWN;
37   uint32_t cpu = 0;
38   pid_t pid = 0;
39   pid_t tid = 0;
40   uint64_t timestamp = 0;
41   uint64_t timebase_count = 0;
42   std::vector<uint64_t> follower_counts;
43 };
44 
45 // A parsed perf sample record (PERF_RECORD_SAMPLE from the kernel buffer).
46 // Self-contained, used as input to the callstack unwinding.
47 struct ParsedSample {
48   // move-only
49   ParsedSample() = default;
50   ParsedSample(const ParsedSample&) = delete;
51   ParsedSample& operator=(const ParsedSample&) = delete;
52   ParsedSample(ParsedSample&&) noexcept = default;
53   ParsedSample& operator=(ParsedSample&&) noexcept = default;
54 
55   CommonSampleData common;
56   std::unique_ptr<unwindstack::Regs> regs;
57   std::vector<char> stack;
58   bool stack_maxed = false;
59   std::vector<uint64_t> kernel_ips;
60 };
61 
62 // Entry in an unwinding queue. Either a sample that requires unwinding, or a
63 // tombstoned entry (valid == false).
64 struct UnwindEntry {
InvalidUnwindEntry65   static UnwindEntry Invalid() { return UnwindEntry{}; }
66 
67   UnwindEntry() = default;  // for initial unwinding queue entries' state
68 
UnwindEntryUnwindEntry69   UnwindEntry(uint64_t _data_source_id, ParsedSample _sample)
70       : valid(true),
71         data_source_id(_data_source_id),
72         sample(std::move(_sample)) {}
73 
74   bool valid = false;
75   uint64_t data_source_id = 0;
76   ParsedSample sample;
77 };
78 
79 // Fully processed sample that is ready for output.
80 struct CompletedSample {
81   // move-only
82   CompletedSample() = default;
83   CompletedSample(const CompletedSample&) = delete;
84   CompletedSample& operator=(const CompletedSample&) = delete;
85   CompletedSample(CompletedSample&&) noexcept = default;
86   CompletedSample& operator=(CompletedSample&&) noexcept = default;
87 
88   CommonSampleData common;
89   std::vector<unwindstack::FrameData> frames;
90   std::vector<std::string> build_ids;
91   unwindstack::ErrorCode unwind_error = unwindstack::ERROR_NONE;
92 };
93 
94 }  // namespace profiling
95 }  // namespace perfetto
96 
97 #endif  // SRC_PROFILING_PERF_COMMON_TYPES_H_
98