From cf57b7b517af68c56fb8d4c229e2988c306384c4 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 9 Jan 2012 17:15:23 -0800 Subject: common-utils: Add support for solaris, bsd and mac os/x get_mem_size() is more standardized now. Change-Id: I8e3dc29df0a64a5eb8eea4fd3965b268cb1a85c2 BUG: 772808 Signed-off-by: Harshavardhana Reviewed-on: http://review.gluster.com/2618 Tested-by: Gluster Build System Reviewed-by: Kaleb KEITHLEY Reviewed-by: Vijay Bellur --- libglusterfs/src/common-utils.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'libglusterfs') diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c index ce1e1f45bff..42bfd035569 100644 --- a/libglusterfs/src/common-utils.c +++ b/libglusterfs/src/common-utils.c @@ -42,6 +42,10 @@ #include #include +#if defined GF_BSD_HOST_OS || defined GF_DARWIN_HOST_OS +#include +#endif + #include "logging.h" #include "common-utils.h" #include "revision.h" @@ -1874,30 +1878,25 @@ get_mem_size () { uint64_t memsize = -1; -#ifdef __linux__ - FILE *fp = NULL; - char line[1028] = {0,}; +#if defined GF_LINUX_HOST_OS || defined GF_SOLARIS_HOST_OS - fp = fopen ("/proc/meminfo", "r"); - if (!fp) { - gf_log ("common-utils", GF_LOG_DEBUG, - "Could not open /proc/meminfo"); - return memsize; - } + uint64_t page_size = 0; + uint64_t num_pages = 0; - while (fgets (line, sizeof (line), fp) != 0) { - if (strncmp (line, "MemTotal:", 9) == 0) { - sscanf (line, "%*s %"SCNu64" kB", &memsize); - memsize *= 1024; //convert to bytes - gf_log ("common-utils", GF_LOG_INFO, - "Total Mem: %"PRIu64, memsize); - break; - } - } + page_size = sysconf (_SC_PAGESIZE); + num_pages = sysconf (_SC_PHYS_PAGES); + + memsize = page_size * num_pages; #endif - // TODO: Methods for other platforms - return memsize; +#if defined GF_BSD_HOST_OS || defined GF_DARWIN_HOST_OS + + size_t len = sizeof(memsize); + int name [] = { CTL_HW, HW_PHYSMEM }; + + sysctl (name, 2, &memsize, &len, NULL, 0); +#endif + return memsize; } -- cgit