15 #include "3rdparty/fmt/chrono.h"
22 std::vector<NewGRFProfiler> _newgrf_profilers;
47 using namespace std::chrono;
50 this->
cur_call.
time = (uint32_t)time_point_cast<microseconds>(high_resolution_clock::now()).time_since_epoch().count();
62 using namespace std::chrono;
63 this->
cur_call.
time = (uint32_t)time_point_cast<microseconds>(high_resolution_clock::now()).time_since_epoch().count() - this->
cur_call.
time;
65 if (result ==
nullptr) {
67 }
else if (result->type == SGT_CALLBACK) {
69 }
else if (result->type == SGT_RESULT) {
86 void NewGRFProfiler::Start()
93 uint32_t NewGRFProfiler::Finish()
95 if (!this->
active)
return 0;
97 if (this->
calls.empty()) {
107 uint32_t total_microseconds = 0;
111 if (!f.has_value()) {
114 fmt::print(*f,
"Tick,Sprite,Feature,Item,CallbackID,Microseconds,Depth,Result\n");
115 for (
const Call &c : this->
calls) {
116 fmt::print(*f,
"{},{},{:#X},{},{:#X},{},{},{}\n", c.tick, c.root_sprite, c.feat, c.item, (uint)c.cb, c.time, c.subs, c.result);
117 total_microseconds += c.time;
122 return total_microseconds;
125 void NewGRFProfiler::Abort()
140 uint32_t NewGRFProfiler::FinishAll()
144 uint64_t max_ticks = 0;
145 uint32_t total_microseconds = 0;
148 total_microseconds += pr.Finish();
153 if (total_microseconds > 0 && max_ticks > 0) {
154 IConsolePrint(
CC_DEBUG,
"Total NewGRF callback processing: {} microseconds over {} ticks.", total_microseconds, max_ticks);
157 return total_microseconds;
165 NewGRFProfiler::FinishAll();
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
A timeout timer will fire once after the interval.
static TickCounter counter
Monotonic counter, in ticks, since start of game.
void IConsolePrint(TextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
Console functions used outside of the console code.
static const TextColour CC_DEBUG
Colour for debug output.
static const TextColour CC_ERROR
Colour for error lines.
std::optional< FileHandle > FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Functions for Standard In/Out file operations.
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
@ NO_DIRECTORY
A path without any base directory.
static TimeoutTimer< TimerGameTick > _profiling_finish_timeout({ TimerGameTick::Priority::NONE, 0 }, []() { NewGRFProfiler::FinishAll();})
Check whether profiling is active and should be finished.
Profiling of NewGRF action 2 handling.
uint32_t GetSpriteLocalID(SpriteID sprite)
Get the GRF-local sprite id of a given sprite.
Functions to cache sprites in memory.
Functions related to low-level strings.
Dynamic data of a loaded NewGRF.
GrfSpecFeature feat
GRF feature being resolved for.
uint32_t time
Time taken for resolution (microseconds)
uint32_t subs
Sub-calls to other sprite groups.
uint32_t root_sprite
Pseudo-sprite index in GRF file.
CallbackID cb
Callback ID.
uint32_t item
Local ID of item being resolved for.
uint32_t result
Result of callback.
Callback profiler for NewGRF development.
void EndResolve(const SpriteGroup *result)
Capture the completion of a sprite group resolution.
void RecursiveResolve()
Capture a recursive sprite group resolution.
static void StartTimer(uint64_t ticks)
Start the timeout timer that will finish all profiling sessions.
~NewGRFProfiler()
Complete profiling session and write data to file.
Call cur_call
Data for current call in progress.
bool active
Is this profiler collecting data.
const GRFFile * grffile
Which GRF is being profiled.
uint64_t start_tick
Tick number this profiler was started on.
static void AbortTimer()
Abort the timeout timer, so the timer callback is never called.
void BeginResolve(const ResolverObject &resolver)
Capture the start of a sprite group resolution.
NewGRFProfiler(const GRFFile *grffile)
Create profiler object and begin profiling session.
std::vector< Call > calls
All calls collected so far.
std::string GetOutputFilename() const
Get name of the file that will be written.
Interface for SpriteGroup-s to access the gamestate.
virtual uint32_t GetDebugID() const
Get an identifier for the item being resolved.
CallbackID callback
Callback being resolved.
virtual GrfSpecFeature GetFeature() const
Get the feature number being resolved for.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Definition of Interval and OneShot timers.
Definition of the tick-based game-timer.