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

les: use new atomic types #27856

Merged
merged 3 commits into from
Aug 23, 2023
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
13 changes: 6 additions & 7 deletions les/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func testCapacityAPI(t *testing.T, clientCount int) {
var wg sync.WaitGroup
stop := make(chan struct{})

reqCount := make([]uint64, len(clientRpcClients))
reqCount := make([]atomic.Uint64, len(clientRpcClients))

// Send light request like crazy.
for i, c := range clientRpcClients {
Expand All @@ -157,7 +157,7 @@ func testCapacityAPI(t *testing.T, clientCount int) {
defer wg.Done()

queue := make(chan struct{}, 100)
reqCount[i] = 0
reqCount[i].Store(0)
for {
select {
case queue <- struct{}{}:
Expand All @@ -173,8 +173,7 @@ func testCapacityAPI(t *testing.T, clientCount int) {
wg.Done()
<-queue
if ok {
count := atomic.AddUint64(&reqCount[i], 1)
if count%10000 == 0 {
if reqCount[i].Add(1)%10000 == 0 {
freezeClient(ctx, t, serverRpcClient, clients[i].ID())
}
}
Expand All @@ -192,7 +191,7 @@ func testCapacityAPI(t *testing.T, clientCount int) {
processedSince := func(start []uint64) []uint64 {
res := make([]uint64, len(reqCount))
for i := range reqCount {
res[i] = atomic.LoadUint64(&reqCount[i])
res[i] = reqCount[i].Load()
if start != nil {
res[i] -= start[i]
}
Expand Down Expand Up @@ -292,8 +291,8 @@ func testCapacityAPI(t *testing.T, clientCount int) {
close(stop)
wg.Wait()

for i, count := range reqCount {
t.Log("client", i, "processed", count)
for i := range reqCount {
t.Log("client", i, "processed", reqCount[i].Load())
}
return true
}) {
Expand Down
10 changes: 5 additions & 5 deletions les/costtracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ type costTracker struct {
reqInfoCh chan reqInfo
totalRechargeCh chan uint64

stats map[uint64][]uint64 // Used for testing purpose.
stats map[uint64][]atomic.Uint64 // Used for testing purpose.

// TestHooks
testing bool // Disable real cost evaluation for testing purpose.
Expand All @@ -152,9 +152,9 @@ func newCostTracker(db ethdb.Database, config *ethconfig.Config) (*costTracker,
ct.outSizeFactor = utilTarget / float64(config.LightEgress)
}
if makeCostStats {
ct.stats = make(map[uint64][]uint64)
ct.stats = make(map[uint64][]atomic.Uint64)
for code := range reqAvgTimeCost {
ct.stats[code] = make([]uint64, 10)
ct.stats[code] = make([]atomic.Uint64, 10)
}
}
ct.gfLoop()
Expand Down Expand Up @@ -423,7 +423,7 @@ func (ct *costTracker) updateStats(code, amount, servingTime, realCost uint64) {
l++
realCost >>= 1
}
atomic.AddUint64(&ct.stats[code][l], 1)
ct.stats[code][l].Add(1)
}
}

Expand Down Expand Up @@ -454,7 +454,7 @@ func (ct *costTracker) printStats() {
return
}
for code, arr := range ct.stats {
log.Info("Request cost statistics", "code", code, "1/16", arr[0], "1/8", arr[1], "1/4", arr[2], "1/2", arr[3], "1", arr[4], "2", arr[5], "4", arr[6], "8", arr[7], "16", arr[8], ">16", arr[9])
log.Info("Request cost statistics", "code", code, "1/16", arr[0].Load(), "1/8", arr[1].Load(), "1/4", arr[2].Load(), "1/2", arr[3].Load(), "1", arr[4].Load(), "2", arr[5].Load(), "4", arr[6].Load(), "8", arr[7].Load(), "16", arr[8].Load(), ">16", arr[9].Load())
}
}

Expand Down
13 changes: 7 additions & 6 deletions les/servingqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import (
// servingQueue allows running tasks in a limited number of threads and puts the
// waiting tasks in a priority queue
type servingQueue struct {
recentTime, queuedTime, servingTimeDiff uint64
burstLimit, burstDropLimit uint64
burstDecRate float64
lastUpdate mclock.AbsTime
recentTime, queuedTime uint64
servingTimeDiff atomic.Uint64
burstLimit, burstDropLimit uint64
burstDecRate float64
lastUpdate mclock.AbsTime

queueAddCh, queueBestCh chan *servingTask
stopThreadCh, quit chan struct{}
Expand Down Expand Up @@ -100,7 +101,7 @@ func (t *servingTask) done() uint64 {
t.timeAdded = t.servingTime
if t.expTime > diff {
t.expTime -= diff
atomic.AddUint64(&t.sq.servingTimeDiff, t.expTime)
t.sq.servingTimeDiff.Add(t.expTime)
} else {
t.expTime = 0
}
Expand Down Expand Up @@ -243,7 +244,7 @@ func (sq *servingQueue) freezePeers() {

// updateRecentTime recalculates the recent serving time value
func (sq *servingQueue) updateRecentTime() {
subTime := atomic.SwapUint64(&sq.servingTimeDiff, 0)
subTime := sq.servingTimeDiff.Swap(0)
now := mclock.Now()
dt := now - sq.lastUpdate
sq.lastUpdate = now
Expand Down