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,
...
- 11-23-2008 #1Just 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;
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;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); } }
$ cat /proc/loadavg; ./test
Help me please.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
Sorry if I misspelled some words, I'm not a native speaker. (french)
- 11-25-2008 #2
Sample code:
Hope this helps.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() */--
Bill
Old age and treachery will overcome youth and skill.
- 11-25-2008 #3Just Joined!
- Join Date
- Nov 2008
- Posts
- 2
that helped, thank you.
can I have a little explication of this:
I'm new to C and i don't really understand what is it.Code:float shiftfloat=(1<<SI_LOAD_SHIFT);
- 11-26-2008 #4Almost 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:I'm new to C
But I'll help you with one item which won't be obvious from the C tutorial, ok? :)Code:C tutorial
One statement in the program is
That file is found at /usr/include/sys/sysinfo.h. In that file is this statement:Code:#include <sys/sysinfo.h>
That file is found at /usr/include/linux/kernel.h. In that file is this statement:Code:#include <linux/kernel.h>
Just so you know.Code:#define SI_LOAD_SHIFT 16
--
Bill
Old age and treachery will overcome youth and skill.


