1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-13 12:40:04 +00:00

Merge branch 'debug_lua_memory' into 'master'

Additional information in Lua profiler

See merge request OpenMW/openmw!2612
This commit is contained in:
psi29a 2023-01-18 14:33:05 +00:00
commit ee980721b8
7 changed files with 38 additions and 7 deletions

View File

@ -18,6 +18,7 @@
#include <components/lua/utilpackage.hpp>
#include <components/lua_ui/content.hpp>
#include <components/lua_ui/util.hpp>
#include "../mwbase/windowmanager.hpp"
@ -639,6 +640,9 @@ namespace MWLua
out << "Total memory usage:";
outMemSize(mLua.getTotalMemoryUsage());
out << "\n";
out << "LuaUtil::ScriptsContainer count: " << LuaUtil::ScriptsContainer::getInstanceCount() << "\n";
out << "LuaUi::Content count: " << LuaUi::Content::getInstanceCount() << "\n";
out << "\n";
out << "small alloc max size = " << smallAllocSize << " (section [Lua] in settings.cfg)\n";
out << "Smaller values give more information for the profiler, but increase performance overhead.\n";
out << " Memory allocations <= " << smallAllocSize << " bytes:";

View File

@ -115,7 +115,7 @@ namespace MWLua
};
uiContent["add"]
= [](LuaUi::Content& content, const sol::table& table) { content.insert(content.size(), table); };
uiContent["indexOf"] = [](LuaUi::Content& content, const sol::table& table) -> sol::optional<size_t> {
uiContent["indexOf"] = [](const LuaUi::Content& content, const sol::table& table) -> sol::optional<size_t> {
size_t index = content.indexOf(table);
if (index < content.size())
return toLuaIndex(index);
@ -123,8 +123,9 @@ namespace MWLua
return sol::nullopt;
};
{
auto pairs = [](LuaUi::Content& content) {
auto next = [](LuaUi::Content& content, size_t i) -> sol::optional<std::tuple<size_t, sol::table>> {
auto pairs = [](const LuaUi::Content& content) {
auto next
= [](const LuaUi::Content& content, size_t i) -> sol::optional<std::tuple<size_t, sol::table>> {
if (i < content.size())
return std::make_tuple(i + 1, content.at(i));
else

View File

@ -15,11 +15,14 @@ namespace LuaUtil
static constexpr std::string_view HANDLER_LOAD = "onLoad";
static constexpr std::string_view HANDLER_INTERFACE_OVERRIDE = "onInterfaceOverride";
int64_t ScriptsContainer::sInstanceCount = 0;
ScriptsContainer::ScriptsContainer(LuaUtil::LuaState* lua, std::string_view namePrefix)
: mNamePrefix(namePrefix)
, mLua(*lua)
, mThis(std::make_shared<ScriptsContainer*>(this))
{
sInstanceCount++;
registerEngineHandlers({ &mUpdateHandlers });
mPublicInterfaces = sol::table(lua->sol(), sol::create);
addPackage("openmw.interfaces", mPublicInterfaces);
@ -476,6 +479,7 @@ namespace LuaUtil
ScriptsContainer::~ScriptsContainer()
{
sInstanceCount--;
for (auto& [_, script] : mScripts)
script.mHiddenData[sScriptIdKey] = sol::nil;
*mThis = nullptr;

View File

@ -155,6 +155,7 @@ namespace LuaUtil
int64_t mMemoryUsage = 0; // bytes
};
void collectStats(std::vector<ScriptStats>& stats) const;
static int64_t getInstanceCount() { return sInstanceCount; }
protected:
struct Handler
@ -265,6 +266,8 @@ namespace LuaUtil
std::map<int, int64_t> mRemovedScriptsMemoryUsage;
std::shared_ptr<ScriptsContainer*> mThis; // used by LuaState to track ownership of memory allocations
static int64_t sInstanceCount; // debug information, shown in Lua profiler
};
// Wrapper for a Lua function.

View File

@ -2,8 +2,11 @@
namespace LuaUi
{
int64_t Content::sInstanceCount = 0;
Content::Content(const sol::table& table)
{
sInstanceCount++;
size_t size = table.size();
for (size_t index = 0; index < size; ++index)
{
@ -94,7 +97,7 @@ namespace LuaUi
return index;
}
size_t Content::indexOf(const sol::table& table)
size_t Content::indexOf(const sol::table& table) const
{
auto it = std::find(mOrdered.begin(), mOrdered.end(), table);
if (it == mOrdered.end())

View File

@ -13,7 +13,20 @@ namespace LuaUi
public:
using iterator = std::vector<sol::table>::iterator;
Content() {}
Content() { sInstanceCount++; }
~Content() { sInstanceCount--; }
Content(const Content& c)
{
this->mNamed = c.mNamed;
this->mOrdered = c.mOrdered;
sInstanceCount++;
}
Content(Content&& c)
{
this->mNamed = std::move(c.mNamed);
this->mOrdered = std::move(c.mOrdered);
sInstanceCount++;
}
// expects a Lua array - a table with keys from 1 to n without any nil values in between
// any other keys are ignored
@ -29,11 +42,14 @@ namespace LuaUi
sol::table at(std::string_view name) const;
size_t remove(size_t index);
size_t remove(std::string_view name);
size_t indexOf(const sol::table& table);
size_t indexOf(const sol::table& table) const;
static int64_t getInstanceCount() { return sInstanceCount; }
private:
std::map<std::string, size_t, std::less<>> mNamed;
std::vector<sol::table> mOrdered;
static int64_t sInstanceCount; // debug information, shown in Lua profiler
};
}

View File

@ -65,7 +65,7 @@ namespace LuaUi
}
if (!contentObj.is<Content>())
throw std::logic_error("Layout content field must be a openmw.ui.content");
Content content = contentObj.as<Content>();
const Content& content = contentObj.as<Content>();
result.resize(content.size());
size_t minSize = std::min(children.size(), content.size());
for (size_t i = 0; i < minSize; i++)