Changeset 8927

Show
Ignore:
Timestamp:
07/08/11 17:25:49 (23 months ago)
Author:
jdenton
Message:

debugged many of the functions written.
added some basic test cases to test with.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/Orange-Branch/src/client/usrint/ucache.c

    r8919 r8927  
    1  
    21#include "ucache.h" 
    32 
    4 /** this is the master pointer to the cache */ 
    53static union user_cache_u *ucache; 
    64static int ucache_blk_cnt; 
     
    3735        char *key_file_path; 
    3836 
     37        /*      Note: had to set: kernel.shmmax amd kernel.shmall       */ 
     38 
    3939        /* set up shared memory region */ 
    4040        key_file_path = GET_KEY_FILE; 
    4141        key = ftok(key_file_path, PROJ_ID); 
     42        printf("key:\t\t\t0x%x\n", key); 
    4243        id = shmget(key, CACHE_SIZE, CACHE_FLAGS); 
     44        printf("id:\t\t\t%d\n", id); 
    4345        ucache = shmat(id, NULL, AT_FLAGS); 
     46        printf("ucache ptr:\t\t0x%x\n", (unsigned int)ucache); 
    4447        ucache_blk_cnt = BLOCKS_IN_CACHE; 
     48 
     49        /*      Error Reporting */ 
     50 
    4551        /* initialize mtbl free list table */ 
    4652        ucache->ftbl.free_mtbl_blk = NIL; 
    4753        ucache->ftbl.free_mtbl_ent = NIL; 
     54 
    4855        add_free_mtbls(0); 
    4956        /* set up list of free blocks */ 
     
    5966                ucache->ftbl.file[i].mtbl_blk = NIL; 
    6067                ucache->ftbl.file[i].mtbl_ent = NIL; 
     68                ucache->ftbl.file[i].next = NIL; 
     69                //printf("next = %u\n", ucache->ftbl.file[i].next); 
    6170        } 
    6271        /* set up list of free hash table entries */ 
     
    6473        for (i = FILE_TABLE_HASH_MAX; i < FILE_TABLE_ENTRY_COUNT - 1; i++) 
    6574        { 
     75                ucache->ftbl.file[i].mtbl_blk = NIL; 
     76                ucache->ftbl.file[i].mtbl_ent = NIL; 
    6677                ucache->ftbl.file[i].next = i+1; 
    6778        } 
     
    95106{ 
    96107        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){ 
    99110                /* Use zero since free_blks have no ititialized mem tables */ 
    100111                ftbl->free_blk = ucache->b[ret].mtbl[0].free_list;  
     
    119130static int get_free_fent(void) 
    120131{ 
    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){ 
    124136                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 
     147static 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 
     164static 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; 
    125169                return ret; 
    126170        } 
     
    131175} 
    132176 
    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  
    153177static void put_free_ment(struct mem_table_s *mtbl, int ent) 
    154178{ 
     
    157181} 
    158182 
    159 static struct mem_table *lookup_file(uint32_t fs_id, uint64_t handle) 
    160 { 
     183static 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"); 
    161192        struct file_table_s *ftbl = &(ucache->ftbl); 
    162193        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   */ 
    164195        index = handle % FILE_TABLE_HASH_MAX; 
     196        printf("\thashed index: %d\n", index); 
     197        if(file_ent_index){ 
     198                *file_ent_index = index; 
     199        } 
    165200        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){ 
    167204                /*      Iterate and examine the fs_id and handle. 
    168205                *       stop when matched or next is NIL        */ 
    169                 while((int)current!=NIL){        
     206                while(1){ 
    170207                        if(current->tag_id==fs_id &&  
    171208                                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[ 
    173217                                        current->mtbl_blk].mtbl[ 
    174218                                        current->mtbl_ent]); 
    175219                        } 
    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); 
    181238                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[ 
    183247                                current->mtbl_blk].mtbl[current->mtbl_ent]); 
    184248                } 
    185                 return (struct mem_table *)NIL; 
     249                printf("\tlookup error: mtbl not found2!\n"); 
     250                return (struct mem_table_s *)NIL; 
    186251        }         
    187252} 
     
    193258                *free_mtbl_ent = ftbl->free_mtbl_ent; 
    194259                /* 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){  
    196261                        ftbl->free_mtbl_blk = ucache->b[*free_mtbl_blk]. 
    197262                                mtbl[*free_mtbl_ent].free_list_blk; 
     
    212277static struct mem_table_s *insert_file(uint32_t fs_id, uint64_t handle) 
    213278{ 
     279        printf("trying to insert file...\n"); 
    214280        struct file_table_s *ftbl = &(ucache->ftbl); 
    215281        struct file_ent_s *current;     /* Current ptr for iteration    */ 
    216282        /*      index into file hash table      */ 
    217283        int index = handle % FILE_TABLE_HASH_MAX; 
     284        printf("\thashed index: %d\n", index); 
    218285        current = &(ftbl->file[index]);  
    219286        /*      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"); 
    221291                /*      get free file entry and update ftbl     */ 
    222292                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 *)&current, 
    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      */ 
    231297                        /*      point new head's "next" to former head  */ 
    232298                        current->next = free_fent;       
     299                        printf("\tnew head's next = %d\n", current->next); 
    233300                } 
    234301                else{   /*      No free file entries available: policy? */ 
    235302                        /*      Evict or return NIL     */ 
     303                        printf("\terror: no free file entries"); 
    236304                        return (struct mem_table_s *)NIL; 
    237305                } 
     306        } 
     307        else{ 
     308                printf("\tno head data @ index\n"); 
    238309        } 
    239310        /*      Get next free mem_table */ 
     
    242313        /* Is there a free memory table available? */ 
    243314        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; 
    244318                /*      Update fent with it's new mtbl: blk and ent */ 
    245319                current->mtbl_blk = free_mtbl_blk; 
    246320                current->mtbl_ent = free_mtbl_ent; 
     321 
     322                //current->next = NIL; 
    247323                /*      Initialize Memory Table */ 
    248324                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])); 
    249328                return &(ucache->b[current->mtbl_blk].mtbl[ 
    250329                        current->mtbl_ent]); 
     
    252331        else{   /*      No free mtbl available: policy? */ 
    253332                /*      Evict or return NIL     */ 
     333                printf("\terror: no free memory tables available"); 
    254334                return (struct mem_table_s *)NIL; 
    255335        } 
     
    259339static int remove_file(uint32_t fs_id, uint64_t handle) 
    260340{ 
    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"); 
    288351                return NIL; 
    289352        } 
    290353        /*      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)   */ 
    292355        uint32_t tmp_blk = ftbl->free_mtbl_blk; 
    293356        uint16_t tmp_ent = ftbl->free_mtbl_ent; 
    294357 
    295         //Need current block and ent corresponding to file to be removed 
     358        /*      newly free mtbl becomes new head of free mtbl list      */ 
    296359        ftbl->free_mtbl_blk = file_mtbl_blk; 
    297360        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; 
    298365         
    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 
    303373        return(1); 
    304374} 
     
    317387static int remove_mem(struct mem_table_s *mtbl, uint64_t offset) 
    318388{ 
     389} 
     390 
     391void 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 
     405void 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.  */ 
     434int main(){ 
     435        //simple_test_1(); 
     436        simple_test_2(); 
     437        return 0; 
    319438} 
    320439