client_test.go | 2 +- file.go | 2 +- piece.go | 2 +- t.go | 10 +++++----- torrent.go | 36 ++++++++++++++++++------------------ torrent_test.go | 2 +- diff --git a/client_test.go b/client_test.go index 414aac0675a52a8d4390a2ed0bd5771f398ba686..7a04c59120f6bb8bde59ccf3ff78897d1b8b6241 100644 --- a/client_test.go +++ b/client_test.go @@ -483,7 +483,7 @@ leecherGreeting.cl.lock() leecherGreeting.downloadPiecesLocked(0, leecherGreeting.numPieces()) if ps.Cancel { - leecherGreeting.cancelPiecesLocked(0, leecherGreeting.NumPieces()) + leecherGreeting.cancelPiecesLocked(0, leecherGreeting.NumPieces(), "") } leecherGreeting.cl.unlock() done := make(chan struct{}) diff --git a/file.go b/file.go index 35fe9239aaaba3a0191d220f46d0e84fe79e1aa5..01515187d0d1aa11f03e3efa8ae3e59bebeb35be 100644 --- a/file.go +++ b/file.go @@ -149,7 +149,7 @@ func (f *File) SetPriority(prio piecePriority) { f.t.cl.lock() if prio != f.prio { f.prio = prio - f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex()) + f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex(), "File.SetPriority") } f.t.cl.unlock() } diff --git a/piece.go b/piece.go index 20c5c19a28399b8e97d6eaf7febbae76071f0f24..411b187c61c4026a70d1d377af296f43317ab95b 100644 --- a/piece.go +++ b/piece.go @@ -191,7 +191,7 @@ func (p *Piece) SetPriority(prio piecePriority) { p.t.cl.lock() defer p.t.cl.unlock() p.priority = prio - p.t.updatePiecePriority(p.index) + p.t.updatePiecePriority(p.index, "Piece.SetPriority") } func (p *Piece) purePriority() (ret piecePriority) { diff --git a/t.go b/t.go index 042a5262560a33375fd052c73bdc015f8a331067..78c500dc438faf33b9d6f3a12dd669ff5fa5566c 100644 --- a/t.go +++ b/t.go @@ -185,25 +185,25 @@ func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) { for i := begin; i < end; i++ { if t.pieces[i].priority.Raise(PiecePriorityNormal) { - t.updatePiecePriority(i) + t.updatePiecePriority(i, "Torrent.DownloadPieces") } } } -func (t *Torrent) CancelPieces(begin, end pieceIndex) { +func (t *Torrent) CancelPieces(begin, end pieceIndex, reason string) { t.cl.lock() - t.cancelPiecesLocked(begin, end) + t.cancelPiecesLocked(begin, end, "Torrent.CancelPieces") t.cl.unlock() } -func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) { +func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex, reason string) { for i := begin; i < end; i++ { p := &t.pieces[i] if p.priority == PiecePriorityNone { continue } p.priority = PiecePriorityNone - t.updatePiecePriority(i) + t.updatePiecePriority(i, reason) } } diff --git a/torrent.go b/torrent.go index a89a114e17dba6c5376bc4f54e810e5e23c073ed..b2596be3a47af8b79a3053e93d189e7a7ec8dd2e 100644 --- a/torrent.go +++ b/torrent.go @@ -1060,7 +1060,7 @@ } func (t *Torrent) readersChanged() { t.updateReaderPieces() - t.updateAllPiecePriorities() + t.updateAllPiecePriorities("Torrent.readersChanged") } func (t *Torrent) updateReaderPieces() { @@ -1079,15 +1079,15 @@ l, h = h, l } if l.end < h.begin { // Two distinct ranges. - t.updatePiecePriorities(l.begin, l.end) - t.updatePiecePriorities(h.begin, h.end) + t.updatePiecePriorities(l.begin, l.end, "Torrent.readerPosChanged") + t.updatePiecePriorities(h.begin, h.end, "Torrent.readerPosChanged") } else { // Ranges overlap. end := l.end if h.end > end { end = h.end } - t.updatePiecePriorities(l.begin, end) + t.updatePiecePriorities(l.begin, end, "Torrent.readerPosChanged") } } @@ -1097,17 +1097,17 @@ t.cl.event.Broadcast() t.openNewConns() } -func (t *Torrent) piecePriorityChanged(piece pieceIndex) { +func (t *Torrent) piecePriorityChanged(piece pieceIndex, reason string) { if t._pendingPieces.Contains(piece) { t.iterPeers(func(c *Peer) { - c.updateRequests("piece priority changed") + c.updateRequests(reason) }) } t.maybeNewConns() t.publishPieceChange(piece) } -func (t *Torrent) updatePiecePriority(piece pieceIndex) { +func (t *Torrent) updatePiecePriority(piece pieceIndex, reason string) { p := &t.pieces[piece] newPrio := p.uncachedPriority() // t.logger.Printf("torrent %p: piece %d: uncached priority: %v", t, piece, newPrio) @@ -1120,18 +1120,18 @@ if !t._pendingPieces.Set(int(piece), newPrio.BitmapPriority()) { return } } - t.piecePriorityChanged(piece) + t.piecePriorityChanged(piece, reason) } -func (t *Torrent) updateAllPiecePriorities() { - t.updatePiecePriorities(0, t.numPieces()) +func (t *Torrent) updateAllPiecePriorities(reason string) { + t.updatePiecePriorities(0, t.numPieces(), reason) } // Update all piece priorities in one hit. This function should have the same // output as updatePiecePriority, but across all pieces. -func (t *Torrent) updatePiecePriorities(begin, end pieceIndex) { +func (t *Torrent) updatePiecePriorities(begin, end pieceIndex, reason string) { for i := begin; i < end; i++ { - t.updatePiecePriority(i) + t.updatePiecePriority(i, reason) } } @@ -1190,14 +1190,14 @@ func (t *Torrent) pendRequest(req RequestIndex) { t.piece(int(req / t.chunksPerRegularPiece())).pendChunkIndex(req % t.chunksPerRegularPiece()) } -func (t *Torrent) pieceCompletionChanged(piece pieceIndex) { +func (t *Torrent) pieceCompletionChanged(piece pieceIndex, reason string) { t.cl.event.Broadcast() if t.pieceComplete(piece) { t.onPieceCompleted(piece) } else { t.onIncompletePiece(piece) } - t.updatePiecePriority(piece) + t.updatePiecePriority(piece, reason) } func (t *Torrent) numReceivedConns() (ret int) { @@ -1275,7 +1275,7 @@ t.logger.Printf("marked piece %v complete but still has dirtiers", piece) } if changed { log.Fstr("piece %d completion changed: %+v -> %+v", piece, cached, uncached).SetLevel(log.Debug).Log(t.logger) - t.pieceCompletionChanged(piece) + t.pieceCompletionChanged(piece, "Torrent.updatePieceCompletion") } return changed } @@ -2030,7 +2030,7 @@ p := t.piece(pi) t.piecesQueuedForHash.Remove(bitmap.BitIndex(pi)) p.hashing = true t.publishPieceChange(pi) - t.updatePiecePriority(pi) + t.updatePiecePriority(pi, "Torrent.tryCreatePieceHasher") t.storageLock.RLock() t.activePieceHashes++ go t.pieceHasher(pi) @@ -2063,7 +2063,7 @@ t.cl.lock() defer t.cl.unlock() p.hashing = false t.pieceHashed(index, correct, copyErr) - t.updatePiecePriority(index) + t.updatePiecePriority(index, "Torrent.pieceHasher") t.activePieceHashes-- t.tryCreateMorePieceHashers() } @@ -2091,7 +2091,7 @@ return } t.piecesQueuedForHash.Add(bitmap.BitIndex(pieceIndex)) t.publishPieceChange(pieceIndex) - t.updatePiecePriority(pieceIndex) + t.updatePiecePriority(pieceIndex, "Torrent.queuePieceCheck") t.tryCreateMorePieceHashers() } diff --git a/torrent_test.go b/torrent_test.go index 64d90b691c5d4cc53fbc0529cdbd902af4945b21..02e56e2dc5f5baf2ba9f6d0f517a70169b00ce34 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -101,7 +101,7 @@ t._completedPieces.Add(bitmap.BitIndex(i)) } t.DownloadPieces(0, t.numPieces()) for i := 0; i < b.N; i += 1 { - t.updateAllPiecePriorities() + t.updateAllPiecePriorities("") } }