Play your Steam library's Windows games on a jailbroken PS5.
C++
Add Fastest as a download server choice
Leonardo Lobato99bcbfa6ddc1
diff --git a/README.md b/README.mdindex c564cba..4a12c13 100644--- a/README.md+++ b/README.md@@ -348,8 +348,10 @@ build/homebrew-host/playden-homebrew --simulation --scenario signed-out**Downloads for slow networks**: Playden then downloads 8 pieces at the same time instead of 4. On a fast connectionit can be slower, so turn it off again if downloads get slower. If downloads stay slow, open Settings › Downloads ›**Download server**, use **Test servers** and choose the fastest server. The test downloads up to 160 MB.- Default uses the server Steam lists first. Each provider chooses its nearby server through DNS or anycast,- so your DNS resolver can affect the result.+ Or choose **Fastest**: each download of 200 MB or more then starts with a short test on its own files, uses the+ fastest server and tests again if that server slows down. **Steam default** is still the default: it uses the+ server Steam lists first. Each provider chooses its nearby server through DNS or anycast, so your DNS resolver+ can affect the result.See the [compatibility list](COMPATIBILITY.md) for what we know about each game.diff --git a/src/homebrew/controller.cpp b/src/homebrew/controller.cppindex a62c830..2abc21e 100644--- a/src/homebrew/controller.cpp+++ b/src/homebrew/controller.cpp@@ -1974,7 +1974,8 @@ void Controller::TickInstalls() {case steam::InstallJobState::kFailed: state = JobState::Failed; break;}jobs.push_back({job.id, state, job.done_bytes, job.total_bytes, job.failure, job.volume_id,- job.bytes_per_second, job.eta_seconds, job.held_bytes, checking_licenses});+ job.bytes_per_second, job.eta_seconds, job.held_bytes, checking_licenses,+ state == JobState::Downloading ? job.server_name : std::string()});}std::vector<LiveInstalledGame> installed;for (const auto& game : snapshot.installed) installed.push_back({game.id, game.install_bytes, game.volume_id, game.available});diff --git a/src/homebrew/host.cpp b/src/homebrew/host.cppindex 22b4114..74fb59f 100644--- a/src/homebrew/host.cpp+++ b/src/homebrew/host.cpp@@ -568,7 +568,8 @@ int main(int argc, char** argv) {if (download_tuning_state.downloads_slow_network.load()) tuning.slots = 8;{std::lock_guard lock(download_tuning_state.mutex);- tuning.preferred_server = download_tuning_state.download_server;+ tuning.fastest = download_tuning_state.download_server == playden::steam::kFastestDownloadServer;+ if (!tuning.fastest) tuning.preferred_server = download_tuning_state.download_server;}return tuning;});diff --git a/src/homebrew/ps5.cpp b/src/homebrew/ps5.cppindex a75fe4f..c0ffdb6 100644--- a/src/homebrew/ps5.cpp+++ b/src/homebrew/ps5.cpp@@ -370,7 +370,8 @@ int main() {if (downloads_slow_network.load()) tuning.slots = 8;{std::lock_guard lock(download_server_mutex);- tuning.preferred_server = download_server;+ tuning.fastest = download_server == playden::steam::kFastestDownloadServer;+ if (!tuning.fastest) tuning.preferred_server = download_server;}return tuning;});diff --git a/src/homebrew/simulation.cpp b/src/homebrew/simulation.cppindex bd99c37..f13fa28 100644--- a/src/homebrew/simulation.cpp+++ b/src/homebrew/simulation.cpp@@ -34,11 +34,11 @@#include <type_traits>namespace playden::homebrew {-std::string FormatDownloadRate(std::optional<std::uint64_t> rate, std::optional<int> eta) {+std::string FormatDownloadRate(std::optional<std::uint64_t> rate, std::optional<int> eta, const std::string& server_name) {if (!rate) return "—";char text[64];std::snprintf(text, sizeof(text), "%.1f MB/s", static_cast<double>(*rate) / (1024.0 * 1024.0));- std::string result(text);+ std::string result = server_name.empty() ? std::string(text) : server_name + " · " + text;result += " · ";if (!eta || *eta < 0) return result + "—";if (*eta < 60) return result + "~" + std::to_string(*eta) + " sec left";@@ -2165,6 +2165,7 @@ void Simulation::UpdateInstalls(const std::vector<LiveInstallJob>& jobs, const sif (live_job.state == JobState::Downloading) {job.bytes_per_second = live_job.bytes_per_second;job.eta_seconds = live_job.eta_seconds;+ job.server_name = live_job.server_name;}projected.push_back(std::move(job));}@@ -2727,7 +2728,15 @@ bool Simulation::download_server_test_allowed() const {});}-std::string Simulation::fastest_download_server() const {+// The modal action for the stored choice; a server that is not listed falls back to Steam default.+std::string Simulation::download_server_choice_key() const {+ if (state_.download_server == kFastestDownloadServer) return "download-server-fastest";+ for (std::size_t index = 0; index < download_servers_.servers.size(); ++index)+ if (download_servers_.servers[index].id == state_.download_server) return "download-server-" + std::to_string(index);+ return "download-server-auto";+}++std::string Simulation::best_tested_download_server() const {const DownloadServerResult* fastest = nullptr;for (const auto& server : download_servers_.servers) {for (const auto& rate : download_servers_.rates) {@@ -2743,7 +2752,7 @@ void Simulation::refresh_download_servers(const std::string& focus) {if (modal.id != "download-server") continue;const auto prior = modal.action_keys.empty() ? std::string{} :modal.action_keys[static_cast<std::size_t>(modal.focused_action)];- modal.action_keys = {"download-server-test", "download-server-auto"};+ modal.action_keys = {"download-server-fastest", "download-server-auto"};for (std::size_t index = 0; index < download_servers_.servers.size(); ++index)modal.action_keys.push_back("download-server-" + std::to_string(index));if (download_servers_.phase == "listing" || download_servers_.phase == "idle")@@ -2752,6 +2761,7 @@ void Simulation::refresh_download_servers(const std::string& focus) {modal.action_keys.push_back("download-server-list-failed");modal.action_keys.push_back("download-server-retry");}+ modal.action_keys.push_back("download-server-test");modal.action_keys.push_back("cancel");const auto wanted = focus.empty() ? prior : focus;const auto found = std::find(modal.action_keys.begin(), modal.action_keys.end(), wanted);@@ -2769,14 +2779,15 @@ void Simulation::ApplyLiveDownloadServers(const LiveDownloadServers& snapshot) {download_server_cancel_pending_ = false;download_server_test_requested_ = false;}+ // After a test the focus goes to the fastest tested server (not to Fastest); otherwise to the choice.std::string focus;- const auto chosen = finished ? fastest_download_server() : state_.download_server;- if (finished || (download_server_focus_choice_ && snapshot.phase != "listing" && snapshot.phase != "idle")) {- if (!finished || !chosen.empty()) {- focus = "download-server-auto";- for (std::size_t index = 0; index < snapshot.servers.size(); ++index)- if (snapshot.servers[index].id == chosen) focus = "download-server-" + std::to_string(index);- }+ if (finished) {+ const auto best = best_tested_download_server();+ for (std::size_t index = 0; index < snapshot.servers.size(); ++index)+ if (!best.empty() && snapshot.servers[index].id == best) focus = "download-server-" + std::to_string(index);+ download_server_focus_choice_ = false;+ } else if (download_server_focus_choice_ && snapshot.phase != "listing" && snapshot.phase != "idle") {+ focus = download_server_choice_key();download_server_focus_choice_ = false;}refresh_download_servers(focus);@@ -3635,7 +3646,9 @@ bool Simulation::scenario(const std::string& name) {item.total_bytes = total;item.completed_bytes = done;item.volume_id = "internal";- if (job_state == JobState::Downloading) { item.bytes_per_second = 18'200'000; item.eta_seconds = 152; }+ if (job_state == JobState::Downloading) {+ item.bytes_per_second = 18'200'000; item.eta_seconds = 152; item.server_name = "Fastly";+ }if (job_state == JobState::Downloaded) { item.finished_at = now - 3600; item.completion_applied = true; }state_.jobs.push_back(item);};@@ -4133,8 +4146,13 @@ void Simulation::rebuild_view() {auto& item = row(action_key, RowKind::Action, action_label(action_key), action_key);item.dangerous = modal.destructive && action_key != "cancel";if (modal.id == "download-server") {- if (action_key == "download-server-auto") {- item.title = "Default";+ if (action_key == "download-server-fastest") {+ item.title = "Fastest";+ item.subtitle = "Tests the servers when a download starts and uses the fastest one.";+ item.selected = state_.download_server == kFastestDownloadServer;+ } else if (action_key == "download-server-auto") {+ item.title = "Steam default";+ item.subtitle = "Uses the server Steam lists first.";item.selected = state_.download_server.empty();} else if (action_key == "download-server-listing" || action_key == "download-server-list-failed") {item.title = action_key == "download-server-listing" ? "Getting the server list…" : "Could not get the server list.";@@ -4178,7 +4196,7 @@ void Simulation::rebuild_view() {item.value = value;}}- if (option.id == fastest_download_server()) item.badge = "Fastest";+ if (option.id == best_tested_download_server()) item.badge = "Fastest";}}continue;@@ -5525,7 +5543,8 @@ void Simulation::rebuild_view() {? "8 pieces at a time. Turn it off if downloads got slower.": "Off: 4 pieces at a time. Turn it on if downloads stay near 1 MB/s.";auto& server = add("settings-download-server", RowKind::Action, "Download server", "download-server-picker");- server.value = state_.download_server.empty() ? "Default"+ const bool fastest = state_.download_server == kFastestDownloadServer;+ server.value = state_.download_server.empty() ? "Steam default" : fastest ? "Fastest": download_servers_.preferred_name.empty() ? state_.download_server : download_servers_.preferred_name;bool offered = false;for (const auto& option : download_servers_.servers) if (option.id == state_.download_server) {@@ -5534,9 +5553,9 @@ void Simulation::rebuild_view() {}const bool listed = download_servers_.phase == "listed" || download_servers_.phase == "testing" ||download_servers_.phase == "tested" || download_servers_.phase == "test-failed";- server.subtitle = state_.download_server.empty()- ? "Uses the server Steam lists first. Choose one if downloads are slow."- : listed && !offered ? "Not offered on this network now. Default is used."+ server.subtitle = state_.download_server.empty() ? "Uses the server Steam lists first."+ : fastest ? "Tests the servers when a download starts and uses the fastest one."+ : listed && !offered ? "Not offered on this network now. Steam default is used.": "If it stops answering, Playden uses another one.";}section = "Display";@@ -6723,10 +6742,7 @@ void Simulation::activate() {download_servers_.phase = "listing";}open_modal({"download-server", "Download server", state_.navigation.focus_key, {"download-server-auto", "cancel"}, 0, false});- std::string focus = "download-server-auto";- for (std::size_t index = 0; index < download_servers_.servers.size(); ++index)- if (download_servers_.servers[index].id == state_.download_server) focus = "download-server-" + std::to_string(index);- refresh_download_servers(focus);+ refresh_download_servers(download_server_choice_key());} else if (action_name == "download-server-retry") {if (live()) download_server_requests_.push_back(DownloadServerRequest::List);download_servers_.phase = "listing";@@ -6743,6 +6759,7 @@ void Simulation::activate() {refresh_download_servers();} else if (action_name == "download-server-auto" || action_name.rfind("download-server-", 0) == 0) {if (action_name == "download-server-auto") state_.download_server.clear();+ else if (action_name == "download-server-fastest") state_.download_server = kFastestDownloadServer;else {std::size_t index = 0;const auto number = action_name.substr(std::string("download-server-").size());diff --git a/src/homebrew/simulation.hpp b/src/homebrew/simulation.hppindex ab2de59..bb3c3c4 100644--- a/src/homebrew/simulation.hpp+++ b/src/homebrew/simulation.hpp@@ -75,7 +75,11 @@ enum class MaintenanceKind { ApplySettings, ClearShaderCache, Repair, BackupSave// Whether a game's PS5 title runs now, as the console last answered.enum class TitleActivity { Unknown, Stopped, Running };// Live install state projected from the real install service.-std::string FormatDownloadRate(std::optional<std::uint64_t> bytes_per_second, std::optional<int> eta_seconds);+// The stored download server choice that races the servers when a download starts.+inline constexpr const char* kFastestDownloadServer = "fastest";+// "Fastly · 18.2 MB/s · ~3 min left": the server's name comes first when there is a rate.+std::string FormatDownloadRate(std::optional<std::uint64_t> bytes_per_second, std::optional<int> eta_seconds,+ const std::string& server_name = {});struct LiveInstallJob {GameId game_id;JobState state = JobState::Queued;@@ -87,6 +91,7 @@ struct LiveInstallJob {std::optional<int> eta_seconds;std::uint64_t held_bytes = 0; // installed bytes already on the drivebool checking_licenses = false; // a slow Steam license check before the download starts+ std::string server_name; // while downloading: the download server's display name};struct LiveInstalledGame {GameId game_id;@@ -508,6 +513,7 @@ struct Job {std::uint64_t held_bytes = 0; // live: installed bytes already on the drivestd::int64_t finished_at = 0; // live: when a Downloaded job finished (Unix seconds), 0 unknownbool checking_licenses = false; // live: a slow Steam license check before the download starts+ std::string server_name; // live, while downloading: the download server's display name// A job that is not a game's (the Playden update): its title and the words for its stage and state.std::string title;std::string stage_text;@@ -669,7 +675,9 @@ struct State {bool cloud_saves_auto = false; // Cloud saves move before Play, after sessions and at startup (opt-in)bool beta_updates = false;bool downloads_slow_network = false; // 8 download pieces at a time instead of 4 (Settings › Storage)- std::string download_server; // empty: Default (Steam's order); otherwise the chosen provider's stable identifier+ // Empty: Steam default (Steam's order); kFastestDownloadServer: Fastest (raced at each download); otherwise+ // the chosen provider's stable identifier.+ std::string download_server;bool controller_connected = true;ControllerState controller;bool persistence_available = true;@@ -944,7 +952,8 @@ private:bool download_server_test_requested_ = false;bool download_server_fixture_ = false;void refresh_download_servers(const std::string& focus = {});- [[nodiscard]] std::string fastest_download_server() const;+ [[nodiscard]] std::string best_tested_download_server() const;+ [[nodiscard]] std::string download_server_choice_key() const;[[nodiscard]] bool download_server_test_allowed() const;std::string self_update_prompted_; // "available 0.3.0" or "staged 0.3.0": each prompt shows oncestd::string pending_update_prompt_; // a prompt waiting for the open dialog to closediff --git a/src/homebrew/view.cpp b/src/homebrew/view.cppindex 8bbc35e..86711ac 100644--- a/src/homebrew/view.cpp+++ b/src/homebrew/view.cpp@@ -2055,7 +2055,8 @@ class Render::Implui::text(scene_, fonts_.regular,!job->failure.empty() ? job->failure: FormatDownloadRate(job->state == JobState::Downloading ? job->bytes_per_second : std::nullopt,- job->state == JobState::Downloading ? job->eta_seconds : std::nullopt),+ job->state == JobState::Downloading ? job->eta_seconds : std::nullopt,+ job->state == JobState::Downloading ? job->server_name : std::string()),right, top + 99.0F, 21.0F, job->failure.empty() ? kSecondary : kRed, gfx::Align::right);}}diff --git a/src/installs/install_service.cpp b/src/installs/install_service.cppindex 20ce8ee..91ac57b 100644--- a/src/installs/install_service.cpp+++ b/src/installs/install_service.cpp@@ -513,7 +513,7 @@ void InstallService::Pause(GameId id) {if (!job) return;if (active_ == id) active_cancel_.store(true);job->view.state = InstallJobState::kPaused;- job->rate.Reset(); job->view.bytes_per_second.reset(); job->view.eta_seconds.reset();+ job->rate.Reset(); job->view.bytes_per_second.reset(); job->view.eta_seconds.reset(); job->view.server_name.clear();job->view.failure.clear();job->waiting = false;Publish();@@ -639,6 +639,7 @@ void InstallService::QueueRemoval(Removal removal) {if (auto* running = Find(*active_); running && running->view.state == InstallJobState::kRunning && !running->cancel_requested) {running->view.state = InstallJobState::kQueued;running->rate.Reset(); running->view.bytes_per_second.reset(); running->view.eta_seconds.reset();+ running->view.server_name.clear();active_cancel_.store(true);}}@@ -745,7 +746,7 @@ void InstallService::Run() {// Trying again: what it said while it waited no longer applies.if (job.waiting) { job.waiting = false; job.view.failure.clear(); }job.rate.Reset();- job.view.bytes_per_second.reset(); job.view.eta_seconds.reset();+ job.view.bytes_per_second.reset(); job.view.eta_seconds.reset(); job.view.server_name.clear();job.view.step = InstallStep::kStarting;job.view.step_since = std::chrono::steady_clock::now();next = job.view.id;@@ -804,7 +805,9 @@ void InstallService::Run() {}const auto result = runner->Install(*next, next_root, active_cancel_,- [this, id = *next](std::uint64_t done, std::uint64_t total, std::uint64_t held) {+ [this, runner, id = *next](std::uint64_t done, std::uint64_t total, std::uint64_t held) {+ // On this worker, inside runner->Install.+ auto server = runner->DownloadServerName();std::lock_guard<std::mutex> lock(mutex_);if (auto* job = Find(id)) {// Under way: a later wait starts short again.@@ -813,6 +816,7 @@ void InstallService::Run() {job->view.done_bytes = done;job->view.total_bytes = total;job->view.held_bytes = held;+ if (job->view.state == InstallJobState::kRunning) job->view.server_name = std::move(server);++revision_; // progress only; the registry is unchanged}},@@ -854,7 +858,7 @@ void InstallService::Run() {job->waiting = true;job->retry_delay = job->retry_delay.count() == 0 ? first_retry_ : std::min(job->retry_delay * 2, most_retry_);job->not_before = std::chrono::steady_clock::now() + job->retry_delay;- job->rate.Reset(); job->view.bytes_per_second.reset(); job->view.eta_seconds.reset();+ job->rate.Reset(); job->view.bytes_per_second.reset(); job->view.eta_seconds.reset(); job->view.server_name.clear();} else if (job && job->view.state == InstallJobState::kRunning) {// A Pause or Resume that arrived while the run was stopping already chose the state.ClearWait(*job);diff --git a/src/installs/install_service.h b/src/installs/install_service.hindex fca3e5e..4c79af1 100644--- a/src/installs/install_service.h+++ b/src/installs/install_service.h@@ -69,6 +69,7 @@ struct InstallJobView {std::string volume_id;std::optional<std::uint64_t> bytes_per_second;std::optional<int> eta_seconds;+ std::string server_name; // while running: where the download comes from, a display name (never a host)InstallStep step = InstallStep::kStarting; // while runningstd::chrono::steady_clock::time_point step_since{};};@@ -136,6 +137,9 @@ class InstallRunner {const std::function<void(std::uint64_t done, std::uint64_t total, std::uint64_t held)>& progress,const std::function<void(InstallStep)>& step) = 0;virtual std::string Remove(const std::string& root) = 0;+ // While Install runs, called on its thread from `progress`: the display name of the server the+ // download comes from, or empty.+ virtual std::string DownloadServerName() const { return {}; }// An uninstalled game whose folder is missing: forgotten with nothing to delete (on the worker).virtual void Forget(const GameId& id) { (void)id; }};diff --git a/src/steam/backend/depot_installer.cpp b/src/steam/backend/depot_installer.cppindex d350861..661dc81 100644--- a/src/steam/backend/depot_installer.cpp+++ b/src/steam/backend/depot_installer.cpp@@ -45,6 +45,28 @@ std::uint64_t Milliseconds(Clock::duration elapsed) {return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());}+// Fastest: one chunk request, as a worker saw it.+struct ServerSample {+ std::size_t server = 0;+ std::uint64_t bytes = 0, transfer_ms = 0; // manifest compressed size; total minus time to first byte+ bool timed = false, new_connection = false, failed = false;+};++bool Scored(const ServerSample& sample) {+ return sample.timed && !sample.failed && !sample.new_connection && sample.bytes >= DepotInstaller::kRaceChunkBytes;+}++double SampleRate(const ServerSample& sample) { // MB/s+ return static_cast<double>(sample.bytes) / static_cast<double>(std::max<std::uint64_t>(sample.transfer_ms, 1)) / 1000.0;+}++double Median(std::vector<double> values) {+ if (values.empty()) return 0;+ std::sort(values.begin(), values.end());+ const auto middle = values.size() / 2;+ return values.size() % 2 ? values[middle] : (values[middle - 1] + values[middle]) / 2;+}+std::string Hex(const std::array<std::uint8_t, 20>& bytes) {static constexpr char kDigits[] = "0123456789abcdef";std::string text;@@ -211,6 +233,11 @@ DownloadTuning ReadDownloadTuning(const std::string& path) {const auto equals = text.find('=');if (equals == std::string::npos) return {};const auto key = text.substr(0, equals);+ if (key == "server" && !seen_server && text.substr(equals + 1) == kFastestDownloadServer) {+ seen_server = true;+ tuning.fastest = true;+ continue;+ }if (key == "server" && !seen_server && ValidCdnHost(text.substr(equals + 1))) {seen_server = true;tuning.preferred_server = text.substr(equals + 1);@@ -383,9 +410,11 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTareturn outcome;}- const auto order = PreferServer(&servers, tuning_.preferred_server);+ const bool fastest = tuning_.fastest;+ const auto order = PreferServer(&servers, fastest ? std::string() : tuning_.preferred_server);if (hooks_.server_list)- hooks_.server_list(servers, tuning_.preferred_server.empty() ? "auto" : order.preferred_found ? "chosen" : "missing");+ hooks_.server_list(servers, fastest ? "fastest" : tuning_.preferred_server.empty() ? "auto"+ : order.preferred_found ? "chosen" : "missing");std::vector<InstallServerStats> coordinator_servers;for (std::size_t i = 0; i < std::min<std::size_t>(servers.size(), 8); ++i)coordinator_servers.push_back({CdnServerLabel(servers[i].host), i});@@ -662,11 +691,91 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTa++files.back().pending;}}+ // Fastest: before the workers start, a short race on this download's largest chunks picks `primary`,+ // or the winner remembered from an earlier download does. Every worker starts each chunk at `primary`+ // and fails over from there. During the download the median rate of large requests to `primary` is+ // judged every kCheckWindowSeconds; two slow windows in a row start a shorter race (a re-race).+ const std::size_t server_count = servers.size();+ const bool checking = fastest && server_count > 1;+ const auto now = [this] { return hooks_.now ? hooks_.now() : Clock::now(); };+ const auto server_tag = [&servers](std::size_t index) {+ return CdnServerLabel(servers[index].host) + "#" + std::to_string(index);+ };+ enum class Probe { kNone, kRace, kRerace };+ struct ProbeState {+ Probe kind = Probe::kNone;+ std::uint64_t generation = 0;+ std::size_t race_end = 0; // a race uses chunks [0, race_end); a re-race the next large chunks+ std::size_t in_flight = 0;+ bool started = false;+ Clock::time_point start{};+ std::vector<ServerSample> samples;+ };+ struct ProbeReport {+ const char* step = "race";+ const char* result = "";+ std::string detail;+ std::optional<std::string> remember; // for remember_fastest; empty forgets+ };+ // Shared with the workers under `mutex` (declared below).+ std::size_t primary = 0;+ bool primary_chosen = order.preferred_found; // a worker that failed over returns to primary after a while+ ProbeState probe;+ std::vector<ServerSample> window; // scored requests since window_start+ Clock::time_point window_start{};+ // MB/s a check compares against: the first judged window after the start or a race, with every worker on+ // `primary`. Not the race's score: a race spreads the workers over the servers, so each request there gets+ // more of the line than once they all share one server.+ double baseline = 0;+ int sags = 0, reraces = 0;+ bool limit_logged = false;+ std::vector<ProbeReport> reports; // logged by the coordinator, outside the lock+ if (fastest) {+ std::size_t large = 0;+ for (const auto& work : chunks) if (work.chunk->compressed_size >= kRaceChunkBytes) ++large;+ const char* skip = server_count == 1 ? "one-server"+ : stats.bytes_pending < kRaceMinPendingBytes ? "skipped-small"+ : large < 2 * server_count ? "skipped-no-large-chunks" : nullptr;+ std::optional<std::size_t> cached;+ if (hooks_.cached_fastest) {+ if (const auto host = hooks_.cached_fastest(); host && !host->empty()) {+ for (std::size_t i = 0; i < server_count && !cached; ++i)+ if (Lower(servers[i].host) == Lower(*host)) cached = i;+ if (!cached && hooks_.remember_fastest) hooks_.remember_fastest("");+ }+ }+ if (cached) { primary = *cached; primary_chosen = true; }+ if (skip || cached) {+ if (hooks_.race) hooks_.race("race", skip ? skip : "cached", "winner=" + (cached ? server_tag(*cached) : "none"));+ } else {+ // The largest chunks move to the front in their own order; the others keep theirs. Two chunks per+ // server visit with the first one unscored give chunks / 2 samples, so 8 per server.+ const auto race_chunks = std::min<std::size_t>(server_count * 8, 64);+ std::vector<std::size_t> candidates;+ for (std::size_t i = 0; i < chunks.size(); ++i)+ if (chunks[i].chunk->compressed_size >= kRaceChunkBytes) candidates.push_back(i);+ std::stable_sort(candidates.begin(), candidates.end(), [&](std::size_t left, std::size_t right) {+ return chunks[left].chunk->compressed_size > chunks[right].chunk->compressed_size;+ });+ candidates.resize(std::min(candidates.size(), race_chunks));+ std::vector<bool> raced(chunks.size());+ for (const auto i : candidates) raced[i] = true;+ std::vector<ChunkWork> ordered;+ ordered.reserve(chunks.size());+ for (std::size_t i = 0; i < chunks.size(); ++i) if (raced[i]) ordered.push_back(std::move(chunks[i]));+ for (std::size_t i = 0; i < chunks.size(); ++i) if (!raced[i]) ordered.push_back(std::move(chunks[i]));+ chunks.swap(ordered);+ probe.kind = Probe::kRace;+ probe.race_end = candidates.size();+ }+ }+ window_start = now();struct Worker {InstallerHooks::ChunkFetcher fetcher;pthread_t thread{};bool started = false, busy = false, ready = false;std::size_t work = 0;+ std::size_t served = 0; // the server the ready chunk came fromstd::vector<std::uint8_t> plain;std::uint64_t compressed_bytes = 0, retries = 0, corrupt_retries = 0;Clock::duration fetch{}, process{}, backoff{};@@ -694,6 +803,89 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTastd::lock_guard<std::mutex> lock(mutex);fail_locked(error, std::move(detail));};+ // Ends the race or re-race once every server has its samples (or keeps failing), its chunks are done+ // or its time is up. The winner has the best median rate; a server with more than half of its+ // requests failed gets none.+ const auto end_probe_locked = [&](Clock::time_point at) {+ if (probe.kind == Probe::kNone) return;+ const bool race = probe.kind == Probe::kRace;+ struct Score { std::size_t requests = 0, failures = 0; std::vector<double> rates; };+ std::vector<Score> scores(server_count);+ for (const auto& sample : probe.samples) {+ auto& score = scores[sample.server];+ ++score.requests;+ if (sample.failed) ++score.failures;+ else if (Scored(sample)) score.rates.push_back(SampleRate(sample));+ }+ const std::size_t need = race ? kRaceSamples : kReraceSamples;+ const bool settled = std::all_of(scores.begin(), scores.end(), [&](const Score& score) {+ return score.rates.size() >= need || score.failures >= need;+ });+ const bool drained = probe.in_flight == 0 && (next_chunk == chunks.size() || (race && next_chunk >= probe.race_end));+ const bool capped = probe.started &&+ at - probe.start >= std::chrono::seconds(race ? kRaceCapSeconds : kReraceCapSeconds);+ if (!settled && !drained && !capped) return;+ std::optional<std::size_t> winner;+ double best = 0;+ std::size_t scored = 0;+ std::string rates;+ for (std::size_t i = 0; i < server_count; ++i) {+ const auto& score = scores[i];+ scored += score.rates.size();+ const bool failing = score.failures * 2 > score.requests;+ const auto rate = failing ? 0.0 : Median(score.rates);+ if (!failing && !score.rates.empty() && (!winner || rate > best)) { winner = i; best = rate; }+ if (i >= 8 || score.requests == 0 || (!failing && score.rates.empty())) continue;+ char text[24] = "failed";+ if (!failing) std::snprintf(text, sizeof(text), "%.1f", rate);+ rates += (rates.empty() ? "" : ",") + server_tag(i) + ":" + text;+ }+ ProbeReport report;+ report.step = race ? "race" : "rerace";+ report.result = winner ? "won" : "timeout";+ report.detail = "winner=" + (winner ? server_tag(*winner) : "none") + " rates=" + (rates.empty() ? "none" : rates) ++ " samples=" + std::to_string(scored) ++ " race_ms=" + std::to_string(probe.started ? Milliseconds(at - probe.start) : 0);+ if (winner) {+ primary = *winner;+ primary_chosen = true;+ report.remember = servers[*winner].host;+ } else if (!race) {+ report.remember = std::string(); // the remembered winner no longer holds+ }+ reports.push_back(std::move(report));+ probe.kind = Probe::kNone;+ probe.started = false;+ probe.in_flight = 0;+ probe.samples.clear();+ ++probe.generation;+ window.clear();+ window_start = at;+ sags = 0;+ baseline = 0;+ };+ const auto check_locked = [&](Clock::time_point at) {+ if (!checking || probe.kind != Probe::kNone || at - window_start < std::chrono::seconds(kCheckWindowSeconds)) return;+ std::vector<double> rates;+ for (const auto& sample : window) if (sample.server == primary) rates.push_back(SampleRate(sample));+ window.clear();+ window_start = at;+ if (rates.size() < kCheckMinSamples) return; // tiny files, or a writer that holds the workers+ const auto rate = Median(rates);+ if (baseline <= 0) { baseline = rate; return; }+ if (rate >= baseline / 2) { sags = 0; return; }+ if (++sags < 2) return;+ sags = 0;+ if (reraces >= kReraceLimit) {+ if (!limit_logged) reports.push_back({"rerace", "limit", "winner=" + server_tag(primary), std::nullopt});+ limit_logged = true;+ return;+ }+ ++reraces;+ probe.kind = Probe::kRerace;+ probe.started = true;+ probe.start = at;+ };// Workers publish their own counters under this lock. Never query their clients here.const auto snapshot = [&] {std::lock_guard<std::mutex> lock(mutex);@@ -746,10 +938,13 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTa#endifif (!worker->fetcher.fetch) { publish_failure(InstallError::kNetwork, "cannot create fetcher"); break; }auto* self = worker.get();- self->run = [&, self] {+ self->run = [&, self, slot = static_cast<std::size_t>(index)] {// Destroy the HTTP client on the same thread that used it, after publishing its counters.ScopeExit release_fetcher{[self] { self->fetcher = {}; }};- std::size_t server_cursor = 0;+ std::size_t server_cursor = 0, last_base = 0;+ std::uint64_t probe_generation = 0;+ std::size_t probe_taken = 0;+ std::vector<ServerSample> attempts;std::uint64_t chunks_since_failover = 0;unsigned failed_returns = 0;bool returning = false;@@ -763,6 +958,9 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTatry {#endifwhile (!stop.load()) {+ std::size_t base = 0;+ bool probe_chunk = false, returns = false;+ std::uint64_t chunk_generation = 0;{std::unique_lock<std::mutex> lock(mutex);changed.wait(lock, [&] { return stop.load() || !self->busy; });@@ -771,16 +969,39 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTaself->busy = true;++occupied;peak_active = std::max(peak_active, static_cast<std::uint32_t>(occupied));+ base = primary;+ returns = primary_chosen;+ // A race goes round the servers, two chunks in a row on each: the first may open the connection.+ if (probe.kind == Probe::kRace ? self->work < probe.race_end+ : probe.kind == Probe::kRerace && chunks[self->work].chunk->compressed_size >= kRaceChunkBytes) {+ if (probe_generation != probe.generation) { probe_generation = probe.generation; probe_taken = 0; }+ base = (slot + probe_taken++ / 2) % server_count;+ probe_chunk = true;+ chunk_generation = probe.generation;+ ++probe.in_flight;+ if (!probe.started) { probe.started = true; probe.start = now(); }+ }}- if (order.preferred_found && server_cursor % servers.size() != 0 && failed_returns < 4 &&+ if (!probe_chunk && base != last_base) {+ // A new primary: start there, as a new download would.+ last_base = base;+ server_cursor = 0;+ chunks_since_failover = 0;+ failed_returns = 0;+ returning = false;+ }+ // A race chunk fails over from its own server and leaves the worker's failover state alone.+ std::size_t probe_cursor = 0;+ auto& cursor = probe_chunk ? probe_cursor : server_cursor;+ if (!probe_chunk && returns && server_cursor % server_count != 0 && failed_returns < 4 &&chunks_since_failover >= (32u << failed_returns)) {server_cursor = 0;returning = true;}const auto failed_over = [&] {- if (returning && server_cursor % servers.size() == 0) { ++failed_returns; returning = false; }- ++server_cursor;- chunks_since_failover = 0;+ if (!probe_chunk && returning && server_cursor % server_count == 0) { ++failed_returns; returning = false; }+ ++cursor;+ if (!probe_chunk) chunks_since_failover = 0;};const auto& work = chunks[self->work];const auto& placement = files[work.file].placement;@@ -790,28 +1011,41 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTastd::string detail;std::vector<std::uint8_t> plain;std::uint64_t compressed_bytes = 0;- const auto attempts = static_cast<std::size_t>(kChunkAttempts) * std::min<std::size_t>(servers.size(), 2);- for (std::size_t attempt = 0; attempt < attempts && !stop.load(); ++attempt) {+ const auto attempts_allowed = static_cast<std::size_t>(kChunkAttempts) * std::min<std::size_t>(server_count, 2);+ attempts.clear();+ for (std::size_t attempt = 0; attempt < attempts_allowed && !stop.load(); ++attempt) {if (attempt > 0) ++retries;std::vector<std::uint8_t> encrypted;- const auto bucket = std::min<std::size_t>(server_cursor % servers.size(), 8);+ const auto server_index = (base + cursor) % server_count;+ const auto bucket = std::min<std::size_t>(server_index, 8);const auto before = self->fetcher.stats ? self->fetcher.stats() : CdnStats{};const auto fetched = Timed(fetch_time, [&] {return Timed(server_fetch[bucket], [&] {- return self->fetcher.fetch(servers[server_cursor % servers.size()], grant.depot_id, Hex(chunk.sha),+ return self->fetcher.fetch(servers[server_index], grant.depot_id, Hex(chunk.sha),static_cast<std::size_t>(chunk.compressed_size) + 4096, stop, &encrypted);});});+ const auto after = self->fetcher.stats ? self->fetcher.stats() : CdnStats{};auto& server = server_stats[bucket];++server.requests;if (fetched != CdnError::kNone) ++server.failures;- server.bytes += self->fetcher.stats ? self->fetcher.stats().bytes - before.bytes : encrypted.size();+ server.bytes += self->fetcher.stats ? after.bytes - before.bytes : encrypted.size();server.fetch_ms = Milliseconds(server_fetch[bucket]);if (fetched == CdnError::kCanceled || stop.load()) { result = InstallError::kCanceled; break; }+ // Transfer time only: connecting and waiting for the first byte are not the server's bandwidth.+ ServerSample sample;+ sample.server = server_index;+ sample.bytes = chunk.compressed_size;+ sample.timed = static_cast<bool>(self->fetcher.stats);+ sample.new_connection = after.new_connections > before.new_connections;+ const auto total_ms = after.total_ms - before.total_ms;+ sample.transfer_ms = total_ms - std::min(total_ms, after.first_byte_ms - before.first_byte_ms);+ sample.failed = fetched != CdnError::kNone;+ attempts.push_back(sample);if (fetched != CdnError::kNone) {failed_over();result = InstallError::kNetwork;- if (hooks_.pause && attempt + 1 < attempts)+ if (hooks_.pause && attempt + 1 < attempts_allowed)Timed(backoff_time, [&] { hooks_.pause(1000 << std::min<std::size_t>(attempt, 3), stop); });continue;}@@ -819,14 +1053,15 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTareturn ProcessDepotChunk(encrypted, grant.key, chunk.original_size, chunk.crc, chunk.sha, &plain);});if (processed == ChunkError::kNone) {- if (server_cursor % servers.size() == 0) {+ if (!probe_chunk && server_cursor % server_count == 0) {returning = false;- } else ++chunks_since_failover;+ } else if (!probe_chunk) ++chunks_since_failover;compressed_bytes = encrypted.size(); result = InstallError::kNone; break;}++server.failures; // received bytes that failed verification are unusable too+ attempts.back().failed = true;failed_over();- if (attempt + 1 < attempts) ++corrupt_retries;+ if (attempt + 1 < attempts_allowed) ++corrupt_retries;result = InstallError::kCorrupt;}if (stop.load()) result = InstallError::kCanceled;@@ -843,8 +1078,16 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTaself->fetch = fetch_time; self->process = process_time; self->backoff = backoff_time;self->retries = retries; self->corrupt_retries = corrupt_retries; self->cdn = cdn;self->server_stats = server_stats;+ if (probe_chunk && probe.kind != Probe::kNone && chunk_generation == probe.generation) {+ --probe.in_flight;+ probe.samples.insert(probe.samples.end(), attempts.begin(), attempts.end());+ } else if (checking && probe.kind == Probe::kNone) {+ for (const auto& sample : attempts) if (Scored(sample)) window.push_back(sample);+ }if (result != InstallError::kNone) { fail_locked(result, std::move(detail)); break; }+ end_probe_locked(now());self->plain = std::move(plain); self->compressed_bytes = compressed_bytes;+ self->served = attempts.empty() ? base : attempts.back().server;self->ready = true;++waiting;peak_waiting = std::max(peak_waiting, static_cast<std::uint32_t>(waiting));@@ -884,6 +1127,37 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTawork.prepared = true;return true;};+ // Ends a race at its deadline, judges the check window, logs what ended and names the server for+ // the Queue: the server the last written chunk came from, `primary` before one, none while the race runs.+ // A provider without a display name is not shown by its host.+ std::optional<std::size_t> named, last_served;+ bool racing = false;+ const auto show = [&](std::optional<std::size_t> index) {+ if (index == named) return;+ named = index;+ progress.server_name.clear();+ if (!index) return;+ progress.server_name = CdnServerDisplayName(servers[*index].host);+ if (Lower(progress.server_name) == Lower(servers[*index].host)) progress.server_name = "Network cache";+ };+ const auto tick = [&] {+ std::vector<ProbeReport> ended;+ std::size_t current = 0;+ {+ std::lock_guard<std::mutex> lock(mutex);+ const auto at = now();+ end_probe_locked(at);+ check_locked(at);+ ended.swap(reports);+ racing = probe.kind == Probe::kRace;+ current = primary;+ }+ for (const auto& report : ended) {+ if (hooks_.race) hooks_.race(report.step, report.result, report.detail);+ if (report.remember && hooks_.remember_fastest) hooks_.remember_fastest(*report.remember);+ }+ show(racing ? std::nullopt : last_served ? last_served : std::optional<std::size_t>(current));+ };std::size_t prepare_next = 0, front_file = 0, written_chunks = 0;while (true) {if (cancel.load()) publish_failure(InstallError::kCanceled, "canceled");@@ -896,10 +1170,13 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTa}}snapshot();+ tick();report_progress();if (ready) {const auto& work = chunks[ready->work];const auto& file = *files[work.file].placement.file;+ last_served = ready->served;+ if (!racing) show(last_served);if (!prepare_file(work.file)) { publish_failure(InstallError::kStorage, "cannot create file"); break; }if (stats.chunks_done == 0) trace("first-write");const auto path = target.install_root + "/" + file.path;@@ -939,6 +1216,7 @@ InstallOutcome DepotInstaller::Run(const StoredSession& session, const InstallTachanged.wait_for(lock, std::chrono::milliseconds(50), [&] { return stop.load() || waiting != 0; });}join_workers();+ tick(); // a race that ended with the last chunkif (first_error != InstallError::kNone) {outcome.error = first_error; outcome.detail = first_detail; outcome.progress = progress;return outcome;diff --git a/src/steam/backend/depot_installer.h b/src/steam/backend/depot_installer.hindex 3c46d15..a551bba 100644--- a/src/steam/backend/depot_installer.h+++ b/src/steam/backend/depot_installer.h@@ -8,8 +8,10 @@#include "steam/session_store.h"#include <atomic>+#include <chrono>#include <cstdint>#include <functional>+#include <optional>#include <string>#include <vector>@@ -46,15 +48,23 @@ struct InstallProgress {// Bytes of install_total this install already occupies on disk: preallocated files// (exFAT allocates on ftruncate) or written chunks (sparse filesystems).std::uint64_t held_bytes = 0;+ // Display name of the server chunks come from (CdnServerDisplayName, never a host); empty while+ // the Fastest race runs.+ std::string server_name;};struct DownloadTuning {int slots = 4;int ahead_files = 256;- std::string preferred_server; // empty uses Automatic order+ std::string preferred_server; // empty uses Steam's order+ bool fastest = false; // race the servers at download start and keep checking (preferred_server is unused)};-// A missing or malformed file uses defaults; valid slot counts are clamped to 1..8.+// The download server choice that races the servers (DownloadTuning::fastest), as preferences store it.+inline constexpr const char* kFastestDownloadServer = "fastest";++// A missing or malformed file uses defaults; valid slot counts are clamped to 1..8. "server=fastest"+// sets `fastest`.DownloadTuning ReadDownloadTuning(const std::string& path);// Manifest and chunk transfers, including failed and corrupt replies.@@ -115,6 +125,15 @@ struct InstallerHooks {// Optional: waits between attempts at a chunk the CDN refused (a busy server answers 503 for a// while); returns early when canceled. Without it attempts follow each other at once.std::function<void(int milliseconds, const std::atomic<bool>&)> pause;+ // Optional, for Fastest: the winner remembered from an earlier download (a host) and where a new+ // winner goes; an empty host forgets it.+ std::function<std::optional<std::string>()> cached_fastest;+ std::function<void(const std::string& host)> remember_fastest;+ // Optional: how a Fastest race ("race") or check ("rerace") ended, with server labels, list+ // indexes and rates; never hosts.+ std::function<void(const char* step, const char* result, const std::string& detail)> race;+ // Optional: the clock for race deadlines and check windows; default steady_clock.+ std::function<std::chrono::steady_clock::time_point()> now;};// Downloads and assembles an app's depots into install_root. Safe to run again after any@@ -134,6 +153,16 @@ class DepotInstaller {static constexpr const char* kFileList = ".playden-files";static constexpr int kChunkAttempts = 3;static constexpr std::size_t kMergeLogLimit = 16;+ // Fastest. Only requests of large chunks over a reused connection are scored, by transfer time.+ static constexpr std::uint64_t kRaceChunkBytes = 256 * 1024;+ static constexpr std::uint64_t kRaceMinPendingBytes = 200ull * 1000 * 1000;+ static constexpr std::size_t kRaceSamples = 4; // scored requests per server that end a race+ static constexpr std::size_t kReraceSamples = 3;+ static constexpr int kRaceCapSeconds = 15;+ static constexpr int kReraceCapSeconds = 5;+ static constexpr int kCheckWindowSeconds = 30;+ static constexpr std::size_t kCheckMinSamples = 8; // a window with fewer large requests is not judged+ static constexpr int kReraceLimit = 2;private:InstallerHooks hooks_;@@ -144,6 +173,11 @@ class DepotInstaller {}void SetTrace(std::function<void(const char*)> trace) { hooks_.trace = std::move(trace); }void SetMerge(std::function<void(const char*, const std::string&)> merge) { hooks_.merge = std::move(merge); }+ void SetRace(std::function<void(const char*, const char*, const std::string&)> race) { hooks_.race = std::move(race); }+ void SetFastestCache(std::function<std::optional<std::string>()> cached, std::function<void(const std::string&)> remember) {+ hooks_.cached_fastest = std::move(cached);+ hooks_.remember_fastest = std::move(remember);+ }void SetTuning(DownloadTuning tuning) { tuning_ = tuning; }private:DownloadTuning tuning_;diff --git a/src/steam/backend/steam_install_runner.cpp b/src/steam/backend/steam_install_runner.cppindex 24bb3d0..d385c7b 100644--- a/src/steam/backend/steam_install_runner.cpp+++ b/src/steam/backend/steam_install_runner.cpp@@ -20,7 +20,7 @@ std::string FormatInstallServerList(const std::vector<ContentServer>& servers, cconst std::string pref = preference ? preference : "auto";std::string detail = "n=" + std::to_string(servers.size()) + " https=" +std::to_string(std::count_if(servers.begin(), servers.end(), [](const ContentServer& server) { return server.https; })) +- " pref=" + (pref == "chosen" || pref == "missing" ? pref : "auto") + " first=" ++ " pref=" + (pref == "chosen" || pref == "missing" || pref == "fastest" ? pref : "auto") + " first=" +(servers.empty() ? "none" : CdnServerLabel(servers.front().host) + "@0") + " order=";for (std::size_t i = 0; i < std::min<std::size_t>(servers.size(), 8); ++i) {if (i) detail += ',';@@ -79,6 +79,28 @@ std::string FormatInstallStats(const char* phase, const InstallStats& stats) {return detail;}+std::optional<std::string> FastestServerCache::Get(std::uint64_t account) {+ std::lock_guard<std::mutex> lock(mutex_);+ if (host_.empty()) return std::nullopt;+ if (account != account_ || Time() - at_ >= std::chrono::minutes(kMinutes)) {+ host_.clear();+ return std::nullopt;+ }+ return host_;+}++void FastestServerCache::Remember(std::uint64_t account, const std::string& host) {+ std::lock_guard<std::mutex> lock(mutex_);+ account_ = account;+ host_ = host;+ at_ = Time();+}++void FastestServerCache::Clear() {+ std::lock_guard<std::mutex> lock(mutex_);+ host_.clear();+}+std::string SteamInstallRunner::Remove(const std::string& root) {std::string failure;const bool ok = RemoveManagedInstall(root, &failure);@@ -136,7 +158,11 @@ InstallRunner::Result SteamInstallRunner::InstallApp(const StoredSession& sessioconst std::function<void(InstallStep)>& step) {Result result;current_app_.store(id);+ server_name_.clear();installer_.SetTuning(tuning_ ? tuning_() : DownloadTuning{});+ const auto account = session.steam_id;+ installer_.SetFastestCache([this, account] { return fastest_.Get(account); },+ [this, account](const std::string& host) { fastest_.Remember(account, host); });auto log = [this, id](const char* step, const char* category, const char* detail) {if (log_) log_(id, step, category, detail);};@@ -242,6 +268,7 @@ InstallRunner::Result SteamInstallRunner::InstallApp(const StoredSession& sessio}if (step) step(InstallStep::kDownloading);const auto outcome = installer_.Run(session, target, cancel, [&](const InstallProgress& state) {+ server_name_ = state.server_name;if (progress) progress(state.download_done, state.download_total, state.held_bytes);});if (!outcome.skipped_depots.empty()) {diff --git a/src/steam/backend/steam_install_runner.h b/src/steam/backend/steam_install_runner.hindex 4acce2a..7c78082 100644--- a/src/steam/backend/steam_install_runner.h+++ b/src/steam/backend/steam_install_runner.h@@ -5,8 +5,32 @@#include "steam/backend/depot_installer.h"#include "steam/install_service.h"+#include <chrono>+#include <mutex>+#include <optional>+namespace playden::steam {+// The Fastest race's winner (a host), kept in memory so a queue of games races once. It belongs to one+// Steam account and lasts kMinutes; another account, an empty host or Clear forgets it.+class FastestServerCache {+ public:+ static constexpr int kMinutes = 45;+ using Now = std::function<std::chrono::steady_clock::time_point()>;+ explicit FastestServerCache(Now now = {}) : now_(std::move(now)) {}+ std::optional<std::string> Get(std::uint64_t account);+ void Remember(std::uint64_t account, const std::string& host);+ void Clear();++ private:+ std::chrono::steady_clock::time_point Time() const { return now_ ? now_() : std::chrono::steady_clock::now(); }+ Now now_;+ std::mutex mutex_;+ std::uint64_t account_ = 0;+ std::string host_;+ std::chrono::steady_clock::time_point at_{};+};+std::string FormatInstallStats(const char* phase, const InstallStats& stats);std::string FormatInstallServerList(const std::vector<ContentServer>& servers, const char* preference);std::vector<std::string> FormatInstallServerStats(const std::vector<InstallServerStats>& servers);@@ -17,13 +41,15 @@ void LogInstallTrace(const InstallLogFn& log, std::uint32_t app, const char* mar// branch), then the resumable depot installer.class SteamInstallRunner final : public SessionInstallRunner {public:- SteamInstallRunner(SessionRepository& sessions, CmClient& cm, CdnClient& cdn)- : SessionInstallRunner(sessions), cm_(cm), installer_(cm, cdn) {}+ SteamInstallRunner(SessionRepository& sessions, CmClient& cm, CdnClient& cdn, FastestServerCache::Now now = {})+ : SessionInstallRunner(sessions), cm_(cm), installer_(cm, cdn), fastest_(std::move(now)) {}Result InstallApp(const StoredSession& session, std::uint32_t id, const std::string& root,const std::atomic<bool>& cancel,const std::function<void(std::uint64_t, std::uint64_t, std::uint64_t)>& progress,const std::function<void(InstallStep)>& step) override;std::string Remove(const std::string& root) override;+ std::string DownloadServerName() const override { return server_name_; }+ FastestServerCache& fastest_server() { return fastest_; }void SetTuning(std::function<DownloadTuning()> tuning) { tuning_ = std::move(tuning); }// Optional sanitized log: the Steam app ID, step, error category and a short detail; never// tokens or account identifiers. Called on the install worker.@@ -51,11 +77,19 @@ class SteamInstallRunner final : public SessionInstallRunner {installer_.SetMerge([this, log](const char* result, const std::string& detail) {if (log) log(current_app_.load(), "merge", result, detail.c_str());});+ installer_.SetRace([this, log](const char* step, const char* result, const std::string& detail) {+ if (log) log(current_app_.load(), step, result, detail.c_str());+ });}+ protected:+ void SignedOut() override { fastest_.Clear(); }+private:CmClient& cm_;DepotInstaller installer_;+ FastestServerCache fastest_;+ std::string server_name_; // the install worker's own: set and read during InstallApp's progressLogFn log_;std::function<DownloadTuning()> tuning_;std::atomic<std::uint32_t> current_app_{0};diff --git a/src/steam/install_service.h b/src/steam/install_service.hindex 81c9b71..004d845 100644--- a/src/steam/install_service.h+++ b/src/steam/install_service.h@@ -45,6 +45,7 @@ class SessionInstallRunner : public InstallRunner {}const auto session = sessions_.Load();if (!session.ok() || !session.value()) {+ SignedOut();result.retry_later = true;result.waiting = kWaitingForSignIn;return result;@@ -55,6 +56,8 @@ class SessionInstallRunner : public InstallRunner {}protected:+ // No saved session when a job starts: what the runner keeps for an account can go.+ virtual void SignedOut() {}virtual Result InstallApp(const StoredSession& session, std::uint32_t app, const std::string& root,const std::atomic<bool>& cancel,const std::function<void(std::uint64_t, std::uint64_t, std::uint64_t)>& progress,diff --git a/tests/depot_installer_test.cpp b/tests/depot_installer_test.cppindex 7db6c4b..7769f56 100644--- a/tests/depot_installer_test.cpp+++ b/tests/depot_installer_test.cpp@@ -6,6 +6,9 @@#include <mbedtls/sha1.h>#include <algorithm>+#include <functional>+#include <optional>+#include <set>#include <atomic>#include <chrono>#include <condition_variable>@@ -1315,6 +1318,571 @@ void RemovesOnlyManagedFolders(const std::filesystem::path& base) {} // namespace+// Fastest: fake HTTP clients on a fake clock. A client's first request to a server opens a connection+// (connect_ms before the first byte); the transfer takes the chunk's declared size at the server's rate.+struct FakeNet {+ struct Spec {+ std::uint64_t connect_ms = 20;+ std::function<double(std::int64_t now_ms, int request)> rate; // MB/s; 0 fails the request+ };+ struct Client {+ std::set<std::string> connected;+ CdnStats stats;+ };+ World* world = nullptr;+ std::mutex mutex;+ std::int64_t now_ms = 0;+ std::vector<std::string> order; // the server list+ std::map<std::string, Spec> specs;+ std::map<std::string, int> requests;+ std::map<std::string, std::uint32_t> declared; // sha -> compressed size the manifest claims+ std::vector<std::string> hosts, shas; // successful requests, in order+ int cancel_after = -1;+ std::atomic<bool>* cancel = nullptr;++ void Add(const std::string& host, double mb_per_s, std::uint64_t connect_ms = 20) {+ order.push_back(host);+ specs[host] = {connect_ms, [mb_per_s](std::int64_t, int) { return mb_per_s; }};+ }+ void Advance(std::int64_t ms) {+ std::lock_guard<std::mutex> lock(mutex);+ now_ms += ms;+ }+ std::chrono::steady_clock::time_point Now() {+ std::lock_guard<std::mutex> lock(mutex);+ return std::chrono::steady_clock::time_point(std::chrono::milliseconds(now_ms));+ }+ // Large files: each chunk is declared at `bytes` compressed bytes; the served containers stay small.+ ManifestFile File(const std::string& path, int count, std::uint32_t bytes) {+ std::vector<std::string> pieces;+ for (int i = 0; i < count; ++i) pieces.push_back(path + "-" + std::to_string(i) + ";");+ auto file = world->AddFile(path, pieces);+ for (auto& chunk : file.chunks) {+ chunk.compressed_size = bytes;+ declared[HexOf(chunk.sha)] = bytes;+ }+ return file;+ }+ InstallerHooks::ChunkFetcher Fetcher() {+ auto client = std::make_shared<Client>();+ InstallerHooks::ChunkFetcher fetcher;+ fetcher.fetch = [this, client](const ContentServer& server, std::uint32_t, const std::string& sha, std::size_t,+ const std::atomic<bool>&, std::vector<std::uint8_t>* out) {+ std::lock_guard<std::mutex> lock(mutex);+ if (cancel_after >= 0 && static_cast<int>(shas.size()) >= cancel_after) {+ cancel->store(true);+ return CdnError::kCanceled;+ }+ const auto& spec = specs.at(server.host);+ const double rate = spec.rate(now_ms, requests[server.host]++);+ const bool fresh = client->connected.insert(server.host).second;+ const std::uint64_t before_body = (fresh ? spec.connect_ms : 0) + 2;+ ++client->stats.requests;+ client->stats.new_connections += fresh ? 1 : 0;+ if (rate <= 0) {+ client->connected.erase(server.host);+ ++client->stats.failures;+ client->stats.first_byte_ms += before_body;+ client->stats.total_ms += before_body;+ now_ms += static_cast<std::int64_t>(before_body);+ return CdnError::kNetwork;+ }+ const auto found = declared.find(sha);+ const std::uint64_t bytes = found == declared.end() ? world->served.at(sha).size() : found->second;+ const auto transfer = static_cast<std::uint64_t>(static_cast<double>(bytes) / (rate * 1000.0));+ client->stats.first_byte_ms += before_body;+ client->stats.total_ms += before_body + transfer;+ client->stats.bytes += bytes;+ now_ms += static_cast<std::int64_t>(before_body + transfer);+ hosts.push_back(server.host);+ shas.push_back(sha);+ *out = world->served.at(sha);+ return CdnError::kNone;+ };+ fetcher.stats = [this, client] {+ std::lock_guard<std::mutex> lock(mutex);+ return client->stats;+ };+ fetcher.last_failure = [] { return std::string(); };+ return fetcher;+ }+ InstallerHooks Hooks() {+ auto hooks = world->Hooks();+ hooks.servers = [this](std::uint32_t, const std::atomic<bool>&, std::vector<ContentServer>* out) {+ for (const auto& host : order) out->push_back({host, host, 0, true});+ return CdnError::kNone;+ };+ hooks.make_fetcher = [this] { return Fetcher(); };+ hooks.now = [this] { return Now(); };+ return hooks;+ }+};++constexpr const char* kFastly = "fastly.cdn.steampipe.steamcontent.com";+constexpr const char* kAkamai = "steampipe.akamaized.net";+constexpr const char* kGoogle = "google2.cdn.steampipe.steamcontent.com";+constexpr std::uint32_t kMegabyte = 1000 * 1000;++struct RaceRun {+ InstallOutcome outcome;+ std::vector<std::string> races; // "<step> <result> <detail>"+ std::vector<std::string> remembered; // remember_fastest calls+ std::string listed;+ std::string server_name; // the last progress report's+ std::optional<std::string> first_written_name; // the report after the first write+};++struct RaceSetup {+ int slots = 4;+ std::optional<std::string> cached;+ std::function<void()> on_progress; // runs on the writer after each report+ bool legacy_chunk_hook = false; // no per-request stats, as InstallerHooks::chunk+};++RaceRun RunFastest(FakeNet& net, const std::filesystem::path& root, RaceSetup setup = {},+ std::atomic<bool>* external_cancel = nullptr) {+ auto hooks = net.Hooks();+ RaceRun run;+ if (setup.legacy_chunk_hook) {+ hooks.make_fetcher = nullptr;+ auto fetcher = std::make_shared<InstallerHooks::ChunkFetcher>(net.Fetcher());+ hooks.chunk = [fetcher](const ContentServer& server, std::uint32_t depot, const std::string& sha, std::size_t cap,+ const std::atomic<bool>& stop, std::vector<std::uint8_t>* out) {+ return fetcher->fetch(server, depot, sha, cap, stop, out);+ };+ }+ hooks.race = [&run](const char* step, const char* result, const std::string& detail) {+ run.races.push_back(std::string(step) + " " + result + " " + detail);+ };+ hooks.cached_fastest = [&setup] { return setup.cached; };+ hooks.remember_fastest = [&run](const std::string& host) { run.remembered.push_back(host); };+ hooks.server_list = [&run](const std::vector<ContentServer>& servers, const char* preference) {+ run.listed = FormatInstallServerList(servers, preference);+ };+ DepotInstaller installer(hooks);+ DownloadTuning tuning{setup.slots, 256};+ tuning.fastest = true;+ installer.SetTuning(tuning);+ std::atomic<bool> own_cancel{false};+ auto& cancel = external_cancel ? *external_cancel : own_cancel;+ net.cancel = &cancel;+ bool written = false;+ run.outcome = installer.Run({}, Target(root), cancel, [&](const InstallProgress& progress) {+ if (!written && progress.download_done > 0) run.first_written_name = progress.server_name;+ if (progress.download_done > 0) written = true;+ run.server_name = progress.server_name;+ if (setup.on_progress) setup.on_progress();+ });+ return run;+}++bool Has(const std::vector<std::string>& lines, const std::string& prefix) {+ return std::any_of(lines.begin(), lines.end(), [&](const std::string& line) { return line.rfind(prefix, 0) == 0; });+}++void FastestWorld(FakeNet& net, World& world) {+ net.world = &world;+ world.manifests[441].files.clear();+ world.manifests[442].files.clear();+}++void RaceWinsAndLaterChunksUseIt(const std::filesystem::path& root) {+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.Add(kAkamai, 40);+ net.Add(kGoogle, 20);+ world.manifests[441].files = {net.File("pak", 300, kMegabyte)};+ const auto run = RunFastest(net, root);+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(run.listed.find("pref=fastest") != std::string::npos);+ CHECK(run.races.size() == 1 && Has(run.races, "race won winner=akamai#1 rates=fastly#0:10.0,akamai#1:40.0,google#2:20.0 "));+ CHECK(std::stoull(run.races[0].substr(run.races[0].find("samples=") + 8)) >= 3 * DepotInstaller::kRaceSamples);+ CHECK(run.races[0].find("steamcontent") == std::string::npos && run.races[0].find("akamaized") == std::string::npos);+ CHECK(run.remembered == std::vector<std::string>{kAkamai});+ // The name is the last written chunk's server; with four workers that can be a raced one (single-worker+ // tests check it exactly).+ CHECK(!run.server_name.empty());+ // Workers that were fetching when the race ended finish there; everything after goes to the winner.+ for (std::size_t i = 60; i < net.hosts.size(); ++i) CHECK(net.hosts[i] == kAkamai);+}++void TinyChunksSkipTheRace(const std::filesystem::path& root) {+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.Add(kAkamai, 40);+ net.Add(kGoogle, 20);+ // 204 MB, but only five chunks of at least 256 KB: fewer than two per server.+ world.manifests[441].files = {net.File("big", 5, kMegabyte), net.File("small", 800, 255 * 1000)};+ const auto run = RunFastest(net, root, {1});+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(run.races == std::vector<std::string>{"race skipped-no-large-chunks winner=none"});+ CHECK(run.remembered.empty() && run.server_name == "Fastly");+ for (const auto& host : net.hosts) CHECK(host == kFastly); // Steam's order+ // No reorder: chunks come in file order.+ const auto& big = world.manifests[441].files[0];+ CHECK(net.shas.front() == HexOf(big.chunks.front().sha));+ // Under 200 MB pending.+ FakeNet small_net;+ auto small_world = MakeWorld();+ FastestWorld(small_net, small_world);+ small_net.Add(kFastly, 10);+ small_net.Add(kAkamai, 40);+ small_world.manifests[441].files = {small_net.File("pak", 150, kMegabyte)};+ const auto small = RunFastest(small_net, root.string() + "-small");+ CHECK(small.outcome.error == InstallError::kNone && small.races == std::vector<std::string>{"race skipped-small winner=none"});+ // One server.+ FakeNet one_net;+ auto one_world = MakeWorld();+ FastestWorld(one_net, one_world);+ one_net.Add(kFastly, 10);+ one_world.manifests[441].files = {one_net.File("pak", 250, kMegabyte)};+ const auto one = RunFastest(one_net, root.string() + "-one");+ CHECK(one.outcome.error == InstallError::kNone && one.races == std::vector<std::string>{"race one-server winner=none"});+}++void RaceFetchesLargeChunksAtTheEnd(const std::filesystem::path& root) {+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.Add(kAkamai, 40);+ std::vector<ManifestFile> files;+ for (int i = 0; i < 40; ++i) files.push_back(net.File("a-tiny-" + std::to_string(i), 1, 2000));+ files.push_back(net.File("z-big", 220, kMegabyte));+ world.manifests[441].files = files;+ const auto run = RunFastest(net, root);+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(Has(run.races, "race won winner=akamai#1 "));+ // The race chunks went first: the first 16 requests were chunks of the large file.+ std::set<std::string> big;+ for (const auto& chunk : files.back().chunks) big.insert(HexOf(chunk.sha));+ for (std::size_t i = 0; i < 16; ++i) CHECK(big.count(net.shas[i]));+ std::string expected;+ for (int i = 0; i < 220; ++i) expected += "z-big-" + std::to_string(i) + ";";+ CHECK(Read(root / "z-big") == expected);+ CHECK(Read(root / "a-tiny-7") == "a-tiny-7-0;");+ CHECK(std::filesystem::exists(root / DepotInstaller::kInstallRecord));+ CHECK(!std::filesystem::exists(root / DepotInstaller::kJournalDir));+}++void SlowWriterAndHandshakesDoNotDecide(const std::filesystem::path& root) {+ {+ // The writer holds the only worker for 3 s after each chunk; scores come from transfer time.+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.Add(kAkamai, 25);+ world.manifests[441].files = {net.File("pak", 220, kMegabyte)};+ RaceSetup setup{1};+ setup.on_progress = [&net] { net.Advance(3000); };+ const auto run = RunFastest(net, root / "writer", setup);+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(Has(run.races, "race won winner=akamai#1 rates=fastly#0:10.0,akamai#1:25.0 "));+ }+ {+ // A 2 s handshake with a fast transfer beats a cheap handshake with a slow transfer.+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10, 5);+ net.Add(kAkamai, 40, 2000);+ world.manifests[441].files = {net.File("pak", 220, kMegabyte)};+ const auto run = RunFastest(net, root / "handshake");+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(Has(run.races, "race won winner=akamai#1 "));+ }+}++void SlowLinkEndsAtTheCap(const std::filesystem::path& root) {+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 0.4);+ net.Add(kAkamai, 0.5);+ world.manifests[441].files = {net.File("pak", 220, kMegabyte)};+ const auto run = RunFastest(net, root, {1});+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(Has(run.races, "race won winner=akamai#1 rates=fastly#0:0.4,akamai#1:0.5 "));+ const auto& line = run.races[0];+ const auto ms = std::stoull(line.substr(line.find("race_ms=") + 8));+ const auto samples = std::stoull(line.substr(line.find("samples=") + 8));+ CHECK(ms >= 15000 && ms < 20000 && samples >= 2 && samples < 2 * DepotInstaller::kRaceSamples);+ CHECK(run.first_written_name == std::string() && run.server_name == "Akamai"); // no name while racing+}++void FailingServerNeverWins(const std::filesystem::path& root) {+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.Add(kAkamai, 0); // refuses every request+ net.Add(kGoogle, 20);+ world.manifests[441].files = {net.File("pak", 220, kMegabyte)};+ const auto run = RunFastest(net, root);+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(Has(run.races, "race won winner=google#2 rates=fastly#0:10.0,akamai#1:failed,google#2:20.0 "));+ CHECK(std::filesystem::exists(root / DepotInstaller::kInstallRecord));+}++void ResumeRacesOnlyWhatIsLeft(const std::filesystem::path& root) {+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.Add(kAkamai, 40);+ world.manifests[441].files = {net.File("pak", 400, kMegabyte)};+ std::atomic<bool> cancel{false};+ net.cancel_after = 150;+ RaceSetup setup{1};+ CHECK(RunFastest(net, root, setup, &cancel).outcome.error == InstallError::kCanceled);+ const std::set<std::string> fetched(net.shas.begin(), net.shas.end());+ net.cancel_after = -1;+ net.shas.clear();+ cancel = false;+ const auto second = RunFastest(net, root, setup, &cancel);+ CHECK(second.outcome.error == InstallError::kNone && Has(second.races, "race won "));+ CHECK(net.shas.size() == 250);+ for (const auto& sha : net.shas) CHECK(!fetched.count(sha));+ // Under 200 MB left: no race.+ auto small_world = MakeWorld();+ FakeNet small_net;+ FastestWorld(small_net, small_world);+ small_net.Add(kFastly, 10);+ small_net.Add(kAkamai, 40);+ small_world.manifests[441].files = {small_net.File("pak", 300, kMegabyte)};+ small_net.cancel_after = 120;+ cancel = false;+ CHECK(RunFastest(small_net, root.string() + "-small", setup, &cancel).outcome.error == InstallError::kCanceled);+ small_net.cancel_after = -1;+ cancel = false;+ const auto rest = RunFastest(small_net, root.string() + "-small", setup, &cancel);+ CHECK(rest.outcome.error == InstallError::kNone);+ CHECK(Has(rest.races, "race skipped-small winner=none"));+}++void CachedWinnerSkipsTheRace(const std::filesystem::path& root) {+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.Add(kAkamai, 40);+ net.Add(kGoogle, 20);+ world.manifests[441].files = {net.File("pak", 220, kMegabyte)};+ RaceSetup setup{1};+ setup.cached = kGoogle;+ const auto run = RunFastest(net, root / "hit", setup);+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(run.races == std::vector<std::string>{"race cached winner=google#2"});+ CHECK(run.remembered.empty() && run.server_name == "Google Cloud" && run.first_written_name == run.server_name);+ for (const auto& host : net.hosts) CHECK(host == kGoogle);+ CHECK(net.shas.front() == HexOf(world.manifests[441].files[0].chunks.front().sha)); // no reorder+ // A remembered winner the list no longer has is forgotten, and the race runs.+ net.hosts.clear();+ setup.cached = "gone.cdn.steampipe.steamcontent.com";+ const auto missing = RunFastest(net, root / "miss", setup);+ CHECK(missing.outcome.error == InstallError::kNone && Has(missing.races, "race won winner=akamai#1 "));+ CHECK(missing.remembered == std::vector<std::string>({"", kAkamai}));+}++void RaceWithoutRequestTimingKeepsSteamOrder(const std::filesystem::path& root) {+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.Add(kAkamai, 40);+ world.manifests[441].files = {net.File("pak", 220, kMegabyte)};+ RaceSetup setup{1};+ setup.legacy_chunk_hook = true;+ const auto run = RunFastest(net, root, setup);+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(Has(run.races, "race timeout winner=none rates=none samples=0 "));+ CHECK(run.remembered.empty() && run.server_name == "Fastly" && net.hosts.back() == kFastly);+}++// A sustained sag starts a re-race on the next large chunks.+void SagReracesToAnotherServer(const std::filesystem::path& root) {+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.specs[kFastly].rate = [](std::int64_t, int request) { return request < 300 ? 10.0 : 1.0; };+ net.Add(kAkamai, 5);+ world.manifests[441].files = {net.File("pak", 700, kMegabyte)};+ const auto run = RunFastest(net, root, {1});+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(run.races.size() == 2 && Has(run.races, "race won winner=fastly#0 "));+ CHECK(Has(run.races, "rerace won winner=akamai#1 rates=fastly#0:1.0,akamai#1:5.0 "));+ CHECK(run.remembered == std::vector<std::string>({kFastly, kAkamai}));+ CHECK(run.server_name == "Akamai" && net.hosts.back() == kAkamai);+ // Started on a remembered winner: the first judged window is what later windows compare against.+ auto cached_world = MakeWorld();+ FakeNet cached_net;+ FastestWorld(cached_net, cached_world);+ cached_net.Add(kFastly, 10);+ cached_net.specs[kFastly].rate = [](std::int64_t, int request) { return request < 600 ? 10.0 : 1.0; };+ cached_net.Add(kAkamai, 5);+ cached_world.manifests[441].files = {cached_net.File("pak", 700, kMegabyte)};+ RaceSetup setup{1};+ setup.cached = kFastly;+ const auto cached = RunFastest(cached_net, root.string() + "-cached", setup);+ CHECK(cached.outcome.error == InstallError::kNone);+ CHECK(cached.races.size() == 2 && cached.races[0] == "race cached winner=fastly#0");+ CHECK(Has(cached.races, "rerace won winner=akamai#1 "));+}++void SteadyRateBelowTheRaceDoesNotRerace(const std::filesystem::path& root) {+ // In the race each request has the line to itself; afterwards every worker shares it on the winner, so+ // each request gets less. That steady rate is the baseline, not the race's score.+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 0);+ net.specs[kFastly].rate = [](std::int64_t, int request) { return request < 20 ? 40.0 : 6.0; };+ net.Add(kAkamai, 10);+ net.specs[kAkamai].rate = [](std::int64_t, int request) { return request < 20 ? 10.0 : 2.0; };+ world.manifests[441].files = {net.File("pak", 1000, kMegabyte)};+ const auto run = RunFastest(net, root, {1});+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(run.races.size() == 1 && Has(run.races, "race won winner=fastly#0 "));+ CHECK(net.now_ms > 120000); // several judged windows+}++void TinyFilesAndSlowWritersDoNotRerace(const std::filesystem::path& root) {+ {+ // After the large file, a long stretch of tiny chunks on a slow server is never judged.+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.specs[kFastly].rate = [](std::int64_t, int request) { return request < 240 ? 10.0 : 0.05; };+ net.Add(kAkamai, 5);+ world.manifests[441].files = {net.File("a-big", 220, kMegabyte), net.File("b-tiny", 400, 100 * 1000)};+ const auto run = RunFastest(net, root / "tiny", {1});+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(run.races.size() == 1 && Has(run.races, "race won winner=fastly#0 "));+ CHECK(net.now_ms > 10 * 60 * 1000); // twenty windows of tiny chunks+ }+ {+ // A writer that takes 2 s per chunk: the transfer rate stays, nothing sags.+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 10);+ net.Add(kAkamai, 5);+ world.manifests[441].files = {net.File("pak", 260, kMegabyte)};+ RaceSetup setup{1};+ setup.on_progress = [&net] { net.Advance(2000); };+ const auto run = RunFastest(net, root / "writer", setup);+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(run.races.size() == 1 && Has(run.races, "race won winner=fastly#0 "));+ }+}++void ReracesStopAtTheLimit(const std::filesystem::path& root) {+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ // Every server slows down by 2.5 times every 100 s.+ const auto rate = [](std::int64_t now_ms, int) {+ double value = 4.0;+ for (std::int64_t step = 100000; step <= now_ms && step <= 400000; step += 100000) value /= 2.5;+ return value;+ };+ net.Add(kFastly, 0);+ net.Add(kAkamai, 0);+ net.specs[kFastly].rate = rate;+ net.specs[kAkamai].rate = rate;+ world.manifests[441].files = {net.File("pak", 1350, 512 * 1000)};+ const auto run = RunFastest(net, root, {1});+ CHECK(run.outcome.error == InstallError::kNone);+ CHECK(net.now_ms > 460000);+ std::size_t reraces = 0, limits = 0;+ for (const auto& line : run.races) {+ if (line.rfind("rerace won ", 0) == 0) ++reraces;+ if (line.rfind("rerace limit ", 0) == 0) ++limits;+ }+ CHECK(reraces == 2 && limits == 1 && run.races.size() == 4);+}++void QueueNamesTheServerInUse(const std::filesystem::path& root) {+ {+ // Steam's first server refuses everything: the name follows the failover.+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add(kFastly, 0);+ net.Add(kAkamai, 10);+ world.manifests[441].files = {net.File("pak", 40, kMegabyte)};+ const auto run = RunFastest(net, root.string() + "-failover", {1});+ CHECK(run.outcome.error == InstallError::kNone && Has(run.races, "race skipped-small "));+ CHECK(run.first_written_name == std::string("Akamai") && run.server_name == "Akamai");+ }+ {+ // A provider without a display name is not shown by its host.+ auto world = MakeWorld();+ FakeNet net;+ FastestWorld(net, world);+ net.Add("cache1.isp.example", 10);+ world.manifests[441].files = {net.File("pak", 40, kMegabyte)};+ const auto run = RunFastest(net, root.string() + "-cache", {1});+ CHECK(run.outcome.error == InstallError::kNone && run.server_name == "Network cache");+ }+}++void FastestWinnerIsRemembered() {+ std::int64_t minutes = 0;+ FastestServerCache cache([&minutes] {+ return std::chrono::steady_clock::time_point(std::chrono::minutes(minutes));+ });+ CHECK(!cache.Get(7));+ cache.Remember(7, kFastly);+ minutes = 44;+ CHECK(cache.Get(7) == std::optional<std::string>(kFastly));+ cache.Remember(7, kAkamai); // a new winner replaces the old one and starts its time again+ minutes = 88;+ CHECK(cache.Get(7) == std::optional<std::string>(kAkamai));+ minutes = 89;+ CHECK(!cache.Get(7)); // 45 minutes+ CHECK(!cache.Get(7));+ cache.Remember(7, kGoogle);+ CHECK(!cache.Get(8)); // another account+ CHECK(!cache.Get(7));+ cache.Remember(7, kGoogle);+ cache.Remember(7, ""); // a re-race without a winner+ CHECK(!cache.Get(7));+ cache.Remember(7, kGoogle);+ cache.Clear();+ CHECK(!cache.Get(7));+ // Signing out: the runner finds no session and forgets the winner.+ CmClient cm(CmClientHooks{}, cm::DeviceIdentity{});+ CdnClient cdn([](const HttpRequest&, const std::atomic<bool>&) { return HttpResponse{}; });+ struct NoSessions final : SessionRepository {+ Result<std::optional<StoredSession>> Load() const override { return Result<std::optional<StoredSession>>::Success(std::nullopt); }+ Result<void> Save(const StoredSession&) const override { return Result<void>::Success(); }+ Result<void> Clear() const override { return Result<void>::Success(); }+ } sessions;+ SteamInstallRunner runner(sessions, cm, cdn);+ runner.fastest_server().Remember(7, kFastly);+ std::atomic<bool> cancel{false};+ const auto result = runner.Install(GameId::Steam(440), "/nowhere/440", cancel, {}, {});+ CHECK(result.retry_later && !runner.fastest_server().Get(7));+}++void FastestTuningFile(const std::filesystem::path& root) {+ std::filesystem::create_directories(root);+ const auto path = (root / "tuning").string();+ std::ofstream(path) << "slots=6\nserver=fastest\n";+ const auto tuning = ReadDownloadTuning(path);+ CHECK(tuning.slots == 6 && tuning.fastest && tuning.preferred_server.empty());+ std::ofstream(path) << "server=fastest\nserver=fastest\n";+ CHECK(!ReadDownloadTuning(path).fastest);+}+int main() {const auto base = std::filesystem::temp_directory_path() / ("playden-installer-test-" + std::to_string(::getpid()));std::filesystem::remove_all(base);@@ -1360,6 +1928,22 @@ int main() {NoContentServersSaysWhy(base / "no-servers");BrokenManifestIsFetchedAgainElsewhere(base / "manifest");RemovesOnlyManagedFolders(base);+ FastestTuningFile(base / "fastest-tuning");+ FastestWinnerIsRemembered();+ QueueNamesTheServerInUse(base / "race-name");+ RaceWinsAndLaterChunksUseIt(base / "race-wins");+ TinyChunksSkipTheRace(base / "race-tiny");+ RaceFetchesLargeChunksAtTheEnd(base / "race-at-end");+ SlowWriterAndHandshakesDoNotDecide(base / "race-writer");+ SlowLinkEndsAtTheCap(base / "race-cap");+ FailingServerNeverWins(base / "race-failing");+ ResumeRacesOnlyWhatIsLeft(base / "race-resume");+ CachedWinnerSkipsTheRace(base / "race-cached");+ RaceWithoutRequestTimingKeepsSteamOrder(base / "race-untimed");+ SagReracesToAnotherServer(base / "rerace");+ TinyFilesAndSlowWritersDoNotRerace(base / "rerace-not");+ SteadyRateBelowTheRaceDoesNotRerace(base / "rerace-steady");+ ReracesStopAtTheLimit(base / "rerace-limit");std::filesystem::remove_all(base);std::puts("depot installer tests passed");}diff --git a/tests/homebrew_library_model_test.cpp b/tests/homebrew_library_model_test.cppindex 97137fa..3923a8b 100644--- a/tests/homebrew_library_model_test.cpp+++ b/tests/homebrew_library_model_test.cpp@@ -3180,23 +3180,27 @@ void live_download_server_picker() {servers.phase = "listed";servers.servers = {{"akamai.invalid", "Akamai"}, {"fastly.invalid", "Fastly"}, {"cache.invalid", "Network cache", true}};model.ApplyLiveDownloadServers(servers);- check(find_row(model, "settings-download-server")->value == "Default" &&- find_row(model, "settings-download-server")->subtitle ==- "Uses the server Steam lists first. Choose one if downloads are slow.",- "the server row explains Default");+ check(find_row(model, "settings-download-server")->value == "Steam default" &&+ find_row(model, "settings-download-server")->subtitle == "Uses the server Steam lists first.",+ "the server row explains Steam default");activate(model, "settings-download-server");check(model.TakeDownloadServerRequests() == std::vector<DownloadServerRequest>{DownloadServerRequest::List},"opening the picker asks the worker for a new list");check(find_row(model, "download-server-listing")->title == "Getting the server list…" &&model.state().navigation.focus_key == "download-server-auto" &&model.view().subtitle == "Applies to downloads that start or resume from now.",- "listing keeps Default available and explains when the choice applies");+ "listing keeps Steam default available and explains when the choice applies");model.ApplyLiveDownloadServers(servers);check(model.state().navigation.modals.back().action_keys ==- std::vector<std::string>{"download-server-test", "download-server-auto", "download-server-0",- "download-server-1", "download-server-2", "cancel"} &&- find_row(model, "download-server-auto")->title == "Default",- "Test servers comes first, then Default and the listed servers");+ std::vector<std::string>{"download-server-fastest", "download-server-auto", "download-server-0",+ "download-server-1", "download-server-2", "download-server-test", "cancel"} &&+ find_row(model, "download-server-fastest")->title == "Fastest" &&+ find_row(model, "download-server-fastest")->subtitle ==+ "Tests the servers when a download starts and uses the fastest one." &&+ !find_row(model, "download-server-fastest")->selected &&+ find_row(model, "download-server-auto")->title == "Steam default" &&+ find_row(model, "download-server-auto")->selected,+ "Fastest comes first, then Steam default, the listed servers and Test servers");check(find_row(model, "download-server-2")->subtitle == "HTTP", "plain HTTP servers are marked");activate(model, "download-server-1");check(model.state().navigation.modals.empty() && model.state().download_server == "fastly.invalid" &&@@ -3257,7 +3261,7 @@ void live_download_server_picker() {restarted.ApplyLiveDownloadServers(servers);check(find_row(restarted, "settings-download-server")->value == "Akamai" &&find_row(restarted, "settings-download-server")->subtitle ==- "Not offered on this network now. Default is used.", "a missing stored server falls back visibly");+ "Not offered on this network now. Steam default is used.", "a missing stored server falls back visibly");activate(restarted, "settings-download-server");(void)restarted.TakeDownloadServerRequests();servers.phase = "list-failed";@@ -3288,7 +3292,42 @@ void live_download_server_picker() {check(find_row(restarted, "download-server-test")->enabled, "pausing the download enables measurement");activate(restarted, "download-server-auto");check(restarted.state().download_server.empty() && restarted.ExportPreferences().download_server.empty(),- "Default clears the stored choice");+ "Steam default clears the stored choice");+ activate(restarted, "settings-download-server");+ (void)restarted.TakeDownloadServerRequests();+ restarted.ApplyLiveDownloadServers(servers);+ activate(restarted, "download-server-fastest");+ check(restarted.state().navigation.modals.empty() && restarted.ExportPreferences().download_server == "fastest" &&+ find_row(restarted, "settings-download-server")->value == "Fastest" &&+ find_row(restarted, "settings-download-server")->subtitle ==+ "Tests the servers when a download starts and uses the fastest one.",+ "choosing Fastest stores it and the row says so");+ activate(restarted, "settings-download-server");+ (void)restarted.TakeDownloadServerRequests();+ check(restarted.state().navigation.focus_key == "download-server-fastest", "the picker opens on Fastest while listing");+ restarted.ApplyLiveDownloadServers(servers);+ check(restarted.state().navigation.focus_key == "download-server-fastest" &&+ find_row(restarted, "download-server-fastest")->selected && !find_row(restarted, "download-server-auto")->selected,+ "the picker marks and focuses Fastest");+ restarted.UpdateInstalls({{SteamId(10), JobState::Paused, 1, 100}}, {});+ activate(restarted, "download-server-test");+ (void)restarted.TakeDownloadServerRequests();+ servers.phase = "tested";+ servers.rates = {{"fastly.invalid", 3.1}, {"cache.invalid", 9.5}};+ restarted.ApplyLiveDownloadServers(servers);+ check(restarted.state().navigation.focus_key == "download-server-1" && restarted.state().download_server == "fastest",+ "after a test the focus goes to the fastest tested server, not to Fastest");+ // A version 8 file written with Fastest loads as Fastest.+ auto reloaded = make_live();+ playden::PreferencesLoadResult fastest;+ fastest.preferences.download_server = "fastest";+ check(reloaded.ApplyPreferences(reloaded.BeginPreferencesLoad(), fastest) &&+ reloaded.state().download_server == "fastest", "a stored Fastest loads");+ reloaded.UpdateAccount({AuthState::SignedIn, "account", "Owner", {}, {}, false});+ reloaded.action("l1");+ servers.phase = "listed";+ reloaded.ApplyLiveDownloadServers(servers);+ check(find_row(reloaded, "settings-download-server")->value == "Fastest", "a loaded Fastest shows in Settings");}void leftover_titles_require_confirmation_and_follow_the_queue() {diff --git a/tests/homebrew_live_model_test.cpp b/tests/homebrew_live_model_test.cppindex 1a18a33..fb90744 100644--- a/tests/homebrew_live_model_test.cpp+++ b/tests/homebrew_live_model_test.cpp@@ -675,12 +675,18 @@ void live_download_telemetry() {check(FormatDownloadRate(0, {}) == "0.0 MB/s · —", "stalled rate has no invented ETA");check(FormatDownloadRate(1048576, {}) == "1.0 MB/s · —", "unknown total keeps rate but hides ETA");check(FormatDownloadRate({}, 300) == "—", "unknown rate hides stale ETA");+ check(FormatDownloadRate(1048576, 61, "Fastly") == "Fastly · 1.0 MB/s · ~2 min left", "the server comes first");+ check(FormatDownloadRate({}, 61, "Fastly") == "—", "no rate, no server");+ job.server_name = "Akamai";+ model.UpdateInstalls({job}, {});+ check(model.state().jobs[0].server_name == "Akamai", "the live server name reaches the Queue");check(FormatDownloadRate(1048576, 59) == "1.0 MB/s · ~59 sec left", "short ETA");check(FormatDownloadRate(1048576, 61) == "1.0 MB/s · ~2 min left", "ETA rounds up");for (const auto state : {JobState::Paused, JobState::Queued, JobState::Failed}) {job.state = state; // stale incoming fields must never appear outside an active transfermodel.UpdateInstalls({job}, {});- check(!model.state().jobs[0].bytes_per_second && !model.state().jobs[0].eta_seconds, "inactive telemetry clears");+ check(!model.state().jobs[0].bytes_per_second && !model.state().jobs[0].eta_seconds &&+ model.state().jobs[0].server_name.empty(), "inactive telemetry clears");}// An imported game's copy into Playden says "Copying" (tiles, Queue) where a store's download says// "Downloading"; other states and a job's own words read the same for both.diff --git a/tests/homebrew_simulation_test.cpp b/tests/homebrew_simulation_test.cppindex 5b3fd1a..c045347 100644--- a/tests/homebrew_simulation_test.cpp+++ b/tests/homebrew_simulation_test.cpp@@ -1398,7 +1398,7 @@ void synthetic_download_servers(const std::filesystem::path& fixtures, const stdconst auto press = [&simulation](const std::string& key) {check(simulation.action("focus:" + key) && simulation.action("cross"), "synthetic server action exists");};- check(row("settings-download-server") && row("settings-download-server")->value == "Default",+ check(row("settings-download-server") && row("settings-download-server")->value == "Steam default","the synthetic scenario shows the Settings row");press("settings-download-server");check(row("download-server-6") && !row("download-server-7") && row("download-server-3")->subtitle == "HTTP" &&@@ -1426,6 +1426,22 @@ void synthetic_download_servers(const std::filesystem::path& fixtures, const stdrestarted.scenario("download-servers");check(restarted.state().download_server == simulation.state().download_server,"the simulation profile keeps the choice across restarts");+ check(restarted.action("focus:settings-download-server") && restarted.action("cross") &&+ restarted.state().navigation.focus_key == "download-server-1" &&+ restarted.action("focus:download-server-fastest") && restarted.action("cross"),+ "the synthetic picker offers Fastest");+ check(restarted.state().download_server == "fastest" && restarted.state().navigation.modals.empty(),+ "the synthetic Fastest choice is saved");+ for (const auto& item : restarted.view().rows)+ if (item.key == "settings-download-server") check(item.value == "Fastest", "the row shows Fastest");+ // Queue: the download names its server before the rate.+ check(restarted.scenario("queue-one-download"), "queue scenario exists");+ check(restarted.state().jobs.front().server_name == "Fastly" &&+ playden::homebrew::FormatDownloadRate(restarted.state().jobs.front().bytes_per_second,+ restarted.state().jobs.front().eta_seconds,+ restarted.state().jobs.front().server_name) ==+ "Fastly · 17.4 MB/s · ~3 min left",+ "the Queue row names the download server");}void leftover_titles_simulation_uses_controller_navigation(const std::filesystem::path& fixtures,diff --git a/tests/install_service_test.cpp b/tests/install_service_test.cppindex f6e34a5..f196d85 100644--- a/tests/install_service_test.cpp+++ b/tests/install_service_test.cpp@@ -47,6 +47,7 @@ class Runner final : public SessionInstallRunner {std::atomic<bool> checking_licenses{false}; // holds the run in the license checkbool telemetry = false;std::uint64_t held = 0; // bytes the fake install reports on disk+ std::string server; // DownloadServerName while installingstd::atomic<int> installs{0};std::atomic<int> canceled{0};std::atomic<int> unreachable{0}; // this many runs find Steam out of reach@@ -84,6 +85,7 @@ class Runner final : public SessionInstallRunner {progress(100, 100, held);return {true, false, {}, 4096};}+ std::string DownloadServerName() const override { return server; }std::string remove_failure; // the next removals fail with this, leaving the folderstd::string Remove(const std::string& root) override {std::lock_guard<std::mutex> lock(mutex);@@ -174,7 +176,7 @@ void SpaceAdmission(const std::filesystem::path& root) {void LiveTelemetryDecaysWithoutCallbacks(const std::filesystem::path& root) {Sessions sessions;- Runner runner(sessions); runner.block = true; runner.telemetry = true;+ Runner runner(sessions); runner.block = true; runner.telemetry = true; runner.server = "Fastly";InstallService service(runner, root.string());service.Install(SteamId(900));AwaitAt(__LINE__, service, [](const InstallSnapshot& snapshot) {@@ -185,6 +187,7 @@ void LiveTelemetryDecaysWithoutCallbacks(const std::filesystem::path& root) {const auto* job = JobOf(moving, SteamId(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+ CHECK(job->server_name == "Fastly"); // the runner's download server, with each progress reportconst auto deadline = std::chrono::steady_clock::now() + 9s;bool stalled = false;while (std::chrono::steady_clock::now() < deadline) {@@ -203,6 +206,7 @@ void LiveTelemetryDecaysWithoutCallbacks(const std::filesystem::path& root) {CHECK(!JobOf(paused, SteamId(900))->bytes_per_second && !JobOf(paused, SteamId(900))->eta_seconds);AwaitAt(__LINE__, service, [&](const InstallSnapshot&) { return runner.canceled == 1; });CHECK(!JobOf(service.Snapshot(), SteamId(900))->bytes_per_second); // late callback cannot restore it+ CHECK(JobOf(service.Snapshot(), SteamId(900))->server_name.empty());service.Resume(SteamId(900));AwaitAt(__LINE__, service, [&](const InstallSnapshot& snapshot) {const auto* current = JobOf(snapshot, SteamId(900));diff --git a/tests/preferences_test.cpp b/tests/preferences_test.cppindex 6045232..3d7a2bc 100644--- a/tests/preferences_test.cpp+++ b/tests/preferences_test.cpp@@ -327,7 +327,8 @@ void DownloadServerRoundTripsAndInvalidHostsUseAutomatic() {const std::string prefix ="version=8\nreduce_motion=1\ntitle_store_suffix=1\ncloud_saves_auto=1\nsteam_sign_in_password=1\n""downloads_slow_network=1\nsteam_stay_signed_in=0\nbeta_updates=1\ndownload_server=";- const std::string valid[] = {"", "fastly.cdn.steampipe.steamcontent.com", "CACHE-1.Example.net", std::string(253, 'a')};+ // "fastest" is the Fastest choice; it needs no version change.+ const std::string valid[] = {"", "fastest", "fastly.cdn.steampipe.steamcontent.com", "CACHE-1.Example.net", std::string(253, 'a')};for (const auto& host : valid) {playden::Preferences preferences;preferences.download_server = host;@@ -336,6 +337,14 @@ void DownloadServerRoundTripsAndInvalidHostsUseAutomatic() {Check(loaded.status == playden::PreferencesLoadStatus::kLoaded && loaded.preferences.download_server == host,"Automatic and valid download server hosts survive restart");}+ {+ const std::string serialized = prefix + "fastest\n";+ Check(storage.WriteAtomic("preferences.conf", playden::Bytes(serialized.begin(), serialized.end())).ok(),+ "version 8 fastest fixture is written");+ const auto loaded = store.Load();+ Check(loaded.status == playden::PreferencesLoadStatus::kLoaded && loaded.preferences.download_server == "fastest",+ "a version 8 file with Fastest loads it");+ }const std::string invalid[] = {"https://cache.example", "cache.example/path", "cache.example:443", "cache_name","cache name", "cache\tname", "cache\rname", "cache=name", "caf\xc3\xa9.example",std::string("cache\0name", 10), std::string(254, 'a')};