t_cached_object *search_cache(t_session *session, char *file) { off_t size; if(file == NULL) { return NULL; } else if((size = filesize(file)) == -1) { return NULL; } int i; if((i = cache_index(file)) == -1) { return NULL; } pthread_mutex_lock(&cache_mutex[i]); t_cached_object *object = cache[i], *result = NULL; struct stat status; /*object = cache[i];*/ while(object != NULL) { if(object->size == size) { if (strcmp(object->file, file) == 0) { if(stat(file, &status) == 0) { if((object->deadline > session->time) && (status.st_mtime == object->last_changed)) { if(same_ip(&(object->last_ip), &(session->ip_address)) == false) { if((object->deadline += TIME_IN_CACHE) > (session->time + MAX_CACHE_TIMER)) { object->deadline = session->time + MAX_CACHE_TIMER; } copy_ip(&(object->last_ip), &(session->ip_address)); } object->in_use++; result = object; } else if(object->in_use <= 0) { remove_from_cache(object, i); } } else if(object->in_use <= 0) { remove_from_cache(object, i); } break; } } object = object->next; } pthread_mutex_unlock(&cache_mutex[i]); return result; }
int add_to_cache(char * data, int site_size, char* uri) { if (site_size + sizeof(cache_object) <= MAX_OBJECT_SIZE) { return 0; } else { dbg_printf("add_to_cache"); /* Writing to the cache - only one thread should be able to write to it */ pthread_rwlock_wrlock(&lock); /* lock on the resource*/ /* Free enough space for the new site in cache*/ while(cache_size + site_size + sizeof(cache_object) >= MAX_CACHE_SIZE ){ remove_from_cache(); } /* Add the site node to the start of the cache list*/ cache_object * site = (cache_object *)malloc(sizeof(cache_object)); site -> next = head; site -> data = data; site -> lru_time_track = global_time++; site -> size = site_size; site -> uri = malloc(( strlen( uri ) + 1 ) * sizeof( char )); strcpy( site -> uri, uri ); head = site; cache_size += sizeof( cache_object ) + site_size + strlen( uri ) + 1; pthread_rwlock_unlock( &lock ); return 1; } }
static void remove_output_from_cache(t_session *session, char *request_uri, t_cot_type cot_type) { t_cached_object *object; char *url; int index; if ((url = make_url(session, request_uri)) == NULL) { return; } else if ((index = cache_index(url)) == -1) { free(url); return; } pthread_mutex_lock(&cache_mutex[index]); object = cache[index]; while (object != NULL) { if (object->type == cot_type) { if (strcmp(object->file, url) == 0) { if (object->in_use <= 0) { remove_from_cache(object, index); } else { object->deadline = 0; } break; } } object = object->next; } pthread_mutex_unlock(&cache_mutex[index]); free(url); }
static void flush_output_cache(t_session *session, t_cot_type cot_type) { t_cached_object *object; int index; size_t len; for (index = 0; index < MAX_CACHE_INDEX; index++) { pthread_mutex_lock(&cache_mutex[index]); object = cache[index]; while (object != NULL) { if (object->type == cot_type) { len = strlen(*(session->host->hostname.item)); if (strncmp(object->file, *(session->host->hostname.item), len) == 0) { if (*(object->file + len) == '/') { if (object->in_use <= 0) { object = remove_from_cache(object, index); continue; } else { object->deadline = 0; } } } } object = object->next; } pthread_mutex_unlock(&cache_mutex[index]); } }
void insert_into_cache(struct cache_header *cache, size_t size, char* data, char* tag){ pthread_rwlock_wrlock(&lock); struct cache_block *new_block = malloc(sizeof(struct cache_block)); if (cache->total_size + size > MAX_CACHE_SIZE) remove_from_cache(cache,size); new_block->next = cache->start; cache->start = new_block; cache->total_size += size; new_block->size = size; new_block->data = malloc(size); memcpy(new_block->data, data, size); new_block->tag = malloc(strlen(tag) + 1); strcpy(new_block->tag,tag); new_block->timestamp = ++cache->currTime; Sem_init(&new_block->mutex, 0, 1); pthread_rwlock_unlock(&lock); }
void manage_cache(time_t now) { t_cached_object *object; int index; for (index = 0; index < MAX_CACHE_INDEX; index++) { pthread_mutex_lock(&cache_mutex[index]); object = cache[index]; while (object != NULL) { if (now > object->deadline) { if (object->in_use <= 0) { object = remove_from_cache(object, index); continue; } } object = object->next; } pthread_mutex_unlock(&cache_mutex[index]); } }
static int swap_out(frame_t victim) { printf("%s: Swap Out Page %5d, Frame %5d\n", current_ref, physical_mem[victim].page_ref->page, victim); /* * Pull from Cache */ remove_from_cache(physical_mem[victim].page_ref->pid, physical_mem[victim].page_ref->page); /* * Pull from TLB */ remove_from_tlb(physical_mem[victim].page_ref->page, physical_mem[victim].page_ref->pid); /* * Simulate time to write out to disk (only if page is dirty) */ if( physical_mem[victim].dirty ) { nanosleep(&ACCESS_TIME_DISK, NULL); stats.swap_out++; } /* * Update the page table of the victim */ physical_mem[victim].page_ref->frame = 0; physical_mem[victim].page_ref->valid = false; physical_mem[victim].page_ref = NULL; /* * Add frame to the free frame list */ next_free_frame++; free_frame_list[next_free_frame] = victim; return 0; }
void check_cache(time_t now) { t_cached_object *object; int i; for(i = 0; i < MAX_CACHE_INDEX; i++) { pthread_mutex_lock(&cache_mutex[i]); object = cache[i]; while(object != NULL) { if(now > object->deadline) { if(object->in_use <= 0) { object = remove_from_cache(object, i); continue; } else { object->deadline = 0; } } object = object->next; } pthread_mutex_unlock(&cache_mutex[i]); } }
static t_cached_object *search_cache_for_output(t_session *session, t_cot_type cot_type) { t_cached_object *object, *result = NULL; char *url; int index; if ((url = make_url(session, NULL)) == NULL) { return NULL; } else if ((index = cache_index(url)) == -1) { free(url); return NULL; } pthread_mutex_lock(&cache_mutex[index]); object = cache[index]; while (object != NULL) { if (object->type == cot_type) { if (strcmp(object->file, url) == 0) { if (object->deadline > session->time) { object->in_use++; result = object; } else if (object->in_use <= 0) { remove_from_cache(object, index); } break; } } object = object->next; } pthread_mutex_unlock(&cache_mutex[index]); free(url); return result; }