10 pp "github.com/anacrolix/torrent/peer_protocol"
13 // Various connection-level metrics. At the Torrent level these are aggregates. Chunks are messages
14 // with data payloads. Data is actual torrent content without any overhead. Useful is something we
15 // needed locally. Unwanted is something we didn't ask for (but may still be useful). Written is
16 // things sent to the peer, and Read is stuff received from them. Due to the implementation of
17 // Count, must be aligned on some platforms: See https://github.com/anacrolix/torrent/issues/262.
18 type ConnStats struct {
19 // Total bytes on the wire. Includes handshakes and encryption.
21 BytesWrittenData Count
25 BytesReadUsefulData Count
26 BytesReadUsefulIntendedData Count
31 ChunksReadUseful Count
32 ChunksReadWasted Count
34 MetadataChunksRead Count
36 // Number of pieces data was written to, that subsequently passed verification.
37 PiecesDirtiedGood Count
38 // Number of pieces data was written to, that subsequently failed verification. Note that a
39 // connection may not have been the sole dirtier of a piece.
40 PiecesDirtiedBad Count
43 func (me *ConnStats) Copy() (ret ConnStats) {
44 for i := 0; i < reflect.TypeOf(ConnStats{}).NumField(); i++ {
45 n := reflect.ValueOf(me).Elem().Field(i).Addr().Interface().(*Count).Int64()
46 reflect.ValueOf(&ret).Elem().Field(i).Addr().Interface().(*Count).Add(n)
55 var _ fmt.Stringer = (*Count)(nil)
57 func (me *Count) Add(n int64) {
58 atomic.AddInt64(&me.n, n)
61 func (me *Count) Int64() int64 {
62 return atomic.LoadInt64(&me.n)
65 func (me *Count) String() string {
66 return fmt.Sprintf("%v", me.Int64())
69 func (me *Count) MarshalJSON() ([]byte, error) {
70 return json.Marshal(me.n)
73 func (cs *ConnStats) wroteMsg(msg *pp.Message) {
74 // TODO: Track messages and not just chunks.
77 cs.ChunksWritten.Add(1)
78 cs.BytesWrittenData.Add(int64(len(msg.Piece)))
82 func (cs *ConnStats) receivedChunk(size int64) {
84 cs.BytesReadData.Add(size)
87 func (cs *ConnStats) incrementPiecesDirtiedGood() {
88 cs.PiecesDirtiedGood.Add(1)
91 func (cs *ConnStats) incrementPiecesDirtiedBad() {
92 cs.PiecesDirtiedBad.Add(1)
95 func add(n int64, f func(*ConnStats) *Count) func(*ConnStats) {
96 return func(cs *ConnStats) {
102 type connStatsReadWriter struct {
107 func (me connStatsReadWriter) Write(b []byte) (n int, err error) {
108 n, err = me.rw.Write(b)
109 me.c.wroteBytes(int64(n))
113 func (me connStatsReadWriter) Read(b []byte) (n int, err error) {
114 n, err = me.rw.Read(b)
115 me.c.readBytes(int64(n))