From 21adfa7567c895e477d1a84c4c99de58981b72b5 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sat, 3 Jun 2023 21:07:56 +0200 Subject: [PATCH] Fix: track "memory installed" for surveys less precisely (#10910) It turns out, for Windows and Linux having the exact memory allows for easy tracing of an individual. That is exactly against the idea of the survey. And honestly, we don't need this precision. --- src/network/network_survey.cpp | 29 +++++++++++++++++++++++++++-- src/os/macosx/survey_osx.cpp | 6 +++++- src/os/unix/survey_unix.cpp | 6 +++++- src/os/windows/survey_win.cpp | 6 +++++- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/network/network_survey.cpp b/src/network/network_survey.cpp index d1c229c7b7..b3f64662a6 100644 --- a/src/network/network_survey.cpp +++ b/src/network/network_survey.cpp @@ -298,6 +298,33 @@ static void SurveyGameScript(nlohmann::json &survey) survey = fmt::format("{}.{}", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion()); } +/** + * Change the bytes of memory into a textual version rounded up to the biggest unit. + * + * For example, 16751108096 would become 16 GiB. + * + * @param memory The bytes of memory. + * @return std::string A textual representation. + */ +std::string SurveyMemoryToText(uint64_t memory) +{ + memory = memory / 1024; // KiB + memory = CeilDiv(memory, 1024); // MiB + + /* Anything above 512 MiB we represent in GiB. */ + if (memory > 512) { + return fmt::format("{} GiB", CeilDiv(memory, 1024)); + } + + /* Anything above 64 MiB we represent in a multiplier of 128 MiB. */ + if (memory > 64) { + return fmt::format("{} MiB", Ceil(memory, 128)); + } + + /* Anything else in a multiplier of 4 MiB. */ + return fmt::format("{} MiB", Ceil(memory, 4)); +} + #endif /* WITH_NLOHMANN_JSON */ /** @@ -328,8 +355,6 @@ std::string NetworkSurveyHandler::CreatePayload(Reason reason, bool for_preview) { auto &info = survey["info"]; SurveyOS(info["os"]); - info["os"]["hardware_concurrency"] = std::thread::hardware_concurrency(); - SurveyOpenTTD(info["openttd"]); SurveyConfiguration(info["configuration"]); SurveyFont(info["font"]); diff --git a/src/os/macosx/survey_osx.cpp b/src/os/macosx/survey_osx.cpp index 88edebd9f6..14260fbd68 100644 --- a/src/os/macosx/survey_osx.cpp +++ b/src/os/macosx/survey_osx.cpp @@ -16,9 +16,12 @@ #include #include +#include #include "../../safeguards.h" +extern std::string SurveyMemoryToText(uint64_t memory); + void SurveyOS(nlohmann::json &json) { int ver_maj, ver_min, ver_bug; @@ -32,7 +35,8 @@ void SurveyOS(nlohmann::json &json) json["min_ver"] = MAC_OS_X_VERSION_MIN_REQUIRED; json["max_ver"] = MAC_OS_X_VERSION_MAX_ALLOWED; - json["memory"] = MacOSGetPhysicalMemory(); + json["memory"] = SurveyMemoryToText(MacOSGetPhysicalMemory()); + json["hardware_concurrency"] = std::thread::hardware_concurrency(); } #endif /* WITH_NLOHMANN_JSON */ diff --git a/src/os/unix/survey_unix.cpp b/src/os/unix/survey_unix.cpp index d86b58e98c..d831a78390 100644 --- a/src/os/unix/survey_unix.cpp +++ b/src/os/unix/survey_unix.cpp @@ -13,10 +13,13 @@ #include #include +#include #include #include "../../safeguards.h" +extern std::string SurveyMemoryToText(uint64_t memory); + void SurveyOS(nlohmann::json &json) { struct utsname name; @@ -32,7 +35,8 @@ void SurveyOS(nlohmann::json &json) long pages = sysconf(_SC_PHYS_PAGES); long page_size = sysconf(_SC_PAGE_SIZE); - json["memory"] = pages * page_size; + json["memory"] = SurveyMemoryToText(pages * page_size); + json["hardware_concurrency"] = std::thread::hardware_concurrency(); } #endif /* WITH_NLOHMANN_JSON */ diff --git a/src/os/windows/survey_win.cpp b/src/os/windows/survey_win.cpp index 407ddb31d4..1af48a39e7 100644 --- a/src/os/windows/survey_win.cpp +++ b/src/os/windows/survey_win.cpp @@ -14,10 +14,13 @@ #include "../../3rdparty/fmt/format.h" #include +#include #include #include "../../safeguards.h" +extern std::string SurveyMemoryToText(uint64_t memory); + void SurveyOS(nlohmann::json &json) { _OSVERSIONINFOA os; @@ -31,7 +34,8 @@ void SurveyOS(nlohmann::json &json) status.dwLength = sizeof(status); GlobalMemoryStatusEx(&status); - json["memory"] = status.ullTotalPhys; + json["memory"] = SurveyMemoryToText(status.ullTotalPhys); + json["hardware_concurrency"] = std::thread::hardware_concurrency(); } #endif /* WITH_NLOHMANN_JSON */