util/profiler.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <cstdint> | ||
| 5 | #include <string> | ||
| 6 | #include <string_view> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "chrono_batch_render/export.h" | ||
| 10 | |||
| 11 | namespace chrono::batch_render::profiler { | ||
| 12 | |||
| 13 | class CHRONO_BATCH_RENDER_API ZoneId { | ||
| 14 | public: | ||
| 15 | static ZoneId Named(std::string_view name); | ||
| 16 | |||
| 17 | std::string_view Name() const; | ||
| 18 | |||
| 19 | 4 | uint32_t Index() const { | |
| 20 | 3 | return m_index; | |
| 21 | } | ||
| 22 | |||
| 23 | 70 | friend bool operator==(ZoneId a, ZoneId b) { | |
| 24 | 70 | return a.m_index == b.m_index; | |
| 25 | } | ||
| 26 | |||
| 27 | 1 | friend bool operator!=(ZoneId a, ZoneId b) { | |
| 28 | 1 | return a.m_index != b.m_index; | |
| 29 | } | ||
| 30 | |||
| 31 | private: | ||
| 32 | 38509 | explicit ZoneId(uint32_t index) : m_index(index) {} | |
| 33 | |||
| 34 | uint32_t m_index; | ||
| 35 | }; | ||
| 36 | |||
| 37 | 123 | struct Track { | |
| 38 | std::string name; | ||
| 39 | bool gpu = false; | ||
| 40 | }; | ||
| 41 | |||
| 42 | struct Zone { | ||
| 43 | ZoneId id; | ||
| 44 | uint32_t track = 0; | ||
| 45 | uint32_t depth = 0; | ||
| 46 | uint64_t startNs = 0; | ||
| 47 | uint64_t endNs = 0; | ||
| 48 | }; | ||
| 49 | |||
| 50 | 167 | struct Frame { | |
| 51 | uint64_t index = 0; | ||
| 52 | uint64_t startNs = 0; | ||
| 53 | uint64_t endNs = 0; | ||
| 54 | std::vector<Zone> zones; | ||
| 55 | }; | ||
| 56 | |||
| 57 | CHRONO_BATCH_RENDER_API void SetEnabled(bool enabled); | ||
| 58 | CHRONO_BATCH_RENDER_API bool Enabled(); | ||
| 59 | |||
| 60 | CHRONO_BATCH_RENDER_API uint64_t Now(); | ||
| 61 | |||
| 62 | CHRONO_BATCH_RENDER_API void BeginFrame(); | ||
| 63 | |||
| 64 | CHRONO_BATCH_RENDER_API uint64_t CurrentFrame(); | ||
| 65 | |||
| 66 | CHRONO_BATCH_RENDER_API std::vector<Frame> Frames(); | ||
| 67 | |||
| 68 | CHRONO_BATCH_RENDER_API void SetHistory(size_t frames); | ||
| 69 | |||
| 70 | CHRONO_BATCH_RENDER_API void Clear(); | ||
| 71 | |||
| 72 | CHRONO_BATCH_RENDER_API void SetThreadName(std::string_view name); | ||
| 73 | |||
| 74 | CHRONO_BATCH_RENDER_API std::vector<Track> Tracks(); | ||
| 75 | |||
| 76 | CHRONO_BATCH_RENDER_API uint32_t AddGpuTrack(std::string_view name); | ||
| 77 | |||
| 78 | CHRONO_BATCH_RENDER_API void AddZone(uint64_t frame, const Zone& zone); | ||
| 79 | |||
| 80 | 56 | struct ReportNode { | |
| 81 | std::string name; | ||
| 82 | double ms = 0; | ||
| 83 | double calls = 0; | ||
| 84 | double percent = 0; | ||
| 85 | double minMs = 0; | ||
| 86 | double maxMs = 0; | ||
| 87 | std::vector<ReportNode> children; | ||
| 88 | }; | ||
| 89 | |||
| 90 | 19 | struct ReportTrack { | |
| 91 | std::string name; | ||
| 92 | bool gpu = false; | ||
| 93 | double ms = 0; | ||
| 94 | std::vector<ReportNode> zones; | ||
| 95 | }; | ||
| 96 | |||
| 97 | 6 | struct Report { | |
| 98 | size_t frames = 0; | ||
| 99 | double frameMs = 0; | ||
| 100 | std::vector<ReportTrack> tracks; | ||
| 101 | }; | ||
| 102 | |||
| 103 | CHRONO_BATCH_RENDER_API Report Summarize(const std::vector<Frame>& frames); | ||
| 104 | |||
| 105 | struct ReportFilter { | ||
| 106 | uint32_t depth = 8; | ||
| 107 | double minMs = 0; | ||
| 108 | bool byTime = false; | ||
| 109 | }; | ||
| 110 | |||
| 111 | CHRONO_BATCH_RENDER_API std::string ReportText(const Report& report, | ||
| 112 | const ReportFilter& filter = {}); | ||
| 113 | |||
| 114 | CHRONO_BATCH_RENDER_API std::string ReportJson(const Report& report); | ||
| 115 | |||
| 116 | CHRONO_BATCH_RENDER_API std::string TraceJson(const std::vector<Frame>& frames); | ||
| 117 | |||
| 118 | class CHRONO_BATCH_RENDER_API ScopedZone { | ||
| 119 | public: | ||
| 120 | explicit ScopedZone(ZoneId zone); | ||
| 121 | ~ScopedZone(); | ||
| 122 | ScopedZone(const ScopedZone&) = delete; | ||
| 123 | ScopedZone& operator=(const ScopedZone&) = delete; | ||
| 124 | |||
| 125 | private: | ||
| 126 | ZoneId m_zone; | ||
| 127 | uint64_t m_startNs = 0; | ||
| 128 | bool m_recording = false; | ||
| 129 | uint8_t m_ranges = 0; | ||
| 130 | }; | ||
| 131 | |||
| 132 | } | ||
| 133 | |||
| 134 | #define CHRONO_BATCH_RENDER_PROFILER_CONCAT_(a, b) a##b | ||
| 135 | #define CHRONO_BATCH_RENDER_PROFILER_CONCAT(a, b) CHRONO_BATCH_RENDER_PROFILER_CONCAT_(a, b) | ||
| 136 | |||
| 137 | #define CHRONO_BATCH_RENDER_ZONE(name) \ | ||
| 138 | static const ::chrono::batch_render::profiler::ZoneId CHRONO_BATCH_RENDER_PROFILER_CONCAT( \ | ||
| 139 | chronoBatchRenderZoneId, __LINE__) = \ | ||
| 140 | ::chrono::batch_render::profiler::ZoneId::Named(name); \ | ||
| 141 | const ::chrono::batch_render::profiler::ScopedZone CHRONO_BATCH_RENDER_PROFILER_CONCAT( \ | ||
| 142 | chronoBatchRenderZone, \ | ||
| 143 | __LINE__)(CHRONO_BATCH_RENDER_PROFILER_CONCAT(chronoBatchRenderZoneId, __LINE__)) | ||
| 144 |