deater/linux_logo

Potential Enhancement to Obtaining Memory Information via Sysinfo Syscall on Linux

Opened this issue · 0 comments

Referencing two issues:

  1. (My bug report to debian) https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=839594
  2. RAM size shown is incorrect (Debian 9.2) #14

The problem was that Linus Torvalds wanted to remove privileged kernel memory information from
/proc/iomem -- but found that it broke a lot of system utilities and decided to only expose the information
to privleged users.
/proc/iomem: only expose physical resource addresses to privileged users
https://lore.kernel.org/patchwork/patch/1006236/

As the general conclusion (in issue #14 and Debian bug 839594) was that /proc/meminfo is available
to unprivileged users for getting memory size information.

However, instead of accessing /proc/meminfo directly why not use the sysinfo(&struct) call on Linux systems?
The structure used by sysinfo should not change (at least not very often) and it would provide a layer of abstraction
between linux_logo and the memory information.

Granted the information returned by sysinfo() can be obtained through /proc/meminfo -- but I think the abstraction provided
by the syscall (returning data in the common structure) means that we wouldn't have to deal with manually accessing the
raw information and would be better moving forward.

The man page for sysinfo(): https://man7.org/linux/man-pages/man2/sysinfo.2.html
Example usage: https://www.informit.com/articles/article.aspx?p=23618&seqNum=15

It seems that code for linux_logo already uses code from libsysinfo and sysinfo.h....so why not do the following:

const double megabyte = 1024 * 1024;
/* Obtain system statistics. */
struct sysinfo si;
sysinfo (&si);
printf ("total RAM   : %5.1f MB\n", si.totalram / megabyte);
printf ("free RAM   : %5.1f MB\n", si.freeram / megabyte);

// if mem_unit is needed (on later kernels -- see below)
printf ("total RAM   : %5.1f MB\n", (si.totalram * si.mem_unit) / megabyte);
printf ("free RAM   : %5.1f MB\n", (si.freeram * si.mem_unit) / megabyte);

There will need a check to determine if the kernel version is 2.3.16, 2.3.23 (i386), or 2.3.48 (all architectures).

Kernel 2.3.16 (and earlier): the sizes of the memory and swap fields are given in bytes

Since Kernel 2.3.23 (i386) & 2.3.48 (all architectures): sizes of the memory and swap fields are given as multiples of mem_unit bytes.

More information on the sysinfo structure:

Sysinfo structure ``` Until Linux 2.3.16, sysinfo() returned information in the following structure:
       struct sysinfo {
           long uptime;             /* Seconds since boot */
           unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
           unsigned long totalram;  /* Total usable main memory size */
           unsigned long freeram;   /* Available memory size */
           unsigned long sharedram; /* Amount of shared memory */
           unsigned long bufferram; /* Memory used by buffers */
           unsigned long totalswap; /* Total swap space size */
           unsigned long freeswap;  /* Swap space still available */
           unsigned short procs;    /* Number of current processes */
           char _f[22];             /* Pads structure to 64 bytes */
       };

   In the above structure, the sizes of the memory and swap fields
   are given in bytes.

   Since Linux 2.3.23 (i386) and Linux 2.3.48 (all architectures)
   the structure is:

       struct sysinfo {
           long uptime;             /* Seconds since boot */
           unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
           unsigned long totalram;  /* Total usable main memory size */
           unsigned long freeram;   /* Available memory size */
           unsigned long sharedram; /* Amount of shared memory */
           unsigned long bufferram; /* Memory used by buffers */
           unsigned long totalswap; /* Total swap space size */
           unsigned long freeswap;  /* Swap space still available */
           unsigned short procs;    /* Number of current processes */
           unsigned long totalhigh; /* Total high memory size */
           unsigned long freehigh;  /* Available high memory size */
           unsigned int mem_unit;   /* Memory unit size in bytes */
           char _f[20-2*sizeof(long)-sizeof(int)];
                                    /* Padding to 64 bytes */
       };

   In the above structure, sizes of the memory and swap fields are
   given as multiples of mem_unit bytes.
</details>