Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Index: cache.c
  2. ===================================================================
  3. --- cache.c (revision 10861)
  4. +++ cache.c (working copy)
  5. @@ -32,6 +32,7 @@
  6.      uint32_t offset;
  7.      uint32_t length;
  8.  
  9. +    time_t time;
  10.      tr_block_index_t block;
  11.  
  12.      uint8_t * buf;
  13. @@ -53,9 +54,16 @@
  14.  *****
  15.  ****/
  16.  
  17. +struct run_info
  18. +{
  19. +  time_t  last_block_time;
  20. +  tr_bool is_aligned;
  21. +};
  22. +
  23. +
  24.  /* return a count of how many contiguous blocks there are starting at this pos */
  25.  static int
  26. -getBlockRun( const tr_cache * cache, int pos )
  27. +getBlockRun( const tr_cache * cache, int pos, struct run_info * info )
  28.  {
  29.      int i;
  30.      const int n = tr_ptrArraySize( &cache->blocks );
  31. @@ -67,35 +75,69 @@
  32.          const struct cache_block * b = blocks[i];
  33.          if( b->block != block ) break;
  34.          if( b->tor != ref->tor ) break;
  35. -//fprintf( stderr, "pos %d tor %d block %zu\n", i, b->tor->uniqueId, (size_t)b->block );
  36. +//fprintf( stderr, "pos %d tor %d block %zu time %zu\n", i, b->tor->uniqueId, (size_t)b->block, (size_t)b->time );
  37.      }
  38.  
  39.  //fprintf( stderr, "run is %d long from [%d to %d)\n", (int)(i-pos), i, (int)pos );
  40. +    if( info != NULL ) {
  41. +        info->last_block_time = blocks[i-1]->time;
  42. +        info->is_aligned = (blocks[pos]->block % 2 == 0) ? TRUE : FALSE;
  43. +    }
  44. +
  45.      return i-pos;
  46.  }
  47.  
  48. -/* return the starting index of the longest contiguous run of blocks */
  49. +/* return the starting index of the longest contiguous run of blocks BUT:
  50. + *   1 - Length of run must be even.
  51. + *   2 - Run must begin with a even block.
  52. + *   3 - Oldest run is preferred.
  53. + */
  54.  static int
  55. -findLargestChunk( tr_cache * cache, int * setme_n )
  56. +findChunk2Discard( tr_cache * cache, int * setme_n )
  57.  {
  58.      const int n = tr_ptrArraySize( &cache->blocks );
  59.      int pos;
  60.      int bestpos = 0;
  61. -    int bestlen = getBlockRun( cache, bestpos );
  62. +    unsigned bestlen = 1;
  63. +    unsigned jump;
  64. +    time_t oldest_time = ~0;
  65. +    struct run_info run;
  66.  
  67. -    for( pos=bestlen; pos<n; )
  68. +    for( pos=0; pos<n; pos+=jump )
  69.      {
  70. -        const int len = getBlockRun( cache, pos );
  71. +        unsigned len = getBlockRun( cache, pos, &run );
  72. +        jump = len;
  73. +
  74. +        /* shortcut */
  75. +        if( len < bestlen )
  76. +            continue;
  77. +
  78. +        /* check alignment */
  79. +        if( len % 2 == 0 ) {
  80. +            if( !run.is_aligned )
  81. +                /* Let it grow. Otherwise we contribute to fragmentation */
  82. +                continue;
  83. +        } else {
  84. +            if( len != 1 ) {
  85. +                --len;
  86. +                if( !run.is_aligned ) {
  87. +                    ++pos;
  88. +                    --jump;
  89. +                }
  90. +            }
  91. +        }
  92.  
  93.          if( bestlen < len ) {
  94.              bestlen = len;
  95.              bestpos = pos;
  96. +            oldest_time = run.last_block_time;
  97. +        } else if( (bestlen == len) && (oldest_time > run.last_block_time) ) {
  98. +            bestpos = pos;
  99. +            oldest_time = run.last_block_time;
  100.          }
  101. -
  102. -        pos += len;
  103.      }
  104.  
  105. -//fprintf( stderr, "LONGEST run is %d long from [%d to %d)\n", bestlen, bestpos, bestpos+bestlen );
  106. +//fprintf( stderr, "DISCARDED run is %d long from [%d to %d)\n", bestlen, bestpos, bestpos+bestlen );
  107.      *setme_n = bestlen;
  108.      return bestpos;
  109.  }
  110. @@ -146,7 +188,7 @@
  111.      while( !err && ( tr_ptrArraySize( &cache->blocks ) > cache->maxBlocks ) )
  112.      {
  113.          int n;
  114. -        const int i = findLargestChunk( cache, &n );
  115. +        const int i = findChunk2Discard( cache, &n );
  116.          err = flushContiguous( cache, i, n );
  117.      }
  118.  
  119. @@ -158,9 +200,9 @@
  120.  ***/
  121.  
  122.  static int
  123. -getMaxBlocks( size_t maxMiB )
  124. +getMaxBlocks( double maxMiB )
  125.  {
  126. -    const double maxBytes = maxMiB * 1024 * 1024;
  127. +    const double maxBytes = maxMiB * (1024 * 1024);
  128.      return maxBytes / MAX_BLOCK_SIZE;
  129.  }
  130.  
  131. @@ -182,7 +224,7 @@
  132.  tr_cache *
  133.  tr_cacheNew( double maxMiB )
  134.  {
  135. -    tr_cache * cache = tr_new( tr_cache, 1 );
  136. +    tr_cache * cache = tr_new0( tr_cache, 1 );
  137.      cache->blocks = TR_PTR_ARRAY_INIT;
  138.      cache->maxBlocks = getMaxBlocks( maxMiB );
  139.      return cache;
  140. @@ -214,10 +256,7 @@
  141.      if( a->block != b->block )
  142.          return a->block < b->block ? -1 : 1;
  143.  
  144. -    if( a->block < b->block ) return -1;
  145. -    if( a->block > b->block ) return  1;
  146. -
  147. -    /* they'r eequal */
  148. +    /* they're equal */
  149.      return 0;
  150.  }
  151.  
  152. @@ -255,6 +294,8 @@
  153.          tr_ptrArrayInsertSorted( &cache->blocks, cb, cache_block_compare );
  154.      }
  155.  
  156. +    cb->time = tr_time();
  157. +
  158.      tr_free( cb->buf );
  159.      cb->buf = tr_memdup( writeme, cb->length );
  160.  
  161. @@ -328,7 +369,7 @@
  162.          const struct cache_block * b = tr_ptrArrayNth( &cache->blocks, pos );
  163.          if( b->tor != torrent ) break;
  164.          if( ( b->block < begin ) || ( b->block >= end ) ) break;
  165. -        err = flushContiguous( cache, pos, getBlockRun( cache, pos ) );
  166. +        err = flushContiguous( cache, pos, getBlockRun( cache, pos, NULL ) );
  167.      }
  168.  
  169.      return err;
  170. @@ -345,7 +386,7 @@
  171.      {
  172.          const struct cache_block * b = tr_ptrArrayNth( &cache->blocks, pos );
  173.          if( b->tor != torrent ) break;
  174. -        err = flushContiguous( cache, pos, getBlockRun( cache, pos ) );
  175. +        err = flushContiguous( cache, pos, getBlockRun( cache, pos, NULL ) );
  176.      }
  177.  
  178.      return err;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement