Selaa lähdekoodia

Initial commit for DMM adapting functions.

Ioannis Koutras 13 vuotta sitten
vanhempi
commit
2e86b41627
6 muutettua tiedostoa jossa 90 lisäystä ja 7 poistoa
  1. 1 1
      Makefile
  2. 9 0
      custom_malloc.c
  3. 53 0
      dmm_adaptor.c
  4. 7 0
      dmm_adaptor.h
  5. 8 0
      dmm_init.c
  6. 12 6
      heap.h

+ 1 - 1
Makefile

@@ -5,7 +5,7 @@ WARNINGS := -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align \
 	          -Wuninitialized -Wconversion -Wstrict-prototypes
 CFLAGS := -g -O -std=c99 $(WARNINGS)
 
-OBJ = posix_lock.o other.o block_header.o sys_alloc.o dmm_init.o coalesce.o custom_malloc.o custom_free.o
+OBJ = posix_lock.o other.o block_header.o sys_alloc.o dmm_init.o coalesce.o dmm_adaptor.o custom_malloc.o custom_free.o
 
 %.o: %.c
 	$(CC) -c -o $@ $< $(CFLAGS)

+ 9 - 0
custom_malloc.c

@@ -3,6 +3,7 @@
 #include "other.h"
 #include "sys_alloc.h"
 #include "block_header.h"
+#include "dmm_adaptor.h"
 
 void * custom_malloc(heap_t* heap, size_t size) {
 	void *ptr;
@@ -80,6 +81,14 @@ void * custom_malloc(heap_t* heap, size_t size) {
 
 	// End of Stats
 
+	// Refresh the state of the heap allocator if a certain number of
+	// malloc's has been served already
+	// TODO Define 50 as a constant
+	if(heap->dmm_stats.num_malloc % 50) {
+		state_refresh(heap);
+	}
+
 	posix_unlock(heap);
 	return ptr;
 }
+

+ 53 - 0
dmm_adaptor.c

@@ -0,0 +1,53 @@
+#include "dmm_adaptor.h"
+#include "heap.h"
+
+void state_refresh(heap_t *heap) {
+	float fragmentation;
+	knob_state_t state;
+
+	fragmentation = (float) heap->dmm_stats.mem_allocated / 
+		(float)	heap->dmm_stats.mem_requested - 1.0;
+
+	// TODO Constant for the threshold, contraints for the memory threshold
+	if(fragmentation <= 0.05) {
+		state = 0;
+		set_fragmentation_params(heap, state);
+	}
+}
+
+void set_fragmentation_params(heap_t *heap, knob_state_t state) {
+	switch(state) {
+		default :
+			heap->dmm_knobs.max_coalesce_size = -1;
+			heap->dmm_knobs.min_split_size = 0;
+			heap->dmm_knobs.empty_threshold = 1.5;
+			break;
+		case 1 :
+			heap->dmm_knobs.max_coalesce_size = 20;
+			heap->dmm_knobs.min_split_size = 400;
+			heap->dmm_knobs.empty_threshold = 1200;
+			break;
+		case 2 :
+			heap->dmm_knobs.max_coalesce_size = 40;
+			heap->dmm_knobs.min_split_size = 800;
+			heap->dmm_knobs.empty_threshold = 1000;
+			break;
+		case 3 :
+			heap->dmm_knobs.max_coalesce_size = 60;
+			heap->dmm_knobs.min_split_size = 1200;
+			heap->dmm_knobs.empty_threshold = 800;
+			break;
+		case 4 :
+			heap->dmm_knobs.max_coalesce_size = 80;
+			heap->dmm_knobs.min_split_size = 1600;
+			heap->dmm_knobs.empty_threshold = 600;
+			break;
+		case 5 :
+			heap->dmm_knobs.max_coalesce_size = 100;
+			heap->dmm_knobs.min_split_size = 2000;
+			heap->dmm_knobs.empty_threshold = 300;
+			break;
+	}
+}
+
+

+ 7 - 0
dmm_adaptor.h

@@ -0,0 +1,7 @@
+#include "heap.h"
+
+typedef uint8_t knob_state_t;
+
+void state_refresh(heap_t *heap);
+void set_fragmentation_params(heap_t *heap, knob_state_t state);
+

+ 8 - 0
dmm_init.c

@@ -22,6 +22,14 @@ allocator_t * dmm_init(void) {
 		main_allocator->heaps[i].dmm_stats.live_objects = 0;
 		main_allocator->heaps[i].dmm_stats.num_malloc = 0;
 		main_allocator->heaps[i].dmm_stats.num_free = 0;
+
+		// Knobs initialization
+		main_allocator->heaps[i].dmm_knobs.max_coalesce_size = -1;
+		// FIXME Create a constant for the initial value of the next
+		// variables
+		main_allocator->heaps[i].dmm_knobs.frag_threshold = 1.0;
+		main_allocator->heaps[i].dmm_knobs.mem_threshold = 17000;
+
 		pthread_mutex_init(&main_allocator->heaps[i].mutex, NULL);
 	}
 

+ 12 - 6
heap.h

@@ -10,8 +10,6 @@
 
 /**
  * A structure to represent a maptable node
- *
- * Heaps contain 
  */
 typedef struct maptable_node_s {
 	unsigned int size; /**< the size of the blocks of the fixed list */
@@ -24,6 +22,7 @@ typedef struct dmmstats_s {
 	uint32_t max_mem_requested; /**< maximum total memory requested */
 	uint32_t mem_allocated; /**< total memory currently allocated */
 	uint32_t mem_requested; /**< total memory currently requested */
+	uint32_t mem_reserved; /**< total memory currently reserved */
 	uint32_t live_objects; /**< number of live objects */
 	uint32_t read_mem_accesses; /**< number of read accesses */
 	uint32_t write_mem_accesses; /**< number of write accesses */
@@ -31,11 +30,18 @@ typedef struct dmmstats_s {
 	uint32_t num_free; /**< number of free()'s served */
 } dmmstats_t;
 
+/**
+ * A structure to represent tunable parameters of a heap
+ */
 typedef struct dmmknobs_s {
-	uint32_t maxCoalesceSize;
-	uint32_t minSplitSize;
-	float empty_threshold;
-	uint32_t percentage;
+	int32_t max_coalesce_size; /**< maximum coalesce size; -1 if coalescing
+				      is not supported */
+	float frag_threshold; /**< fragmentation threshold to enable
+					 coalescing or not */ 
+	uint32_t mem_threshold; /**< memory size threshold */
+	uint32_t min_split_size; // FIXME to be investigated if needed
+	float empty_threshold; // FIXME to be investigated if needed
+	uint32_t percentage; // FIXME to be investigated if needed
 	char frag_state; //FIXME It was in the old code to refresh the frag check
 } dmmknobs_t;