Find the answer to your Linux question:
Results 1 to 4 of 4
hi, I need some precisions about a field of struct sysinfo (passed to sysinfo() ); well. I wrote this function; Code: void get_sys_informations(sys_informations * sys_info) { if(sysinfo(sys_info)==0) { printf("%ld\n",sys_info->loads[0]); printf(sys_info_format, ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    2

    [SOLVED] [c / sysinfo()] explication about struct sysinfo

    hi,
    I need some precisions about a field of struct sysinfo (passed to sysinfo() ); well. I wrote this function;
    Code:
    void get_sys_informations(sys_informations * sys_info)
    {
       if(sysinfo(sys_info)==0)
       {
          printf("%ld\n",sys_info->loads[0]);
    
          printf(sys_info_format,
             sys_info->uptime,
             sys_info->loads[0]/1000,
             sys_info->loads[1]/1000,
             sys_info->loads[2]/1000,
             (sys_info->totalram - sys_info->freeram)/MEGABYTE,
             (sys_info->totalram)/MEGABYTE,
             (sys_info->sharedram)/MEGABYTE,
             (sys_info->bufferram)/MEGABYTE,
             (sys_info->totalswap - sys_info->freeswap)/MEGABYTE,
             (sys_info->totalswap)/MEGABYTE,
             sys_info->procs,
             (sys_info->totalhigh - sys_info->freehigh)/MEGABYTE,
             (sys_info->totalhigh)/MEGABYTE,
             sys_info->mem_unit
          );
       }
       else
       {
          printf("[EE] Can't get system informations.\n");
          exit(EXIT_FAILURE);
       }
    }
    As you can see, I divided sys_info->loads by 1000 as I thinks that will tell me the average cpu usage in %. but I learned that was wrong. anyway... Now, I know that it's the computer load average as described here. My question is, how do i interpret the sysinfo->loads values. man page is not really helpful about this. here is a little exemple about output of this function;
    $ cat /proc/loadavg; ./test
    Code:
    0.05 0.06 0.08 2/158 14502  
    3040
    Uptime: 59320 seconds
    Load averages:
    1 minute: 3% 5 minutes: 3% 15 minutes: 5%
    memory: 1490MB used of 1504MB
    Shared memory: 0MB
    Memory used by buffers: 27MB
    Swap: 0MB used of 996MB
    Process count: 158
    High Mem: 0MB used of 0MB
    Size of a memory block: 1 byte
    Help me please.
    Sorry if I misspelled some words, I'm not a native speaker. (french)

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Sample code:
    Code:
    #include <sys/sysinfo.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
      volatile time_t time1;
      volatile time_t time2;
    
      float           fav[3];
      float           shiftfloat;
    
      struct sysinfo  the_info;
    
      time1=time(NULL);
    
      for(;;)
      {
        time2=time(NULL);
    
        if(time2>=time1+5)
        {
          break;   /* <--------- */
        }
      }
    
      shiftfloat=(float)(1<<SI_LOAD_SHIFT);
    
      if(sysinfo(&the_info))
      {
        perror("sysinfo()");
    
        exit(1);
      }
    
      fav[0]=((float)the_info.loads[0])/shiftfloat;
      fav[1]=((float)the_info.loads[1])/shiftfloat;
      fav[2]=((float)the_info.loads[2])/shiftfloat;
    
      printf("SI_LOAD_SHIFT %d\n",SI_LOAD_SHIFT);
      printf("%f %f %f\n",fav[0],fav[1],fav[2]);
    
      system("uptime");
    
      return 0;
    
    } /* main() */
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    2
    that helped, thank you.

    can I have a little explication of this:
    Code:
    float shiftfloat=(1<<SI_LOAD_SHIFT);
    I'm new to C and i don't really understand what is it.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm new to C
    Almost all of the elements of the statement you quote can be understood if you have basic knowlege of C. I don't have time to write a C tutorial at this point, but several people have done so. To find their tutorials, scroogle this:
    Code:
    C tutorial
    But I'll help you with one item which won't be obvious from the C tutorial, ok? :)

    One statement in the program is
    Code:
    #include <sys/sysinfo.h>
    That file is found at /usr/include/sys/sysinfo.h. In that file is this statement:
    Code:
    #include <linux/kernel.h>
    That file is found at /usr/include/linux/kernel.h. In that file is this statement:
    Code:
    #define SI_LOAD_SHIFT   16
    Just so you know.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...