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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-21 07:40:57 +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"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.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"
|
|
|
|
|
2017-05-22 20:12:57 +00:00
|
|
|
#define dbgmsg(...) tr_logAddDeepNamed(MY_NAME, __VA_ARGS__)
|
2010-07-01 16:26:30 +00:00
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
struct cache_block
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_piece_index_t piece;
|
|
|
|
uint32_t offset;
|
|
|
|
uint32_t length;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
time_t time;
|
|
|
|
tr_block_index_t block;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* evbuf;
|
2010-06-19 14:25:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_cache
|
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int pos;
|
|
|
|
int rank;
|
|
|
|
time_t last_block_time;
|
|
|
|
bool is_multi_piece;
|
|
|
|
bool is_piece_done;
|
|
|
|
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 */
|
2017-04-20 16:02:19 +00:00
|
|
|
static int getBlockRun(tr_cache const* cache, int pos, struct run_info* info)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const n = tr_ptrArraySize(&cache->blocks);
|
|
|
|
struct cache_block const* const* blocks = (struct cache_block const* const*)tr_ptrArrayBase(&cache->blocks);
|
|
|
|
struct cache_block const* ref = blocks[pos];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_block_index_t block = ref->block;
|
2017-05-13 22:38:31 +00:00
|
|
|
int len = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
for (int i = pos; i < n; ++i)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct cache_block const* b = blocks[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (b->block != block)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b->tor != ref->tor)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2020-11-08 19:54:40 +00:00
|
|
|
|
|
|
|
++block;
|
|
|
|
++len;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (info != NULL)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
struct cache_block const* b = blocks[pos + len - 1];
|
2017-04-19 12:04:45 +00:00
|
|
|
info->last_block_time = b->time;
|
|
|
|
info->is_piece_done = tr_torrentPieceIsComplete(b->tor, b->piece);
|
|
|
|
info->is_multi_piece = b->piece != blocks[pos]->piece;
|
2017-05-13 22:38:31 +00:00
|
|
|
info->len = len;
|
2017-04-19 12:04:45 +00:00
|
|
|
info->pos = pos;
|
2010-06-26 16:21:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
return len;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2011-03-16 00:49:43 +00:00
|
|
|
/* higher rank comes before lower rank */
|
2017-04-20 16:02:19 +00:00
|
|
|
static int compareRuns(void const* va, void const* vb)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct run_info const* a = va;
|
|
|
|
struct run_info const* b = vb;
|
2017-04-19 12:04:45 +00:00
|
|
|
return b->rank - a->rank;
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
*/
|
2020-11-05 22:46:21 +00:00
|
|
|
static int calcRuns(tr_cache const* cache, struct run_info* runs)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const n = tr_ptrArraySize(&cache->blocks);
|
2017-05-01 15:46:41 +00:00
|
|
|
int i = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int pos = 0; pos < n; pos += runs[i++].len)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int rank = getBlockRun(cache, pos, &runs[i]);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* 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
|
|
|
|
2017-04-19 12:04:45 +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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Move the multi piece runs higher */
|
|
|
|
rank |= runs[i].is_multi_piece ? MULTIFLAG : 0;
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
runs[i].rank = rank;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
qsort(runs, i, sizeof(struct run_info), compareRuns);
|
|
|
|
return i;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int flushContiguous(tr_cache* cache, int pos, int n)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
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;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_piece_index_t const piece = b->piece;
|
|
|
|
uint32_t const offset = b->offset;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
b = blocks[pos + i];
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArrayErase(&cache->blocks, pos, pos + n);
|
|
|
|
|
|
|
|
err = tr_ioWrite(tor, piece, offset, walk - buf, buf);
|
|
|
|
tr_free(buf);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
++cache->disk_writes;
|
|
|
|
cache->disk_write_bytes += walk - buf;
|
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int flushRuns(tr_cache* cache, struct run_info* runs, int n)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err = 0;
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; err == 0 && i < n; i++)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
err = flushContiguous(cache, runs[i].pos, runs[i].len);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int j = i + 1; j < n; j++)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (runs[j].pos > runs[i].pos)
|
|
|
|
{
|
|
|
|
runs[j].pos -= runs[i].len;
|
|
|
|
}
|
|
|
|
}
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return err;
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int cacheTrim(tr_cache* cache)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err = 0;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_ptrArraySize(&cache->blocks) > cache->max_blocks)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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. */
|
2017-04-20 16:02:19 +00:00
|
|
|
int const cacheCutoff = 1 + cache->max_blocks / 4;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct run_info* runs = tr_new(struct run_info, tr_ptrArraySize(&cache->blocks));
|
2017-05-01 15:46:41 +00:00
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int getMaxBlocks(int64_t max_bytes)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return max_bytes / (double)MAX_BLOCK_SIZE;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_cacheSetLimit(tr_cache* cache, int64_t max_bytes)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char buf[128];
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
cache->max_bytes = max_bytes;
|
|
|
|
cache->max_blocks = getMaxBlocks(max_bytes);
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_formatter_mem_B(buf, cache->max_bytes, sizeof(buf));
|
|
|
|
tr_logAddNamedDbg(MY_NAME, "Maximum cache size set to %s (%d blocks)", buf, cache->max_blocks);
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return cacheTrim(cache);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int64_t tr_cacheGetLimit(tr_cache const* cache)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return cache->max_bytes;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cache* tr_cacheNew(int64_t max_bytes)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_cacheFree(tr_cache* cache)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_ptrArrayEmpty(&cache->blocks));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArrayDestruct(&cache->blocks, NULL);
|
|
|
|
tr_free(cache);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int cache_block_compare(void const* va, void const* vb)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct cache_block const* a = va;
|
|
|
|
struct cache_block const* b = vb;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +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
|
|
|
|
2017-04-19 12:04:45 +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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* they're equal */
|
|
|
|
return 0;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static struct cache_block* findBlock(tr_cache* cache, tr_torrent* torrent, tr_piece_index_t piece, uint32_t offset)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
int tr_cacheWriteBlock(
|
|
|
|
tr_cache* cache,
|
|
|
|
tr_torrent* torrent,
|
|
|
|
tr_piece_index_t piece,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t length,
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* writeme)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_amInEventThread(torrent->session));
|
2013-01-31 17:39:06 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct cache_block* cb = findBlock(cache, torrent, piece, offset);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (cb == NULL)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
}
|
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(cb->length == length);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
cb->time = tr_time();
|
2010-06-26 16:21:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
cache->cache_writes++;
|
|
|
|
cache->cache_write_bytes += cb->length;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return cacheTrim(cache);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
int tr_cacheReadBlock(
|
|
|
|
tr_cache* cache,
|
|
|
|
tr_torrent* torrent,
|
|
|
|
tr_piece_index_t piece,
|
|
|
|
uint32_t offset,
|
|
|
|
uint32_t len,
|
2017-04-19 12:04:45 +00:00
|
|
|
uint8_t* setme)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err = 0;
|
|
|
|
struct cache_block* cb = findBlock(cache, torrent, piece, offset);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (cb != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_copyout(cb->evbuf, setme, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = tr_ioRead(torrent, piece, offset, len, setme);
|
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_cachePrefetchBlock(tr_cache* cache, tr_torrent* torrent, tr_piece_index_t piece, uint32_t offset, uint32_t len)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err = 0;
|
2020-11-09 03:31:02 +00:00
|
|
|
struct cache_block const* const cb = findBlock(cache, torrent, piece, offset);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (cb == NULL)
|
|
|
|
{
|
|
|
|
err = tr_ioPrefetch(torrent, piece, offset, len);
|
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
static int findBlockPos(tr_cache const* cache, tr_torrent* torrent, tr_piece_index_t block)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_cacheFlushDone(tr_cache* cache)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err = 0;
|
2010-07-08 17:38:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_ptrArraySize(&cache->blocks) > 0)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
int i;
|
|
|
|
int n;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct run_info* runs;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
runs = tr_new(struct run_info, tr_ptrArraySize(&cache->blocks));
|
|
|
|
i = 0;
|
|
|
|
n = calcRuns(cache, runs);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (i < n && (runs[i].is_piece_done || runs[i].is_multi_piece))
|
|
|
|
{
|
|
|
|
runs[i++].rank |= SESSIONFLAG;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
err = flushRuns(cache, runs, i);
|
|
|
|
tr_free(runs);
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return err;
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_cacheFlushFile(tr_cache* cache, tr_torrent* torrent, tr_file_index_t i)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int pos;
|
|
|
|
int err = 0;
|
|
|
|
tr_block_index_t first;
|
|
|
|
tr_block_index_t last;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torGetFileBlockRange(torrent, i, &first, &last);
|
|
|
|
pos = findBlockPos(cache, torrent, first);
|
|
|
|
dbgmsg("flushing file %d from cache to disk: blocks [%zu...%zu]", (int)i, (size_t)first, (size_t)last);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* flush out all the blocks in that file */
|
2017-04-30 16:25:26 +00:00
|
|
|
while (err == 0 && pos < tr_ptrArraySize(&cache->blocks))
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct cache_block const* b = tr_ptrArrayNth(&cache->blocks, pos);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (b->tor != torrent)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (b->block < first || b->block > last)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
err = flushContiguous(cache, pos, getBlockRun(cache, pos, NULL));
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_cacheFlushTorrent(tr_cache* cache, tr_torrent* torrent)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const pos = findBlockPos(cache, torrent, 0);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* flush out all the blocks in that torrent */
|
2017-04-30 16:25:26 +00:00
|
|
|
while (err == 0 && pos < tr_ptrArraySize(&cache->blocks))
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct cache_block const* b = tr_ptrArrayNth(&cache->blocks, pos);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (b->tor != torrent)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
err = flushContiguous(cache, pos, getBlockRun(cache, pos, NULL));
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|