2010-06-19 14:25:11 +00:00
|
|
|
/*
|
2014-01-18 20:56:57 +00:00
|
|
|
* This file Copyright (C) 2010-2014 Mnemosyne LLC
|
2010-06-19 14:25:11 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2010-06-19 14:25:11 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <stdlib.h> /* qsort () */
|
2011-03-16 18:04:23 +00:00
|
|
|
|
2011-01-29 18:56:53 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "cache.h"
|
|
|
|
#include "inout.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2010-06-19 14:25:11 +00:00
|
|
|
#include "peer-common.h" /* MAX_BLOCK_SIZE */
|
|
|
|
#include "ptrarray.h"
|
|
|
|
#include "torrent.h"
|
2013-01-31 17:39:06 +00:00
|
|
|
#include "trevent.h"
|
2010-06-19 14:25:11 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#define MY_NAME "Cache"
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#define dbgmsg(...) \
|
|
|
|
do \
|
|
|
|
{ \
|
2013-01-25 23:34:20 +00:00
|
|
|
if (tr_logGetDeepEnabled ()) \
|
|
|
|
tr_logAddDeep (__FILE__, __LINE__, MY_NAME, __VA_ARGS__); \
|
2012-12-05 17:29:46 +00:00
|
|
|
} \
|
|
|
|
while (0)
|
2010-07-01 16:26:30 +00:00
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
struct cache_block
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrent * tor;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_piece_index_t piece;
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t length;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
time_t time;
|
|
|
|
tr_block_index_t block;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
struct evbuffer * evbuf;
|
2010-06-19 14:25:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_cache
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ptrArray blocks;
|
|
|
|
int max_blocks;
|
|
|
|
size_t max_bytes;
|
|
|
|
|
|
|
|
size_t disk_writes;
|
|
|
|
size_t disk_write_bytes;
|
|
|
|
size_t cache_writes;
|
|
|
|
size_t cache_write_bytes;
|
2010-06-19 14:25:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2010-06-26 16:21:50 +00:00
|
|
|
struct run_info
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int pos;
|
|
|
|
int rank;
|
|
|
|
time_t last_block_time;
|
|
|
|
bool is_multi_piece;
|
|
|
|
bool is_piece_done;
|
2015-03-15 11:43:32 +00:00
|
|
|
unsigned int len;
|
2010-06-26 16:21:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
/* return a count of how many contiguous blocks there are starting at this pos */
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
getBlockRun (const tr_cache * cache, int pos, struct run_info * info)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int i;
|
|
|
|
const int n = tr_ptrArraySize (&cache->blocks);
|
|
|
|
const struct cache_block ** blocks = (const struct cache_block**) tr_ptrArrayBase (&cache->blocks);
|
|
|
|
const struct cache_block * ref = blocks[pos];
|
|
|
|
tr_block_index_t block = ref->block;
|
|
|
|
|
|
|
|
for (i=pos; i<n; ++i, ++block)
|
|
|
|
{
|
|
|
|
const struct cache_block * b = blocks[i];
|
|
|
|
if (b->block != block)
|
|
|
|
break;
|
|
|
|
if (b->tor != ref->tor)
|
|
|
|
break;
|
2013-09-08 17:32:09 +00:00
|
|
|
//fprintf (stderr, "pos %d tor %d block %"TR_PRIuSIZE" time %"TR_PRIuSIZE"\n", i, b->tor->uniqueId, (size_t)b->block, (size_t)b->time);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
//fprintf (stderr, "run is %d long from [%d to %d)\n", (int)(i-pos), i, (int)pos);
|
|
|
|
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
|
|
|
const struct cache_block * b = blocks[i-1];
|
|
|
|
info->last_block_time = b->time;
|
2013-08-18 13:06:39 +00:00
|
|
|
info->is_piece_done = tr_torrentPieceIsComplete (b->tor, b->piece);
|
2013-08-24 18:18:38 +00:00
|
|
|
info->is_multi_piece = b->piece != blocks[pos]->piece;
|
2012-12-05 17:29:46 +00:00
|
|
|
info->len = i - pos;
|
|
|
|
info->pos = pos;
|
2010-06-26 16:21:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return i-pos;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2011-03-16 00:49:43 +00:00
|
|
|
/* higher rank comes before lower rank */
|
2010-06-19 14:25:11 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compareRuns (const void * va, const void * vb)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const struct run_info * a = va;
|
|
|
|
const struct run_info * b = vb;
|
|
|
|
return b->rank - a->rank;
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
MULTIFLAG = 0x1000,
|
|
|
|
DONEFLAG = 0x2000,
|
|
|
|
SESSIONFLAG = 0x4000
|
2010-07-08 17:38:11 +00:00
|
|
|
};
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2010-07-08 17:38:11 +00:00
|
|
|
/* Calculte runs
|
|
|
|
* - Stale runs, runs sitting in cache for a long time or runs not growing, get priority.
|
2010-08-22 09:49:10 +00:00
|
|
|
* Returns number of runs.
|
2010-07-08 17:38:11 +00:00
|
|
|
*/
|
2010-08-22 09:49:10 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
calcRuns (tr_cache * cache, struct run_info * runs)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const int n = tr_ptrArraySize (&cache->blocks);
|
|
|
|
int i = 0, pos;
|
|
|
|
const time_t now = tr_time ();
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (pos = 0; pos < n; pos += runs[i++].len)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int rank = getBlockRun (cache, pos, &runs[i]);
|
|
|
|
|
|
|
|
/* This adds ~2 to the relative length of a run for every minute it has
|
|
|
|
* languished in the cache. */
|
|
|
|
rank += (now - runs[i].last_block_time) / 32;
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Flushing stale blocks should be a top priority as the probability of them
|
|
|
|
* growing is very small, for blocks on piece boundaries, and nonexistant for
|
|
|
|
* blocks inside pieces. */
|
|
|
|
rank |= runs[i].is_piece_done ? DONEFLAG : 0;
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Move the multi piece runs higher */
|
|
|
|
rank |= runs[i].is_multi_piece ? MULTIFLAG : 0;
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
runs[i].rank = rank;
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
//fprintf (stderr,"block run at pos %d of length %d and age %ld adjusted +%d\n",runs[i].pos,runs[i].len,now-runs[i].last_block_time,rank-runs[i].len);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
//fprintf (stderr, "%d block runs\n", i);
|
|
|
|
qsort (runs, i, sizeof (struct run_info), compareRuns);
|
|
|
|
return i;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
flushContiguous (tr_cache * cache, int pos, int n)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int i;
|
|
|
|
int err = 0;
|
|
|
|
uint8_t * buf = tr_new (uint8_t, n * MAX_BLOCK_SIZE);
|
|
|
|
uint8_t * walk = buf;
|
|
|
|
struct cache_block ** blocks = (struct cache_block**) tr_ptrArrayBase (&cache->blocks);
|
|
|
|
|
|
|
|
struct cache_block * b = blocks[pos];
|
|
|
|
tr_torrent * tor = b->tor;
|
|
|
|
const tr_piece_index_t piece = b->piece;
|
|
|
|
const uint32_t offset = b->offset;
|
|
|
|
|
|
|
|
for (i=pos; i<pos+n; ++i)
|
|
|
|
{
|
|
|
|
b = blocks[i];
|
|
|
|
evbuffer_copyout (b->evbuf, walk, b->length);
|
|
|
|
walk += b->length;
|
|
|
|
evbuffer_free (b->evbuf);
|
|
|
|
tr_free (b);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ptrArrayErase (&cache->blocks, pos, pos+n);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
err = tr_ioWrite (tor, piece, offset, walk-buf, buf);
|
|
|
|
tr_free (buf);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
++cache->disk_writes;
|
|
|
|
cache->disk_write_bytes += walk-buf;
|
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2010-07-08 17:38:11 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
flushRuns (tr_cache * cache, struct run_info * runs, int n)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int i;
|
|
|
|
int err = 0;
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; !err && i<n; i++)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int j;
|
|
|
|
|
|
|
|
err = flushContiguous (cache, runs[i].pos, runs[i].len);
|
|
|
|
|
|
|
|
for (j=i+1; j<n; j++)
|
|
|
|
if (runs[j].pos > runs[i].pos)
|
|
|
|
runs[j].pos -= runs[i].len;
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return err;
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
cacheTrim (tr_cache * cache)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int err = 0;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tr_ptrArraySize (&cache->blocks) > cache->max_blocks)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
/* Amount of cache that should be removed by the flush. This influences how large
|
|
|
|
* runs can grow as well as how often flushes will happen. */
|
|
|
|
const int cacheCutoff = 1 + cache->max_blocks / 4;
|
|
|
|
struct run_info * runs = tr_new (struct run_info, tr_ptrArraySize (&cache->blocks));
|
|
|
|
int i=0, j=0;
|
|
|
|
|
|
|
|
calcRuns (cache, runs);
|
|
|
|
while (j < cacheCutoff)
|
|
|
|
j += runs[i++].len;
|
|
|
|
err = flushRuns (cache, runs, i);
|
|
|
|
tr_free (runs);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
getMaxBlocks (int64_t max_bytes)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return max_bytes / (double)MAX_BLOCK_SIZE;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cacheSetLimit (tr_cache * cache, int64_t max_bytes)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char buf[128];
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
cache->max_bytes = max_bytes;
|
|
|
|
cache->max_blocks = getMaxBlocks (max_bytes);
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_formatter_mem_B (buf, cache->max_bytes, sizeof (buf));
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedDbg (MY_NAME, "Maximum cache size set to %s (%d blocks)", buf, cache->max_blocks);
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return cacheTrim (cache);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
int64_t
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cacheGetLimit (const tr_cache * cache)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return cache->max_bytes;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_cache *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cacheNew (int64_t max_bytes)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cache * cache = tr_new0 (tr_cache, 1);
|
|
|
|
cache->blocks = TR_PTR_ARRAY_INIT;
|
|
|
|
cache->max_bytes = max_bytes;
|
|
|
|
cache->max_blocks = getMaxBlocks (max_bytes);
|
|
|
|
return cache;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cacheFree (tr_cache * cache)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (tr_ptrArrayEmpty (&cache->blocks));
|
|
|
|
tr_ptrArrayDestruct (&cache->blocks, NULL);
|
|
|
|
tr_free (cache);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
cache_block_compare (const void * va, const void * vb)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const struct cache_block * a = va;
|
|
|
|
const struct cache_block * b = vb;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* primary key: torrent id */
|
|
|
|
if (a->tor->uniqueId != b->tor->uniqueId)
|
|
|
|
return a->tor->uniqueId < b->tor->uniqueId ? -1 : 1;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* secondary key: block # */
|
|
|
|
if (a->block != b->block)
|
|
|
|
return a->block < b->block ? -1 : 1;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* they're equal */
|
|
|
|
return 0;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct cache_block *
|
2012-12-05 17:29:46 +00:00
|
|
|
findBlock (tr_cache * cache,
|
2010-11-11 15:31:11 +00:00
|
|
|
tr_torrent * torrent,
|
2010-06-19 14:25:11 +00:00
|
|
|
tr_piece_index_t piece,
|
2012-12-05 17:29:46 +00:00
|
|
|
uint32_t offset)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct cache_block key;
|
|
|
|
key.tor = torrent;
|
|
|
|
key.block = _tr_block (torrent, piece, offset);
|
|
|
|
return tr_ptrArrayFindSorted (&cache->blocks, &key, cache_block_compare);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cacheWriteBlock (tr_cache * cache,
|
2010-06-19 14:25:11 +00:00
|
|
|
tr_torrent * torrent,
|
|
|
|
tr_piece_index_t piece,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length,
|
2012-12-05 17:29:46 +00:00
|
|
|
struct evbuffer * writeme)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct cache_block * cb = findBlock (cache, torrent, piece, offset);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2013-01-31 17:39:06 +00:00
|
|
|
assert (tr_amInEventThread (torrent->session));
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (cb == NULL)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
cb = tr_new (struct cache_block, 1);
|
|
|
|
cb->tor = torrent;
|
|
|
|
cb->piece = piece;
|
|
|
|
cb->offset = offset;
|
|
|
|
cb->length = length;
|
|
|
|
cb->block = _tr_block (torrent, piece, offset);
|
|
|
|
cb->evbuf = evbuffer_new ();
|
|
|
|
tr_ptrArrayInsertSorted (&cache->blocks, cb, cache_block_compare);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
cb->time = tr_time ();
|
2010-06-26 16:21:50 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (cb->length == length);
|
|
|
|
evbuffer_drain (cb->evbuf, evbuffer_get_length (cb->evbuf));
|
|
|
|
evbuffer_remove_buffer (writeme, cb->evbuf, cb->length);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
cache->cache_writes++;
|
|
|
|
cache->cache_write_bytes += cb->length;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return cacheTrim (cache);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cacheReadBlock (tr_cache * cache,
|
2010-06-19 14:25:11 +00:00
|
|
|
tr_torrent * torrent,
|
|
|
|
tr_piece_index_t piece,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t len,
|
2012-12-05 17:29:46 +00:00
|
|
|
uint8_t * setme)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int err = 0;
|
|
|
|
struct cache_block * cb = findBlock (cache, torrent, piece, offset);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (cb)
|
|
|
|
evbuffer_copyout (cb->evbuf, setme, len);
|
|
|
|
else
|
|
|
|
err = tr_ioRead (torrent, piece, offset, len, setme);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cachePrefetchBlock (tr_cache * cache,
|
2010-06-19 14:25:11 +00:00
|
|
|
tr_torrent * torrent,
|
|
|
|
tr_piece_index_t piece,
|
|
|
|
uint32_t offset,
|
2012-12-05 17:29:46 +00:00
|
|
|
uint32_t len)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int err = 0;
|
|
|
|
struct cache_block * cb = findBlock (cache, torrent, piece, offset);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (cb == NULL)
|
|
|
|
err = tr_ioPrefetch (torrent, piece, offset, len);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
findBlockPos (tr_cache * cache, tr_torrent * torrent, tr_piece_index_t block)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct cache_block key;
|
|
|
|
key.tor = torrent;
|
|
|
|
key.block = block;
|
|
|
|
return tr_ptrArrayLowerBound (&cache->blocks, &key, cache_block_compare, NULL);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
int tr_cacheFlushDone (tr_cache * cache)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int err = 0;
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tr_ptrArraySize (&cache->blocks) > 0)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int i, n;
|
|
|
|
struct run_info * runs;
|
|
|
|
|
|
|
|
runs = tr_new (struct run_info, tr_ptrArraySize (&cache->blocks));
|
|
|
|
i = 0;
|
|
|
|
n = calcRuns (cache, runs);
|
|
|
|
|
|
|
|
while (i < n && (runs[i].is_piece_done || runs[i].is_multi_piece))
|
|
|
|
runs[i++].rank |= SESSIONFLAG;
|
|
|
|
|
|
|
|
err = flushRuns (cache, runs, i);
|
|
|
|
tr_free (runs);
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return err;
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cacheFlushFile (tr_cache * cache, tr_torrent * torrent, tr_file_index_t i)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int pos;
|
|
|
|
int err = 0;
|
|
|
|
tr_block_index_t first;
|
|
|
|
tr_block_index_t last;
|
|
|
|
|
|
|
|
tr_torGetFileBlockRange (torrent, i, &first, &last);
|
|
|
|
pos = findBlockPos (cache, torrent, first);
|
2013-09-08 17:32:09 +00:00
|
|
|
dbgmsg ("flushing file %d from cache to disk: blocks [%"TR_PRIuSIZE"...%"TR_PRIuSIZE"]", (int)i, (size_t)first, (size_t)last);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
/* flush out all the blocks in that file */
|
|
|
|
while (!err && (pos < tr_ptrArraySize (&cache->blocks)))
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const struct cache_block * b = tr_ptrArrayNth (&cache->blocks, pos);
|
|
|
|
|
|
|
|
if (b->tor != torrent)
|
|
|
|
break;
|
|
|
|
if ((b->block < first) || (b->block > last))
|
|
|
|
break;
|
|
|
|
|
|
|
|
err = flushContiguous (cache, pos, getBlockRun (cache, pos, NULL));
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cacheFlushTorrent (tr_cache * cache, tr_torrent * torrent)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int err = 0;
|
|
|
|
const int pos = findBlockPos (cache, torrent, 0);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* flush out all the blocks in that torrent */
|
|
|
|
while (!err && (pos < tr_ptrArraySize (&cache->blocks)))
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const struct cache_block * b = tr_ptrArrayNth (&cache->blocks, pos);
|
|
|
|
|
|
|
|
if (b->tor != torrent)
|
|
|
|
break;
|
|
|
|
|
|
|
|
err = flushContiguous (cache, pos, getBlockRun (cache, pos, NULL));
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|