载入中...
搜索中...
未找到
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
51class ProfilerQueryImpl final : public eve::IProfilerQuery {
52public:
53 bool enabled() const override { return eve::prof::Profiler::enabled(); }
54
55 std::string frameJson() const override {
56 const bool hasFrame = eve::prof::Profiler::hasFrame();
57 auto* timer = eve::cap::query<eve::service::IGpuTimer>();
58 const bool gpuKnown = timer != nullptr && timer->gpuTimingAvailable();
59
60 // Depth-0 samples are the roots of each thread's zone tree, so their self
61 // times are the CPU work the zones account for.
62 static const std::vector<eve::prof::ZoneSample> kNoZones;
63 const std::vector<eve::prof::ZoneSample>& zones = hasFrame ? eve::prof::Profiler::lastFrame() : kNoZones;
64 double cpuFrameMs = 0.0;
65 for (const auto& zone : zones) {
66 if (zone.minDepth == 0) cpuFrameMs += zone.selfMs;
67 }
68
69 std::string out = "{\"schema\":\"eve.profiler.frame\",\"version\":1,\"enabled\":";
70 out += enabled() ? "true" : "false";
71 out += ",\"hasFrame\":";
72 out += hasFrame ? "true" : "false";
73 out += ",\"cpuFrameMs\":";
74 appendNumber(out, cpuFrameMs);
75 out += ",\"gpuMs\":";
76 appendNumber(out, gpuKnown ? static_cast<double>(timer->gpuFrameMs()) : 0.0);
77 out += ",\"gpuTimingAvailable\":";
78 out += gpuKnown ? "true" : "false";
79 out += ",\"zoneCount\":" + std::to_string(zones.size()) + ",\"zones\":[";
80 for (std::size_t index = 0; index < zones.size(); ++index) {
81 const eve::prof::ZoneSample& zone = zones[index];
82 if (index) out += ',';
83 out += "{\"module\":";
84 appendString(out, zone.module);
85 out += ",\"name\":";
86 appendString(out, zone.name);
87 out += ",\"thread\":";
88 appendString(out, zone.thread);
89 out += ",\"selfMs\":";
90 appendNumber(out, zone.selfMs);
91 out += ",\"totalMs\":";
92 appendNumber(out, zone.totalMs);
93 out += ",\"count\":" + std::to_string(zone.count) + ",\"depth\":" + std::to_string(zone.minDepth) + "}";
94 }
95 out += "]}";
96 return out;
97 }
98
99 std::string textReport() const override {
100 std::string report = eve::prof::Profiler::textReport();
101 auto* timer = eve::cap::query<eve::service::IGpuTimer>();
102 if (timer && timer->gpuTimingAvailable()) {
103 char buffer[64];
104 std::snprintf(buffer, sizeof(buffer), " GPU frame: %.2f ms\n", timer->gpuFrameMs());
105 report += buffer;
106 }
107 return report;
108 }
109};
110
111} // namespace
112
114 static ProfilerQueryImpl impl;
115 eve::cap::provide<eve::IProfilerQuery>(&impl);
116}
117
118} // namespace eve::profiler
double value
void * impl
std::int32_t c
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
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:226
static const std::vector< ZoneSample > & lastFrame()
Per-module/per-zone samples of the last frame, sorted by self time.
Definition Profile.cpp:224
static bool hasFrame()
True once at least one frame has been aggregated.
Definition Profile.cpp:222
static bool enabled()
True when collection is active.
Definition Profile.cpp:94
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:49
std::string thread
Definition Profile.h:52
std::string std::string name
Definition Profile.h:51