allow bitswap stat to output wasted bytes
bitswap stat can now track bytes that are wasted by receiving duplicate blocks. ps, gitcop smells License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Sep 25, 2015 at 14:02 UTC
0c7421643da0657c0e5b04f598ad9cf287b429ce
3 files changed
+8
-3
core/commands/bitswap.go
+1
@@ -156,6 +156,7 @@ var bitswapStatCmd = &cmds.Command{
156
fmt.Fprintf(buf, "\tprovides buffer: %d / %d\n", out.ProvideBufLen, bitswap.HasBlockBufferSize)
157
fmt.Fprintf(buf, "\tblocks received: %d\n", out.BlocksReceived)
158
fmt.Fprintf(buf, "\tdup blocks received: %d\n", out.DupBlksReceived)
159
+ fmt.Fprintf(buf, "\tdup data received: %d\n", out.DupDataReceived)
160
fmt.Fprintf(buf, "\twantlist [%d keys]\n", len(out.Wantlist))
161
for _, k := range out.Wantlist {
162
fmt.Fprintf(buf, "\t\t%s\n", k.B58String())
exchange/bitswap/bitswap.go
+5
-3
@@ -131,6 +131,7 @@ type Bitswap struct {
131
counterLk sync.Mutex
132
blocksRecvd int
133
dupBlocksRecvd int
134
+ dupDataRecvd uint64
135
}
136
137
type blockRequest struct {
@@ -320,7 +321,7 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
321
go func(b *blocks.Block) {
322
defer wg.Done()
323
323
- if err := bs.updateReceiveCounters(b.Key()); err != nil {
324
+ if err := bs.updateReceiveCounters(b); err != nil {
325
return // ignore error, is either logged previously, or ErrAlreadyHaveBlock
326
}
327
@@ -338,17 +339,18 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
339
340
var ErrAlreadyHaveBlock = errors.New("already have block")
341
341
-func (bs *Bitswap) updateReceiveCounters(k key.Key) error {
342
+func (bs *Bitswap) updateReceiveCounters(b *blocks.Block) error {
343
bs.counterLk.Lock()
344
defer bs.counterLk.Unlock()
345
bs.blocksRecvd++
345
- has, err := bs.blockstore.Has(k)
346
+ has, err := bs.blockstore.Has(b.Key())
347
if err != nil {
348
log.Infof("blockstore.Has error: %s", err)
349
return err
350
}
351
if err == nil && has {
352
bs.dupBlocksRecvd++
353
+ bs.dupDataRecvd += uint64(len(b.Data))
354
}
355
356
if has {
exchange/bitswap/stat.go
+2
@@ -11,6 +11,7 @@ type Stat struct {
11
Peers []string
12
BlocksReceived int
13
DupBlksReceived int
14
+ DupDataReceived uint64
15
}
16
17
func (bs *Bitswap) Stat() (*Stat, error) {
@@ -20,6 +21,7 @@ func (bs *Bitswap) Stat() (*Stat, error) {
21
bs.counterLk.Lock()
22
st.BlocksReceived = bs.blocksRecvd
23
st.DupBlksReceived = bs.dupBlocksRecvd
24
+ st.DupDataReceived = bs.dupDataRecvd
25
bs.counterLk.Unlock()
26
27
for _, p := range bs.engine.Peers() {