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/gpu_event_parser.h"
18
19 #include <cinttypes>
20
21 #include "perfetto/ext/base/utils.h"
22 #include "perfetto/protozero/field.h"
23 #include "src/trace_processor/importers/common/args_tracker.h"
24 #include "src/trace_processor/importers/common/event_tracker.h"
25 #include "src/trace_processor/importers/common/process_tracker.h"
26 #include "src/trace_processor/importers/common/slice_tracker.h"
27 #include "src/trace_processor/importers/common/track_tracker.h"
28 #include "src/trace_processor/storage/trace_storage.h"
29 #include "src/trace_processor/types/trace_processor_context.h"
30
31 #include "protos/perfetto/common/gpu_counter_descriptor.pbzero.h"
32 #include "protos/perfetto/trace/android/graphics_frame_event.pbzero.h"
33 #include "protos/perfetto/trace/gpu/gpu_counter_event.pbzero.h"
34 #include "protos/perfetto/trace/gpu/gpu_log.pbzero.h"
35 #include "protos/perfetto/trace/gpu/gpu_render_stage_event.pbzero.h"
36 #include "protos/perfetto/trace/gpu/vulkan_api_event.pbzero.h"
37 #include "protos/perfetto/trace/gpu/vulkan_memory_event.pbzero.h"
38 #include "protos/perfetto/trace/interned_data/interned_data.pbzero.h"
39
40 namespace perfetto {
41 namespace trace_processor {
42
43 namespace {
44
45 // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkObjectType.html
46 typedef enum VkObjectType {
47 VK_OBJECT_TYPE_UNKNOWN = 0,
48 VK_OBJECT_TYPE_INSTANCE = 1,
49 VK_OBJECT_TYPE_PHYSICAL_DEVICE = 2,
50 VK_OBJECT_TYPE_DEVICE = 3,
51 VK_OBJECT_TYPE_QUEUE = 4,
52 VK_OBJECT_TYPE_SEMAPHORE = 5,
53 VK_OBJECT_TYPE_COMMAND_BUFFER = 6,
54 VK_OBJECT_TYPE_FENCE = 7,
55 VK_OBJECT_TYPE_DEVICE_MEMORY = 8,
56 VK_OBJECT_TYPE_BUFFER = 9,
57 VK_OBJECT_TYPE_IMAGE = 10,
58 VK_OBJECT_TYPE_EVENT = 11,
59 VK_OBJECT_TYPE_QUERY_POOL = 12,
60 VK_OBJECT_TYPE_BUFFER_VIEW = 13,
61 VK_OBJECT_TYPE_IMAGE_VIEW = 14,
62 VK_OBJECT_TYPE_SHADER_MODULE = 15,
63 VK_OBJECT_TYPE_PIPELINE_CACHE = 16,
64 VK_OBJECT_TYPE_PIPELINE_LAYOUT = 17,
65 VK_OBJECT_TYPE_RENDER_PASS = 18,
66 VK_OBJECT_TYPE_PIPELINE = 19,
67 VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT = 20,
68 VK_OBJECT_TYPE_SAMPLER = 21,
69 VK_OBJECT_TYPE_DESCRIPTOR_POOL = 22,
70 VK_OBJECT_TYPE_DESCRIPTOR_SET = 23,
71 VK_OBJECT_TYPE_FRAMEBUFFER = 24,
72 VK_OBJECT_TYPE_COMMAND_POOL = 25,
73 VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION = 1000156000,
74 VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE = 1000085000,
75 VK_OBJECT_TYPE_SURFACE_KHR = 1000000000,
76 VK_OBJECT_TYPE_SWAPCHAIN_KHR = 1000001000,
77 VK_OBJECT_TYPE_DISPLAY_KHR = 1000002000,
78 VK_OBJECT_TYPE_DISPLAY_MODE_KHR = 1000002001,
79 VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT = 1000011000,
80 VK_OBJECT_TYPE_OBJECT_TABLE_NVX = 1000086000,
81 VK_OBJECT_TYPE_INDIRECT_COMMANDS_LAYOUT_NVX = 1000086001,
82 VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT = 1000128000,
83 VK_OBJECT_TYPE_VALIDATION_CACHE_EXT = 1000160000,
84 VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_NV = 1000165000,
85 VK_OBJECT_TYPE_PERFORMANCE_CONFIGURATION_INTEL = 1000210000,
86 VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE_KHR =
87 VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE,
88 VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION_KHR =
89 VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION,
90 VK_OBJECT_TYPE_MAX_ENUM = 0x7FFFFFFF
91 } VkObjectType;
92
93 } // anonymous namespace
94
GpuEventParser(TraceProcessorContext * context)95 GpuEventParser::GpuEventParser(TraceProcessorContext* context)
96 : context_(context),
97 vulkan_memory_tracker_(context),
98 description_id_(context->storage->InternString("description")),
99 gpu_render_stage_scope_id_(
100 context->storage->InternString("gpu_render_stage")),
101 gpu_log_track_name_id_(context_->storage->InternString("GPU Log")),
102 gpu_log_scope_id_(context_->storage->InternString("gpu_log")),
103 tag_id_(context_->storage->InternString("tag")),
104 log_message_id_(context->storage->InternString("message")),
105 log_severity_ids_{{context_->storage->InternString("UNSPECIFIED"),
106 context_->storage->InternString("VERBOSE"),
107 context_->storage->InternString("DEBUG"),
108 context_->storage->InternString("INFO"),
109 context_->storage->InternString("WARNING"),
110 context_->storage->InternString("ERROR"),
111 context_->storage->InternString(
112 "UNKNOWN_SEVERITY") /* must be last */}},
113 vk_event_track_id_(context->storage->InternString("Vulkan Events")),
114 vk_event_scope_id_(context->storage->InternString("vulkan_events")),
115 vk_queue_submit_id_(context->storage->InternString("vkQueueSubmit")),
116 gpu_mem_total_name_id_(context->storage->InternString("GPU Memory")),
117 gpu_mem_total_unit_id_(context->storage->InternString(
118 std::to_string(protos::pbzero::GpuCounterDescriptor::BYTE).c_str())),
119 gpu_mem_total_global_desc_id_(context->storage->InternString(
120 "Total GPU memory used by the entire system")),
121 gpu_mem_total_proc_desc_id_(context->storage->InternString(
122 "Total GPU memory used by this process")) {}
123
ParseGpuCounterEvent(int64_t ts,ConstBytes blob)124 void GpuEventParser::ParseGpuCounterEvent(int64_t ts, ConstBytes blob) {
125 protos::pbzero::GpuCounterEvent::Decoder event(blob.data, blob.size);
126
127 protos::pbzero::GpuCounterDescriptor::Decoder descriptor(
128 event.counter_descriptor());
129 // Add counter spec to ID map.
130 for (auto it = descriptor.specs(); it; ++it) {
131 protos::pbzero::GpuCounterDescriptor_GpuCounterSpec::Decoder spec(*it);
132 if (!spec.has_counter_id()) {
133 PERFETTO_ELOG("Counter spec missing counter id");
134 context_->storage->IncrementStats(stats::gpu_counters_invalid_spec);
135 continue;
136 }
137 if (!spec.has_name()) {
138 context_->storage->IncrementStats(stats::gpu_counters_invalid_spec);
139 continue;
140 }
141
142 auto counter_id = spec.counter_id();
143 auto name = spec.name();
144 if (gpu_counter_track_ids_.find(counter_id) ==
145 gpu_counter_track_ids_.end()) {
146 auto desc = spec.description();
147
148 StringId unit_id = kNullStringId;
149 if (spec.has_numerator_units() || spec.has_denominator_units()) {
150 char buffer[1024];
151 base::StringWriter unit(buffer, sizeof(buffer));
152 for (auto numer = spec.numerator_units(); numer; ++numer) {
153 if (unit.pos()) {
154 unit.AppendChar(':');
155 }
156 unit.AppendInt(*numer);
157 }
158 char sep = '/';
159 for (auto denom = spec.denominator_units(); denom; ++denom) {
160 unit.AppendChar(sep);
161 unit.AppendInt(*denom);
162 sep = ':';
163 }
164 unit_id = context_->storage->InternString(unit.GetStringView());
165 }
166
167 auto name_id = context_->storage->InternString(name);
168 auto desc_id = context_->storage->InternString(desc);
169 auto track_id = context_->track_tracker->CreateGpuCounterTrack(
170 name_id, 0 /* gpu_id */, desc_id, unit_id);
171 gpu_counter_track_ids_.emplace(counter_id, track_id);
172 if (spec.has_groups()) {
173 for (auto group = spec.groups(); group; ++group) {
174 tables::GpuCounterGroupTable::Row row;
175 row.group_id = *group;
176 row.track_id = track_id;
177 context_->storage->mutable_gpu_counter_group_table()->Insert(row);
178 }
179 } else {
180 tables::GpuCounterGroupTable::Row row;
181 row.group_id = protos::pbzero::GpuCounterDescriptor::UNCLASSIFIED;
182 row.track_id = track_id;
183 context_->storage->mutable_gpu_counter_group_table()->Insert(row);
184 }
185 } else {
186 // Either counter spec was repeated or it came after counter data.
187 PERFETTO_ELOG("Duplicated counter spec found. (counter_id=%d, name=%s)",
188 counter_id, name.ToStdString().c_str());
189 context_->storage->IncrementStats(stats::gpu_counters_invalid_spec);
190 }
191 }
192
193 for (auto it = event.counters(); it; ++it) {
194 protos::pbzero::GpuCounterEvent_GpuCounter::Decoder counter(*it);
195 if (counter.has_counter_id() &&
196 (counter.has_int_value() || counter.has_double_value())) {
197 auto counter_id = counter.counter_id();
198 // Check missing counter_id
199 if (gpu_counter_track_ids_.find(counter_id) ==
200 gpu_counter_track_ids_.end()) {
201 continue;
202 }
203 double counter_val = counter.has_int_value()
204 ? static_cast<double>(counter.int_value())
205 : counter.double_value();
206 context_->event_tracker->PushCounter(ts, counter_val,
207 gpu_counter_track_ids_[counter_id]);
208 }
209 }
210 }
211
GetFullStageName(PacketSequenceStateGeneration * sequence_state,const protos::pbzero::GpuRenderStageEvent_Decoder & event) const212 const StringId GpuEventParser::GetFullStageName(
213 PacketSequenceStateGeneration* sequence_state,
214 const protos::pbzero::GpuRenderStageEvent_Decoder& event) const {
215 StringId stage_name;
216 if (event.has_stage_iid()) {
217 auto stage_iid = event.stage_iid();
218 auto* decoder = sequence_state->LookupInternedMessage<
219 protos::pbzero::InternedData::kGpuSpecificationsFieldNumber,
220 protos::pbzero::InternedGpuRenderStageSpecification>(stage_iid);
221 if (!decoder) {
222 return kNullStringId;
223 }
224 stage_name = context_->storage->InternString(decoder->name());
225 } else {
226 uint64_t stage_id = static_cast<uint64_t>(event.stage_id());
227
228 if (stage_id < gpu_render_stage_ids_.size()) {
229 stage_name = gpu_render_stage_ids_[static_cast<size_t>(stage_id)].first;
230 } else {
231 base::StackString<64> name("render stage(%" PRIu64 ")", stage_id);
232 stage_name = context_->storage->InternString(name.string_view());
233 }
234 }
235 return stage_name;
236 }
237
238 /**
239 * Create a GPU render stage track based
240 * GpuRenderStageEvent.Specifications.Description.
241 */
InsertGpuTrack(const protos::pbzero::GpuRenderStageEvent_Specifications_Description_Decoder & hw_queue)242 void GpuEventParser::InsertGpuTrack(
243 const protos::pbzero::
244 GpuRenderStageEvent_Specifications_Description_Decoder& hw_queue) {
245 StringId track_name = context_->storage->InternString(hw_queue.name());
246 if (gpu_hw_queue_counter_ >= gpu_hw_queue_ids_.size() ||
247 !gpu_hw_queue_ids_[gpu_hw_queue_counter_].has_value()) {
248 tables::GpuTrackTable::Row track(track_name);
249 track.scope = gpu_render_stage_scope_id_;
250 track.description = context_->storage->InternString(hw_queue.description());
251 if (gpu_hw_queue_counter_ >= gpu_hw_queue_ids_.size()) {
252 gpu_hw_queue_ids_.emplace_back(
253 context_->track_tracker->InternGpuTrack(track));
254 } else {
255 // If a gpu_render_stage_event is received before the specification, it is
256 // possible that the slot has already been allocated.
257 gpu_hw_queue_ids_[gpu_hw_queue_counter_] =
258 context_->track_tracker->InternGpuTrack(track);
259 }
260 } else {
261 // If a gpu_render_stage_event is received before the specification, a track
262 // will be automatically generated. In that case, update the name and
263 // description.
264 auto track_id = gpu_hw_queue_ids_[gpu_hw_queue_counter_];
265 if (track_id.has_value()) {
266 auto row = context_->storage->mutable_gpu_track_table()
267 ->id()
268 .IndexOf(track_id.value())
269 .value();
270 context_->storage->mutable_gpu_track_table()->mutable_name()->Set(
271 row, track_name);
272 context_->storage->mutable_gpu_track_table()->mutable_description()->Set(
273 row, context_->storage->InternString(hw_queue.description()));
274 } else {
275 tables::GpuTrackTable::Row track(track_name);
276 track.scope = gpu_render_stage_scope_id_;
277 track.description =
278 context_->storage->InternString(hw_queue.description());
279 }
280 }
281 ++gpu_hw_queue_counter_;
282 }
FindDebugName(int32_t vk_object_type,uint64_t vk_handle) const283 std::optional<std::string> GpuEventParser::FindDebugName(
284 int32_t vk_object_type,
285 uint64_t vk_handle) const {
286 auto map = debug_marker_names_.find(vk_object_type);
287 if (map == debug_marker_names_.end()) {
288 return std::nullopt;
289 }
290
291 auto name = map->second.find(vk_handle);
292 if (name == map->second.end()) {
293 return std::nullopt;
294 } else {
295 return name->second;
296 }
297 }
298
ParseRenderSubpasses(const protos::pbzero::GpuRenderStageEvent_Decoder & event) const299 const StringId GpuEventParser::ParseRenderSubpasses(
300 const protos::pbzero::GpuRenderStageEvent_Decoder& event) const {
301 if (!event.has_render_subpass_index_mask()) {
302 return kNullStringId;
303 }
304 char buf[256];
305 base::StringWriter writer(buf, sizeof(buf));
306 uint32_t bit_index = 0;
307 bool first = true;
308 for (auto it = event.render_subpass_index_mask(); it; ++it) {
309 auto subpasses_bits = *it;
310 do {
311 if ((subpasses_bits & 1) != 0) {
312 if (!first) {
313 writer.AppendChar(',');
314 }
315 first = false;
316 writer.AppendUnsignedInt(bit_index);
317 }
318 subpasses_bits >>= 1;
319 ++bit_index;
320 } while (subpasses_bits != 0);
321 // Round up to the next multiple of 64.
322 bit_index = ((bit_index - 1) / 64 + 1) * 64;
323 }
324 return context_->storage->InternString(writer.GetStringView());
325 }
326
ParseGpuRenderStageEvent(int64_t ts,PacketSequenceStateGeneration * sequence_state,ConstBytes blob)327 void GpuEventParser::ParseGpuRenderStageEvent(
328 int64_t ts,
329 PacketSequenceStateGeneration* sequence_state,
330 ConstBytes blob) {
331 protos::pbzero::GpuRenderStageEvent::Decoder event(blob.data, blob.size);
332
333 if (event.has_specifications()) {
334 protos::pbzero::GpuRenderStageEvent_Specifications::Decoder spec(
335 event.specifications().data, event.specifications().size);
336 for (auto it = spec.hw_queue(); it; ++it) {
337 protos::pbzero::GpuRenderStageEvent_Specifications_Description::Decoder
338 hw_queue(*it);
339 if (hw_queue.has_name()) {
340 InsertGpuTrack(hw_queue);
341 }
342 }
343 for (auto it = spec.stage(); it; ++it) {
344 protos::pbzero::GpuRenderStageEvent_Specifications_Description::Decoder
345 stage(*it);
346 if (stage.has_name()) {
347 gpu_render_stage_ids_.emplace_back(std::make_pair(
348 context_->storage->InternString(stage.name()),
349 context_->storage->InternString(stage.description())));
350 }
351 }
352 }
353
354 auto args_callback = [this, &event,
355 sequence_state](ArgsTracker::BoundInserter* inserter) {
356 if (event.has_stage_iid()) {
357 size_t stage_iid = static_cast<size_t>(event.stage_iid());
358 auto* decoder = sequence_state->LookupInternedMessage<
359 protos::pbzero::InternedData::kGpuSpecificationsFieldNumber,
360 protos::pbzero::InternedGpuRenderStageSpecification>(stage_iid);
361 if (decoder) {
362 // TODO: Add RenderStageCategory to gpu_slice table.
363 inserter->AddArg(description_id_,
364 Variadic::String(context_->storage->InternString(
365 decoder->description())));
366 }
367 } else if (event.has_stage_id()) {
368 size_t stage_id = static_cast<size_t>(event.stage_id());
369 if (stage_id < gpu_render_stage_ids_.size()) {
370 auto description = gpu_render_stage_ids_[stage_id].second;
371 if (description != kNullStringId) {
372 inserter->AddArg(description_id_, Variadic::String(description));
373 }
374 }
375 }
376 for (auto it = event.extra_data(); it; ++it) {
377 protos::pbzero::GpuRenderStageEvent_ExtraData_Decoder datum(*it);
378 StringId name_id = context_->storage->InternString(datum.name());
379 StringId value = context_->storage->InternString(
380 datum.has_value() ? datum.value() : base::StringView());
381 inserter->AddArg(name_id, Variadic::String(value));
382 }
383 };
384
385 if (event.has_event_id()) {
386 TrackId track_id;
387 uint64_t hw_queue_id = 0;
388 if (event.has_hw_queue_iid()) {
389 hw_queue_id = event.hw_queue_iid();
390 auto* decoder = sequence_state->LookupInternedMessage<
391 protos::pbzero::InternedData::kGpuSpecificationsFieldNumber,
392 protos::pbzero::InternedGpuRenderStageSpecification>(hw_queue_id);
393 if (!decoder) {
394 // Skip
395 return;
396 }
397 // TODO: Add RenderStageCategory to gpu_track table.
398 tables::GpuTrackTable::Row track(
399 context_->storage->InternString(decoder->name()));
400 track.scope = gpu_render_stage_scope_id_;
401 track.description =
402 context_->storage->InternString(decoder->description());
403 track_id = context_->track_tracker->InternGpuTrack(track);
404 } else {
405 uint32_t id = static_cast<uint32_t>(event.hw_queue_id());
406 if (id < gpu_hw_queue_ids_.size() && gpu_hw_queue_ids_[id].has_value()) {
407 track_id = gpu_hw_queue_ids_[id].value();
408 } else {
409 // If the event has a hw_queue_id that does not have a Specification,
410 // create a new track for it.
411 char buf[128];
412 base::StringWriter writer(buf, sizeof(buf));
413 writer.AppendLiteral("Unknown GPU Queue ");
414 if (id > 1024) {
415 // We don't expect this to happen, but just in case there is a corrupt
416 // packet, make sure we don't allocate a ridiculous amount of memory.
417 id = 1024;
418 context_->storage->IncrementStats(
419 stats::gpu_render_stage_parser_errors);
420 PERFETTO_ELOG("Invalid hw_queue_id.");
421 } else {
422 writer.AppendInt(event.hw_queue_id());
423 }
424 StringId track_name =
425 context_->storage->InternString(writer.GetStringView());
426 tables::GpuTrackTable::Row track(track_name);
427 track.scope = gpu_render_stage_scope_id_;
428 track_id = context_->track_tracker->InternGpuTrack(track);
429 gpu_hw_queue_ids_.resize(id + 1);
430 gpu_hw_queue_ids_[id] = track_id;
431 }
432 hw_queue_id = id;
433 }
434
435 auto render_target_name =
436 FindDebugName(VK_OBJECT_TYPE_FRAMEBUFFER, event.render_target_handle());
437 auto render_target_name_id = render_target_name.has_value()
438 ? context_->storage->InternString(
439 render_target_name.value().c_str())
440 : kNullStringId;
441 auto render_pass_name =
442 FindDebugName(VK_OBJECT_TYPE_RENDER_PASS, event.render_pass_handle());
443 auto render_pass_name_id =
444 render_pass_name.has_value()
445 ? context_->storage->InternString(render_pass_name.value().c_str())
446 : kNullStringId;
447 auto command_buffer_name = FindDebugName(VK_OBJECT_TYPE_COMMAND_BUFFER,
448 event.command_buffer_handle());
449 auto command_buffer_name_id = command_buffer_name.has_value()
450 ? context_->storage->InternString(
451 command_buffer_name.value().c_str())
452 : kNullStringId;
453
454 tables::GpuSliceTable::Row row;
455 row.ts = ts;
456 row.track_id = track_id;
457 row.name = GetFullStageName(sequence_state, event);
458 row.dur = static_cast<int64_t>(event.duration());
459 // TODO: Create table for graphics context and lookup
460 // InternedGraphicsContext.
461 row.context_id = static_cast<int64_t>(event.context());
462 row.render_target = static_cast<int64_t>(event.render_target_handle());
463 row.render_target_name = render_target_name_id;
464 row.render_pass = static_cast<int64_t>(event.render_pass_handle());
465 row.render_pass_name = render_pass_name_id;
466 row.render_subpasses = ParseRenderSubpasses(event);
467 row.command_buffer = static_cast<int64_t>(event.command_buffer_handle());
468 row.command_buffer_name = command_buffer_name_id;
469 row.submission_id = event.submission_id();
470 row.hw_queue_id = static_cast<int64_t>(hw_queue_id);
471
472 context_->slice_tracker->ScopedTyped(
473 context_->storage->mutable_gpu_slice_table(), row, args_callback);
474 }
475 }
476
UpdateVulkanMemoryAllocationCounters(UniquePid upid,const VulkanMemoryEvent::Decoder & event)477 void GpuEventParser::UpdateVulkanMemoryAllocationCounters(
478 UniquePid upid,
479 const VulkanMemoryEvent::Decoder& event) {
480 StringId track_str_id = kNullStringId;
481 TrackId track = kInvalidTrackId;
482 auto allocation_scope = VulkanMemoryEvent::SCOPE_UNSPECIFIED;
483 uint32_t memory_type = std::numeric_limits<uint32_t>::max();
484 switch (event.source()) {
485 case VulkanMemoryEvent::SOURCE_DRIVER:
486 allocation_scope = static_cast<VulkanMemoryEvent::AllocationScope>(
487 event.allocation_scope());
488 if (allocation_scope == VulkanMemoryEvent::SCOPE_UNSPECIFIED)
489 return;
490 switch (event.operation()) {
491 case VulkanMemoryEvent::OP_CREATE:
492 vulkan_driver_memory_counters_[allocation_scope] +=
493 event.memory_size();
494 break;
495 case VulkanMemoryEvent::OP_DESTROY:
496 vulkan_driver_memory_counters_[allocation_scope] -=
497 event.memory_size();
498 break;
499 case VulkanMemoryEvent::OP_UNSPECIFIED:
500 case VulkanMemoryEvent::OP_BIND:
501 case VulkanMemoryEvent::OP_DESTROY_BOUND:
502 case VulkanMemoryEvent::OP_ANNOTATIONS:
503 return;
504 }
505 track_str_id = vulkan_memory_tracker_.FindAllocationScopeCounterString(
506 allocation_scope);
507 track = context_->track_tracker->InternProcessCounterTrack(track_str_id,
508 upid);
509 context_->event_tracker->PushCounter(
510 event.timestamp(),
511 static_cast<double>(vulkan_driver_memory_counters_[allocation_scope]),
512 track);
513 break;
514
515 case VulkanMemoryEvent::SOURCE_DEVICE_MEMORY:
516 memory_type = static_cast<uint32_t>(event.memory_type());
517 switch (event.operation()) {
518 case VulkanMemoryEvent::OP_CREATE:
519 vulkan_device_memory_counters_allocate_[memory_type] +=
520 event.memory_size();
521 break;
522 case VulkanMemoryEvent::OP_DESTROY:
523 vulkan_device_memory_counters_allocate_[memory_type] -=
524 event.memory_size();
525 break;
526 case VulkanMemoryEvent::OP_UNSPECIFIED:
527 case VulkanMemoryEvent::OP_BIND:
528 case VulkanMemoryEvent::OP_DESTROY_BOUND:
529 case VulkanMemoryEvent::OP_ANNOTATIONS:
530 return;
531 }
532 track_str_id = vulkan_memory_tracker_.FindMemoryTypeCounterString(
533 memory_type,
534 VulkanMemoryTracker::DeviceCounterType::kAllocationCounter);
535 track = context_->track_tracker->InternProcessCounterTrack(track_str_id,
536 upid);
537 context_->event_tracker->PushCounter(
538 event.timestamp(),
539 static_cast<double>(
540 vulkan_device_memory_counters_allocate_[memory_type]),
541 track);
542 break;
543
544 case VulkanMemoryEvent::SOURCE_BUFFER:
545 case VulkanMemoryEvent::SOURCE_IMAGE:
546 memory_type = static_cast<uint32_t>(event.memory_type());
547 switch (event.operation()) {
548 case VulkanMemoryEvent::OP_BIND:
549 vulkan_device_memory_counters_bind_[memory_type] +=
550 event.memory_size();
551 break;
552 case VulkanMemoryEvent::OP_DESTROY_BOUND:
553 vulkan_device_memory_counters_bind_[memory_type] -=
554 event.memory_size();
555 break;
556 case VulkanMemoryEvent::OP_UNSPECIFIED:
557 case VulkanMemoryEvent::OP_CREATE:
558 case VulkanMemoryEvent::OP_DESTROY:
559 case VulkanMemoryEvent::OP_ANNOTATIONS:
560 return;
561 }
562 track_str_id = vulkan_memory_tracker_.FindMemoryTypeCounterString(
563 memory_type, VulkanMemoryTracker::DeviceCounterType::kBindCounter);
564 track = context_->track_tracker->InternProcessCounterTrack(track_str_id,
565 upid);
566 context_->event_tracker->PushCounter(
567 event.timestamp(),
568 static_cast<double>(vulkan_device_memory_counters_bind_[memory_type]),
569 track);
570 break;
571 case VulkanMemoryEvent::SOURCE_UNSPECIFIED:
572 case VulkanMemoryEvent::SOURCE_DEVICE:
573 return;
574 }
575 }
576
ParseVulkanMemoryEvent(PacketSequenceStateGeneration * sequence_state,ConstBytes blob)577 void GpuEventParser::ParseVulkanMemoryEvent(
578 PacketSequenceStateGeneration* sequence_state,
579 ConstBytes blob) {
580 using protos::pbzero::InternedData;
581 VulkanMemoryEvent::Decoder vulkan_memory_event(blob.data, blob.size);
582 tables::VulkanMemoryAllocationsTable::Row vulkan_memory_event_row;
583 vulkan_memory_event_row.source = vulkan_memory_tracker_.FindSourceString(
584 static_cast<VulkanMemoryEvent::Source>(vulkan_memory_event.source()));
585 vulkan_memory_event_row.operation =
586 vulkan_memory_tracker_.FindOperationString(
587 static_cast<VulkanMemoryEvent::Operation>(
588 vulkan_memory_event.operation()));
589 vulkan_memory_event_row.timestamp = vulkan_memory_event.timestamp();
590 vulkan_memory_event_row.upid =
591 context_->process_tracker->GetOrCreateProcess(vulkan_memory_event.pid());
592 if (vulkan_memory_event.has_device())
593 vulkan_memory_event_row.device =
594 static_cast<int64_t>(vulkan_memory_event.device());
595 if (vulkan_memory_event.has_device_memory())
596 vulkan_memory_event_row.device_memory =
597 static_cast<int64_t>(vulkan_memory_event.device_memory());
598 if (vulkan_memory_event.has_heap())
599 vulkan_memory_event_row.heap = vulkan_memory_event.heap();
600 if (vulkan_memory_event.has_memory_type())
601 vulkan_memory_event_row.memory_type = vulkan_memory_event.memory_type();
602 if (vulkan_memory_event.has_caller_iid()) {
603 vulkan_memory_event_row.function_name =
604 vulkan_memory_tracker_
605 .GetInternedString<InternedData::kFunctionNamesFieldNumber>(
606 sequence_state,
607 static_cast<uint64_t>(vulkan_memory_event.caller_iid()));
608 }
609 if (vulkan_memory_event.has_object_handle())
610 vulkan_memory_event_row.object_handle =
611 static_cast<int64_t>(vulkan_memory_event.object_handle());
612 if (vulkan_memory_event.has_memory_address())
613 vulkan_memory_event_row.memory_address =
614 static_cast<int64_t>(vulkan_memory_event.memory_address());
615 if (vulkan_memory_event.has_memory_size())
616 vulkan_memory_event_row.memory_size =
617 static_cast<int64_t>(vulkan_memory_event.memory_size());
618 if (vulkan_memory_event.has_allocation_scope())
619 vulkan_memory_event_row.scope =
620 vulkan_memory_tracker_.FindAllocationScopeString(
621 static_cast<VulkanMemoryEvent::AllocationScope>(
622 vulkan_memory_event.allocation_scope()));
623
624 UpdateVulkanMemoryAllocationCounters(vulkan_memory_event_row.upid.value(),
625 vulkan_memory_event);
626
627 auto* allocs = context_->storage->mutable_vulkan_memory_allocations_table();
628 VulkanAllocId id = allocs->Insert(vulkan_memory_event_row).id;
629
630 if (vulkan_memory_event.has_annotations()) {
631 auto inserter = context_->args_tracker->AddArgsTo(id);
632
633 for (auto it = vulkan_memory_event.annotations(); it; ++it) {
634 protos::pbzero::VulkanMemoryEventAnnotation::Decoder annotation(*it);
635
636 auto key_id =
637 vulkan_memory_tracker_
638 .GetInternedString<InternedData::kVulkanMemoryKeysFieldNumber>(
639 sequence_state, static_cast<uint64_t>(annotation.key_iid()));
640
641 if (annotation.has_int_value()) {
642 inserter.AddArg(key_id, Variadic::Integer(annotation.int_value()));
643 } else if (annotation.has_double_value()) {
644 inserter.AddArg(key_id, Variadic::Real(annotation.double_value()));
645 } else if (annotation.has_string_iid()) {
646 auto string_id =
647 vulkan_memory_tracker_
648 .GetInternedString<InternedData::kVulkanMemoryKeysFieldNumber>(
649 sequence_state,
650 static_cast<uint64_t>(annotation.string_iid()));
651
652 inserter.AddArg(key_id, Variadic::String(string_id));
653 }
654 }
655 }
656 }
657
ParseGpuLog(int64_t ts,ConstBytes blob)658 void GpuEventParser::ParseGpuLog(int64_t ts, ConstBytes blob) {
659 protos::pbzero::GpuLog::Decoder event(blob.data, blob.size);
660
661 tables::GpuTrackTable::Row track(gpu_log_track_name_id_);
662 track.scope = gpu_log_scope_id_;
663 TrackId track_id = context_->track_tracker->InternGpuTrack(track);
664
665 auto args_callback = [this, &event](ArgsTracker::BoundInserter* inserter) {
666 if (event.has_tag()) {
667 inserter->AddArg(
668 tag_id_,
669 Variadic::String(context_->storage->InternString(event.tag())));
670 }
671 if (event.has_log_message()) {
672 inserter->AddArg(log_message_id_,
673 Variadic::String(context_->storage->InternString(
674 event.log_message())));
675 }
676 };
677
678 auto severity = static_cast<size_t>(event.severity());
679 StringId severity_id =
680 severity < log_severity_ids_.size()
681 ? log_severity_ids_[static_cast<size_t>(event.severity())]
682 : log_severity_ids_[log_severity_ids_.size() - 1];
683
684 tables::GpuSliceTable::Row row;
685 row.ts = ts;
686 row.track_id = track_id;
687 row.name = severity_id;
688 row.dur = 0;
689 context_->slice_tracker->ScopedTyped(
690 context_->storage->mutable_gpu_slice_table(), row, args_callback);
691 }
692
ParseVulkanApiEvent(int64_t ts,ConstBytes blob)693 void GpuEventParser::ParseVulkanApiEvent(int64_t ts, ConstBytes blob) {
694 protos::pbzero::VulkanApiEvent::Decoder vk_event(blob.data, blob.size);
695 if (vk_event.has_vk_debug_utils_object_name()) {
696 protos::pbzero::VulkanApiEvent_VkDebugUtilsObjectName::Decoder event(
697 vk_event.vk_debug_utils_object_name());
698 debug_marker_names_[event.object_type()][event.object()] =
699 event.object_name().ToStdString();
700 }
701 if (vk_event.has_vk_queue_submit()) {
702 protos::pbzero::VulkanApiEvent_VkQueueSubmit::Decoder event(
703 vk_event.vk_queue_submit());
704 // Once flow table is implemented, we can create a nice UI that link the
705 // vkQueueSubmit to GpuRenderStageEvent. For now, just add it as in a GPU
706 // track so that they can appear close to the render stage slices.
707 tables::GpuTrackTable::Row track(vk_event_track_id_);
708 track.scope = vk_event_scope_id_;
709 TrackId track_id = context_->track_tracker->InternGpuTrack(track);
710 tables::GpuSliceTable::Row row;
711 row.ts = ts;
712 row.dur = static_cast<int64_t>(event.duration_ns());
713 row.track_id = track_id;
714 row.name = vk_queue_submit_id_;
715 if (event.has_vk_command_buffers()) {
716 row.command_buffer = static_cast<int64_t>(*event.vk_command_buffers());
717 }
718 row.submission_id = event.submission_id();
719 auto args_callback = [this, &event](ArgsTracker::BoundInserter* inserter) {
720 inserter->AddArg(context_->storage->InternString("pid"),
721 Variadic::Integer(event.pid()));
722 inserter->AddArg(context_->storage->InternString("tid"),
723 Variadic::Integer(event.tid()));
724 };
725 context_->slice_tracker->ScopedTyped(
726 context_->storage->mutable_gpu_slice_table(), row, args_callback);
727 }
728 }
729
ParseGpuMemTotalEvent(int64_t ts,ConstBytes blob)730 void GpuEventParser::ParseGpuMemTotalEvent(int64_t ts, ConstBytes blob) {
731 protos::pbzero::GpuMemTotalEvent::Decoder gpu_mem_total(blob.data, blob.size);
732
733 TrackId track = kInvalidTrackId;
734 const uint32_t pid = gpu_mem_total.pid();
735 if (pid == 0) {
736 // Pid 0 is used to indicate the global total
737 track = context_->track_tracker->InternGlobalCounterTrack(
738 TrackTracker::Group::kMemory, gpu_mem_total_name_id_, {},
739 gpu_mem_total_unit_id_, gpu_mem_total_global_desc_id_);
740 } else {
741 // Process emitting the packet can be different from the pid in the event.
742 UniqueTid utid = context_->process_tracker->UpdateThread(pid, pid);
743 UniquePid upid = context_->storage->thread_table().upid()[utid].value_or(0);
744 track = context_->track_tracker->InternProcessCounterTrack(
745 gpu_mem_total_name_id_, upid, gpu_mem_total_unit_id_,
746 gpu_mem_total_proc_desc_id_);
747 }
748 context_->event_tracker->PushCounter(
749 ts, static_cast<double>(gpu_mem_total.size()), track);
750 }
751
752 } // namespace trace_processor
753 } // namespace perfetto
754