| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/Analytics.h" |
| 5 | |
| 6 | #include <cmath> |
| 7 | #include <cstdio> |
| 8 | #include <string> |
| 9 | #include <type_traits> |
| 10 | |
| 11 | #include "Common/CommonTypes.h" |
| 12 | #include "Common/StringUtil.h" |
| 13 | #include "Common/Thread.h" |
| 14 | |
| 15 | namespace Common |
| 16 | { |
| 17 | namespace |
| 18 | { |
| 19 | // Format version number, used as the first byte of every report sent. |
| 20 | // Increment for any change to the wire format. |
| 21 | constexpr u8 WIRE_FORMAT_VERSION = 1; |
| 22 | |
| 23 | // Identifiers for the value types supported by the analytics reporting wire |
| 24 | // format. |
| 25 | enum class TypeId : u8 |
| 26 | { |
| 27 | STRING = 0, |
| 28 | BOOL = 1, |
| 29 | UINT = 2, |
| 30 | SINT = 3, |
| 31 | FLOAT = 4, |
| 32 | |
| 33 | // Flags which can be combined with other types. |
| 34 | ARRAY = 0x80, |
| 35 | }; |
| 36 | |
| 37 | TypeId operator|(TypeId l, TypeId r) |
| 38 | { |
| 39 | using ut = std::underlying_type_t<TypeId>; |
| 40 | return static_cast<TypeId>(static_cast<ut>(l) | static_cast<ut>(r)); |
| 41 | } |
| 42 | |
| 43 | void AppendBool(std::string* out, bool v) |
| 44 | { |
| 45 | out->push_back(v ? '\xFF' : '\x00'); |
| 46 | } |
| 47 | |
| 48 | void AppendVarInt(std::string* out, u64 v) |
| 49 | { |
| 50 | do |
| 51 | { |
| 52 | u8 current_byte = v & 0x7F; |
| 53 | v >>= 7; |
| 54 | current_byte |= (!!v) << 7; |
| 55 | out->push_back(current_byte); |
| 56 | } while (v); |
| 57 | } |
| 58 | |
| 59 | void AppendBytes(std::string* out, const u8* bytes, u32 length, bool encode_length = true) |
| 60 | { |
| 61 | if (encode_length) |
| 62 | { |
| 63 | AppendVarInt(out, length); |
| 64 | } |
| 65 | out->append(reinterpret_cast<const char*>(bytes), length); |
| 66 | } |
| 67 | |
| 68 | void AppendType(std::string* out, TypeId type) |
| 69 | { |
| 70 | out->push_back(static_cast<u8>(type)); |
| 71 | } |
| 72 | } // namespace |
| 73 | |
| 74 | AnalyticsReportBuilder::AnalyticsReportBuilder() |
| 75 | { |
| 76 | m_report.push_back(WIRE_FORMAT_VERSION); |
| 77 | } |
| 78 | |
| 79 | void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, std::string_view v) |
| 80 | { |
| 81 | AppendType(report, TypeId::STRING); |
| 82 | AppendBytes(report, reinterpret_cast<const u8*>(v.data()), static_cast<u32>(v.size())); |
| 83 | } |
| 84 | |
| 85 | // We can't remove this overload despite the string_view overload due to the fact that |
| 86 | // pointers can implicitly convert to bool, so if we removed the overload, then all |
| 87 | // const char strings passed in would begin forwarding to the bool overload, |
| 88 | // which is definitely not what we want to occur. |
| 89 | void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, const char* v) |
| 90 | { |
| 91 | AppendSerializedValue(report, std::string_view(v)); |
| 92 | } |
| 93 | |
| 94 | void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, bool v) |
| 95 | { |
| 96 | AppendType(report, TypeId::BOOL); |
| 97 | AppendBool(report, v); |
| 98 | } |
| 99 | |
| 100 | void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, u64 v) |
| 101 | { |
| 102 | AppendType(report, TypeId::UINT); |
| 103 | AppendVarInt(report, v); |
| 104 | } |
| 105 | |
| 106 | void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, s64 v) |
| 107 | { |
| 108 | AppendType(report, TypeId::SINT); |
| 109 | AppendBool(report, v >= 0); |
| 110 | AppendVarInt(report, static_cast<u64>(std::abs(v))); |
| 111 | } |
| 112 | |
| 113 | void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, u32 v) |
| 114 | { |
| 115 | AppendSerializedValue(report, static_cast<u64>(v)); |
| 116 | } |
| 117 | |
| 118 | void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, s32 v) |
| 119 | { |
| 120 | AppendSerializedValue(report, static_cast<s64>(v)); |
| 121 | } |
| 122 | |
| 123 | void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, float v) |
| 124 | { |
| 125 | AppendType(report, TypeId::FLOAT); |
| 126 | AppendBytes(report, reinterpret_cast<u8*>(&v), sizeof(v), false); |
| 127 | } |
| 128 | |
| 129 | void AnalyticsReportBuilder::AppendSerializedValueVector(std::string* report, |
| 130 | const std::vector<u32>& v) |
| 131 | { |
| 132 | AppendType(report, TypeId::UINT | TypeId::ARRAY); |
| 133 | AppendVarInt(report, v.size()); |
| 134 | for (u32 x : v) |
| 135 | AppendVarInt(report, x); |
| 136 | } |
| 137 | |
| 138 | AnalyticsReporter::AnalyticsReporter() |
| 139 | { |
| 140 | m_reporter_thread = std::thread(&AnalyticsReporter::ThreadProc, this); |
| 141 | } |
| 142 | |
| 143 | AnalyticsReporter::~AnalyticsReporter() |
| 144 | { |
| 145 | // Set the exit request flag and wait for the thread to honor it. |
| 146 | m_reporter_stop_request.Set(); |
| 147 | m_reporter_event.Set(); |
| 148 | m_reporter_thread.join(); |
| 149 | } |
| 150 | |
| 151 | void AnalyticsReporter::Send(AnalyticsReportBuilder&& report) |
| 152 | { |
| 153 | #if defined(USE_ANALYTICS) && USE_ANALYTICS |
| 154 | // Put a bound on the size of the queue to avoid uncontrolled memory growth. |
| 155 | constexpr u32 QUEUE_SIZE_LIMIT = 25; |
| 156 | if (m_reports_queue.Size() < QUEUE_SIZE_LIMIT) |
| 157 | { |
| 158 | m_reports_queue.Push(report.Consume()); |
| 159 | m_reporter_event.Set(); |
| 160 | } |
| 161 | #endif |
| 162 | } |
| 163 | |
| 164 | void AnalyticsReporter::ThreadProc() |
| 165 | { |
| 166 | Common::SetCurrentThreadName("Analytics"); |
| 167 | while (true) |
| 168 | { |
| 169 | m_reporter_event.Wait(); |
| 170 | if (m_reporter_stop_request.IsSet()) |
| 171 | { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | while (!m_reports_queue.Empty()) |
| 176 | { |
| 177 | std::shared_ptr<AnalyticsReportingBackend> backend(m_backend); |
| 178 | |
| 179 | if (backend) |
| 180 | { |
| 181 | std::string report; |
| 182 | m_reports_queue.Pop(report); |
| 183 | backend->Send(std::move(report)); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | // Recheck after each report sent. |
| 191 | if (m_reporter_stop_request.IsSet()) |
| 192 | { |
| 193 | return; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void StdoutAnalyticsBackend::Send(std::string report) |
| 200 | { |
| 201 | printf("Analytics report sent:\n%s", |
| 202 | HexDump(reinterpret_cast<const u8*>(report.data()), report.size()).c_str()); |
| 203 | } |
| 204 | |
| 205 | HttpAnalyticsBackend::HttpAnalyticsBackend(std::string endpoint) : m_endpoint(std::move(endpoint)) |
| 206 | { |
| 207 | } |
| 208 | |
| 209 | HttpAnalyticsBackend::~HttpAnalyticsBackend() = default; |
| 210 | |
| 211 | void HttpAnalyticsBackend::Send(std::string report) |
| 212 | { |
| 213 | if (m_http.IsValid()) |
| 214 | m_http.Post(m_endpoint, report); |
| 215 | } |
| 216 | } // namespace Common |