convert remaining disk io jobs

This commit is contained in:
arvidn 2016-11-23 01:43:57 -05:00 committed by Arvid Norberg
parent f08501b869
commit 1d5f42d6d0
8 changed files with 75 additions and 71 deletions

View File

@ -39,7 +39,6 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
struct disk_io_job;
struct storage_interface;
struct peer_request;
struct disk_observer;
@ -59,6 +58,13 @@ namespace libtorrent
file_exist = -4
};
enum flags_t
{
// this flag is set on a job when a read operation did
// not hit the disk, but found the data in the read cache.
cache_hit = 0x2,
};
virtual void async_read(storage_interface* storage, peer_request const& r
, std::function<void(aux::block_cache_reference ref, char* block
, int flags, storage_error const& se)> handler, void* requester, int flags = 0) = 0;
@ -79,17 +85,17 @@ namespace libtorrent
virtual void async_flush_piece(storage_interface* storage, int piece
, std::function<void()> handler = std::function<void()>()) = 0;
virtual void async_stop_torrent(storage_interface* storage
, std::function<void(disk_io_job const*)> handler)= 0;
, std::function<void()> handler = std::function<void()>()) = 0;
virtual void async_rename_file(storage_interface* storage, int index, std::string const& name
, std::function<void(disk_io_job const*)> handler) = 0;
, std::function<void(std::string const&, int, storage_error const&)> handler) = 0;
virtual void async_delete_files(storage_interface* storage, int options
, std::function<void(disk_io_job const*)> handler) = 0;
, std::function<void(storage_error const&)> handler) = 0;
virtual void async_set_file_priority(storage_interface* storage
, std::vector<std::uint8_t> const& prio
, std::function<void(disk_io_job const*)> handler) = 0;
, std::function<void(storage_error const&)> handler) = 0;
virtual void async_clear_piece(storage_interface* storage, int index
, std::function<void(disk_io_job const*)> handler) = 0;
, std::function<void(int)> handler) = 0;
virtual void clear_piece(storage_interface* storage, int index) = 0;
virtual void update_stats_counters(counters& c) const = 0;

View File

@ -102,10 +102,6 @@ namespace libtorrent
{
sequential_access = 0x1,
// this flag is set on a job when a read operation did
// not hit the disk, but found the data in the read cache.
cache_hit = 0x2,
// force making a copy of the cached block, rather
// than getting a reference to the block already in
// the cache.
@ -160,7 +156,8 @@ namespace libtorrent
using move_handler = std::function<void(int, std::string const&, storage_error const&)>;
using release_handler = std::function<void()>;
using check_handler = std::function<void(int, storage_error const&)>;
using generic_handler = std::function<void(disk_io_job const*)>;
using rename_handler = std::function<void(std::string const&, int, storage_error const&)>;
using clear_piece_handler = std::function<void(int)>;
boost::variant<read_handler
, write_handler
@ -168,7 +165,8 @@ namespace libtorrent
, move_handler
, release_handler
, check_handler
, generic_handler> callback;
, rename_handler
, clear_piece_handler> callback;
// the error code from the file operation
// on error, this also contains the path of the

View File

@ -305,23 +305,23 @@ namespace libtorrent
void async_release_files(storage_interface* storage
, std::function<void()> handler = std::function<void()>()) override;
void async_delete_files(storage_interface* storage, int options
, std::function<void(disk_io_job const*)> handler) override;
, std::function<void(storage_error const&)> handler) override;
void async_check_files(storage_interface* storage
, add_torrent_params const* resume_data
, std::vector<std::string>& links
, std::function<void(int, storage_error const&)> handler) override;
void async_rename_file(storage_interface* storage, int index, std::string const& name
, std::function<void(disk_io_job const*)> handler) override;
, std::function<void(std::string const&, int, storage_error const&)> handler) override;
void async_stop_torrent(storage_interface* storage
, std::function<void(disk_io_job const*)> handler) override;
, std::function<void()> handler) override;
void async_flush_piece(storage_interface* storage, int piece
, std::function<void()> handler = std::function<void()>()) override;
void async_set_file_priority(storage_interface* storage
, std::vector<std::uint8_t> const& prio
, std::function<void(disk_io_job const*)> handler) override;
, std::function<void(storage_error const&)> handler) override;
void async_clear_piece(storage_interface* storage, int index
, std::function<void(disk_io_job const*)> handler) override;
, std::function<void(int)> handler) override;
// 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.

View File

@ -92,7 +92,6 @@ namespace libtorrent
struct storage_interface;
class bt_peer_connection;
struct listen_socket_t;
struct disk_io_job;
enum class waste_reason
{
@ -381,7 +380,6 @@ namespace libtorrent
void add_piece(int piece, char const* data, int flags = 0);
void on_disk_write_complete(storage_error const& error
, peer_request p);
void on_disk_tick_done(disk_io_job const* j);
void set_progress_ppm(int p) { m_progress_ppm = std::uint32_t(p); }
struct read_piece_struct
@ -537,7 +535,7 @@ namespace libtorrent
void set_file_priority(int index, int priority);
int file_priority(int index) const;
void on_file_priority();
void on_file_priority(storage_error const&);
void prioritize_files(std::vector<int> const& files);
void file_priorities(std::vector<int>*) const;
@ -887,10 +885,10 @@ namespace libtorrent
// this is the handler for hash failure piece synchronization
// i.e. resetting the piece
void on_piece_sync(disk_io_job const* j);
void on_piece_sync(int piece);
// this is the handler for write failure piece synchronization
void on_piece_fail_sync(disk_io_job const* j, piece_block b);
void on_piece_fail_sync(int piece, piece_block b);
void add_redundant_bytes(int b, waste_reason reason);
void add_failed_bytes(int b);
@ -1116,11 +1114,13 @@ namespace libtorrent
void construct_storage();
void update_list(int list, bool in);
void on_files_deleted(disk_io_job const* j);
void on_torrent_paused(disk_io_job const* j);
void on_files_deleted(storage_error const& error);
void on_torrent_paused();
void on_storage_moved(int status, std::string const& path
, storage_error const& error);
void on_file_renamed(disk_io_job const* j);
void on_file_renamed(std::string const& filename
, int const file_idx
, storage_error const& error);
void on_cache_flushed();
// upload and download rate limits for the torrent

View File

@ -76,10 +76,16 @@ namespace libtorrent
h(m_job.ret, m_job.error);
}
void operator()(disk_io_job::generic_handler& h) const
void operator()(disk_io_job::rename_handler& h) const
{
if (!h) return;
h(&m_job);
h(m_job.buffer.string, m_job.piece, m_job.error);
}
void operator()(disk_io_job::clear_piece_handler& h) const
{
if (!h) return;
h(m_job.piece);
}
private:

View File

@ -1372,7 +1372,7 @@ namespace libtorrent
// cache-hit
m_stats_counters.inc_stats_counter(counters::num_blocks_cache_hits);
DLOG("do_read: cache hit\n");
j->flags |= disk_io_job::cache_hit;
j->flags |= disk_interface::cache_hit;
j->ret = ret;
completed_jobs.push_back(j);
}
@ -1562,7 +1562,7 @@ namespace libtorrent
{
m_stats_counters.inc_stats_counter(counters::num_blocks_cache_hits);
DLOG("do_read: cache hit\n");
j->flags |= disk_io_job::cache_hit;
j->flags |= disk_interface::cache_hit;
j->ret = ret;
return 0;
}
@ -1784,7 +1784,7 @@ namespace libtorrent
void disk_io_thread::async_delete_files(storage_interface* storage
, int const options
, std::function<void(disk_io_job const*)> handler)
, std::function<void(storage_error const&)> handler)
{
// remove cache blocks belonging to this torrent
jobqueue_t completed_jobs;
@ -1847,7 +1847,7 @@ namespace libtorrent
}
void disk_io_thread::async_rename_file(storage_interface* storage, int index, std::string const& name
, std::function<void(disk_io_job const*)> handler)
, std::function<void(std::string const&, int, storage_error const&)> handler)
{
disk_io_job* j = allocate_job(disk_io_job::rename_file);
j->storage = storage->shared_from_this();
@ -1858,7 +1858,7 @@ namespace libtorrent
}
void disk_io_thread::async_stop_torrent(storage_interface* storage
, std::function<void(disk_io_job const*)> handler)
, std::function<void()> handler)
{
// remove outstanding hash jobs belonging to this torrent
std::unique_lock<std::mutex> l2(m_job_mutex);
@ -1913,7 +1913,7 @@ namespace libtorrent
void disk_io_thread::async_set_file_priority(storage_interface* storage
, std::vector<std::uint8_t> const& prios
, std::function<void(disk_io_job const*)> handler)
, std::function<void(storage_error const&)> handler)
{
std::vector<std::uint8_t>* p = new std::vector<std::uint8_t>(prios);
@ -1926,7 +1926,7 @@ namespace libtorrent
}
void disk_io_thread::async_clear_piece(storage_interface* storage, int index
, std::function<void(disk_io_job const*)> handler)
, std::function<void(int)> handler)
{
disk_io_job* j = allocate_job(disk_io_job::clear_piece);
j->storage = storage->shared_from_this();

View File

@ -61,7 +61,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/ip_filter.hpp"
#include "libtorrent/kademlia/node_id.hpp"
#include "libtorrent/close_reason.hpp"
#include "libtorrent/disk_io_job.hpp"
#include "libtorrent/aux_/has_block.hpp"
#include "libtorrent/aux_/time.hpp"
@ -3032,9 +3031,7 @@ namespace libtorrent
// is m_abort true? if so, we should probably just
// exit this function early, no need to keep the picker
// state up-to-date, right?
disk_io_job sj;
sj.piece = p.piece;
t->on_piece_fail_sync(&sj, block_finished);
t->on_piece_fail_sync(p.piece, block_finished);
}
}
t->update_gauge();
@ -5236,7 +5233,7 @@ namespace libtorrent
, "piece: %d s: %x l: %x b: %p c: %s e: %s rtt: %d us"
, r.piece, r.start, r.length
, static_cast<void*>(disk_block)
, (flags & disk_io_job::cache_hit ? "cache hit" : "cache miss")
, (flags & disk_interface::cache_hit ? "cache hit" : "cache miss")
, error.ec.message().c_str(), disk_rtt);
}
#endif
@ -5303,7 +5300,7 @@ namespace libtorrent
// if it's rare enough to make it into the suggested piece
// push another piece out
if (m_settings.get_int(settings_pack::suggest_mode) == settings_pack::suggest_read_cache
&& (flags & disk_io_job::cache_hit) == 0)
&& (flags & disk_interface::cache_hit) == 0)
{
t->add_suggest_piece(r.piece);
}

View File

@ -1097,11 +1097,8 @@ namespace libtorrent
pause();
}
void torrent::on_piece_fail_sync(disk_io_job const* j, piece_block b) try
void torrent::on_piece_fail_sync(int, piece_block) try
{
TORRENT_UNUSED(j);
TORRENT_UNUSED(b);
if (m_abort) return;
update_gauge();
@ -4139,9 +4136,7 @@ namespace libtorrent
// it doesn't really matter what we do
// here, since we're about to destruct the
// torrent anyway.
disk_io_job j;
j.piece = index;
on_piece_sync(&j);
on_piece_sync(index);
}
#if TORRENT_USE_ASSERTS
@ -4176,7 +4171,7 @@ namespace libtorrent
c.send_block_requests();
}
void torrent::on_piece_sync(disk_io_job const* j) try
void torrent::on_piece_sync(int const piece) try
{
// the user may have called force_recheck, which clears
// the piece picker
@ -4184,12 +4179,12 @@ namespace libtorrent
// unlock the piece and restore it, as if no block was
// ever downloaded for it.
m_picker->restore_piece(j->piece);
m_picker->restore_piece(piece);
// we have to let the piece_picker know that
// this piece failed the check as it can restore it
// and mark it as being interesting for download
TORRENT_ASSERT(m_picker->have_piece(j->piece) == false);
TORRENT_ASSERT(m_picker->have_piece(piece) == false);
// loop over all peers and re-request potential duplicate
// blocks to this piece
@ -4203,14 +4198,14 @@ namespace libtorrent
, end2(dq.end()); k != end2; ++k)
{
if (k->timed_out || k->not_wanted) continue;
if (int(k->block.piece_index) != j->piece) continue;
if (int(k->block.piece_index) != piece) continue;
m_picker->mark_as_downloading(k->block, p->peer_info_struct()
, p->picker_options());
}
for (std::vector<pending_block>::const_iterator k = rq.begin()
, end2(rq.end()); k != end2; ++k)
{
if (int(k->block.piece_index) != j->piece) continue;
if (int(k->block.piece_index) != piece) continue;
m_picker->mark_as_downloading(k->block, p->peer_info_struct()
, p->picker_options());
}
@ -4412,15 +4407,15 @@ namespace libtorrent
return avail_vec[random(avail_vec.size() - 1)];
}
void torrent::on_files_deleted(disk_io_job const* j) try
void torrent::on_files_deleted(storage_error const& error) try
{
TORRENT_ASSERT(is_single_thread());
if (j->ret != 0)
if (error)
{
if (alerts().should_post<torrent_delete_failed_alert>())
alerts().emplace_alert<torrent_delete_failed_alert>(get_handle()
, j->error.ec, m_torrent_file->info_hash());
, error.ec, m_torrent_file->info_hash());
}
else
{
@ -4429,27 +4424,29 @@ namespace libtorrent
}
catch (...) { handle_exception(); }
void torrent::on_file_renamed(disk_io_job const* j) try
void torrent::on_file_renamed(std::string const& filename
, int const file_idx
, storage_error const& error) try
{
TORRENT_ASSERT(is_single_thread());
if (j->ret == 0)
{
if (alerts().should_post<file_renamed_alert>())
alerts().emplace_alert<file_renamed_alert>(get_handle()
, j->buffer.string, j->piece);
m_torrent_file->rename_file(j->piece, j->buffer.string);
}
else
if (error)
{
if (alerts().should_post<file_rename_failed_alert>())
alerts().emplace_alert<file_rename_failed_alert>(get_handle()
, j->piece, j->error.ec);
, file_idx, error.ec);
}
else
{
if (alerts().should_post<file_renamed_alert>())
alerts().emplace_alert<file_renamed_alert>(get_handle()
, filename, file_idx);
m_torrent_file->rename_file(file_idx, filename);
}
}
catch (...) { handle_exception(); }
void torrent::on_torrent_paused(disk_io_job const*) try
void torrent::on_torrent_paused() try
{
TORRENT_ASSERT(is_single_thread());
@ -4912,7 +4909,7 @@ namespace libtorrent
}
}
void torrent::on_file_priority() {}
void torrent::on_file_priority(storage_error const&) {}
void torrent::prioritize_files(std::vector<int> const& files)
{
@ -4945,7 +4942,7 @@ namespace libtorrent
if (m_torrent_file->num_pieces() > 0 && m_storage)
{
m_ses.disk_thread().async_set_file_priority(m_storage.get()
, m_file_priority, std::bind(&torrent::on_file_priority, this));
, m_file_priority, std::bind(&torrent::on_file_priority, this, _1));
}
update_piece_priorities();
@ -4983,7 +4980,7 @@ namespace libtorrent
if (m_storage)
{
m_ses.disk_thread().async_set_file_priority(m_storage.get()
, m_file_priority, std::bind(&torrent::on_file_priority, this));
, m_file_priority, std::bind(&torrent::on_file_priority, this, _1));
}
update_piece_priorities();
}
@ -7725,7 +7722,7 @@ namespace libtorrent
}
m_ses.disk_thread().async_rename_file(m_storage.get(), index, name
, std::bind(&torrent::on_file_renamed, shared_from_this(), _1));
, std::bind(&torrent::on_file_renamed, shared_from_this(), _1, _2, _3));
return;
}
@ -8639,7 +8636,7 @@ namespace libtorrent
{
// the torrent_paused alert will be posted from on_torrent_paused
m_ses.disk_thread().async_stop_torrent(m_storage.get()
, std::bind(&torrent::on_torrent_paused, shared_from_this(), _1));
, std::bind(&torrent::on_torrent_paused, shared_from_this()));
}
else
{