-
Notifications
You must be signed in to change notification settings - Fork 0
/
lmemprof.cpp
309 lines (220 loc) · 5.97 KB
/
lmemprof.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <unordered_map>
#include <string>
#include <lua.hpp>
namespace memprof {
lua_Alloc original_allocator = nullptr;
class MemProf {
public:
inline bool IsEnabled() const {
return enabled_;
}
inline std::string_view GetZone() const {
return current_zone_;
}
inline size_t GetCounter() const noexcept {
return profiler_zones_.count(current_zone_) ? profiler_zones_.at(current_zone_) : 0;
}
inline size_t GetCounter(const std::string_view name) const noexcept {
return profiler_zones_.count(name) ? profiler_zones_.at(name) : 0;
}
public:
inline void Enable() {
enabled_ = true;
}
inline void Disable() {
enabled_ = false;
}
inline void SetZone(const std::string_view name) {
current_zone_ = name;
}
inline void ClearZone() {
current_zone_ = "";
}
inline void IncrementCounter(size_t number) noexcept {
profiler_zones_[current_zone_] += number;
}
inline void DecrementCounter(size_t number) noexcept {
profiler_zones_[current_zone_] -= number;
}
inline void ResetCounter(std::string_view name) noexcept {
profiler_zones_.insert_or_assign(name, size_t{0});
}
inline void ResetCounter() noexcept {
ResetCounter(current_zone_);
}
private:
bool enabled_ = false;
std::unordered_map<std::string_view, size_t> profiler_zones_;
std::string_view current_zone_;
};
MemProf prof;
namespace lua {
void* Hook(void* ud, void* ptr, size_t osize, size_t nsize) {
if (prof.IsEnabled()) {
if (osize < nsize) {
prof.IncrementCounter(nsize - osize);
}
else {
prof.DecrementCounter(osize - nsize);
}
}
return original_allocator(ud, ptr, osize, nsize);
}
#ifdef FOR_LUA
int IsEnabled(lua_State* L) {
lua_pushboolean(L, prof.IsEnabled());
return 1;
}
int GetZone(lua_State* L) {
const std::string_view name = prof.GetZone();
lua_pushlstring(L, name.data(), name.size());
return 1;
}
int Enable([[maybe_unused]] lua_State* L) {
prof.Enable();
return 0;
}
int Disable([[maybe_unused]] lua_State* L) {
prof.Disable();
return 0;
}
int SetZone(lua_State* L) {
prof.SetZone(luaL_checkstring(L, 1));
return 0;
}
int ClearZone([[maybe_unused]] lua_State* L) {
prof.ClearZone();
return 0;
}
int IncrementCounter(lua_State* L) noexcept {
prof.IncrementCounter(static_cast<size_t>(luaL_checknumber(L, 1)));
return 0;
}
int DecrementCounter(lua_State* L) noexcept {
prof.DecrementCounter(static_cast<size_t>(luaL_checknumber(L, 1)));
return 0;
}
int ResetCounter([[maybe_unused]] lua_State* L) noexcept {
prof.ResetCounter();
return 0;
}
int ResetCounterFor(lua_State* L) noexcept {
prof.ResetCounter(luaL_checkstring(L, 1));
return 0;
}
int GetCounter(lua_State* L) noexcept {
lua_pushinteger(L, static_cast<lua_Integer>(prof.GetCounter()));
return 1;
}
int GetCounterFor(lua_State* L) noexcept {
lua_pushinteger(L, static_cast<lua_Integer>(prof.GetCounter(luaL_checkstring(L, 1))));
return 1;
}
#endif // FOR_LUA
#ifdef FOR_LUAJIT
extern "C" {
__declspec(dllexport) bool IsEnabled() {
return prof.IsEnabled();
}
__declspec(dllexport) const char* GetZone() {
const std::string_view name = prof.GetZone();
return name.data();
}
__declspec(dllexport) void Enable() {
prof.Enable();
return;
}
__declspec(dllexport) void Disable() {
prof.Disable();
return;
}
__declspec(dllexport) void SetZone(const char* name) {
prof.SetZone(name);
return;
}
__declspec(dllexport) void ClearZone() {
prof.ClearZone();
return;
}
__declspec(dllexport) void IncrementCounter(size_t number) noexcept {
prof.IncrementCounter(number);
return;
}
__declspec(dllexport) void DecrementCounter(size_t number) noexcept {
prof.DecrementCounter(number);
return;
}
__declspec(dllexport) void ResetCounter() noexcept {
prof.ResetCounter();
return;
}
__declspec(dllexport) void ResetCounterFor(const char* name) noexcept {
prof.ResetCounter(name);
return;
}
__declspec(dllexport) double GetCounter() noexcept {
return static_cast<double>(prof.GetCounter());
}
__declspec(dllexport) double GetCounterFor(const char* name) noexcept {
return static_cast<double>(prof.GetCounter(name));
}
}
#endif // FOR_LUAJIT
int Handle__tostring(lua_State* L) {
lua_pushstring(L, "lmemprof_handle");
return 1;
}
int Handle__gc(lua_State* L) {
void* ud;
lua_getallocf(L, &ud);
lua_setallocf(L, memprof::original_allocator, ud);
return 0;
}
} // namespace lua
} // namespace memprof
extern "C" __declspec(dllexport) int luaopen_lmemprof(lua_State * L) noexcept {
void* ud;
lua_Alloc allocator = lua_getallocf(L, &ud);
memprof::original_allocator = allocator;
lua_setallocf(L, memprof::lua::Hook, ud);
#ifdef FOR_LUA
lua_createtable(L, 0, 7);
lua_pushcfunction(L, memprof::lua::GetZone);
lua_setfield(L, -2, "GetZone");
lua_pushcfunction(L, memprof::lua::IsEnabled);
lua_setfield(L, -2, "IsEnabled");
lua_pushcfunction(L, memprof::lua::Enable);
lua_setfield(L, -2, "Enable");
lua_pushcfunction(L, memprof::lua::Disable);
lua_setfield(L, -2, "Disable");
lua_pushcfunction(L, memprof::lua::SetZone);
lua_setfield(L, -2, "SetZone");
lua_pushcfunction(L, memprof::lua::ClearZone);
lua_setfield(L, -2, "ClearZone");
lua_pushcfunction(L, memprof::lua::IncrementCounter);
lua_setfield(L, -2, "IncrementCounter");
lua_pushcfunction(L, memprof::lua::DecrementCounter);
lua_setfield(L, -2, "DecrementCounter");
lua_pushcfunction(L, memprof::lua::ResetCounter);
lua_setfield(L, -2, "ResetCounter");
lua_pushcfunction(L, memprof::lua::ResetCounterFor);
lua_setfield(L, -2, "ResetCounterFor");
lua_pushcfunction(L, memprof::lua::GetCounter);
lua_setfield(L, -2, "GetCounter");
lua_pushcfunction(L, memprof::lua::GetCounterFor);
lua_setfield(L, -2, "GetCounterFor");
#endif // FOR_LUA
lua_newuserdata(L, 0);
lua_createtable(L, 0, 2);
lua_pushcfunction(L, memprof::lua::Handle__tostring);
lua_setfield(L, -2, "__tostring");
lua_pushcfunction(L, memprof::lua::Handle__gc);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -2);
luaL_ref(L, LUA_REGISTRYINDEX);
#ifdef FOR_LUA
return 1;
#elif FOR_LUAJIT
return 0;
#endif
}