Kernel AND C SYSINFO


Did you want check load average with  6 digs decimal precision …

Hi a lite time ago i needed check in system information about memory process from kernel level without using calssic  system tools like free uptime ps top  .

First what i need is information about where kernel store information about uptime buffer ram and memory in kernel 2.4 was syscall structure i don’t think so this is changein kernel 4.5.


#define SI_LOAD_SHIFT 16
struct sysinfo {
__kernel_long_t uptime; /* Seconds since boot */
__kernel_ulong_t loads[3]; /* 1, 5, and 15 minute load averages */
__kernel_ulong_t totalram; /* Total usable main memory size */
__kernel_ulong_t freeram; /* Available memory size */
__kernel_ulong_t sharedram; /* Amount of shared memory */
__kernel_ulong_t bufferram; /* Memory used by buffers */
__kernel_ulong_t totalswap; /* Total swap space size */
__kernel_ulong_t freeswap; /* swap space still available */
__u16 procs; /* Number of current processes */
__u16 pad; /* Explicit padding for m68k */
__kernel_ulong_t totalhigh; /* Total high memory size */
__kernel_ulong_t freehigh; /* Available high memory size */
__u32 mem_unit; /* Memory unit size in bytes */
char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)]; /* Padding: libc5 uses this.. */
};

#endif /* _LINUX_SYSINFO_H */

Torvalds github is the best place for this type research:
https://github.com/torvalds/…

So for this structure I create simple code

1 step we must declarate pointer to struct struct


struct sysinfo pointer_to_sysinfo;//declarate struct 
sysinfo (&pointer_to_sysinfo); //

Step 2 print element of struct


pointer_to_sysinfo.uptime
pointer_to_sysinfo.freeram

#include <linux/kernel.h>
#include <stdio.h>
#include <sys/sysinfo.h>

int main ()
{
 /* conversion */
 long minute = 60;
 long hour = 60 * 60;
 long day = 60*60* 24;
 double meg = 1024 * 1024;



 struct sysinfo pointer_to_sysinfo;
 sysinfo (&pointer_to_sysinfo);
//uptime
 printf ("Sytem uptime: %ld is equal ", pointer_to_sysinfo.uptime);
 printf (" %ld days, %ld:%02ld:%02ld  \n\n", pointer_to_sysinfo.uptime / day, (pointer_to_sysinfo.uptime % day) / hour,
     (pointer_to_sysinfo.uptime % hour) / minute, pointer_to_sysinfo.uptime % minute);
//memory
 printf ("total RAM: %5.1f MB\t", pointer_to_sysinfo.totalram / meg);
 printf ("free RAM: %5.1f MB\n", pointer_to_sysinfo.freeram / meg);
 printf   ("Amount of shared memory: %5.1f MB\t", pointer_to_sysinfo.sharedram / meg);
 printf ("Memory used by buffers: %5.1f MB\n", pointer_to_sysinfo.bufferram / meg);
 printf ("Total high memory size: %5.1f \t Available high memory size: %5.1f\n", pointer_to_sysinfo.totalhigh / meg, pointer_to_sysinfo.freehigh / meg);
 printf ("Available high memory size: %5f\n\n", pointer_to_sysinfo.mem_unit );
 //swap
 printf ("Total swap space size: %5.1f MB \t Free Swap: %6.1f MB\n\n", pointer_to_sysinfo.totalswap / meg, pointer_to_sysinfo.freeswap / meg);
//process
 printf ("process count : %d\n\n", pointer_to_sysinfo.procs );
 //Load Average
  float           new_table[3];
  float           div_by_num;
  div_by_num=(float)(1<<SI_LOAD_SHIFT);
  new_table[0]=((float)pointer_to_sysinfo.loads[0])/div_by_num;
  new_table[1]=((float)pointer_to_sysinfo.loads[1])/div_by_num;
  new_table[2]=((float)pointer_to_sysinfo.loads[2])/div_by_num;
 printf ("Load Average 1, 5, and 15 minute\n\t1: %f \t5: %f\t15: %f\n", new_table[0],new_table[1],new_table[2]);

 return 0;
}

 

This code you can find on my GITHUB https://github.com/dzaczek/LINUX_HOBBY/blob/master/testSYSINFO.c

Output:

Screenshot from 2016-07-09 23-24-48

 

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s