-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.c
More file actions
183 lines (142 loc) · 3.81 KB
/
simulator.c
File metadata and controls
183 lines (142 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "simulator.h"
int table_size;
int window_size;
int page_size;
int current_window_usage;
int * current_window;
int number_of_pages = 0;
int working_set_total = 0;
int number_of_windows = 0;
void recordOperation(unsigned int address){
int pageNumber = address / page_size;
insertIntoWindowArray(current_window, pageNumber, window_size);
if(current_window_usage == 1){
current_window_usage = window_size;
working_set_total += number_of_pages;
number_of_windows++;
printf("Window %d had size of %d\n", number_of_windows, number_of_pages);
//Reset the array and reset the number of pages used to 0
memset(current_window, -1, sizeof(int) * window_size);
number_of_pages = 0;
}
else{
current_window_usage--;
}
}
linked_list* ll_insert(linked_list* head, linked_list* new){
new->next=head;
if(head != NULL)
head->previous=new;
head=new;
return head;
}
linked_list* ll_delete(linked_list* head, linked_list* item){
if(item->previous != NULL)
item->previous->next=item->next;
if(item->next!=NULL)
item->next->previous=item->previous;
if(item==head)
head=item->next;
return head ;
}
linked_list* ll_search(linked_list* head, unsigned int key){
for(;head!=NULL; head=head->next)
{
if(head->key==key)
return head;
}
return NULL;
}
linked_list* ll_new(int key, int data){
linked_list* new =(linked_list *) malloc(sizeof(linked_list));
new->key = key;
new->data = data;
new->next = NULL;
new->previous = NULL;
return new;
}
void printStatistics(){
double average_working_set_size = (double)working_set_total/number_of_windows;
printf("Average working set size over time of execution: %lf\n", average_working_set_size);
}
void printArray(){
//Start of array
int i;
printf("Starting Array: ");
for(i = 0; i < window_size; i++){
int temp = current_window[i];
printf("%d ", temp);
}
printf(" Ending Array\n");
}
//Tries to insert that page number into current list if its not already in there
// If it is in there it returns
void insertIntoWindowArray(int* array, int number, int size){
int i;
for(i = 0; i < size; i++){
int temp = array[i];
//If it is unitialized insert our new element in there
if(temp == -1){
array[i] = number;
number_of_pages++;
return;
}
if (number == temp){
return;
}
}
printf("Array is full that you are inserting into. This should not happen.\n");
}
void done(){
printf("Got to done.\n");
int i;
printStatistics();
for(i=0; i<table_size; i++){
linked_list* head = table[i];
//Clean up all linked list elements
freeLinkedList(head);
}
free(table);
free(current_window);
}
void freeLinkedList(linked_list* head){
while(head != NULL){
linked_list* temp = head;
head = head->next;
free(temp);
}
}
void init(int psize,int winsize){
page_size=psize;
window_size = winsize;
current_window_usage = winsize;
current_window = (int *)malloc(sizeof(int)* winsize);
memset(current_window, -1, sizeof(int) * window_size);
}
void setTableSize(int numOfEntries){
//Simple formula for determining a decent sized table
table_size = numOfEntries/2 + 1;
table = (linked_list **)calloc(table_size, sizeof(linked_list*));
}
//Insert it if it is not in there and swap values if it is
void ht_insert(linked_list** table, int size, linked_list* item){
//Key we are searching for
int key=item->key;
table[key%size] = ll_insert(table[key%size],item);
}
void ht_delete(linked_list** table, int size, linked_list* item){
//log
int key= item->key;
table[key%size]=ll_delete(table[key%size],item);
}
void put(unsigned int address, int value){
linked_list* item = ll_new(address, value);
ht_insert(table, table_size, item);
recordOperation(address);
}
int get(unsigned int address){
linked_list* head = table[address%table_size];
linked_list* list = ll_search(head,address);
recordOperation(address);
return list->data;
}