Commit 018912de authored by Son Pham's avatar Son Pham

Lab 9.3 completed

parent 594539a1
......@@ -11,7 +11,7 @@ BIN = ./bin
vpath %.h ./include
vpath %.c ./src
EXECS = dlisttest frag-eval
EXECS = dlisttest frag-eval simulate
all: $(EXECS)
......@@ -33,6 +33,9 @@ dlisttest: $(SRC)/dlisttest.c $(OBJ)/dnode.o $(OBJ)/dlist.o
frag-eval: $(SRC)/frag-eval.c $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/frag-eval.c -o $(BIN)/frag-eval
simulate: $(SRC)/simulate.c $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o
$(CC) $(CFLAGS) $(OBJ)/allocator.o $(OBJ)/dnode.o $(OBJ)/dlist.o $(SRC)/simulate.c -o $(BIN)/simulate
.PHONY: clean
clean:
/bin/rm -rf $(BIN)/* $(OBJ)/* core* *~
......
......@@ -55,3 +55,28 @@ while (r< requests):
if (p[r] != NULL) deallocate(p[r]);
average_frag();
[3.1] I wrote the program simulate.c to automate the whole process.
For each policy first-fit, best-fit, worst-fit, I ran 1000 trials.
I do 1000 runs and gather the average fragmentation of each policy.
R = 1000
num_requests = 50
total_size = 10240
confidence level = 95%
Here are the resutls
FIRST FIT BEST FIT WORST FIT
Point estimate 532.67262639 534.359251622 531.727357461
Standard deviation 56.7256590679 58.1002574132 59.3634876871
Confidence interval 0.1125131144 0.1152395762 0.1177451438
Results are exported in exp-results.pdf
[3.2] Judging from the results, we can see that no policy has significant advantage over the other. WORST-FIT has the lowest fragmentation average but highest confidence interval, while FIRST-FIT has the lowest standard deviation
WORST-FIT seems to be the BEST policy for my simulation because most of my memory allocation has roughly variable size, since worst-fit always go for the worst memory allocation, the size of each fragmentation will be averaged out, and leaves the most room for other memory chunks to fit it.
BEST-FIT might not works best in this case because it will leave a lot of small memory residuals which won't be able to fit big chunks of data.
Overall however, the difference between three policies is not signifcant. Therefore, a better simulation might be needed to better compare the three policies.
No preview for this file type
This diff is collapsed.
......@@ -9,8 +9,8 @@
#define _ALLOCATOR_H_
#define FIRST_FIT 0
#define WORST_FIT 1
#define BEST_FIT 2
#define BEST_FIT 1
#define WORST_FIT 2
/* Necessary information about the doubly linked list */
struct dlist* free_list;
......@@ -71,4 +71,15 @@ int deallocate(void* ptr);
*/
double average_frag();
/**
* Run a flag evaluation and returns all the value
* @return: point estimate value
*/
double frag_eval(int seed, int num_requests, int policy);
/**
* Generate a random number between begin and end
*/
int random_number(int begin, int end);
#endif /* _ALLOCATOR_H_ */
......@@ -235,3 +235,52 @@ double average_frag() {
*/
return (double) total_size / (double) num_frags;
}
/**
* Run a flag evaluation and returns all the value
* @return: point estimate value
*/
double frag_eval(int seed, int num_requests, int policy) {
/** Allocate 10K of memory */
allocator_init(10240);
/** Apply the algorithm */
srand(seed);
int r = 0;
void **p[num_requests];
int s = 0;
/** Allocate all at once */
while (r < num_requests) {
s = random_number(100,1000);
p[r] = allocate(policy, s);
r++;
}
/** Deallocate all at once */
r = 0;
while (r < num_requests) {
if (p[r] != NULL) deallocate(p[r]);
r++;
}
/** Take fragmentation */
return average_frag();
}
/**
* Generate a random number between begin and end
*/
int random_number(int begin, int end) {
/* Calculate the fraction */
double fraction = (double) rand() / RAND_MAX;
/* Get the random number */
int range = end - begin;
int rand = begin + (int) (fraction * range + 0.5);
/* Return the random number */
return rand;
}
......@@ -13,7 +13,6 @@
#include "allocator.h"
int print_both_list();
int random_number(int begin, int end);
/**
* Main program that test the functionality of allocator
......@@ -66,16 +65,3 @@ int print_both_list() {
dlist_print(allocate_list);
return 0;
}
int random_number(int begin, int end) {
/* Calculate the fraction */
double fraction = (double) rand() / RAND_MAX;
/* Get the random number */
int range = end - begin;
int rand = begin + (int) (fraction * range + 0.5);
/* Return the random number */
return rand;
};
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* Measure Point Estimate and Confidence Interval
*/
#include <stdlib.h>
#include <stdio.h>
#include "dnode.h"
#include "dlist.h"
#include "allocator.h"
#define NUM_SAMPLES 1000
#define NUM_REQUESTS 50 /* Big enough to make the fragmentation saturate */
int main() {
double first_fit[NUM_SAMPLES];
double best_fit[NUM_SAMPLES];
double worst_fit[NUM_SAMPLES];
int seed = 0;
/* Evaluate first-fit*/
policy = FIRST_FIT;
int i = 0;
for (i = 0; i < NUM_SAMPLES; i++) {
first_fit[i] = frag_eval(seed, NUM_REQUESTS, policy);
seed++;
}
/* Evaluate best-fit*/
policy = BEST_FIT;
for (i = 0; i < NUM_SAMPLES; i++) {
best_fit[i] = frag_eval(seed, NUM_REQUESTS, policy);
seed++;
}
/* Evaluate worst-fit */
policy = WORST_FIT;
for (i = 0; i < NUM_SAMPLES; i++) {
worst_fit[i] = frag_eval(seed, NUM_REQUESTS, policy);
seed++;
}
/* Write the results to a file */
FILE *f = fopen("data.txt", "w");
if (f == NULL) {
printf("Error opening file!\n");
exit(1);
}
/* Print some text */
for (i = 0; i < NUM_SAMPLES; i++) {
fprintf(f, "%lf %lf %lf\n", first_fit[i], best_fit[i], worst_fit[i]);
}
/* Close the file to grab the data */
fclose(f);
return 0;
}
/**
* Name: Son Pham
* Class: CSCI 315
* Prof: Luiz Felipe Perrone
* Measure Point Estimate and Confidence Interval
*/
#include <stdlib.h>
#include <stdio.h>
#include "dnode.h"
#include "dlist.h"
#include "allocator.h"
#define NUM_SAMPLES 1000
#define NUM_REQUESTS 1000 /* Big enough to make the fragmentation saturate */
int main() {
double first_fit[NUM_SAMPLES];
double best_fit[NUM_SAMPLES];
double worst_fit[NUM_SAMPLES];
/* Evaluate first-fit*/
policy = FIRST_FIT;
int i = 0;
for (i = 0; i < NUM_SAMPLES; i++) {
first_fit[i] = frag_eval(i, NUM_REQUESTS, policy);
}
/* Evaluate best-fit*/
policy = BEST_FIT;
for (i = 0; i < NUM_SAMPLES; i++) {
best_fit[i] = frag_eval(i, NUM_REQUESTS, policy);
}
/* Evaluate worst-fit */
policy = WORST_FIT;
for (i = 0; i < NUM_SAMPLES; i++) {
worst_fit[i] = frag_eval(i, NUM_REQUESTS, policy);
}
/* Write the results to a file */
FILE *f = fopen("data.txt", "w");
if (f == NULL) {
printf("Error opening file!\n");
exit(1);
}
/* Print some text */
for (i = 0; i < NUM_SAMPLES; i++) {
fprintf(f, "%lf %lf %lf\n", first_fit[i], best_fit[i], worst_fit[i]);
}
/* Close the file to grab the data */
fclose(f);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment