summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common/CMakeLists.txt2
-rw-r--r--src/common/telemetry.cpp40
-rw-r--r--src/common/telemetry.h196
-rw-r--r--src/core/CMakeLists.txt2
-rw-r--r--src/core/core.cpp3
-rw-r--r--src/core/core.h16
-rw-r--r--src/core/loader/ncch.cpp3
-rw-r--r--src/core/telemetry_session.cpp42
-rw-r--r--src/core/telemetry_session.h38
9 files changed, 342 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 4b30185f1..6905d2d50 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -38,6 +38,7 @@ set(SRCS
38 param_package.cpp 38 param_package.cpp
39 scm_rev.cpp 39 scm_rev.cpp
40 string_util.cpp 40 string_util.cpp
41 telemetry.cpp
41 thread.cpp 42 thread.cpp
42 timer.cpp 43 timer.cpp
43 ) 44 )
@@ -74,6 +75,7 @@ set(HEADERS
74 string_util.h 75 string_util.h
75 swap.h 76 swap.h
76 synchronized_wrapper.h 77 synchronized_wrapper.h
78 telemetry.h
77 thread.h 79 thread.h
78 thread_queue_list.h 80 thread_queue_list.h
79 timer.h 81 timer.h
diff --git a/src/common/telemetry.cpp b/src/common/telemetry.cpp
new file mode 100644
index 000000000..bf1f54886
--- /dev/null
+++ b/src/common/telemetry.cpp
@@ -0,0 +1,40 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include "common/telemetry.h"
7
8namespace Telemetry {
9
10void FieldCollection::Accept(VisitorInterface& visitor) const {
11 for (const auto& field : fields) {
12 field.second->Accept(visitor);
13 }
14}
15
16void FieldCollection::AddField(std::unique_ptr<FieldInterface> field) {
17 fields[field->GetName()] = std::move(field);
18}
19
20template <class T>
21void Field<T>::Accept(VisitorInterface& visitor) const {
22 visitor.Visit(*this);
23}
24
25template class Field<bool>;
26template class Field<double>;
27template class Field<float>;
28template class Field<u8>;
29template class Field<u16>;
30template class Field<u32>;
31template class Field<u64>;
32template class Field<s8>;
33template class Field<s16>;
34template class Field<s32>;
35template class Field<s64>;
36template class Field<std::string>;
37template class Field<const char*>;
38template class Field<std::chrono::microseconds>;
39
40} // namespace Telemetry
diff --git a/src/common/telemetry.h b/src/common/telemetry.h
new file mode 100644
index 000000000..dd6bbd759
--- /dev/null
+++ b/src/common/telemetry.h
@@ -0,0 +1,196 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <chrono>
8#include <map>
9#include <memory>
10#include <string>
11#include "common/common_types.h"
12
13namespace Telemetry {
14
15/// Field type, used for grouping fields together in the final submitted telemetry log
16enum class FieldType : u8 {
17 None = 0, ///< No specified field group
18 App, ///< Citra application fields (e.g. version, branch, etc.)
19 Session, ///< Emulated session fields (e.g. title ID, log, etc.)
20 Performance, ///< Emulated performance (e.g. fps, emulated CPU speed, etc.)
21 UserFeedback, ///< User submitted feedback (e.g. star rating, user notes, etc.)
22 UserConfig, ///< User configuration fields (e.g. emulated CPU core, renderer, etc.)
23 UserSystem, ///< User system information (e.g. host CPU type, RAM, etc.)
24};
25
26struct VisitorInterface;
27
28/**
29 * Interface class for telemetry data fields.
30 */
31class FieldInterface : NonCopyable {
32public:
33 virtual ~FieldInterface() = default;
34
35 /**
36 * Accept method for the visitor pattern.
37 * @param visitor Reference to the visitor that will visit this field.
38 */
39 virtual void Accept(VisitorInterface& visitor) const = 0;
40
41 /**
42 * Gets the name of this field.
43 * @returns Name of this field as a string.
44 */
45 virtual const std::string& GetName() const = 0;
46};
47
48/**
49 * Represents a telemetry data field, i.e. a unit of data that gets logged and submitted to our
50 * telemetry web service.
51 */
52template <typename T>
53class Field : public FieldInterface {
54public:
55 Field(FieldType type, std::string name, const T& value)
56 : type(type), name(std::move(name)), value(value) {}
57
58 Field(FieldType type, std::string name, T&& value)
59 : type(type), name(std::move(name)), value(std::move(value)) {}
60
61 Field(const Field& other) : Field(other.type, other.name, other.value) {}
62
63 Field& operator=(const Field& other) {
64 type = other.type;
65 name = other.name;
66 value = other.value;
67 return *this;
68 }
69
70 Field& operator=(Field&& other) {
71 type = other.type;
72 name = std::move(other.name);
73 value = std::move(other.value);
74 return *this;
75 }
76
77 void Accept(VisitorInterface& visitor) const override;
78
79 const std::string& GetName() const override {
80 return name;
81 }
82
83 /**
84 * Returns the type of the field.
85 */
86 FieldType GetType() const {
87 return type;
88 }
89
90 /**
91 * Returns the value of the field.
92 */
93 const T& GetValue() const {
94 return value;
95 }
96
97 inline bool operator==(const Field<T>& other) {
98 return (type == other.type) && (name == other.name) && (value == other.value);
99 }
100
101 inline bool operator!=(const Field<T>& other) {
102 return !(*this == other);
103 }
104
105private:
106 std::string name; ///< Field name, must be unique
107 FieldType type{}; ///< Field type, used for grouping fields together
108 T value; ///< Field value
109};
110
111/**
112 * Collection of data fields that have been logged.
113 */
114class FieldCollection final : NonCopyable {
115public:
116 FieldCollection() = default;
117
118 /**
119 * Accept method for the visitor pattern, visits each field in the collection.
120 * @param visitor Reference to the visitor that will visit each field.
121 */
122 void Accept(VisitorInterface& visitor) const;
123
124 /**
125 * Creates a new field and adds it to the field collection.
126 * @param type Type of the field to add.
127 * @param name Name of the field to add.
128 * @param value Value for the field to add.
129 */
130 template <typename T>
131 void AddField(FieldType type, const char* name, T value) {
132 return AddField(std::make_unique<Field<T>>(type, name, std::move(value)));
133 }
134
135 /**
136 * Adds a new field to the field collection.
137 * @param field Field to add to the field collection.
138 */
139 void AddField(std::unique_ptr<FieldInterface> field);
140
141private:
142 std::map<std::string, std::unique_ptr<FieldInterface>> fields;
143};
144
145/**
146 * Telemetry fields visitor interface class. A backend to log to a web service should implement
147 * this interface.
148 */
149struct VisitorInterface : NonCopyable {
150 virtual ~VisitorInterface() = default;
151
152 virtual void Visit(const Field<bool>& field) = 0;
153 virtual void Visit(const Field<double>& field) = 0;
154 virtual void Visit(const Field<float>& field) = 0;
155 virtual void Visit(const Field<u8>& field) = 0;
156 virtual void Visit(const Field<u16>& field) = 0;
157 virtual void Visit(const Field<u32>& field) = 0;
158 virtual void Visit(const Field<u64>& field) = 0;
159 virtual void Visit(const Field<s8>& field) = 0;
160 virtual void Visit(const Field<s16>& field) = 0;
161 virtual void Visit(const Field<s32>& field) = 0;
162 virtual void Visit(const Field<s64>& field) = 0;
163 virtual void Visit(const Field<std::string>& field) = 0;
164 virtual void Visit(const Field<const char*>& field) = 0;
165 virtual void Visit(const Field<std::chrono::microseconds>& field) = 0;
166
167 /// Completion method, called once all fields have been visited
168 virtual void Complete() = 0;
169};
170
171/**
172 * Empty implementation of VisitorInterface that drops all fields. Used when a functional
173 * backend implementation is not available.
174 */
175struct NullVisitor : public VisitorInterface {
176 ~NullVisitor() = default;
177
178 void Visit(const Field<bool>& /*field*/) override {}
179 void Visit(const Field<double>& /*field*/) override {}
180 void Visit(const Field<float>& /*field*/) override {}
181 void Visit(const Field<u8>& /*field*/) override {}
182 void Visit(const Field<u16>& /*field*/) override {}
183 void Visit(const Field<u32>& /*field*/) override {}
184 void Visit(const Field<u64>& /*field*/) override {}
185 void Visit(const Field<s8>& /*field*/) override {}
186 void Visit(const Field<s16>& /*field*/) override {}
187 void Visit(const Field<s32>& /*field*/) override {}
188 void Visit(const Field<s64>& /*field*/) override {}
189 void Visit(const Field<std::string>& /*field*/) override {}
190 void Visit(const Field<const char*>& /*field*/) override {}
191 void Visit(const Field<std::chrono::microseconds>& /*field*/) override {}
192
193 void Complete() override {}
194};
195
196} // namespace Telemetry
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index c733e5d21..57504529f 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -174,6 +174,7 @@ set(SRCS
174 memory.cpp 174 memory.cpp
175 perf_stats.cpp 175 perf_stats.cpp
176 settings.cpp 176 settings.cpp
177 telemetry_session.cpp
177 ) 178 )
178 179
179set(HEADERS 180set(HEADERS
@@ -366,6 +367,7 @@ set(HEADERS
366 mmio.h 367 mmio.h
367 perf_stats.h 368 perf_stats.h
368 settings.h 369 settings.h
370 telemetry_session.h
369 ) 371 )
370 372
371include_directories(../../externals/dynarmic/include) 373include_directories(../../externals/dynarmic/include)
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 881f1e93c..450e7566d 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -132,6 +132,8 @@ System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
132 cpu_core = std::make_unique<ARM_DynCom>(USER32MODE); 132 cpu_core = std::make_unique<ARM_DynCom>(USER32MODE);
133 } 133 }
134 134
135 telemetry_session = std::make_unique<Core::TelemetrySession>();
136
135 CoreTiming::Init(); 137 CoreTiming::Init();
136 HW::Init(); 138 HW::Init();
137 Kernel::Init(system_mode); 139 Kernel::Init(system_mode);
@@ -162,6 +164,7 @@ void System::Shutdown() {
162 CoreTiming::Shutdown(); 164 CoreTiming::Shutdown();
163 cpu_core = nullptr; 165 cpu_core = nullptr;
164 app_loader = nullptr; 166 app_loader = nullptr;
167 telemetry_session = nullptr;
165 168
166 LOG_DEBUG(Core, "Shutdown OK"); 169 LOG_DEBUG(Core, "Shutdown OK");
167} 170}
diff --git a/src/core/core.h b/src/core/core.h
index 6c9c936b5..6af772831 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -9,6 +9,7 @@
9#include "common/common_types.h" 9#include "common/common_types.h"
10#include "core/memory.h" 10#include "core/memory.h"
11#include "core/perf_stats.h" 11#include "core/perf_stats.h"
12#include "core/telemetry_session.h"
12 13
13class EmuWindow; 14class EmuWindow;
14class ARM_Interface; 15class ARM_Interface;
@@ -80,6 +81,14 @@ public:
80 return cpu_core != nullptr; 81 return cpu_core != nullptr;
81 } 82 }
82 83
84 /**
85 * Returns a reference to the telemetry session for this emulation session.
86 * @returns Reference to the telemetry session.
87 */
88 Core::TelemetrySession& TelemetrySession() const {
89 return *telemetry_session;
90 }
91
83 /// Prepare the core emulation for a reschedule 92 /// Prepare the core emulation for a reschedule
84 void PrepareReschedule(); 93 void PrepareReschedule();
85 94
@@ -117,6 +126,9 @@ private:
117 /// When true, signals that a reschedule should happen 126 /// When true, signals that a reschedule should happen
118 bool reschedule_pending{}; 127 bool reschedule_pending{};
119 128
129 /// Telemetry session for this emulation session
130 std::unique_ptr<Core::TelemetrySession> telemetry_session;
131
120 static System s_instance; 132 static System s_instance;
121}; 133};
122 134
@@ -124,4 +136,8 @@ inline ARM_Interface& CPU() {
124 return System::GetInstance().CPU(); 136 return System::GetInstance().CPU();
125} 137}
126 138
139inline TelemetrySession& Telemetry() {
140 return System::GetInstance().TelemetrySession();
141}
142
127} // namespace Core 143} // namespace Core
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp
index 1a4e3efa8..beeb13ffa 100644
--- a/src/core/loader/ncch.cpp
+++ b/src/core/loader/ncch.cpp
@@ -9,6 +9,7 @@
9#include "common/logging/log.h" 9#include "common/logging/log.h"
10#include "common/string_util.h" 10#include "common/string_util.h"
11#include "common/swap.h" 11#include "common/swap.h"
12#include "core/core.h"
12#include "core/file_sys/archive_selfncch.h" 13#include "core/file_sys/archive_selfncch.h"
13#include "core/hle/kernel/process.h" 14#include "core/hle/kernel/process.h"
14#include "core/hle/kernel/resource_limit.h" 15#include "core/hle/kernel/resource_limit.h"
@@ -339,6 +340,8 @@ ResultStatus AppLoader_NCCH::Load() {
339 340
340 LOG_INFO(Loader, "Program ID: %016" PRIX64, ncch_header.program_id); 341 LOG_INFO(Loader, "Program ID: %016" PRIX64, ncch_header.program_id);
341 342
343 Core::Telemetry().AddField(Telemetry::FieldType::Session, "ProgramId", ncch_header.program_id);
344
342 is_loaded = true; // Set state to loaded 345 is_loaded = true; // Set state to loaded
343 346
344 result = LoadExec(); // Load the executable into memory for booting 347 result = LoadExec(); // Load the executable into memory for booting
diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp
new file mode 100644
index 000000000..ddc8b262e
--- /dev/null
+++ b/src/core/telemetry_session.cpp
@@ -0,0 +1,42 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstring>
6
7#include "common/scm_rev.h"
8#include "core/telemetry_session.h"
9
10namespace Core {
11
12TelemetrySession::TelemetrySession() {
13 // TODO(bunnei): Replace with a backend that logs to our web service
14 backend = std::make_unique<Telemetry::NullVisitor>();
15
16 // Log one-time session start information
17 const auto duration{std::chrono::steady_clock::now().time_since_epoch()};
18 const auto start_time{std::chrono::duration_cast<std::chrono::microseconds>(duration).count()};
19 AddField(Telemetry::FieldType::Session, "StartTime", start_time);
20
21 // Log one-time application information
22 const bool is_git_dirty{std::strstr(Common::g_scm_desc, "dirty") != nullptr};
23 AddField(Telemetry::FieldType::App, "GitIsDirty", is_git_dirty);
24 AddField(Telemetry::FieldType::App, "GitBranch", Common::g_scm_branch);
25 AddField(Telemetry::FieldType::App, "GitRevision", Common::g_scm_rev);
26}
27
28TelemetrySession::~TelemetrySession() {
29 // Log one-time session end information
30 const auto duration{std::chrono::steady_clock::now().time_since_epoch()};
31 const auto end_time{std::chrono::duration_cast<std::chrono::microseconds>(duration).count()};
32 AddField(Telemetry::FieldType::Session, "EndTime", end_time);
33
34 // Complete the session, submitting to web service if necessary
35 // This is just a placeholder to wrap up the session once the core completes and this is
36 // destroyed. This will be moved elsewhere once we are actually doing real I/O with the service.
37 field_collection.Accept(*backend);
38 backend->Complete();
39 backend = nullptr;
40}
41
42} // namespace Core
diff --git a/src/core/telemetry_session.h b/src/core/telemetry_session.h
new file mode 100644
index 000000000..cf53835c3
--- /dev/null
+++ b/src/core/telemetry_session.h
@@ -0,0 +1,38 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8#include "common/telemetry.h"
9
10namespace Core {
11
12/**
13 * Instruments telemetry for this emulation session. Creates a new set of telemetry fields on each
14 * session, logging any one-time fields. Interfaces with the telemetry backend used for submitting
15 * data to the web service. Submits session data on close.
16 */
17class TelemetrySession : NonCopyable {
18public:
19 TelemetrySession();
20 ~TelemetrySession();
21
22 /**
23 * Wrapper around the Telemetry::FieldCollection::AddField method.
24 * @param type Type of the field to add.
25 * @param name Name of the field to add.
26 * @param value Value for the field to add.
27 */
28 template <typename T>
29 void AddField(Telemetry::FieldType type, const char* name, T value) {
30 field_collection.AddField(type, name, std::move(value));
31 }
32
33private:
34 Telemetry::FieldCollection field_collection; ///< Tracks all added fields for the session
35 std::unique_ptr<Telemetry::VisitorInterface> backend; ///< Backend interface that logs fields
36};
37
38} // namespace Core