Skip to content

Commit 742a7f9

Browse files
committed
core, metrics: switch some invalid counters to gauges (ethereum#20047)
1 parent 67825d8 commit 742a7f9

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

core/tx_pool.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ var (
110110
invalidTxMeter = metrics.NewRegisteredMeter("txpool/invalid", nil)
111111
underpricedTxMeter = metrics.NewRegisteredMeter("txpool/underpriced", nil)
112112

113-
pendingCounter = metrics.NewRegisteredCounter("txpool/pending", nil)
114-
queuedCounter = metrics.NewRegisteredCounter("txpool/queued", nil)
115-
localCounter = metrics.NewRegisteredCounter("txpool/local", nil)
113+
pendingGauge = metrics.NewRegisteredGauge("txpool/pending", nil)
114+
queuedGauge = metrics.NewRegisteredGauge("txpool/queued", nil)
115+
localGauge = metrics.NewRegisteredGauge("txpool/local", nil)
116116
)
117117

118118
// TxStatus is the current status of a transaction as seen by the pool.
@@ -730,7 +730,7 @@ func (pool *TxPool) add(tx *types.Transaction, local bool) (replaced bool, err e
730730
}
731731
}
732732
if local || pool.locals.contains(from) {
733-
localCounter.Inc(1)
733+
localGauge.Inc(1)
734734
}
735735
pool.journalTx(from, tx)
736736

@@ -760,7 +760,7 @@ func (pool *TxPool) enqueueTx(hash common.Hash, tx *types.Transaction) (bool, er
760760
queuedReplaceMeter.Mark(1)
761761
} else {
762762
// Nothing was replaced, bump the queued counter
763-
queuedCounter.Inc(1)
763+
queuedGauge.Inc(1)
764764
}
765765
if pool.all.Get(hash) == nil {
766766
pool.all.Add(tx)
@@ -809,7 +809,7 @@ func (pool *TxPool) promoteTx(addr common.Address, hash common.Hash, tx *types.T
809809
pendingReplaceMeter.Mark(1)
810810
} else {
811811
// Nothing was replaced, bump the pending counter
812-
pendingCounter.Inc(1)
812+
pendingGauge.Inc(1)
813813
}
814814
// Failsafe to work around direct pending inserts (tests)
815815
if pool.all.Get(hash) == nil {
@@ -840,7 +840,7 @@ func (pool *TxPool) promoteSpecialTx(addr common.Address, tx *types.Transaction)
840840
pendingReplaceMeter.Mark(1)
841841
} else {
842842
// Nothing was replaced, bump the pending counter
843-
pendingCounter.Inc(1)
843+
pendingGauge.Inc(1)
844844
}
845845
list.txs.Put(tx)
846846
if cost := tx.Cost(); list.costcap.Cmp(cost) < 0 {
@@ -980,7 +980,7 @@ func (pool *TxPool) removeTx(hash common.Hash, outofbound bool) {
980980
pool.priced.Removed(1)
981981
}
982982
if pool.locals.contains(addr) {
983-
localCounter.Dec(1)
983+
localGauge.Dec(1)
984984
}
985985
// Remove the transaction from the pending lists and reset the account nonce
986986
if pending := pool.pending[addr]; pending != nil {
@@ -997,15 +997,15 @@ func (pool *TxPool) removeTx(hash common.Hash, outofbound bool) {
997997
// Update the account nonce if needed
998998
pool.pendingNonces.setIfLower(addr, tx.Nonce())
999999
// Reduce the pending counter
1000-
pendingCounter.Dec(int64(1 + len(invalids)))
1000+
pendingGauge.Dec(int64(1 + len(invalids)))
10011001
return
10021002
}
10031003
}
10041004
// Transaction is in the future queue
10051005
if future := pool.queue[addr]; future != nil {
10061006
if removed, _ := future.Remove(tx); removed {
10071007
// Reduce the queued counter
1008-
queuedCounter.Dec(1)
1008+
queuedGauge.Dec(1)
10091009
}
10101010
if future.Empty() {
10111011
delete(pool.queue, addr)
@@ -1313,7 +1313,7 @@ func (pool *TxPool) promoteExecutables(accounts []common.Address) []*types.Trans
13131313
promoted = append(promoted, tx)
13141314
}
13151315
}
1316-
queuedCounter.Dec(int64(len(readies)))
1316+
queuedGauge.Dec(int64(len(readies)))
13171317

13181318
// Drop all transactions over the allowed limit
13191319
var caps types.Transactions
@@ -1328,9 +1328,9 @@ func (pool *TxPool) promoteExecutables(accounts []common.Address) []*types.Trans
13281328
}
13291329
// Mark all the items dropped as removed
13301330
pool.priced.Removed(len(forwards) + len(drops) + len(caps))
1331-
queuedCounter.Dec(int64(len(forwards) + len(drops) + len(caps)))
1331+
queuedGauge.Dec(int64(len(forwards) + len(drops) + len(caps)))
13321332
if pool.locals.contains(addr) {
1333-
localCounter.Dec(int64(len(forwards) + len(drops) + len(caps)))
1333+
localGauge.Dec(int64(len(forwards) + len(drops) + len(caps)))
13341334
}
13351335
// Delete the entire queue entry if it became empty.
13361336
if list.Empty() {
@@ -1389,9 +1389,9 @@ func (pool *TxPool) truncatePending() {
13891389
log.Trace("Removed fairness-exceeding pending transaction", "hash", hash)
13901390
}
13911391
pool.priced.Removed(len(caps))
1392-
pendingCounter.Dec(int64(len(caps)))
1392+
pendingGauge.Dec(int64(len(caps)))
13931393
if pool.locals.contains(offenders[i]) {
1394-
localCounter.Dec(int64(len(caps)))
1394+
localGauge.Dec(int64(len(caps)))
13951395
}
13961396
pending--
13971397
}
@@ -1416,9 +1416,9 @@ func (pool *TxPool) truncatePending() {
14161416
log.Trace("Removed fairness-exceeding pending transaction", "hash", hash)
14171417
}
14181418
pool.priced.Removed(len(caps))
1419-
pendingCounter.Dec(int64(len(caps)))
1419+
pendingGauge.Dec(int64(len(caps)))
14201420
if pool.locals.contains(addr) {
1421-
localCounter.Dec(int64(len(caps)))
1421+
localGauge.Dec(int64(len(caps)))
14221422
}
14231423
pending--
14241424
}
@@ -1506,9 +1506,9 @@ func (pool *TxPool) demoteUnexecutables() {
15061506
log.Trace("Demoting pending transaction", "hash", hash)
15071507
pool.enqueueTx(hash, tx)
15081508
}
1509-
pendingCounter.Dec(int64(len(olds) + len(drops) + len(invalids)))
1509+
pendingGauge.Dec(int64(len(olds) + len(drops) + len(invalids)))
15101510
if pool.locals.contains(addr) {
1511-
localCounter.Dec(int64(len(olds) + len(drops) + len(invalids)))
1511+
localGauge.Dec(int64(len(olds) + len(drops) + len(invalids)))
15121512
}
15131513
// If there's a gap in front, alert (should never happen) and postpone all transactions
15141514
if list.Len() > 0 && list.txs.Get(nonce) == nil {
@@ -1518,7 +1518,7 @@ func (pool *TxPool) demoteUnexecutables() {
15181518
log.Warn("Demoting invalidated transaction", "hash", hash)
15191519
pool.enqueueTx(hash, tx)
15201520
}
1521-
pendingCounter.Dec(int64(len(gapped)))
1521+
pendingGauge.Dec(int64(len(gapped)))
15221522
}
15231523
// Delete the entire queue entry if it became empty.
15241524
if list.Empty() {

metrics/gauge.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import "sync/atomic"
66
type Gauge interface {
77
Snapshot() Gauge
88
Update(int64)
9+
Dec(int64)
10+
Inc(int64)
911
Value() int64
1012
}
1113

@@ -65,6 +67,16 @@ func (GaugeSnapshot) Update(int64) {
6567
panic("Update called on a GaugeSnapshot")
6668
}
6769

70+
// Dec panics.
71+
func (GaugeSnapshot) Dec(int64) {
72+
panic("Dec called on a GaugeSnapshot")
73+
}
74+
75+
// Inc panics.
76+
func (GaugeSnapshot) Inc(int64) {
77+
panic("Inc called on a GaugeSnapshot")
78+
}
79+
6880
// Value returns the value at the time the snapshot was taken.
6981
func (g GaugeSnapshot) Value() int64 { return int64(g) }
7082

@@ -77,6 +89,12 @@ func (NilGauge) Snapshot() Gauge { return NilGauge{} }
7789
// Update is a no-op.
7890
func (NilGauge) Update(v int64) {}
7991

92+
// Dec is a no-op.
93+
func (NilGauge) Dec(i int64) {}
94+
95+
// Inc is a no-op.
96+
func (NilGauge) Inc(i int64) {}
97+
8098
// Value is a no-op.
8199
func (NilGauge) Value() int64 { return 0 }
82100

@@ -101,6 +119,16 @@ func (g *StandardGauge) Value() int64 {
101119
return atomic.LoadInt64(&g.value)
102120
}
103121

122+
// Dec decrements the gauge's current value by the given amount.
123+
func (g *StandardGauge) Dec(i int64) {
124+
atomic.AddInt64(&g.value, -i)
125+
}
126+
127+
// Inc increments the gauge's current value by the given amount.
128+
func (g *StandardGauge) Inc(i int64) {
129+
atomic.AddInt64(&g.value, i)
130+
}
131+
104132
// FunctionalGauge returns value from given function
105133
type FunctionalGauge struct {
106134
value func() int64
@@ -118,3 +146,13 @@ func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) }
118146
func (FunctionalGauge) Update(int64) {
119147
panic("Update called on a FunctionalGauge")
120148
}
149+
150+
// Dec panics.
151+
func (FunctionalGauge) Dec(int64) {
152+
panic("Dec called on a FunctionalGauge")
153+
}
154+
155+
// Inc panics.
156+
func (FunctionalGauge) Inc(int64) {
157+
panic("Inc called on a FunctionalGauge")
158+
}

0 commit comments

Comments
 (0)