Commit c8f38296 authored by Jeromy's avatar Jeromy

extract bitswap metrics to separate struct for 64bit alignment

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent 194f8988
...@@ -99,6 +99,7 @@ func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork, ...@@ -99,6 +99,7 @@ func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork,
newBlocks: make(chan *cid.Cid, HasBlockBufferSize), newBlocks: make(chan *cid.Cid, HasBlockBufferSize),
provideKeys: make(chan *cid.Cid, provideKeysBufferSize), provideKeys: make(chan *cid.Cid, provideKeysBufferSize),
wm: NewWantManager(ctx, network), wm: NewWantManager(ctx, network),
counters: new(counters),
dupMetric: dupHist, dupMetric: dupHist,
allMetric: allHist, allMetric: allHist,
...@@ -152,14 +153,8 @@ type Bitswap struct { ...@@ -152,14 +153,8 @@ type Bitswap struct {
process process.Process process process.Process
// Counters for various statistics // Counters for various statistics
counterLk sync.Mutex counterLk sync.Mutex
blocksRecvd int counters *counters
dupBlocksRecvd int
dupDataRecvd uint64
blocksSent int
dataSent uint64
dataRecvd uint64
messagesRecvd uint64
// Metrics interface metrics // Metrics interface metrics
dupMetric metrics.Histogram dupMetric metrics.Histogram
...@@ -173,6 +168,16 @@ type Bitswap struct { ...@@ -173,6 +168,16 @@ type Bitswap struct {
sessIDLk sync.Mutex sessIDLk sync.Mutex
} }
type counters struct {
blocksRecvd uint64
dupBlocksRecvd uint64
dupDataRecvd uint64
blocksSent uint64
dataSent uint64
dataRecvd uint64
messagesRecvd uint64
}
type blockRequest struct { type blockRequest struct {
Cid *cid.Cid Cid *cid.Cid
Ctx context.Context Ctx context.Context
...@@ -338,7 +343,7 @@ func (bs *Bitswap) SessionsForBlock(c *cid.Cid) []*Session { ...@@ -338,7 +343,7 @@ func (bs *Bitswap) SessionsForBlock(c *cid.Cid) []*Session {
} }
func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg.BitSwapMessage) { func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg.BitSwapMessage) {
atomic.AddUint64(&bs.messagesRecvd, 1) atomic.AddUint64(&bs.counters.messagesRecvd, 1)
// This call records changes to wantlists, blocks received, // This call records changes to wantlists, blocks received,
// and number of bytes transfered. // and number of bytes transfered.
...@@ -403,12 +408,13 @@ func (bs *Bitswap) updateReceiveCounters(b blocks.Block) { ...@@ -403,12 +408,13 @@ func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
bs.counterLk.Lock() bs.counterLk.Lock()
defer bs.counterLk.Unlock() defer bs.counterLk.Unlock()
c := bs.counters
bs.blocksRecvd++ c.blocksRecvd++
bs.dataRecvd += uint64(len(b.RawData())) c.dataRecvd += uint64(len(b.RawData()))
if has { if has {
bs.dupBlocksRecvd++ c.dupBlocksRecvd++
bs.dupDataRecvd += uint64(blkLen) c.dupDataRecvd += uint64(blkLen)
} }
} }
......
...@@ -291,7 +291,7 @@ func TestEmptyKey(t *testing.T) { ...@@ -291,7 +291,7 @@ func TestEmptyKey(t *testing.T) {
} }
} }
func assertStat(st *Stat, sblks, rblks int, sdata, rdata uint64) error { func assertStat(st *Stat, sblks, rblks, sdata, rdata uint64) error {
if sblks != st.BlocksSent { if sblks != st.BlocksSent {
return fmt.Errorf("mismatch in blocks sent: %d vs %d", sblks, st.BlocksSent) return fmt.Errorf("mismatch in blocks sent: %d vs %d", sblks, st.BlocksSent)
} }
......
...@@ -103,8 +103,8 @@ func TestSessionBetweenPeers(t *testing.T) { ...@@ -103,8 +103,8 @@ func TestSessionBetweenPeers(t *testing.T) {
} }
} }
for _, is := range inst[2:] { for _, is := range inst[2:] {
if is.Exchange.messagesRecvd > 2 { if is.Exchange.counters.messagesRecvd > 2 {
t.Fatal("uninvolved nodes should only receive two messages", is.Exchange.messagesRecvd) t.Fatal("uninvolved nodes should only receive two messages", is.Exchange.counters.messagesRecvd)
} }
} }
} }
......
...@@ -10,11 +10,11 @@ type Stat struct { ...@@ -10,11 +10,11 @@ type Stat struct {
ProvideBufLen int ProvideBufLen int
Wantlist []*cid.Cid Wantlist []*cid.Cid
Peers []string Peers []string
BlocksReceived int BlocksReceived uint64
DataReceived uint64 DataReceived uint64
BlocksSent int BlocksSent uint64
DataSent uint64 DataSent uint64
DupBlksReceived int DupBlksReceived uint64
DupDataReceived uint64 DupDataReceived uint64
} }
...@@ -23,12 +23,13 @@ func (bs *Bitswap) Stat() (*Stat, error) { ...@@ -23,12 +23,13 @@ func (bs *Bitswap) Stat() (*Stat, error) {
st.ProvideBufLen = len(bs.newBlocks) st.ProvideBufLen = len(bs.newBlocks)
st.Wantlist = bs.GetWantlist() st.Wantlist = bs.GetWantlist()
bs.counterLk.Lock() bs.counterLk.Lock()
st.BlocksReceived = bs.blocksRecvd c := bs.counters
st.DupBlksReceived = bs.dupBlocksRecvd st.BlocksReceived = c.blocksRecvd
st.DupDataReceived = bs.dupDataRecvd st.DupBlksReceived = c.dupBlocksRecvd
st.BlocksSent = bs.blocksSent st.DupDataReceived = c.dupDataRecvd
st.DataSent = bs.dataSent st.BlocksSent = c.blocksSent
st.DataReceived = bs.dataRecvd st.DataSent = c.dataSent
st.DataReceived = c.dataRecvd
bs.counterLk.Unlock() bs.counterLk.Unlock()
for _, p := range bs.engine.Peers() { for _, p := range bs.engine.Peers() {
......
...@@ -73,8 +73,8 @@ func (bs *Bitswap) taskWorker(ctx context.Context, id int) { ...@@ -73,8 +73,8 @@ func (bs *Bitswap) taskWorker(ctx context.Context, id int) {
bs.wm.SendBlock(ctx, envelope) bs.wm.SendBlock(ctx, envelope)
bs.counterLk.Lock() bs.counterLk.Lock()
bs.blocksSent++ bs.counters.blocksSent++
bs.dataSent += uint64(len(envelope.Block.RawData())) bs.counters.dataSent += uint64(len(envelope.Block.RawData()))
bs.counterLk.Unlock() bs.counterLk.Unlock()
case <-ctx.Done(): case <-ctx.Done():
return return
......
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