Resource Limits
It is apropos that after Earth Day, my thoughts turn to resource limits on my computers. rlimit is a small utility I wrote for myself decades ago. When introduced to new platforms, whether HP-UX, Linux, Raspberry Pi, or more recently, Apple's M1, rlimit let me get to know the system better.
Sysadmins have an array of commands to do likewise, and on large servers, they have offered me detailed profiles and configurations to suit my needs. Still, I preferred a limited programmer's view, at least to start with. For example, sometimes all I wanted to know was how many files I could have open in a given process, and how much I could increase it programmatically.
Below is the code for rlimit, and it compiles on both Linux and MacOS. One improvement would be to display the output in friendlier units other than bytes and micro-seconds.
To build:
I used the -w option to supress the warning of printing rlim_t as an unsigned long.
To run:
Sample output on a MacBook Air M1:
#include <stdlib.h>
#include <memory.h>
#include <sys/resource.h>
#include <inttypes.h>
#ifdef __DARWIN_C_LEVEL
// Structure for MacOS
struct
{
int rcode;
char *description;
}rlimitInfo[]=
{
{RLIMIT_CPU, "CPU time per process"},
{RLIMIT_FSIZE, "File size"},
{RLIMIT_DATA, "Data segment size"},
{RLIMIT_STACK, "Stack size"},
{RLIMIT_CORE, "Core file size"},
#if __DARWIN_C_LEVEL >= __DARWIN_C_FULL
{RLIMIT_RSS, "Source compatibility alias"},
{RLIMIT_MEMLOCK, "Locked-in-memory address space"},
{RLIMIT_NPROC, "Number of processes"},
#else
{RLIMIT_RSS, "Address spece (resident set size)"},
{RLIMIT_MEMLOCK, "Undefined"},
{RLIMIT_NPROC, "Undefined"},
#endif
{RLIMIT_NOFILE, "Number of open files"},
};
#else
// Structure for Linux
// Can also check for
// #ifdef __USE_POSIX
struct
{
int rcode;
char *description;
}rlimitInfo[]=
{
{RLIMIT_CPU, "CPU time in seconds"},
{RLIMIT_FSIZE, "File size"},
{RLIMIT_DATA, "Data size"},
{RLIMIT_STACK, "Stack size"},
{RLIMIT_CORE, "Core file size"},
{RLIMIT_RSS, "Resident set size"},
{RLIMIT_NPROC, "Number of processes"},
{RLIMIT_NOFILE, "Number of open files"},
{RLIMIT_MEMLOCK, "Locked-in-memory address space"},
{RLIMIT_AS, "Address space limit"},
{RLIMIT_LOCKS, "File locks held"},
{RLIMIT_SIGPENDING, "Number of pending signals"},
{RLIMIT_MSGQUEUE, "Bytes in POSIX mqueues"},
{RLIMIT_NICE, "Nice prio allowed to raise to"},
{RLIMIT_RTPRIO, "Realtime priority"},
{RLIMIT_RTTIME, "Timeout for RT tasks in micro-seconds"}
};
#endif
int main()
{
int i;
struct rlimit limit;
for (int i=0; i<RLIM_NLIMITS; ++i)
{
getrlimit (rlimitInfo[i].rcode, &limit);
printf ("code=%d %s\n\tcurrent=%lu, max=%lu\n",
rlimitInfo[i].rcode, rlimitInfo[i].description,
limit.rlim_cur, limit.rlim_max);
}
return 0;
}
Comments