From aacb70d371825414e776c083f5f485eeffc85859 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 18 Jan 2021 00:58:43 +0000 Subject: [PATCH] Debug: Extend random debug logging to also include state checksum updates --- src/aircraft_cmd.cpp | 1 + src/core/checksum_func.hpp | 21 +++++++++++++++++++++ src/debug.cpp | 4 +++- src/debug.h | 1 + src/disaster_vehicle.cpp | 1 + src/effectvehicle.cpp | 1 + src/openttd.cpp | 1 + src/roadveh_cmd.cpp | 3 +++ src/ship_cmd.cpp | 2 ++ src/train_cmd.cpp | 4 +++- 10 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 408acf1694..6fa0791102 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -2163,6 +2163,7 @@ static bool AircraftEventHandler(Aircraft *v, int loop) bool Aircraft::Tick() { + DEBUG_UPDATESTATECHECKSUM("Aircraft::Tick: v: %u, x: %d, y: %d", this->index, this->x_pos, this->y_pos); UpdateStateChecksum((((uint64) this->x_pos) << 32) | this->y_pos); if (!this->IsNormalAircraft()) return true; diff --git a/src/core/checksum_func.hpp b/src/core/checksum_func.hpp index eb0a60c624..049460b260 100644 --- a/src/core/checksum_func.hpp +++ b/src/core/checksum_func.hpp @@ -12,6 +12,16 @@ #include "bitmath_func.hpp" +#ifdef RANDOM_DEBUG +#include "../network/network.h" +#include "../network/network_server.h" +#include "../network/network_internal.h" +#include "../company_func.h" +#include "../fileio_func.h" +#include "../date_func.h" +#include "../debug.h" +#endif /* RANDOM_DEBUG */ + struct SimpleChecksum64 { uint64 state = 0; @@ -28,4 +38,15 @@ inline void UpdateStateChecksum(uint64 input) _state_checksum.Update(input); } +#ifdef RANDOM_DEBUG +inline bool ShouldLogUpdateStateChecksum() +{ + return _networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE)); +} +# define DEBUG_UPDATESTATECHECKSUM(str, ...) if (ShouldLogUpdateStateChecksum()) DEBUG(statecsum, 0, "date{%08x; %02x; %02x}; %04x; %02x; " OTTD_PRINTFHEX64PAD "; %s:%d " str, \ + _date, _date_fract, _tick_skip_counter, _frame_counter, (byte)_current_company, _state_checksum.state, __FILE__, __LINE__, __VA_ARGS__); +#else +# define DEBUG_UPDATESTATECHECKSUM(str, ...) +#endif /* RANDOM_DEBUG */ + #endif /* CHECKSUM_FUNC_HPP */ diff --git a/src/debug.cpp b/src/debug.cpp index 271774a5e8..baa9f25c65 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -52,6 +52,7 @@ int _debug_linkgraph_level; int _debug_sound_level; #ifdef RANDOM_DEBUG int _debug_random_level; +int _debug_statecsum_level; #endif const char *_savegame_DBGL_data = nullptr; @@ -88,6 +89,7 @@ struct DebugLevel { DEBUG_LEVEL(sound), #ifdef RANDOM_DEBUG DEBUG_LEVEL(random), + DEBUG_LEVEL(statecsum), #endif }; #undef DEBUG_LEVEL @@ -140,7 +142,7 @@ static void debug_print(const char *dbg, const char *buf) fflush(f); } #ifdef RANDOM_DEBUG - } else if (strcmp(dbg, "random") == 0) { + } else if (strcmp(dbg, "random") == 0 || strcmp(dbg, "statecsum") == 0) { #if defined(UNIX) && defined(__GLIBC__) static bool have_inited = false; static FILE *f = nullptr; diff --git a/src/debug.h b/src/debug.h index 9a90e96762..d14fa7b06f 100644 --- a/src/debug.h +++ b/src/debug.h @@ -55,6 +55,7 @@ extern int _debug_linkgraph_level; extern int _debug_sound_level; #ifdef RANDOM_DEBUG extern int _debug_random_level; +extern int _debug_statecsum_level; #endif extern const char *_savegame_DBGL_data; diff --git a/src/disaster_vehicle.cpp b/src/disaster_vehicle.cpp index 7278a1e41f..13a9e40d16 100644 --- a/src/disaster_vehicle.cpp +++ b/src/disaster_vehicle.cpp @@ -716,6 +716,7 @@ static DisasterVehicleTickProc * const _disastervehicle_tick_procs[] = { bool DisasterVehicle::Tick() { + DEBUG_UPDATESTATECHECKSUM("DisasterVehicle::Tick: v: %u, x: %d, y: %d", this->index, this->x_pos, this->y_pos); UpdateStateChecksum((((uint64) this->x_pos) << 32) | this->y_pos); return _disastervehicle_tick_procs[this->subtype](this); } diff --git a/src/effectvehicle.cpp b/src/effectvehicle.cpp index 6e606db454..111aa27d59 100644 --- a/src/effectvehicle.cpp +++ b/src/effectvehicle.cpp @@ -672,6 +672,7 @@ EffectVehicle *CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, Eff bool EffectVehicle::Tick() { + DEBUG_UPDATESTATECHECKSUM("EffectVehicle::Tick: v: %u, x: %d, y: %d", this->index, this->x_pos, this->y_pos); UpdateStateChecksum((((uint64) this->x_pos) << 32) | this->y_pos); return _effect_tick_procs[this->subtype](this); } diff --git a/src/openttd.cpp b/src/openttd.cpp index b5b4742220..5b64b8b0f6 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1852,6 +1852,7 @@ void StateGameLoop() cur_company.Restore(); for (Company *c : Company::Iterate()) { + DEBUG_UPDATESTATECHECKSUM("Company: %u, Money: " OTTD_PRINTF64, c->index, (int64)c->money); UpdateStateChecksum(c->money); } } diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 41c0fbbb6b..6941258857 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -1259,6 +1259,7 @@ static Trackdir RoadFindPathToDest(RoadVehicle *v, TileIndex tile, DiagDirection default: NOT_REACHED(); } + DEBUG_UPDATESTATECHECKSUM("RoadFindPathToDest: v: %u, path_found: %d, best_track: %d", v->index, path_found, best_track); UpdateStateChecksum((((uint64) v->index) << 32) | (path_found << 16) | best_track); v->HandlePathfindingResult(path_found); @@ -2116,7 +2117,9 @@ Money RoadVehicle::GetRunningCost() const bool RoadVehicle::Tick() { + DEBUG_UPDATESTATECHECKSUM("RoadVehicle::Tick 1: v: %u, x: %d, y: %d", this->index, this->x_pos, this->y_pos); UpdateStateChecksum((((uint64) this->x_pos) << 32) | this->y_pos); + DEBUG_UPDATESTATECHECKSUM("RoadVehicle::Tick 2: v: %u, state: %d, frame: %d", this->index, this->state, this->frame); UpdateStateChecksum((((uint64) this->state) << 32) | this->frame); if (this->IsFrontEngine()) { if (!(this->IsRoadVehicleStopped() || this->IsWaitingInDepot())) this->running_ticks++; diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index c3c8a06522..da27ec8696 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -524,6 +524,7 @@ static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, Tr default: NOT_REACHED(); } } + DEBUG_UPDATESTATECHECKSUM("ChooseShipTrack: v: %u, path_found: %d, track: %d", v->index, path_found, track); UpdateStateChecksum((((uint64) v->index) << 32) | (path_found << 16) | track); v->HandlePathfindingResult(path_found); @@ -971,6 +972,7 @@ reverse_direction: bool Ship::Tick() { + DEBUG_UPDATESTATECHECKSUM("Ship::Tick: v: %u, x: %d, y: %d", this->index, this->x_pos, this->y_pos); UpdateStateChecksum((((uint64) this->x_pos) << 32) | this->y_pos); if (!((this->vehstatus & VS_STOPPED) || this->IsWaitingInDepot())) this->running_ticks++; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 6c75aea4d7..1fdee5de3e 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -3326,6 +3326,7 @@ static Track ChooseTrainTrack(Train *v, TileIndex tile, DiagDirection enterdir, TileIndex new_tile = res_dest.tile; Track next_track = DoTrainPathfind(v, new_tile, dest_enterdir, tracks, path_found, do_track_reservation, &res_dest); + DEBUG_UPDATESTATECHECKSUM("ChooseTrainTrack: v: %u, path_found: %d, next_track: %d", v->index, path_found, next_track); UpdateStateChecksum((((uint64) v->index) << 32) | (path_found << 16) | next_track); if (new_tile == tile) best_track = next_track; v->HandlePathfindingResult(path_found); @@ -5220,7 +5221,8 @@ Money Train::GetRunningCost() const */ bool Train::Tick() { - UpdateStateChecksum((((uint64) this->x_pos) << 32) | (this->y_pos << 16) | this->track ); + DEBUG_UPDATESTATECHECKSUM("Train::Tick: v: %u, x: %d, y: %d, track: %d", this->index, this->x_pos, this->y_pos, this->track); + UpdateStateChecksum((((uint64) this->x_pos) << 32) | (this->y_pos << 16) | this->track); if (this->IsFrontEngine()) { if (!((this->vehstatus & VS_STOPPED) || this->IsWaitingInDepot()) || this->cur_speed > 0) this->running_ticks++;