2007-06-10 22:46:09 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2007-2018, Arvid Norberg, Steven Siloti
|
2007-06-10 22:46:09 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-12-17 23:07:03 +01:00
|
|
|
#ifndef TORRENT_DISK_IO_THREAD
|
|
|
|
#define TORRENT_DISK_IO_THREAD
|
|
|
|
|
2015-04-19 00:00:27 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
2018-04-26 09:01:14 +02:00
|
|
|
#include "libtorrent/fwd.hpp"
|
2017-06-14 09:55:23 +02:00
|
|
|
#include "libtorrent/debug.hpp"
|
2007-06-10 22:46:09 +02:00
|
|
|
#include "libtorrent/storage.hpp"
|
2009-01-15 18:09:36 +01:00
|
|
|
#include "libtorrent/allocator.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/io_service.hpp"
|
2010-03-03 08:09:04 +01:00
|
|
|
#include "libtorrent/sliding_average.hpp"
|
2016-06-16 02:49:28 +02:00
|
|
|
#include "libtorrent/disk_io_thread_pool.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/disk_io_job.hpp"
|
|
|
|
#include "libtorrent/disk_job_pool.hpp"
|
|
|
|
#include "libtorrent/block_cache.hpp"
|
|
|
|
#include "libtorrent/file_pool.hpp"
|
|
|
|
#include "libtorrent/disk_interface.hpp"
|
2014-08-01 08:07:48 +02:00
|
|
|
#include "libtorrent/performance_counters.hpp"
|
2015-04-19 00:00:27 +02:00
|
|
|
#include "libtorrent/aux_/session_settings.hpp"
|
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2016-05-01 09:38:35 +02:00
|
|
|
#include <atomic>
|
2016-09-01 03:42:18 +02:00
|
|
|
#include <memory>
|
2016-09-01 21:04:58 +02:00
|
|
|
#include <vector>
|
2016-09-01 03:42:18 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
struct counters;
|
2016-09-01 21:04:58 +02:00
|
|
|
class alert_manager;
|
2010-01-27 05:25:45 +01:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace aux {
|
|
|
|
|
2016-11-18 06:25:27 +01:00
|
|
|
struct block_cache_reference;
|
|
|
|
}
|
|
|
|
|
2008-02-08 11:22:05 +01:00
|
|
|
struct cached_piece_info
|
|
|
|
{
|
2016-11-13 03:45:30 +01:00
|
|
|
storage_interface* storage;
|
2013-07-21 07:05:21 +02:00
|
|
|
|
|
|
|
// holds one entry for each block in this piece. ``true`` represents
|
|
|
|
// the data for that block being in the disk cache and ``false`` means it's not.
|
2008-02-08 11:22:05 +01:00
|
|
|
std::vector<bool> blocks;
|
2013-07-21 07:05:21 +02:00
|
|
|
|
|
|
|
// the time when a block was last written to this piece. The older
|
|
|
|
// a piece is, the more likely it is to be flushed to disk.
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point last_use;
|
2013-07-21 07:05:21 +02:00
|
|
|
|
|
|
|
// The index of the next block that needs to be hashed.
|
|
|
|
// Blocks are hashed as they are downloaded in order to not
|
|
|
|
// have to re-read them from disk once the piece is complete, to
|
|
|
|
// compare its hash against the hashes in the .torrent file.
|
2011-03-20 06:47:27 +01:00
|
|
|
int next_to_hash;
|
2013-07-21 07:05:21 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the piece index for this cache entry.
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t piece;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
enum kind_t { read_cache = 0, write_cache = 1, volatile_read_cache = 2 };
|
2013-07-21 07:05:21 +02:00
|
|
|
|
|
|
|
// specifies if this piece is part of the read cache or the write cache.
|
2008-07-11 12:29:26 +02:00
|
|
|
kind_t kind;
|
2013-11-26 03:00:02 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
bool need_readback;
|
2007-06-10 22:46:09 +02:00
|
|
|
};
|
2015-05-18 07:32:18 +02:00
|
|
|
|
2018-03-22 17:01:38 +01:00
|
|
|
using jobqueue_t = tailqueue<disk_io_job>;
|
2015-08-19 15:22:00 +02:00
|
|
|
|
2013-11-19 18:57:16 +01:00
|
|
|
// this struct holds a number of statistics counters
|
|
|
|
// relevant for the disk io thread and disk cache.
|
2013-08-04 11:02:19 +02:00
|
|
|
struct TORRENT_EXPORT cache_status
|
2008-02-08 11:22:05 +01:00
|
|
|
{
|
2013-11-20 19:02:48 +01:00
|
|
|
// initializes all counters to 0
|
2008-02-22 05:11:04 +01:00
|
|
|
cache_status()
|
2014-08-01 08:07:48 +02:00
|
|
|
: pieces()
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2014-08-01 08:07:48 +02:00
|
|
|
, blocks_written(0)
|
2008-02-22 05:11:04 +01:00
|
|
|
, writes(0)
|
|
|
|
, blocks_read(0)
|
|
|
|
, blocks_read_hit(0)
|
|
|
|
, reads(0)
|
2011-02-21 06:24:41 +01:00
|
|
|
, queued_bytes(0)
|
2008-02-22 05:11:04 +01:00
|
|
|
, cache_size(0)
|
2014-07-06 21:18:00 +02:00
|
|
|
, write_cache_size(0)
|
2008-02-22 05:11:04 +01:00
|
|
|
, read_cache_size(0)
|
2014-07-06 21:18:00 +02:00
|
|
|
, pinned_blocks(0)
|
2009-05-02 08:52:57 +02:00
|
|
|
, total_used_buffers(0)
|
2010-03-04 04:43:50 +01:00
|
|
|
, average_read_time(0)
|
2011-03-11 08:37:12 +01:00
|
|
|
, average_write_time(0)
|
2011-03-14 06:21:46 +01:00
|
|
|
, average_hash_time(0)
|
2011-03-18 04:07:10 +01:00
|
|
|
, average_job_time(0)
|
2011-03-19 23:23:58 +01:00
|
|
|
, cumulative_job_time(0)
|
|
|
|
, cumulative_read_time(0)
|
|
|
|
, cumulative_write_time(0)
|
|
|
|
, cumulative_hash_time(0)
|
2011-03-20 02:19:14 +01:00
|
|
|
, total_read_back(0)
|
2011-03-20 10:04:03 +01:00
|
|
|
, read_queue_size(0)
|
2014-07-06 21:18:00 +02:00
|
|
|
, blocked_jobs(0)
|
|
|
|
, queued_jobs(0)
|
|
|
|
, peak_queued(0)
|
|
|
|
, pending_jobs(0)
|
|
|
|
, num_jobs(0)
|
|
|
|
, num_read_jobs(0)
|
|
|
|
, num_write_jobs(0)
|
|
|
|
, arc_mru_size(0)
|
|
|
|
, arc_mru_ghost_size(0)
|
|
|
|
, arc_mfu_size(0)
|
|
|
|
, arc_mfu_ghost_size(0)
|
|
|
|
, arc_write_size(0)
|
|
|
|
, arc_volatile_size(0)
|
|
|
|
, num_writing_threads(0)
|
2014-10-25 22:07:50 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2017-01-26 17:13:40 +01:00
|
|
|
std::memset(num_fence_jobs, 0, sizeof(num_fence_jobs));
|
2014-10-25 22:59:54 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<cached_piece_info> pieces;
|
2008-02-22 05:11:04 +01:00
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2013-07-21 07:05:21 +02:00
|
|
|
// the total number of 16 KiB blocks written to disk
|
|
|
|
// since this session was started.
|
2014-08-01 09:32:54 +02:00
|
|
|
int blocks_written;
|
2013-07-21 07:05:21 +02:00
|
|
|
|
|
|
|
// the total number of write operations performed since this
|
|
|
|
// session was started.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-07-21 07:05:21 +02:00
|
|
|
// The ratio (``blocks_written`` - ``writes``) / ``blocks_written`` represents
|
|
|
|
// the number of saved write operations per total write operations. i.e. a kind
|
|
|
|
// of cache hit ratio for the write cahe.
|
2014-08-01 09:32:54 +02:00
|
|
|
int writes;
|
2008-02-22 05:11:04 +01:00
|
|
|
|
2013-07-21 07:05:21 +02:00
|
|
|
// the number of blocks that were requested from the
|
|
|
|
// bittorrent engine (from peers), that were served from disk or cache.
|
2014-08-01 09:32:54 +02:00
|
|
|
int blocks_read;
|
2013-07-21 07:05:21 +02:00
|
|
|
|
2008-02-22 05:11:04 +01:00
|
|
|
// the number of blocks that was just copied from the read cache
|
2013-07-21 07:05:21 +02:00
|
|
|
//
|
|
|
|
// The ratio ``blocks_read_hit`` / ``blocks_read`` is the cache hit ratio
|
|
|
|
// for the read cache.
|
2014-08-01 09:32:54 +02:00
|
|
|
int blocks_read_hit;
|
2013-07-21 07:05:21 +02:00
|
|
|
|
2008-02-22 05:11:04 +01:00
|
|
|
// the number of read operations used
|
2014-08-01 09:32:54 +02:00
|
|
|
int reads;
|
2008-02-22 05:11:04 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the number of bytes queued for writing, including bytes
|
|
|
|
// submitted to the OS for writing, but not yet complete
|
2016-06-18 20:01:38 +02:00
|
|
|
mutable std::int64_t queued_bytes;
|
2009-07-21 06:32:27 +02:00
|
|
|
|
2013-07-21 07:05:21 +02:00
|
|
|
// the number of 16 KiB blocks currently in the disk cache (both read and write).
|
|
|
|
// This includes both read and write cache.
|
2008-02-22 05:11:04 +01:00
|
|
|
int cache_size;
|
2014-10-25 22:07:50 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the number of blocks in the cache used for write cache
|
|
|
|
int write_cache_size;
|
2008-02-22 05:11:04 +01:00
|
|
|
|
2013-07-21 07:05:21 +02:00
|
|
|
// the number of 16KiB blocks in the read cache.
|
2008-02-22 05:11:04 +01:00
|
|
|
int read_cache_size;
|
2009-05-02 08:52:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the number of blocks with a refcount > 0, i.e.
|
|
|
|
// they may not be evicted
|
|
|
|
int pinned_blocks;
|
|
|
|
|
2013-07-21 07:05:21 +02:00
|
|
|
// the total number of buffers currently in use.
|
|
|
|
// This includes the read/write disk cache as well as send and receive buffers
|
|
|
|
// used in peer connections.
|
2017-01-13 13:52:57 +01:00
|
|
|
// deprecated, use session_stats_metrics "disk.disk_blocks_in_use"
|
2009-05-02 08:52:57 +02:00
|
|
|
mutable int total_used_buffers;
|
2010-03-03 08:09:04 +01:00
|
|
|
|
2013-07-21 07:05:21 +02:00
|
|
|
// the number of microseconds an average disk I/O job
|
|
|
|
// has to wait in the job queue before it get processed.
|
|
|
|
|
|
|
|
// the time read jobs takes on average to complete
|
|
|
|
// (not including the time in the queue), in microseconds. This only measures
|
|
|
|
// read cache misses.
|
2010-03-03 08:09:04 +01:00
|
|
|
int average_read_time;
|
2013-07-21 07:05:21 +02:00
|
|
|
|
|
|
|
// the time write jobs takes to complete, on average,
|
|
|
|
// in microseconds. This does not include the time the job sits in the disk job
|
|
|
|
// queue or in the write cache, only blocks that are flushed to disk.
|
2011-03-11 08:37:12 +01:00
|
|
|
int average_write_time;
|
2013-07-21 07:05:21 +02:00
|
|
|
|
|
|
|
// the time hash jobs takes to complete on average, in
|
|
|
|
// microseconds. Hash jobs include running SHA-1 on the data (which for the most
|
|
|
|
// part is done incrementally) and sometimes reading back parts of the piece. It
|
|
|
|
// also includes checking files without valid resume data.
|
2011-03-14 06:21:46 +01:00
|
|
|
int average_hash_time;
|
2011-03-18 04:07:10 +01:00
|
|
|
int average_job_time;
|
2011-03-19 23:23:58 +01:00
|
|
|
|
2013-11-19 18:57:16 +01:00
|
|
|
// the number of milliseconds spent in all disk jobs, and specific ones
|
|
|
|
// since the start of the session. Times are specified in milliseconds
|
2014-08-01 09:32:54 +02:00
|
|
|
int cumulative_job_time;
|
|
|
|
int cumulative_read_time;
|
|
|
|
int cumulative_write_time;
|
|
|
|
int cumulative_hash_time;
|
2013-11-19 18:57:16 +01:00
|
|
|
|
2014-02-13 08:28:48 +01:00
|
|
|
// the number of blocks that had to be read back from disk because
|
2013-11-19 18:57:16 +01:00
|
|
|
// they were flushed before the SHA-1 hash got to hash them. If this
|
|
|
|
// is large, a larger cache could significantly improve performance
|
2011-03-20 02:19:14 +01:00
|
|
|
int total_read_back;
|
2013-11-19 18:57:16 +01:00
|
|
|
|
|
|
|
// number of read jobs in the disk job queue
|
2011-03-20 10:04:03 +01:00
|
|
|
int read_queue_size;
|
2015-08-20 01:33:20 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// number of jobs blocked because of a fence
|
|
|
|
int blocked_jobs;
|
|
|
|
|
|
|
|
// number of jobs waiting to be issued (m_to_issue)
|
|
|
|
// average over 30 seconds
|
2017-01-13 13:52:57 +01:00
|
|
|
// deprecated, use session_stats_metrics "disk.queued_disk_jobs"
|
2014-07-06 21:18:00 +02:00
|
|
|
int queued_jobs;
|
2014-10-25 22:07:50 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// largest ever seen number of queued jobs
|
|
|
|
int peak_queued;
|
2014-10-25 22:07:50 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// number of jobs waiting to complete (m_pending)
|
|
|
|
// average over 30 seconds
|
|
|
|
int pending_jobs;
|
|
|
|
|
|
|
|
// total number of disk job objects allocated right now
|
|
|
|
int num_jobs;
|
|
|
|
|
|
|
|
// total number of disk read job objects allocated right now
|
|
|
|
int num_read_jobs;
|
|
|
|
|
|
|
|
// total number of disk write job objects allocated right now
|
|
|
|
int num_write_jobs;
|
|
|
|
|
|
|
|
// ARC cache stats. All of these counters are in number of pieces
|
|
|
|
// not blocks. A piece does not necessarily correspond to a certain
|
|
|
|
// number of blocks. The pieces in the ghost list never have any
|
|
|
|
// blocks in them
|
|
|
|
int arc_mru_size;
|
|
|
|
int arc_mru_ghost_size;
|
|
|
|
int arc_mfu_size;
|
|
|
|
int arc_mfu_ghost_size;
|
|
|
|
int arc_write_size;
|
|
|
|
int arc_volatile_size;
|
|
|
|
|
|
|
|
// the number of threads currently writing to disk
|
|
|
|
int num_writing_threads;
|
|
|
|
|
|
|
|
// counts only fence jobs that are currently blocking jobs
|
|
|
|
// not fences that are themself blocked
|
2017-06-11 19:53:15 +02:00
|
|
|
int num_fence_jobs[static_cast<int>(job_action_t::num_job_ids)];
|
2014-10-25 22:59:54 +02:00
|
|
|
#endif
|
2008-02-08 11:22:05 +01:00
|
|
|
};
|
2015-07-12 05:01:27 +02:00
|
|
|
|
2009-01-21 08:31:49 +01:00
|
|
|
// this is a singleton consisting of the thread and a queue
|
|
|
|
// of disk io jobs
|
2016-04-30 17:05:54 +02:00
|
|
|
struct TORRENT_EXTRA_EXPORT disk_io_thread final
|
2014-07-06 21:18:00 +02:00
|
|
|
: disk_job_pool
|
|
|
|
, disk_interface
|
|
|
|
, buffer_allocator_interface
|
2009-01-21 08:31:49 +01:00
|
|
|
{
|
2018-01-03 12:54:03 +01:00
|
|
|
disk_io_thread(io_service& ios, counters& cnt);
|
2017-12-28 21:41:46 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2009-01-21 08:31:49 +01:00
|
|
|
~disk_io_thread();
|
2017-12-28 21:41:46 +01:00
|
|
|
#endif
|
2009-01-21 08:31:49 +01:00
|
|
|
|
2018-06-23 23:19:53 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
// every 4:th thread is a hash thread
|
|
|
|
hasher_thread_divisor = 4
|
|
|
|
};
|
2009-01-21 08:31:49 +01:00
|
|
|
|
2016-12-26 01:07:31 +01:00
|
|
|
void set_settings(settings_pack const* sett);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-06-14 22:00:04 +02:00
|
|
|
void abort(bool wait);
|
|
|
|
|
2017-04-07 00:11:24 +02:00
|
|
|
storage_holder new_torrent(storage_constructor_type sc
|
|
|
|
, storage_params p, std::shared_ptr<void> const&) override;
|
2016-12-31 18:35:10 +01:00
|
|
|
void remove_torrent(storage_index_t) override;
|
|
|
|
|
|
|
|
void async_read(storage_index_t storage, peer_request const& r
|
2016-12-29 02:47:18 +01:00
|
|
|
, std::function<void(disk_buffer_holder block
|
2017-09-09 19:43:54 +02:00
|
|
|
, disk_job_flags_t flags, storage_error const& se)> handler, disk_job_flags_t flags = {}) override;
|
2016-12-31 18:35:10 +01:00
|
|
|
bool async_write(storage_index_t storage, peer_request const& r
|
2016-12-26 08:38:05 +01:00
|
|
|
, char const* buf, std::shared_ptr<disk_observer> o
|
2016-11-21 05:58:48 +01:00
|
|
|
, std::function<void(storage_error const&)> handler
|
2017-09-09 19:43:54 +02:00
|
|
|
, disk_job_flags_t flags = {}) override;
|
|
|
|
void async_hash(storage_index_t storage, piece_index_t piece, disk_job_flags_t flags
|
2017-06-08 00:46:49 +02:00
|
|
|
, std::function<void(piece_index_t, sha1_hash const&, storage_error const&)> handler) override;
|
2017-06-01 10:15:15 +02:00
|
|
|
void async_move_storage(storage_index_t storage, std::string p, move_flags_t flags
|
2016-11-26 07:51:47 +01:00
|
|
|
, std::function<void(status_t, std::string const&, storage_error const&)> handler) override;
|
2016-12-31 18:35:10 +01:00
|
|
|
void async_release_files(storage_index_t storage
|
2016-11-22 07:48:14 +01:00
|
|
|
, std::function<void()> handler = std::function<void()>()) override;
|
2017-07-27 22:26:12 +02:00
|
|
|
void async_delete_files(storage_index_t storage, remove_flags_t options
|
2016-11-23 07:43:57 +01:00
|
|
|
, std::function<void(storage_error const&)> handler) override;
|
2016-12-31 18:35:10 +01:00
|
|
|
void async_check_files(storage_index_t storage
|
2016-02-15 00:17:32 +01:00
|
|
|
, add_torrent_params const* resume_data
|
2016-12-22 16:42:33 +01:00
|
|
|
, aux::vector<std::string, file_index_t>& links
|
2016-11-26 07:51:47 +01:00
|
|
|
, std::function<void(status_t, storage_error const&)> handler) override;
|
2017-04-16 22:37:39 +02:00
|
|
|
void async_rename_file(storage_index_t storage, file_index_t index, std::string name
|
2016-12-22 16:42:33 +01:00
|
|
|
, std::function<void(std::string const&, file_index_t, storage_error const&)> handler) override;
|
2016-12-31 18:35:10 +01:00
|
|
|
void async_stop_torrent(storage_index_t storage
|
2016-11-23 07:43:57 +01:00
|
|
|
, std::function<void()> handler) override;
|
2016-12-31 18:35:10 +01:00
|
|
|
void async_flush_piece(storage_index_t storage, piece_index_t piece
|
2016-11-23 01:03:27 +01:00
|
|
|
, std::function<void()> handler = std::function<void()>()) override;
|
2016-12-31 18:35:10 +01:00
|
|
|
void async_set_file_priority(storage_index_t storage
|
2017-10-29 00:44:40 +02:00
|
|
|
, aux::vector<download_priority_t, file_index_t> prio
|
2018-07-08 14:36:32 +02:00
|
|
|
, std::function<void(storage_error const&, aux::vector<download_priority_t, file_index_t>)> handler) override;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-31 18:35:10 +01:00
|
|
|
void async_clear_piece(storage_index_t storage, piece_index_t index
|
2016-12-22 16:42:33 +01:00
|
|
|
, std::function<void(piece_index_t)> handler) override;
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is not asynchronous and requires that the piece does not
|
|
|
|
// have any pending buffers. It's meant to be used for pieces that
|
|
|
|
// were just read and hashed and failed the hash check.
|
|
|
|
// there should be no read-operations left, and all buffers should
|
|
|
|
// be discardable
|
2016-12-31 18:35:10 +01:00
|
|
|
void clear_piece(storage_index_t storage, piece_index_t index) override;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// implements buffer_allocator_interface
|
2016-11-18 06:25:27 +01:00
|
|
|
void reclaim_blocks(span<aux::block_cache_reference> ref) override;
|
2016-04-30 17:05:54 +02:00
|
|
|
void free_disk_buffer(char* buf) override { m_disk_cache.free_buffer(buf); }
|
2014-07-06 21:18:00 +02:00
|
|
|
void trigger_cache_trim();
|
2016-04-30 17:05:54 +02:00
|
|
|
void update_stats_counters(counters& c) const override;
|
2016-12-31 18:35:10 +01:00
|
|
|
void get_cache_info(cache_status* ret, storage_index_t storage
|
|
|
|
, bool no_pieces, bool session) const override;
|
|
|
|
storage_interface* get_torrent(storage_index_t) override;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2017-04-09 00:24:50 +02:00
|
|
|
std::vector<open_file_state> get_status(storage_index_t) const override;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this submits all queued up jobs to the thread
|
2017-07-18 09:03:37 +02:00
|
|
|
void submit_jobs() override;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
block_cache* cache() { return &m_disk_cache; }
|
|
|
|
|
2014-07-19 10:20:20 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2016-04-30 17:05:54 +02:00
|
|
|
bool is_disk_buffer(char* buffer) const override
|
2016-04-02 18:27:42 +02:00
|
|
|
{ return m_disk_cache.is_disk_buffer(buffer); }
|
2014-07-06 21:18:00 +02:00
|
|
|
#endif
|
2007-12-24 09:15:10 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int prep_read_job_impl(disk_io_job* j, bool check_fence = true);
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2015-08-19 15:22:00 +02:00
|
|
|
void maybe_issue_queued_read_jobs(cached_piece_entry* pe,
|
|
|
|
jobqueue_t& completed_jobs);
|
2016-11-26 07:51:47 +01:00
|
|
|
status_t do_read(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_uncached_read(disk_io_job* j);
|
|
|
|
|
|
|
|
status_t do_write(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_uncached_write(disk_io_job* j);
|
|
|
|
|
|
|
|
status_t do_hash(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_uncached_hash(disk_io_job* j);
|
|
|
|
|
|
|
|
status_t do_move_storage(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_release_files(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_delete_files(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_check_fastresume(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_rename_file(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_stop_torrent(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_flush_piece(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_flush_hashed(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_flush_storage(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_trim_cache(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_file_priority(disk_io_job* j, jobqueue_t& completed_jobs);
|
|
|
|
status_t do_clear_piece(disk_io_job* j, jobqueue_t& completed_jobs);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-10-30 23:21:07 +01:00
|
|
|
void call_job_handlers();
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2016-06-16 02:49:28 +02:00
|
|
|
struct job_queue : pool_thread_interface
|
|
|
|
{
|
2017-01-09 01:22:59 +01:00
|
|
|
explicit job_queue(disk_io_thread& owner) : m_owner(owner) {}
|
2016-06-16 02:49:28 +02:00
|
|
|
|
2017-09-12 23:10:11 +02:00
|
|
|
void notify_all() override
|
2016-06-16 02:49:28 +02:00
|
|
|
{
|
|
|
|
m_job_cond.notify_all();
|
|
|
|
}
|
|
|
|
|
2017-01-09 01:22:59 +01:00
|
|
|
void thread_fun(disk_io_thread_pool& pool, io_service::work work) override
|
|
|
|
{
|
2017-06-14 09:55:23 +02:00
|
|
|
ADD_OUTSTANDING_ASYNC("disk_io_thread::work");
|
2017-01-09 01:22:59 +01:00
|
|
|
m_owner.thread_fun(*this, pool);
|
|
|
|
|
|
|
|
// w's dtor releases the io_service to allow the run() call to return
|
|
|
|
// we do this once we stop posting new callbacks to it.
|
|
|
|
// after the dtor has been called, the disk_io_thread object may be destructed
|
|
|
|
TORRENT_UNUSED(work);
|
2017-06-14 09:55:23 +02:00
|
|
|
COMPLETE_ASYNC("disk_io_thread::work");
|
2017-01-09 01:22:59 +01:00
|
|
|
}
|
2016-06-16 02:49:28 +02:00
|
|
|
|
|
|
|
disk_io_thread& m_owner;
|
|
|
|
|
|
|
|
// used to wake up the disk IO thread when there are new
|
|
|
|
// jobs on the job queue (m_queued_jobs)
|
|
|
|
std::condition_variable m_job_cond;
|
|
|
|
|
|
|
|
// jobs queued for servicing
|
|
|
|
jobqueue_t m_queued_jobs;
|
|
|
|
};
|
|
|
|
|
2017-01-09 01:22:59 +01:00
|
|
|
void thread_fun(job_queue& queue, disk_io_thread_pool& pool);
|
|
|
|
|
2016-06-16 02:49:28 +02:00
|
|
|
// returns true if the thread should exit
|
|
|
|
static bool wait_for_job(job_queue& jobq, disk_io_thread_pool& threads
|
|
|
|
, std::unique_lock<std::mutex>& l);
|
|
|
|
|
2015-08-19 15:22:00 +02:00
|
|
|
void add_completed_jobs(jobqueue_t& jobs);
|
|
|
|
void add_completed_jobs_impl(jobqueue_t& jobs
|
|
|
|
, jobqueue_t& completed_jobs);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-08-19 15:22:00 +02:00
|
|
|
void fail_jobs(storage_error const& e, jobqueue_t& jobs_);
|
|
|
|
void fail_jobs_impl(storage_error const& e, jobqueue_t& src, jobqueue_t& dst);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
void check_cache_level(std::unique_lock<std::mutex>& l, jobqueue_t& completed_jobs);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-08-19 15:22:00 +02:00
|
|
|
void perform_job(disk_io_job* j, jobqueue_t& completed_jobs);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// this queues up another job to be submitted
|
2015-06-14 22:00:04 +02:00
|
|
|
void add_job(disk_io_job* j, bool user_add = true);
|
2016-12-31 18:35:10 +01:00
|
|
|
void add_fence_job(disk_io_job* j, bool user_add = true);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
// assumes l is locked (cache std::mutex).
|
2014-07-06 21:18:00 +02:00
|
|
|
// writes out the blocks [start, end) (releases the lock
|
|
|
|
// during the file operation)
|
|
|
|
int flush_range(cached_piece_entry* p, int start, int end
|
2016-05-01 00:54:23 +02:00
|
|
|
, jobqueue_t& completed_jobs, std::unique_lock<std::mutex>& l);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// low level flush operations, used by flush_range
|
|
|
|
int build_iovec(cached_piece_entry* pe, int start, int end
|
2017-01-11 06:42:10 +01:00
|
|
|
, span<iovec_t> iov, span<int> flushing, int block_base_index = 0);
|
|
|
|
void flush_iovec(cached_piece_entry* pe, span<iovec_t const> iov, span<int const> flushing
|
2014-07-06 21:18:00 +02:00
|
|
|
, int num_blocks, storage_error& error);
|
2018-04-26 19:32:02 +02:00
|
|
|
// returns true if the piece entry was freed
|
|
|
|
bool iovec_flushed(cached_piece_entry* pe
|
2014-07-06 21:18:00 +02:00
|
|
|
, int* flushing, int num_blocks, int block_offset
|
|
|
|
, storage_error const& error
|
2015-08-19 15:22:00 +02:00
|
|
|
, jobqueue_t& completed_jobs);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
// assumes l is locked (the cache std::mutex).
|
2014-07-06 21:18:00 +02:00
|
|
|
// assumes pe->hash to be set.
|
|
|
|
// If there are new blocks in piece 'pe' that have not been
|
|
|
|
// hashed by the partial_hash object attached to this piece,
|
|
|
|
// the piece will
|
2016-05-01 00:54:23 +02:00
|
|
|
void kick_hasher(cached_piece_entry* pe, std::unique_lock<std::mutex>& l);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// flags to pass in to flush_cache()
|
2017-04-09 07:28:46 +02:00
|
|
|
enum flush_flags_t : std::uint32_t
|
2008-02-08 11:22:05 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
// only flush read cache (this is cheap)
|
|
|
|
flush_read_cache = 1,
|
|
|
|
// flush read cache, and write cache
|
|
|
|
flush_write_cache = 2,
|
|
|
|
// flush read cache, delete write cache without flushing to disk
|
|
|
|
flush_delete_cache = 4,
|
|
|
|
// expect all pieces for the storage to have been
|
|
|
|
// cleared when flush_cache() returns. This is only
|
|
|
|
// used for asserts and only applies for fence jobs
|
|
|
|
flush_expect_clear = 8
|
2008-02-08 11:22:05 +01:00
|
|
|
};
|
2017-04-09 07:28:46 +02:00
|
|
|
void flush_cache(storage_interface* storage, std::uint32_t flags, jobqueue_t& completed_jobs, std::unique_lock<std::mutex>& l);
|
2016-05-01 00:54:23 +02:00
|
|
|
void flush_expired_write_blocks(jobqueue_t& completed_jobs, std::unique_lock<std::mutex>& l);
|
2017-04-09 07:28:46 +02:00
|
|
|
void flush_piece(cached_piece_entry* pe, std::uint32_t flags, jobqueue_t& completed_jobs, std::unique_lock<std::mutex>& l);
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
int try_flush_hashed(cached_piece_entry* p, int cont_blocks, jobqueue_t& completed_jobs, std::unique_lock<std::mutex>& l);
|
2010-01-27 05:25:45 +01:00
|
|
|
|
2016-05-01 00:54:23 +02:00
|
|
|
void try_flush_write_blocks(int num, jobqueue_t& completed_jobs, std::unique_lock<std::mutex>& l);
|
2008-02-25 00:14:10 +01:00
|
|
|
|
2015-06-14 22:00:04 +02:00
|
|
|
void maybe_flush_write_blocks();
|
|
|
|
void execute_job(disk_io_job* j);
|
|
|
|
void immediate_execute();
|
|
|
|
void abort_jobs();
|
2018-06-24 00:11:36 +02:00
|
|
|
void abort_hash_jobs(storage_index_t storage);
|
2015-06-14 22:00:04 +02:00
|
|
|
|
2016-06-16 02:49:28 +02:00
|
|
|
// returns the maximum number of threads
|
|
|
|
// the actual number of threads may be less
|
|
|
|
int num_threads() const;
|
|
|
|
job_queue& queue_for_job(disk_io_job* j);
|
|
|
|
disk_io_thread_pool& pool_for_job(disk_io_job* j);
|
2009-10-20 04:49:56 +02:00
|
|
|
|
2015-06-14 22:00:04 +02:00
|
|
|
// set to true once we start shutting down
|
2016-10-02 21:27:50 +02:00
|
|
|
std::atomic<bool> m_abort{false};
|
2015-06-14 22:00:04 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is a counter of how many threads are currently running.
|
|
|
|
// it's used to identify the last thread still running while
|
|
|
|
// shutting down. This last thread is responsible for cleanup
|
2016-06-18 22:53:23 +02:00
|
|
|
// must hold the job mutex to access
|
2016-10-02 21:27:50 +02:00
|
|
|
int m_num_running_threads = 0;
|
2008-07-18 01:41:46 +02:00
|
|
|
|
2016-06-16 02:49:28 +02:00
|
|
|
// std::mutex to protect the m_generic_io_jobs and m_hash_io_jobs lists
|
|
|
|
mutable std::mutex m_job_mutex;
|
|
|
|
|
|
|
|
// most jobs are posted to m_generic_io_jobs
|
|
|
|
// but hash jobs are posted to m_hash_io_jobs if m_hash_threads
|
|
|
|
// has a non-zero maximum thread count
|
|
|
|
job_queue m_generic_io_jobs;
|
|
|
|
disk_io_thread_pool m_generic_threads;
|
|
|
|
job_queue m_hash_io_jobs;
|
|
|
|
disk_io_thread_pool m_hash_threads;
|
2012-04-07 02:35:25 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
aux::session_settings m_settings;
|
|
|
|
|
|
|
|
// the last time we expired write blocks from the cache
|
2016-10-02 21:27:50 +02:00
|
|
|
time_point m_last_cache_expiry = min_time();
|
2017-04-09 00:24:50 +02:00
|
|
|
|
|
|
|
// we call close_oldest_file on the file_pool regularly. This is the next
|
|
|
|
// time we should call it
|
|
|
|
time_point m_next_close_oldest_file = min_time();
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// LRU cache of open files
|
2016-10-02 21:27:50 +02:00
|
|
|
file_pool m_file_pool{40};
|
2008-02-22 05:11:04 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// disk cache
|
2016-05-01 00:54:23 +02:00
|
|
|
mutable std::mutex m_cache_mutex;
|
2014-07-06 21:18:00 +02:00
|
|
|
block_cache m_disk_cache;
|
2016-03-14 03:36:04 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
cache_check_idle,
|
|
|
|
cache_check_active,
|
|
|
|
cache_check_reinvoke
|
|
|
|
};
|
2016-10-02 21:27:50 +02:00
|
|
|
int m_cache_check_state = cache_check_idle;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2008-02-22 05:11:04 +01:00
|
|
|
// total number of blocks in use by both the read
|
|
|
|
// and the write cache. This is not supposed to
|
|
|
|
// exceed m_cache_size
|
2014-08-01 08:07:48 +02:00
|
|
|
|
|
|
|
counters& m_stats_counters;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is the main thread io_service. Callbacks are
|
|
|
|
// posted on this in order to have them execute in
|
|
|
|
// the main thread.
|
2008-05-03 18:05:42 +02:00
|
|
|
io_service& m_ios;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// jobs that are completed are put on this queue
|
|
|
|
// whenever the queue size grows from 0 to 1
|
|
|
|
// a message is posted to the network thread, which
|
|
|
|
// will then drain the queue and execute the jobs'
|
|
|
|
// handler functions
|
2016-05-01 00:54:23 +02:00
|
|
|
std::mutex m_completed_jobs_mutex;
|
2015-08-19 15:22:00 +02:00
|
|
|
jobqueue_t m_completed_jobs;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-11-06 07:39:41 +01:00
|
|
|
// storages that have had write activity recently and will get ticked
|
|
|
|
// soon, for deferred actions (say, flushing partfile metadata)
|
2016-11-13 03:45:30 +01:00
|
|
|
std::vector<std::pair<time_point, std::weak_ptr<storage_interface>>> m_need_tick;
|
2017-05-18 14:50:22 +02:00
|
|
|
std::mutex m_need_tick_mutex;
|
2016-11-06 07:39:41 +01:00
|
|
|
|
2016-10-19 07:18:05 +02:00
|
|
|
// this is protected by the completed_jobs_mutex. It's true whenever
|
|
|
|
// there's a call_job_handlers message in-flight to the network thread. We
|
|
|
|
// only ever keep one such message in flight at a time, and coalesce
|
|
|
|
// completion callbacks in m_completed jobs
|
|
|
|
bool m_job_completions_in_flight = false;
|
|
|
|
|
2016-12-31 18:35:10 +01:00
|
|
|
aux::vector<std::shared_ptr<storage_interface>, storage_index_t> m_torrents;
|
|
|
|
|
2017-03-23 13:18:04 +01:00
|
|
|
// indices into m_torrents to empty slots
|
|
|
|
std::vector<storage_index_t> m_free_slots;
|
|
|
|
|
2014-02-03 02:55:26 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2016-10-02 21:27:50 +02:00
|
|
|
int m_magic = 0x1337;
|
|
|
|
std::atomic<bool> m_jobs_aborted{false};
|
2014-02-03 02:55:26 +01:00
|
|
|
#endif
|
2007-06-10 22:46:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-12-17 23:07:03 +01:00
|
|
|
#endif
|