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 char buffer[64];
202 base::StringWriter writer(buffer, sizeof(buffer));
203 writer.AppendString("gpu_counter(");
204 writer.AppendUnsignedInt(counter_id);
205 writer.AppendString(")");
206 auto name_id = context_->storage->InternString(writer.GetStringView());
207
208 TrackId track = context_->track_tracker->CreateGpuCounterTrack(
209 name_id, 0 /* gpu_id */);
210 gpu_counter_track_ids_.emplace(counter_id, track);
211 context_->storage->IncrementStats(stats::gpu_counters_missing_spec);
212 }
213 double counter_val = counter.has_int_value()
214 ? static_cast<double>(counter.int_value())
215 : counter.double_value();
216 context_->event_tracker->PushCounter(ts, counter_val,
217 gpu_counter_track_ids_[counter_id]);
218 }
219 }
220 }
221
GetFullStageName(PacketSequenceStateGeneration * sequence_state,const protos::pbzero::GpuRenderStageEvent_Decoder & event) const222 const StringId GpuEventParser::GetFullStageName(
223 PacketSequenceStateGeneration* sequence_state,
224 const protos::pbzero::GpuRenderStageEvent_Decoder& event) const {
225 StringId stage_name;
226 if (event.has_stage_iid()) {
227 auto stage_iid = event.stage_iid();
228 auto* decoder = sequence_state->LookupInternedMessage<
229 protos::pbzero::InternedData::kGpuSpecificationsFieldNumber,
230 protos::pbzero::InternedGpuRenderStageSpecification>(stage_iid);
231 if (!decoder) {
232 return kNullStringId;
233 }
234 stage_name = context_->storage->InternString(decoder->name());
235 } else {
236 uint64_t stage_id = static_cast<uint64_t>(event.stage_id());
237
238 if (stage_id < gpu_render_stage_ids_.size()) {
239 stage_name = gpu_render_stage_ids_[static_cast<size_t>(stage_id)].first;
240 } else {
241 base::StackString<64> name("render stage(%" PRIu64 ")", stage_id);
242 stage_name = context_->storage->InternString(name.string_view());
243 }
244 }
245 return stage_name;
246 }
247
248 /**
249 * Create a GPU render stage track based
250 * GpuRenderStageEvent.Specifications.Description.
251 */
InsertGpuTrack(const protos::pbzero::GpuRenderStageEvent_Specifications_Description_Decoder & hw_queue)252 void GpuEventParser::InsertGpuTrack(
253 const protos::pbzero::
254 GpuRenderStageEvent_Specifications_Description_Decoder& hw_queue) {
255 StringId track_name = context_->storage->InternString(hw_queue.name());
256 if (gpu_hw_queue_counter_ >= gpu_hw_queue_ids_.size() ||
257 !gpu_hw_queue_ids_[gpu_hw_queue_counter_].has_value()) {
258 tables::GpuTrackTable::Row track(track_name);
259 track.scope = gpu_render_stage_scope_id_;
260 track.description = context_->storage->InternString(hw_queue.description());
261 if (gpu_hw_queue_counter_ >= gpu_hw_queue_ids_.size()) {
262 gpu_hw_queue_ids_.emplace_back(
263 context_->track_tracker->InternGpuTrack(track));
264 } else {
265 // If a gpu_render_stage_event is received before the specification, it is
266 // possible that the slot has already been allocated.
267 gpu_hw_queue_ids_[gpu_hw_queue_counter_] =
268 context_->track_tracker->InternGpuTrack(track);
269 }
270 } else {
271 // If a gpu_render_stage_event is received before the specification, a track
272 // will be automatically generated. In that case, update the name and
273 // description.
274 auto track_id = gpu_hw_queue_ids_[gpu_hw_queue_counter_];
275 if (track_id.has_value()) {
276 auto row = context_->storage->mutable_gpu_track_table()
277 ->id()
278 .IndexOf(track_id.value())
279 .value();
280 context_->storage->mutable_gpu_track_table()->mutable_name()->Set(
281 row, track_name);
282 context_->storage->mutable_gpu_track_table()->mutable_description()->Set(
283 row, context_->storage->InternString(hw_queue.description()));
284 } else {
285 tables::GpuTrackTable::Row track(track_name);
286 track.scope = gpu_render_stage_scope_id_;
287 track.description =
288 context_->storage->InternString(hw_queue.description());
289 }
290 }
291 ++gpu_hw_queue_counter_;
292 }
FindDebugName(int32_t vk_object_type,uint64_t vk_handle) const293 base::Optional<std::string> GpuEventParser::FindDebugName(
294 int32_t vk_object_type,
295 uint64_t vk_handle) const {
296 auto map = debug_marker_names_.find(vk_object_type);
297 if (map == debug_marker_names_.end()) {
298 return base::nullopt;
299 }
300
301 auto name = map->second.find(vk_handle);
302 if (name == map->second.end()) {
303 return base::nullopt;
304 } else {
305 return name->second;
306 }
307 }
308
ParseRenderSubpasses(const protos::pbzero::GpuRenderStageEvent_Decoder & event) const309 const StringId GpuEventParser::ParseRenderSubpasses(
310 const protos::pbzero::GpuRenderStageEvent_Decoder& event) const {
311 if (!event.has_render_subpass_index_mask()) {
312 return kNullStringId;
313 }
314 char buf[256];
315 base::StringWriter writer(buf, sizeof(buf));
316 uint32_t bit_index = 0;
317 bool first = true;
318 for (auto it = event.render_subpass_index_mask(); it; ++it) {
319 auto subpasses_bits = *it;
320 do {
321 if ((subpasses_bits & 1) != 0) {
322 if (!first) {
323 writer.AppendChar(',');
324 }
325 first = false;
326 writer.AppendUnsignedInt(bit_index);
327 }
328 subpasses_bits >>= 1;
329 ++bit_index;
330 } while (subpasses_bits != 0);
331 // Round up to the next multiple of 64.
332 bit_index = ((bit_index - 1) / 64 + 1) * 64;
333 }
334 return context_->storage->InternString(writer.GetStringView());
335 }
336
ParseGpuRenderStageEvent(int64_t ts,PacketSequenceStateGeneration * sequence_state,ConstBytes blob)337 void GpuEventParser::ParseGpuRenderStageEvent(
338 int64_t ts,
339 PacketSequenceStateGeneration* sequence_state,
340 ConstBytes blob) {
341 protos::pbzero::GpuRenderStageEvent::Decoder event(blob.data, blob.size);
342
343 if (event.has_specifications()) {
344 protos::pbzero::GpuRenderStageEvent_Specifications::Decoder spec(
345 event.specifications().data, event.specifications().size);
346 for (auto it = spec.hw_queue(); it; ++it) {
347 protos::pbzero::GpuRenderStageEvent_Specifications_Description::Decoder
348 hw_queue(*it);
349 if (hw_queue.has_name()) {
350 InsertGpuTrack(hw_queue);
351 }
352 }
353 for (auto it = spec.stage(); it; ++it) {
354 protos::pbzero::GpuRenderStageEvent_Specifications_Description::Decoder
355 stage(*it);
356 if (stage.has_name()) {
357 gpu_render_stage_ids_.emplace_back(std::make_pair(
358 context_->storage->InternString(stage.name()),
359 context_->storage->InternString(stage.description())));
360 }
361 }
362 }
363
364 auto args_callback = [this, &event,
365 sequence_state](ArgsTracker::BoundInserter* inserter) {
366 if (event.has_stage_iid()) {
367 size_t stage_iid = static_cast<size_t>(event.stage_iid());
368 auto* decoder = sequence_state->LookupInternedMessage<
369 protos::pbzero::InternedData::kGpuSpecificationsFieldNumber,
370 protos::pbzero::InternedGpuRenderStageSpecification>(stage_iid);
371 if (decoder) {
372 // TODO: Add RenderStageCategory to gpu_slice table.
373 inserter->AddArg(description_id_,
374 Variadic::String(context_->storage->InternString(
375 decoder->description())));
376 }
377 } else if (event.has_stage_id()) {
378 size_t stage_id = static_cast<size_t>(event.stage_id());
379 if (stage_id < gpu_render_stage_ids_.size()) {
380 auto description = gpu_render_stage_ids_[stage_id].second;
381 if (description != kNullStringId) {
382 inserter->AddArg(description_id_, Variadic::String(description));
383 }
384 }
385 }
386 for (auto it = event.extra_data(); it; ++it) {
387 protos::pbzero::GpuRenderStageEvent_ExtraData_Decoder datum(*it);
388 StringId name_id = context_->storage->InternString(datum.name());
389 StringId value = context_->storage->InternString(
390 datum.has_value() ? datum.value() : base::StringView());
391 inserter->AddArg(name_id, Variadic::String(value));
392 }
393 };
394
395 if (event.has_event_id()) {
396 TrackId track_id;
397 uint64_t hw_queue_id = 0;
398 if (event.has_hw_queue_iid()) {
399 hw_queue_id = event.hw_queue_iid();
400 auto* decoder = sequence_state->LookupInternedMessage<
401 protos::pbzero::InternedData::kGpuSpecificationsFieldNumber,
402 protos::pbzero::InternedGpuRenderStageSpecification>(hw_queue_id);
403 if (!decoder) {
404 // Skip
405 return;
406 }
407 // TODO: Add RenderStageCategory to gpu_track table.
408 tables::GpuTrackTable::Row track(
409 context_->storage->InternString(decoder->name()));
410 track.scope = gpu_render_stage_scope_id_;
411 track.description =
412 context_->storage->InternString(decoder->description());
413 track_id = context_->track_tracker->InternGpuTrack(track);
414 } else {
415 uint32_t id = static_cast<uint32_t>(event.hw_queue_id());
416 if (id < gpu_hw_queue_ids_.size() && gpu_hw_queue_ids_[id].has_value()) {
417 track_id = gpu_hw_queue_ids_[id].value();
418 } else {
419 // If the event has a hw_queue_id that does not have a Specification,
420 // create a new track for it.
421 char buf[128];
422 base::StringWriter writer(buf, sizeof(buf));
423 writer.AppendLiteral("Unknown GPU Queue ");
424 if (id > 1024) {
425 // We don't expect this to happen, but just in case there is a corrupt
426 // packet, make sure we don't allocate a ridiculous amount of memory.
427 id = 1024;
428 context_->storage->IncrementStats(
429 stats::gpu_render_stage_parser_errors);
430 PERFETTO_ELOG("Invalid hw_queue_id.");
431 } else {
432 writer.AppendInt(event.hw_queue_id());
433 }
434 StringId track_name =
435 context_->storage->InternString(writer.GetStringView());
436 tables::GpuTrackTable::Row track(track_name);
437 track.scope = gpu_render_stage_scope_id_;
438 track_id = context_->track_tracker->InternGpuTrack(track);
439 gpu_hw_queue_ids_.resize(id + 1);
440 gpu_hw_queue_ids_[id] = track_id;
441 }
442 hw_queue_id = id;
443 }
444
445 auto render_target_name =
446 FindDebugName(VK_OBJECT_TYPE_FRAMEBUFFER, event.render_target_handle());
447 auto render_target_name_id = render_target_name.has_value()
448 ? context_->storage->InternString(
449 render_target_name.value().c_str())
450 : kNullStringId;
451 auto render_pass_name =
452 FindDebugName(VK_OBJECT_TYPE_RENDER_PASS, event.render_pass_handle());
453 auto render_pass_name_id =
454 render_pass_name.has_value()
455 ? context_->storage->InternString(render_pass_name.value().c_str())
456 : kNullStringId;
457 auto command_buffer_name = FindDebugName(VK_OBJECT_TYPE_COMMAND_BUFFER,
458 event.command_buffer_handle());
459 auto command_buffer_name_id = command_buffer_name.has_value()
460 ? context_->storage->InternString(
461 command_buffer_name.value().c_str())
462 : kNullStringId;
463
464 tables::GpuSliceTable::Row row;
465 row.ts = ts;
466 row.track_id = track_id;
467 row.name = GetFullStageName(sequence_state, event);
468 row.dur = static_cast<int64_t>(event.duration());
469 // TODO: Create table for graphics context and lookup
470 // InternedGraphicsContext.
471 row.context_id = static_cast<int64_t>(event.context());
472 row.render_target = static_cast<int64_t>(event.render_target_handle());
473 row.render_target_name = render_target_name_id;
474 row.render_pass = static_cast<int64_t>(event.render_pass_handle());
475 row.render_pass_name = render_pass_name_id;
476 row.render_subpasses = ParseRenderSubpasses(event);
477 row.command_buffer = static_cast<int64_t>(event.command_buffer_handle());
478 row.command_buffer_name = command_buffer_name_id;
479 row.submission_id = event.submission_id();
480 row.hw_queue_id = static_cast<int64_t>(hw_queue_id);
481
482 context_->slice_tracker->ScopedTyped(
483 context_->storage->mutable_gpu_slice_table(), row, args_callback);
484 }
485 }
486
UpdateVulkanMemoryAllocationCounters(UniquePid upid,const VulkanMemoryEvent::Decoder & event)487 void GpuEventParser::UpdateVulkanMemoryAllocationCounters(
488 UniquePid upid,
489 const VulkanMemoryEvent::Decoder& event) {
490 StringId track_str_id = kNullStringId;
491 TrackId track = kInvalidTrackId;
492 auto allocation_scope = VulkanMemoryEvent::SCOPE_UNSPECIFIED;
493 uint32_t memory_type = std::numeric_limits<uint32_t>::max();
494 switch (event.source()) {
495 case VulkanMemoryEvent::SOURCE_DRIVER:
496 allocation_scope = static_cast<VulkanMemoryEvent::AllocationScope>(
497 event.allocation_scope());
498 if (allocation_scope == VulkanMemoryEvent::SCOPE_UNSPECIFIED)
499 return;
500 switch (event.operation()) {
501 case VulkanMemoryEvent::OP_CREATE:
502 vulkan_driver_memory_counters_[allocation_scope] +=
503 event.memory_size();
504 break;
505 case VulkanMemoryEvent::OP_DESTROY:
506 vulkan_driver_memory_counters_[allocation_scope] -=
507 event.memory_size();
508 break;
509 case VulkanMemoryEvent::OP_UNSPECIFIED:
510 case VulkanMemoryEvent::OP_BIND:
511 case VulkanMemoryEvent::OP_DESTROY_BOUND:
512 case VulkanMemoryEvent::OP_ANNOTATIONS:
513 return;
514 }
515 track_str_id = vulkan_memory_tracker_.FindAllocationScopeCounterString(
516 allocation_scope);
517 track = context_->track_tracker->InternProcessCounterTrack(track_str_id,
518 upid);
519 context_->event_tracker->PushCounter(
520 event.timestamp(),
521 static_cast<double>(vulkan_driver_memory_counters_[allocation_scope]),
522 track);
523 break;
524
525 case VulkanMemoryEvent::SOURCE_DEVICE_MEMORY:
526 memory_type = static_cast<uint32_t>(event.memory_type());
527 switch (event.operation()) {
528 case VulkanMemoryEvent::OP_CREATE:
529 vulkan_device_memory_counters_allocate_[memory_type] +=
530 event.memory_size();
531 break;
532 case VulkanMemoryEvent::OP_DESTROY:
533 vulkan_device_memory_counters_allocate_[memory_type] -=
534 event.memory_size();
535 break;
536 case VulkanMemoryEvent::OP_UNSPECIFIED:
537 case VulkanMemoryEvent::OP_BIND:
538 case VulkanMemoryEvent::OP_DESTROY_BOUND:
539 case VulkanMemoryEvent::OP_ANNOTATIONS:
540 return;
541 }
542 track_str_id = vulkan_memory_tracker_.FindMemoryTypeCounterString(
543 memory_type,
544 VulkanMemoryTracker::DeviceCounterType::kAllocationCounter);
545 track = context_->track_tracker->InternProcessCounterTrack(track_str_id,
546 upid);
547 context_->event_tracker->PushCounter(
548 event.timestamp(),
549 static_cast<double>(
550 vulkan_device_memory_counters_allocate_[memory_type]),
551 track);
552 break;
553
554 case VulkanMemoryEvent::SOURCE_BUFFER:
555 case VulkanMemoryEvent::SOURCE_IMAGE:
556 memory_type = static_cast<uint32_t>(event.memory_type());
557 switch (event.operation()) {
558 case VulkanMemoryEvent::OP_BIND:
559 vulkan_device_memory_counters_bind_[memory_type] +=
560 event.memory_size();
561 break;
562 case VulkanMemoryEvent::OP_DESTROY_BOUND:
563 vulkan_device_memory_counters_bind_[memory_type] -=
564 event.memory_size();
565 break;
566 case VulkanMemoryEvent::OP_UNSPECIFIED:
567 case VulkanMemoryEvent::OP_CREATE:
568 case VulkanMemoryEvent::OP_DESTROY:
569 case VulkanMemoryEvent::OP_ANNOTATIONS:
570 return;
571 }
572 track_str_id = vulkan_memory_tracker_.FindMemoryTypeCounterString(
573 memory_type, VulkanMemoryTracker::DeviceCounterType::kBindCounter);
574 track = context_->track_tracker->InternProcessCounterTrack(track_str_id,
575 upid);
576 context_->event_tracker->PushCounter(
577 event.timestamp(),
578 static_cast<double>(vulkan_device_memory_counters_bind_[memory_type]),
579 track);
580 break;
581 case VulkanMemoryEvent::SOURCE_UNSPECIFIED:
582 case VulkanMemoryEvent::SOURCE_DEVICE:
583 return;
584 }
585 }
586
ParseVulkanMemoryEvent(PacketSequenceStateGeneration * sequence_state,ConstBytes blob)587 void GpuEventParser::ParseVulkanMemoryEvent(
588 PacketSequenceStateGeneration* sequence_state,
589 ConstBytes blob) {
590 using protos::pbzero::InternedData;
591 VulkanMemoryEvent::Decoder vulkan_memory_event(blob.data, blob.size);
592 tables::VulkanMemoryAllocationsTable::Row vulkan_memory_event_row;
593 vulkan_memory_event_row.source = vulkan_memory_tracker_.FindSourceString(
594 static_cast<VulkanMemoryEvent::Source>(vulkan_memory_event.source()));
595 vulkan_memory_event_row.operation =
596 vulkan_memory_tracker_.FindOperationString(
597 static_cast<VulkanMemoryEvent::Operation>(
598 vulkan_memory_event.operation()));
599 vulkan_memory_event_row.timestamp = vulkan_memory_event.timestamp();
600 vulkan_memory_event_row.upid =
601 context_->process_tracker->GetOrCreateProcess(vulkan_memory_event.pid());
602 if (vulkan_memory_event.has_device())
603 vulkan_memory_event_row.device =
604 static_cast<int64_t>(vulkan_memory_event.device());
605 if (vulkan_memory_event.has_device_memory())
606 vulkan_memory_event_row.device_memory =
607 static_cast<int64_t>(vulkan_memory_event.device_memory());
608 if (vulkan_memory_event.has_heap())
609 vulkan_memory_event_row.heap = vulkan_memory_event.heap();
610 if (vulkan_memory_event.has_memory_type())
611 vulkan_memory_event_row.memory_type = vulkan_memory_event.memory_type();
612 if (vulkan_memory_event.has_caller_iid()) {
613 vulkan_memory_event_row.function_name =
614 vulkan_memory_tracker_
615 .GetInternedString<InternedData::kFunctionNamesFieldNumber>(
616 sequence_state,
617 static_cast<uint64_t>(vulkan_memory_event.caller_iid()));
618 }
619 if (vulkan_memory_event.has_object_handle())
620 vulkan_memory_event_row.object_handle =
621 static_cast<int64_t>(vulkan_memory_event.object_handle());
622 if (vulkan_memory_event.has_memory_address())
623 vulkan_memory_event_row.memory_address =
624 static_cast<int64_t>(vulkan_memory_event.memory_address());
625 if (vulkan_memory_event.has_memory_size())
626 vulkan_memory_event_row.memory_size =
627 static_cast<int64_t>(vulkan_memory_event.memory_size());
628 if (vulkan_memory_event.has_allocation_scope())
629 vulkan_memory_event_row.scope =
630 vulkan_memory_tracker_.FindAllocationScopeString(
631 static_cast<VulkanMemoryEvent::AllocationScope>(
632 vulkan_memory_event.allocation_scope()));
633
634 UpdateVulkanMemoryAllocationCounters(vulkan_memory_event_row.upid.value(),
635 vulkan_memory_event);
636
637 auto* allocs = context_->storage->mutable_vulkan_memory_allocations_table();
638 VulkanAllocId id = allocs->Insert(vulkan_memory_event_row).id;
639
640 if (vulkan_memory_event.has_annotations()) {
641 auto inserter = context_->args_tracker->AddArgsTo(id);
642
643 for (auto it = vulkan_memory_event.annotations(); it; ++it) {
644 protos::pbzero::VulkanMemoryEventAnnotation::Decoder annotation(*it);
645
646 auto key_id =
647 vulkan_memory_tracker_
648 .GetInternedString<InternedData::kVulkanMemoryKeysFieldNumber>(
649 sequence_state, static_cast<uint64_t>(annotation.key_iid()));
650
651 if (annotation.has_int_value()) {
652 inserter.AddArg(key_id, Variadic::Integer(annotation.int_value()));
653 } else if (annotation.has_double_value()) {
654 inserter.AddArg(key_id, Variadic::Real(annotation.double_value()));
655 } else if (annotation.has_string_iid()) {
656 auto string_id =
657 vulkan_memory_tracker_
658 .GetInternedString<InternedData::kVulkanMemoryKeysFieldNumber>(
659 sequence_state,
660 static_cast<uint64_t>(annotation.string_iid()));
661
662 inserter.AddArg(key_id, Variadic::String(string_id));
663 }
664 }
665 }
666 }
667
ParseGpuLog(int64_t ts,ConstBytes blob)668 void GpuEventParser::ParseGpuLog(int64_t ts, ConstBytes blob) {
669 protos::pbzero::GpuLog::Decoder event(blob.data, blob.size);
670
671 tables::GpuTrackTable::Row track(gpu_log_track_name_id_);
672 track.scope = gpu_log_scope_id_;
673 TrackId track_id = context_->track_tracker->InternGpuTrack(track);
674
675 auto args_callback = [this, &event](ArgsTracker::BoundInserter* inserter) {
676 if (event.has_tag()) {
677 inserter->AddArg(
678 tag_id_,
679 Variadic::String(context_->storage->InternString(event.tag())));
680 }
681 if (event.has_log_message()) {
682 inserter->AddArg(log_message_id_,
683 Variadic::String(context_->storage->InternString(
684 event.log_message())));
685 }
686 };
687
688 auto severity = static_cast<size_t>(event.severity());
689 StringId severity_id =
690 severity < log_severity_ids_.size()
691 ? log_severity_ids_[static_cast<size_t>(event.severity())]
692 : log_severity_ids_[log_severity_ids_.size() - 1];
693
694 tables::GpuSliceTable::Row row;
695 row.ts = ts;
696 row.track_id = track_id;
697 row.name = severity_id;
698 row.dur = 0;
699 context_->slice_tracker->ScopedTyped(
700 context_->storage->mutable_gpu_slice_table(), row, args_callback);
701 }
702
ParseVulkanApiEvent(int64_t ts,ConstBytes blob)703 void GpuEventParser::ParseVulkanApiEvent(int64_t ts, ConstBytes blob) {
704 protos::pbzero::VulkanApiEvent::Decoder vk_event(blob.data, blob.size);
705 if (vk_event.has_vk_debug_utils_object_name()) {
706 protos::pbzero::VulkanApiEvent_VkDebugUtilsObjectName::Decoder event(
707 vk_event.vk_debug_utils_object_name());
708 debug_marker_names_[event.object_type()][event.object()] =
709 event.object_name().ToStdString();
710 }
711 if (vk_event.has_vk_queue_submit()) {
712 protos::pbzero::VulkanApiEvent_VkQueueSubmit::Decoder event(
713 vk_event.vk_queue_submit());
714 // Once flow table is implemented, we can create a nice UI that link the
715 // vkQueueSubmit to GpuRenderStageEvent. For now, just add it as in a GPU
716 // track so that they can appear close to the render stage slices.
717 tables::GpuTrackTable::Row track(vk_event_track_id_);
718 track.scope = vk_event_scope_id_;
719 TrackId track_id = context_->track_tracker->InternGpuTrack(track);
720 tables::GpuSliceTable::Row row;
721 row.ts = ts;
722 row.dur = static_cast<int64_t>(event.duration_ns());
723 row.track_id = track_id;
724 row.name = vk_queue_submit_id_;
725 if (event.has_vk_command_buffers()) {
726 row.command_buffer = static_cast<int64_t>(*event.vk_command_buffers());
727 }
728 row.submission_id = event.submission_id();
729 auto args_callback = [this, &event](ArgsTracker::BoundInserter* inserter) {
730 inserter->AddArg(context_->storage->InternString("pid"),
731 Variadic::Integer(event.pid()));
732 inserter->AddArg(context_->storage->InternString("tid"),
733 Variadic::Integer(event.tid()));
734 };
735 context_->slice_tracker->ScopedTyped(
736 context_->storage->mutable_gpu_slice_table(), row, args_callback);
737 }
738 }
739
ParseGpuMemTotalEvent(int64_t ts,ConstBytes blob)740 void GpuEventParser::ParseGpuMemTotalEvent(int64_t ts, ConstBytes blob) {
741 protos::pbzero::GpuMemTotalEvent::Decoder gpu_mem_total(blob.data, blob.size);
742
743 TrackId track = kInvalidTrackId;
744 const uint32_t pid = gpu_mem_total.pid();
745 if (pid == 0) {
746 // Pid 0 is used to indicate the global total
747 track = context_->track_tracker->InternGlobalCounterTrack(
748 gpu_mem_total_name_id_, gpu_mem_total_unit_id_,
749 gpu_mem_total_global_desc_id_);
750 } else {
751 // Process emitting the packet can be different from the pid in the event.
752 UniqueTid utid = context_->process_tracker->UpdateThread(pid, pid);
753 UniquePid upid = context_->storage->thread_table().upid()[utid].value_or(0);
754 track = context_->track_tracker->InternProcessCounterTrack(
755 gpu_mem_total_name_id_, upid, gpu_mem_total_unit_id_,
756 gpu_mem_total_proc_desc_id_);
757 }
758 context_->event_tracker->PushCounter(
759 ts, static_cast<double>(gpu_mem_total.size()), track);
760 }
761
762 } // namespace trace_processor
763 } // namespace perfetto
764