From 73db2acc374c99ca8224e44a7383f69e7ca24a4f Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 2 Sep 2010 10:57:58 +0200 Subject: [PATCH] memory fragmentation reporting in INFO also added for Mac OS X --- src/config.h | 5 +++++ src/zmalloc.c | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/config.h b/src/config.h index acc95cf5..e2d84818 100644 --- a/src/config.h +++ b/src/config.h @@ -26,6 +26,11 @@ #define HAVE_PROCFS 1 #endif +/* test for task_info() */ +#if defined(__APPLE__) +#define HAVE_TASKINFO 1 +#endif + /* test for backtrace() */ #if defined(__APPLE__) || defined(__linux__) #define HAVE_BACKTRACE 1 diff --git a/src/zmalloc.c b/src/zmalloc.c index 81fc4c04..544155e7 100644 --- a/src/zmalloc.c +++ b/src/zmalloc.c @@ -32,10 +32,7 @@ #include #include #include -#include -#include -#include -#include + #include "config.h" #if defined(__sun) @@ -176,8 +173,14 @@ void zmalloc_enable_thread_safeness(void) { } /* Fragmentation = RSS / allocated-bytes */ + +#if defined(HAVE_PROCFS) +#include +#include +#include +#include + float zmalloc_get_fragmentation_ratio(void) { -#ifdef HAVE_PROCFS size_t allocated = zmalloc_used_memory(); int page = sysconf(_SC_PAGESIZE); size_t rss; @@ -208,7 +211,29 @@ float zmalloc_get_fragmentation_ratio(void) { rss = strtoll(p,NULL,10); rss *= page; return (float)rss/allocated; -#else - return 0; -#endif } +#elif defined(HAVE_TASKINFO) +#include +#include +#include +#include +#include +#include +#include + +float zmalloc_get_fragmentation_ratio(void) { + task_t task = MACH_PORT_NULL; + struct task_basic_info t_info; + mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; + + if (task_for_pid(current_task(), getpid(), &task) != KERN_SUCCESS) + return 0; + task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count); + + return (float)t_info.resident_size/zmalloc_used_memory(); +} +#else +float zmalloc_get_fragmentation_ratio(void) { + return 0; +} +#endif