diff --git a/tools/timeit.c b/tools/timeit.c --- a/tools/timeit.c +++ b/tools/timeit.c @@ -19,6 +19,10 @@ #include #include +#if defined(__APPLE__) +#include +#endif + /* Enumeration for our exit status codes. */ enum ExitCode { /* \brief Indicates a failure monitoring the target. */ @@ -333,7 +337,17 @@ if (g_target_data_size_limit != ~(rlim_t) 0) { set_resource_limit(RLIMIT_DATA, g_target_data_size_limit); } + if (g_target_rss_size_limit != ~(rlim_t) 0) { +#if defined(__APPLE__) + // On Apple platforms, RLIMIT_RSS is mapped to RLIMIT_AS and setting + // RLIMIT_AS to a value smaller than the current virtual memory size will + // fail. To avoid issues, first get the current size of the virtual memory + // and add the user requested limit. + struct proc_taskinfo task_info; + proc_pidinfo(getpid(), PROC_PIDTASKINFO, 0, &task_info, sizeof(task_info)); + g_target_rss_size_limit += task_info.pti_virtual_size; +#endif set_resource_limit(RLIMIT_RSS, g_target_rss_size_limit); } if (g_target_file_size_limit != ~(rlim_t) 0) {