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 <optional> 21 #include <vector> 22 23 #include "perfetto/ext/base/string_writer.h" 24 #include "perfetto/protozero/field.h" 25 #include "protos/perfetto/trace/android/gpu_mem_event.pbzero.h" 26 #include "protos/perfetto/trace/gpu/gpu_render_stage_event.pbzero.h" 27 #include "src/trace_processor/importers/common/args_tracker.h" 28 #include "src/trace_processor/importers/proto/vulkan_memory_tracker.h" 29 #include "src/trace_processor/storage/trace_storage.h" 30 31 #include "protos/perfetto/trace/gpu/vulkan_memory_event.pbzero.h" 32 33 namespace perfetto { 34 35 namespace protos { 36 namespace pbzero { 37 38 class GpuRenderStageEvent_Decoder; 39 40 } // namespace pbzero 41 } // namespace protos 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 = VulkanMemoryEvent::Source; 59 using VulkanMemoryEventOperation = VulkanMemoryEvent::Operation; 60 explicit GpuEventParser(TraceProcessorContext*); 61 62 void ParseGpuCounterEvent(int64_t ts, ConstBytes); 63 void ParseGpuRenderStageEvent(int64_t ts, 64 PacketSequenceStateGeneration*, 65 ConstBytes); 66 void ParseGraphicsFrameEvent(int64_t timestamp, ConstBytes); 67 void ParseGpuLog(int64_t ts, ConstBytes); 68 69 void ParseVulkanMemoryEvent(PacketSequenceStateGeneration*, ConstBytes); 70 void UpdateVulkanMemoryAllocationCounters(UniquePid, 71 const VulkanMemoryEvent::Decoder&); 72 73 void ParseVulkanApiEvent(int64_t, ConstBytes); 74 75 void ParseGpuMemTotalEvent(int64_t, ConstBytes); 76 77 private: 78 const StringId GetFullStageName( 79 PacketSequenceStateGeneration* sequence_state, 80 const protos::pbzero::GpuRenderStageEvent_Decoder& event) const; 81 void InsertGpuTrack( 82 const protos::pbzero:: 83 GpuRenderStageEvent_Specifications_Description_Decoder& hw_queue); 84 std::optional<std::string> FindDebugName(int32_t vk_object_type, 85 uint64_t vk_handle) const; 86 const StringId ParseRenderSubpasses( 87 const protos::pbzero::GpuRenderStageEvent_Decoder& event) const; 88 89 TraceProcessorContext* const context_; 90 VulkanMemoryTracker vulkan_memory_tracker_; 91 // For GpuCounterEvent 92 std::unordered_map<uint32_t, TrackId> gpu_counter_track_ids_; 93 // For GpuRenderStageEvent 94 const StringId description_id_; 95 const StringId gpu_render_stage_scope_id_; 96 std::vector<std::optional<TrackId>> gpu_hw_queue_ids_; 97 size_t gpu_hw_queue_counter_ = 0; 98 // Map of stage ID -> pair(stage name, stage description) 99 std::vector<std::pair<StringId, StringId>> gpu_render_stage_ids_; 100 // For VulkanMemoryEvent 101 std::unordered_map<VulkanMemoryEvent::AllocationScope, 102 int64_t /*counter_value*/, 103 ProtoEnumHasher> 104 vulkan_driver_memory_counters_; 105 std::unordered_map<uint32_t /*memory_type*/, int64_t /*counter_value*/> 106 vulkan_device_memory_counters_allocate_; 107 std::unordered_map<uint32_t /*memory_type*/, int64_t /*counter_value*/> 108 vulkan_device_memory_counters_bind_; 109 // For GpuLog 110 const StringId gpu_log_track_name_id_; 111 const StringId gpu_log_scope_id_; 112 const StringId tag_id_; 113 const StringId log_message_id_; 114 std::array<StringId, 7> log_severity_ids_; 115 // For Vulkan events. 116 // For VulkanApiEvent.VkDebugUtilsObjectName. 117 // Map of vk handle -> vk object name. 118 using DebugMarkerMap = std::unordered_map<uint64_t, std::string>; 119 // Map of VkObjectType -> DebugMarkerMap. 120 std::unordered_map<int32_t, DebugMarkerMap> debug_marker_names_; 121 // For VulkanApiEvent.VkQueueSubmit. 122 StringId vk_event_track_id_; 123 StringId vk_event_scope_id_; 124 StringId vk_queue_submit_id_; 125 // For GpuMemTotalEvent 126 const StringId gpu_mem_total_name_id_; 127 const StringId gpu_mem_total_unit_id_; 128 const StringId gpu_mem_total_global_desc_id_; 129 const StringId gpu_mem_total_proc_desc_id_; 130 }; 131 } // namespace trace_processor 132 } // namespace perfetto 133 134 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GPU_EVENT_PARSER_H_ 135