Commit 0c742164 authored by Jeromy's avatar Jeromy

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: default avatarJeromy <jeromyj@gmail.com>
parent 5b981137
......@@ -156,6 +156,7 @@ var bitswapStatCmd = &cmds.Command{
fmt.Fprintf(buf, "\tprovides buffer: %d / %d\n", out.ProvideBufLen, bitswap.HasBlockBufferSize)
fmt.Fprintf(buf, "\tblocks received: %d\n", out.BlocksReceived)
fmt.Fprintf(buf, "\tdup blocks received: %d\n", out.DupBlksReceived)
fmt.Fprintf(buf, "\tdup data received: %d\n", out.DupDataReceived)
fmt.Fprintf(buf, "\twantlist [%d keys]\n", len(out.Wantlist))
for _, k := range out.Wantlist {
fmt.Fprintf(buf, "\t\t%s\n", k.B58String())
......
......@@ -131,6 +131,7 @@ type Bitswap struct {
counterLk sync.Mutex
blocksRecvd int
dupBlocksRecvd int
dupDataRecvd uint64
}
type blockRequest struct {
......@@ -320,7 +321,7 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
go func(b *blocks.Block) {
defer wg.Done()
if err := bs.updateReceiveCounters(b.Key()); err != nil {
if err := bs.updateReceiveCounters(b); err != nil {
return // ignore error, is either logged previously, or ErrAlreadyHaveBlock
}
......@@ -338,17 +339,18 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
var ErrAlreadyHaveBlock = errors.New("already have block")
func (bs *Bitswap) updateReceiveCounters(k key.Key) error {
func (bs *Bitswap) updateReceiveCounters(b *blocks.Block) error {
bs.counterLk.Lock()
defer bs.counterLk.Unlock()
bs.blocksRecvd++
has, err := bs.blockstore.Has(k)
has, err := bs.blockstore.Has(b.Key())
if err != nil {
log.Infof("blockstore.Has error: %s", err)
return err
}
if err == nil && has {
bs.dupBlocksRecvd++
bs.dupDataRecvd += uint64(len(b.Data))
}
if has {
......
......@@ -11,6 +11,7 @@ type Stat struct {
Peers []string
BlocksReceived int
DupBlksReceived int
DupDataReceived uint64
}
func (bs *Bitswap) Stat() (*Stat, error) {
......@@ -20,6 +21,7 @@ func (bs *Bitswap) Stat() (*Stat, error) {
bs.counterLk.Lock()
st.BlocksReceived = bs.blocksRecvd
st.DupBlksReceived = bs.dupBlocksRecvd
st.DupDataReceived = bs.dupDataRecvd
bs.counterLk.Unlock()
for _, p := range bs.engine.Peers() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment