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