2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright 2010-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cstdlib> /* qsort() */
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <ctime>
|
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"
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
static char constexpr MyName[] = "Cache";
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
#define dbgmsg(...) tr_logAddDeepNamed(MyName, __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);
|
2022-01-17 18:39:50 +00:00
|
|
|
auto const* const* blocks = (struct cache_block const* const*)tr_ptrArrayBase(&cache->blocks);
|
2017-04-20 16:02:19 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (info != nullptr)
|
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;
|
2021-11-25 18:26:51 +00:00
|
|
|
info->is_piece_done = b->tor->hasPiece(b->piece);
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
{
|
2022-01-23 22:47:41 +00:00
|
|
|
auto const* const a = static_cast<struct run_info const*>(va);
|
|
|
|
auto const* const b = static_cast<struct run_info const*>(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
|
|
|
*/
|
2021-10-23 15:43:15 +00:00
|
|
|
// TODO: return std::vector
|
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;
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* const buf = tr_new(uint8_t, n * MAX_BLOCK_SIZE);
|
|
|
|
auto* walk = buf;
|
|
|
|
auto** blocks = (struct cache_block**)tr_ptrArrayBase(&cache->blocks);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
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;
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* const 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
|
|
|
cache->max_bytes = max_bytes;
|
|
|
|
cache->max_blocks = getMaxBlocks(max_bytes);
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
tr_logAddNamedDbg(
|
2022-01-17 18:39:50 +00:00
|
|
|
MyName,
|
2021-12-31 00:30:21 +00:00
|
|
|
"Maximum cache size set to %s (%d blocks)",
|
|
|
|
tr_formatter_mem_B(cache->max_bytes).c_str(),
|
|
|
|
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
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* const cache = tr_new0(tr_cache, 1);
|
2021-09-12 17:41:49 +00:00
|
|
|
cache->blocks = {};
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_ptrArrayDestruct(&cache->blocks, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* a = static_cast<struct cache_block const*>(va);
|
|
|
|
auto const* b = static_cast<struct cache_block const*>(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;
|
2021-11-24 14:48:52 +00:00
|
|
|
key.block = torrent->blockOf(piece, offset);
|
2021-09-12 17:41:49 +00:00
|
|
|
return static_cast<struct cache_block*>(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);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cb == nullptr)
|
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;
|
2021-11-24 14:48:52 +00:00
|
|
|
cb->block = torrent->blockOf(piece, offset);
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cb != nullptr)
|
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
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cb == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
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;
|
2021-09-15 00:18:09 +00:00
|
|
|
return tr_ptrArrayLowerBound(&cache->blocks, &key, cache_block_compare, nullptr);
|
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
|
|
|
{
|
2021-10-23 15:43:15 +00:00
|
|
|
auto* const runs = tr_new(struct run_info, tr_ptrArraySize(&cache->blocks));
|
|
|
|
int i = 0;
|
|
|
|
int const 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
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
auto const [begin, end] = tr_torGetFileBlockSpan(torrent, i);
|
2021-10-23 15:43:15 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
int pos = findBlockPos(cache, torrent, begin);
|
|
|
|
dbgmsg("flushing file %d from cache to disk: blocks [%zu...%zu)", (int)i, (size_t)begin, (size_t)end);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* flush out all the blocks in that file */
|
2021-10-23 15:43:15 +00:00
|
|
|
int err = 0;
|
2017-04-30 16:25:26 +00:00
|
|
|
while (err == 0 && pos < tr_ptrArraySize(&cache->blocks))
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* b = static_cast<struct cache_block const*>(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
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
if (b->block < begin || b->block >= end)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
err = flushContiguous(cache, pos, getBlockRun(cache, pos, nullptr));
|
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
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* b = static_cast<struct cache_block const*>(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
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
err = flushContiguous(cache, pos, getBlockRun(cache, pos, nullptr));
|
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
|
|
|
}
|