| 441 | } |
| 442 | |
| 443 | size_t cbm_mem_peak_rss(void) { |
| 444 | size_t peak_rss = 0; |
| 445 | mi_process_info(NULL, NULL, NULL, NULL, &peak_rss, NULL, NULL, NULL); |
| 446 | /* Peak RSS is by definition >= current RSS. On Linux cbm_mem_rss() reads the |
| 447 | * live /proc/self/statm value (page-granular), while mimalloc's peak_rss |
| 448 | * comes from getrusage's ru_maxrss (KB-granular, and it can lag the live |
| 449 | * statm read by a few pages). Reading the two sources independently lets a |
| 450 | * fresh current read momentarily exceed the reported peak, breaking the |
| 451 | * peak >= current invariant. Reconcile them: the true peak is at least the |
| 452 | * current RSS. (Not observable on macOS, where both come from mimalloc.) */ |
| 453 | size_t current = cbm_mem_rss(); |
| 454 | if (current > peak_rss) { |
| 455 | peak_rss = current; |
| 456 | } |
| 457 | if (peak_rss > 0) { |
| 458 | return peak_rss; |
| 459 | } |
| 460 | /* No OS fallback for peak — return current as best approximation */ |
| 461 | return os_rss(); |
| 462 | } |
| 463 | |
| 464 | size_t cbm_mem_budget(void) { |
| 465 | return g_budget; |