瀏覽代碼

Support for raw block and global statistics

Ioannis Koutras 13 年之前
父節點
當前提交
f73e004b24

+ 1 - 1
CMakeLists.txt

@@ -94,7 +94,7 @@ if(RAW_BLOCKS_TYPE STREQUAL "freelist")
 
 endif(RAW_BLOCKS_TYPE STREQUAL "freelist")
 
-message(STATUS "Have statistics: " ${WITH_STATS})
+message(STATUS "Have statistics: " ${STATS})
 message(STATUS "Requested Size per Block: " ${REQUEST_SIZE_INFO})
 message(STATUS "Adaptivity: " ${WITH_ADAPTIVITY})
 message(STATUS "Support for realloc(): " ${WITH_REALLOC})

+ 7 - 2
DefineOptions.cmake

@@ -5,7 +5,7 @@ option(WITH_REALLOC "Build with realloc" OFF)
 set(TRACE_LEVEL 1 CACHE INTEGER "Choose the trace level, options are: 0, 1, 2 and 3")
 option(WITH_EXAMPLES "Build with examples" OFF)
 option(WITH_FIXED_LISTS "Build with predefined lists of fixed-sized blocks" ON)
-option(WITH_STATS "Build with statistics" OFF)
+set(STATS "global" CACHE STRING "Choose if the memory allocator keeps internally statistics per raw block or globally, options are: none, rawblock, global")
 option(WITH_ADAPTIVITY "Build with adaptivity" OFF)
 option(WITH_STATIC_LIB "Build a static library" OFF)
 option(WITH_SHARED_LIB "Build a shared library" OFF)
@@ -76,7 +76,6 @@ if (LINUXTEST)
   set(WITH_SHARED_LIB ON)
   set(WITH_STATIC_LIB ON)
   set(WITH_REALLOC ON)
-  set(WITH_STATS ON)
   set(WITH_DOC ON)
 endif (LINUXTEST)
 
@@ -86,6 +85,12 @@ elseif(RAW_BLOCKS_TYPE STREQUAL "bitmap")
   set(BITMAP_RB_ONLY ON)
 endif(RAW_BLOCKS_TYPE STREQUAL "freelist")
 
+if(STATS STREQUAL "rawblock")
+  set(WITH_RAWBLOCK_STATS ON)
+elseif(STATS STREQUAL "global")
+  set(WITH_ALLOCATOR_STATS ON)
+endif(STATS STREQUAL "rawblock")
+
 if(COALESCE_AFTER_SPLIT)
   if(WITH_SPLITTING STREQUAL "never")
          message(FATAL_ERROR "You have to set WITH_SPLITTING to fixed or variable if you want to coalesce after split.")

+ 2 - 1
dmm_config.h.in

@@ -40,7 +40,8 @@
 
 #cmakedefine REQUEST_SIZE_INFO
 
-#cmakedefine WITH_STATS
+#cmakedefine WITH_RAWBLOCK_STATS
+#cmakedefine WITH_ALLOCATOR_STATS
 #cmakedefine COUNT_ACCESSES
 #cmakedefine COUNT_HOPS
 

+ 7 - 0
include/dmmlib/allocator.h

@@ -31,6 +31,10 @@
 #include <pthread.h>
 #endif /* HAVE_LOCKS */
 
+#ifdef WITH_ALLOCATOR_STATS
+#include <dmmlib/dmmstats.h>
+#endif /* WITH_ALLOCATOR_STATS */
+
 /** The allocator structure of dmmlib. */
 typedef struct allocator_s {
     raw_block_header_t *raw_block_head; /**< The head of the raw blocks list. */
@@ -38,6 +42,9 @@ typedef struct allocator_s {
     pthread_mutex_t creation_mutex; /**< Mutex to allow the creation of new raw
                                       blocks. */
 #endif /* HAVE_LOCKS */
+#ifdef WITH_ALLOCATOR_STATS
+    dmmstats_t dmm_stats;
+#endif /* WITH_ALLOCATOR_STATS */
 } allocator_t;
 
 #endif /* ALLOCATOR_H */

+ 4 - 4
include/dmmlib/raw_block.h

@@ -26,9 +26,9 @@
 #define RAW_BLOCK_H
 #include "dmm_config.h"
 
-#ifdef WITH_STATS
+#ifdef WITH_RAWBLOCK_STATS
 #include <dmmlib/dmmstats.h>
-#endif /* WITH_STATS */
+#endif /* WITH_RAWBLOCK_STATS */
 #ifdef HAVE_LOCKS
 #include <pthread.h> /* FIXME To be removed once mutex is removed. */
 #endif /* HAVE_LOCKS */
@@ -50,9 +50,9 @@ typedef struct raw_block_header_s {
     size_t size; /**< Total available size of the raw block. */
     struct raw_block_header_s *next_raw_block; /**< Pointer to the next raw
                                                  block. */
-#ifdef WITH_STATS
+#ifdef WITH_RAWBLOCK_STATS
     dmmstats_t dmm_stats; /**< Statistics of the heap. */
-#endif /* WITH_STATS */
+#endif /* WITH_RAWBLOCK_STATS */
 #ifdef HAVE_LOCKS
     pthread_mutex_t mutex;/**< Mutex when POSIX Threads are used. */
 #endif /* HAVE_LOCKS */

+ 0 - 7
src/CMakeLists.txt

@@ -118,13 +118,6 @@ elseif(WITH_SYSTEM_CALLS STREQUAL "mmap")
 
 endif(WITH_SYSTEM_CALLS STREQUAL "none")
 
-if (WITH_STATS)
-  set(dmmlib_SRCS
-    ${dmmlib_SRCS}
-#    print_stats.c
-  )
-endif (WITH_STATS)
-
 if (WITH_REALLOC)
   set(dmmlib_SRCS
     ${dmmlib_SRCS}

+ 20 - 2
src/bitmap/bitmap_free.c

@@ -32,6 +32,10 @@
 #include <pthread.h>
 #endif /* HAVE_LOCKS */
 
+#ifdef WITH_ALLOCATOR_STATS
+#include "dmmlib/dmmlib.h"
+#endif /* WITH_ALLOCATOR_STATS */
+
 /** Frees the memory block inside of a specific bitmap-organized raw block.
  * @param raw_block The pointer of the raw block.
  * @param ptr       The pointer of the memory block to be freed.
@@ -62,7 +66,21 @@ void bitmap_free(raw_block_header_t *raw_block, void *ptr) {
             chunk_header->requested_size);
 #endif /* REQUEST_SIZE_INFO */
 
-#ifdef WITH_STATS
+#ifdef WITH_ALLOCATOR_STATS
+    systemallocator.dmm_stats.total_mem_allocated -= cells_used *
+        rb_header->bytes_per_cell;
+    TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
+            systemallocator.dmm_stats.total_mem_allocated);
+#ifdef REQUEST_SIZE_INFO
+    systemallocator.dmm_stats.total_mem_requested -= chunk_header->requested_size;
+    TRACE_1("dmmlib - global requested memory: %zu bytes\n",
+            systemallocator.dmm_stats.total_mem_requested);
+#endif /* REQUEST_SIZE_INFO */
+    systemallocator.dmm_stats.live_objects--;
+    systemallocator.dmm_stats.num_free++;
+#endif /* WITH_ALLOCATOR_STATS */
+
+#ifdef WITH_RAWBLOCK_STATS
     raw_block->dmm_stats.total_mem_allocated -= cells_used *
         rb_header->bytes_per_cell;
     TRACE_1("dmmlib - total allocated memory: %zu bytes\n",
@@ -74,7 +92,7 @@ void bitmap_free(raw_block_header_t *raw_block, void *ptr) {
 #endif /* REQUEST_SIZE_INFO */
     raw_block->dmm_stats.live_objects--;
     raw_block->dmm_stats.num_free++;
-#endif /* WITH_STATS */
+#endif /* WITH_RAWBLOCK_STATS */
 
     cell_no = ((char *)chunk_header - ((char *)rb_header + sizeof(bitmap_rb_t)))
         / rb_header->bytes_per_cell;

+ 21 - 2
src/bitmap/bitmap_malloc.c

@@ -32,6 +32,10 @@
 #include <pthread.h>
 #endif /* HAVE_LOCKS */
 
+#ifdef WITH_ALLOCATOR_STATS
+#include "dmmlib/dmmlib.h"
+#endif /* WITH_ALLOCATOR_STATS */
+
 /**
  * Returns a memory block from a bitmap-organized raw block
  *
@@ -116,7 +120,22 @@ void * bitmap_malloc(raw_block_header_t *raw_block, size_t req_size) {
                     cells * rb_header->bytes_per_cell,
                     (void *)raw_block);
 
-#ifdef WITH_STATS
+#ifdef WITH_ALLOCATOR_STATS
+            systemallocator.dmm_stats.total_mem_allocated +=
+                cells * rb_header->bytes_per_cell;
+            systemallocator.dmm_stats.live_objects++;
+            systemallocator.dmm_stats.num_malloc++;
+            TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
+                    systemallocator.dmm_stats.total_mem_allocated);
+#ifdef REQUEST_SIZE_INFO
+            systemallocator.dmm_stats.total_mem_requested += req_size -
+                CHUNK_HDR_SIZE;
+            TRACE_1("dmmlib - global requested memory: %zu bytes\n",
+                    systemallocator.dmm_stats.total_mem_requested);
+#endif /* REQUEST_SIZE_INFO */
+#endif /* WITH_ALLOCATOR_STATS */
+
+#ifdef WITH_RAWBLOCK_STATS
             raw_block->dmm_stats.total_mem_allocated +=
                 cells * rb_header->bytes_per_cell;
             raw_block->dmm_stats.live_objects++;
@@ -129,7 +148,7 @@ void * bitmap_malloc(raw_block_header_t *raw_block, size_t req_size) {
             TRACE_1("dmmlib - total requested memory: %zu bytes\n",
                     raw_block->dmm_stats.total_mem_requested);
 #endif /* REQUEST_SIZE_INFO */
-#endif /* WITH_STATS */
+#endif /* WITH_RAWBLOCK_STATS */
 
             break;
         }

+ 32 - 0
src/dmmlib.c

@@ -75,6 +75,21 @@ void * malloc(size_t size) {
             if(ptr != NULL) {
                 TRACE_1("dmmlib - malloc - allocated a whole raw block of %zu"
                         " bytes at %p\n", allocation_size, (void *)ptr);
+
+#ifdef WITH_ALLOCATOR_STATS
+                systemallocator.dmm_stats.total_mem_allocated +=
+                    allocation_size;
+                systemallocator.dmm_stats.live_objects++;
+                systemallocator.dmm_stats.num_malloc++;
+                TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
+                        systemallocator.dmm_stats.total_mem_allocated);
+#ifdef REQUEST_SIZE_INFO
+                systemallocator.dmm_stats.total_mem_requested += size;
+                TRACE_1("dmmlib - global requested memory: %zu bytes\n",
+                        systemallocator.dmm_stats.total_mem_requested);
+#endif /* REQUEST_SIZE_INFO */
+#endif /* WITH_ALLOCATOR_STATS */
+
                 ptr = (void *)((char *)ptr + sizeof(raw_block_header_t));
             }
             return ptr;
@@ -127,6 +142,23 @@ void free(void *ptr) {
                 sizeof(raw_block_header_t));
         TRACE_1("dmmlib - free - free'ing %zu bytes from raw block %p\n",
                 current_raw_block->size, (void *)current_raw_block);
+
+#ifdef WITH_ALLOCATOR_STATS
+        systemallocator.dmm_stats.total_mem_allocated -=
+            current_raw_block->size;
+        TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
+                systemallocator.dmm_stats.total_mem_allocated);
+#ifdef REQUEST_SIZE_INFO
+        systemallocator.dmm_stats.total_mem_requested -=
+            current_raw_block->size - sizeof(raw_block_header_t) -
+            sizeof(bitmap_rb_t); // FIXME allocation size is like that currently
+        TRACE_1("dmmlib - global requested memory: %zu bytes\n",
+                systemallocator.dmm_stats.total_mem_requested);
+#endif /* REQUEST_SIZE_INFO */
+        systemallocator.dmm_stats.live_objects--;
+        systemallocator.dmm_stats.num_free++;
+#endif /* WITH_ALLOCATOR_STATS */
+
     }
 }
 

+ 2 - 2
src/freelist/freelist_free.c

@@ -44,11 +44,11 @@ void freelist_free(raw_block_header_t *raw_block, void *ptr) {
 
     // Memory stats get updated here in case the space gets coalesced with its
     // free neighbors.
-#ifdef WITH_STATS
+#ifdef WITH_RAWBLOCK_STATS
     raw_block->dmm_stats.total_mem_allocated -= get_size(ptr);
     raw_block->dmm_stats.live_objects--;
     raw_block->dmm_stats.num_free++;
-#endif /* WITH_STATS */
+#endif /* WITH_RAWBLOCK_STATS */
 
 #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)
     coalesce(rb_header, ptr);

+ 2 - 2
src/freelist/freelist_malloc.c

@@ -123,7 +123,7 @@ void * freelist_malloc(raw_block_header_t *raw_block, size_t size) {
         }
     }
 
-#ifdef WITH_STATS
+#ifdef WITH_RAWBLOCK_STATS
     if(ptr != NULL) {
 #if !defined (SPLITTING_FIXED) && !defined (SPLITTING_VARIABLE)
         raw_block->dmm_stats.total_mem_allocated += get_size(ptr);
@@ -133,7 +133,7 @@ void * freelist_malloc(raw_block_header_t *raw_block, size_t size) {
 #ifdef REQUEST_SIZE_INFO
         raw_block->dmm_stats.total_mem_requested += size;
 #endif /* REQUEST_SIZE_INFO */
-#endif /* WITH_STATS */
+#endif /* WITH_RAWBLOCK_STATS */
     }
 
 #ifdef HAVE_LOCKS