Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
FrameProfiler.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
8#include <atomic>
9#include <mutex>
10#include <vector>
11#include <string>
12#include <cstring>
13#include <cstdio>
14#ifdef __EMSCRIPTEN__
15#include <emscripten.h>
16#else
17#include <chrono>
18#endif
19
20namespace esengine {
21
29inline double es_profile_now_ms() {
30#ifdef __EMSCRIPTEN__
31 return emscripten_get_now();
32#else
33 using clock = std::chrono::steady_clock;
34 return std::chrono::duration<double, std::milli>(clock::now().time_since_epoch()).count();
35#endif
36}
37
39public:
40 static FrameProfiler& get() {
41 static FrameProfiler instance;
42 return instance;
43 }
44
45 void setEnabled(bool e) { enabled_ = e; }
46 bool enabled() const { return enabled_; }
47
48 void add(const char* name, double ms) {
49 std::lock_guard lock(mutex_);
50 for (auto& e : cur_) {
51 if (std::strcmp(e.name, name) == 0) { e.ms += ms; return; }
52 }
53 cur_.push_back({name, ms});
54 }
55
56 void counter(const char* name, double value) {
57 std::lock_guard lock(mutex_);
58 setEntry(counters_cur_, name, value);
59 }
60 void gpuScope(const char* name, double ms) {
61 std::lock_guard lock(mutex_);
62 setEntry(gpu_cur_, name, ms);
63 }
64
65 void commit() {
66 std::lock_guard lock(mutex_);
67 last_.swap(cur_);
68 cur_.clear();
69 counters_last_.swap(counters_cur_);
70 counters_cur_.clear();
71 gpu_last_.swap(gpu_cur_);
72 gpu_cur_.clear();
73 }
74
75 std::string lastJson() const {
76 std::lock_guard lock(mutex_);
77 return toJson(last_);
78 }
79 std::string lastCountersJson() const {
80 std::lock_guard lock(mutex_);
81 return toJson(counters_last_);
82 }
83 std::string lastGpuJson() const {
84 std::lock_guard lock(mutex_);
85 return toJson(gpu_last_);
86 }
87
88private:
89 struct Entry { const char* name; double ms; };
90
91 static void setEntry(std::vector<Entry>& v, const char* name, double value) {
92 for (auto& e : v) {
93 if (std::strcmp(e.name, name) == 0) { e.ms = value; return; }
94 }
95 v.push_back({name, value});
96 }
97
98 static std::string toJson(const std::vector<Entry>& v) {
99 std::string s = "{";
100 for (std::size_t i = 0; i < v.size(); ++i) {
101 if (i) s += ',';
102 char buf[32];
103 std::snprintf(buf, sizeof(buf), "%.4f", v[i].ms);
104 s += '"';
105 s += v[i].name;
106 s += "\":";
107 s += buf;
108 }
109 s += '}';
110 return s;
111 }
112
113 std::vector<Entry> cur_;
114 std::vector<Entry> last_;
115 std::vector<Entry> counters_cur_;
116 std::vector<Entry> counters_last_;
117 std::vector<Entry> gpu_cur_;
118 std::vector<Entry> gpu_last_;
119 std::atomic_bool enabled_{false};
120 mutable std::mutex mutex_;
121};
122
124public:
125 explicit ScopeTimer(const char* name)
126 : name_(name),
127 active_(FrameProfiler::get().enabled()),
128 start_(active_ ? es_profile_now_ms() : 0.0) {}
130 if (active_) FrameProfiler::get().add(name_, es_profile_now_ms() - start_);
131 }
132 ScopeTimer(const ScopeTimer&) = delete;
133 ScopeTimer& operator=(const ScopeTimer&) = delete;
134
135private:
136 const char* name_;
137 bool active_;
138 double start_;
139};
140
141#define ES_PROFILE_CONCAT_(a, b) a##b
142#define ES_PROFILE_CONCAT(a, b) ES_PROFILE_CONCAT_(a, b)
143#define ES_PROFILE_SCOPE(name) ::esengine::ScopeTimer ES_PROFILE_CONCAT(es_scope_, __LINE__)(name)
144#define ES_PROFILE_COUNTER(name, value) \
145 do { if (::esengine::FrameProfiler::get().enabled()) ::esengine::FrameProfiler::get().counter(name, static_cast<double>(value)); } while (0)
146
147} // namespace esengine
void counter(const char *name, double value)
std::string lastGpuJson() const
void gpuScope(const char *name, double ms)
std::string lastJson() const
void add(const char *name, double ms)
std::string lastCountersJson() const
static FrameProfiler & get()
ScopeTimer & operator=(const ScopeTimer &)=delete
ScopeTimer(const char *name)
ScopeTimer(const ScopeTimer &)=delete
double es_profile_now_ms()