/*------------------------------------------------------* | Author: Maurizio Loreti, aka MLO or (HAM) I3NOO | | Work: University of Padova - Department of Physics | | Via F. Marzolo, 8 - 35131 PADOVA - Italy | | Phone: ++39(49) 827-7216 FAX: ++39(49) 827-7102 | | EMail: loreti@padova.infn.it | | WWW: http://wwwcdf.pd.infn.it/~loreti/mlo.html | *------------------------------------------------------* Tries to list available memory on several architectures; Needs -lmach to be linked under Digital Unix. *------------------------------------------------------*/ #include #include #include #include static int memory(void); int main() { struct utsname un; if (uname(&un) >= 0) { fprintf(stdout, "RAM on machine \"%s\", running %s %s :\n", un.nodename, un.sysname, un.release); } return memory(); } #ifdef __sun__ static int memory(void) { long npag; /* Number of physical pages */ long nbyt; /* Numper of bytes per page */ int nmeg; /* Number of Megabytes on the system */ double x; npag = sysconf(_SC_PHYS_PAGES); nbyt = sysconf(_SC_PAGESIZE); x = npag; x *= nbyt; x /= 1024.0; x /= 1024.0; nmeg = x + 0.5; fprintf(stdout, "%ld Pages, %ld Bytes each - %d MBytes total.\n", npag, nbyt, nmeg); return EXIT_SUCCESS; } #endif /* __sun__ */ #ifdef __linux__ #include #include #define BUFFER_LEN 128 static int memory(void) { int retVal = EXIT_FAILURE; int fd; if ((fd = open("/proc/meminfo", O_RDONLY)) < 0) { fputs("Couldn't open /proc/meminfo\n", stderr); perror("open"); } else { char buffer[BUFFER_LEN]; size_t len; if ((len = read(fd, buffer, sizeof(buffer)-1)) < 0) { fputs("Error read from /proc/meminfo\n", stderr); perror("read"); } else { const char *pc = buffer; char *end; unsigned long total, used, free; /* buffer[len]='\0'; puts(buffer); */ while (*pc++ != '\n') {} while (!isspace((unsigned char) *pc)) pc++; total = strtoul(pc, &end, 0) / 1024; used = strtoul(end, &end, 0) / 1024; free = strtoul(end, &end, 0) / 1024; fprintf(stdout, "Total memory: %lu kBytes (%lu kBytes used, %lu kBytes free)\n", total, used, free); retVal = EXIT_SUCCESS; } close(fd); } return retVal; } #endif /* __linux__ */ #ifdef __osf__ #include #include #define LOG1024 10 /* log base 2 of 1024 */ static int memory(void) { int pagesize; int pageshift; /* log base 2 of the pagesize */ long active, total, free; vm_statistics_data_t vmstats; pagesize = getpagesize(); for (pageshift = 0; pagesize > 1; pageshift++, pagesize >>= 1) {} pageshift -= LOG1024; (void) vm_statistics(task_self(), &vmstats); total = active = vmstats.active_count << pageshift; total += (free = vmstats.free_count << pageshift); total += (vmstats.inactive_count + vmstats.wire_count) << pageshift; fprintf(stdout, "%ld active, %ld free, %ld total (in kBytes).\n", active, free, total); return EXIT_SUCCESS; } #endif /* __osf__ */