2016-07-05 13:52:33 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2016-07-12 14:42:04 +08:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2016-07-05 13:52:33 +08:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
|
|
|
)
|
|
|
|
|
2018-02-02 16:04:56 +08:00
|
|
|
// Various connection-level metrics. At the Torrent level these are
|
|
|
|
// aggregates. Chunks are messages with data payloads. Data is actual torrent
|
|
|
|
// content without any overhead. Useful is something we needed locally.
|
|
|
|
// Unwanted is something we didn't ask for (but may still be useful). Written
|
|
|
|
// is things sent to the peer, and Read is stuff received from them.
|
2016-07-05 13:52:33 +08:00
|
|
|
type ConnStats struct {
|
2016-07-12 14:42:04 +08:00
|
|
|
// Total bytes on the wire. Includes handshakes and encryption.
|
2018-02-02 21:41:13 +08:00
|
|
|
BytesWritten int64
|
|
|
|
BytesWrittenData int64
|
2018-02-02 16:04:56 +08:00
|
|
|
|
2018-02-02 21:41:13 +08:00
|
|
|
BytesRead int64
|
|
|
|
BytesReadData int64
|
|
|
|
BytesReadUsefulData int64
|
2018-02-02 16:04:56 +08:00
|
|
|
|
|
|
|
ChunksWritten int64
|
|
|
|
|
|
|
|
ChunksRead int64
|
|
|
|
ChunksReadUseful int64
|
|
|
|
ChunksReadUnwanted int64
|
|
|
|
|
|
|
|
// Number of pieces data was written to, that subsequently passed verification.
|
2018-02-02 21:41:13 +08:00
|
|
|
PiecesDirtiedGood int64
|
2018-02-02 16:04:56 +08:00
|
|
|
// Number of pieces data was written to, that subsequently failed
|
|
|
|
// verification. Note that a connection may not have been the sole dirtier
|
|
|
|
// of a piece.
|
2018-02-02 21:41:13 +08:00
|
|
|
PiecesDirtiedBad int64
|
2016-07-05 13:52:33 +08:00
|
|
|
}
|
|
|
|
|
2016-07-12 14:42:04 +08:00
|
|
|
func (cs *ConnStats) wroteMsg(msg *pp.Message) {
|
2018-02-04 16:14:07 +08:00
|
|
|
// TODO: Track messages and not just chunks.
|
2016-07-05 13:52:33 +08:00
|
|
|
switch msg.Type {
|
|
|
|
case pp.Piece:
|
2016-07-12 14:42:04 +08:00
|
|
|
cs.ChunksWritten++
|
2018-02-02 21:41:13 +08:00
|
|
|
cs.BytesWrittenData += int64(len(msg.Piece))
|
2016-07-05 13:52:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 14:42:04 +08:00
|
|
|
func (cs *ConnStats) readMsg(msg *pp.Message) {
|
|
|
|
switch msg.Type {
|
|
|
|
case pp.Piece:
|
|
|
|
cs.ChunksRead++
|
2018-02-02 21:41:13 +08:00
|
|
|
cs.BytesReadData += int64(len(msg.Piece))
|
2016-07-12 14:42:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 07:18:52 +08:00
|
|
|
func (cs *ConnStats) incrementPiecesDirtiedGood() {
|
|
|
|
cs.PiecesDirtiedGood++
|
2016-07-12 14:42:04 +08:00
|
|
|
}
|
|
|
|
|
2018-06-10 07:18:52 +08:00
|
|
|
func (cs *ConnStats) incrementPiecesDirtiedBad() {
|
|
|
|
cs.PiecesDirtiedBad++
|
|
|
|
}
|
|
|
|
|
|
|
|
func add(n int64, f func(*ConnStats) *int64) func(*ConnStats) {
|
|
|
|
return func(cs *ConnStats) {
|
|
|
|
p := f(cs)
|
|
|
|
*p += n
|
|
|
|
}
|
2016-07-12 14:42:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type connStatsReadWriter struct {
|
|
|
|
rw io.ReadWriter
|
|
|
|
l sync.Locker
|
|
|
|
c *connection
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me connStatsReadWriter) Write(b []byte) (n int, err error) {
|
|
|
|
n, err = me.rw.Write(b)
|
2018-06-09 20:11:19 +08:00
|
|
|
go func() {
|
|
|
|
me.l.Lock()
|
|
|
|
me.c.wroteBytes(int64(n))
|
|
|
|
me.l.Unlock()
|
|
|
|
}()
|
2016-07-12 14:42:04 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me connStatsReadWriter) Read(b []byte) (n int, err error) {
|
|
|
|
n, err = me.rw.Read(b)
|
2018-06-09 20:11:19 +08:00
|
|
|
go func() {
|
|
|
|
me.l.Lock()
|
|
|
|
me.c.readBytes(int64(n))
|
|
|
|
me.l.Unlock()
|
|
|
|
}()
|
2016-07-12 14:42:04 +08:00
|
|
|
return
|
2016-07-05 13:52:33 +08:00
|
|
|
}
|