Play your Steam library's Windows games on a jailbroken PS5.
C++
Steady the download speed and time left labels
Leonardo Lobatoa8cde7bd9187
diff --git a/src/steam/download_rate.h b/src/steam/download_rate.hindex 3cd36b4..c960f8f 100644--- a/src/steam/download_rate.h+++ b/src/steam/download_rate.h@@ -1,7 +1,7 @@#pragma once#include <algorithm>-#include <array>#include <cstdint>+#include <deque>#include <limits>#include <optional>namespace playden::steam {@@ -9,47 +9,78 @@ struct DownloadEstimate {std::optional<std::uint64_t> bytes_per_second;std::optional<int> eta_seconds;};-// Six fixed buckets cover a recent five-second window; all times are steady-clock milliseconds.+// Download speed and time left for display; all times are steady-clock milliseconds. Speed averages the+// last 8 seconds and time left the last 30, so chunk bursts do not make the labels jump. Shown() changes+// speed at most every 2 seconds and time left every 5, except that a stall shows at once.class DownloadRate {public:+ static constexpr std::int64_t kWarmupMs = 3000, kStallMs = 5000;+ static constexpr std::int64_t kSpeedWindowMs = 8000, kEtaWindowMs = 30000;+ static constexpr std::int64_t kSpeedRefreshMs = 2000, kEtaRefreshMs = 5000;+void Reset() { *this = DownloadRate{}; }void Record(std::uint64_t done, std::int64_t now) {if (now < 0) { Reset(); return; }if (!started_ || now < last_ms_ || done < last_done_) {- Reset(); started_ = true; start_ms_ = now; last_ms_ = now; last_done_ = done;+ Reset(); started_ = true; start_ms_ = progress_ms_ = last_ms_ = now; last_done_ = done;+ samples_.push_back({now, done});return; // A journal's resumed bytes are the baseline, never new traffic.}- const auto second = now / 1000;- auto& bucket = buckets_[second % buckets_.size()];- if (bucket.second != second) bucket = {second, 0};- bucket.bytes += done - last_done_;+ if (done > last_done_) progress_ms_ = now;last_ms_ = now; last_done_ = done;+ // One sample per quarter second is plenty; the baseline stays until a newer sample covers the longest window.+ if (samples_.size() > 1 && now - samples_[samples_.size() - 2].ms < kSampleMs) samples_.back() = {now, done};+ else samples_.push_back({now, done});+ // Keep one sample at or before the longest window's start.+ while (samples_.size() > 2 && samples_[1].ms <= now - kEtaWindowMs) samples_.pop_front();}+ // The current estimate, unthrottled.DownloadEstimate Estimate(std::int64_t now, std::uint64_t done, std::uint64_t total) const {- if (!started_ || now < last_ms_ || now - start_ms_ < 1000) return {};- std::uint64_t bytes = 0;- for (const auto& bucket : buckets_)- if (bucket.second >= 0 && now >= bucket.second * 1000 && now - bucket.second * 1000 < 5000)- bytes += bucket.bytes;- // The counted buckets start at a whole second, so they span 4 to 5 seconds, not always 5.- const auto window = 4000 + now % 1000;- const auto elapsed = std::min<std::int64_t>(window, now - start_ms_);- const long double per_second = static_cast<long double>(bytes) * 1000 / elapsed;+ if (!started_ || now < last_ms_ || now - start_ms_ < kWarmupMs) return {};DownloadEstimate result;- result.bytes_per_second = per_second >= std::numeric_limits<std::uint64_t>::max()- ? std::numeric_limits<std::uint64_t>::max() : static_cast<std::uint64_t>(per_second);- if (total && *result.bytes_per_second) {+ if (now - progress_ms_ >= kStallMs) { result.bytes_per_second = 0; return result; }+ result.bytes_per_second = Average(now, done, kSpeedWindowMs);+ const auto eta_rate = Average(now, done, kEtaWindowMs);+ if (total && eta_rate) {const auto remaining = total > done ? total - done : 0;- const auto seconds = remaining ? (remaining - 1) / *result.bytes_per_second + 1 : 0;+ const auto seconds = remaining ? (remaining - 1) / eta_rate + 1 : 0;result.eta_seconds = static_cast<int>(std::min<std::uint64_t>(seconds, std::numeric_limits<int>::max()));}return result;}+ // The estimate to show: refreshed at most every kSpeedRefreshMs / kEtaRefreshMs.+ DownloadEstimate Shown(std::int64_t now, std::uint64_t done, std::uint64_t total) {+ const auto fresh = Estimate(now, done, total);+ if (!fresh.bytes_per_second) { shown_ = {}; return shown_; }+ const bool was_stalled = !shown_.bytes_per_second || *shown_.bytes_per_second == 0;+ if (was_stalled || *fresh.bytes_per_second == 0 || now - speed_ms_ >= kSpeedRefreshMs) {+ shown_.bytes_per_second = fresh.bytes_per_second; speed_ms_ = now;+ }+ if (!fresh.eta_seconds || !shown_.eta_seconds || now - eta_ms_ >= kEtaRefreshMs) {+ shown_.eta_seconds = fresh.eta_seconds; eta_ms_ = now;+ }+ return shown_;+ }+private:- struct Bucket { std::int64_t second = -1; std::uint64_t bytes = 0; };- std::array<Bucket, 6> buckets_{};+ static constexpr std::int64_t kSampleMs = 250;+ struct Sample { std::int64_t ms = 0; std::uint64_t done = 0; };+ std::uint64_t Average(std::int64_t now, std::uint64_t done, std::int64_t window) const {+ const Sample* reference = &samples_.front();+ for (const auto& sample : samples_) {+ if (sample.ms > now - window) break;+ reference = &sample;+ }+ const auto elapsed = now - reference->ms;+ if (elapsed <= 0 || done <= reference->done) return 0;+ const long double per_second = static_cast<long double>(done - reference->done) * 1000 / elapsed;+ return per_second >= std::numeric_limits<std::uint64_t>::max() ? std::numeric_limits<std::uint64_t>::max()+ : static_cast<std::uint64_t>(per_second);+ }+ std::deque<Sample> samples_;+ DownloadEstimate shown_;bool started_ = false;- std::int64_t start_ms_ = 0, last_ms_ = 0;+ std::int64_t start_ms_ = 0, last_ms_ = 0, progress_ms_ = 0, speed_ms_ = 0, eta_ms_ = 0;std::uint64_t last_done_ = 0;};}diff --git a/src/steam/install_service.cpp b/src/steam/install_service.cppindex 47a0835..421ef61 100644--- a/src/steam/install_service.cpp+++ b/src/steam/install_service.cpp@@ -370,11 +370,8 @@ InstallSnapshot InstallService::Snapshot() {const auto now = RateNowMs();for (auto& job : jobs_) {DownloadEstimate estimate;- if (job.view.state == InstallJobState::kRunning) {- if (job.rate_tick == now / 1000) continue;- job.rate_tick = now / 1000;- estimate = job.rate.Estimate(now, job.view.done_bytes, job.view.total_bytes);- }+ if (job.view.state == InstallJobState::kRunning)+ estimate = job.rate.Shown(now, job.view.done_bytes, job.view.total_bytes);if (job.view.bytes_per_second != estimate.bytes_per_second || job.view.eta_seconds != estimate.eta_seconds) {job.view.bytes_per_second = estimate.bytes_per_second; job.view.eta_seconds = estimate.eta_seconds;++revision_; // Time-only telemetry changes do not write the install registry.@@ -425,7 +422,7 @@ void InstallService::Run() {continue;}job.view.state = InstallJobState::kRunning;- job.rate.Reset(); job.rate_tick = -1;+ job.rate.Reset();job.view.bytes_per_second.reset(); job.view.eta_seconds.reset();next = job.view.id;next_root = RootOn(job.view.volume_id, job.view.id);@@ -464,10 +461,7 @@ void InstallService::Run() {[this, id = *next](std::uint64_t done, std::uint64_t total) {std::lock_guard<std::mutex> lock(mutex_);if (auto* job = Find(id)) {- if (job->view.state == InstallJobState::kRunning) {- job->rate.Record(done, RateNowMs());- job->rate_tick = -1;- }+ if (job->view.state == InstallJobState::kRunning) job->rate.Record(done, RateNowMs());job->view.done_bytes = done;job->view.total_bytes = total;++revision_; // progress only; the registry is unchangeddiff --git a/src/steam/install_service.h b/src/steam/install_service.hindex 59c1184..1de0120 100644--- a/src/steam/install_service.h+++ b/src/steam/install_service.h@@ -121,7 +121,6 @@ class InstallService {struct Job {InstallJobView view;DownloadRate rate;- std::int64_t rate_tick = -1;bool cancel_requested = false; // delete files when the run stops};void Run();diff --git a/tests/download_rate_test.cpp b/tests/download_rate_test.cppindex 526d864..75f67a7 100644--- a/tests/download_rate_test.cpp+++ b/tests/download_rate_test.cpp@@ -5,42 +5,83 @@using namespace playden::steam;#define CHECK(value) do { if (!(value)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #value); std::exit(1); } } while(false)int main() {- DownloadRate rate;- CHECK(!rate.Estimate(0, 0, 100000000).bytes_per_second);- rate.Record(9000000000ull, 0); // resumed bytes seed the baseline; they were not transferred now- CHECK(!rate.Estimate(0, 9000000000ull, 9100000000ull).bytes_per_second);- rate.Record(9002000000ull, 1000);- auto result = rate.Estimate(1000, 9002000000ull, 9100000000ull);- CHECK(result.bytes_per_second == 2000000 && result.eta_seconds == 49);- rate.Record(9004000000ull, 2000);- CHECK(rate.Estimate(2000, 9004000000ull, 9100000000ull).bytes_per_second == 2000000);- // Time alone changes the estimate, even when no callback arrives during a stall.- CHECK(rate.Estimate(4000, 9004000000ull, 9100000000ull).bytes_per_second == 1000000);- result = rate.Estimate(7000, 9004000000ull, 9100000000ull);- CHECK(result.bytes_per_second == 0 && !result.eta_seconds);- rate.Reset();- rate.Record(9004000000ull, 8000);- CHECK(!rate.Estimate(8500, 9004000000ull, 9100000000ull).bytes_per_second);- rate.Record(9005000000ull, 9000);- CHECK(rate.Estimate(9000, 9005000000ull, 0).bytes_per_second == 1000000);- CHECK(!rate.Estimate(9000, 9005000000ull, 0).eta_seconds);- CHECK(rate.Estimate(9000, 9005000000ull, 9005000000ull).eta_seconds == 0);- rate.Record(5, 10000); // regressing progress starts a new baseline- CHECK(!rate.Estimate(10000, 5, 10).bytes_per_second);- rate.Record(6, 11000);- CHECK(rate.Estimate(11000, 6, std::numeric_limits<std::uint64_t>::max()).eta_seconds == std::numeric_limits<int>::max());- rate.Record(10, 500); // a changed clock resets too- CHECK(!rate.Estimate(500, 10, 20).bytes_per_second);- // A steady 10 MB/s reads as about 10 MB/s whatever the time within the second.- DownloadRate steady;- steady.Record(0, 0);- std::uint64_t done = 0;- for (std::int64_t now = 100; now <= 12900; now += 100) {- done += 1000000;- steady.Record(done, now);- if (now >= 10000) {- const auto speed = *steady.Estimate(now, done, done * 2).bytes_per_second;- CHECK(speed >= 9500000 && speed <= 10500000);+ {+ // Resumed bytes seed the baseline; nothing shows during the warm-up.+ DownloadRate rate;+ CHECK(!rate.Estimate(0, 0, 100000000).bytes_per_second);+ rate.Record(9000000000ull, 0);+ rate.Record(9002000000ull, 1000);+ rate.Record(9004000000ull, 2000);+ CHECK(!rate.Estimate(2999, 9004000000ull, 9100000000ull).bytes_per_second);+ rate.Record(9006000000ull, 3000);+ const auto result = rate.Estimate(3000, 9006000000ull, 9100000000ull);+ CHECK(result.bytes_per_second == 2000000 && result.eta_seconds == 47);+ }+ {+ // A steady 10 MB/s reads as 10 MB/s, and time left follows it.+ DownloadRate rate;+ rate.Record(0, 0);+ std::uint64_t done = 0;+ for (std::int64_t now = 100; now <= 40000; now += 100) {+ done += 1000000;+ rate.Record(done, now);+ if (now >= 10000) {+ const auto result = rate.Estimate(now, done, 1000000000);+ CHECK(*result.bytes_per_second >= 9900000 && *result.bytes_per_second <= 10100000);+ const auto expected = static_cast<int>((1000000000 - done) / 10000000);+ CHECK(result.eta_seconds && *result.eta_seconds >= expected - 1 && *result.eta_seconds <= expected + 1);+ }+ }+ }+ {+ // Bursts of 30 MB every third second still read as about 10 MB/s, and the shown values hold still+ // between refreshes.+ DownloadRate rate;+ rate.Record(0, 0);+ std::uint64_t done = 0;+ DownloadEstimate last;+ std::int64_t speed_changed = 0, eta_changed = 0;+ for (std::int64_t now = 100; now <= 60000; now += 100) {+ if (now % 3000 == 0) { done += 30000000; rate.Record(done, now); }+ const auto shown = rate.Shown(now, done, 2000000000);+ if (now >= 12000) {+ CHECK(*shown.bytes_per_second >= 6000000 && *shown.bytes_per_second <= 12000000);+ if (shown.bytes_per_second != last.bytes_per_second) {+ CHECK(now - speed_changed >= DownloadRate::kSpeedRefreshMs); speed_changed = now;+ }+ if (shown.eta_seconds != last.eta_seconds) {+ CHECK(now - eta_changed >= DownloadRate::kEtaRefreshMs); eta_changed = now;+ }+ }+ last = shown;}+ CHECK(speed_changed > 0 && eta_changed > 0);+ }+ {+ // A stall shows at once as zero with no time left, and recovers when bytes arrive again.+ DownloadRate rate;+ rate.Record(0, 0);+ for (std::int64_t now = 1000; now <= 10000; now += 1000) rate.Record(now * 1000, now);+ CHECK(*rate.Shown(10000, 10000000, 100000000).bytes_per_second > 0);+ CHECK(*rate.Shown(14999, 10000000, 100000000).bytes_per_second > 0);+ const auto stalled = rate.Shown(15000, 10000000, 100000000);+ CHECK(stalled.bytes_per_second == 0 && !stalled.eta_seconds);+ rate.Record(11000000, 15100);+ CHECK(*rate.Shown(15100, 11000000, 100000000).bytes_per_second > 0);+ }+ {+ DownloadRate rate;+ rate.Record(1000, 0);+ rate.Record(5000000, 4000);+ CHECK(rate.Estimate(4000, 5000000, 5000000).eta_seconds == 0);+ CHECK(!rate.Estimate(4000, 5000000, 0).eta_seconds);+ CHECK(rate.Estimate(4000, 5000000, std::numeric_limits<std::uint64_t>::max()).eta_seconds ==+ std::numeric_limits<int>::max());+ rate.Record(5, 5000); // regressing progress starts a new baseline+ CHECK(!rate.Estimate(5000, 5, 10).bytes_per_second);+ rate.Record(10, 500); // a changed clock resets too+ CHECK(!rate.Estimate(500, 10, 20).bytes_per_second);+ rate.Reset();+ CHECK(!rate.Shown(9000, 10, 20).bytes_per_second);}}diff --git a/tests/install_service_test.cpp b/tests/install_service_test.cppindex a503aee..b059d9d 100644--- a/tests/install_service_test.cpp+++ b/tests/install_service_test.cpp@@ -78,7 +78,7 @@ class Runner final : public InstallRunner {template <typename Ready>void AwaitAt(int line, InstallService& service, Ready ready) {- const auto deadline = std::chrono::steady_clock::now() + 2s;+ const auto deadline = std::chrono::steady_clock::now() + 6s;while (std::chrono::steady_clock::now() < deadline) {if (ready(service.Snapshot())) return;std::this_thread::sleep_for(1ms);@@ -110,7 +110,7 @@ void LiveTelemetryDecaysWithoutCallbacks(const std::filesystem::path& root) {const auto* job = JobOf(moving, 900);CHECK(job && *job->bytes_per_second <= 100000000 && job->eta_seconds);CHECK(job->done_bytes == 9100000000ull); // only the fresh 100 MB belongs in the rate- const auto deadline = std::chrono::steady_clock::now() + 6s;+ const auto deadline = std::chrono::steady_clock::now() + 9s;bool stalled = false;while (std::chrono::steady_clock::now() < deadline) {const auto snapshot = service.Snapshot();