Resource Limits

Screen shot of rlimit.c

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:

$ gcc rlimit.c -w -o rlimit

I used the -w option to supress the warning of printing rlim_t as an unsigned long

To run:

$ rlimit

Sample output on a MacBook Air M1:

code=0  CPU time per process
current=9223372036854775807, max=9223372036854775807
code=1  File size
current=9223372036854775807, max=9223372036854775807
code=2  Data segment size
current=9223372036854775807, max=9223372036854775807
code=3  Stack size
current=8372224, max=67092480
code=4  Core file size
current=0, max=9223372036854775807
code=5  Source compatibility alias
current=9223372036854775807, max=9223372036854775807
code=6  Locked-in-memory address space
current=9223372036854775807, max=9223372036854775807
code=7  Number of processes
current=1333, max=2000
code=8  Number of open files
current=256, max=9223372036854775807

Source Code: rlimit.c

#include <stdio.h>

#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

Popular posts from this blog

Bookshelf: UNIX A History and a Memoir

Bookshelf Classic: The C Programming Language

Connections