pipeline_statistics.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <string_view>
  4. #include <fmt/format.h>
  5. #include "common/common_types.h"
  6. #include "common/logging/log.h"
  7. #include "video_core/renderer_vulkan/pipeline_statistics.h"
  8. #include "video_core/vulkan_common/vulkan_device.h"
  9. #include "video_core/vulkan_common/vulkan_wrapper.h"
  10. namespace Vulkan {
  11. using namespace std::string_view_literals;
  12. static u64 GetUint64(const VkPipelineExecutableStatisticKHR& statistic) {
  13. switch (statistic.format) {
  14. case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_INT64_KHR:
  15. return static_cast<u64>(statistic.value.i64);
  16. case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_UINT64_KHR:
  17. return statistic.value.u64;
  18. case VK_PIPELINE_EXECUTABLE_STATISTIC_FORMAT_FLOAT64_KHR:
  19. return static_cast<u64>(statistic.value.f64);
  20. default:
  21. return 0;
  22. }
  23. }
  24. PipelineStatistics::PipelineStatistics(const Device& device_) : device{device_} {}
  25. void PipelineStatistics::Collect(VkPipeline pipeline) {
  26. const auto& dev{device.GetLogical()};
  27. const std::vector properties{dev.GetPipelineExecutablePropertiesKHR(pipeline)};
  28. const u32 num_executables{static_cast<u32>(properties.size())};
  29. for (u32 executable = 0; executable < num_executables; ++executable) {
  30. const auto statistics{dev.GetPipelineExecutableStatisticsKHR(pipeline, executable)};
  31. if (statistics.empty()) {
  32. continue;
  33. }
  34. Stats stage_stats;
  35. for (const auto& statistic : statistics) {
  36. const char* const name{statistic.name};
  37. if (name == "Binary Size"sv || name == "Code size"sv || name == "Instruction Count"sv) {
  38. stage_stats.code_size = GetUint64(statistic);
  39. } else if (name == "Register Count"sv) {
  40. stage_stats.register_count = GetUint64(statistic);
  41. } else if (name == "SGPRs"sv || name == "numUsedSgprs"sv) {
  42. stage_stats.sgpr_count = GetUint64(statistic);
  43. } else if (name == "VGPRs"sv || name == "numUsedVgprs"sv) {
  44. stage_stats.vgpr_count = GetUint64(statistic);
  45. } else if (name == "Branches"sv) {
  46. stage_stats.branches_count = GetUint64(statistic);
  47. } else if (name == "Basic Block Count"sv) {
  48. stage_stats.basic_block_count = GetUint64(statistic);
  49. }
  50. }
  51. std::scoped_lock lock{mutex};
  52. collected_stats.push_back(stage_stats);
  53. }
  54. }
  55. void PipelineStatistics::Report() const {
  56. double num{};
  57. Stats total;
  58. {
  59. std::scoped_lock lock{mutex};
  60. for (const Stats& stats : collected_stats) {
  61. total.code_size += stats.code_size;
  62. total.register_count += stats.register_count;
  63. total.sgpr_count += stats.sgpr_count;
  64. total.vgpr_count += stats.vgpr_count;
  65. total.branches_count += stats.branches_count;
  66. total.basic_block_count += stats.basic_block_count;
  67. }
  68. num = static_cast<double>(collected_stats.size());
  69. }
  70. std::string report;
  71. const auto add = [&](const char* fmt, u64 value) {
  72. if (value > 0) {
  73. report += fmt::format(fmt::runtime(fmt), static_cast<double>(value) / num);
  74. }
  75. };
  76. add("Code size: {:9.03f}\n", total.code_size);
  77. add("Register count: {:9.03f}\n", total.register_count);
  78. add("SGPRs: {:9.03f}\n", total.sgpr_count);
  79. add("VGPRs: {:9.03f}\n", total.vgpr_count);
  80. add("Branches count: {:9.03f}\n", total.branches_count);
  81. add("Basic blocks: {:9.03f}\n", total.basic_block_count);
  82. LOG_INFO(Render_Vulkan,
  83. "\nAverage pipeline statistics\n"
  84. "==========================================\n"
  85. "{}\n",
  86. report);
  87. }
  88. } // namespace Vulkan