-
Notifications
You must be signed in to change notification settings - Fork 0
/
segLRUSim.m
272 lines (236 loc) · 9.88 KB
/
segLRUSim.m
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
function out=segLRUSim(trace_file, avg_chunk, cache_prop, aPT, stupd, ...
kmin, avg_len, stepNum, size_as_id, timestamp, requested_file, file_sizes, obj_ids)
% This script takes trace data as input and outputs performance metrics
% glRU cache replacement policy
% Trace_file: filename where trace data is tored
% chunksize: size in MB of each chunk
% cache_prop: size of cache in proportion of total number of chunks
% aPT: average processing time of each video chunk as a proportion of
% chunk lenth
% avg_len: average length of each file (in seconds)
% stupd: Start up delay for watching a video file
% kmin: proportion of cache devoted to initial chunks
%% Input Parameters
if ~exist('timestamp')
load(trace_file)
end
if size_as_id
obj_ids = double(unique(file_sizes));
requested_file = file_sizes;
file_sizes = obj_ids;
end
num_row = length(requested_file);
chunksize = mean(obj_ids) / avg_chunk;
sizes = ceil(obj_ids/chunksize); % Number of chunks in each file
clen = avg_len / mean(file_sizes/chunksize);% Length of each video chunk
cachesize = ceil(sum(file_sizes/chunksize) * cache_prop);
num_obj = length(obj_ids);
id_to_ind = containers.Map(obj_ids, 1:num_obj);
biggest = max(sizes); % Compute size of largest objects
aProcTime = clen * aPT;
smallcachesize = round(kmin * cachesize);
bigcachesize = cachesize - smallcachesize;
% Compute number and size of segments
maxnumsegs = ceil(log2(double(biggest)))+1;
sigsizes = 1;
kmineff = [1, 1];
below_thresh = 0;
for i = 1:maxnumsegs
sigsizes = [sigsizes, 2^(i-1)];
bigger = sizes > 2^i;
below_thresh = below_thresh + sum(bigger) * 2^(i-1);
if below_thresh < .25 * sum(sizes)
kmineff(1) = i+1;
kmineff(2) = 2^i;
end
end
% Compute numerber of segments for each file
allsizes = containers.Map(obj_ids, sizes);
sizess = containers.Map(obj_ids, min(kmineff(2), sizes)); % Number of small chunks for each file
sizesb = containers.Map(obj_ids, max(0, sizes-kmineff(2))); % Number of big chunks for each file
%% Complete Simulation
disp('Entering segLRU Simulation')
smallcache = zeros(1,smallcachesize);
newsmallcache = zeros(1, smallcachesize);
queue_time = 0; % Time until all object in queue are processed
numins = containers.Map(obj_ids,zeros(1,num_obj));
numinb = containers.Map(obj_ids,zeros(1, num_obj));
numsegin = zeros(1, num_obj);
lastreq = zeros(1, num_obj);
fintime = zeros(1, num_obj);
totalins = 0;
totalinb = 0;
hits = zeros(1,stepNum);
hitss = zeros(1,stepNum);
miss = zeros(1,stepNum);
wait = zeros(1,stepNum);
delay = zeros(1,stepNum);
sel_sizes = zeros(1,stepNum);
out = zeros(1,6);
for k = 1:2
hitsum =0;
hitssum =0;
sizesum = 0;
numhitsum = 0;
waitsum = 0;
delaysum = 0;
numdelaysum = 0;
time = 0;
queue_time = 0;
% numhitsd = 0;
% waitsd = 0;
% delaysd = 0;
% numdelaysd =0;
for j = 1:num_row
%Check if space overflow
i = mod(j,stepNum);
if i == 0
i = stepNum;
end
%Advance time
if j > 1
time_step = (timestamp(j)-timestamp(j-1))*10^(-7);
else
time_step = timestamp(1)*10^(-7);
end
queue_time = max(0, queue_time - time_step);
time = time + time_step;
%Extract index of selected object; note that we take the file ID to
%be the file size
chosen = requested_file(j);
%Collect data
numin = numins(chosen) + numinb(chosen);
hitss(i) = numins(chosen);
hits(i) = numin;
notins = sizess(chosen)-numins(chosen);
miss(i) = allsizes(chosen)- hits(i);
sel_sizes(i) = allsizes(chosen);
proctimes = exprnd((aProcTime), 1, miss(i));
delay(i) = sum(max([(cumsum(proctimes) + queue_time) ...
- ([double(numin:(allsizes(chosen)-1))] .* clen + stupd) 0]));
queue_time = queue_time + sum(proctimes);
if hits(i) == allsizes(chosen)
wait(i) = 0;
else
wait(i) = queue_time;
end
fintime(id_to_ind(chosen)) = time + delay(i) + clen * allsizes(chosen);
% First handle smaller, pure LRU stack
% Move all cached segments to front of stack
newsmallcache(1:numins(chosen)) = chosen;
ind = smallcache == chosen;
newsmallcache(numins(chosen)+1:end) = smallcache(~ind);
% Add any additional chunks if necessary and account for those
% which fall out the end
if notins > 0
% Compute number of empty spots in queue and number to evict
emptyspots = smallcachesize - totalins;
numtoevict = max(notins - emptyspots, 0);
totalins = min(totalins + notins, smallcachesize);
% Evict necessary cunks
if numtoevict > 0
[evicted, ~ ,ic] = unique(newsmallcache(end-numtoevict-emptyspots+1:end-emptyspots));
ev_counts = accumarray(ic,1);
ev_counts = ev_counts(evicted>0);
evicted = evicted(evicted>0);
for k = 1:length(evicted)
numins(evicted(k)) = numins(evicted(k)) - ev_counts(k);
end
end
newsmallcache(notins+1:end) = newsmallcache(1:end-notins);
newsmallcache(1:notins) = chosen;
numins(chosen) = numins(chosen)+notins;
end
% Now handle larger queue
if lastreq(id_to_ind(chosen)) > 0 && numins(chosen) == sizess(chosen)
% only iterate through segments if the previous one has been
% added
added = 1;
while added == 1 && sizesb(chosen) > numinb(chosen)
% iterate until enough replacements are evicted to provide
% space for new
newspace = bigcachesize - totalinb;
if newspace < sigsizes(numsegin(id_to_ind(chosen))+1)
space = 0;
else
space = 1;
end
nocand = 0;
while space == 0 && nocand == 0
%only want to focus on chunks not currently playing
notplaying = time > fintime;
anyin = numsegin > 0;
% find file with lowest replacment value
scores = 1./(time - lastreq.*numsegin)...
.*notplaying.*anyin;
[canmin, tempcan] = min(scores(scores>0));
% check if lower than requested segment
if canmin < 1/(time - lastreq(id_to_ind(chosen))*numsegin(id_to_ind(chosen)))
can = find(scores, tempcan);
% compute space freed up
newspace = newspace + sigsizes(numsegin(can(1)));
if newspace > sigsizes(numsegin(id_to_ind(chosen))+1)
space = 1;
end
numsegin(can) = numsegin(can) - 1;
else
nocand = 1;
end
end
% If enough space is created then add new chunk
if space == 1
numsegin(id_to_ind(chosen)) = numsegin(id_to_ind(chosen)) + 1;
numinb(chosen) = min(numinb(chosen) + sigsizes(numsegin(id_to_ind(chosen))), sizesb(chosen));
totalinb = totalinb + sigsizes(numsegin(id_to_ind(chosen)));
else
added = 0;
end
end
end
lastreq(id_to_ind(chosen)) = time;
smallcache = newsmallcache;
%Calculate Stats
if mod(j,stepNum) == 0 || j == num_row
j;
hitsum = hitsum + sum(hits);
hitssum = hitssum + sum(hitss);
sizesum = sizesum + sum(sel_sizes);
numhitsum = numhitsum + sum(hits == 0);
waitsum = waitsum + sum(wait);
delaysum = delaysum + sum(delay);
numdelaysum = numdelaysum + sum(delay >0);
out(1) = hitsum/sizesum;
out(2) = numhitsum/j;
out(3) = waitsum/j;
out(4) = delaysum/j;
out(5) = numdelaysum/j;
out(6) = hitssum/sizesum;
% numhitsd = sqrt(((j-stepNum-1)*numhitsd^2 + (stepNum-1)*std(hits==0)^2)/(j-1))
% waitsd = sqrt(((j-stepNum-1)*waitsd^2 + (stepNum-1)*std(wait)^2)/(j-1))
% delaysd = sqrt(((j-stepNum-1)*delaysd^2 + (stepNum-1)*std(delay)^2)/(j-1))
% numdelaysd = sqrt(((j-stepNum-1)*numdelaysd^2 + (stepNum-1)*std(delay>0)^2)/(j-1))
hits = zeros(1,stepNum);
hitss = zeros(1,stepNum);
miss = zeros(1,stepNum);
wait = zeros(1,stepNum);
delay = zeros(1,stepNum);
sel_sizes = zeros(1,stepNum);
end
j = j+1;
end
end
j = j-1;
disp('Proportion from Cache')
out(1)
disp('Prop Entire Miss')
out(2)
disp('Mean Wait Time')
out(3)
disp('Mean Delay')
out(4)
disp('Prop Delayed')
out(5)
disp('Proportion from Small Cache')
out(6)
% save('seglRUout.mat', 'out')
end