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