载入中...
搜索中...
未找到
ProfilerCapabilities.cpp
浏览该文件的文档.
2
3#include "common/Capability.h"
4#include "common/GpuTimer.h"
5#include "common/Profile.h"
6
7#include <cstdio>
8#include <string>
9#include <vector>
10
11namespace eve::profiler {
12namespace {
13
15void appendNumber(std::string& out, double value) {
16 char buffer[64];
17 std::snprintf(buffer, sizeof(buffer), "%.4f", value);
18 out += buffer;
19}
20
21void appendString(std::string& out, const std::string& value) {
22 out += '"';
23 for (char c : value) {
24 switch (c) {
25 case '"': out += "\\\""; break;
26 case '\\': out += "\\\\"; break;
27 case '\n': out += "\\n"; break;
28 case '\r': out += "\\r"; break;
29 case '\t': out += "\\t"; break;
30 default:
31 if (static_cast<unsigned char>(c) < 0x20) {
32 char escape[8];
33 std::snprintf(escape, sizeof(escape), "\\u%04x", static_cast<unsigned>(c));
34 out += escape;
35 } else {
36 out += c;
37 }
38 break;
39 }
40 }
41 out += '"';
42}
43
51void appendZone(std::string& out, const eve::prof::ZoneSample& zone) {
52 out += "{\"module\":";
53 appendString(out, zone.module);
54 out += ",\"name\":";
55 appendString(out, zone.name);
56 out += ",\"thread\":";
57 appendString(out, zone.thread);
58 out += ",\"selfMs\":";
59 appendNumber(out, zone.selfMs);
60 out += ",\"totalMs\":";
61 appendNumber(out, zone.totalMs);
62 out += ",\"count\":" + std::to_string(zone.count) + ",\"depth\":" + std::to_string(zone.minDepth) + "}";
63}
64
65class ProfilerQueryImpl final : public eve::IProfilerQuery {
66public:
67 bool enabled() const override { return eve::prof::Profiler::enabled(); }
68
69 std::string frameJson() const override {
70 const bool hasFrame = eve::prof::Profiler::hasFrame();
71 auto* timer = eve::cap::query<eve::service::IGpuTimer>();
72 const bool gpuKnown = timer != nullptr && timer->gpuTimingAvailable();
73
74 // Depth-0 samples are the roots of each thread's zone tree, so their self
75 // times are the CPU work the zones account for.
76 static const std::vector<eve::prof::ZoneSample> kNoZones;
77 const std::vector<eve::prof::ZoneSample>& zones = hasFrame ? eve::prof::Profiler::lastFrame() : kNoZones;
78 const auto hotspots = eve::prof::Profiler::hotspots(8, 0.0);
79 double cpuFrameMs = 0.0;
80 for (const auto& zone : zones) {
81 if (zone.minDepth == 0) cpuFrameMs += zone.selfMs;
82 }
83
84 std::string out = "{\"schema\":\"eve.profiler.frame\",\"version\":2,\"enabled\":";
85 out += enabled() ? "true" : "false";
86 out += ",\"hasFrame\":";
87 out += hasFrame ? "true" : "false";
88 out += ",\"cpuFrameMs\":";
89 appendNumber(out, cpuFrameMs);
90 out += ",\"gpuMs\":";
91 appendNumber(out, gpuKnown ? static_cast<double>(timer->gpuFrameMs()) : 0.0);
92 out += ",\"gpuTimingAvailable\":";
93 out += gpuKnown ? "true" : "false";
94 out += ",\"zoneCount\":" + std::to_string(zones.size()) + ",\"hotspots\":[";
95 for (std::size_t index = 0; index < hotspots.size(); ++index) {
96 if (index) out += ',';
97 appendZone(out, hotspots[index]);
98 }
99 out += "],\"zones\":[";
100 for (std::size_t index = 0; index < zones.size(); ++index) {
101 if (index) out += ',';
102 appendZone(out, zones[index]);
103 }
104 out += "]}";
105 return out;
106 }
107
108 std::string hotspotsJson(std::size_t limit, double minSelfMs) const override {
109 const bool hasFrame = eve::prof::Profiler::hasFrame();
110 const auto zones = eve::prof::Profiler::hotspots(limit, minSelfMs);
111 std::string out = "{\"schema\":\"eve.profiler.hotspots\",\"version\":1,\"hasFrame\":";
112 out += hasFrame ? "true" : "false";
113 out += ",\"limit\":" + std::to_string(limit) + ",\"minSelfMs\":";
114 appendNumber(out, minSelfMs);
115 out += ",\"count\":" + std::to_string(zones.size()) + ",\"zones\":[";
116 for (std::size_t index = 0; index < zones.size(); ++index) {
117 if (index) out += ',';
118 appendZone(out, zones[index]);
119 }
120 out += "]}";
121 return out;
122 }
123
124 std::string textReport() const override {
125 std::string report = eve::prof::Profiler::textReport();
126 auto* timer = eve::cap::query<eve::service::IGpuTimer>();
127 if (timer && timer->gpuTimingAvailable()) {
128 char buffer[64];
129 std::snprintf(buffer, sizeof(buffer), " GPU frame: %.2f ms\n", timer->gpuFrameMs());
130 report += buffer;
131 }
132 return report;
133 }
134};
135
136} // namespace
137
139 static ProfilerQueryImpl impl;
140 eve::cap::provide<eve::IProfilerQuery>(&impl);
141}
142
143} // namespace eve::profiler
void * impl
std::int32_t c
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
Object::Ptr value
ScriptStructure out
int limit
Definition TreeMesh.cpp:166
uint32_t index
Runtime profiler read surface (provided by the profiler module).
static std::string textReport()
Human-readable report of the last frame.
Definition Profile.cpp:244
static const std::vector< ZoneSample > & lastFrame()
Per-module/per-zone samples of the last frame, sorted by self time.
Definition Profile.cpp:229
static std::vector< ZoneSample > hotspots(std::size_t limit, double minSelfMs)
Copy the hottest zones from the last completed frame.
Definition Profile.cpp:231
static bool hasFrame()
True once at least one frame has been aggregated.
Definition Profile.cpp:227
static bool enabled()
True when collection is active.
Definition Profile.cpp:95
EVENGINE_API_FOUNDATION void registerProfilerCapabilities()
Publish the read-only profiler capability (eve::IProfilerQuery).
bool enabled
Per-module/per-zone aggregate of the last completed frame.
Definition Profile.h:50
std::string thread
Definition Profile.h:53
std::string std::string name
Definition Profile.h:52