diff options
author | worktycho <work.tycho@gmail.com> | 2015-09-29 14:32:30 +0200 |
---|---|---|
committer | worktycho <work.tycho@gmail.com> | 2015-09-29 14:32:30 +0200 |
commit | c76a89fbec7b503aba070e5189c566662231e7b0 (patch) | |
tree | 8ebb9154a78f78ca4d81e39d3c0604afdf0cadf3 /src/Bindings/LuaState.cpp | |
parent | Merge pull request #2507 from cuberite/LuaStrictStrings (diff) | |
parent | Added LuaState tracker and memory stats logging. (diff) | |
download | cuberite-c76a89fbec7b503aba070e5189c566662231e7b0.tar cuberite-c76a89fbec7b503aba070e5189c566662231e7b0.tar.gz cuberite-c76a89fbec7b503aba070e5189c566662231e7b0.tar.bz2 cuberite-c76a89fbec7b503aba070e5189c566662231e7b0.tar.lz cuberite-c76a89fbec7b503aba070e5189c566662231e7b0.tar.xz cuberite-c76a89fbec7b503aba070e5189c566662231e7b0.tar.zst cuberite-c76a89fbec7b503aba070e5189c566662231e7b0.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Bindings/LuaState.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index 1509bd19f..385b1f659 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -43,6 +43,76 @@ const cLuaState::cRet cLuaState::Return = {}; //////////////////////////////////////////////////////////////////////////////// +// cLuaStateTracker: + +void cLuaStateTracker::Add(cLuaState & a_LuaState) +{ + auto & Instance = Get(); + cCSLock Lock(Instance.m_CSLuaStates); + Instance.m_LuaStates.push_back(&a_LuaState); +} + + + + +void cLuaStateTracker::Del(cLuaState & a_LuaState) +{ + auto & Instance = Get(); + cCSLock Lock(Instance.m_CSLuaStates); + Instance.m_LuaStates.erase( + std::remove_if( + Instance.m_LuaStates.begin(), Instance.m_LuaStates.end(), + [&a_LuaState](cLuaStatePtr a_StoredLuaState) + { + return (&a_LuaState == a_StoredLuaState); + } + ), + Instance.m_LuaStates.end() + ); +} + + + + + +AString cLuaStateTracker::GetStats(void) +{ + auto & Instance = Get(); + cCSLock Lock(Instance.m_CSLuaStates); + AString res; + int Total = 0; + for (auto state: Instance.m_LuaStates) + { + int Mem = 0; + if (!state->Call("collectgarbage", "count", cLuaState::Return, Mem)) + { + res.append(Printf("Cannot query memory for state \"%s\"\n", state->GetSubsystemName().c_str())); + } + else + { + res.append(Printf("State \"%s\" is using %d KiB of memory\n", state->GetSubsystemName().c_str(), Mem)); + Total += Mem; + } + } + res.append(Printf("Total memory used by Lua: %d KiB\n", Total)); + return res; +} + + + + + +cLuaStateTracker & cLuaStateTracker::Get(void) +{ + static cLuaStateTracker Inst; // The singleton + return Inst; +} + + + + + +//////////////////////////////////////////////////////////////////////////////// // cLuaState: cLuaState::cLuaState(const AString & a_SubsystemName) : @@ -98,6 +168,7 @@ void cLuaState::Create(void) m_LuaState = lua_open(); luaL_openlibs(m_LuaState); m_IsOwned = true; + cLuaStateTracker::Add(*this); } @@ -133,6 +204,7 @@ void cLuaState::Close(void) Detach(); return; } + cLuaStateTracker::Del(*this); lua_close(m_LuaState); m_LuaState = nullptr; m_IsOwned = false; |