To extend the code to meet the requirements of the project you described, you need to modify the client and server functions to implement the query generation and update arrival processes. You also need to add code to collect the cache hit ratio and query delay results for different combinations of T_query and T_update parameters. Here is an updated implementation in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "csim.h"
#define CACHE_SIZE 100
#define DB_SIZE 500
#define ITEM_SIZE 1024
#define HOT_DATA_ITEMS 50
#define HOT_DATA_PROB 0.8
#define HOT_DATA_UPDATE_PROB 0.33
typedef struct cache {
int id;
char data[ITEM_SIZE];
struct cache *prev;
struct cache *next;
} Cache;
typedef struct query {
int id;
int data_id;
} Query;
typedef struct request {
int id;
int data_id;
} Request;
typedef struct check {
int id;
int data_id;
int version;
} Check;
typedef struct reply {
int id;
int data_id;
char data[ITEM_SIZE];
int version;
} Reply;
typedef struct confirm {
int id;
int data_id;
int version;
} Confirm;
Cache *cache_head = NULL;
Cache *cache_tail = NULL;
int cache_size = 0;
int db_versions[DB_SIZE] = {0};
char db_data[DB_SIZE][ITEM_SIZE];
int num_queries = 0;
int num_hits = 0;
double total_delay = 0;
void cache_insert(int id, char data[ITEM_SIZE]) {
Cache *cache_item = (Cache*)malloc(sizeof(Cache));
cache_item->id = id;
memcpy(cache_item->data, data, ITEM_SIZE);
cache_item->prev = NULL;
cache_item->next = cache_head;
if (cache_head == NULL) {
cache_head = cache_item;
cache_tail = cache_item;
} else {
cache_head->prev = cache_item;
cache_head = cache_item;
}
cache_size++;
if (cache_size > CACHE_SIZE) {
cache_tail = cache_tail->prev;
free(cache_tail->next);
cache_tail->next = NULL;
cache_size--;
}
}
Cache* cache_lookup(int id) {
Cache *curr = cache_head;
while (curr != NULL) {
if (curr->id == id) {
if (curr != cache_head) {
if (curr == cache_tail) {
cache_tail = curr->prev;
cache_tail->next = NULL;
} else {
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
}
curr->prev = NULL;
curr->next = cache_head;
cache_head->prev = curr;
cache_head = curr;
}
return curr;
}
curr = curr->next;
}
return NULL;
}
void client() {
int i, data_id, wait_time, version, hit;
Cache *cache_item;
Query query;
Request request;
Check check;
Reply reply;
Confirm confirm;
for (i = 0; i < 100; i++) {
num_queries++;
wait_time = (int)expntl(1.0/T_query); // exponential distribution
hold(wait_time);
data_id = (int)(HOT_DATA_PROB * HOT_DATA_ITEMS) + rnd(1, DB_SIZE-HOT_DATA_ITEMS);
cache_item = cache_lookup(data_id);
if (cache_item != NULL) {
hit = 1;
check.id = i;
check.data_id = data_id;
check.version = db_versions[data_id-1];
send(server, CHECK_MSG, &check);
recv(server, REPLY_MSG, &reply);
if (reply.version > db_versions[data_id-1]) {
cache_insert(data_id, reply.data);
db_versions[data_id-1] = reply.version;
}
} else {
hit = 0;
request.id = i;
request.data_id = data_id;
send(server, REQUEST_MSG, &request);
recv(server, REPLY_MSG, &reply);
cache_insert(data_id, reply.data);
db_versions[data_id-1] = reply.version;
}
total_delay += event_time - wait_time;
if (hit) {
num_hits++;
}
}
}
void server() {
int i, data_id, wait_time, version;
Request request;
Check check;
Reply reply;
Confirm confirm;
while (1) {
recv(client, ANY_MSG, &request);
data_id = request.data_id;
if (data_id > 0 && data_id <= DB_SIZE) {
if (db_versions[data_id-1] > 0) {
reply.id = request.id;
reply.data_id = data_id;
memcpy(reply.data, db_data[data_id-1], ITEM_SIZE);
reply.version = db_versions[data_id-1];
send(client, REPLY_MSG, &reply);
}
}
hold(0.05);
wait_time = (int)expntl(1.0/T_update);
hold(wait_time);
if (rnd(0, 1) < HOT_DATA_UPDATE_PROB) {
data_id = rnd(1, HOT_DATA_ITEMS);
} else {
data_id = rnd(HOT_DATA_ITEMS+1, DB_SIZE);
}
if (db_versions[data_id-1] > 0) {
version = db_versions[data_id-1] + 1;
} else {
version = 1;
}
db_versions[data_id-1] = version;
memset(db_data[data_id-1], 0, ITEM_SIZE);
sprintf(db_data[data_id-1], "Data item %d version %d", data_id, version);
confirm.id = 0;
confirm.data_id = data_id;
confirm.version = version;
send(client, CONFIRM_MSG, &confirm);
}
}
int main() {
int i, j, num_clients = 5;
double hit_ratio, avg_delay;
double T_query_arr[] = {5, 10, 25, 50, 100};
double T_update_arr[] = {5, 10, 20, 100, 200};
init();
set_model_name("Cache Invalidation");
add_device("Client", num_clients);
add_device("Server", 1);
for (i = 1; i <= DB_SIZE; i++) {
sprintf(db_data[i-1], "Data item %d version %d", i, db_versions[i-1]);
}
for (i = 0; i < sizeof(T_query_arr)/sizeof(T_query_arr[0]); i++) {
for (j = 0; j < sizeof(T_update_arr)/sizeof(T_update_arr[0]); j++) {
num_queries = 0;
num_hits = 0;
total_delay = 0;
T_query = T_query_arr[i];
T_update = T_update_arr[j];
event_list_reset();
event_list_scan();
hit_ratio = (double)num_hits / num_queries;
avg_delay = total_delay / num_queries;
printf("T_query=%f, T_update=%f, hit_ratio=%f, avg_delay=%f\n", T_query, T_update, hit_ratio, avg_delay);
}
}
return 0;
}
In this updated implementation, the simulation is performed for all possible combinations of T_query and T_update parameters. For each combination, the cache hit ratio and query delay are calculated and displayed in the output. The simulation results can then be plotted to analyze the performance of the pull-based cache invalidation scheme.