Re: Querying Kernel about RAM
From: Kasper Dupont (kasperd_at_daimi.au.dk)
Date: 09/29/04
- Previous message: David Schwartz: "Re: i thought RAID 5 had a stake through its heart"
- In reply to: Devlin Doe: "Re: Querying Kernel about RAM"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Sep 2004 22:51:15 +0200
Devlin Doe wrote:
>
> #include <stdio.h>
> #include <string.h>
> #include <unistd.h>
> #include <sys/sysinfo.h>
>
> int main(){
> struct sysinfo si;
> long page_size, num_pages;
>
> page_size = sysconf(_SC_PAGESIZE);
> num_pages = sysconf(_SC_PHYS_PAGES);
> printf("sysconf: Total ram %ld MB\n", (page_size*num_pages)/(1024*1024));
>
> memset(&si, 0, sizeof(sysinfo));
> if ( sysinfo(&si) == 0 ){
> printf("sysinfo: Total ram %ld MB\n", si.totalram/(1024*1024));
> }
> return 0;
> }
Obviously this doesn't work on machines with lots
of RAM. Here is what the code produces on a machine
with lots of RAM:
sysconf: Total ram -129 MB
sysinfo: Total ram 1 MB
Now if I change the code slightly I can avoid
overflows.
printf("sysconf: Total ram %ld MB\n", num_pages/(1024*1024/page_size));
...
printf("sysinfo: Total ram %ld MB\n",si.totalram/(1024*1024/si.mem_unit));
Obviously this only works as long as page_size and
mem_unit divide 1MB, but that is the case.
-- Kasper Dupont
- Previous message: David Schwartz: "Re: i thought RAID 5 had a stake through its heart"
- In reply to: Devlin Doe: "Re: Querying Kernel about RAM"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|