| 512 | } |
| 513 | |
| 514 | static void write_diagnostics(void) { |
| 515 | #ifdef CBM_DIAGNOSTICS_ENABLE_TEST_API |
| 516 | diag_test_pause_writer(); |
| 517 | #endif |
| 518 | if (atomic_load_explicit(&g_diag_stop, memory_order_acquire)) { |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | size_t elapsed_ms = 0; |
| 523 | size_t user_ms = 0; |
| 524 | size_t sys_ms = 0; |
| 525 | size_t current_rss = 0; |
| 526 | size_t peak_rss = 0; |
| 527 | size_t current_commit = 0; |
| 528 | size_t peak_commit = 0; |
| 529 | size_t page_faults = 0; |
| 530 | mi_process_info(&elapsed_ms, &user_ms, &sys_ms, ¤t_rss, &peak_rss, ¤t_commit, |
| 531 | &peak_commit, &page_faults); |
| 532 | /* Do NOT report mi_process_info's rss fields: on Linux mimalloc never sets |
| 533 | * current_rss, so the field keeps mimalloc's internal committed-page |
| 534 | * counter — deliberately tuned low here (purge_decommits=1, purge_delay=0) |
| 535 | * and able to go transiently NEGATIVE under purge, which wraps through |
| 536 | * size_t and printed rss_bytes of ~2^64 (soak read it as a 17-exabyte |
| 537 | * "leak"). cbm_mem_rss()/cbm_mem_peak_rss() already encode the whole |
| 538 | * lesson: /proc statm is authoritative on Linux, mimalloc is correct on |
| 539 | * macOS/Windows, and peak is reconciled to never undercut current. */ |
| 540 | current_rss = cbm_mem_rss(); |
| 541 | peak_rss = cbm_mem_peak_rss(); |
| 542 | /* The committed pair has the same failure: the counter is signed and |
| 543 | * merged per thread at thread exit, so a thread-per-connection daemon |
| 544 | * reads it negative once short-lived threads have freed what a long-lived |
| 545 | * one committed (query-leak soak, Linux 2026-09-14: heap_committed_bytes |
| 546 | * printed 2^64 - 121 MB from the second sample on). cbm_mem_allocator_ |
| 547 | * committed reports 0 for a negative reading; the peak never undercuts |
| 548 | * the current value and never carries a wrapped one. */ |
| 549 | current_commit = cbm_mem_allocator_committed(); |
| 550 | if (peak_commit > (SIZE_MAX >> 1)) { |
| 551 | peak_commit = 0; |
| 552 | } |
| 553 | if (peak_commit < current_commit) { |
| 554 | peak_commit = current_commit; |
| 555 | } |
| 556 | |
| 557 | int fds = count_open_fds(); |
| 558 | time_t now = time(NULL); |
| 559 | long uptime = (long)(now - g_start_time); |
| 560 | int qcount = atomic_load(&g_query_stats.count); |
| 561 | int qerrors = atomic_load(&g_query_stats.errors); |
| 562 | long long qtime = atomic_load(&g_query_stats.time_us); |
| 563 | long long qmax = atomic_load(&g_query_stats.max_us); |
| 564 | long long qavg = qcount > 0 ? qtime / qcount : 0; |
| 565 | |
| 566 | /* Memory map: live allocator bytes on this thread's heap, a size-class |
| 567 | * histogram, and the residual the walk does NOT account for. The residual |
| 568 | * is what keeps this honest — see mem.h. */ |
| 569 | diag_write_allocator_stats(); |
| 570 | |
| 571 | cbm_mem_map_t map; |
no test coverage detected