| 470 | } |
| 471 | |
| 472 | size_t cbm_mem_peak_rss(void) { |
| 473 | size_t peak_rss = 0; |
| 474 | mi_process_info(NULL, NULL, NULL, NULL, &peak_rss, NULL, NULL, NULL); |
| 475 | /* Peak RSS is by definition >= current RSS. On Linux cbm_mem_rss() reads the |
| 476 | * live /proc/self/statm value (page-granular), while mimalloc's peak_rss |
| 477 | * comes from getrusage's ru_maxrss (KB-granular, and it can lag the live |
| 478 | * statm read by a few pages). Reading the two sources independently lets a |
| 479 | * fresh current read momentarily exceed the reported peak, breaking the |
| 480 | * peak >= current invariant. Reconcile them: the true peak is at least the |
| 481 | * current RSS. (Not observable on macOS, where both come from mimalloc.) */ |
| 482 | size_t current = cbm_mem_rss(); |
| 483 | if (current > peak_rss) { |
| 484 | peak_rss = current; |
| 485 | } |
| 486 | if (peak_rss > 0) { |
| 487 | return peak_rss; |
| 488 | } |
| 489 | /* No OS fallback for peak — return current as best approximation */ |
| 490 | return os_rss(); |
| 491 | } |
| 492 | |
| 493 | size_t cbm_mem_budget(void) { |
| 494 | return g_budget; |