]> git.saurik.com Git - redis.git/commitdiff
memory fragmentation reporting in INFO also added for Mac OS X
authorantirez <antirez@gmail.com>
Thu, 2 Sep 2010 08:57:58 +0000 (10:57 +0200)
committerantirez <antirez@gmail.com>
Thu, 2 Sep 2010 08:57:58 +0000 (10:57 +0200)
src/config.h
src/zmalloc.c

index acc95cf52cee467aa8bdeb4123a717f1779356b1..e2d84818714b2515af68b6c7fb178c5f66eeaba2 100644 (file)
 #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
index 81fc4c044a68ba4ef425e4fa24703ff8ed20991b..544155e78792413554c8d206500450ef0dba820c 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <pthread.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
+
 #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 <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
 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;
+}
+#elif defined(HAVE_TASKINFO)
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <mach/task.h>
+#include <mach/mach_init.h>
+
+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
 }
+#endif