Repository navigation
Use 64-bit counters for run statistics - #552
Conversation
Byte, op, hit/miss, error and duration accumulators were `unsigned long`, which is 32-bit on LLP64 (Windows) and ILP32 (i386/armhf Linux): byte totals wrap at 4 GiB, microsecond durations wrap after ~71 minutes, and `tv_sec * 1000000` overflowed where `tv_sec` is 32-bit. Widen them to `unsigned long long` / `long long` and do microsecond arithmetic in `long long`. On LP64 the printed text, CSV, JSON and StatsD output is unchanged; only the printf conversions (%lu -> %llu, %ld -> %lld) change to match the types.
|
🤖 Automated first-pass review — a human maintainer's review is still required before merge. Thanks for this, and for the very thorough write-up. I went through the whole diff. The i386 numbers (negative
The type changes themselves look solid and consistent to me. I'd mainly like to hear your thoughts on keeping the format attributes, since that would protect this fix from regressing. (would file this as a comment, not a formal block) |
run_stats::debug_dump() still printed the now 64-bit m_ops and byte counters with %u; on 32-bit targets that consumes the wrong number of argument slots and shifts every later argument. Print them with %llu, and print the timeval fields (time_t/suseconds_t are not unsigned int) as long long. benchmark_debug_log(), benchmark_error_log(), json_handler::write_obj() and the Prometheus append_fmt() are variadic without a printf format attribute, so -Wformat could not see these. Audited every call with the attribute applied temporarily on x86-64 and i386; two more pre-existing 64-bit mismatches turned up and are fixed here: the reqs_processed debug message in shard_connection.cpp and the key_minimum/key_maximum JSON fields (both unsigned long long printed with %u). Output is unchanged for values below 2^32. print_json() declared start_time_ms/end_time_ms unsigned long long but printed them with %lld; declare them long long to match the signed arithmetic. The printed values are unchanged.
|
Fixed in bf87593:
The x86-64 warning set is identical to master's, and clang-format 18 is clean. |
c0b5ec2 to
bf87593
Compare
fcostaoliveira
left a comment
There was a problem hiding this comment.
Thanks — fixes real wrap/UB on our i386/armhf packages too. CI green.
Why
unsigned longis 32-bit on LLP64 (Windows) and ILP32 (i386/armhf Linux). The run-statistics accumulators use it, so on those platforms:tv_sec * 1000000overflows wheretv_secis 32-bit.I hit this while adding native Windows support (follow-up PR: #553, which depends on this one). It also affects 32-bit Linux builds today.
What
unsigned long long/long long.ts_diff()/ts_diff_now()and the JSON start/finish times is done inlong long.%lu→%llu,%ld→%lld.result_print_to_json()already printed itslongparameters with%lld, which this fixes too.On LP64 (64-bit Linux/macOS) the values and the text, CSV, JSON and StatsD output stay byte-identical. Only the types and their matching conversions change. No JSON keys or CSV columns change.
Testing
run-differential) and the CLI fuzzer also pass.run-fuzz,run-soak): monitor-input fuzz passes. Soak passes every scenario except S4test_connection_churn.py. S4 fails the same way on unmodified master (8 of 8 thread(s) ended before the requested test time was reached,test_connection_churn.py:115) and passes on 2.4, so it predates this PR. Reported separately as Soak S4 (test_connection_churn) fails on master: all threads stop ~120 s in despite --reconnect-on-error #554.bits=32): a 30 s run of 256 KB SETs pushes about 41 GB through the counters.Sets/Countcame out as-622618800193123421, the byte sum was negative, andKB/secread 25,645 instead of about 1,335,909.unsigned longpassed to%lld. The bytes andKB/secerrors come from 32-bit wrapping.Countmatches the server-sideSETcalls exactly, andKB/secmatches Count×size/duration to within 0.02%.--command,--data-import,--run-count, timed.CountandBytes TX, JSON key paths and types, per-second CSV and HDR headers were identical.KB/sec TX= 62,442 before this change (the true byte total modulo 2^32) and 528,047 after. The second figure matches the sum of the per-second byte counts to within 0.003%. Before the change the JSONStart timealso came out negative.Note
Medium Risk
Broad type and format-string sweep across stats, reporting, and metrics paths; low logic change but any missed
%lu/unsigned longpairing could cause wrong output or ABI mismatches on some platforms.Overview
Widens run-statistics accumulators and their public getters from
unsigned long/longtounsigned long long/long longso byte totals, op counts, hits/misses, errors, and microsecond durations do not wrap on 32-bit Linux, Windows LLP64, or similar platforms.The change threads through aggregation (
client_group,run_stats,run_stats_types), live progress and CSV/JSON output (memtier_benchmark,run_stats), and metrics export (metrics_snapshot, StatsDgauge(long long)), with matchingprintf/snprintfconversions (%lu→%llu,%ld→%lld). Duration math ints_diff()/ts_diff_now()and JSON runtime timestamps now uses signed 64-bit arithmetic to avoidtv_sec * 1e6overflow.On 64-bit LP64 builds behavior and formatted output are intended to stay the same aside from the wider types; schema keys and CSV columns are unchanged.
Reviewed by Cursor Bugbot for commit bf87593. Bugbot is set up for automated code reviews on this repo. Configure here.