-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.c
233 lines (217 loc) · 5.09 KB
/
policy.c
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* INSTRUCTIONS:
This file contains all functions related with various policies.
Each policy returns the hit rate
*/
#include "definitions.h"
#include <stdio.h>
#include <stdlib.h>
// helper function to check a hit or miss
int hit(int page_no, int arr[], int n, int* index){
int j = 0;
// printf("%d\n", page_no);
for(j=0;j<n;j++){
if(arr[j]==page_no){
*index = j;
// it is a hit so return 1
return 1;
}
}
// It is a miss so return 0
*index = 0;
return 0;
}
// FIFO
float policy_FIFO(struct workload * w, int cache_size)
{
float hit_rate;
int i;
// creating array for cache
int cache[cache_size];
// initializing all values in cache to -1
for(i=0;i<cache_size;i++){
cache[i] = -1;
}
int *p = w->work;
float wsize = w->size; // workload size
i = 0;
float faults = 0; // number of faults
int m;
int available; // variable to store a hit or miss
int index;
// iterate over the workload
while(i<wsize){
// printf("%d\n", *p);
available = hit(*p, cache, cache_size, &index);
// if it is a miss
if(available==0){
// shift the pages in cache to append the latest page
for(m=0; m<cache_size-1; m++){
cache[m] = cache[m+1];
}
cache[m] = *p;
// printf("%d\n",cache[m]);
faults++;
}
i++;
p++; // increment the workload pointer
}
hit_rate = (wsize - faults)/wsize;
// printf("%f\n", hit_rate);
// printf("FIFO ended\n");
return hit_rate;
}
float policy_LRU(struct workload * w, int cache_size)
{
float hit_rate = 0;
// creating array for cache
int cache[cache_size];
// initializing all values in cache to -1
int i;
for(i=0;i<cache_size;i++){
cache[i] = -1;
}
int *p = w->work;
float wsize = w->size;
i = 0;
float faults = 0;
int n = cache_size;
int j = 0;
int index = 0;
int available;
int unique_pages = w->pages;
// iterating over the workload
while(i<wsize){
// printf("%d\n", *p);
available = hit(*p, cache, n, &index);
// printf("%d\n", available);
// it is a hit
if (available==1){
// just make the current page at the top of the queue
for(j=index;j<n-1;j++){
cache[j] = cache[j+1];
}
cache[j] = *p;
}
else{
// it is a miss then pop the last one which was least frequent
for(j=0;j<n-1;j++){
cache[j] = cache[j+1];
}
cache[j] = *p;
// printf("%d\n", cache[j]);
faults++;
}
i++;
p++; // increment the workload pointer
}
// printf("%f\n", faults);
// printf("%f\n", wsize);
hit_rate = (wsize - faults)/wsize;
return hit_rate;
}
// LRU Approx (second chance)
float policy_LRUapprox(struct workload * w, int cache_size)
{
float hit_rate = 0;
// creating array for cache
int cache[cache_size];
// initializing all values in cache to -1
int i = 0;
for(i=0;i<cache_size;i++){
cache[i] = -1;
}
int *p = w->work;
float wsize = w->size;
float faults = 0;
int n;
int unique_pages = w->pages;
n = cache_size;
// define an array use_bits to keep track of which one was used
int use_bits[w->pages];
for(i=0; i<w->pages; i++){
use_bits[i]=0;
}
i = 0;
int index = 0;
int available;
int page_replc = 0; // which page index will be replaced
while(i<wsize){
available = hit(*p, cache, n, &index);
if (available==0){
// it is a miss
faults+=1; // increment the fault count
if(use_bits[page_replc]==1){
// a do - while loop to check for the use_bit which is 0
do{
use_bits[page_replc] = 0;
page_replc+=1;
if(page_replc==n){
page_replc = 0;
}
}
while(use_bits[page_replc]!=0);
}
// after getting the index to be replace replace it in the cache
if(use_bits[page_replc]==0){
cache[page_replc] = *p;
use_bits[page_replc] = 1;
page_replc+=1;
}
}
else{
// it is a hit so make the use bit = 1
if(use_bits[index]==0){
use_bits[index] = 1;
}
}
// if page_replc variable is at last of the array make it 0
if(page_replc==n){
page_replc = 0;
}
p++; // increment the workload
i++;
}
// printf("faults in approx %d\n",faults);
// printf("faults in approx %d\n",wsize);
hit_rate = (wsize - faults)/wsize;
return hit_rate;
}
// Random policy
float policy_RANDOM(struct workload * w, int cache_size)
{
float hit_rate = 0;
// creating array for cache
int cache[cache_size];
// initializing all values in cache to -1
int i = 0;
for(i=0;i<cache_size;i++){
cache[i] = -1;
}
int *p = w->work;
float wsize = w->size;
float faults = 0;
int n;
// iterate over the workload
for(i=0;i<wsize;i++){
n = cache_size;
int j = 0;
int index;
int available;
available = hit(*p, cache, n, &index);
if(available==0){
// it is a miss
int index;
// choose a random index and pop it out
index = (rand() % (n-1 - 0 + 1)) + 0;
for(j=index;j<n-1;j++){
cache[j] = cache[j+1];
}
// put the current page at the top of the queue
cache[j] = *p;
faults++;
}
p++; // increment the workload pointer
}
hit_rate = (wsize - faults)/wsize;
return hit_rate;
}