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 <vector> 21 22 #include "perfetto/ext/base/optional.h" 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/proto_incremental_state.h" 29 #include "src/trace_processor/importers/proto/vulkan_memory_tracker.h" 30 #include "src/trace_processor/storage/trace_storage.h" 31 32 #include "protos/perfetto/trace/gpu/vulkan_memory_event.pbzero.h" 33 34 namespace perfetto { 35 36 namespace protos { 37 namespace pbzero { 38 39 class GpuRenderStageEvent_Decoder; 40 41 } // namespace pbzero 42 } // namespace protos 43 44 namespace trace_processor { 45 46 class TraceProcessorContext; 47 48 struct ProtoEnumHasher { 49 template <typename T> operatorProtoEnumHasher50 std::size_t operator()(T t) const { 51 return static_cast<std::size_t>(t); 52 } 53 }; 54 55 // Class for parsing graphics related events. 56 class GpuEventParser { 57 public: 58 using ConstBytes = protozero::ConstBytes; 59 using VulkanMemoryEventSource = VulkanMemoryEvent::Source; 60 using VulkanMemoryEventOperation = 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(UniquePid, 72 const VulkanMemoryEvent::Decoder&); 73 74 void ParseVulkanApiEvent(int64_t, ConstBytes); 75 76 void ParseGpuMemTotalEvent(int64_t, ConstBytes); 77 78 private: 79 const StringId GetFullStageName( 80 PacketSequenceStateGeneration* sequence_state, 81 const protos::pbzero::GpuRenderStageEvent_Decoder& event) const; 82 void InsertGpuTrack( 83 const protos::pbzero:: 84 GpuRenderStageEvent_Specifications_Description_Decoder& hw_queue); 85 base::Optional<std::string> FindDebugName(int32_t vk_object_type, 86 uint64_t vk_handle) const; 87 const StringId ParseRenderSubpasses( 88 const protos::pbzero::GpuRenderStageEvent_Decoder& event) const; 89 90 TraceProcessorContext* const context_; 91 VulkanMemoryTracker vulkan_memory_tracker_; 92 // For GpuCounterEvent 93 std::unordered_map<uint32_t, TrackId> gpu_counter_track_ids_; 94 // For GpuRenderStageEvent 95 const StringId description_id_; 96 const StringId gpu_render_stage_scope_id_; 97 std::vector<perfetto::base::Optional<TrackId>> gpu_hw_queue_ids_; 98 size_t gpu_hw_queue_counter_ = 0; 99 // Map of stage ID -> pair(stage name, stage description) 100 std::vector<std::pair<StringId, StringId>> gpu_render_stage_ids_; 101 // For VulkanMemoryEvent 102 std::unordered_map<VulkanMemoryEvent::AllocationScope, 103 int64_t /*counter_value*/, 104 ProtoEnumHasher> 105 vulkan_driver_memory_counters_; 106 std::unordered_map<uint32_t /*memory_type*/, int64_t /*counter_value*/> 107 vulkan_device_memory_counters_allocate_; 108 std::unordered_map<uint32_t /*memory_type*/, int64_t /*counter_value*/> 109 vulkan_device_memory_counters_bind_; 110 // For GpuLog 111 const StringId gpu_log_track_name_id_; 112 const StringId gpu_log_scope_id_; 113 const StringId tag_id_; 114 const StringId log_message_id_; 115 std::array<StringId, 7> log_severity_ids_; 116 // For Vulkan events. 117 // For VulkanApiEvent.VkDebugUtilsObjectName. 118 // Map of vk handle -> vk object name. 119 using DebugMarkerMap = std::unordered_map<uint64_t, std::string>; 120 // Map of VkObjectType -> DebugMarkerMap. 121 std::unordered_map<int32_t, DebugMarkerMap> debug_marker_names_; 122 // For VulkanApiEvent.VkQueueSubmit. 123 StringId vk_event_track_id_; 124 StringId vk_event_scope_id_; 125 StringId vk_queue_submit_id_; 126 // For GpuMemTotalEvent 127 const StringId gpu_mem_total_name_id_; 128 const StringId gpu_mem_total_unit_id_; 129 const StringId gpu_mem_total_global_desc_id_; 130 const StringId gpu_mem_total_proc_desc_id_; 131 }; 132 } // namespace trace_processor 133 } // namespace perfetto 134 135 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GPU_EVENT_PARSER_H_ 136