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