Skip to content

Commit

Permalink
Add tracy.LuaTracyPlot and tracy.LuaTracyPlotConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Beherith authored and sprunk committed Aug 18, 2024
1 parent c1e2610 commit 86b9481
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 4 deletions.
1 change: 1 addition & 0 deletions rts/Lua/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set(sources_engine_Lua
"${CMAKE_CURRENT_SOURCE_DIR}/LuaSyncedRead.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/LuaSyncedTable.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/LuaTextures.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/LuaTracyExtra.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/LuaAtlasTextures.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/LuaUI.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/LuaUICommand.cpp"
Expand Down
8 changes: 7 additions & 1 deletion rts/Lua/LuaHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "LuaOpenGL.h"
#include "LuaBitOps.h"
#include "LuaMathExtra.h"
#include "LuaTracyExtra.h"
#include "LuaUtils.h"
#include "LuaZip.h"
#include "Game/Game.h"
Expand Down Expand Up @@ -147,6 +148,11 @@ CLuaHandle::CLuaHandle(const string& _name, int _order, bool _userMode, bool _sy

// register tracy functions in global scope
tracy::LuaRegister(L);
#ifdef TRACY_ENABLE
lua_getglobal(L, "tracy");
LuaTracyExtra::PushEntries(L);
lua_pop(L, 1);
#endif
}


Expand Down Expand Up @@ -494,7 +500,7 @@ bool CLuaHandle::LoadCode(lua_State* L, std::string code, const string& debug)

const LuaUtils::ScopedDebugTraceBack traceBack(L);

tracy::LuaRemove(code.data());
LuaUtils::TracyRemoveAlsoExtras(code.data());
const int error = luaL_loadbuffer(L, code.c_str(), code.size(), debug.c_str());

if (error != 0) {
Expand Down
4 changes: 2 additions & 2 deletions rts/Lua/LuaParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ bool LuaParser::Execute()
char errorBuf[4096] = {0};
int errorNum = 0;

tracy::LuaRemove(code.data());
LuaUtils::TracyRemoveAlsoExtras(code.data());
if ((errorNum = luaL_loadbuffer(L, code.c_str(), code.size(), codeLabel.c_str())) != 0) {
SNPRINTF(errorBuf, sizeof(errorBuf), "[loadbuf] error %d (\"%s\") in %s", errorNum, lua_tostring(L, -1), codeLabel.c_str());
LUA_CLOSE(&L);
Expand Down Expand Up @@ -643,7 +643,7 @@ int LuaParser::Include(lua_State* L)
lua_error(L);
}

tracy::LuaRemove(code.data());
LuaUtils::TracyRemoveAlsoExtras(code.data());
int error = luaL_loadbuffer(L, code.c_str(), code.size(), filename.c_str());
if (error != 0) {
char buf[1024];
Expand Down
82 changes: 82 additions & 0 deletions rts/Lua/LuaTracyExtra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* This file is part of the Recoil engine (GPL v2 or later). */

#include "LuaInclude.h"
#include "LuaUtils.h"

#include "LuaTracyExtra.h"
#include "System/Misc/TracyDefs.h"

#include <tracy/Tracy.hpp>
#include <common/TracyQueue.hpp>

#include "System/Log/ILog.h"

/******************************************************************************
* tracy extensions
* @module TracyExtra
* @see rts/Lua/LuaTracyExtra.cpp
******************************************************************************/

bool LuaTracyExtra::PushEntries(lua_State* L)
{
LuaPushNamedCFunc(L, "LuaTracyPlot" , LuaTracyPlot );
LuaPushNamedCFunc(L, "LuaTracyPlotConfig", LuaTracyPlotConfig);
return true;
}



/*** Configure custom appearence for a Tracy plot for use in debugging or profiling
*
* @function tracy.LuaTracyPlotConfig
* @string plotName which should be customized
* @string[opt] plotFormatType "Number"|"Percentage"|"Memory", default "Number"
* @bool[opt] step stepwise chart, default true is stepwise
* @bool[opt] fill color fill, default false is no fill
* @number[opt] color unit32 number as BGR color, default white
* @treturn nil
*/

int LuaTracyExtra::LuaTracyPlotConfig(lua_State* L)
{
const auto plotName = luaL_checkstring(L, 1);
const auto plotFormatTypeString = luaL_optstring(L, 2, "");
const auto step = luaL_optboolean(L, 3, true); // stepwise default
const auto fill = luaL_optboolean(L, 4, false); // no fill default
const uint32_t color = luaL_optint(L, 5, 0xFFFFFF); // white default

tracy::PlotFormatType plotFormatType;
switch (plotFormatTypeString[0]) {
case 'p': case 'P': plotFormatType = tracy::PlotFormatType::Percentage; break;
case 'm': case 'M': plotFormatType = tracy::PlotFormatType::Memory; break;
default: plotFormatType = tracy::PlotFormatType::Number; break;
}

auto plot = tracyLuaPlots.find(plotName);
if (plot == tracyLuaPlots.end())
plot = tracyLuaPlots.emplace(plotName).first;

TracyPlotConfig(plot->c_str(), plotFormatType, step, fill, color);
return 0;
}


/*** Update a Tracy Plot with a value
*
* @function tracy.LuaTracyPlot
* @string plotName which LuaPlot should be updated
* @number plotvalue the number to show on the Tracy plot
* @treturn nil
*/
int LuaTracyExtra::LuaTracyPlot(lua_State* L)
{
const auto plotName = luaL_checkstring(L, 1);
const auto plotValue = luaL_checkfloat(L, 2);

auto plot = tracyLuaPlots.find(plotName);
if (plot == tracyLuaPlots.end())
plot = tracyLuaPlots.emplace(plotName).first;

TracyPlot(plot->c_str(), plotValue);
return 0;
}
26 changes: 26 additions & 0 deletions rts/Lua/LuaTracyExtra.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* This file is part of the Recoil engine (GPL v2 or later), see LICENSE.html */

#pragma once

#include <functional>
#include <set>
#include <string>

struct lua_State;

class LuaTracyExtra {

private:
/* Tracy seems to want unique, unchanging strings to be passed to
* its API, so we need to immanentize the ephemeral Lua strings
* and store them.
*
* NB: strings here are never really cleaned up, but the use case assumes
* that they live a long time and there's just a handful of them. */
inline static std::set <std::string, std::less<>> tracyLuaPlots;

static int LuaTracyPlot(lua_State* L);
static int LuaTracyPlotConfig(lua_State* L);
public:
static bool PushEntries(lua_State* L);
};
2 changes: 2 additions & 0 deletions rts/Lua/LuaUnsyncedCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
#undef CreateDirectory
#undef Yield

#include <tracy/Tracy.hpp>
#include <common/TracyQueue.hpp>

/******************************************************************************
* Callouts to set state
Expand Down
44 changes: 44 additions & 0 deletions rts/Lua/LuaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define SCOPED_TIMER(x)
#endif

#include <tracy/Tracy.hpp>
#include <tracy/TracyLua.hpp>

static const int maxDepth = 16;

Expand Down Expand Up @@ -1798,3 +1800,45 @@ void LuaUtils::PushAttackerInfo(lua_State* L, const CUnit* const attacker)
lua_pushnil(L);
}
#endif


void LuaUtils::TracyRemoveAlsoExtras(char* script)
{
// tracy's built-in remover; does not handle our local TracyExtra functions
tracy::LuaRemove(script);

#ifndef TRACY_ENABLE
// Our extras are handled manually below, the same way Tracy does.
// Code is on BSD-3 licence, (c) 2017 Bartosz Taudul aka wolfpld

const auto FindEnd = [] (char *ptr) {
unsigned int cnt = 1;
while (cnt) {
if (*ptr == '(') ++ cnt;
else if (*ptr == ')') -- cnt;
++ ptr;
}
return ptr;
};

const auto Wipe = [&script, FindEnd] (size_t offset) {
const auto end = FindEnd(script + offset);
memset(script, ' ', end - script);
script = end;
};

while (*script) {
if (strncmp(script, "tracy.LuaTracyPlot", 18)) {
++ script;
continue;
}

if (!strncmp(script + 18, "Config(", 7))
Wipe(18 + 7);
else if (!strncmp(script + 18, "(", 1))
Wipe(18 + 1);
else
script += 18;
}
#endif
}
2 changes: 2 additions & 0 deletions rts/Lua/LuaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ class LuaUtils {
static const TObj* IdToObject(int id, const char* func = nullptr);
template<typename TObj>
static const TObj* SolIdToObject(int id, const char* func = nullptr);

static void TracyRemoveAlsoExtras(char *script);
};


Expand Down
3 changes: 2 additions & 1 deletion rts/Lua/LuaVFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ int LuaVFS::Include(lua_State* L, bool synced)
lua_error(L);
}

tracy::LuaRemove(fileData.data());
LuaUtils::TracyRemoveAlsoExtras(fileData.data());
if ((luaError = luaL_loadbuffer(L, fileData.c_str(), fileData.size(), fileName.c_str())) != 0) {
const auto buf = fmt::format("[LuaVFS::{}(synced={})][loadbuf] file={} error={} ({}) cenv={} vfsmode={}", __func__, synced, fileName, luaError, lua_tostring(L, -1), hasCustomEnv, mode);
lua_pushlstring(L, buf.c_str(), buf.size());
Expand Down Expand Up @@ -229,6 +229,7 @@ int LuaVFS::LoadFile(lua_State* L, bool synced)

string data;
if (LoadFileWithModes(filename, data, GetModes(L, 2, synced)) == 1) {
LuaUtils::TracyRemoveAlsoExtras(data.data());
lua_pushsstring(L, data);
return 1;
}
Expand Down

0 comments on commit 86b9481

Please sign in to comment.