extract bitswap metrics to separate struct for 64bit alignment
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jul 6, 2017 at 12:17 UTC
124afdbaaad5f6e42e3fed70afeeb604bc473ae0
5 files changed
+34
-27
exchange/bitswap/bitswap.go
+19
-13
@@ -99,6 +99,7 @@ func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork,
99
newBlocks: make(chan *cid.Cid, HasBlockBufferSize),
100
provideKeys: make(chan *cid.Cid, provideKeysBufferSize),
101
wm: NewWantManager(ctx, network),
102
+ counters: new(counters),
103
104
dupMetric: dupHist,
105
allMetric: allHist,
@@ -152,14 +153,8 @@ type Bitswap struct {
153
process process.Process
154
155
// Counters for various statistics
155
- counterLk sync.Mutex
156
- blocksRecvd int
157
- dupBlocksRecvd int
158
- dupDataRecvd uint64
159
- blocksSent int
160
- dataSent uint64
161
- dataRecvd uint64
162
- messagesRecvd uint64
156
+ counterLk sync.Mutex
157
+ counters *counters
158
159
// Metrics interface metrics
160
dupMetric metrics.Histogram
@@ -173,6 +168,16 @@ type Bitswap struct {
168
sessIDLk sync.Mutex
169
}
170
171
+type counters struct {
172
+ blocksRecvd uint64
173
+ dupBlocksRecvd uint64
174
+ dupDataRecvd uint64
175
+ blocksSent uint64
176
+ dataSent uint64
177
+ dataRecvd uint64
178
+ messagesRecvd uint64
179
+}
180
+
181
type blockRequest struct {
182
Cid *cid.Cid
183
Ctx context.Context
@@ -338,7 +343,7 @@ func (bs *Bitswap) SessionsForBlock(c *cid.Cid) []*Session {
343
}
344
345
func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg.BitSwapMessage) {
341
- atomic.AddUint64(&bs.messagesRecvd, 1)
346
+ atomic.AddUint64(&bs.counters.messagesRecvd, 1)
347
348
// This call records changes to wantlists, blocks received,
349
// and number of bytes transfered.
@@ -403,12 +408,13 @@ func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
408
409
bs.counterLk.Lock()
410
defer bs.counterLk.Unlock()
411
+ c := bs.counters
412
407
- bs.blocksRecvd++
408
- bs.dataRecvd += uint64(len(b.RawData()))
413
+ c.blocksRecvd++
414
+ c.dataRecvd += uint64(len(b.RawData()))
415
if has {
410
- bs.dupBlocksRecvd++
411
- bs.dupDataRecvd += uint64(blkLen)
416
+ c.dupBlocksRecvd++
417
+ c.dupDataRecvd += uint64(blkLen)
418
}
419
}
420
exchange/bitswap/bitswap_test.go
+1
-1
@@ -291,7 +291,7 @@ func TestEmptyKey(t *testing.T) {
291
}
292
}
293
294
-func assertStat(st *Stat, sblks, rblks int, sdata, rdata uint64) error {
294
+func assertStat(st *Stat, sblks, rblks, sdata, rdata uint64) error {
295
if sblks != st.BlocksSent {
296
return fmt.Errorf("mismatch in blocks sent: %d vs %d", sblks, st.BlocksSent)
297
}
exchange/bitswap/session_test.go
+2
-2
@@ -103,8 +103,8 @@ func TestSessionBetweenPeers(t *testing.T) {
103
}
104
}
105
for _, is := range inst[2:] {
106
- if is.Exchange.messagesRecvd > 2 {
107
- t.Fatal("uninvolved nodes should only receive two messages", is.Exchange.messagesRecvd)
106
+ if is.Exchange.counters.messagesRecvd > 2 {
107
+ t.Fatal("uninvolved nodes should only receive two messages", is.Exchange.counters.messagesRecvd)
108
}
109
}
110
}
exchange/bitswap/stat.go
+10
-9
@@ -10,11 +10,11 @@ type Stat struct {
10
ProvideBufLen int
11
Wantlist []*cid.Cid
12
Peers []string
13
- BlocksReceived int
13
+ BlocksReceived uint64
14
DataReceived uint64
15
- BlocksSent int
15
+ BlocksSent uint64
16
DataSent uint64
17
- DupBlksReceived int
17
+ DupBlksReceived uint64
18
DupDataReceived uint64
19
}
20
@@ -23,12 +23,13 @@ func (bs *Bitswap) Stat() (*Stat, error) {
23
st.ProvideBufLen = len(bs.newBlocks)
24
st.Wantlist = bs.GetWantlist()
25
bs.counterLk.Lock()
26
- st.BlocksReceived = bs.blocksRecvd
27
- st.DupBlksReceived = bs.dupBlocksRecvd
28
- st.DupDataReceived = bs.dupDataRecvd
29
- st.BlocksSent = bs.blocksSent
30
- st.DataSent = bs.dataSent
31
- st.DataReceived = bs.dataRecvd
26
+ c := bs.counters
27
+ st.BlocksReceived = c.blocksRecvd
28
+ st.DupBlksReceived = c.dupBlocksRecvd
29
+ st.DupDataReceived = c.dupDataRecvd
30
+ st.BlocksSent = c.blocksSent
31
+ st.DataSent = c.dataSent
32
+ st.DataReceived = c.dataRecvd
33
bs.counterLk.Unlock()
34
35
for _, p := range bs.engine.Peers() {
exchange/bitswap/workers.go
+2
-2
@@ -73,8 +73,8 @@ func (bs *Bitswap) taskWorker(ctx context.Context, id int) {
73
74
bs.wm.SendBlock(ctx, envelope)
75
bs.counterLk.Lock()
76
- bs.blocksSent++
77
- bs.dataSent += uint64(len(envelope.Block.RawData()))
76
+ bs.counters.blocksSent++
77
+ bs.counters.dataSent += uint64(len(envelope.Block.RawData()))
78
bs.counterLk.Unlock()
79
case <-ctx.Done():
80
return