21 std::vector<NewGRFProfiler> _newgrf_profilers;
22 Date _newgrf_profile_end_date;
47 using namespace std::chrono;
50 this->
cur_call.
time = (uint32)time_point_cast<microseconds>(high_resolution_clock::now()).time_since_epoch().count();
62 using namespace std::chrono;
63 this->
cur_call.
time = (uint32)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 NewGRFProfiler::Finish()
95 if (!this->
active)
return 0;
97 if (this->
calls.empty()) {
108 uint32 total_microseconds = 0;
110 fputs(
"Tick,Sprite,Feature,Item,CallbackID,Microseconds,Depth,Result\n", f);
112 fprintf(f,
"%u,%u,0x%X,%d,0x%X,%u,%u,%u\n", c.tick, c.root_sprite, c.feat, c.item, (uint)c.cb, c.time, c.subs, c.result);
113 total_microseconds += c.time;
118 return total_microseconds;
121 void NewGRFProfiler::Abort()
133 time_t write_time = time(
nullptr);
135 char timestamp[16] = {};
136 strftime(timestamp,
lengthof(timestamp),
"%Y%m%d-%H%M", localtime(&write_time));
138 char filepath[MAX_PATH] = {};
141 return std::string(filepath);
144 uint32 NewGRFProfiler::FinishAll()
147 uint32 total_microseconds = 0;
150 total_microseconds += pr.Finish();
155 if (total_microseconds > 0 && max_ticks > 0) {
156 IConsolePrintF(
CC_DEBUG,
"Total NewGRF callback processing: %u microseconds over %d ticks", total_microseconds, max_ticks);
159 _newgrf_profile_end_date =
MAX_DAY;
161 return total_microseconds;
Profiling of NewGRF action 2 handling.
Functions related to dates.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Interface for SpriteGroup-s to access the gamestate.
~NewGRFProfiler()
Complete profiling session and write data to file.
Functions for Standard In/Out file operations.
#define lastof(x)
Get the last element of an fixed size array.
Measurement of a single sprite group resolution.
Call cur_call
Data for current call in progress.
std::vector< Call > calls
All calls collected so far.
static T max(const T a, const T b)
Returns the maximum of two values.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
void EndResolve(const SpriteGroup *result)
Capture the completion of a sprite group resolution.
Functions related to low-level strings.
uint32 item
Local ID of item being resolved for.
GrfSpecFeature feat
GRF feature being resolved for.
void RecursiveResolve()
Capture a recursive sprite group resolution.
uint32 subs
Sub-calls to other sprite groups.
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
void CDECL IConsolePrintF(TextColour colour_code, const char *format,...)
Handle the printing of text entered into the console or redirected there by any other means...
A path without any base directory.
static const TextColour CC_DEBUG
Colour for debug output.
Auto-close a file upon scope exit.
Console functions used outside of the console code.
uint32 result
Result of callback.
void BeginResolve(const ResolverObject &resolver)
Capture the start of a sprite group resolution.
#define lengthof(x)
Return the length of an fixed size array.
Callback profiler for NewGRF development.
Functions to cache sprites in memory.
const GRFFile * grffile
Which GRF is being profiled.
uint16 _tick_counter
Ever incrementing (and sometimes wrapping) tick counter for setting off various events.
uint16 start_tick
Tick number this profiler was started on.
virtual GrfSpecFeature GetFeature() const
Get the feature number being resolved for.
std::string GetOutputFilename() const
Get name of the file that will be written.
CallbackID callback
Callback being resolved.
NewGRFProfiler(const GRFFile *grffile)
Create profiler object and begin profiling session.
uint32 time
Time taken for resolution (microseconds)
bool active
Is this profiler collecting data.
#define MAX_DAY
The number of days till the last day.
uint32 GetSpriteLocalID(SpriteID sprite)
Get the GRF-local sprite id of a given sprite.
int32 Date
The type to store our dates in.
virtual uint32 GetDebugID() const
Get an identifier for the item being resolved.
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
CallbackID cb
Callback ID.
uint32 root_sprite
Pseudo-sprite index in GRF file.
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Dynamic data of a loaded NewGRF.