forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_stats.go
117 lines (94 loc) · 2.8 KB
/
conn_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package torrent
import (
"encoding/json"
"fmt"
"io"
"reflect"
"sync/atomic"
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. Due to the implementation of
// Count, must be aligned on some platforms: See https://github.com/anacrolix/torrent/issues/262.
type ConnStats struct {
// Total bytes on the wire. Includes handshakes and encryption.
BytesWritten Count
BytesWrittenData Count
BytesRead Count
BytesReadData Count
BytesReadUsefulData Count
BytesReadUsefulIntendedData Count
ChunksWritten Count
ChunksRead Count
ChunksReadUseful Count
ChunksReadWasted Count
MetadataChunksRead Count
// Number of pieces data was written to, that subsequently passed verification.
PiecesDirtiedGood Count
// 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.
PiecesDirtiedBad Count
}
func (me *ConnStats) Copy() (ret ConnStats) {
for i := 0; i < reflect.TypeOf(ConnStats{}).NumField(); i++ {
n := reflect.ValueOf(me).Elem().Field(i).Addr().Interface().(*Count).Int64()
reflect.ValueOf(&ret).Elem().Field(i).Addr().Interface().(*Count).Add(n)
}
return
}
type Count struct {
n int64
}
var _ fmt.Stringer = (*Count)(nil)
func (me *Count) Add(n int64) {
atomic.AddInt64(&me.n, n)
}
func (me *Count) Int64() int64 {
return atomic.LoadInt64(&me.n)
}
func (me *Count) String() string {
return fmt.Sprintf("%v", me.Int64())
}
func (me *Count) MarshalJSON() ([]byte, error) {
return json.Marshal(me.n)
}
func (cs *ConnStats) wroteMsg(msg *pp.Message) {
// TODO: Track messages and not just chunks.
switch msg.Type {
case pp.Piece:
cs.ChunksWritten.Add(1)
cs.BytesWrittenData.Add(int64(len(msg.Piece)))
}
}
func (cs *ConnStats) receivedChunk(size int64) {
cs.ChunksRead.Add(1)
cs.BytesReadData.Add(size)
}
func (cs *ConnStats) incrementPiecesDirtiedGood() {
cs.PiecesDirtiedGood.Add(1)
}
func (cs *ConnStats) incrementPiecesDirtiedBad() {
cs.PiecesDirtiedBad.Add(1)
}
func add(n int64, f func(*ConnStats) *Count) func(*ConnStats) {
return func(cs *ConnStats) {
p := f(cs)
p.Add(n)
}
}
type connStatsReadWriter struct {
rw io.ReadWriter
c *PeerConn
}
func (me connStatsReadWriter) Write(b []byte) (n int, err error) {
n, err = me.rw.Write(b)
me.c.wroteBytes(int64(n))
return
}
func (me connStatsReadWriter) Read(b []byte) (n int, err error) {
n, err = me.rw.Read(b)
me.c.readBytes(int64(n))
return
}