载入中...
搜索中...
未找到
ProfilerModel.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <map>
5#include <tuple>
6#include <utility>
7#include "profiler/Profiler.h"
8
9// ProfilerModel.cpp
11namespace {
12
13bool finiteNonNegative(double value) { return std::isfinite(value) && value >= 0.0; }
14
15bool contains(const std::string& value, const std::string& filter) {
16 return filter.empty() || value.find(filter) != std::string::npos;
17}
18
19} // namespace
20
22
24 if (!finiteNonNegative(budgets.cpuFrameMs) || !finiteNonNegative(budgets.gpuFrameMs) ||
25 !finiteNonNegative(budgets.zoneSelfMs) || budgets.historyFrames < 2 || budgets.historyFrames > 36000) {
26 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.profiler.invalid-budgets"),
27 "Profiler budgets or history capacity are invalid");
28 }
29 budgets_ = budgets;
30 while (history_.size() > budgets_.historyFrames) history_.pop_front();
31 return eve::editing::applied<void>();
32}
33
35 if (frame.sequence == 0 || !finiteNonNegative(frame.cpuFrameMs) || !finiteNonNegative(frame.gpuFrameMs) ||
36 frame.zones.size() > 100000) {
37 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.profiler.invalid-frame"),
38 "Profiler frame shape or timing is invalid");
39 }
40 if (!history_.empty() && frame.sequence <= history_.back().sequence) {
41 return eve::editing::failed<void>(EditorStatus::Conflict, RuleId("editor.profiler.stale-frame"),
42 "Profiler frame sequence is stale");
43 }
44 for (const auto& zone : frame.zones) {
45 if (zone.module.empty() || zone.name.empty() || zone.thread.empty() || !finiteNonNegative(zone.selfMs) ||
46 !finiteNonNegative(zone.totalMs) || zone.selfMs > zone.totalMs || zone.count < 1 || zone.depth < 0) {
47 return eve::editing::failed<void>(EditorStatus::Rejected, RuleId("editor.profiler.invalid-zone"),
48 "Profiler zone identity or timing is invalid");
49 }
50 }
51 history_.push_back(std::move(frame));
52 while (history_.size() > budgets_.historyFrames) history_.pop_front();
53 return eve::editing::applied<void>();
54}
55
56void EditorProfilerModel::clear() { history_.clear(); }
57
58std::vector<EditorProfilerZone> EditorProfilerModel::query(const EditorProfilerQuery& options) const {
59 std::vector<EditorProfilerZone> result;
60 if (history_.empty() || options.limit == 0) return result;
61 for (const auto& zone : history_.back().zones) {
62 if (contains(zone.module, options.module) && contains(zone.thread, options.thread) &&
63 (contains(zone.name, options.text) || contains(zone.module, options.text))) {
64 result.push_back(zone);
65 }
66 }
67 const auto less = [&](const EditorProfilerZone& left, const EditorProfilerZone& right) {
68 switch (options.sort) {
69 case EditorProfilerSort::SelfTime: return left.selfMs < right.selfMs;
70 case EditorProfilerSort::TotalTime: return left.totalMs < right.totalMs;
71 case EditorProfilerSort::Count: return left.count < right.count;
73 return std::tie(left.module, left.name, left.thread) < std::tie(right.module, right.name, right.thread);
74 }
75 return false;
76 };
77 std::stable_sort(result.begin(), result.end(), [&](const auto& left, const auto& right) {
78 return options.descending ? less(right, left) : less(left, right);
79 });
80 if (result.size() > options.limit) result.resize(options.limit);
81 return result;
82}
83
84std::vector<EditorProfilerModuleSummary> EditorProfilerModel::moduleSummary() const {
85 std::map<std::string, EditorProfilerModuleSummary> grouped;
86 if (!history_.empty()) {
87 for (const auto& zone : history_.back().zones) {
88 auto& summary = grouped[zone.module];
89 summary.module = zone.module;
90 summary.selfMs += zone.selfMs;
91 summary.totalMs += zone.totalMs;
92 summary.count += zone.count;
93 ++summary.zones;
94 }
95 }
96 std::vector<EditorProfilerModuleSummary> result;
97 result.reserve(grouped.size());
98 for (auto& [name, summary] : grouped) result.push_back(std::move(summary));
99 std::stable_sort(result.begin(), result.end(), [](const auto& left, const auto& right) {
100 if (left.selfMs != right.selfMs) return left.selfMs > right.selfMs;
101 return left.module < right.module;
102 });
103 return result;
104}
105
106std::vector<EditorDiagnostic> EditorProfilerModel::diagnostics() const {
107 std::vector<EditorDiagnostic> result;
108 if (history_.empty()) return result;
109 const auto& frame = history_.back();
110 if (budgets_.cpuFrameMs > 0.0 && frame.cpuFrameMs > budgets_.cpuFrameMs) {
112 RuleId("editor.profiler.cpu-budget"), DiagnosticSeverity::Warning,
113 "CPU frame time exceeds the editor budget"));
114 }
115 if (frame.gpuTimingAvailable && budgets_.gpuFrameMs > 0.0 && frame.gpuFrameMs > budgets_.gpuFrameMs) {
117 RuleId("editor.profiler.gpu-budget"), DiagnosticSeverity::Warning,
118 "GPU frame time exceeds the editor budget"));
119 }
120 if (budgets_.zoneSelfMs > 0.0) {
121 for (const auto& zone : frame.zones) {
122 if (zone.selfMs > budgets_.zoneSelfMs) {
124 eve::DiagnosticCode::PreconditionViolation, RuleId("editor.profiler.zone-budget"),
125 DiagnosticSeverity::Warning,
126 "Profiler zone exceeds its self-time budget: " + zone.module + "/" + zone.name));
127 }
128 }
129 }
130 return result;
131}
132
133} // namespace eve::profiler_editing
134
135// ProfilerRuntime.cpp
136namespace eve::profiler_editing {
137
139 auto captured = profiler.captureFrame();
140 if (!captured.hasValue()) {
141 std::vector<EditorDiagnostic> diagnostics;
142 for (const auto& diagnostic : captured.diagnostics())
143 diagnostics.push_back(eve::editing::ruleDiagnostic(diagnostic.code(), RuleId("editor.profiler.capture"),
144 DiagnosticSeverity::Error, diagnostic.message()));
145 const EditorStatus status =
146 captured.code() == eve::StatusCode::NotFound ? EditorStatus::NotFound : EditorStatus::Failed;
147 return Result<void>::failure(eve::Status(status, std::move(diagnostics)));
148 }
149 auto runtimeFrame = std::move(captured).takeValue();
151 frame.sequence = runtimeFrame.sequence;
152 frame.cpuFrameMs = runtimeFrame.cpuFrameMs;
153 frame.gpuFrameMs = runtimeFrame.gpuFrameMs;
154 frame.gpuTimingAvailable = runtimeFrame.gpuTimingAvailable;
155 frame.zones.reserve(runtimeFrame.zones.size());
156 for (auto& zone : runtimeFrame.zones) {
157 frame.zones.push_back({std::move(zone.module), std::move(zone.name), std::move(zone.thread), zone.selfMs,
158 zone.totalMs, zone.count, zone.depth});
159 }
160 return model.ingest(std::move(frame));
161}
162
163} // namespace eve::profiler_editing
std::string name
Definition ECS.cpp:154
wgpu::PopErrorScopeStatus status
HexVec3 left
HexVec3 right
Poco::JSON::Object::Ptr result
ssq::Object frame
Object::Ptr value
glm::mat4 model
std::string filter
const SquirrelValueOptions & options
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Structured status and zero or more diagnostics for an operation.
Definition Status.h:70
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:313
Script-facing wrapper around the engine-wide profiler core (eve::prof, see common/Profile....
Definition Profiler.h:68
eve::Result< ProfilerFrameSnapshot > captureFrame() const
Copy the latest completed frame for editor and diagnostics consumers.
Definition Profiler.cpp:114
Result< void > collect(const profiler::Profiler &profiler, EditorProfilerModel &model) const
Copy and ingest the latest runtime frame.
Bounded owning profiler history and deterministic presentation queries. @thread Main-thread only....
EditorProfilerModel()
Construct with default budgets and a 300-frame history.
Result< void > configure(EditorProfilerBudgets budgets)
Validate and atomically replace editor-only performance budgets.
EditorProfilerBudgets budgets() const
Return the current editor-only budgets by value.
std::vector< EditorProfilerZone > query(const EditorProfilerQuery &query) const
Query and sort copied hotspot rows from the latest frame.
std::vector< EditorDiagnostic > diagnostics() const
Produce CPU/GPU/zone budget diagnostics for the latest frame.
void clear()
Clear retained frames without changing configured budgets.
Result< void > ingest(EditorProfilerFrame frame)
Append an owning completed-frame snapshot.
std::vector< EditorProfilerModuleSummary > moduleSummary() const
Aggregate copied rows by module for the latest frame.
Diagnostic ruleDiagnostic(eve::DiagnosticCode code, RuleId rule, DiagnosticSeverity severity, std::string message)
Build an owning common diagnostic carrying an open editing rule identity.
editing::RuleId RuleId
StatusCode
Stable outcome category for an operation.
Definition Status.h:28
Configurable performance budgets used only for editor diagnostics.
One owning completed-frame sample retained by the editor.
Non-persistent query applied to the latest profiler frame.
One owning profiler hotspot row projected for editor presentation.