This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.c
396 lines (316 loc) · 8.7 KB
/
main_test.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
Welcome to our project!
The goal of our project: implementing one of the caching strategies - ARC
ARC - Adaptive Replacement Cache
You can read more in this article:
http://theory.stanford.edu/~megiddo/pdf/IEEE_COMPUTER_0404.pdf
Authors of the project: 1st year students of FRTK:
Alexander Latikov
Sergey Koshelev
Zaitsev Vasilii
Development Date: May 2020
A brief overview of the files included in our project:
dl_list.h and dl_list.c - functions and auxiliary structures for the "List" structure
dl_list_test.c - Testing system for a List
dl_cells.c - functions and auxiliary structures for the "List cell" structure
ARC.c and ARC.h - Main ARC Cache Emulation Algorithm
pages.h - Header with constants and basic structures
main_test.c - Testing system, user interface, data output for analysis <You are here>
ARC_readme.txt - more about what is happening in ARC
dl_list_readme.txt - more on how to use the dl_list functions
colors.h - Header with materials for changing the color of text in the console
LICENSE - GNU GENERAL PUBLIC LICENSE
*/
#define _CRT_SECURE_NO_WARNINGS
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stddef.h>
#include "dl_list.h"
#include "pages.h"
#include "ARC.h"
#include "colors.h"
#include "conditions.h"
size_t cachesize = 0; ///Size of cache
size_t req_size = 0; ///Size of requests in contest mode
//Creating main memory of MEM_SIZE
struct page_t* create_fill_mem (size_t size)
{
srand((unsigned int)time(NULL));
struct page_t* mem = (struct page_t*) calloc (size, sizeof(struct page_t));
assert(mem && "CreateFillMem");
for(unsigned int i = 0; i < size; i++)
{
mem[i].index = i;
for (int j = 0; j < DATA_SIZE; j++)
mem[i].data[j] = rand() % 127;
}
return mem;
}
//Creating cache_memory of CACHE_SIZE
struct cache_t* create_fill_cache (size_t size)
{
struct cache_t* cache_mem = (struct cache_t*) calloc (size, sizeof(struct cache_t));
assert(cache_mem && "CreateFillCache");
return cache_mem;
}
//Removing main memory
void remove_mem(struct page_t* mem)
{
free(mem);
mem = NULL;
}
//Removing cache
void remove_cache(struct cache_t* cache_mem)
{
free(cache_mem);
cache_mem = NULL;
}
//Finding page in memory
struct page_t* find_page(long long int number, struct page_t* mem)
{
assert(mem && "Find_P");
for(int i = 0; i < MEM_SIZE; i++)
{
if (mem[i].index == number)
return mem + i;
}
exit(404);
}
//Printing memory
void print_mem(struct page_t* mem)
{
assert(mem && "Print");
for(int i = 0; i < MEM_SIZE; i++)
{
print_page(mem + i);
}
}
//Printing requested page
void print_page(struct page_t* target)
{
assert(target && "Print");
printf("\n");
color_on(BLUE);
printf("################################");
color_off();
printf("\n");
printf("Page requested!\n");
printf("Index: %lld\n", target -> index);
printf("Data: ");
for (int j = 0; j < DATA_SIZE; j++)
{
printf("%c", target -> data[j]);
}
printf("\n");
color_on(BLUE);
printf("################################");
color_off();
printf("\n");
}
//Getting page, bypassing the cache, directly from memory
void slow_get_page (struct page_t* target, struct page_t* mem, long long int number)
{
assert(mem && "Get_P");
assert(target && "Get_P");
struct page_t* page = find_page(number, mem);
memcpy(target, page, sizeof(struct page_t));
#ifdef DELAY
my_delay(MEM_DELAY);
#endif
}
///CLEAR EVERYTHING!!!
void clear_everything( struct page_t* mem,
struct cache_t* cache_mem,
struct list_t* T1,
struct list_t* T2,
struct list_t* B1,
struct list_t* B2)
{
assert((mem != NULL) && (cache_mem!= NULL) && (T1!= NULL) && (T2!= NULL) && (B1!= NULL) && (B2!= NULL) && "Clearing");
remove_mem(mem);
remove_cache(cache_mem);
destroy_list(T1);
destroy_list(T2);
destroy_list(B1);
destroy_list(B2);
}
///Toggle console-color on
void color_on(const char* str)
{
printf("%s\n", str);
}
///Toggle console-color off
void color_off()
{
printf(RES);
}
//Testing function that requests page with random index
void request( int mode,
int size,
unsigned long long * p,struct list_t* T1,
struct list_t* T2, struct list_t* B1,
struct list_t* B2,
struct page_t* mem,
struct cache_t* cache_mem)
{
assert(((mode == SLOW) || (mode == FAST)) && (mem != NULL) && (cache_mem!= NULL) && (T1!= NULL) && (T2!= NULL) && (B1!= NULL) && (B2!= NULL) && "Request");
srand((unsigned int)time(NULL));
struct page_t* target = (struct page_t*) calloc (1, sizeof(struct page_t));
long long int req_n = 0;
struct cell* buffer = NULL;
if (mode == SLOW)
{
for (int i = 0; i < size; i++)
{
req_n = rand() % MEM_SIZE;
slow_get_page(target, mem, req_n);
#ifdef PRINT
print_page(target);
#endif
}
}
if (mode == FAST)
{
long long int T_hits = 0;
for (int i = 0; i < size; i++)
{
req_n = rand() % MEM_SIZE;
buffer = fast_get_page(p, req_n, T1, T2, B1, B2, mem, cache_mem, &T_hits);
#ifdef PRINT_REQ
print_all_lists(T1, T2, B1, B2);
#endif
assert(buffer != NULL);
assert(cell_cache(buffer) != NULL);
assert(&(cell_cache(buffer)->page)!= NULL);
memcpy(target, &(cell_cache(buffer)->page), sizeof(struct page_t));
#ifdef PRINT
print_page(target);
#endif
}
color_on(MAGENTA);
printf("Number of requests: %d\n", size);
printf("Number of hits: %lld\n", T_hits);
printf("Hit ratio %lg percents\n", (double) T_hits/ (double) size * 100);
color_off();
}
buffer = NULL;
free(target);
}
///Artificial Delay function
void my_delay(int millis)
{
millis *=1000;
clock_t start_time = clock();
while (clock() < start_time + millis)
{}
}
///Testing functions for contest mode
void contest_testing(unsigned long long * p,
struct list_t* T1,
struct list_t* T2,
struct list_t* B1,
struct list_t* B2,
struct page_t* mem,
struct cache_t* cache_mem,
FILE* inp)
{
assert((mem != NULL) && (cache_mem!= NULL) && (T1!= NULL) && (T2!= NULL) && (B1!= NULL) && (B2!= NULL) && (p!= NULL) && (inp!= NULL) && "Contset testing");
long long int page_n;
long long int T_hits = 0;
long long int req_qt = 0;
for (size_t i = 0 ; i < req_size; i++)
{
fscanf(inp, "%lld", &page_n);
fast_get_page(p, page_n, T1, T2, B1, B2, mem, cache_mem, &T_hits);
req_qt++;
}
printf("Number of requests: %lld\n", req_qt);
printf("Number of hits: %lld\n", T_hits);
printf("Hit ratio %lg percents\n", (double) T_hits/ (double) req_qt * 100);
}
///General function
int main()
{
///Initializing the size of the cache
#ifdef CONTEST
fscanf(stdin , "%lu", &cachesize);
fscanf(stdin , "%lu", &req_size);
#endif
#ifndef CONTEST
cachesize = CACHE_SIZE;
#endif
unsigned long long p = 0; //Param for ARC algorithm
///Initializing main structures
struct page_t* mem = create_fill_mem(MEM_SIZE);
struct cache_t* cache_mem = create_fill_cache(cachesize);
struct list_t* T1 = make_list();
struct list_t* T2 = make_list();
struct list_t* B1 = make_list();
struct list_t* B2 = make_list();
assert((mem != NULL) && (cache_mem!= NULL) && (T1!= NULL) && (T2!= NULL) && (B1!= NULL) && (B2!= NULL));
///Initializing parametrs for time-counting functions
#ifdef TIME
long int start_t = 0;
long int end_t = 0;
double slow_t = 0.0;
double fast_t = 0.0;
#endif
#ifdef TIME
start_t = clock();
#endif
///Testing in contest mode
#ifdef CONTEST
contest_testing(&p, T1, T2, B1, B2, mem, cache_mem, stdin);
#endif
///"Canonical" mode of the programm
#ifndef CONTEST
///Slow requesting, without cache
color_on(RED);
printf("Request from memory\n");
color_off();
request(SLOW, REQ_SIZE, &p, T1, T2, B1, B2, mem, cache_mem);
#endif
#ifdef TIME
end_t = clock();
slow_t = ((double) (end_t - start_t))/CLOCKS_PER_SEC;
#endif
#ifdef TIME
start_t = clock();
#endif
#ifndef CONTEST
///Fast requesting, using ARC
color_on(RED);
printf("Request from cache\n");
color_off();
request(FAST, REQ_SIZE, &p, T1, T2, B1, B2, mem, cache_mem);
#endif
///Time-counting functions
#ifdef TIME
end_t = clock();
color_on(RED);
fast_t = ((double) (end_t - start_t))/CLOCKS_PER_SEC;
printf("Time spent on slow request: %lg seconds\n", slow_t);
printf("Time spent on fast request: %lg seconds\n", fast_t);
color_off();
#endif
///Wanna see some statistics????
#ifdef STATS
color_on(GREEN);
printf("**********************\n");
printf("STATS:\n");
printf("Memory size = %d\n", MEM_SIZE);
printf("Cache size = %lu\n", cachesize);
printf("Memory delay = %d millis\n", MEM_DELAY);
printf("Cache delay = %d millis\n", CACHE_DELAY);
printf("Number of requests = %d\n", REQ_SIZE);
printf("**********************\n");
printf("\n");
color_off();
#endif
///Always clear the memory after yourself!
clear_everything(mem, cache_mem, T1, T2, B1, B2);
return 0;
}