载入中...
搜索中...
未找到
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 <cstdint>
10#include <string>
11#include <vector>
12
13// ---------------------------------------------------------------------------
14// Engine-wide profiling core (Tracy / UE Insights style scoped zones).
15//
16// Any module may sprinkle scoped zones at its per-frame entry points:
17//
18// void World::update(float dt) {
19// EV_PROFILE_MODULE("physics", "World::update");
20// ...
21// }
22//
23// Zones are cheap when disabled (a single atomic branch); when enabled they
24// push {name, module, thread, start, end, parent} into a per-thread buffer and
25// frameMark() aggregates the frame into a call tree with self/total time per
26// module and per zone. This core lives in common/ so lower layers can use it
27// without violating the module layering rules.
28// ---------------------------------------------------------------------------
29
30namespace eve::prof {
31
36 const char* name = nullptr; // pointer is static storage, never freed
37 uint32_t module = 0; // interned module id (0 = default)
38 uint32_t threadId = 0;
39 int32_t parent = -1; // index into the same thread's buffer
40 int16_t depth = 0;
41 int64_t startNs = 0;
42 int64_t endNs = 0; // 0 while open
43 uint8_t kind = 0; // 0 = CPU, 1 = GPU
44};
45
50 std::string module;
51 std::string name;
52 std::string thread;
53 double selfMs = 0.0;
54 double totalMs = 0.0;
55 int count = 0;
56 int minDepth = 0x7fffffff;
57};
58
66public:
68 static void setEnabled(bool on);
70 static bool enabled();
71
73 static void zoneBegin(const char* name, const char* module = nullptr);
75 static void zoneEnd();
76
82 static double frameMark();
83
85 static bool hasFrame();
87 static const std::vector<ZoneSample>& lastFrame();
89 static std::string textReport();
90
92 static void reset();
93
95 static uint32_t moduleId(const char* module);
96
97private:
98 struct ThreadBuffer;
99 struct Frame;
100
101 static ThreadBuffer& currentBuffer();
102
104 static std::vector<ThreadBuffer*>& threads();
106 static Frame& lastFrameStorage();
107 static bool& hasFrameFlag();
108};
109
114public:
116 ZoneScope(const char* name, const char* module = nullptr) {
118 Profiler::zoneBegin(name, module);
119 }
121 ~ZoneScope() { Profiler::zoneEnd(); }
122 ZoneScope(const ZoneScope&) = delete;
123 ZoneScope& operator=(const ZoneScope&) = delete;
124};
125
126} // namespace eve::prof
127
128#define EV_PROFILE_CAT_I(a, b) a##b
129#define EV_PROFILE_CAT(a, b) EV_PROFILE_CAT_I(a, b)
130
132#define EV_PROFILE_ZONE_FUNC() \
133 ::eve::prof::ZoneScope EV_PROFILE_CAT(ev_zone_, __LINE__)(__func__)
134
136#define EV_PROFILE_ZONE(name) \
137 ::eve::prof::ZoneScope EV_PROFILE_CAT(ev_zone_, __LINE__)(name)
138
140#define EV_PROFILE_MODULE(module, name) \
141 ::eve::prof::ZoneScope EV_PROFILE_CAT(ev_zone_, __LINE__)(name, module)
142
143#endif
#define EVENGINE_API_FOUNDATION_INLINE
仅供"只有内联成员"的类型使用的组宏:拥有者 dllexport,消费方不导入。
Definition Export.h:170
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
TokenKind kind
std::int32_t parent
std::string name
std::uint32_t count
std::uint32_t depth
Global zone collector + per-frame aggregator.
Definition Profile.h:65
RAII scoped zone (used via the EV_PROFILE_* macros).
Definition Profile.h:113
ZoneScope(const char *name, const char *module=nullptr)
Zone scope.
Definition Profile.h:116
ZoneScope(const ZoneScope &)=delete
~ZoneScope()
Zone scope.
Definition Profile.h:121
ZoneScope & operator=(const ZoneScope &)=delete
bool enabled
One completed profiling zone (pre-order, parent-linked within a thread).
Definition Profile.h:35
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