9 pp "github.com/anacrolix/torrent/peer_protocol"
12 // Various connection-level metrics. At the Torrent level these are
13 // aggregates. Chunks are messages with data payloads. Data is actual torrent
14 // content without any overhead. Useful is something we needed locally.
15 // Unwanted is something we didn't ask for (but may still be useful). Written
16 // is things sent to the peer, and Read is stuff received from them.
17 type ConnStats struct {
18 // Total bytes on the wire. Includes handshakes and encryption.
20 BytesWrittenData Count
24 BytesReadUsefulData Count
29 ChunksReadUseful Count
30 ChunksReadUnwanted Count
32 // Number of pieces data was written to, that subsequently passed verification.
33 PiecesDirtiedGood Count
34 // Number of pieces data was written to, that subsequently failed
35 // verification. Note that a connection may not have been the sole dirtier
37 PiecesDirtiedBad Count
40 func (me *ConnStats) Copy() (ret ConnStats) {
41 for i := 0; i < reflect.TypeOf(ConnStats{}).NumField(); i++ {
42 n := reflect.ValueOf(me).Elem().Field(i).Addr().Interface().(*Count).Int64()
43 reflect.ValueOf(&ret).Elem().Field(i).Addr().Interface().(*Count).Add(n)
52 var _ fmt.Stringer = (*Count)(nil)
54 func (me *Count) Add(n int64) {
55 atomic.AddInt64(&me.n, n)
58 func (me *Count) Int64() int64 {
59 return atomic.LoadInt64(&me.n)
62 func (me *Count) String() string {
63 return fmt.Sprintf("%v", me.Int64())
66 func (cs *ConnStats) wroteMsg(msg *pp.Message) {
67 // TODO: Track messages and not just chunks.
70 cs.ChunksWritten.Add(1)
71 cs.BytesWrittenData.Add(int64(len(msg.Piece)))
75 func (cs *ConnStats) readMsg(msg *pp.Message) {
79 cs.BytesReadData.Add(int64(len(msg.Piece)))
83 func (cs *ConnStats) incrementPiecesDirtiedGood() {
84 cs.PiecesDirtiedGood.Add(1)
87 func (cs *ConnStats) incrementPiecesDirtiedBad() {
88 cs.PiecesDirtiedBad.Add(1)
91 func add(n int64, f func(*ConnStats) *Count) func(*ConnStats) {
92 return func(cs *ConnStats) {
98 type connStatsReadWriter struct {
103 func (me connStatsReadWriter) Write(b []byte) (n int, err error) {
104 n, err = me.rw.Write(b)
105 me.c.wroteBytes(int64(n))
109 func (me connStatsReadWriter) Read(b []byte) (n int, err error) {
110 n, err = me.rw.Read(b)
111 me.c.readBytes(int64(n))