From 615f6923d5fde35478c82845c564eeaa38174676 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 17 Dec 2014 17:11:20 +0100 Subject: [PATCH] getMemorySize() moved into zmalloc.c with other low level mem utils. See issue #2218. --- src/redis.c | 51 +-------------------------------------------------- src/zmalloc.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/zmalloc.h | 1 + 3 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/redis.c b/src/redis.c index 732db76b..6bb78a3f 100644 --- a/src/redis.c +++ b/src/redis.c @@ -289,7 +289,6 @@ struct redisCommand redisCommandTable[] = { }; struct evictionPoolEntry *evictionPoolAlloc(void); -static size_t getMemorySize(void); /*============================ Utility functions ============================ */ @@ -1761,7 +1760,7 @@ void initServer(void) { server.clients_waiting_acks = listCreate(); server.get_ack_from_slaves = 0; server.clients_paused = 0; - server.system_memory_size = getMemorySize(); + server.system_memory_size = zmalloc_get_memory_size(); createSharedObjects(); adjustOpenFilesLimit(); @@ -2572,54 +2571,6 @@ void commandCommand(redisClient *c) { } } -/** - * Returns the size of physical memory (RAM) in bytes. - * It looks ugly, but this is the cleanest way to achive cross platform results. - * Cleaned up from: - * http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system - */ -size_t getMemorySize() { -#if defined(__unix__) || defined(__unix) || defined(unix) || \ - (defined(__APPLE__) && defined(__MACH__)) -#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64)) - int mib[2]; - mib[0] = CTL_HW; -#if defined(HW_MEMSIZE) - mib[1] = HW_MEMSIZE; /* OSX. --------------------- */ -#elif defined(HW_PHYSMEM64) - mib[1] = HW_PHYSMEM64; /* NetBSD, OpenBSD. --------- */ -#endif - int64_t size = 0; /* 64-bit */ - size_t len = sizeof(size); - if (sysctl( mib, 2, &size, &len, NULL, 0) == 0) - return (size_t)size; - return 0L; /* Failed? */ - -#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) - /* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */ - return (size_t)sysconf(_SC_PHYS_PAGES) * (size_t)sysconf(_SC_PAGESIZE); - -#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM)) - /* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */ - int mib[2]; - mib[0] = CTL_HW; -#if defined(HW_REALMEM) - mib[1] = HW_REALMEM; /* FreeBSD. ----------------- */ -#elif defined(HW_PYSMEM) - mib[1] = HW_PHYSMEM; /* Others. ------------------ */ -#endif - unsigned int size = 0; /* 32-bit */ - size_t len = sizeof(size); - if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) - return (size_t)size; - return 0L; /* Failed? */ -#endif /* sysctl and sysconf variants */ - -#else - return 0L; /* Unknown OS. */ -#endif -} - /* Convert an amount of bytes into a human readable string in the form * of 100B, 2G, 100M, 4K, and so forth. */ void bytesToHuman(char *s, unsigned long long n) { diff --git a/src/zmalloc.c b/src/zmalloc.c index 6df51a80..fe4a0a18 100644 --- a/src/zmalloc.c +++ b/src/zmalloc.c @@ -364,3 +364,52 @@ size_t zmalloc_get_smap_bytes_by_field(char *field) { size_t zmalloc_get_private_dirty(void) { return zmalloc_get_smap_bytes_by_field("Private_Dirty:"); } + +/* Returns the size of physical memory (RAM) in bytes. + * It looks ugly, but this is the cleanest way to achive cross platform results. + * Cleaned up from: + * http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system + */ +size_t zmalloc_get_memory_size(void) { +#if defined(__unix__) || defined(__unix) || defined(unix) || \ + (defined(__APPLE__) && defined(__MACH__)) +#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64)) + int mib[2]; + mib[0] = CTL_HW; +#if defined(HW_MEMSIZE) + mib[1] = HW_MEMSIZE; /* OSX. --------------------- */ +#elif defined(HW_PHYSMEM64) + mib[1] = HW_PHYSMEM64; /* NetBSD, OpenBSD. --------- */ +#endif + int64_t size = 0; /* 64-bit */ + size_t len = sizeof(size); + if (sysctl( mib, 2, &size, &len, NULL, 0) == 0) + return (size_t)size; + return 0L; /* Failed? */ + +#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) + /* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */ + return (size_t)sysconf(_SC_PHYS_PAGES) * (size_t)sysconf(_SC_PAGESIZE); + +#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM)) + /* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */ + int mib[2]; + mib[0] = CTL_HW; +#if defined(HW_REALMEM) + mib[1] = HW_REALMEM; /* FreeBSD. ----------------- */ +#elif defined(HW_PYSMEM) + mib[1] = HW_PHYSMEM; /* Others. ------------------ */ +#endif + unsigned int size = 0; /* 32-bit */ + size_t len = sizeof(size); + if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) + return (size_t)size; + return 0L; /* Failed? */ +#endif /* sysctl and sysconf variants */ + +#else + return 0L; /* Unknown OS. */ +#endif +} + + diff --git a/src/zmalloc.h b/src/zmalloc.h index 4de2cffe..a47ea6cc 100644 --- a/src/zmalloc.h +++ b/src/zmalloc.h @@ -77,6 +77,7 @@ float zmalloc_get_fragmentation_ratio(size_t rss); size_t zmalloc_get_rss(void); size_t zmalloc_get_private_dirty(void); size_t zmalloc_get_smap_bytes_by_field(char *field); +size_t zmalloc_get_memory_size(void); void zlibc_free(void *ptr); #ifndef HAVE_MALLOC_SIZE