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 #include "src/trace_processor/importers/proto/vulkan_memory_tracker.h"
18
19 #include <string>
20 #include "protos/perfetto/trace/interned_data/interned_data.pbzero.h"
21 #include "src/trace_processor/importers/common/process_tracker.h"
22 #include "src/trace_processor/types/trace_processor_context.h"
23
24 #include "perfetto/base/logging.h"
25
26 namespace perfetto {
27 namespace trace_processor {
28
VulkanMemoryTracker(TraceProcessorContext * context)29 VulkanMemoryTracker::VulkanMemoryTracker(TraceProcessorContext* context)
30 : context_(context),
31 vulkan_driver_memory_counter_str_("vulkan.mem.driver.scope."),
32 vulkan_device_memory_counter_str_("vulkan.mem.device.memory.type.") {
33 SetupSourceAndTypeInternedStrings();
34 }
35
SetupSourceAndTypeInternedStrings()36 void VulkanMemoryTracker::SetupSourceAndTypeInternedStrings() {
37 const std::vector<std::string> event_sources = {
38 "UNSPECIFIED", "DRIVER", "DEVICE",
39 "GPU_DEVICE_MEMORY", "GPU_BUFFER", "GPU_IMAGE"};
40 for (size_t i = 0; i < event_sources.size(); i++) {
41 source_strs_id_.emplace_back(context_->storage->InternString(
42 base::StringView(event_sources[i].c_str(), event_sources[i].length())));
43 }
44
45 const std::vector<std::string> event_operations = {
46 "UNSPECIFIED", "CREATE", "DESTROY",
47 "BIND", "DESTROY_BOUND", "ANNOTATIONS"};
48 for (size_t i = 0; i < event_operations.size(); i++) {
49 operation_strs_id_.emplace_back(
50 context_->storage->InternString(base::StringView(
51 event_operations[i].c_str(), event_operations[i].length())));
52 }
53
54 const std::vector<std::string> event_scopes = {
55 "UNSPECIFIED", "COMMAND", "OBJECT", "CACHE", "DEVICE", "INSTANCE"};
56 for (size_t i = 0; i < event_scopes.size(); i++) {
57 scope_strs_id_.emplace_back(context_->storage->InternString(
58 base::StringView(event_scopes[i].c_str(), event_scopes[i].length())));
59 auto scope_counter_str =
60 vulkan_driver_memory_counter_str_ + event_scopes[i];
61 scope_counter_strs_id_.emplace_back(
62 context_->storage->InternString(base::StringView(
63 scope_counter_str.c_str(), scope_counter_str.length())));
64 }
65 }
66
FindSourceString(VulkanMemoryEvent::Source source)67 StringId VulkanMemoryTracker::FindSourceString(
68 VulkanMemoryEvent::Source source) {
69 return source_strs_id_[static_cast<size_t>(source)];
70 }
71
FindOperationString(VulkanMemoryEvent::Operation operation)72 StringId VulkanMemoryTracker::FindOperationString(
73 VulkanMemoryEvent::Operation operation) {
74 return operation_strs_id_[static_cast<size_t>(operation)];
75 }
76
FindAllocationScopeString(VulkanMemoryEvent::AllocationScope scope)77 StringId VulkanMemoryTracker::FindAllocationScopeString(
78 VulkanMemoryEvent::AllocationScope scope) {
79 return scope_strs_id_[static_cast<size_t>(scope)];
80 }
81
FindAllocationScopeCounterString(VulkanMemoryEvent::AllocationScope scope)82 StringId VulkanMemoryTracker::FindAllocationScopeCounterString(
83 VulkanMemoryEvent::AllocationScope scope) {
84 return scope_counter_strs_id_[static_cast<size_t>(scope)];
85 }
86
FindMemoryTypeCounterString(uint32_t memory_type,DeviceCounterType counter_type)87 StringId VulkanMemoryTracker::FindMemoryTypeCounterString(
88 uint32_t memory_type,
89 DeviceCounterType counter_type) {
90 StringId res = kNullStringId;
91 std::unordered_map<uint32_t, StringId>::iterator it;
92 std::string type_counter_str;
93 switch (counter_type) {
94 case DeviceCounterType::kAllocationCounter:
95 it = memory_type_allocation_counter_string_map_.find(memory_type);
96 if (it == memory_type_allocation_counter_string_map_.end()) {
97 type_counter_str = vulkan_device_memory_counter_str_ +
98 std::to_string(memory_type) + ".allocation";
99 res = context_->storage->InternString(base::StringView(
100 type_counter_str.c_str(), type_counter_str.length()));
101 memory_type_allocation_counter_string_map_.emplace(memory_type, res);
102 } else {
103 res = it->second;
104 }
105 break;
106 case DeviceCounterType::kBindCounter:
107 it = memory_type_bind_counter_string_map_.find(memory_type);
108 if (it == memory_type_bind_counter_string_map_.end()) {
109 type_counter_str = vulkan_device_memory_counter_str_ +
110 std::to_string(memory_type) + ".bind";
111 res = context_->storage->InternString(base::StringView(
112 type_counter_str.c_str(), type_counter_str.length()));
113 memory_type_bind_counter_string_map_.emplace(memory_type, res);
114 } else {
115 res = it->second;
116 }
117 break;
118 }
119 return res;
120 }
121
122 } // namespace trace_processor
123 } // namespace perfetto
124