Changeset 8927
- Timestamp:
- 07/08/11 17:25:49 (23 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/Orange-Branch/src/client/usrint/ucache.c
r8919 r8927 1 2 1 #include "ucache.h" 3 2 4 /** this is the master pointer to the cache */5 3 static union user_cache_u *ucache; 6 4 static int ucache_blk_cnt; … … 37 35 char *key_file_path; 38 36 37 /* Note: had to set: kernel.shmmax amd kernel.shmall */ 38 39 39 /* set up shared memory region */ 40 40 key_file_path = GET_KEY_FILE; 41 41 key = ftok(key_file_path, PROJ_ID); 42 printf("key:\t\t\t0x%x\n", key); 42 43 id = shmget(key, CACHE_SIZE, CACHE_FLAGS); 44 printf("id:\t\t\t%d\n", id); 43 45 ucache = shmat(id, NULL, AT_FLAGS); 46 printf("ucache ptr:\t\t0x%x\n", (unsigned int)ucache); 44 47 ucache_blk_cnt = BLOCKS_IN_CACHE; 48 49 /* Error Reporting */ 50 45 51 /* initialize mtbl free list table */ 46 52 ucache->ftbl.free_mtbl_blk = NIL; 47 53 ucache->ftbl.free_mtbl_ent = NIL; 54 48 55 add_free_mtbls(0); 49 56 /* set up list of free blocks */ … … 59 66 ucache->ftbl.file[i].mtbl_blk = NIL; 60 67 ucache->ftbl.file[i].mtbl_ent = NIL; 68 ucache->ftbl.file[i].next = NIL; 69 //printf("next = %u\n", ucache->ftbl.file[i].next); 61 70 } 62 71 /* set up list of free hash table entries */ … … 64 73 for (i = FILE_TABLE_HASH_MAX; i < FILE_TABLE_ENTRY_COUNT - 1; i++) 65 74 { 75 ucache->ftbl.file[i].mtbl_blk = NIL; 76 ucache->ftbl.file[i].mtbl_ent = NIL; 66 77 ucache->ftbl.file[i].next = i+1; 67 78 } … … 95 106 { 96 107 struct file_table_s *ftbl = &(ucache->ftbl); 97 int ret = ftbl->free_blk;98 if( ret!=NIL){108 uint32_t ret = ftbl->free_blk; 109 if((int32_t)ret!=NIL){ 99 110 /* Use zero since free_blks have no ititialized mem tables */ 100 111 ftbl->free_blk = ucache->b[ret].mtbl[0].free_list; … … 119 130 static int get_free_fent(void) 120 131 { 121 struct file_table_s *ftbl = &(ucache->ftbl); 122 int ret = ftbl->free_list; 123 if(ret!=NIL){ 132 printf("trying to get free file entry...\n"); 133 struct file_table_s *ftbl = &(ucache->ftbl); 134 uint16_t ret = ftbl->free_list; 135 if((int16_t)ret!=NIL){ 124 136 ftbl->free_list = ftbl->file[ret].next; 137 printf("\tfree file entry index = %d\n", ret); 138 return ret; 139 } 140 else{ 141 /* EVICT? */ 142 printf("\terror getting free file entry...\n"); 143 return NIL; 144 } 145 } 146 147 static void put_free_fent(int fent) 148 { 149 printf("freeing file entry = %d\n", fent); 150 struct file_table_s *ftbl = &(ucache->ftbl); 151 ftbl->file[fent].tag_handle = NIL; 152 ftbl->file[fent].tag_id = NIL; 153 printf("\ttag_id\t\t0X%X\n", NIL); 154 printf("\ttag_handle\t0X%llX\n", ftbl->file[fent].tag_handle); 155 if(fent>(FILE_TABLE_HASH_MAX-1)){ 156 ftbl->file[fent].next = ftbl->free_list; 157 ftbl->free_list = fent; 158 } 159 else{ 160 ftbl->file[fent].next = NIL; 161 } 162 } 163 164 static int get_free_ment(struct mem_table_s *mtbl) 165 { 166 uint16_t ret = mtbl->free_list; 167 if((int16_t)ret!=NIL){ 168 mtbl->free_list = mtbl->mem[ret].next; 125 169 return ret; 126 170 } … … 131 175 } 132 176 133 static void put_free_fent(int fent)134 {135 struct file_table_s *ftbl = &(ucache->ftbl);136 ftbl->file[fent].next = ftbl->free_list;137 ftbl->free_list = fent;138 }139 140 static int get_free_ment(struct mem_table_s *mtbl)141 {142 int ret = mtbl->free_list;143 if(ret!=NIL){144 mtbl->free_list = mtbl->mem[ret].next;145 return ret;146 }147 else{148 /* EVICT? */149 return NIL;150 }151 }152 153 177 static void put_free_ment(struct mem_table_s *mtbl, int ent) 154 178 { … … 157 181 } 158 182 159 static struct mem_table *lookup_file(uint32_t fs_id, uint64_t handle) 160 { 183 static struct mem_table_s *lookup_file( 184 uint32_t fs_id, 185 uint64_t handle, 186 uint32_t *file_mtbl_blk, /* Can be NULL if not desired */ 187 uint16_t *file_mtbl_ent, /* Can be NULL if not desired */ 188 uint16_t *file_ent_index /* Can be NULL if not desired */ 189 ) 190 { 191 printf("performing lookup...\n"); 161 192 struct file_table_s *ftbl = &(ucache->ftbl); 162 193 struct file_ent_s *current; /* Current ptr for iteration */ 163 int index; /* index into file hash table */194 int index; /* Index into file hash table */ 164 195 index = handle % FILE_TABLE_HASH_MAX; 196 printf("\thashed index: %d\n", index); 197 if(file_ent_index){ 198 *file_ent_index = index; 199 } 165 200 current = &(ftbl->file[index]); 166 if(current->mtbl_blk!=NIL && current->mtbl_ent!=NIL){ 201 /* Is there a link? */ 202 printf("\tcurrent->next = %d\n", (int16_t)current->next); 203 if((int16_t)current->next!=-1){ 167 204 /* Iterate and examine the fs_id and handle. 168 205 * stop when matched or next is NIL */ 169 while( (int)current!=NIL){206 while(1){ 170 207 if(current->tag_id==fs_id && 171 208 current->tag_handle==handle){ 172 return (struct mem_table *)&(ucache->b[ 209 printf("\tFile located in chain\n"); 210 if(file_mtbl_blk!=NULL){ 211 *file_mtbl_blk = current->mtbl_blk; 212 } 213 if(file_mtbl_ent!=NULL){ 214 *file_mtbl_ent = current->mtbl_ent; 215 } 216 return (struct mem_table_s *)&(ucache->b[ 173 217 current->mtbl_blk].mtbl[ 174 218 current->mtbl_ent]); 175 219 } 176 current = &(ftbl->file[current->next]); 177 } 178 return (struct mem_table *)NIL; 179 } 180 else{ 220 if(file_ent_index){ 221 *file_ent_index = current->next; 222 } 223 if((int16_t)current->next!=-1){ 224 current = &(ftbl->file[current->next]); 225 printf("\tIterating across the chain, next=%d\n", current->next); 226 } 227 else{ 228 break; 229 } 230 } 231 printf("\tlookup error: mtbl not found1\n"); 232 return (struct mem_table_s *)NIL; 233 } 234 else{ 235 printf("\tno chain detected\n"); 236 printf("\tcurrent->tag_id\t\t0X%X\n\tfs_id\t\t\t0X%X\n", current->tag_id, fs_id); 237 printf("\tcurrent->tag_handle\t0X%llX\n\thandle\t\t\t0X%llX\n", current->tag_handle, handle); 181 238 if(current->tag_id==fs_id && current->tag_handle==handle){ 182 return (struct mem_table *)&(ucache->b[ 239 if(file_mtbl_blk!=NULL){ 240 *file_mtbl_blk = current->mtbl_blk; 241 } 242 if(file_mtbl_ent!=NULL){ 243 *file_mtbl_ent = current->mtbl_ent; 244 } 245 printf("\tfile entry match\n"); 246 return (struct mem_table_s *)&(ucache->b[ 183 247 current->mtbl_blk].mtbl[current->mtbl_ent]); 184 248 } 185 return (struct mem_table *)NIL; 249 printf("\tlookup error: mtbl not found2!\n"); 250 return (struct mem_table_s *)NIL; 186 251 } 187 252 } … … 193 258 *free_mtbl_ent = ftbl->free_mtbl_ent; 194 259 /* is free mtbl_blk available? */ 195 if( *free_mtbl_blk!=NIL &&*free_mtbl_ent!=NIL){260 if((int32_t)*free_mtbl_blk!=NIL && (int16_t)*free_mtbl_ent!=NIL){ 196 261 ftbl->free_mtbl_blk = ucache->b[*free_mtbl_blk]. 197 262 mtbl[*free_mtbl_ent].free_list_blk; … … 212 277 static struct mem_table_s *insert_file(uint32_t fs_id, uint64_t handle) 213 278 { 279 printf("trying to insert file...\n"); 214 280 struct file_table_s *ftbl = &(ucache->ftbl); 215 281 struct file_ent_s *current; /* Current ptr for iteration */ 216 282 /* index into file hash table */ 217 283 int index = handle % FILE_TABLE_HASH_MAX; 284 printf("\thashed index: %d\n", index); 218 285 current = &(ftbl->file[index]); 219 286 /* Insert at index, relocating head data if necessary. */ 220 if(current->mtbl_blk!=NIL && current->mtbl_ent!=NIL){ /* relocating! */ 287 288 /* Need to relocate data? */ 289 if((int32_t)current->mtbl_blk!=NIL && (int16_t)current->mtbl_ent!=NIL){ /* relocating! */ 290 printf("\tmust relocate head data\n"); 221 291 /* get free file entry and update ftbl */ 222 292 uint16_t free_fent = get_free_fent(); 223 if(free_fent!=NIL){ 224 /* copy data @ index to new file entry */ 225 memcpy ((void *)&(ftbl->file[free_fent]), 226 (void *)¤t, 227 sizeof(struct file_ent_s)); 228 /* insert file data @ index */ 229 current->tag_id = fs_id; 230 current->tag_handle = handle; 293 if((int16_t)free_fent!=NIL){ 294 /* copy data from 1 struct to the other */ 295 ftbl->file[free_fent] = *current; 296 /* These should match */ 231 297 /* point new head's "next" to former head */ 232 298 current->next = free_fent; 299 printf("\tnew head's next = %d\n", current->next); 233 300 } 234 301 else{ /* No free file entries available: policy? */ 235 302 /* Evict or return NIL */ 303 printf("\terror: no free file entries"); 236 304 return (struct mem_table_s *)NIL; 237 305 } 306 } 307 else{ 308 printf("\tno head data @ index\n"); 238 309 } 239 310 /* Get next free mem_table */ … … 242 313 /* Is there a free memory table available? */ 243 314 if(get_next_free_mtbl(&free_mtbl_blk, &free_mtbl_ent)==1){ 315 /* insert file data @ index */ 316 current->tag_id = fs_id; 317 current->tag_handle = handle; 244 318 /* Update fent with it's new mtbl: blk and ent */ 245 319 current->mtbl_blk = free_mtbl_blk; 246 320 current->mtbl_ent = free_mtbl_ent; 321 322 //current->next = NIL; 247 323 /* Initialize Memory Table */ 248 324 init_memory_table(free_mtbl_blk, free_mtbl_ent); 325 printf("\trecieved free memory table: 0X%X\n", 326 (unsigned int)&(ucache->b[current->mtbl_blk].mtbl[ 327 current->mtbl_ent])); 249 328 return &(ucache->b[current->mtbl_blk].mtbl[ 250 329 current->mtbl_ent]); … … 252 331 else{ /* No free mtbl available: policy? */ 253 332 /* Evict or return NIL */ 333 printf("\terror: no free memory tables available"); 254 334 return (struct mem_table_s *)NIL; 255 335 } … … 259 339 static int remove_file(uint32_t fs_id, uint64_t handle) 260 340 { 261 struct file_table_s *ftbl = &(ucache->ftbl); 262 struct file_ent_s *current; /* Current ptr for iteration */ 263 uint32_t file_mtbl_blk = NIL; 264 uint16_t file_mtbl_ent = NIL; 265 /* index into file hash table */ 266 int index = handle % FILE_TABLE_HASH_MAX; 267 current = &(ftbl->file[index]); 268 if(current->mtbl_blk!=NIL && current->mtbl_ent!=NIL){ /* links present */ 269 /* Iterate and examine the fs_id and handle. 270 * stop when matched or next is NIL */ 271 while((int)current!=NIL){ 272 if(current->tag_id==fs_id && 273 current->tag_handle==handle){ 274 file_mtbl_blk = current->mtbl_blk; 275 file_mtbl_ent = current->mtbl_ent; 276 } 277 current = &(ftbl->file[current->next]); 278 } 279 } 280 else{ /* No links present */ 281 if(current->tag_id==fs_id && current->tag_handle==handle){ 282 file_mtbl_blk = current->mtbl_blk; 283 file_mtbl_ent = current->mtbl_ent; 284 } 285 } 286 //Verify we've recieved the necesasry info 287 if(file_mtbl_blk==NIL || file_mtbl_ent==NIL){ 341 printf("trying to remove file...\n"); 342 struct file_table_s *ftbl = &(ucache->ftbl); 343 int32_t file_mtbl_blk; 344 int16_t file_mtbl_ent; 345 int16_t file_ent_index; 346 struct mem_table_s *mtbl = lookup_file(fs_id, handle, 347 &file_mtbl_blk, &file_mtbl_ent, &file_ent_index); 348 /* Verify we've recieved the necessary info */ 349 if((int32_t)file_mtbl_blk==NIL || (int16_t)file_mtbl_ent==NIL){ 350 printf("\tremoval error: no matching mtbl\n"); 288 351 return NIL; 289 352 } 290 353 /* add mem_table back to free list */ 291 /* First: store temp copy of current head*/354 /* Temporarily store copy of current head (the new next) */ 292 355 uint32_t tmp_blk = ftbl->free_mtbl_blk; 293 356 uint16_t tmp_ent = ftbl->free_mtbl_ent; 294 357 295 / /Need current block and ent corresponding to file to be removed358 /* newly free mtbl becomes new head of free mtbl list */ 296 359 ftbl->free_mtbl_blk = file_mtbl_blk; 297 360 ftbl->free_mtbl_ent = file_mtbl_ent; 361 362 /* Point to the next free mtbl (the former head) */ 363 mtbl->free_list_blk = tmp_blk; 364 mtbl->free_list = tmp_ent; 298 365 299 //Set nexts 300 // ftbl->free_mtbl_blk 301 302 //put_free_fent(index); 366 /* Free the file entry */ 367 put_free_fent(file_ent_index); 368 printf("\tremoval sucessful\n"); 369 //print the values to ensure they really got set 370 printf("\ttag_id\t\t0X%X\n", ftbl->file[file_ent_index].tag_id); 371 printf("\ttag_handle\t0X%llX\n", ftbl->file[file_ent_index].tag_handle); 372 303 373 return(1); 304 374 } … … 317 387 static int remove_mem(struct mem_table_s *mtbl, uint64_t offset) 318 388 { 389 } 390 391 void simple_test_1(void){ 392 ucache_initialize(); 393 /* Check Global Data */ 394 printf("address of ucache ptr:\t0x%x\n", (unsigned int)&ucache); 395 printf("ucache ptr:\t\t0x%x\n", (unsigned int)ucache); 396 printf("ftbl ptr:\t\t0x%x\n", (unsigned int)&(ucache->ftbl)); 397 printf("cache initialized...\n\n"); 398 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAAA, NULL, NULL, NULL); 399 insert_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAAA); 400 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAAA, NULL, NULL, NULL); 401 remove_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAAA); 402 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAAA, NULL, NULL, NULL); 403 } 404 405 void simple_test_2(void){ 406 ucache_initialize(); 407 /* Check Global Data */ 408 printf("address of ucache ptr:\t0x%x\n", (unsigned int)&ucache); 409 printf("ucache ptr:\t\t0x%x\n", (unsigned int)ucache); 410 printf("ftbl ptr:\t\t0x%x\n", (unsigned int)&(ucache->ftbl)); 411 printf("cache initialized...\n\n"); 412 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAAA, NULL, NULL, NULL); 413 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAC9, NULL, NULL, NULL); 414 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAA8B, NULL, NULL, NULL); 415 insert_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAAA); 416 insert_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAC9); 417 insert_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAA8B); 418 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAAA, NULL, NULL, NULL); 419 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAC9, NULL, NULL, NULL); 420 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAA8B, NULL, NULL, NULL); 421 remove_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAAA); 422 remove_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAC9); 423 remove_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAA8B); 424 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAAA, NULL, NULL, NULL); 425 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAAC9, NULL, NULL, NULL); 426 lookup_file(0XAAAAAAAA, 0XAAAAAAAAAAAAAA8B, NULL, NULL, NULL); 427 } 428 429 /* Note: When unsigned ints are set to NIL, their values are based on type: 430 ex: 16 0xFFFF 431 32 0XFFFFFFFF 432 64 0XFFFFFFFFFFFFFFFF 433 ALL EQUAL THE SIGNED REPRESENTATION OF -1, CALLED NIL. */ 434 int main(){ 435 //simple_test_1(); 436 simple_test_2(); 437 return 0; 319 438 } 320 439
