Debian Codegraph @

Source/Core/Common/Analytics.h

raw symbols SCIP
1 // Copyright 2016 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <chrono>
7 #include <memory>
8 #include <shared_mutex>
9 #include <string>
10 #include <string_view>
11 #include <thread>
12 #include <utility>
13 #include <vector>
14
15 #include "Common/CommonTypes.h"
16 #include "Common/Event.h"
17 #include "Common/Flag.h"
18 #include "Common/HttpRequest.h"
19 #include "Common/SPSCQueue.h"
20
21 // Utilities for analytics reporting in Dolphin. This reporting is designed to
22 // provide anonymous data about how well Dolphin performs in the wild. It also
23 // allows developers to declare trace points in Dolphin's source code and get
24 // information about what games trigger these conditions.
25 //
26 // This unfortunately implements Yet Another Serialization Framework within
27 // Dolphin. We cannot really use ChunkFile because there is precedents for
28 // backwards incompatible changes in the ChunkFile format. We could use
29 // something like protobuf but setting up external dependencies is Hard™.
30 //
31 // Example usage:
32 //
33 // static auto s_reporter = std::make_unique<AnalyticsReporter>();
34 // if (user_gave_consent)
35 // {
36 // s_reporter->SetBackend(std::make_unique<MyReportingBackend>());
37 // }
38 // s_reporter->Send(s_reporter->Builder()
39 // .AddData("my_key", 42)
40 // .AddData("other_key", false));
41
42 namespace Common
43 {
44 // Generic interface for an analytics reporting backends. The main
45 // implementation used in Dolphin can be found in Core/DolphinAnalytics.h.
46 class AnalyticsReportingBackend
47 {
48 public:
49 virtual ~AnalyticsReportingBackend() = default;
50 // Called from the AnalyticsReporter backend thread.
51 virtual void Send(std::string report) = 0;
52 };
53
54 // Builder object for an analytics report.
55 class AnalyticsReportBuilder
56 {
57 public:
58 AnalyticsReportBuilder();
59 ~AnalyticsReportBuilder() = default;
60
61 AnalyticsReportBuilder(const AnalyticsReportBuilder& other) : m_report{other.Get()} {}
62 AnalyticsReportBuilder(AnalyticsReportBuilder&& other) : m_report{other.Consume()} {}
63
64 const AnalyticsReportBuilder& operator=(const AnalyticsReportBuilder& other)
65 {
66 if (this != &other)
67 {
68 std::string other_report = other.Get();
69 std::lock_guard lk{m_lock};
70 m_report = std::move(other_report);
71 }
72 return *this;
73 }
74
75 // Append another builder to this one.
76 AnalyticsReportBuilder& AddBuilder(const AnalyticsReportBuilder& other)
77 {
78 // Get before locking the object to avoid deadlocks with this += this.
79 std::string other_report = other.Get();
80 std::lock_guard lk{m_lock};
81 m_report += other_report;
82 return *this;
83 }
84
85 template <typename T>
86 AnalyticsReportBuilder& AddData(std::string_view key, const T& value)
87 {
88 std::lock_guard lk{m_lock};
89 AppendSerializedValue(&m_report, key);
90 AppendSerializedValue(&m_report, value);
91 return *this;
92 }
93
94 template <typename T>
95 AnalyticsReportBuilder& AddData(std::string_view key, const std::vector<T>& value)
96 {
97 std::lock_guard lk{m_lock};
98 AppendSerializedValue(&m_report, key);
99 AppendSerializedValueVector(&m_report, value);
100 return *this;
101 }
102
103 std::string Get() const
104 {
105 std::shared_lock lk{m_lock};
106 return m_report;
107 }
108
109 // More efficient version of Get().
110 std::string Consume()
111 {
112 std::lock_guard lk{m_lock};
113 return std::move(m_report);
114 }
115
116 protected:
117 static void AppendSerializedValue(std::string* report, std::string_view v);
118 static void AppendSerializedValue(std::string* report, const char* v);
119 static void AppendSerializedValue(std::string* report, bool v);
120 static void AppendSerializedValue(std::string* report, u64 v);
121 static void AppendSerializedValue(std::string* report, s64 v);
122 static void AppendSerializedValue(std::string* report, u32 v);
123 static void AppendSerializedValue(std::string* report, s32 v);
124 static void AppendSerializedValue(std::string* report, float v);
125
126 static void AppendSerializedValueVector(std::string* report, const std::vector<u32>& v);
127
128 mutable std::shared_mutex m_lock;
129 std::string m_report;
130 };
131
132 class AnalyticsReporter
133 {
134 public:
135 AnalyticsReporter();
136 ~AnalyticsReporter();
137
138 // Sets a reporting backend and enables sending reports. Do not set a remote
139 // backend without user consent.
140 void SetBackend(std::unique_ptr<AnalyticsReportingBackend> backend)
141 {
142 m_backend = std::move(backend);
143 m_reporter_event.Set(); // In case reports are waiting queued.
144 }
145
146 // Gets the base report builder which is closed for each subsequent report
147 // being sent. DO NOT use this builder to send a report. Only use it to add
148 // new fields that should be globally available.
149 AnalyticsReportBuilder& BaseBuilder() { return m_base_builder; }
150 // Gets a cloned builder that can be used to send a report.
151 AnalyticsReportBuilder Builder() const { return m_base_builder; }
152 // Enqueues a report for sending. Consumes the report builder.
153 void Send(AnalyticsReportBuilder&& report);
154
155 // For convenience.
156 void Send(AnalyticsReportBuilder& report) { Send(std::move(report)); }
157
158 protected:
159 void ThreadProc();
160
161 std::shared_ptr<AnalyticsReportingBackend> m_backend;
162 AnalyticsReportBuilder m_base_builder;
163
164 std::thread m_reporter_thread;
165 Common::Event m_reporter_event;
166 Common::Flag m_reporter_stop_request;
167 SPSCQueue<std::string> m_reports_queue;
168 };
169
170 // Analytics backend to be used for debugging purpose, which dumps reports to
171 // stdout.
172 class StdoutAnalyticsBackend : public AnalyticsReportingBackend
173 {
174 public:
175 void Send(std::string report) override;
176 };
177
178 // Analytics backend that POSTs data to a remote HTTP(s) endpoint. WARNING:
179 // remember to get explicit user consent before using.
180 class HttpAnalyticsBackend : public AnalyticsReportingBackend
181 {
182 public:
183 explicit HttpAnalyticsBackend(std::string endpoint);
184 ~HttpAnalyticsBackend() override;
185
186 void Send(std::string report) override;
187
188 protected:
189 std::string m_endpoint;
190 HttpRequest m_http{std::chrono::seconds{5}};
191 };
192 } // namespace Common