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-lru.c < prev    next >
C/C++ Source or Header  |  2006-05-26  |  2KB  |  84 lines

  1. /* Specific functions related to LRU cache policy */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #include "internal.h"
  6. #include "state.h"
  7. #include "flags.h"
  8. #include "cache.h"
  9. #include "ncac-lru.h"
  10. #include "pvfs2-internal.h"
  11.  
  12.  
  13. /* add an extent into a lru cache list. The caller should hold the lock 
  14.  * of "cache". 
  15.  */
  16. void LRU_add_cache_item(struct cache_stack *cache,struct extent *extent)
  17. {
  18.     /* Insert an entry after the specified head "active_list". */
  19.     list_add(&extent->lru, &cache->active_list);
  20.     SetPageLRU(extent);
  21.     extent->mapping->nrpages++;
  22.     cache->nr_active++;
  23. }
  24.  
  25. /* remove an extent from a lru cache list. The caller should hold the
  26.  * the lock of cache.
  27.  */
  28. void LRU_remove_cache_item(struct cache_stack *cache, struct extent *extent)
  29. {
  30.     list_del(&extent->lru);
  31.     extent->mapping->nrpages--;
  32.     cache->nr_active--;
  33. }
  34.  
  35. /* shrink the LRU cache list by discarding some extents from the list.
  36.  * The expected number of extents discarded is "expected", while the
  37.  * real number of discarded extents is "shrinked".
  38.  */
  39. int LRU_shrink_cache(struct cache_stack *cache, unsigned int expected,
  40.                 unsigned int *shrinked)
  41. {
  42.     struct list_head *lru_head, *lru_tail;
  43.     struct extent *victim;
  44.     int ret = 0;
  45.  
  46.     fprintf(stderr, "%s: expected:%d\n", __FUNCTION__, expected);
  47.      
  48.     *shrinked = 0;
  49.     lru_head = &cache->active_list;
  50.     lru_tail = lru_head->prev;
  51.  
  52.     while (*shrinked < expected && lru_tail != (& cache->active_list) ){
  53.         victim = list_entry(lru_tail, struct extent, lru);
  54.  
  55.         if ( !PageLRU(victim) ){
  56.             NCAC_error("extent flag is wrong. LRU flag is expected\n");
  57.             ret = NCAC_INVAL_FLAGS;
  58.             break;
  59.         }
  60.  
  61.         lru_tail = lru_tail->prev;
  62.  
  63.         if (PageReadPending(victim) || PageWritePending(victim)){
  64.             ret = NCAC_check_ioreq(victim);
  65.             if (ret < 0){
  66.                 NCAC_error("NCAC_check_ioreq error: index=%ld, ioreq=%lld\n",
  67.                         victim->index, lld(victim->ioreq));
  68.                 break;
  69.             }
  70.  
  71.             if (ret) { /* completion */
  72.                 list_set_clean_page(victim);
  73.             }
  74.         }
  75.  
  76.         if ( is_extent_discardable(victim) ){
  77.             LRU_remove_cache_item(cache, victim);
  78.             list_add_tail(&victim->list, &cache->free_extent_list);
  79.             (*shrinked)++;
  80.         }
  81.     }
  82.     return ret;
  83. }
  84.