From 124c6057be27e98eb6527f858bb35b7e47e50e53 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Sun, 19 Nov 2023 20:15:42 +0000 Subject: [PATCH] more counters --- crates/librqbit/src/http_api.rs | 7 +++++-- crates/librqbit/src/torrent_state.rs | 29 +++++++++++++++++----------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index 2ce53f5..e63156f 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -7,12 +7,12 @@ use dht::{Dht, DhtStats}; use http::StatusCode; use librqbit_core::id20::Id20; use librqbit_core::torrent_metainfo::TorrentMetaV1Info; -use tracing::{warn, info}; use parking_lot::RwLock; use serde::{Deserialize, Serialize}; use std::net::SocketAddr; use std::sync::Arc; use std::time::{Duration, Instant}; +use tracing::{info, warn}; use axum::Router; @@ -341,7 +341,10 @@ impl ApiInternal { let mgr = self.mgr_handle(idx)?; Ok(format!( "{:?}", - mgr.torrent_state().lock_read().chunks.get_have_pieces(), + mgr.torrent_state() + .lock_read("api_dump_haves") + .chunks + .get_have_pieces(), )) } } diff --git a/crates/librqbit/src/torrent_state.rs b/crates/librqbit/src/torrent_state.rs index 827e741..76ca4d4 100644 --- a/crates/librqbit/src/torrent_state.rs +++ b/crates/librqbit/src/torrent_state.rs @@ -115,8 +115,7 @@ impl PeerStates { reason: &'static str, f: impl FnOnce(&mut Peer) -> R, ) -> Option { - self.states - .get_mut(&addr) + timeit(reason, || self.states.get_mut(&addr)) .map(|e| f(TimedExistence::new(e, reason).value_mut())) } pub fn with_live(&self, addr: PeerHandle, f: impl FnOnce(&LivePeerState) -> R) -> Option { @@ -521,20 +520,23 @@ impl TorrentState { pub fn initially_needed(&self) -> u64 { self.needed } - pub fn lock_read(&self) -> RwLockReadGuard { - self.locked.read() + pub fn lock_read( + &self, + reason: &'static str, + ) -> TimedExistence> { + TimedExistence::new(timeit(reason, || self.locked.read()), reason) } pub fn lock_write( &self, reason: &'static str, ) -> TimedExistence> { - TimedExistence::new(self.locked.write(), reason) + TimedExistence::new(timeit(reason, || self.locked.write()), reason) } fn get_next_needed_piece(&self, peer_handle: PeerHandle) -> Option { self.peers - .with_live_mut(peer_handle, "get_next_needed_piece", |live| { - let g = self.locked.read(); + .with_live_mut(peer_handle, "l(get_next_needed_piece)", |live| { + let g = self.lock_read("g(get_next_needed_piece)"); let bf = live.bitfield.as_ref()?; for n in g.chunks.iter_needed_pieces() { if bf.get(n).map(|v| *v) == Some(true) { @@ -624,7 +626,7 @@ impl TorrentState { use rand::seq::IteratorRandom; self.peers .with_live(handle, |live| { - let g = self.locked.read(); + let g = self.lock_read("try_steal_piece"); g.inflight_pieces .keys() .filter(|p| !live.inflight_requests.iter().any(|req| req.piece == **p)) @@ -889,7 +891,7 @@ impl PeerConnectionHandler for PeerHandler { } fn serialize_bitfield_message_to_buf(&self, buf: &mut Vec) -> Option { - let g = self.state.locked.read(); + let g = self.state.lock_read("serialize_bitfield_message_to_buf"); let msg = Message::Bitfield(ByteBuf(g.chunks.get_have_pieces().as_raw_slice())); let len = msg.serialize(buf, None).unwrap(); debug!("sending: {:?}, length={}", &msg, len); @@ -946,7 +948,7 @@ impl PeerHandler { let tx = { if !self .state - .lock_read() + .lock_read("is_chunk_ready_to_upload") .chunks .is_chunk_ready_to_upload(&chunk_info) { @@ -1105,7 +1107,12 @@ impl PeerHandler { }; for chunk in self.state.lengths.iter_chunk_infos(next) { - if self.state.lock_read().chunks.is_chunk_downloaded(&chunk) { + if self + .state + .lock_read("is_chunk_downloaded") + .chunks + .is_chunk_downloaded(&chunk) + { continue; }