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 InsertTrackForUninternedRenderStage( 84 uint32_t id, 85 const protos::pbzero::GpuRenderStageEvent::Specifications::Description:: 86 Decoder&); 87 std::optional<std::string> FindDebugName(int32_t vk_object_type, 88 uint64_t vk_handle) const; 89 StringId ParseRenderSubpasses( 90 const protos::pbzero::GpuRenderStageEvent_Decoder& event) const; 91 92 TraceProcessorContext* const context_; 93 VulkanMemoryTracker vulkan_memory_tracker_; 94 95 const StringId context_id_id_; 96 const StringId render_target_id_; 97 const StringId render_target_name_id_; 98 const StringId render_pass_id_; 99 const StringId render_pass_name_id_; 100 const StringId render_subpasses_id_; 101 const StringId command_buffer_id_; 102 const StringId command_buffer_name_id_; 103 const StringId frame_id_id_; 104 const StringId submission_id_id_; 105 const StringId hw_queue_id_id_; 106 const StringId upid_id_; 107 const StringId pid_id_; 108 const StringId tid_id_; 109 110 // For GpuCounterEvent 111 std::unordered_map<uint32_t, TrackId> gpu_counter_track_ids_; 112 113 // For GpuRenderStageEvent 114 const StringId description_id_; 115 std::vector<std::optional<TrackId>> gpu_hw_queue_ids_; 116 117 // Map of stage ID -> pair(stage name, stage description) 118 std::vector<std::pair<StringId, StringId>> gpu_render_stage_ids_; 119 120 // For VulkanMemoryEvent 121 std::unordered_map<protos::pbzero::VulkanMemoryEvent::AllocationScope, 122 int64_t /*counter_value*/, 123 ProtoEnumHasher> 124 vulkan_driver_memory_counters_; 125 std::unordered_map<uint32_t /*memory_type*/, int64_t /*counter_value*/> 126 vulkan_device_memory_counters_allocate_; 127 std::unordered_map<uint32_t /*memory_type*/, int64_t /*counter_value*/> 128 vulkan_device_memory_counters_bind_; 129 130 // For GpuLog 131 const StringId tag_id_; 132 const StringId log_message_id_; 133 std::array<StringId, 7> log_severity_ids_; 134 135 // For Vulkan events. 136 // For VulkanApiEvent.VkDebugUtilsObjectName. 137 // Map of vk handle -> vk object name. 138 using DebugMarkerMap = std::unordered_map<uint64_t, std::string>; 139 140 // Map of VkObjectType -> DebugMarkerMap. 141 std::unordered_map<int32_t, DebugMarkerMap> debug_marker_names_; 142 143 // For VulkanApiEvent.VkQueueSubmit. 144 StringId vk_event_track_id_; 145 StringId vk_queue_submit_id_; 146 }; 147 } // namespace trace_processor 148 } // namespace perfetto 149 150 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GPU_EVENT_PARSER_H_ 151