Explorar o código

Statistics code refactoring

Ioannis Koutras %!s(int64=12) %!d(string=hai) anos
pai
achega
c0eca08a90

+ 48 - 0
private-include/statistics.h

@@ -0,0 +1,48 @@
+/*
+ *   Copyright Institute of Communication and Computer Systems (ICCS) 
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+/**
+ * @file   statistics.h
+ * @author Ioannis Koutras (joko@microlab.ntua.gr)
+ * @date   September 2012
+ * @brief  Statistics function.
+ */
+
+#ifndef STATISTICS_H
+#define STATISTICS_H
+#include "dmm_config.h"
+
+#include <inttypes.h>
+#include "dmmlib/dmmstats.h"
+
+/** DMM event type enumeration */
+typedef enum dmm_event_type_en {
+    MALLOC,
+    FREE
+} dmm_event_type;
+
+void update_stats
+(
+ dmmstats_t *dmm_stats,
+ dmm_event_type event,
+#ifdef REQUEST_SIZE_INFO
+ size_t mem_req,
+#endif /* REQUEST_SIZE_INFO */
+ size_t mem_alloc
+);
+
+#endif /* STATISTICS_H */

+ 7 - 0
src/CMakeLists.txt

@@ -135,6 +135,13 @@ if(WITH_DEBUG)
   )
 endif(WITH_DEBUG)
 
+if(NOT STATS STREQUAL "none")
+  set(dmmlib_SRCS
+    ${dmmlib_SRCS}
+    statistics.c
+  )
+endif(NOT STATS STREQUAL "none")
+
 if (WITH_REALLOC)
   set(dmmlib_SRCS
     ${dmmlib_SRCS}

+ 22 - 25
src/bitmap/bitmap_free.c

@@ -29,6 +29,7 @@
 #include "trace.h"
 
 #ifdef WITH_ALLOCATOR_STATS
+#include "statistics.h"
 #include "dmmlib/dmmlib.h"
 #endif /* WITH_ALLOCATOR_STATS */
 
@@ -45,31 +46,6 @@ void bitmap_free(bitmap_rb_t *raw_block, void *ptr) {
 
     cells_used = chunk_header->num_of_cells;
 
-#ifndef REQUEST_SIZE_INFO
-    TRACE_1("dmmlib - free - free'ing %zu bytes from bitmap raw block %p\n",
-            cells_used * raw_block->bytes_per_cell, (void *)raw_block);
-#else /* REQUEST_SIZE_INFO */
-    TRACE_1("dmmlib - free - free'ing %zu bytes from bitmap raw block %p, out of "
-            "which %zu were used by the application\n",
-            cells_used * raw_block->bytes_per_cell,
-            (void *)raw_block,
-            chunk_header->requested_size);
-#endif /* REQUEST_SIZE_INFO */
-
-#ifdef WITH_ALLOCATOR_STATS
-    systemallocator.dmm_stats.total_mem_allocated -= cells_used *
-        raw_block->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 */
-
     cell_no = (size_t) ((char *)chunk_header -
             ((char *)raw_block + sizeof(bitmap_rb_t) + 
              raw_block->elements * BMAP_EL_SIZE))
@@ -94,4 +70,25 @@ void bitmap_free(bitmap_rb_t *raw_block, void *ptr) {
             mask_counter = 0;
         }
     }
+
+#ifndef REQUEST_SIZE_INFO
+    TRACE_1("dmmlib - free - free'ing %zu bytes from bitmap raw block %p\n",
+            cells_used * raw_block->bytes_per_cell, (void *)raw_block);
+#else /* REQUEST_SIZE_INFO */
+    TRACE_1("dmmlib - free - free'ing %zu bytes from bitmap raw block %p, "
+            "out of which %zu were used by the application\n",
+            cells_used * raw_block->bytes_per_cell,
+            (void *)raw_block,
+            chunk_header->requested_size);
+#endif /* REQUEST_SIZE_INFO */
+
+#ifdef WITH_ALLOCATOR_STATS
+            update_stats(&systemallocator.dmm_stats,
+                    FREE,
+#ifdef REQUEST_SIZE_INFO
+                    chunk_header->requested_size,
+#endif /* REQUEST_SIZE_INFO */
+                    cells_used * raw_block->bytes_per_cell);
+#endif /* WITH_ALLOCATOR_STATS */
+
 }

+ 5 - 11
src/bitmap/bitmap_malloc.c

@@ -29,6 +29,7 @@
 #include "trace.h"
 
 #ifdef WITH_ALLOCATOR_STATS
+#include "statistics.h"
 #include "dmmlib/dmmlib.h"
 #endif /* WITH_ALLOCATOR_STATS */
 
@@ -129,20 +130,13 @@ void * bitmap_malloc(bitmap_rb_t *raw_block, size_t req_size) {
                     (void *)raw_block);
 
             // 6. Statistics support
-
 #ifdef WITH_ALLOCATOR_STATS
-            systemallocator.dmm_stats.total_mem_allocated +=
-                cells * raw_block->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);
+            update_stats(&systemallocator.dmm_stats,
+                    MALLOC,
 #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);
+                    req_size - CHUNK_HDR_SIZE,
 #endif /* REQUEST_SIZE_INFO */
+                    cells * raw_block->bytes_per_cell);
 #endif /* WITH_ALLOCATOR_STATS */
 
             break;

+ 8 - 10
src/free.c

@@ -37,6 +37,10 @@
 #include "freelist/freelist_rb.h"
 #endif /* FL_RB_ONLY */
 
+#ifdef WITH_ALLOCATOR_STATS
+#include "statistics.h"
+#endif /* WITH_ALLOCATOR_STATS */
+
 #include "release_memory.h"
 
 #include "trace.h"
@@ -95,18 +99,12 @@ void free(void *ptr) {
                 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);
+        update_stats(&systemallocator.dmm_stats,
+                FREE,
 #ifdef REQUEST_SIZE_INFO
-        systemallocator.dmm_stats.total_mem_requested -=
-            current_raw_block->size - sizeof(raw_block_header_t);
-        TRACE_1("dmmlib - global requested memory: %zu bytes\n",
-                systemallocator.dmm_stats.total_mem_requested);
+                current_raw_block->size - sizeof(raw_block_header_t),
 #endif /* REQUEST_SIZE_INFO */
-        systemallocator.dmm_stats.live_objects--;
-        systemallocator.dmm_stats.num_free++;
+                current_raw_block->size);
 #endif /* WITH_ALLOCATOR_STATS */
 
         release_memory(current_raw_block);

+ 6 - 10
src/freelist/freelist_free.c

@@ -32,6 +32,7 @@
 #include "trace.h"
 
 #ifdef WITH_ALLOCATOR_STATS
+#include "statistics.h"
 #include "dmmlib/dmmlib.h"
 #endif /* WITH_ALLOCATOR_STATS */
 
@@ -56,19 +57,14 @@ void freelist_free(freelist_rb_t *raw_block, void *ptr) {
 
     // Memory stats get updated here in case the space gets coalesced with its
     // free neighbors.
+
 #ifdef WITH_ALLOCATOR_STATS
-    systemallocator.dmm_stats.total_mem_allocated -=
-        get_size(block) + HEADER_SIZE;
-    TRACE_1("dmmlib - global allocated memory: %zu bytes\n",
-            systemallocator.dmm_stats.total_mem_allocated);
+            update_stats(&systemallocator.dmm_stats,
+                    FREE,
 #ifdef REQUEST_SIZE_INFO
-    systemallocator.dmm_stats.total_mem_requested -=
-        get_requested_size(block);
-    TRACE_1("dmmlib - global requested memory: %zu bytes\n",
-            systemallocator.dmm_stats.total_mem_requested);
+                    get_requested_size(block),
 #endif /* REQUEST_SIZE_INFO */
-    systemallocator.dmm_stats.live_objects--;
-    systemallocator.dmm_stats.num_free++;
+                    get_size(block) + HEADER_SIZE);
 #endif /* WITH_ALLOCATOR_STATS */
 
 #if defined (COALESCING_FIXED) || defined (COALESCING_VARIABLE)

+ 5 - 9
src/freelist/freelist_malloc.c

@@ -35,6 +35,7 @@
 #include "trace.h"
 
 #ifdef WITH_ALLOCATOR_STATS
+#include "statistics.h"
 #include "dmmlib/dmmlib.h"
 #endif /* WITH_ALLOCATOR_STATS */
 
@@ -137,17 +138,12 @@ void * freelist_malloc(freelist_rb_t *raw_block, size_t size) {
     if(ptr != NULL) {
 
 #ifdef WITH_ALLOCATOR_STATS
-            systemallocator.dmm_stats.total_mem_allocated +=
-                get_size(ptr) + HEADER_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);
+        update_stats(&systemallocator.dmm_stats,
+                MALLOC,
 #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);
+                size,
 #endif /* REQUEST_SIZE_INFO */
+                get_size(ptr) + HEADER_SIZE);
 #endif /* WITH_ALLOCATOR_STATS */
 
         return (void *)((char *)ptr + HEADER_SIZE);

+ 8 - 9
src/malloc.c

@@ -35,6 +35,10 @@
 #include "freelist/freelist_rb.h"
 #endif /* FL_RB_ONLY */
 
+#ifdef WITH_ALLOCATOR_STATS
+#include "statistics.h"
+#endif /* WITH_ALLOCATOR_STATS */
+
 #include "trace.h"
 
 void * malloc(size_t size) {
@@ -110,17 +114,12 @@ void * malloc(size_t size) {
                         (void *)ptr);
 
 #ifdef WITH_ALLOCATOR_STATS
-                systemallocator.dmm_stats.total_mem_allocated +=
-                    size + sizeof(raw_block_header_t);
-                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);
+                update_stats(&systemallocator.dmm_stats,
+                        MALLOC,
 #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);
+                        size,
 #endif /* REQUEST_SIZE_INFO */
+                        size + sizeof(raw_block_header_t));
 #endif /* WITH_ALLOCATOR_STATS */
 
                 ptr = (void *)((char *)ptr + sizeof(raw_block_header_t));

+ 66 - 0
src/statistics.c

@@ -0,0 +1,66 @@
+/*
+ *   Copyright Institute of Communication and Computer Systems (ICCS) 
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+/**
+ * @file   statistics.c
+ * @author Ioannis Koutras (joko@microlab.ntua.gr)
+ * @date   September 2012
+ * @brief  Statistics function implementation.
+ */
+
+#include "statistics.h"
+
+/** Updates a dmmstats_t variable
+ *
+ * @param dmm_stats Pointer to the dmmstats_t variable.
+ * @param event     The event type, could be MALLOC or FREE.
+ * @param mem_alloc Allocated memory size info.
+ * */
+#ifdef REQUEST_SIZE_INFO
+ /**
+  * @param mem_req   Requested memory size info.
+  */
+#endif /* REQUEST_SIZE_INFO */
+void update_stats
+(
+ dmmstats_t *dmm_stats, 
+ dmm_event_type event, 
+#ifdef REQUEST_SIZE_INFO
+ size_t mem_req,
+#endif /* REQUEST_SIZE_INFO */
+ size_t mem_alloc
+) {
+    if(event == MALLOC) {
+        dmm_stats->total_mem_allocated += mem_alloc;
+#ifdef REQUEST_SIZE_INFO
+        dmm_stats->total_mem_requested += mem_req;
+#endif /* REQUEST_SIZE_INFO */
+        dmm_stats->live_objects++;
+        dmm_stats->num_malloc++;
+    }
+
+    if(event == FREE) {
+        dmm_stats->total_mem_allocated -= mem_alloc;
+#ifdef REQUEST_SIZE_INFO
+        dmm_stats->total_mem_requested -= mem_req;
+#endif /* REQUEST_SIZE_INFO */
+        dmm_stats->live_objects--;
+        dmm_stats->num_free++;
+    }
+
+}
+