| 511 | } |
| 512 | |
| 513 | static void write_diagnostics(void) { |
| 514 | #ifdef CBM_DIAGNOSTICS_ENABLE_TEST_API |
| 515 | diag_test_pause_writer(); |
| 516 | #endif |
| 517 | if (atomic_load_explicit(&g_diag_stop, memory_order_acquire)) { |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | size_t elapsed_ms = 0; |
| 522 | size_t user_ms = 0; |
| 523 | size_t sys_ms = 0; |
| 524 | size_t current_rss = 0; |
| 525 | size_t peak_rss = 0; |
| 526 | size_t current_commit = 0; |
| 527 | size_t peak_commit = 0; |
| 528 | size_t page_faults = 0; |
| 529 | mi_process_info(&elapsed_ms, &user_ms, &sys_ms, ¤t_rss, &peak_rss, ¤t_commit, |
| 530 | &peak_commit, &page_faults); |
| 531 | /* Do NOT report mi_process_info's rss fields: on Linux mimalloc never sets |
| 532 | * current_rss, so the field keeps mimalloc's internal committed-page |
| 533 | * counter — deliberately tuned low here (purge_decommits=1, purge_delay=0) |
| 534 | * and able to go transiently NEGATIVE under purge, which wraps through |
| 535 | * size_t and printed rss_bytes of ~2^64 (soak read it as a 17-exabyte |
| 536 | * "leak"). cbm_mem_rss()/cbm_mem_peak_rss() already encode the whole |
| 537 | * lesson: /proc statm is authoritative on Linux, mimalloc is correct on |
| 538 | * macOS/Windows, and peak is reconciled to never undercut current. */ |
| 539 | current_rss = cbm_mem_rss(); |
| 540 | peak_rss = cbm_mem_peak_rss(); |
| 541 | |
| 542 | int fds = count_open_fds(); |
| 543 | time_t now = time(NULL); |
| 544 | long uptime = (long)(now - g_start_time); |
| 545 | int qcount = atomic_load(&g_query_stats.count); |
| 546 | int qerrors = atomic_load(&g_query_stats.errors); |
| 547 | long long qtime = atomic_load(&g_query_stats.time_us); |
| 548 | long long qmax = atomic_load(&g_query_stats.max_us); |
| 549 | long long qavg = qcount > 0 ? qtime / qcount : 0; |
| 550 | |
| 551 | /* Memory map: live allocator bytes on this thread's heap, a size-class |
| 552 | * histogram, and the residual the walk does NOT account for. The residual |
| 553 | * is what keeps this honest — see mem.h. */ |
| 554 | diag_write_allocator_stats(); |
| 555 | |
| 556 | cbm_mem_map_t map; |
| 557 | (void)cbm_mem_map_collect_os(&map); |
| 558 | size_t residual = |
| 559 | map.os_committed_bytes > map.live_bytes ? map.os_committed_bytes - map.live_bytes : 0; |
| 560 | char buckets[CBM_SZ_512]; |
| 561 | int bucket_length = 0; |
| 562 | for (int i = 0; |
| 563 | i < CBM_MEM_MAP_BUCKETS && bucket_length >= 0 && (size_t)bucket_length < sizeof(buckets); |
| 564 | i++) { |
| 565 | int written = |
| 566 | snprintf(buckets + bucket_length, sizeof(buckets) - (size_t)bucket_length, |
| 567 | "%s{\"limit\": %zu, \"bytes\": %zu, \"blocks\": %zu}", i == 0 ? "" : ", ", |
| 568 | cbm_mem_map_bucket_limit(i), map.bucket_bytes[i], map.bucket_blocks[i]); |
| 569 | if (written < 0) { |
| 570 | break; |
no test coverage detected