Skip to content

Commit

Permalink
downloader: small changes (#13334)
Browse files Browse the repository at this point in the history
Removed the rate calculation logic from the `ReCalcStats` function and
replaced it with a call to the new `calculateRates` function.

---------

Co-authored-by: awskii <[email protected]>
  • Loading branch information
dvovk and awskii authored Jan 10, 2025
1 parent f5c815d commit ea136cb
Showing 1 changed file with 23 additions and 46 deletions.
69 changes: 23 additions & 46 deletions erigon-lib/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2230,52 +2230,11 @@ func (d *Downloader) ReCalcStats(interval time.Duration) {
}
}

decay := func(prev uint64) uint64 {
switch {
case prev < 1000:
return prev / 16
case stats.FlushRate < 10000:
return prev / 8
case stats.FlushRate < 100000:
return prev / 4
default:
return prev / 2
}
}

if stats.BytesDownload > prevStats.BytesDownload {
stats.DownloadRate = (stats.BytesDownload - prevStats.BytesDownload) / uint64(interval.Seconds())
} else {
stats.DownloadRate = decay(prevStats.DownloadRate)
}

if stats.BytesHashed > prevStats.BytesHashed {
stats.HashRate = (stats.BytesHashed - prevStats.BytesHashed) / uint64(interval.Seconds())
} else {
stats.HashRate = decay(prevStats.HashRate)
}

if stats.BytesCompleted > stats.BytesTotal {
stats.BytesCompleted = stats.BytesTotal
}

if stats.BytesCompleted > prevStats.BytesCompleted {
stats.CompletionRate = (stats.BytesCompleted - prevStats.BytesCompleted) / uint64(interval.Seconds())
} else {
stats.CompletionRate = decay(prevStats.CompletionRate)
}

if stats.BytesFlushed > prevStats.BytesFlushed {
stats.FlushRate = (stats.BytesFlushed - prevStats.BytesFlushed) / uint64(interval.Seconds())
} else {
stats.FlushRate = decay(prevStats.FlushRate)
}

if stats.BytesUpload > prevStats.BytesUpload {
stats.UploadRate = (stats.BytesUpload - prevStats.BytesUpload) / uint64(interval.Seconds())
} else {
stats.UploadRate = decay(prevStats.UploadRate)
}
stats.DownloadRate = calculateRate(stats.BytesDownload, prevStats.BytesDownload, prevStats.DownloadRate, interval)
stats.HashRate = calculateRate(stats.BytesHashed, prevStats.BytesHashed, prevStats.HashRate, interval)
stats.FlushRate = calculateRate(stats.BytesFlushed, prevStats.BytesFlushed, prevStats.FlushRate, interval)
stats.UploadRate = calculateRate(stats.BytesUpload, prevStats.BytesUpload, prevStats.UploadRate, interval)
stats.CompletionRate = calculateRate(stats.BytesCompleted, prevStats.BytesCompleted, prevStats.CompletionRate, interval)

if stats.BytesTotal == 0 {
stats.Progress = 0
Expand Down Expand Up @@ -2324,6 +2283,24 @@ func (d *Downloader) ReCalcStats(interval time.Duration) {
}
}

// Calculating rate with decay in order to avoid rate spikes
func calculateRate(current, previous uint64, prevRate uint64, interval time.Duration) uint64 {
if current > previous {
return (current - previous) / uint64(interval.Seconds())
}

switch {
case prevRate < 1000:
return prevRate / 16
case prevRate < 10000:
return prevRate / 8
case prevRate < 100000:
return prevRate / 4
default:
return prevRate / 2
}
}

type filterWriter struct {
files map[string][]byte
remainder []byte
Expand Down

0 comments on commit ea136cb

Please sign in to comment.