Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
ygopro-core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
ygopro-core
Commits
c839efc3
Commit
c839efc3
authored
Apr 02, 2025
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add memory tracer
parent
ca5bd0f5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
1 deletion
+127
-1
LuaMemTracker.cpp
LuaMemTracker.cpp
+73
-0
LuaMemTracker.h
LuaMemTracker.h
+38
-0
interpreter.cpp
interpreter.cpp
+10
-1
interpreter.h
interpreter.h
+6
-0
No files found.
LuaMemTracker.cpp
0 → 100644
View file @
c839efc3
#include "LuaMemTracker.h"
LuaMemTracker
::
LuaMemTracker
(
lua_Alloc
alloc_func
,
void
*
ud
,
size_t
mem_limit
)
:
real_alloc
(
alloc_func
),
real_ud
(
ud
),
limit
(
mem_limit
)
{
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
time_t
now
=
time
(
nullptr
);
char
filename
[
64
];
std
::
snprintf
(
filename
,
sizeof
(
filename
),
"memtrace-%ld.log"
,
static_cast
<
long
>
(
now
));
log_file
=
std
::
fopen
(
filename
,
"a"
);
if
(
log_file
)
{
std
::
fprintf
(
log_file
,
"---- Lua memory tracking started ----
\n
"
);
}
#endif
}
LuaMemTracker
::~
LuaMemTracker
()
{
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
if
(
log_file
)
{
std
::
fprintf
(
log_file
,
"---- Lua memory tracking ended ----
\n
"
);
std
::
fclose
(
log_file
);
log_file
=
nullptr
;
}
#endif
}
void
*
LuaMemTracker
::
AllocThunk
(
void
*
ud
,
void
*
ptr
,
size_t
osize
,
size_t
nsize
)
{
return
static_cast
<
LuaMemTracker
*>
(
ud
)
->
Alloc
(
ptr
,
osize
,
nsize
);
}
void
*
LuaMemTracker
::
Alloc
(
void
*
ptr
,
size_t
osize
,
size_t
nsize
)
{
if
(
nsize
==
0
)
{
total_allocated
-=
osize
;
return
real_alloc
(
real_ud
,
ptr
,
osize
,
nsize
);
}
else
{
if
(
limit
&&
(
total_allocated
-
osize
+
nsize
>
limit
))
{
return
nullptr
;
// over limit
}
void
*
newptr
=
real_alloc
(
real_ud
,
ptr
,
osize
,
nsize
);
if
(
newptr
)
{
total_allocated
=
total_allocated
-
osize
+
nsize
;
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
write_log
();
#endif
}
return
newptr
;
}
}
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
void
LuaMemTracker
::
write_log
()
{
if
(
!
log_file
)
return
;
time_t
now
=
time
(
nullptr
);
struct
tm
*
tm_info
=
localtime
(
&
now
);
char
time_buf
[
32
];
std
::
strftime
(
time_buf
,
sizeof
(
time_buf
),
"%Y-%m-%d %H:%M:%S"
,
tm_info
);
if
(
total_allocated
>
max_used
)
max_used
=
total_allocated
;
if
(
limit
)
std
::
fprintf
(
log_file
,
"%s | used = %zu bytes | max_used = %zu bytes | limit = %zu
\n
"
,
time_buf
,
total_allocated
,
max_used
,
limit
);
else
std
::
fprintf
(
log_file
,
"%s | used = %zu bytes | max_used = %zu bytes | limit = unlimited
\n
"
,
time_buf
,
total_allocated
,
max_used
);
std
::
fflush
(
log_file
);
// 确保实时写入磁盘
}
#endif
LuaMemTracker.h
0 → 100644
View file @
c839efc3
#ifndef LUA_MEM_TRACKER_H
#define LUA_MEM_TRACKER_H
#include <cstdlib>
#include <cstddef>
#include <lua.h>
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
#include <ctime> // time_t
#include <cstdio> // FILE*, fopen, fprintf
#endif
class
LuaMemTracker
{
public:
LuaMemTracker
(
lua_Alloc
real_alloc
,
void
*
real_ud
,
size_t
mem_limit
=
0
);
~
LuaMemTracker
();
static
void
*
AllocThunk
(
void
*
ud
,
void
*
ptr
,
size_t
osize
,
size_t
nsize
);
void
*
Alloc
(
void
*
ptr
,
size_t
osize
,
size_t
nsize
);
size_t
get_total
()
const
{
return
total_allocated
;
}
size_t
get_limit
()
const
{
return
limit
;
}
private:
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
FILE
*
log_file
=
nullptr
;
void
write_log
();
#endif
lua_Alloc
real_alloc
;
void
*
real_ud
;
size_t
total_allocated
=
0
;
size_t
limit
=
0
;
#ifdef YGOPRO_LOG_LUA_MEMORY_SIZE
size_t
max_used
=
0
;
// for logging purposes, to track peak memory usage
#endif
};
#endif // LUA_MEM_TRACKER_H
interpreter.cpp
View file @
c839efc3
...
@@ -15,7 +15,15 @@
...
@@ -15,7 +15,15 @@
#include "interpreter.h"
#include "interpreter.h"
interpreter
::
interpreter
(
duel
*
pd
)
:
coroutines
(
256
)
{
interpreter
::
interpreter
(
duel
*
pd
)
:
coroutines
(
256
)
{
lua_state
=
luaL_newstate
();
lua_State
*
tmp_L
=
luaL_newstate
();
// 只是为了拿默认 alloc
lua_Alloc
raw_alloc
;
void
*
raw_ud
;
raw_alloc
=
lua_getallocf
(
tmp_L
,
&
raw_ud
);
lua_close
(
tmp_L
);
mem_tracker
=
new
LuaMemTracker
(
raw_alloc
,
raw_ud
,
YGOPRO_LUA_MEMORY_SIZE
);
lua_state
=
lua_newstate
(
LuaMemTracker
::
AllocThunk
,
mem_tracker
);
current_state
=
lua_state
;
current_state
=
lua_state
;
pduel
=
pd
;
pduel
=
pd
;
std
::
memcpy
(
lua_getextraspace
(
lua_state
),
&
pd
,
LUA_EXTRASPACE
);
//set_duel_info
std
::
memcpy
(
lua_getextraspace
(
lua_state
),
&
pd
,
LUA_EXTRASPACE
);
//set_duel_info
...
@@ -144,6 +152,7 @@ interpreter::interpreter(duel* pd): coroutines(256) {
...
@@ -144,6 +152,7 @@ interpreter::interpreter(duel* pd): coroutines(256) {
}
}
interpreter
::~
interpreter
()
{
interpreter
::~
interpreter
()
{
lua_close
(
lua_state
);
lua_close
(
lua_state
);
delete
mem_tracker
;
}
}
void
interpreter
::
register_card
(
card
*
pcard
)
{
void
interpreter
::
register_card
(
card
*
pcard
)
{
if
(
!
pcard
)
if
(
!
pcard
)
...
...
interpreter.h
View file @
c839efc3
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
#include <list>
#include <list>
#include <vector>
#include <vector>
#include <cstdio>
#include <cstdio>
#include "LuaMemTracker.h"
class
card
;
class
card
;
struct
card_data
;
struct
card_data
;
...
@@ -54,6 +55,7 @@ public:
...
@@ -54,6 +55,7 @@ public:
int32_t
call_depth
;
int32_t
call_depth
;
int32_t
disable_action_check
;
int32_t
disable_action_check
;
int32_t
preloaded
;
int32_t
preloaded
;
LuaMemTracker
*
mem_tracker
=
nullptr
;
explicit
interpreter
(
duel
*
pd
);
explicit
interpreter
(
duel
*
pd
);
~
interpreter
();
~
interpreter
();
...
@@ -100,4 +102,8 @@ public:
...
@@ -100,4 +102,8 @@ public:
#define COROUTINE_YIELD 2
#define COROUTINE_YIELD 2
#define COROUTINE_ERROR 3
#define COROUTINE_ERROR 3
#ifndef YGOPRO_LUA_MEMORY_SIZE
#define YGOPRO_LUA_MEMORY_SIZE 0
#endif
#endif
/* INTERPRETER_H_ */
#endif
/* INTERPRETER_H_ */
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment