Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow bitswap stat to return total number of bytes wasted #1752

Merged
merged 1 commit into from
Sep 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/commands/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
8 changes: 5 additions & 3 deletions exchange/bitswap/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type Bitswap struct {
counterLk sync.Mutex
blocksRecvd int
dupBlocksRecvd int
dupDataRecvd uint64
}

type blockRequest struct {
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions exchange/bitswap/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Stat struct {
Peers []string
BlocksReceived int
DupBlksReceived int
DupDataReceived uint64
}

func (bs *Bitswap) Stat() (*Stat, error) {
Expand All @@ -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() {
Expand Down