torrent/conn_stats.go

83 lines
1.9 KiB
Go
Raw Normal View History

2016-07-05 05:52:33 +00:00
package torrent
import (
2016-07-12 06:42:04 +00:00
"io"
"sync"
2016-07-05 05:52:33 +00:00
pp "github.com/anacrolix/torrent/peer_protocol"
)
// 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 05:52:33 +00:00
type ConnStats struct {
2016-07-12 06:42:04 +00:00
// Total bytes on the wire. Includes handshakes and encryption.
2018-02-02 13:41:13 +00:00
BytesWritten int64
BytesWrittenData int64
2018-02-02 13:41:13 +00:00
BytesRead int64
BytesReadData int64
BytesReadUsefulData int64
ChunksWritten int64
ChunksRead int64
ChunksReadUseful int64
ChunksReadUnwanted int64
// Number of pieces data was written to, that subsequently passed verification.
2018-02-02 13:41:13 +00:00
PiecesDirtiedGood int64
// 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 13:41:13 +00:00
PiecesDirtiedBad int64
2016-07-05 05:52:33 +00:00
}
2016-07-12 06:42:04 +00:00
func (cs *ConnStats) wroteMsg(msg *pp.Message) {
2016-07-05 05:52:33 +00:00
switch msg.Type {
case pp.Piece:
2016-07-12 06:42:04 +00:00
cs.ChunksWritten++
2018-02-02 13:41:13 +00:00
cs.BytesWrittenData += int64(len(msg.Piece))
2016-07-05 05:52:33 +00:00
}
}
2016-07-12 06:42:04 +00:00
func (cs *ConnStats) readMsg(msg *pp.Message) {
switch msg.Type {
case pp.Piece:
cs.ChunksRead++
2018-02-02 13:41:13 +00:00
cs.BytesReadData += int64(len(msg.Piece))
2016-07-12 06:42:04 +00:00
}
}
func (cs *ConnStats) wroteBytes(n int64) {
cs.BytesWritten += n
}
func (cs *ConnStats) readBytes(n int64) {
cs.BytesRead += n
}
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)
me.l.Lock()
me.c.wroteBytes(int64(n))
me.l.Unlock()
return
}
func (me connStatsReadWriter) Read(b []byte) (n int, err error) {
n, err = me.rw.Read(b)
me.l.Lock()
me.c.readBytes(int64(n))
me.l.Unlock()
return
2016-07-05 05:52:33 +00:00
}