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

downloader: small changes #13334

Merged
merged 2 commits into from
Jan 10, 2025
Merged
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
69 changes: 23 additions & 46 deletions erigon-lib/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2221,52 +2221,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 @@ -2315,6 +2274,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
Loading