home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / pvfs2 / orangefs-2.8.3-20110323.tar.gz / orangefs-2.8.3-20110323.tar / orangefs / src / io / buffer / ncac-init.c < prev    next >
C/C++ Source or Header  |  2006-09-16  |  4KB  |  153 lines

  1. /* this file includes a couple functions to initiate all kinds resources and free them
  2.  * when needed. */
  3.  
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8.  
  9. #include "ncac-list.h"
  10.  
  11. #include "internal.h"
  12. #include "radix.h"
  13.  
  14. /* global variable */
  15. NCAC_dev_t NCAC_dev;
  16. struct inode *inode_arr[MAX_INODE_NUM];
  17.  
  18. static inline void init_free_extent_list(int num);
  19. static inline void init_free_req_list(int num);
  20. static inline void init_cache_stack_list(void);
  21.  
  22. unsigned long radix_get_value(const void *item);
  23.  
  24. /* TODO: */
  25. static int extlog2(int extsize)
  26. {
  27.     return 15;
  28. }
  29.  
  30.  
  31. /* cache_init(): initiate cache. */
  32.  
  33. int cache_init(NCAC_info_t *info)
  34. {
  35.     int reqnum;
  36.  
  37.     if ( info->max_req_num == -1 ) reqnum = MAX_DELT_REQ_NUM;
  38.     else reqnum = info->max_req_num;
  39.  
  40.     NCAC_dev.free_req_src = (NCAC_req_t*) malloc(reqnum*sizeof(struct NCAC_req));
  41.     if ( NCAC_dev.free_req_src == NULL){
  42.         fprintf(stderr, "cache_init: cannot allocate request memory\n");
  43.         return -ENOMEM;
  44.     }
  45.     memset( NCAC_dev.free_req_src, 0, reqnum*sizeof(struct NCAC_req) );
  46.     init_free_req_list(reqnum);
  47.  
  48.     NCAC_dev.extsize = info->extsize;
  49.     NCAC_dev.extlog2 = extlog2(info->extsize);
  50.  
  51.     NCAC_dev.cachesize = info->cachesize;
  52.     NCAC_dev.cachemem  = (char *) info->cachespace;
  53.     if ( !info->cachespace )
  54.     {
  55.          fprintf(stderr, "cache space is NULL\n");
  56.     }
  57.  
  58.     /* we expect extcnt is a power of two number */
  59.     NCAC_dev.extcnt = (NCAC_dev.cachesize/NCAC_dev.extsize);
  60.  
  61.     NCAC_dev.free_extent_src = (struct extent *)malloc(NCAC_dev.extcnt*sizeof(struct extent) );
  62.     memset(NCAC_dev.free_extent_src, 0, NCAC_dev.extcnt*sizeof(struct extent) );
  63.     if ( NCAC_dev.free_extent_src == NULL){
  64.         fprintf(stderr, "cache_init: cannot allocate extent memory\n");
  65.         return -ENOMEM;
  66.     }
  67.  
  68.     init_free_extent_list(NCAC_dev.extcnt);
  69.     init_cache_stack_list();
  70.  
  71.  
  72.     INIT_LIST_HEAD( &NCAC_dev.prepare_list);
  73.     INIT_LIST_HEAD( &NCAC_dev.bufcomp_list);
  74.     INIT_LIST_HEAD( &NCAC_dev.comp_list);
  75.  
  76.  
  77.     memset( inode_arr, 0, sizeof(struct inode*)*MAX_INODE_NUM );
  78.  
  79.     NCAC_dev.get_value = radix_get_value;
  80.     NCAC_dev.max_b     = RADIX_MAX_BITS;
  81.  
  82.     fprintf(stderr, "CACHE SUMMARY:\n");
  83.     fprintf(stderr, "---- req num:%d\t    ext num: %ld\t\n", reqnum, NCAC_dev.extcnt);
  84.  
  85.     return 0;
  86. }
  87.  
  88.  
  89. /* for radix tree if linux radix tree is not used */
  90. unsigned long radix_get_value(const void *item)
  91. {
  92.     const struct extent *e = item;
  93.     return e->index;
  94. }
  95.  
  96. static inline void init_free_extent_list(int num)
  97. {
  98.     int i;
  99.     struct extent *start;
  100.     struct cache_stack *cache;
  101.     struct list_head *head;
  102.  
  103.     cache = &NCAC_dev.cache_stack;
  104.     start = NCAC_dev.free_extent_src;
  105.  
  106.  
  107.     head = &cache->free_extent_list;
  108.     INIT_LIST_HEAD( head );
  109.  
  110.     for (i = 0; i < num; i++ ){
  111.         list_add_tail( &start[i].list, head );
  112.         //DPRINT("%d: %p\n", i, start+i);
  113.         start[i].addr = NCAC_dev.cachemem+i*NCAC_dev.extsize;
  114.         start[i].ioreq = INVAL_IOREQ;
  115.     }
  116.     cache->nr_free = num;
  117.     cache->nr_dirty = 0;
  118. }
  119.  
  120. static inline void init_free_req_list(int num)
  121. {
  122.     int i;
  123.     struct NCAC_req *start;
  124.     struct list_head *head;
  125.  
  126.     head = &NCAC_dev.free_req_list;
  127.     start = NCAC_dev.free_req_src;
  128.  
  129.     INIT_LIST_HEAD(head);
  130.     for (i = 0; i < num; i++ ){
  131.         list_add_tail(&start[i].list, head);
  132.         //DPRINT("%d: %p\n", i, start+i);
  133.  
  134.         start[i].id = i;
  135.         start[i].reserved_cbufcnt = 0;
  136.     }
  137.     NCAC_dev.free_req_num = num;
  138.     spin_lock_init(&NCAC_dev.req_list_lock);
  139.     
  140. }
  141.  
  142. static inline void init_cache_stack_list()
  143. {
  144.     struct cache_stack *cache;
  145.  
  146.     cache = &NCAC_dev.cache_stack;
  147.  
  148.     cache->nr_active = cache->nr_inactive = 0;
  149.     INIT_LIST_HEAD( &cache->active_list);
  150.     INIT_LIST_HEAD( &cache->inactive_list);
  151.     spin_lock_init(&cache->lock);
  152. }
  153.