blob: ed5db0a4cdb194346c494867c5fe3e3588a1003c [file] [log] [blame]
Mohammad Reza Zakerinasabb06d1852019-09-11 14:41:36 -04001/*
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/vulkan_memory_tracker.h"
18
Mohammad Reza Zakerinasab79c16f92019-10-25 09:47:14 -040019#include "protos/perfetto/trace/interned_data/interned_data.pbzero.h"
Mohammad Reza Zakerinasabb06d1852019-09-11 14:41:36 -040020#include "src/trace_processor/process_tracker.h"
21#include "src/trace_processor/trace_processor_context.h"
22
23#include "perfetto/base/logging.h"
24
25namespace perfetto {
26namespace trace_processor {
27
28VulkanMemoryTracker::VulkanMemoryTracker(TraceProcessorContext* context)
Mohammad Reza Zakerinasab79c16f92019-10-25 09:47:14 -040029 : context_(context) {
Mohammad Reza Zakerinasabb06d1852019-09-11 14:41:36 -040030 SetupSourceAndTypeInternedStrings();
31}
32
33void VulkanMemoryTracker::SetupSourceAndTypeInternedStrings() {
34 // It seems a good idea to have the source and type of the event coded as
35 // a string id inside the Perfetto db instead of having the enum value
36 // stored. However, it seems that Perfetto only allows protobufs which are
37 // optimized for LITE_RUNTIME (removing the line results in link errors).
38 // Apparently, there is also a minimal implementation of protobuf descriptor
39 // in the code base, but it does not have the reflection to retrieve the name
40 // of the enum. More investigation is required to resolve this.
41
42 // TODO (zakerinasab): Fix and uncomment the following code to avoid
43 // hardcoding the interned strings for the source and type of memory events.
44 // const protos::pbzero::EnumDescriptor* source_descriptor =
45 // protos::pbzero::VulkanMemoryEvent::Source_descriptor();
46 // for (int i = 0; i < source_descriptor->value_count(); i++) {
47 // auto source_enum = source_descriptor->value(i);
48 // auto source_str = source_enum->name();
49 // auto str_id = context_->storage->InternString(
50 // base::StringView(source_str.c_str(), source_str.length()));
51 // source_string_map_.emplace(source_enum->number(), str_id);
52 // }
53
54 // const protos::pbzero::EnumDescriptor* type_descriptor =
55 // protos::pbzero::VulkanMemoryEvent::Type_descriptor();
56 // for (int i = 0; i < type_descriptor->value_count(); i++) {
57 // auto type_enum = type_descriptor->value(i);
58 // auto type_str = type_enum->name();
59 // auto str_id = context_->storage->InternString(
60 // base::StringView(type_str.c_str(), type_str.length()));
61 // type_string_map_.emplace(type_enum->number(), str_id);
62 // }
63
64 std::unordered_map<int, std::string> event_sources({
65 {0, "UNKNOWN_SOURCE"},
66 {1, "DEVICE"},
67 {2, "HOST"},
68 {3, "GPU_DEVICE_MEMORY"},
69 {4, "GPU_BUFFER"},
70 {5, "GPU_IMAGE"},
71 });
72 for (const auto& source : event_sources) {
73 source_string_map_.emplace(
74 source.first, context_->storage->InternString(base::StringView(
75 source.second.c_str(), source.second.length())));
76 }
77
78 std::unordered_map<int, std::string> event_types({
79 {0, "UNKNOWN_TYPE"},
80 {1, "CREATE"},
81 {2, "DESTROY"},
82 {3, "BIND"},
83 {4, "DESTROY_BOUND"},
84 {5, "ANNOTATIONS"},
85 });
86 for (const auto& type : event_types) {
87 type_string_map_.emplace(type.first,
88 context_->storage->InternString(base::StringView(
89 type.second.c_str(), type.second.length())));
90 }
91}
92
Mohammad Reza Zakerinasab79c16f92019-10-25 09:47:14 -040093StringId VulkanMemoryTracker::FindSourceString(
Mohammad Reza Zakerinasabb06d1852019-09-11 14:41:36 -040094 SourceStringId source) {
Mohammad Reza Zakerinasab79c16f92019-10-25 09:47:14 -040095 StringId res = kNullStringId;
Mohammad Reza Zakerinasabb06d1852019-09-11 14:41:36 -040096 auto it = source_string_map_.find(source);
97 if (it == source_string_map_.end()) {
98 context_->storage->IncrementStats(
99 stats::vulkan_allocations_invalid_string_id);
Mohammad Reza Zakerinasabb06d1852019-09-11 14:41:36 -0400100 return res;
101 }
102 res = it->second;
103 return res;
104}
105
Mohammad Reza Zakerinasab79c16f92019-10-25 09:47:14 -0400106StringId VulkanMemoryTracker::FindTypeString(
Mohammad Reza Zakerinasabb06d1852019-09-11 14:41:36 -0400107 SourceStringId type) {
Mohammad Reza Zakerinasab79c16f92019-10-25 09:47:14 -0400108 StringId res = kNullStringId;
Mohammad Reza Zakerinasabb06d1852019-09-11 14:41:36 -0400109 auto it = type_string_map_.find(type);
110 if (it == type_string_map_.end()) {
111 context_->storage->IncrementStats(
112 stats::vulkan_allocations_invalid_string_id);
Mohammad Reza Zakerinasabb06d1852019-09-11 14:41:36 -0400113 return res;
114 }
115 res = it->second;
116 return res;
117}
118
119} // namespace trace_processor
120} // namespace perfetto