-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
535 lines (467 loc) · 17.4 KB
/
main.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* Project #3: Cache or Not to Cache by Kongmanee, Jaturong */
#include <csim.h>
#include <stdio.h>
/* #define is used to define CONSTANT */
#define SIM_TIME 50000.0
#define NUM_CLIENTS 3l
#define NUM_SERVER 1l
#define DB_SIZE 1000
#define HOT_DATA_ITEM_LIMIT 49
#define COLD_DATA_ITEM_START 50
#define CACHE_SIZE 100
#define BROADCAST_INTERVAL 20
#define T_UPDATE 10
#define T_QUERY 100
#define HOT_DATA_UPDATE_PROB 0.33
#define HOT_DATA_ACCESS_PROB 0.8
struct nde {
MBOX mbox;
};
struct nde node[NUM_CLIENTS + NUM_SERVER];
/* structure for server side */
struct data_items {
long id;
TIME last_updated_time;
};
struct data_items database[DB_SIZE];
struct ir_msg {
long id;
TIME last_updated_time;
long ir_size;
int ir_status;
};
struct ir_msg *ir;
struct ir_msg *data_item_msg;
long ir_temp[100];
long l_bcast[100];
/* structure for client side */
struct cache_items {
int valid;
long id;
TIME last_updated_time;
TIME last_accessed_time;
};
/* Since we know exactly the size of cache,
and we randomly read/access data items so many time.
It's good to use array since its reading time complexity is O(1)*/
struct cache_items cache_size[NUM_CLIENTS + 1][CACHE_SIZE];
struct request {
long item_id;
};
struct request *q;
void init();
/* server side and its function */
void server();
void update_data_items();
void invalidation_report();
void receive_request();
long ir_counter;
long query_counter;
long ir_msg_size;
long bcast_list_size;
/* client side and its function*/
void client();
void generate_query();
void receive_ir();
/* utility function */
int is_cached();
int is_duplicated();
int get_list_size();
void clear_list();
int is_cache_full();
int get_oldest_invalid();
int get_oldest_valid();
/* measurement variable */
long cache_hit;
long cache_miss;
long T_update;
long T_query;
long query_count;
long num_query_per_interval;
TIME q_gen;
TIME q_delay;
void sim() {
printf("Enter Mean update arrival time (T_update) in seconds:\n");
scanf("%ld", &T_update);
printf("You've entered T_update: %ld\n", T_update);
printf("Enter Mean query generate time (T_query) in seconds:\n");
scanf("%ld", &T_query);
printf("You've entered T_query: %ld\n", T_query);
create("sim");
init();
hold(SIM_TIME);
printf("Enter Mean update arrival time (T_update) in seconds: %ld\n", T_update);
printf("Enter Mean query generate time (T_query) in seconds: %ld\n", T_query);
printf("#Cache hit: %ld ------ #Cache miss: %ld\n", cache_hit, cache_miss);
printf("#Cache hit ratio %.2f\n", cache_hit/(float)(cache_hit + cache_miss));
printf("Query delay (seconds): %12.3f\n", (q_delay/(float)NUM_CLIENTS)/(SIM_TIME/BROADCAST_INTERVAL));
printf("#Queries served per interval: raw %ld, avg: %3.5f\n", num_query_per_interval, num_query_per_interval/((SIM_TIME)/BROADCAST_INTERVAL));
}
void init() {
long i;
char str[24];
max_events(NUM_CLIENTS * NUM_CLIENTS * 100 + NUM_CLIENTS);
max_mailboxes(NUM_CLIENTS * NUM_CLIENTS * 100 + NUM_CLIENTS);
max_messages(NUM_CLIENTS * NUM_CLIENTS * 100 + NUM_CLIENTS);
sprintf(str, "server_mailbox.%d", 0);
node[0].mbox = mailbox(str);
for (i = 1; i <= NUM_CLIENTS; i++) {
sprintf(str, "client_mailbox.%d", i);
node[i].mbox = mailbox(str);
}
server(0);
printf("Calling server\n");
for (i = 1; i <= NUM_CLIENTS; i++) {
client(i);
printf("Calling client %ld\n", i);
}
/* allocate memory for query request */
q = (struct request *)malloc(sizeof(struct request) * 5);
}
void server(long n) {
create("server");
printf("Server is generated\n");
/* Set up database */
long i;
for (i = 0; i < DB_SIZE; i++) {
database[i].id = i;
database[i].last_updated_time = clock;
}
printf("Setting up a database ...\n");
update_data_items();
receive_request();
invalidation_report();
while(clock < SIM_TIME){
hold(exponential(1));
}
}
void invalidation_report() {
create("ir");
ir = NULL;
data_item_msg = NULL;
printf("Creating IR ...\n");
while(clock < SIM_TIME){
hold(BROADCAST_INTERVAL);
/* Broadcast: send the IR into all clients' mailboxes */
/* Create a new IR */
if (ir != NULL) {
free(ir);
ir = NULL;
printf("Deallocating IR's memory ... \n");
}
/* check size of ir_temp */
ir_msg_size = get_list_size(ir_temp, 0);
if (ir_msg_size > 0) {
ir = (struct ir_msg *)malloc(sizeof(struct ir_msg) * ir_msg_size);
if (ir == NULL) {
printf("Memory allocation error\n");
}
printf("Creating an IR ... with size %ld at address %ld\n", ir_msg_size, &ir);
long i;
for (i = 0; i < ir_msg_size; i++) {
/* Segmentation fault: ir_temp[i] = 4640444542306902472 for example */
if (ir_temp[i] < 1000) {
ir[i].id = database[ir_temp[i]].id;
ir[i].last_updated_time = database[ir_temp[i]].last_updated_time;
ir[i].ir_size = ir_msg_size;
ir[i].ir_status = 1;
}
}
for (i = 0; i < ir_msg_size; i++) {
printf("Testing IR ... data item id %ld, updated time %6.3f, IR size %ld\n", ir[i].id, ir[i].last_updated_time, ir[i].ir_size);
}
for (i = 1; i <= NUM_CLIENTS; i++) {
send(node[i].mbox, (long)ir);
printf("Broadcasting IR to node %ld \n", i);
}
/* clear ir_temp list */
clear_list(ir_temp);
ir_counter = 0;
printf("IR counter is set to 0\n");
}
/* broadcast L_bcast */
/*if (data_item_msg != NULL) {
free(data_item_msg);
data_item_msg = NULL;
printf("Deallocating DATA ITEM's memory ... \n");
}*/
bcast_list_size = get_list_size(l_bcast, 1);
num_query_per_interval += bcast_list_size;
if (bcast_list_size > 0) {
data_item_msg = (struct ir_msg *)malloc(sizeof(struct ir_msg) * bcast_list_size);
if (data_item_msg == NULL) {
printf("Memory allocation error\n");
}
printf("Creating an L_bcast ... with size %ld at address %ld\n", bcast_list_size, &data_item_msg);
long i;
for (i = 0; i < bcast_list_size; i++) {
if (l_bcast[i] < 1000) {
data_item_msg[i].id = database[l_bcast[i]].id;
data_item_msg[i].last_updated_time = database[l_bcast[i]].last_updated_time;
data_item_msg[i].ir_size = bcast_list_size;
data_item_msg[i].ir_status = 0;
}
}
for (i = 1; i <= NUM_CLIENTS; i++) {
send(node[i].mbox, (long)(data_item_msg));
printf("Broadcasting DATA ITEM to node %ld \n", i);
}
for (i = 0; i < bcast_list_size; i++) {
printf("Testing DATA ITEM ... id %ld, updated time %6.3f, IR size %ld\n", data_item_msg[i].id, data_item_msg[i].last_updated_time, data_item_msg[i].ir_size);
}
clear_list(l_bcast);
query_counter = 0;
printf("QUERY counter is set to 0\n");
}
}
}
int get_list_size(long list[], long bit) {
int i;
int counter = 0;
if (bit == 0) {
printf("Getting IR LIST size ... \n");
} else {
printf("Getting L_bcast LIST size ... \n");
}
for (i = 0; i < 100; i++) {
if (list[i] != -1 && list[i] != 0) {
/*printf("IR id %ld\n", ir_temp[i]);*/
counter++;
}
}
return counter;
}
void clear_list(long list[]) {
long i;
for (i = 0; i < 100; i++) {
list[i] = 0;
}
}
void update_data_items() {
create("update");
printf("Updating data items ...\n");
ir_counter = 0;
while(clock < SIM_TIME){
hold(exponential(T_update));
if (uniform(0.0, 1.0) <= 0.33) {
long rand_hot_item = random(0, HOT_DATA_ITEM_LIMIT);
database[rand_hot_item].last_updated_time = clock;
printf("Updating HOT DATA ITEM at index %ld, updated time %6.3f\n", rand_hot_item, database[rand_hot_item].last_updated_time);
ir_temp[ir_counter] = database[rand_hot_item].id;
ir_counter++;
/*printf("Adding updated hot data item id %ld to IR %ld, counter %ld\n", ir_temp[ir_counter-1], database[rand_hot_item].id, ir_counter-1);*/
} else {
long rand_cold_item = random(COLD_DATA_ITEM_START, DB_SIZE);
database[rand_cold_item].last_updated_time = clock;
printf("Updating COLD DATA ITEM at index %ld, updated time %6.3f\n", rand_cold_item, database[rand_cold_item].last_updated_time);
ir_temp[ir_counter] = database[rand_cold_item].id;
ir_counter++;
/*printf("Adding updated cold data item id %ld to IR %ld, counter %ld\n", ir_temp[ir_counter-1], database[rand_cold_item].id, ir_counter-1);*/
}
}
}
void receive_request() {
create("receive_request");
query_counter = 0;
status_mailboxes();
while(clock < SIM_TIME){
hold(exponential(1));
receive(node[0].mbox, (long*)&q);
/*printf("Server is receiving query %ld\n", q->item_id);*/
if (!is_duplicated(l_bcast, q->item_id) && query_counter < 100) {
l_bcast[query_counter] = q->item_id;
printf("Server RECEIVES QUERY request id %ld\n", l_bcast[query_counter]);
query_counter++;
}
}
}
int is_duplicated(long list[], long id) {
int i;
for (i = 0; i < 100; i++) {
if (id == list[i]) {
printf("Id %ld is duplicated\n", id);
return 1;
} else {
printf("Id %ld is NOT duplicated\n", id);
return 0;
}
}
}
void client(long n) {
create("client");
printf("Client %ld is generated\n", n);
/* Set up cache size */
long i;
for (i = 0; i < CACHE_SIZE; i++) {
cache_size[n][i].valid = 0;
cache_size[n][i].id = -1;
cache_size[n][i].last_updated_time = 0;
cache_size[n][i].last_accessed_time = 0;
}
printf("Node %ld, cache address is %ld\n", n, &cache_size[n]);
receive_ir(n);
generate_query(n);
while(clock < SIM_TIME){
hold(exponential(1));
}
}
void generate_query(long n) {
create("query");
while(clock < SIM_TIME) {
hold(exponential(T_query));
if (uniform(0.0, 1.0) <= 0.8) {
long rand_access_hot_item_id = random(0, HOT_DATA_ITEM_LIMIT);
/* check cache */
if (!is_cached(n, rand_access_hot_item_id)) {
/* generate query request */
q->item_id = rand_access_hot_item_id;
send(node[0].mbox, (long)q);
printf("Client %ld is GENERATING QUERY request at HOT data... with id %ld at %6.3f\n", n, rand_access_hot_item_id, clock);
q_gen = clock;
}
} else {
long rand_access_cold_item_id = random(COLD_DATA_ITEM_START, DB_SIZE);
if (!is_cached(n, rand_access_cold_item_id)) {
q->item_id = rand_access_cold_item_id;
send(node[0].mbox, (long)q);
printf("Client %ld is GENERATING QUERY request at COLD data... with id %ld at %6.3f\n", n, rand_access_cold_item_id, clock);
q_gen = clock;
}
}
}
}
int is_cached(long n, long item_id) {
int cached = 0;
long i;
for (i = 0; i < CACHE_SIZE; i++) {
if (cache_size[n][i].id == item_id) {
cache_size[n][i].last_accessed_time = clock;
if (is_cache_full(n)) {
cache_hit++;
}
printf("node %ld CACHE HIT id %ld ---- generated query id %ld\n", n, cache_size[n][i].id, item_id);
cached = 1;
}
}
if (is_cache_full(n)) {
cache_miss++;
}
if (cached != 1) {
printf("node %ld CACHE MISS id %ld\n", n, item_id);
}
return cached;
}
void receive_ir(long n) {
create("receive_ir");
printf("receiving IR...\n");
while(clock < SIM_TIME){
hold(exponential(1));
receive(node[n].mbox, (long*)&ir);
if (ir[0].ir_status == 1) {
printf("Node %ld address %ld, receives IR size %ld\n", n, &cache_size[n], ir[0].ir_size);
} else {
printf("Node %ld address %ld, receives DATA ITEM size %ld\n", n, &cache_size[n], ir[0].ir_size);
}
if (ir[0].ir_status == 1) {
int i, j;
for (i = 0; i < ir[0].ir_size; i++) {
printf("IR id %ld\n", ir[i].id);
for (j = 0; j < CACHE_SIZE; j++) {
if (cache_size[n][j].id == ir[i].id) {
cache_size[n][j].valid = 0;
printf("Invalidating cache id %ld, valid %ld, updated time %6.3f ... at node %ld\n", cache_size[n][j].id, cache_size[n][j].valid, cache_size[n][j].last_updated_time, n);
}
}
}
} else {
int i, j;
for (i = 0; i < ir[0].ir_size; i++) {
printf("DATA ITEM id %ld\n", ir[i].id);
if (!is_cache_full(n)) {
for (j = 0; j < CACHE_SIZE; j++) {
if (cache_size[n][j].id == -1) {
cache_size[n][j].id = ir[i].id;
cache_size[n][j].valid = 1;
cache_size[n][j].last_updated_time = ir[i].last_updated_time;
cache_size[n][j].last_accessed_time = 0;
printf("NEW DATA ITEM is cached: id %ld, valid %ld, last updated time %6.3f, last accessed time %6.3f\n", cache_size[n][j].id, cache_size[n][j].valid, cache_size[n][j].last_updated_time, cache_size[n][j].last_accessed_time);
break;
}
}
} else {
int updated = 0;
for (j = 0; j < CACHE_SIZE; j++) {
if (ir[i].id == cache_size[n][j].id && cache_size[n][j].valid == 0) {
cache_size[n][j].valid = 1;
cache_size[n][j].last_updated_time = ir[i].last_updated_time;
printf("CACHED DATA ITEM is updated: id %ld, valid %ld, last updated time %6.3f, last accessed time %6.3f\n", cache_size[n][j].id, cache_size[n][j].valid, cache_size[n][j].last_updated_time, cache_size[n][j].last_accessed_time);
updated = 1;
q_delay += clock - q_gen;
break;
}
}
if (!updated) {
int inv_idx = get_oldest_invalid(n);
int val_idx = get_oldest_valid(n);
if (inv_idx != 0) {
printf("OLDEST INVALID data item id %ld idx %ld --- ", cache_size[n][inv_idx].id, inv_idx);
cache_size[n][inv_idx].id = ir[i].id;
cache_size[n][inv_idx].valid = 1;
cache_size[n][inv_idx].last_updated_time = ir[i].last_updated_time;
cache_size[n][inv_idx].last_accessed_time = 0;
printf("is replaced by new data item %ld\n", cache_size[n][inv_idx].id);
q_delay += clock - q_gen;
}
else if (val_idx != 0) {
printf("OLDEST VVVVALID data item id %ld idx %ld --- ", cache_size[n][val_idx].id, val_idx);
cache_size[n][val_idx].id = ir[i].id;
cache_size[n][val_idx].valid = 1;
cache_size[n][val_idx].last_updated_time = ir[i].last_updated_time;
cache_size[n][val_idx].last_accessed_time = 0;
printf("is replaced by new data item %ld\n", cache_size[n][val_idx].id);
q_delay += clock - q_gen;
}
}
}
}
}
}
}
int get_oldest_invalid(long n) {
int k;
int lru_idx = 0;
long lru_time = cache_size[n][0].last_updated_time;
for (k = 1; k < CACHE_SIZE; k++) {
if (cache_size[n][k].valid == 0 && cache_size[n][k].last_updated_time <= lru_time) {
lru_time = cache_size[n][k].last_updated_time;
lru_idx = k;
}
}
return lru_idx;
}
int get_oldest_valid(long n) {
int k;
int lru_idx = 0;
long lru_time = cache_size[n][0].last_accessed_time;
for (k = 1; k < CACHE_SIZE; k++) {
if (cache_size[n][k].valid == 1 && cache_size[n][k].last_accessed_time <= lru_time) {
lru_time = cache_size[n][k].last_accessed_time;
lru_idx = k;
}
}
return lru_idx;
}
int is_cache_full(long n) {
int full = 1;
long i;
for (i = 0; i < CACHE_SIZE; i++) {
if (cache_size[n][i].id == -1) {
full = 0;
return full;
}
}
if (full) {
printf("node %ld cache is full\n", n);
}
return full;
}