2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright 2010-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// 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
|
|
|
|
2022-08-18 14:14:12 +00:00
|
|
|
#include <algorithm>
|
2022-06-12 00:46:30 +00:00
|
|
|
#include <cstdlib> // std::lldiv()
|
|
|
|
#include <iterator> // std::distance(), std::next(), std::prev()
|
|
|
|
#include <limits> // std::numeric_limits<size_t>::max()
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory>
|
2022-06-12 00:46:30 +00:00
|
|
|
#include <numeric> // std::accumulate()
|
|
|
|
#include <utility> // std::make_pair()
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <vector>
|
2011-03-16 18:04:23 +00:00
|
|
|
|
2022-03-15 14:52:16 +00:00
|
|
|
#include <fmt/core.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 "torrent.h"
|
2022-06-12 00:46:30 +00:00
|
|
|
#include "torrents.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2022-08-04 13:44:18 +00:00
|
|
|
#include "utils.h" // tr_time(), tr_formatter
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
Cache::Key Cache::makeKey(tr_torrent const* torrent, tr_block_info::Location loc) noexcept
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-06-17 15:43:04 +00:00
|
|
|
return std::make_pair(torrent->id(), loc.block);
|
2022-06-12 00:46:30 +00:00
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
std::pair<Cache::CIter, Cache::CIter> Cache::findContiguous(CIter const begin, CIter const end, CIter const iter) noexcept
|
2010-06-26 16:21:50 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
if (iter == end)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
return std::make_pair(end, end);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
auto span_begin = iter;
|
|
|
|
for (auto key = iter->key;;)
|
|
|
|
{
|
|
|
|
if (span_begin == begin)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
--key.second;
|
|
|
|
auto const prev = std::prev(span_begin);
|
|
|
|
if (prev->key != key)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
auto span_end = std::next(iter);
|
|
|
|
for (auto key = iter->key;;)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
if (span_end == end)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
++key.second;
|
|
|
|
if (span_end->key != key)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
return std::make_pair(span_begin, span_end);
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
int Cache::writeContiguous(CIter const begin, CIter const end) const
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2023-01-30 18:04:40 +00:00
|
|
|
// The most common case without an extra data copy.
|
|
|
|
auto const* towrite = begin->buf.get();
|
|
|
|
|
|
|
|
// Contiguous area to join more than one block, if any.
|
2022-06-12 00:46:30 +00:00
|
|
|
auto buf = std::vector<uint8_t>{};
|
2023-01-30 18:04:40 +00:00
|
|
|
|
|
|
|
if (end - begin > 1)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2023-01-30 18:04:40 +00:00
|
|
|
// Yes, there are.
|
|
|
|
auto const buflen = std::accumulate(
|
|
|
|
begin,
|
|
|
|
end,
|
|
|
|
size_t{},
|
|
|
|
[](size_t sum, auto const& block) { return sum + std::size(*block.buf); });
|
|
|
|
buf.reserve(buflen);
|
|
|
|
for (auto iter = begin; iter != end; ++iter)
|
|
|
|
{
|
|
|
|
TR_ASSERT(begin->key.first == iter->key.first);
|
|
|
|
TR_ASSERT(begin->key.second + std::distance(begin, iter) == iter->key.second);
|
|
|
|
buf.insert(std::end(buf), std::begin(*iter->buf), std::end(*iter->buf));
|
|
|
|
}
|
|
|
|
TR_ASSERT(std::size(buf) == buflen);
|
|
|
|
towrite = &buf;
|
2022-06-12 00:46:30 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
// save it
|
2022-08-26 19:46:46 +00:00
|
|
|
auto const& [torrent_id, block] = begin->key;
|
|
|
|
auto* const tor = torrents_.get(torrent_id);
|
|
|
|
if (tor == nullptr)
|
|
|
|
{
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const loc = tor->blockLoc(block);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-01-30 18:04:40 +00:00
|
|
|
if (auto const err = tr_ioWrite(tor, loc, std::size(*towrite), std::data(*towrite)); err != 0)
|
2022-06-12 00:46:30 +00:00
|
|
|
{
|
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
++disk_writes_;
|
2023-01-30 18:04:40 +00:00
|
|
|
disk_write_bytes_ += std::size(*towrite);
|
2022-06-12 00:46:30 +00:00
|
|
|
return {};
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
size_t Cache::getMaxBlocks(int64_t max_bytes) noexcept
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-03-24 05:18:41 +00:00
|
|
|
return std::lldiv(max_bytes, tr_block_info::BlockSize).quot;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
int Cache::setLimit(int64_t new_limit)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
max_bytes_ = new_limit;
|
|
|
|
max_blocks_ = getMaxBlocks(new_limit);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
tr_logAddDebug(fmt::format("Maximum cache size set to {} ({} blocks)", tr_formatter_mem_B(max_bytes_), max_blocks_));
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
return cacheTrim();
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
Cache::Cache(tr_torrents& torrents, int64_t max_bytes)
|
|
|
|
: torrents_{ torrents }
|
|
|
|
, max_blocks_(getMaxBlocks(max_bytes))
|
|
|
|
, max_bytes_(max_bytes)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-02-14 19:50:28 +00:00
|
|
|
int Cache::writeBlock(tr_torrent_id_t tor_id, tr_block_index_t block, std::unique_ptr<std::vector<uint8_t>> writeme)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-06-20 04:08:58 +00:00
|
|
|
auto const key = Key{ tor_id, block };
|
2022-06-12 00:46:30 +00:00
|
|
|
auto iter = std::lower_bound(std::begin(blocks_), std::end(blocks_), key, CompareCacheBlockByKey{});
|
|
|
|
if (iter == std::end(blocks_) || iter->key != key)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
iter = blocks_.emplace(iter);
|
|
|
|
iter->key = key;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
iter->time_added = tr_time();
|
2010-06-26 16:21:50 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
iter->buf = std::move(writeme);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
++cache_writes_;
|
2022-06-20 04:08:58 +00:00
|
|
|
cache_write_bytes_ += std::size(*iter->buf);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-10-24 21:57:07 +00:00
|
|
|
return cacheTrim();
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
Cache::CIter Cache::getBlock(tr_torrent const* torrent, tr_block_info::Location loc) noexcept
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
if (auto const [begin, end] = std::equal_range(
|
|
|
|
std::begin(blocks_),
|
|
|
|
std::end(blocks_),
|
|
|
|
makeKey(torrent, loc),
|
|
|
|
CompareCacheBlockByKey{});
|
|
|
|
begin < end)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
return begin;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-06-12 00:46:30 +00:00
|
|
|
|
|
|
|
return std::end(blocks_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Cache::readBlock(tr_torrent* torrent, tr_block_info::Location loc, uint32_t len, uint8_t* setme)
|
|
|
|
{
|
|
|
|
if (auto const iter = getBlock(torrent, loc); iter != std::end(blocks_))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-06-20 04:08:58 +00:00
|
|
|
std::copy_n(std::begin(*iter->buf), len, setme);
|
2022-06-12 00:46:30 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
return tr_ioRead(torrent, loc, len, setme);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
int Cache::prefetchBlock(tr_torrent* torrent, tr_block_info::Location loc, uint32_t len)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
if (auto const iter = getBlock(torrent, loc); iter != std::end(blocks_))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
return {}; // already have it
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
return tr_ioPrefetch(torrent, loc, len);
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
int Cache::flushSpan(CIter const begin, CIter const end)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
for (auto walk = begin; walk < end;)
|
2010-07-08 17:38:11 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
auto const [contig_begin, contig_end] = findContiguous(begin, end, walk);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
if (auto const err = writeContiguous(contig_begin, contig_end); err != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
return err;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
walk = contig_end;
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
blocks_.erase(begin, end);
|
|
|
|
return {};
|
2010-07-08 17:38:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 17:52:58 +00:00
|
|
|
int Cache::flushFile(tr_torrent const* torrent, tr_file_index_t file)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
auto const compare = CompareCacheBlockByKey{};
|
2022-06-17 15:43:04 +00:00
|
|
|
auto const tor_id = torrent->id();
|
2022-08-03 06:15:37 +00:00
|
|
|
auto const [block_begin, block_end] = tr_torGetFileBlockSpan(torrent, file);
|
2021-10-23 15:43:15 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
return flushSpan(
|
|
|
|
std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id, block_begin), compare),
|
|
|
|
std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id, block_end), compare));
|
|
|
|
}
|
2022-03-15 14:52:16 +00:00
|
|
|
|
2022-09-06 17:52:58 +00:00
|
|
|
int Cache::flushTorrent(tr_torrent const* torrent)
|
2022-06-12 00:46:30 +00:00
|
|
|
{
|
|
|
|
auto const compare = CompareCacheBlockByKey{};
|
2022-06-17 15:43:04 +00:00
|
|
|
auto const tor_id = torrent->id();
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
return flushSpan(
|
|
|
|
std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id, 0), compare),
|
|
|
|
std::lower_bound(std::begin(blocks_), std::end(blocks_), std::make_pair(tor_id + 1, 0), compare));
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
int Cache::flushOldest()
|
|
|
|
{
|
|
|
|
auto const oldest = std::min_element(
|
|
|
|
std::begin(blocks_),
|
|
|
|
std::end(blocks_),
|
|
|
|
[](auto const& a, auto const& b) { return a.time_added < b.time_added; });
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
if (oldest == std::end(blocks_)) // nothing to flush
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const [begin, end] = findContiguous(std::begin(blocks_), std::end(blocks_), oldest);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
if (auto const err = writeContiguous(begin, end); err != 0)
|
|
|
|
{
|
|
|
|
return err;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
blocks_.erase(begin, end);
|
|
|
|
return 0;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
int Cache::cacheTrim()
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
while (std::size(blocks_) > max_blocks_)
|
2010-06-19 14:25:11 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
if (auto const err = flushOldest(); err != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-06-12 00:46:30 +00:00
|
|
|
return err;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
return 0;
|
2010-06-19 14:25:11 +00:00
|
|
|
}
|