forked from rosebudai/OpenRCT2-Web
Implement profiler (#16194)
* Implement profiling API * Add console commands for the profiler * Remove accidental line * Correct csv output * Add copyright notice * Add missing override * Add default virtual destructor * Explicitly pass template argument * Use static * Add plugin API for profiler * Add more profile calls * Workaround for GCC hopefully * Add missing static keyword * Use uint64 for call count * Reduce name length see if CI passes * Improve handling of function names * Work around (broken) static inline variables * Fix missing include * Disable profiler for clang 5 and older * Update copyright date * Profile UpdateAllMiscEntities * Apply review suggestions Co-authored-by: Ted John <ted@brambles.org>
This commit is contained in:
1 parent
e78704f48f
commit
993b168bfd
28 files changed
+678
-8
No files matched your search
Vendored
+19
@@ -39,6 +39,8 @@ declare global {
|
||||
var scenario: Scenario;
|
||||
/** APIs for the climate and weather. */
|
||||
var climate: Climate;
|
||||
/** APIs for performance profiling. */
|
||||
var profiler: Profiler;
|
||||
/**
|
||||
* APIs for creating and editing title sequences.
|
||||
* These will only be available to clients that are not running headless mode.
|
||||
@@ -2697,4 +2699,21 @@ declare global {
|
||||
start: number;
|
||||
count: number;
|
||||
}
|
||||
|
||||
interface Profiler {
|
||||
getData(): ProfiledFunction[];
|
||||
start(): void;
|
||||
stop(): void;
|
||||
reset(): void;
|
||||
}
|
||||
|
||||
interface ProfiledFunction {
|
||||
readonly name: string;
|
||||
readonly callCount: number;
|
||||
readonly minTime: number;
|
||||
readonly maxTime: number;
|
||||
readonly totalTime: number;
|
||||
readonly parents: number[];
|
||||
readonly children: number[];
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,7 @@
|
||||
#include "platform/Crash.h"
|
||||
#include "platform/Platform2.h"
|
||||
#include "platform/platform.h"
|
||||
#include "profiling/Profiling.h"
|
||||
#include "ride/TrackData.h"
|
||||
#include "ride/TrackDesignRepository.h"
|
||||
#include "scenario/Scenario.h"
|
||||
@@ -1063,9 +1064,7 @@ namespace OpenRCT2
|
||||
|
||||
if (ShouldDraw())
|
||||
{
|
||||
_drawingEngine->BeginDraw();
|
||||
_painter->Paint(*_drawingEngine);
|
||||
_drawingEngine->EndDraw();
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1099,14 +1098,23 @@ namespace OpenRCT2
|
||||
const float alpha = std::min(_ticksAccumulator / GAME_UPDATE_TIME_MS, 1.0f);
|
||||
tweener.Tween(alpha);
|
||||
|
||||
_drawingEngine->BeginDraw();
|
||||
_painter->Paint(*_drawingEngine);
|
||||
_drawingEngine->EndDraw();
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
|
||||
void Draw()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
_drawingEngine->BeginDraw();
|
||||
_painter->Paint(*_drawingEngine);
|
||||
_drawingEngine->EndDraw();
|
||||
}
|
||||
|
||||
void Tick()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
// TODO: This variable has been never "variable" in time, some code expects
|
||||
// this to be 40Hz (25 ms). Refactor this once the UI is decoupled.
|
||||
gCurrentDeltaTime = static_cast<uint32_t>(GAME_UPDATE_TIME_MS * 1000.0f);
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "management/NewsItem.h"
|
||||
#include "network/network.h"
|
||||
#include "platform/Platform2.h"
|
||||
#include "profiling/Profiling.h"
|
||||
#include "ride/Vehicle.h"
|
||||
#include "scenario/Scenario.h"
|
||||
#include "scripting/ScriptEngine.h"
|
||||
@@ -55,6 +56,8 @@ GameState::GameState()
|
||||
*/
|
||||
void GameState::InitAll(int32_t mapSize)
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
gInMapInitCode = true;
|
||||
gCurrentTicks = 0;
|
||||
|
||||
@@ -93,6 +96,8 @@ void GameState::InitAll(int32_t mapSize)
|
||||
*/
|
||||
void GameState::Tick()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
gInUpdateCode = true;
|
||||
|
||||
// Normal game play will update only once every GAME_UPDATE_TIME_MS
|
||||
@@ -241,6 +246,8 @@ void GameState::Tick()
|
||||
|
||||
void GameState::UpdateLogic(LogicTimings* timings)
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
auto start_time = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto report_time = [timings, start_time](LogicTimePart part) {
|
||||
@@ -386,6 +393,8 @@ void GameState::UpdateLogic(LogicTimings* timings)
|
||||
|
||||
void GameState::CreateStateSnapshot()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
IGameStateSnapshots* snapshots = GetContext()->GetGameStateSnapshots();
|
||||
|
||||
auto& snapshot = snapshots->CreateSnapshot();
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "../localisation/Localisation.h"
|
||||
#include "../network/network.h"
|
||||
#include "../platform/platform.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../scenario/Scenario.h"
|
||||
#include "../scripting/Duktape.hpp"
|
||||
#include "../scripting/HookEngine.h"
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "../entity/Staff.h"
|
||||
#include "../interface/Viewport.h"
|
||||
#include "../peep/RideUseSystem.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../ride/Vehicle.h"
|
||||
#include "../scenario/Scenario.h"
|
||||
#include "Balloon.h"
|
||||
@@ -395,6 +396,8 @@ template<typename... T> void MiscUpdateAllTypes()
|
||||
*/
|
||||
void UpdateAllMiscEntities()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
MiscUpdateAllTypes<
|
||||
SteamParticle, MoneyEffect, VehicleCrashParticle, ExplosionCloud, CrashSplashParticle, ExplosionFlare, JumpingFountain,
|
||||
Balloon, Duck>();
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "../network/network.h"
|
||||
#include "../paint/Paint.h"
|
||||
#include "../peep/GuestPathfinding.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../ride/Ride.h"
|
||||
#include "../ride/RideData.h"
|
||||
#include "../ride/ShopItem.h"
|
||||
@@ -197,6 +198,8 @@ int32_t peep_get_staff_count()
|
||||
*/
|
||||
void peep_update_all()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (gScreenFlags & SCREEN_FLAGS_EDITOR)
|
||||
return;
|
||||
|
||||
@@ -1163,6 +1166,8 @@ void peep_stop_crowd_noise()
|
||||
*/
|
||||
void peep_update_crowd_noise()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (OpenRCT2::Audio::gGameSoundsOff)
|
||||
return;
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
#include "../object/ObjectManager.h"
|
||||
#include "../object/ObjectRepository.h"
|
||||
#include "../platform/platform.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../ride/Ride.h"
|
||||
#include "../ride/RideData.h"
|
||||
#include "../ride/Vehicle.h"
|
||||
@@ -1725,6 +1726,53 @@ static int32_t cc_add_news_item([[maybe_unused]] InteractiveConsole& console, [[
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t cc_profiler_reset([[maybe_unused]] InteractiveConsole& console, [[maybe_unused]] const arguments_t& argv)
|
||||
{
|
||||
OpenRCT2::Profiling::ResetData();
|
||||
return 0;
|
||||
}
|
||||
static int32_t cc_profiler_start([[maybe_unused]] InteractiveConsole& console, [[maybe_unused]] const arguments_t& argv)
|
||||
{
|
||||
if (!OpenRCT2::Profiling::IsEnabled())
|
||||
console.WriteLine("Started profiler");
|
||||
OpenRCT2::Profiling::Enable();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t cc_profiler_exportcsv([[maybe_unused]] InteractiveConsole& console, [[maybe_unused]] const arguments_t& argv)
|
||||
{
|
||||
if (argv.size() < 1)
|
||||
{
|
||||
console.WriteLineError("Missing argument: <file path>");
|
||||
return 1;
|
||||
}
|
||||
|
||||
const auto& csvFilePath = argv[0];
|
||||
if (!OpenRCT2::Profiling::ExportCSV(csvFilePath))
|
||||
{
|
||||
console.WriteFormatLine("Unable to export CSV file to %s", csvFilePath.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
console.WriteFormatLine("Wrote file CSV file: \"%s\"", csvFilePath.c_str());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t cc_profiler_stop([[maybe_unused]] InteractiveConsole& console, [[maybe_unused]] const arguments_t& argv)
|
||||
{
|
||||
if (OpenRCT2::Profiling::IsEnabled())
|
||||
console.WriteLine("Stopped profiler");
|
||||
OpenRCT2::Profiling::Disable();
|
||||
|
||||
// Export to CSV if argument is provided.
|
||||
if (argv.size() >= 1)
|
||||
{
|
||||
return cc_profiler_exportcsv(console, argv);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
using console_command_func = int32_t (*)(InteractiveConsole& console, const arguments_t& argv);
|
||||
struct console_command
|
||||
{
|
||||
@@ -1829,6 +1877,10 @@ static constexpr const console_command console_command_table[] = {
|
||||
"replay_normalise <input file> <output file>" },
|
||||
{ "mp_desync", cc_mp_desync, "Forces a multiplayer desync",
|
||||
"cc_mp_desync [desync_type, 0 = Random t-shirt color on random guest, 1 = Remove random guest ]" },
|
||||
{ "profiler_reset", cc_profiler_reset, "Resets the profiler data.", "profiler_reset" },
|
||||
{ "profiler_start", cc_profiler_start, "Starts the profiler.", "profiler_start" },
|
||||
{ "profiler_stop", cc_profiler_stop, "Stops the profiler.", "profiler_stop [<output file>]" },
|
||||
{ "profiler_exportcsv", cc_profiler_exportcsv, "Exports the current profiler data.", "profiler_exportcsv <output file>" },
|
||||
};
|
||||
|
||||
static int32_t cc_windows(InteractiveConsole& console, [[maybe_unused]] const arguments_t& argv)
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "../entity/Guest.h"
|
||||
#include "../entity/Staff.h"
|
||||
#include "../paint/Paint.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../ride/Ride.h"
|
||||
#include "../ride/TrackDesign.h"
|
||||
#include "../ride/Vehicle.h"
|
||||
@@ -950,6 +951,8 @@ void viewport_paint(
|
||||
const rct_viewport* viewport, rct_drawpixelinfo* dpi, const ScreenRect& screenRect,
|
||||
std::vector<RecordedPaintSession>* recorded_sessions)
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
const uint32_t viewFlags = viewport->flags;
|
||||
uint32_t width = screenRect.GetWidth();
|
||||
uint32_t height = screenRect.GetHeight();
|
||||
|
||||
@@ -315,6 +315,8 @@
|
||||
<ClInclude Include="platform\Crash.h" />
|
||||
<ClInclude Include="platform\platform.h" />
|
||||
<ClInclude Include="platform\Platform2.h" />
|
||||
<ClInclude Include="profiling\Profiling.h" />
|
||||
<ClInclude Include="profiling\ProfilingMacros.hpp" />
|
||||
<ClInclude Include="rct12\EntryList.h" />
|
||||
<ClInclude Include="rct12\Limits.h" />
|
||||
<ClInclude Include="rct12\RCT12.h" />
|
||||
@@ -449,6 +451,7 @@
|
||||
<ClInclude Include="scripting\bindings\entity\ScPeep.hpp" />
|
||||
<ClInclude Include="scripting\bindings\entity\ScStaff.hpp" />
|
||||
<ClInclude Include="scripting\bindings\entity\ScVehicle.hpp" />
|
||||
<ClInclude Include="scripting\bindings\game\ScProfiler.hpp" />
|
||||
<ClInclude Include="scripting\bindings\network\ScPlayer.hpp" />
|
||||
<ClInclude Include="scripting\bindings\network\ScPlayerGroup.hpp" />
|
||||
<ClInclude Include="scripting\bindings\ride\ScRideStation.hpp" />
|
||||
@@ -791,6 +794,7 @@
|
||||
<ClCompile Include="platform\Posix.cpp" />
|
||||
<ClCompile Include="platform\Shared.cpp" />
|
||||
<ClCompile Include="platform\Windows.cpp" />
|
||||
<ClCompile Include="profiling\Profiling.cpp" />
|
||||
<ClCompile Include="rct12\RCT12.cpp" />
|
||||
<ClCompile Include="rct12\SawyerChunk.cpp" />
|
||||
<ClCompile Include="rct12\SawyerChunkReader.cpp" />
|
||||
@@ -947,4 +951,4 @@
|
||||
<ClCompile Include="world\Wall.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
</Project>
|
||||
</Project>
|
||||
@@ -8,6 +8,7 @@
|
||||
*****************************************************************************/
|
||||
|
||||
#include "../Game.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../util/Math.hpp"
|
||||
#include "Date.h"
|
||||
#include "StringIds.h"
|
||||
@@ -76,6 +77,8 @@ void date_set(int32_t year, int32_t month, int32_t day)
|
||||
|
||||
void date_update()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
int32_t monthTicks = gDateMonthTicks + 4;
|
||||
if (monthTicks >= TICKS_PER_MONTH)
|
||||
{
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "../localisation/Formatter.h"
|
||||
#include "../localisation/Localisation.h"
|
||||
#include "../management/Research.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../ride/Ride.h"
|
||||
#include "../ride/Vehicle.h"
|
||||
#include "../util/Util.h"
|
||||
@@ -153,6 +154,8 @@ bool News::ItemQueues::CurrentShouldBeArchived() const
|
||||
*/
|
||||
void News::UpdateCurrentItem()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
// Check if there is a current news item
|
||||
if (gNewsItems.IsEmpty())
|
||||
return;
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "../localisation/StringIds.h"
|
||||
#include "../object/ObjectList.h"
|
||||
#include "../object/RideObject.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../ride/Ride.h"
|
||||
#include "../ride/RideData.h"
|
||||
#include "../ride/RideEntry.h"
|
||||
@@ -312,6 +313,8 @@ void research_finish_item(ResearchItem* researchItem)
|
||||
*/
|
||||
void research_update()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
int32_t editorScreenFlags, researchLevel, currentResearchProgress;
|
||||
|
||||
editorScreenFlags = SCREEN_FLAGS_SCENARIO_EDITOR | SCREEN_FLAGS_TRACK_DESIGNER | SCREEN_FLAGS_TRACK_MANAGER;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "../localisation/Localisation.h"
|
||||
#include "../localisation/LocalisationService.h"
|
||||
#include "../paint/Painter.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../util/Math.hpp"
|
||||
#include "Paint.Entity.h"
|
||||
#include "tile_element/Paint.TileElement.h"
|
||||
@@ -466,6 +467,7 @@ template<int TRotation> static void PaintSessionArrange(PaintSessionCore& sessio
|
||||
*/
|
||||
void PaintSessionArrange(PaintSessionCore& session)
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
switch (session.CurrentRotation)
|
||||
{
|
||||
case 0:
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2022 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#include "Profiling.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <stack>
|
||||
|
||||
namespace OpenRCT2::Profiling
|
||||
{
|
||||
inline static bool _enabled = false;
|
||||
|
||||
void Enable()
|
||||
{
|
||||
_enabled = true;
|
||||
}
|
||||
|
||||
void Disable()
|
||||
{
|
||||
_enabled = false;
|
||||
}
|
||||
|
||||
bool IsEnabled()
|
||||
{
|
||||
return _enabled;
|
||||
}
|
||||
|
||||
namespace Detail
|
||||
{
|
||||
using Clock = std::chrono::high_resolution_clock;
|
||||
using Tp = Clock::time_point;
|
||||
|
||||
struct FunctionEntry
|
||||
{
|
||||
FunctionInternal* Parent;
|
||||
FunctionInternal* Func;
|
||||
Tp EntryTime;
|
||||
|
||||
FunctionEntry(FunctionInternal* parent, FunctionInternal* func, const Tp& entryTime)
|
||||
: Parent(parent)
|
||||
, Func(func)
|
||||
, EntryTime(entryTime)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
static thread_local std::stack<FunctionEntry> _callStack;
|
||||
|
||||
void FunctionEnter(Function& func)
|
||||
{
|
||||
const auto entryTime = Clock::now();
|
||||
|
||||
auto& funcInternal = static_cast<FunctionInternal&>(func);
|
||||
funcInternal.CallCount++;
|
||||
|
||||
FunctionInternal* parent = nullptr;
|
||||
|
||||
if (!_callStack.empty())
|
||||
parent = _callStack.top().Func;
|
||||
|
||||
_callStack.emplace(parent, &funcInternal, entryTime);
|
||||
}
|
||||
|
||||
void FunctionExit(Function& func)
|
||||
{
|
||||
const auto exitTime = Clock::now();
|
||||
|
||||
assert(!_callStack.empty());
|
||||
|
||||
auto& stackEntry = _callStack.top();
|
||||
|
||||
const auto deltaTime = exitTime - stackEntry.EntryTime;
|
||||
|
||||
// Elapsed microseconds.
|
||||
const auto elapsedTimeUs = std::chrono::duration_cast<std::chrono::nanoseconds>(deltaTime).count() / 1000.0;
|
||||
|
||||
auto* funcData = stackEntry.Func;
|
||||
|
||||
// We don't need a lock for this, we only have a fixed window.
|
||||
const auto sampleEntryIdx = funcData->SampleIterator++ % funcData->Samples.size();
|
||||
funcData->Samples[sampleEntryIdx] = elapsedTimeUs;
|
||||
|
||||
if (stackEntry.Parent)
|
||||
{
|
||||
std::scoped_lock lock(stackEntry.Parent->Mutex);
|
||||
stackEntry.Parent->Children.insert(funcData);
|
||||
}
|
||||
|
||||
// This requires locking.
|
||||
{
|
||||
std::scoped_lock lock(funcData->Mutex);
|
||||
|
||||
if (stackEntry.Parent)
|
||||
funcData->Parents.insert(stackEntry.Parent);
|
||||
|
||||
if (funcData->MinTimeUs == 0.0)
|
||||
funcData->MinTimeUs = elapsedTimeUs;
|
||||
else
|
||||
funcData->MinTimeUs = std::min(elapsedTimeUs, funcData->MinTimeUs);
|
||||
|
||||
funcData->MaxTimeUs = std::max(elapsedTimeUs, funcData->MaxTimeUs);
|
||||
funcData->TotalTimeUs += elapsedTimeUs;
|
||||
}
|
||||
|
||||
_callStack.pop();
|
||||
}
|
||||
|
||||
std::vector<Function*>& GetRegistry()
|
||||
{
|
||||
static std::vector<Function*> Registry;
|
||||
return Registry;
|
||||
}
|
||||
|
||||
} // namespace Detail
|
||||
|
||||
const std::vector<Function*>& GetData()
|
||||
{
|
||||
return Detail::GetRegistry();
|
||||
}
|
||||
|
||||
void ResetData()
|
||||
{
|
||||
for (auto* func : Detail::GetRegistry())
|
||||
{
|
||||
auto* funcInternal = static_cast<Detail::FunctionInternal*>(func);
|
||||
|
||||
std::scoped_lock lock(funcInternal->Mutex);
|
||||
funcInternal->CallCount = 0;
|
||||
funcInternal->MinTimeUs = 0.0;
|
||||
funcInternal->MaxTimeUs = 0.0;
|
||||
funcInternal->SampleIterator = 0;
|
||||
funcInternal->Children.clear();
|
||||
funcInternal->Parents.clear();
|
||||
}
|
||||
}
|
||||
|
||||
bool ExportCSV(const std::string& filePath)
|
||||
{
|
||||
std::ofstream out(filePath);
|
||||
if (!out.is_open())
|
||||
return false;
|
||||
|
||||
out << "function_name;calls;min_microseconds;max_microseconds;average_microseconds\n";
|
||||
out << std::setprecision(12);
|
||||
|
||||
const auto& data = GetData();
|
||||
for (auto* func : data)
|
||||
{
|
||||
out << "\"" << func->GetName() << "\""
|
||||
<< ";";
|
||||
out << func->GetCallCount() << ";";
|
||||
out << func->GetMinTime() << ";";
|
||||
out << func->GetMaxTime() << ";";
|
||||
|
||||
double avg = 0.0;
|
||||
if (func->GetCallCount() > 0)
|
||||
avg = func->GetTotalTime() / func->GetCallCount();
|
||||
|
||||
out << avg << "\n";
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace OpenRCT2::Profiling
|
||||
@@ -0,0 +1,193 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2022 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
#pragma once
|
||||
|
||||
#include "ProfilingMacros.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace OpenRCT2::Profiling
|
||||
{
|
||||
void Enable();
|
||||
void Disable();
|
||||
bool IsEnabled();
|
||||
|
||||
struct Function
|
||||
{
|
||||
virtual ~Function() = default;
|
||||
|
||||
// This returns the full function prototype not just the name.
|
||||
virtual const char* GetName() const noexcept = 0;
|
||||
|
||||
// Total calls made to the function.
|
||||
virtual uint64_t GetCallCount() const noexcept = 0;
|
||||
|
||||
// Returns a small window of the most recent times.
|
||||
virtual std::vector<double> GetTimeSamples() const = 0;
|
||||
|
||||
// Returns all time accumulated in microseconds.
|
||||
virtual double GetTotalTime() const = 0;
|
||||
|
||||
// Returns the min. time in microseconds.
|
||||
virtual double GetMinTime() const = 0;
|
||||
|
||||
// Returns the max. time in microseconds.
|
||||
virtual double GetMaxTime() const = 0;
|
||||
|
||||
// Returns a list of all functions that called this function.
|
||||
virtual std::vector<Function*> GetParents() const = 0;
|
||||
|
||||
// Returns a list of function this function is calling.
|
||||
virtual std::vector<Function*> GetChildren() const = 0;
|
||||
};
|
||||
|
||||
namespace Detail
|
||||
{
|
||||
static constexpr auto MaxSamplesSize = 1024;
|
||||
static constexpr auto MaxNameSize = 250;
|
||||
|
||||
std::vector<Function*>& GetRegistry();
|
||||
|
||||
struct FunctionInternal : Function
|
||||
{
|
||||
FunctionInternal()
|
||||
{
|
||||
GetRegistry().push_back(this);
|
||||
}
|
||||
|
||||
virtual ~FunctionInternal() = default;
|
||||
|
||||
// Functions marked MT require locking, this is per function basis.
|
||||
mutable std::mutex Mutex;
|
||||
|
||||
std::array<char, MaxNameSize> Name{};
|
||||
|
||||
// Call count of function.
|
||||
std::atomic<uint64_t> CallCount{};
|
||||
|
||||
// Function times in microseconds.
|
||||
std::array<double, MaxSamplesSize> Samples{};
|
||||
|
||||
// Used internally to write into Samples without a lock.
|
||||
std::atomic<size_t> SampleIterator{};
|
||||
|
||||
double MinTimeUs{};
|
||||
|
||||
double MaxTimeUs{};
|
||||
|
||||
double TotalTimeUs{};
|
||||
|
||||
// Functions that called us.
|
||||
std::unordered_set<Function*> Parents;
|
||||
|
||||
// Functions that this function called.
|
||||
std::unordered_set<Function*> Children;
|
||||
|
||||
uint64_t GetCallCount() const noexcept override
|
||||
{
|
||||
return CallCount.load();
|
||||
}
|
||||
|
||||
std::vector<double> GetTimeSamples() const override
|
||||
{
|
||||
const auto numSamples = std::min(SampleIterator.load(), Samples.size());
|
||||
return { Samples.begin(), Samples.begin() + numSamples };
|
||||
}
|
||||
|
||||
std::vector<Function*> GetParents() const override
|
||||
{
|
||||
std::scoped_lock lock(Mutex);
|
||||
return { Parents.begin(), Parents.end() };
|
||||
}
|
||||
|
||||
std::vector<Function*> GetChildren() const override
|
||||
{
|
||||
std::scoped_lock lock(Mutex);
|
||||
return { Children.begin(), Children.end() };
|
||||
}
|
||||
|
||||
double GetTotalTime() const override
|
||||
{
|
||||
std::scoped_lock lock(Mutex);
|
||||
return TotalTimeUs;
|
||||
}
|
||||
|
||||
double GetMinTime() const override
|
||||
{
|
||||
std::scoped_lock lock(Mutex);
|
||||
return MinTimeUs;
|
||||
}
|
||||
|
||||
double GetMaxTime() const override
|
||||
{
|
||||
std::scoped_lock lock(Mutex);
|
||||
return MaxTimeUs;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename TName> struct FunctionWrapper : FunctionInternal
|
||||
{
|
||||
const char* GetName() const noexcept override
|
||||
{
|
||||
return TName::Str();
|
||||
}
|
||||
};
|
||||
|
||||
// Wrapper to avoid static initialization inside the function.
|
||||
// This avoids the compiler generating thread-safe initialization
|
||||
// by making a unique type per function which hosts a global using
|
||||
// the inline keyword for the variable (C++17).
|
||||
template<typename TName> struct Storage
|
||||
{
|
||||
static inline FunctionWrapper<TName> Data;
|
||||
};
|
||||
|
||||
void FunctionEnter(Function& func);
|
||||
void FunctionExit(Function& func);
|
||||
|
||||
} // namespace Detail
|
||||
|
||||
template<typename T> class ScopedProfiling
|
||||
{
|
||||
bool _enabled;
|
||||
T& _func;
|
||||
|
||||
public:
|
||||
ScopedProfiling(T& func)
|
||||
: _enabled{ IsEnabled() }
|
||||
, _func(func)
|
||||
{
|
||||
if (_enabled)
|
||||
{
|
||||
Detail::FunctionEnter(_func);
|
||||
}
|
||||
}
|
||||
~ScopedProfiling()
|
||||
{
|
||||
if (!_enabled)
|
||||
return;
|
||||
Detail::FunctionExit(_func);
|
||||
}
|
||||
};
|
||||
|
||||
// Clears all the current data of each function.
|
||||
void ResetData();
|
||||
|
||||
// Returns all functions.
|
||||
const std::vector<Function*>& GetData();
|
||||
|
||||
bool ExportCSV(const std::string& filePath);
|
||||
|
||||
} // namespace OpenRCT2::Profiling
|
||||
@@ -0,0 +1,42 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2022 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
#pragma once
|
||||
|
||||
namespace OpenRCT2::Profiling
|
||||
{
|
||||
#if defined(__clang__) || defined(__GNUC__)
|
||||
# define PROFILING_FUNC_NAME __PRETTY_FUNCTION__
|
||||
#elif defined(_MSC_VER)
|
||||
# define PROFILING_FUNC_NAME __FUNCSIG__
|
||||
#else
|
||||
# error "Unsupported compiler"
|
||||
#endif
|
||||
|
||||
#define PROFILED_FUNCTION_NAME(func) \
|
||||
static constexpr auto _profiling_func_name = func; \
|
||||
struct Profiler_FunctionLiteral \
|
||||
{ \
|
||||
static constexpr const char* Str() \
|
||||
{ \
|
||||
return _profiling_func_name; \
|
||||
} \
|
||||
};
|
||||
|
||||
#if defined(__clang_major__) && __clang_major__ <= 5
|
||||
// Clang 5 crashes using the profiler, we need to disable it.
|
||||
# define PROFILED_FUNCTION()
|
||||
#else
|
||||
|
||||
# define PROFILED_FUNCTION() \
|
||||
PROFILED_FUNCTION_NAME(PROFILING_FUNC_NAME) \
|
||||
static auto& _profiling_func = ::OpenRCT2::Profiling::Detail::Storage<Profiler_FunctionLiteral>::Data; \
|
||||
::OpenRCT2::Profiling::ScopedProfiling<decltype(_profiling_func)> _profiling_scope(_profiling_func);
|
||||
#endif
|
||||
|
||||
} // namespace OpenRCT2::Profiling
|
||||
@@ -42,6 +42,7 @@
|
||||
#include "../object/RideObject.h"
|
||||
#include "../object/StationObject.h"
|
||||
#include "../paint/VirtualFloor.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../rct1/RCT1.h"
|
||||
#include "../scenario/Scenario.h"
|
||||
#include "../ui/UiContext.h"
|
||||
@@ -923,6 +924,8 @@ void reset_all_ride_build_dates()
|
||||
*/
|
||||
void Ride::UpdateAll()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
// Remove all rides if scenario editor
|
||||
if (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR)
|
||||
{
|
||||
@@ -1978,6 +1981,8 @@ static void ride_measurement_update(Ride& ride, RideMeasurement& measurement)
|
||||
*/
|
||||
void ride_measurements_update()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR)
|
||||
return;
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "../OpenRCT2.h"
|
||||
#include "../interface/Window.h"
|
||||
#include "../localisation/Date.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../scripting/ScriptEngine.h"
|
||||
#include "../world/Footpath.h"
|
||||
#include "../world/Map.h"
|
||||
@@ -118,6 +119,8 @@ void ride_ratings_update_ride(const Ride& ride)
|
||||
*/
|
||||
void ride_ratings_update_all()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR)
|
||||
return;
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "../localisation/Localisation.h"
|
||||
#include "../management/NewsItem.h"
|
||||
#include "../platform/platform.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../rct12/RCT12.h"
|
||||
#include "../scenario/Scenario.h"
|
||||
#include "../scripting/HookEngine.h"
|
||||
@@ -1139,6 +1140,8 @@ static void UpdateSound(
|
||||
*/
|
||||
void vehicle_sounds_update()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (!OpenRCT2::Audio::IsAvailable())
|
||||
return;
|
||||
|
||||
@@ -1224,6 +1227,8 @@ void vehicle_sounds_update()
|
||||
*/
|
||||
void vehicle_update_all()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR)
|
||||
return;
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include "../object/ObjectList.h"
|
||||
#include "../object/ObjectManager.h"
|
||||
#include "../platform/platform.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../rct1/RCT1.h"
|
||||
#include "../rct12/RCT12.h"
|
||||
#include "../ride/Ride.h"
|
||||
@@ -399,6 +400,8 @@ static void scenario_update_daynight_cycle()
|
||||
*/
|
||||
void scenario_update()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (gScreenFlags == SCREEN_FLAGS_PLAYING)
|
||||
{
|
||||
if (date_is_day_start(gDateMonthTicks))
|
||||
|
||||
@@ -137,6 +137,13 @@ namespace OpenRCT2::Scripting
|
||||
duk_put_prop_string(_ctx, _idx, name);
|
||||
}
|
||||
|
||||
void Set(const char* name, double value)
|
||||
{
|
||||
EnsureObjectPushed();
|
||||
duk_push_number(_ctx, value);
|
||||
duk_put_prop_string(_ctx, _idx, name);
|
||||
}
|
||||
|
||||
void Set(const char* name, std::string_view value)
|
||||
{
|
||||
EnsureObjectPushed();
|
||||
|
||||
@@ -41,7 +41,7 @@ void Plugin::Load()
|
||||
LoadCodeFromFile();
|
||||
}
|
||||
|
||||
std::string projectedVariables = "console,context,date,map,network,park";
|
||||
std::string projectedVariables = "console,context,date,map,network,park,profiler";
|
||||
if (!gOpenRCT2Headless)
|
||||
{
|
||||
projectedVariables += ",ui";
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
# include "bindings/game/ScConsole.hpp"
|
||||
# include "bindings/game/ScContext.hpp"
|
||||
# include "bindings/game/ScDisposable.hpp"
|
||||
# include "bindings/game/ScProfiler.hpp"
|
||||
# include "bindings/network/ScNetwork.hpp"
|
||||
# include "bindings/network/ScPlayer.hpp"
|
||||
# include "bindings/network/ScPlayerGroup.hpp"
|
||||
@@ -402,6 +403,7 @@ void ScriptEngine::Initialise()
|
||||
ScParkMessage::Register(ctx);
|
||||
ScPlayer::Register(ctx);
|
||||
ScPlayerGroup::Register(ctx);
|
||||
ScProfiler::Register(ctx);
|
||||
ScRide::Register(ctx);
|
||||
ScRideStation::Register(ctx);
|
||||
ScRideObject::Register(ctx);
|
||||
@@ -429,6 +431,7 @@ void ScriptEngine::Initialise()
|
||||
dukglue_register_global(ctx, std::make_shared<ScMap>(ctx), "map");
|
||||
dukglue_register_global(ctx, std::make_shared<ScNetwork>(ctx), "network");
|
||||
dukglue_register_global(ctx, std::make_shared<ScPark>(), "park");
|
||||
dukglue_register_global(ctx, std::make_shared<ScProfiler>(ctx), "profiler");
|
||||
dukglue_register_global(ctx, std::make_shared<ScScenario>(), "scenario");
|
||||
|
||||
_initialised = true;
|
||||
@@ -648,6 +651,8 @@ void ScriptEngine::StopPlugins()
|
||||
|
||||
void ScriptEngine::Tick()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (!_initialised)
|
||||
{
|
||||
Initialise();
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2022 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef ENABLE_SCRIPTING
|
||||
|
||||
# include "../../../profiling/Profiling.h"
|
||||
# include "../../Duktape.hpp"
|
||||
|
||||
namespace OpenRCT2::Scripting
|
||||
{
|
||||
class ScProfiler
|
||||
{
|
||||
private:
|
||||
duk_context* _ctx{};
|
||||
|
||||
public:
|
||||
ScProfiler(duk_context* ctx)
|
||||
: _ctx(ctx)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
DukValue getData()
|
||||
{
|
||||
const auto& data = OpenRCT2::Profiling::GetData();
|
||||
duk_push_array(_ctx);
|
||||
duk_uarridx_t index = 0;
|
||||
for (const auto& f : data)
|
||||
{
|
||||
DukObject obj(_ctx);
|
||||
obj.Set("name", f->GetName());
|
||||
obj.Set("callCount", f->GetCallCount());
|
||||
obj.Set("minTime", f->GetMinTime());
|
||||
obj.Set("maxTime", f->GetMaxTime());
|
||||
obj.Set("totalTime", f->GetTotalTime());
|
||||
obj.Set("parents", GetFunctionIndexArray(data, f->GetParents()));
|
||||
obj.Set("children", GetFunctionIndexArray(data, f->GetChildren()));
|
||||
obj.Take().push();
|
||||
duk_put_prop_index(_ctx, /* duk stack index */ -2, index);
|
||||
index++;
|
||||
}
|
||||
return DukValue::take_from_stack(_ctx);
|
||||
}
|
||||
|
||||
DukValue GetFunctionIndexArray(
|
||||
const std::vector<OpenRCT2::Profiling::Function*>& all, const std::vector<OpenRCT2::Profiling::Function*>& items)
|
||||
{
|
||||
duk_push_array(_ctx);
|
||||
duk_uarridx_t index = 0;
|
||||
for (const auto& item : items)
|
||||
{
|
||||
auto it = std::find(all.begin(), all.end(), item);
|
||||
if (it != all.end())
|
||||
{
|
||||
auto value = static_cast<duk_int_t>(std::distance(all.begin(), it));
|
||||
duk_push_int(_ctx, value);
|
||||
duk_put_prop_index(_ctx, /* duk stack index */ -2, index);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return DukValue::take_from_stack(_ctx);
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
OpenRCT2::Profiling::Enable();
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
OpenRCT2::Profiling::Disable();
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
OpenRCT2::Profiling::ResetData();
|
||||
}
|
||||
|
||||
public:
|
||||
static void Register(duk_context* ctx)
|
||||
{
|
||||
dukglue_register_method(ctx, &ScProfiler::getData, "getData");
|
||||
dukglue_register_method(ctx, &ScProfiler::start, "start");
|
||||
dukglue_register_method(ctx, &ScProfiler::stop, "stop");
|
||||
dukglue_register_method(ctx, &ScProfiler::reset, "reset");
|
||||
}
|
||||
};
|
||||
} // namespace OpenRCT2::Scripting
|
||||
|
||||
#endif
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "../drawing/Drawing.h"
|
||||
#include "../interface/Window.h"
|
||||
#include "../localisation/Date.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../scenario/Scenario.h"
|
||||
#include "../sprites.h"
|
||||
#include "../util/Util.h"
|
||||
@@ -113,6 +114,8 @@ void climate_reset(ClimateType climate)
|
||||
*/
|
||||
void climate_update()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
// Only do climate logic if playing (not in scenario editor or title screen)
|
||||
if (gScreenFlags & (~SCREEN_FLAGS_PLAYING))
|
||||
return;
|
||||
@@ -208,6 +211,8 @@ void climate_force_weather(WeatherType weather)
|
||||
|
||||
void climate_update_sound()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (!OpenRCT2::Audio::IsAvailable())
|
||||
return;
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "../network/network.h"
|
||||
#include "../object/ObjectManager.h"
|
||||
#include "../object/TerrainSurfaceObject.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../ride/RideConstruction.h"
|
||||
#include "../ride/RideData.h"
|
||||
#include "../ride/Track.h"
|
||||
@@ -727,6 +728,8 @@ bool map_coord_is_connected(const TileCoordsXYZ& loc, uint8_t faceDirection)
|
||||
*/
|
||||
void map_update_path_wide_flags()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (gScreenFlags & (SCREEN_FLAGS_TRACK_DESIGNER | SCREEN_FLAGS_TRACK_MANAGER))
|
||||
{
|
||||
return;
|
||||
@@ -1308,6 +1311,8 @@ TileElement* tile_element_insert(const CoordsXYZ& loc, int32_t occupiedQuadrants
|
||||
*/
|
||||
void map_update_tiles()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
int32_t ignoreScreenFlags = SCREEN_FLAGS_SCENARIO_EDITOR | SCREEN_FLAGS_TRACK_DESIGNER | SCREEN_FLAGS_TRACK_MANAGER;
|
||||
if (gScreenFlags & ignoreScreenFlags)
|
||||
return;
|
||||
@@ -1349,6 +1354,8 @@ void map_update_tiles()
|
||||
|
||||
void map_remove_provisional_elements()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (gProvisionalFootpath.Flags & PROVISIONAL_PATH_FLAG_1)
|
||||
{
|
||||
footpath_provisional_remove();
|
||||
@@ -1370,6 +1377,8 @@ void map_remove_provisional_elements()
|
||||
|
||||
void map_restore_provisional_elements()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
if (gProvisionalFootpath.Flags & PROVISIONAL_PATH_FLAG_1)
|
||||
{
|
||||
gProvisionalFootpath.Flags &= ~PROVISIONAL_PATH_FLAG_1;
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "../entity/Peep.h"
|
||||
#include "../interface/Viewport.h"
|
||||
#include "../object/StationObject.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../ride/Ride.h"
|
||||
#include "../ride/RideData.h"
|
||||
#include "../ride/Track.h"
|
||||
@@ -69,6 +70,8 @@ void map_animation_create(int32_t type, const CoordsXYZ& loc)
|
||||
*/
|
||||
void map_animation_invalidate_all()
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
auto it = _mapAnimations.begin();
|
||||
while (it != _mapAnimations.end())
|
||||
{
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "../management/Marketing.h"
|
||||
#include "../management/Research.h"
|
||||
#include "../network/network.h"
|
||||
#include "../profiling/Profiling.h"
|
||||
#include "../ride/Ride.h"
|
||||
#include "../ride/RideData.h"
|
||||
#include "../ride/ShopItem.h"
|
||||
@@ -307,6 +308,8 @@ void Park::Initialise()
|
||||
|
||||
void Park::Update(const Date& date)
|
||||
{
|
||||
PROFILED_FUNCTION();
|
||||
|
||||
// Every ~13 seconds
|
||||
if (gCurrentTicks % 512 == 0)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user