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_VULKAN_MEMORY_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_VULKAN_MEMORY_TRACKER_H_
19 
20 #include "src/trace_processor/importers/proto/proto_incremental_state.h"
21 #include "src/trace_processor/storage/trace_storage.h"
22 
23 #include "protos/perfetto/trace/gpu/vulkan_memory_event.pbzero.h"
24 
25 namespace perfetto {
26 namespace trace_processor {
27 
28 using protos::pbzero::VulkanMemoryEvent;
29 
30 class TraceProcessorContext;
31 
32 class VulkanMemoryTracker {
33  public:
34   enum class DeviceCounterType {
35     kAllocationCounter = 0,
36     kBindCounter = 1,
37   };
38 
39   explicit VulkanMemoryTracker(TraceProcessorContext* context);
40   ~VulkanMemoryTracker() = default;
41 
42   template <int32_t FieldId>
GetInternedString(PacketSequenceStateGeneration * state,uint64_t iid)43   StringId GetInternedString(PacketSequenceStateGeneration* state,
44                              uint64_t iid) {
45     auto* decoder =
46         state->LookupInternedMessage<FieldId, protos::pbzero::InternedString>(
47             iid);
48     if (!decoder)
49       return kNullStringId;
50     return context_->storage->InternString(
51         base::StringView(reinterpret_cast<const char*>(decoder->str().data),
52                          decoder->str().size));
53   }
54 
55   StringId FindSourceString(VulkanMemoryEvent::Source);
56   StringId FindOperationString(VulkanMemoryEvent::Operation);
57   StringId FindAllocationScopeString(VulkanMemoryEvent::AllocationScope);
58   StringId FindAllocationScopeCounterString(VulkanMemoryEvent::AllocationScope);
59   StringId FindMemoryTypeCounterString(uint32_t /*memory_type*/,
60                                        DeviceCounterType);
61 
62  private:
63   TraceProcessorContext* const context_;
64 
65   const std::string vulkan_driver_memory_counter_str_;
66   const std::string vulkan_device_memory_counter_str_;
67   std::vector<StringId> source_strs_id_;
68   std::vector<StringId> operation_strs_id_;
69   std::vector<StringId> scope_strs_id_;
70   std::vector<StringId> scope_counter_strs_id_;
71   std::unordered_map<uint32_t /*memory_type*/, StringId>
72       memory_type_allocation_counter_string_map_;
73   std::unordered_map<uint32_t /*memory_type*/, StringId>
74       memory_type_bind_counter_string_map_;
75 
76   void SetupSourceAndTypeInternedStrings();
77 };
78 
79 }  // namespace trace_processor
80 }  // namespace perfetto
81 
82 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_VULKAN_MEMORY_TRACKER_H_
83