载入中...
搜索中...
未找到
Profile.h
浏览该文件的文档.
1#pragma once
2
3#ifndef EVENGINE_COMMON_PROFILE_H
4#define EVENGINE_COMMON_PROFILE_H
5
6#include "common/Export.h"
7
8#include <atomic>
9#include <cstddef>
10#include <cstdint>
11#include <string>
12#include <vector>
13
14// ---------------------------------------------------------------------------
15// Engine-wide profiling core (Tracy / UE Insights style scoped zones).
16//
17// Any module may sprinkle scoped zones at its per-frame entry points:
18//
19// void World::update(float dt) {
20// EV_PROFILE_MODULE("physics", "World::update");
21// ...
22// }
23//
24// Zones are cheap when disabled (a single atomic branch); when enabled they
25// push {name, module, thread, start, end, parent} into a per-thread buffer and
26// frameMark() aggregates the frame into a call tree with self/total time per
27// module and per zone. This core lives in common/ so lower layers can use it
28// without violating the module layering rules.
29// ---------------------------------------------------------------------------
30
31namespace eve::prof {
32
37 const char* name = nullptr; // pointer is static storage, never freed
38 uint32_t module = 0; // interned module id (0 = default)
39 uint32_t threadId = 0;
40 int32_t parent = -1; // index into the same thread's buffer
41 int16_t depth = 0;
42 int64_t startNs = 0;
43 int64_t endNs = 0; // 0 while open
44 uint8_t kind = 0; // 0 = CPU, 1 = GPU
45};
46
51 std::string module;
52 std::string name;
53 std::string thread;
54 double selfMs = 0.0;
55 double totalMs = 0.0;
56 int count = 0;
57 int minDepth = 0x7fffffff;
58};
59
67public:
69 static void setEnabled(bool on);
71 static bool enabled();
72
82 static void zoneBegin(const char* name, const char* module = nullptr);
84 static void zoneEnd();
85
91 static double frameMark();
92
94 static bool hasFrame();
96 static const std::vector<ZoneSample>& lastFrame();
105 [[nodiscard]] static std::vector<ZoneSample> hotspots(std::size_t limit, double minSelfMs);
107 static std::string textReport();
108
110 static void reset();
111
113 static uint32_t moduleId(const char* module);
114
115private:
116 struct ThreadBuffer;
117 struct Frame;
118
119 static ThreadBuffer& currentBuffer();
120
122 static std::vector<ThreadBuffer*>& threads();
124 static Frame& lastFrameStorage();
125 static bool& hasFrameFlag();
126};
127
132public:
134 ZoneScope(const char* name, const char* module = nullptr) {
136 Profiler::zoneBegin(name, module);
137 }
139 ~ZoneScope() { Profiler::zoneEnd(); }
140 ZoneScope(const ZoneScope&) = delete;
141 ZoneScope& operator=(const ZoneScope&) = delete;
142};
143
144} // namespace eve::prof
145
146#define EV_PROFILE_CAT_I(a, b) a##b
147#define EV_PROFILE_CAT(a, b) EV_PROFILE_CAT_I(a, b)
148
150#define EV_PROFILE_ZONE_FUNC() \
151 ::eve::prof::ZoneScope EV_PROFILE_CAT(ev_zone_, __LINE__)(__func__)
152
154#define EV_PROFILE_ZONE(name) \
155 ::eve::prof::ZoneScope EV_PROFILE_CAT(ev_zone_, __LINE__)(name)
156
158#define EV_PROFILE_MODULE(module, name) \
159 ::eve::prof::ZoneScope EV_PROFILE_CAT(ev_zone_, __LINE__)(name, module)
160
161#endif
std::string name
Definition ECS.cpp:154
#define EVENGINE_API_FOUNDATION_INLINE
仅供"只有内联成员"的类型使用的组宏:拥有者 dllexport,消费方不导入。
Definition Export.h:170
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
const ColourScope * parent
std::string kind
int count
int limit
Definition TreeMesh.cpp:166
std::uint32_t depth
Global zone collector + per-frame aggregator.
Definition Profile.h:66
RAII scoped zone (used via the EV_PROFILE_* macros).
Definition Profile.h:131
ZoneScope(const char *name, const char *module=nullptr)
Zone scope.
Definition Profile.h:134
ZoneScope(const ZoneScope &)=delete
~ZoneScope()
Zone scope.
Definition Profile.h:139
ZoneScope & operator=(const ZoneScope &)=delete
bool enabled
One completed profiling zone (pre-order, parent-linked within a thread).
Definition Profile.h:36
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