profiler_reporting.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <chrono>
  7. #include <mutex>
  8. #include <utility>
  9. #include <vector>
  10. #include "common/profiler.h"
  11. #include "common/synchronized_wrapper.h"
  12. namespace Common {
  13. namespace Profiling {
  14. struct TimingCategoryInfo {
  15. static const unsigned int NO_PARENT = -1;
  16. TimingCategory* category;
  17. const char* name;
  18. unsigned int parent;
  19. };
  20. struct ProfilingFrameResult {
  21. /// Time since the last delivered frame
  22. Duration interframe_time;
  23. /// Time spent processing a frame, excluding VSync
  24. Duration frame_time;
  25. /// Total amount of time spent inside each category in this frame. Indexed by the category id
  26. std::vector<Duration> time_per_category;
  27. };
  28. class ProfilingManager final {
  29. public:
  30. ProfilingManager();
  31. unsigned int RegisterTimingCategory(TimingCategory* category, const char* name);
  32. void SetTimingCategoryParent(unsigned int category, unsigned int parent);
  33. const std::vector<TimingCategoryInfo>& GetTimingCategoriesInfo() const {
  34. return timing_categories;
  35. }
  36. /// This should be called after swapping screen buffers.
  37. void BeginFrame();
  38. /// This should be called before swapping screen buffers.
  39. void FinishFrame();
  40. /// Get the timing results from the previous frame. This is updated when you call FinishFrame().
  41. const ProfilingFrameResult& GetPreviousFrameResults() const {
  42. return results;
  43. }
  44. private:
  45. std::vector<TimingCategoryInfo> timing_categories;
  46. Clock::time_point last_frame_end;
  47. Clock::time_point this_frame_start;
  48. ProfilingFrameResult results;
  49. };
  50. struct AggregatedDuration {
  51. Duration avg, min, max;
  52. };
  53. struct AggregatedFrameResult {
  54. /// Time since the last delivered frame
  55. AggregatedDuration interframe_time;
  56. /// Time spent processing a frame, excluding VSync
  57. AggregatedDuration frame_time;
  58. float fps;
  59. /// Total amount of time spent inside each category in this frame. Indexed by the category id
  60. std::vector<AggregatedDuration> time_per_category;
  61. };
  62. class TimingResultsAggregator final {
  63. public:
  64. TimingResultsAggregator(size_t window_size);
  65. void Clear();
  66. void SetNumberOfCategories(size_t n);
  67. void AddFrame(const ProfilingFrameResult& frame_result);
  68. AggregatedFrameResult GetAggregatedResults() const;
  69. size_t max_window_size;
  70. size_t window_size;
  71. size_t cursor;
  72. std::vector<Duration> interframe_times;
  73. std::vector<Duration> frame_times;
  74. std::vector<std::vector<Duration>> times_per_category;
  75. };
  76. ProfilingManager& GetProfilingManager();
  77. SynchronizedRef<TimingResultsAggregator> GetTimingResultsAggregator();
  78. } // namespace Profiling
  79. } // namespace Common