CheckCaches: Improve VehicleCargoList/StationCargoList cache checks

Log failures with details instead of just asserting
pull/510/head
Jonathan G Rennison 1 year ago
parent 08e43a0814
commit 1350e23e29

@ -279,6 +279,20 @@ public:
return this->count == 0 ? 0 : this->cargo_days_in_transit / this->count;
}
/**
* Returns sum of cargo, including reserved cargo.
* @return Sum of cargo.
*/
inline uint TotalCount() const
{
return this->count;
}
inline uint64 CargoDaysInTransit() const
{
return this->cargo_days_in_transit;
}
void InvalidateCache();
};
@ -399,15 +413,6 @@ public:
return this->count - this->action_counts[MTA_LOAD];
}
/**
* Returns sum of cargo, including reserved cargo.
* @return Sum of cargo.
*/
inline uint TotalCount() const
{
return this->count;
}
/**
* Returns sum of reserved cargo.
* @return Sum of reserved cargo.

@ -1500,6 +1500,13 @@ void CheckCaches(bool force_check, std::function<void(const char *)> log, CheckC
auto output_veh_info = [&](char *&p, const Vehicle *u, const Vehicle *v, uint length) {
WriteVehicleInfo(p, lastof(cclog_buffer), u, v, length);
};
auto output_veh_info_single = [&](char *&p, const Vehicle *v) {
uint length = 0;
for (const Vehicle *u = v->First(); u != v; u = u->Next()) {
length++;
}
WriteVehicleInfo(p, lastof(cclog_buffer), v, v->First(), length);
};
#define CCLOGV(...) { \
char *p = cclog_buffer + seprintf(cclog_buffer, lastof(cclog_buffer), __VA_ARGS__); \
@ -1507,6 +1514,12 @@ void CheckCaches(bool force_check, std::function<void(const char *)> log, CheckC
cclog_common(); \
}
#define CCLOGV1(...) { \
char *p = cclog_buffer + seprintf(cclog_buffer, lastof(cclog_buffer), __VA_ARGS__); \
output_veh_info_single(p, v); \
cclog_common(); \
}
if (flags & CHECK_CACHE_GENERAL) {
/* Check the town caches. */
std::vector<TownCache> old_town_caches;
@ -1816,18 +1829,40 @@ void CheckCaches(bool force_check, std::function<void(const char *)> log, CheckC
/* Check whether the caches are still valid */
for (Vehicle *v : Vehicle::Iterate()) {
byte buff[sizeof(VehicleCargoList)];
memcpy(buff, &v->cargo, sizeof(VehicleCargoList));
Money old_feeder_share = v->cargo.FeederShare();
uint old_count = v->cargo.TotalCount();
uint64 old_cargo_days_in_transit = v->cargo.CargoDaysInTransit();
v->cargo.InvalidateCache();
assert(memcmp(&v->cargo, buff, sizeof(VehicleCargoList)) == 0);
uint changed = 0;
if (v->cargo.FeederShare() != old_feeder_share) SetBit(changed, 0);
if (v->cargo.TotalCount() != old_count) SetBit(changed, 1);
if (v->cargo.CargoDaysInTransit() != old_cargo_days_in_transit) SetBit(changed, 2);
if (changed != 0) {
CCLOGV1("vehicle cargo cache mismatch: %c%c%c",
HasBit(changed, 0) ? 'f' : '-',
HasBit(changed, 1) ? 't' : '-',
HasBit(changed, 2) ? 'd' : '-');
}
}
for (Station *st : Station::Iterate()) {
for (CargoID c = 0; c < NUM_CARGO; c++) {
byte buff[sizeof(StationCargoList)];
memcpy(buff, &st->goods[c].cargo, sizeof(StationCargoList));
uint old_count = st->goods[c].cargo.TotalCount();
uint64 old_cargo_days_in_transit = st->goods[c].cargo.CargoDaysInTransit();
st->goods[c].cargo.InvalidateCache();
assert(memcmp(&st->goods[c].cargo, buff, sizeof(StationCargoList)) == 0);
uint changed = 0;
if (st->goods[c].cargo.TotalCount() != old_count) SetBit(changed, 0);
if (st->goods[c].cargo.CargoDaysInTransit() != old_cargo_days_in_transit) SetBit(changed, 1);
if (changed != 0) {
CCLOG("station cargo cache mismatch: station %i, company %i, cargo %u: %c%c",
st->index, (int)st->owner, c,
HasBit(changed, 0) ? 't' : '-',
HasBit(changed, 1) ? 'd' : '-');
}
}
/* Check docking tiles */
@ -1903,8 +1938,9 @@ void CheckCaches(bool force_check, std::function<void(const char *)> log, CheckC
}
}
#undef CCLOGV
#undef CCLOG
#undef CCLOGV
#undef CCLOGV1
}
/**

Loading…
Cancel
Save