vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi .misc, I've written a piece of code based on the examples of the man pages of sysctl(). After some modifications and compiling, I executed the binary and got: 0.00000 Which wasn't the kind of load average I was looking for.. Does anybody know where I'm going wrong here? The code -> [teksec:~/C]$ cat data.c #include <sys/param.h> #include <sys/sysctl.h> #include <uvm/uvm_param.h> int main() { int mib[2]; struct loadavg load; size_t len; len = sizeof(load); mib[0] = CTL_VM; mib[1] = VM_LOADAVG; sysctl(mib,2,&load, &len, NULL, 0); printf("%f\n",load.ldavg[0]); } [teksec:~/C]$ <- edoc ehT If somebody could help me out with this one... Thanks! Sacha PS: struct loadavg... load.ldavg is of some wicked filetype. I don't think this goes well with the given printf() parameters. |
| |||
| On Fri, 17 Oct 2003, S.Ligthert wrote: > After some modifications and compiling, I executed the binary and got: > 0.00000 > Which wasn't the kind of load average I was looking for.. > printf("%f\n",load.ldavg[0]); ldavg is fixed point (integer), not floating point. divide by the scale. you want printf("%f\n", (float)load.ldavg[0] / load.fscale); > PS: struct loadavg... load.ldavg is of some wicked filetype. I don't > think this goes well with the given printf() parameters. well, kinda. fixpt_t is really uint32_t and works fine with %d, but you probably want to scale and print as a float. -- "First, it was not a strip bar, it was an erotic club. And second, what can I say? I'm a night owl." - M. Barry, Mayor of Washington, DC |
| |||
| Hi Ted, >>printf("%f\n",load.ldavg[0]); > > > ldavg is fixed point (integer), not floating point. divide by the scale. > > you want printf("%f\n", (float)load.ldavg[0] / load.fscale); Thanks! I would never have thought of that.. (thanking my inferior C knowledge) >>PS: struct loadavg... load.ldavg is of some wicked filetype. I don't >>think this goes well with the given printf() parameters. > > > well, kinda. fixpt_t is really uint32_t and works fine with %d, but you > probably want to scale and print as a float. Do you have any RTFM/pointer/hints/etc concerning these kind of file types and how to handle them? (So I won't have to bother comp.unix.bsd.openbsd.misc with it again I have to confess this is the first time I have even coded this "deep" with systemfunctions. But .c .h and man files are a great help Sacha |
| |||
| On Sat, 18 Oct 2003, S.Ligthert wrote: > > well, kinda. fixpt_t is really uint32_t and works fine with %d, but you > > probably want to scale and print as a float. > > Do you have any RTFM/pointer/hints/etc concerning these kind of file > types and how to handle them? look at the sources to a program like w that does the same thing. or grep for fixpt_t in sys/sys/*.h and find the typedef. -- "What right does Congress have to go around making laws just because they deem it necessary?" - M. Barry, Mayor of Washington, DC |
| |||
| > look at the sources to a program like w that does the same thing. > or grep for fixpt_t in sys/sys/*.h and find the typedef. These kind if tools help me a lott atm. But I'm still stuck at with the following piece of code :s The code of top.c is very helpfull, with machine.c at the front of it all! ----> code #include <stdlib.h> #include <stdio.h> #include <sys/param.h> #include <sys/sysctl.h> #include <uvm/uvm_param.h> int main() { int mib[2]; size_t length; struct vmtotal memstats; mib[0] = CTL_VM; mib[1] = VM_METER; length = sizeof(memstats); sysctl( mib, 2, &memstats, &length, NULL, 0); /* All the folowing memstats.* types are: u_int32_t */ /* Struct info: vmmeter.h: line 44 of 99 [44%] */ printf("Total Virtual Memory: %d\n",memstats.t_vm); printf("Active Virtual Memory: %d\n",memstats.t_avm); printf("Total Real Memory in use: %d\n",memstats.t_rm); printf("Active Real Memory: %d\n",memstats.t_arm); printf("Shared Real Memory: %d\n",memstats.t_vmshr); printf("Active Shared Virtual Memory: %d\n",memstats.t_avmshr); printf("Shared Real Memory: %d\n",memstats.t_rmshr); printf("Active Shared Real Memory %d\n",memstats.t_armshr); } edoc <---- It gives the weirdest outputs (that obviously need / 1024. But doesn't help alot :-s ). I think I have to convert it from "pages" with a size of "4096" to Kilobytes. After some googling Cygwin site told me that u_int32_t the same is as unsigned int (which doesn't show up at sys/sys/*.h). Thanx so far! :-) Sacha PS: Sorry for bugging you with this :-\ This little student really under-estimated his C-skills. But after this is fixed, I think I got the hardest part behind me then. |
| |||
| Hi .misc Ted Unangst wrote: > look at the sources to a program like w that does the same thing. > or grep for fixpt_t in sys/sys/*.h and find the typedef. These kind if tools help me a lott atm. But I'm still stuck at with the following piece of code :s The code of top.c is very helpfull, with machine.c at the front of it all! ----> code #include <stdlib.h> #include <stdio.h> #include <sys/param.h> #include <sys/sysctl.h> #include <uvm/uvm_param.h> int main() { int mib[2]; size_t length; struct vmtotal memstats; mib[0] = CTL_VM; mib[1] = VM_METER; length = sizeof(memstats); sysctl( mib, 2, &memstats, &length, NULL, 0); /* All the folowing memstats.* types are: u_int32_t */ /* Struct info: vmmeter.h: line 44 of 99 [44%] */ printf("Total Virtual Memory: %d\n",memstats.t_vm); printf("Active Virtual Memory: %d\n",memstats.t_avm); printf("Total Real Memory in use: %d\n",memstats.t_rm); printf("Active Real Memory: %d\n",memstats.t_arm); printf("Shared Real Memory: %d\n",memstats.t_vmshr); printf("Active Shared Virtual Memory: %d\n",memstats.t_avmshr); printf("Shared Real Memory: %d\n",memstats.t_rmshr); printf("Active Shared Real Memory %d\n",memstats.t_armshr); } edoc <---- It gives the weirdest outputs (that obviously need / 1024. But doesn't help alot :-s ). I think I have to convert it from "pages" with a size of "4096" to Kilobytes. After some googling Cygwin site told me that u_int32_t the same is as unsigned int (which doesn't show up at sys/sys/*.h). Thanx so far! Sacha PS: Sorry for bugging you with this This little student really under-estimated his C-skills. But after this is fixed, I think I got the hardest part behind me then. |
| |||
| On Sun, 19 Oct 2003, S.Ligthert wrote: > It gives the weirdest outputs (that obviously need / 1024. But doesn't > help alot :-s ). > I think I have to convert it from "pages" with a size of "4096" to > Kilobytes. After some googling Cygwin site told me that u_int32_t the > same is as unsigned int (which doesn't show up at sys/sys/*.h). not sure what the question is, but many vm stats are kept in pages. so to get k, have to multiply by (pagesize / 1024). u_int32_t may be defined in arch/*/include/*.h, there's many more typedefs and defines hiding in there. -- "I am making this trip to Africa because Washington is an international city, just like Tokyo, Nigeria or Israel. As mayor, I am an international symbol. Can you deny that to Africa?" - M. Barry, Mayor of Washington, DC |
| ||||
| Hi Ted + .misc > not sure what the question is, but many vm stats are kept in pages. so to > get k, have to multiply by (pagesize / 1024). > u_int32_t may be defined in arch/*/include/*.h, there's many more typedefs > and defines hiding in there. The issue is now resolved. The figures I got was the memory size in pages of 4096 bits. Instead of /1024 or /4096 the lott I had to do *4096 to get the memory size in bits. Twice /1024 to get the size in Megabytes It took me a g'ddamn weekend, but hey! A wise lesson learned today Thanks for your feedback Sacha |