xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/proto/gpu_event_parser.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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_IMPORTERS_PROTO_GPU_EVENT_PARSER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GPU_EVENT_PARSER_H_
19 
20 #include <array>
21 #include <cstddef>
22 #include <cstdint>
23 #include <optional>
24 #include <string>
25 #include <unordered_map>
26 #include <utility>
27 #include <vector>
28 
29 #include "perfetto/protozero/field.h"
30 #include "protos/perfetto/trace/gpu/gpu_render_stage_event.pbzero.h"
31 #include "src/trace_processor/importers/proto/packet_sequence_state_generation.h"
32 #include "src/trace_processor/importers/proto/vulkan_memory_tracker.h"
33 #include "src/trace_processor/storage/trace_storage.h"
34 
35 #include "protos/perfetto/trace/gpu/vulkan_memory_event.pbzero.h"
36 
37 namespace perfetto {
38 
39 namespace protos::pbzero {
40 class GpuRenderStageEvent_Decoder;
41 }  // namespace protos::pbzero
42 
43 namespace trace_processor {
44 
45 class TraceProcessorContext;
46 
47 struct ProtoEnumHasher {
48   template <typename T>
operatorProtoEnumHasher49   std::size_t operator()(T t) const {
50     return static_cast<std::size_t>(t);
51   }
52 };
53 
54 // Class for parsing graphics related events.
55 class GpuEventParser {
56  public:
57   using ConstBytes = protozero::ConstBytes;
58   using VulkanMemoryEventSource = protos::pbzero::VulkanMemoryEvent::Source;
59   using VulkanMemoryEventOperation =
60       protos::pbzero::VulkanMemoryEvent::Operation;
61   explicit GpuEventParser(TraceProcessorContext*);
62 
63   void ParseGpuCounterEvent(int64_t ts, ConstBytes);
64   void ParseGpuRenderStageEvent(int64_t ts,
65                                 PacketSequenceStateGeneration*,
66                                 ConstBytes);
67   void ParseGraphicsFrameEvent(int64_t timestamp, ConstBytes);
68   void ParseGpuLog(int64_t ts, ConstBytes);
69 
70   void ParseVulkanMemoryEvent(PacketSequenceStateGeneration*, ConstBytes);
71   void UpdateVulkanMemoryAllocationCounters(
72       UniquePid,
73       const protos::pbzero::VulkanMemoryEvent::Decoder&);
74 
75   void ParseVulkanApiEvent(int64_t, ConstBytes);
76 
77   void ParseGpuMemTotalEvent(int64_t, ConstBytes);
78 
79  private:
80   StringId GetFullStageName(
81       PacketSequenceStateGeneration* sequence_state,
82       const protos::pbzero::GpuRenderStageEvent_Decoder& event) const;
83   void InsertGpuTrack(
84       const protos::pbzero::
85           GpuRenderStageEvent_Specifications_Description_Decoder& hw_queue);
86   std::optional<std::string> FindDebugName(int32_t vk_object_type,
87                                            uint64_t vk_handle) const;
88   StringId ParseRenderSubpasses(
89       const protos::pbzero::GpuRenderStageEvent_Decoder& event) const;
90 
91   TraceProcessorContext* const context_;
92   VulkanMemoryTracker vulkan_memory_tracker_;
93   // For GpuCounterEvent
94   std::unordered_map<uint32_t, TrackId> gpu_counter_track_ids_;
95   // For GpuRenderStageEvent
96   const StringId description_id_;
97   const StringId gpu_render_stage_scope_id_;
98   std::vector<std::optional<TrackId>> gpu_hw_queue_ids_;
99   size_t gpu_hw_queue_counter_ = 0;
100   // Map of stage ID -> pair(stage name, stage description)
101   std::vector<std::pair<StringId, StringId>> gpu_render_stage_ids_;
102   // For VulkanMemoryEvent
103   std::unordered_map<protos::pbzero::VulkanMemoryEvent::AllocationScope,
104                      int64_t /*counter_value*/,
105                      ProtoEnumHasher>
106       vulkan_driver_memory_counters_;
107   std::unordered_map<uint32_t /*memory_type*/, int64_t /*counter_value*/>
108       vulkan_device_memory_counters_allocate_;
109   std::unordered_map<uint32_t /*memory_type*/, int64_t /*counter_value*/>
110       vulkan_device_memory_counters_bind_;
111   // For GpuLog
112   const StringId gpu_log_track_name_id_;
113   const StringId gpu_log_scope_id_;
114   const StringId tag_id_;
115   const StringId log_message_id_;
116   std::array<StringId, 7> log_severity_ids_;
117   // For Vulkan events.
118   // For VulkanApiEvent.VkDebugUtilsObjectName.
119   // Map of vk handle -> vk object name.
120   using DebugMarkerMap = std::unordered_map<uint64_t, std::string>;
121   // Map of VkObjectType -> DebugMarkerMap.
122   std::unordered_map<int32_t, DebugMarkerMap> debug_marker_names_;
123   // For VulkanApiEvent.VkQueueSubmit.
124   StringId vk_event_track_id_;
125   StringId vk_event_scope_id_;
126   StringId vk_queue_submit_id_;
127 };
128 }  // namespace trace_processor
129 }  // namespace perfetto
130 
131 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GPU_EVENT_PARSER_H_
132