2016-06-18 00:43:59 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-06-18 00:43:59 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "Common/Analytics.h"
|
|
|
|
|
2016-06-18 00:43:59 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
2018-10-27 02:58:39 +00:00
|
|
|
#include <type_traits>
|
2016-06-18 00:43:59 +00:00
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2017-04-29 07:29:52 +00:00
|
|
|
#include "Common/Thread.h"
|
2016-06-18 00:43:59 +00:00
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// Format version number, used as the first byte of every report sent.
|
|
|
|
// Increment for any change to the wire format.
|
2018-10-27 02:58:39 +00:00
|
|
|
constexpr u8 WIRE_FORMAT_VERSION = 1;
|
2016-06-18 00:43:59 +00:00
|
|
|
|
|
|
|
// Identifiers for the value types supported by the analytics reporting wire
|
|
|
|
// format.
|
|
|
|
enum class TypeId : u8
|
|
|
|
{
|
|
|
|
STRING = 0,
|
|
|
|
BOOL = 1,
|
|
|
|
UINT = 2,
|
|
|
|
SINT = 3,
|
|
|
|
FLOAT = 4,
|
2018-10-27 02:58:39 +00:00
|
|
|
|
|
|
|
// Flags which can be combined with other types.
|
|
|
|
ARRAY = 0x80,
|
2016-06-18 00:43:59 +00:00
|
|
|
};
|
|
|
|
|
2018-10-27 02:58:39 +00:00
|
|
|
TypeId operator|(TypeId l, TypeId r)
|
|
|
|
{
|
|
|
|
using ut = std::underlying_type_t<TypeId>;
|
|
|
|
return static_cast<TypeId>(static_cast<ut>(l) | static_cast<ut>(r));
|
|
|
|
}
|
|
|
|
|
2016-06-18 00:43:59 +00:00
|
|
|
void AppendBool(std::string* out, bool v)
|
|
|
|
{
|
|
|
|
out->push_back(v ? '\xFF' : '\x00');
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendVarInt(std::string* out, u64 v)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
u8 current_byte = v & 0x7F;
|
|
|
|
v >>= 7;
|
|
|
|
current_byte |= (!!v) << 7;
|
|
|
|
out->push_back(current_byte);
|
|
|
|
} while (v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendBytes(std::string* out, const u8* bytes, u32 length, bool encode_length = true)
|
|
|
|
{
|
|
|
|
if (encode_length)
|
|
|
|
{
|
|
|
|
AppendVarInt(out, length);
|
|
|
|
}
|
|
|
|
out->append(reinterpret_cast<const char*>(bytes), length);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendType(std::string* out, TypeId type)
|
|
|
|
{
|
|
|
|
out->push_back(static_cast<u8>(type));
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
AnalyticsReportBuilder::AnalyticsReportBuilder()
|
|
|
|
{
|
|
|
|
m_report.push_back(WIRE_FORMAT_VERSION);
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:35:26 +00:00
|
|
|
void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, std::string_view v)
|
2016-06-18 00:43:59 +00:00
|
|
|
{
|
|
|
|
AppendType(report, TypeId::STRING);
|
|
|
|
AppendBytes(report, reinterpret_cast<const u8*>(v.data()), static_cast<u32>(v.size()));
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:35:26 +00:00
|
|
|
// We can't remove this overload despite the string_view overload due to the fact that
|
|
|
|
// pointers can implicitly convert to bool, so if we removed the overload, then all
|
|
|
|
// const char strings passed in would begin forwarding to the bool overload,
|
|
|
|
// which is definitely not what we want to occur.
|
2016-06-18 00:43:59 +00:00
|
|
|
void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, const char* v)
|
|
|
|
{
|
2019-06-03 22:35:26 +00:00
|
|
|
AppendSerializedValue(report, std::string_view(v));
|
2016-06-18 00:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, bool v)
|
|
|
|
{
|
|
|
|
AppendType(report, TypeId::BOOL);
|
|
|
|
AppendBool(report, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, u64 v)
|
|
|
|
{
|
|
|
|
AppendType(report, TypeId::UINT);
|
|
|
|
AppendVarInt(report, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, s64 v)
|
|
|
|
{
|
|
|
|
AppendType(report, TypeId::SINT);
|
|
|
|
AppendBool(report, v >= 0);
|
|
|
|
AppendVarInt(report, static_cast<u64>(std::abs(v)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, u32 v)
|
|
|
|
{
|
|
|
|
AppendSerializedValue(report, static_cast<u64>(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, s32 v)
|
|
|
|
{
|
|
|
|
AppendSerializedValue(report, static_cast<s64>(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, float v)
|
|
|
|
{
|
|
|
|
AppendType(report, TypeId::FLOAT);
|
|
|
|
AppendBytes(report, reinterpret_cast<u8*>(&v), sizeof(v), false);
|
|
|
|
}
|
|
|
|
|
2018-10-27 02:58:39 +00:00
|
|
|
void AnalyticsReportBuilder::AppendSerializedValueVector(std::string* report,
|
|
|
|
const std::vector<u32>& v)
|
|
|
|
{
|
|
|
|
AppendType(report, TypeId::UINT | TypeId::ARRAY);
|
|
|
|
AppendVarInt(report, v.size());
|
|
|
|
for (u32 x : v)
|
|
|
|
AppendVarInt(report, x);
|
|
|
|
}
|
|
|
|
|
2016-06-18 00:43:59 +00:00
|
|
|
AnalyticsReporter::AnalyticsReporter()
|
|
|
|
{
|
|
|
|
m_reporter_thread = std::thread(&AnalyticsReporter::ThreadProc, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalyticsReporter::~AnalyticsReporter()
|
|
|
|
{
|
|
|
|
// Set the exit request flag and wait for the thread to honor it.
|
|
|
|
m_reporter_stop_request.Set();
|
|
|
|
m_reporter_event.Set();
|
|
|
|
m_reporter_thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyticsReporter::Send(AnalyticsReportBuilder&& report)
|
|
|
|
{
|
2017-04-26 21:27:13 +00:00
|
|
|
#if defined(USE_ANALYTICS) && USE_ANALYTICS
|
2016-06-18 00:43:59 +00:00
|
|
|
// Put a bound on the size of the queue to avoid uncontrolled memory growth.
|
|
|
|
constexpr u32 QUEUE_SIZE_LIMIT = 25;
|
|
|
|
if (m_reports_queue.Size() < QUEUE_SIZE_LIMIT)
|
|
|
|
{
|
|
|
|
m_reports_queue.Push(report.Consume());
|
|
|
|
m_reporter_event.Set();
|
|
|
|
}
|
2017-04-26 21:27:13 +00:00
|
|
|
#endif
|
2016-06-18 00:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnalyticsReporter::ThreadProc()
|
|
|
|
{
|
2017-04-29 07:29:52 +00:00
|
|
|
Common::SetCurrentThreadName("Analytics");
|
2016-06-18 00:43:59 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
m_reporter_event.Wait();
|
|
|
|
if (m_reporter_stop_request.IsSet())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-06-18 00:43:59 +00:00
|
|
|
while (!m_reports_queue.Empty())
|
|
|
|
{
|
|
|
|
std::shared_ptr<AnalyticsReportingBackend> backend(m_backend);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-06-18 00:43:59 +00:00
|
|
|
if (backend)
|
|
|
|
{
|
|
|
|
std::string report;
|
|
|
|
m_reports_queue.Pop(report);
|
|
|
|
backend->Send(std::move(report));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-06-18 00:43:59 +00:00
|
|
|
// Recheck after each report sent.
|
|
|
|
if (m_reporter_stop_request.IsSet())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StdoutAnalyticsBackend::Send(std::string report)
|
|
|
|
{
|
|
|
|
printf("Analytics report sent:\n%s",
|
|
|
|
HexDump(reinterpret_cast<const u8*>(report.data()), report.size()).c_str());
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:33:02 +00:00
|
|
|
HttpAnalyticsBackend::HttpAnalyticsBackend(std::string endpoint) : m_endpoint(std::move(endpoint))
|
2016-06-18 00:43:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-12 15:17:05 +00:00
|
|
|
HttpAnalyticsBackend::~HttpAnalyticsBackend() = default;
|
2016-06-18 00:43:59 +00:00
|
|
|
|
|
|
|
void HttpAnalyticsBackend::Send(std::string report)
|
|
|
|
{
|
2017-06-12 15:17:05 +00:00
|
|
|
if (m_http.IsValid())
|
|
|
|
m_http.Post(m_endpoint, report);
|
2016-06-18 00:43:59 +00:00
|
|
|
}
|
|
|
|
} // namespace Common
|