Skip to content

Commit 63f4be3

Browse files
committed
Implement thread affinity Manager class to protect (for a few seconds) against aggressive scheduler thread hopping which tends to wreck cache locality. Remove --debug cli option, as we're now fetching and statically linking SDL3 so it's no longer required (or maintained).
1 parent 05934ea commit 63f4be3

File tree

12 files changed

+122
-78
lines changed

12 files changed

+122
-78
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ if(MSVC)
124124

125125
target_link_options(
126126
"${PROJECT_NAME}" PRIVATE
127-
$<$<CONFIG:Release>: /LTCG /SUBSYSTEM:WINDOWS /INCREMENTAL:NO>
127+
$<$<CONFIG:Release>: /LTCG /SUBSYSTEM:WINDOWS /INCREMENTAL:NO /OPT:ICF>
128128
$<$<CONFIG:Debug>: /SUBSYSTEM:CONSOLE /INCREMENTAL>
129129
)
130130

include/frontend/CubeChip.cpp

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ SDL_AppResult SDL_AppInit(void **Host, int argc, char *argv[]) {
6464
cxxopts::value<bool>()->default_value("false"));
6565

6666
options.add_options("General")
67-
("debug", "Print application SDL debuf info.")
6867
("version", "Print application version info.")
6968
("help", "List application options.");
7069

@@ -73,25 +72,6 @@ SDL_AppResult SDL_AppInit(void **Host, int argc, char *argv[]) {
7372

7473
auto result{ options.parse(argc, argv) };
7574

76-
if (result.count("debug")) {
77-
Console::Attach();
78-
fmt::println("SDL3 dev version tested: 23/03/25 (dd/mm/yy)");
79-
80-
const auto compiled{ SDL_VERSION }; /* hardcoded number from SDL headers */
81-
const auto linked{ SDL_GetVersion() }; /* reported by linked SDL library */
82-
83-
fmt::println("Compiled with SDL version: {}.{}.{}",
84-
SDL_VERSIONNUM_MAJOR(compiled),
85-
SDL_VERSIONNUM_MINOR(compiled),
86-
SDL_VERSIONNUM_MICRO(compiled));
87-
fmt::println("Linked with SDL version: {}.{}.{}",
88-
SDL_VERSIONNUM_MAJOR(linked),
89-
SDL_VERSIONNUM_MINOR(linked),
90-
SDL_VERSIONNUM_MICRO(linked));
91-
92-
return SDL_APP_SUCCESS;
93-
}
94-
9575
if (result.count("version")) {
9676
Console::Attach();
9777
fmt::println("{} compiled on: {} ({})",
@@ -110,7 +90,7 @@ SDL_AppResult SDL_AppInit(void **Host, int argc, char *argv[]) {
11090
if (!FrontendHost::initApplication(
11191
result.count("homedir") ? result["homedir"].as<Str>() : ""s,
11292
result.count("config") ? result["config"] .as<Str>() : ""s,
113-
result.count("portable") ? true : false, "", AppName
93+
result.count("portable") ? true : false
11494
)) { return SDL_APP_FAILURE; }
11595

11696
*Host = FrontendHost::initialize(result.count("program") ? result["program"].as<Str>() : ""s);
@@ -123,7 +103,7 @@ SDL_AppResult SDL_AppInit(void **Host, int argc, char *argv[]) {
123103
/*==================================================================*/
124104

125105
SDL_AppResult SDL_AppIterate(void *pHost) {
126-
auto Host{ static_cast<FrontendHost*>(pHost) };
106+
auto* Host{ static_cast<FrontendHost*>(pHost) };
127107

128108
Host->processFrame();
129109

@@ -133,7 +113,7 @@ SDL_AppResult SDL_AppIterate(void *pHost) {
133113
/*==================================================================*/
134114

135115
SDL_AppResult SDL_AppEvent(void *pHost, SDL_Event *event) {
136-
auto Host{ static_cast<FrontendHost*>(pHost) };
116+
auto* Host{ static_cast<FrontendHost*>(pHost) };
137117

138118
return static_cast<SDL_AppResult>(Host->processEvents(event));
139119
}
@@ -142,7 +122,7 @@ SDL_AppResult SDL_AppEvent(void *pHost, SDL_Event *event) {
142122

143123
void SDL_AppQuit(void *pHost, SDL_AppResult) {
144124
if (pHost) {
145-
auto Host{ static_cast<FrontendHost*>(pHost) };
125+
auto* Host{ static_cast<FrontendHost*>(pHost) };
146126
Host->quitApplication();
147127
}
148128
}

include/frontend/FrontendHost.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,9 @@ void FrontendHost::quitApplication() noexcept {
111111
);
112112
}
113113

114-
bool FrontendHost::initApplication(
115-
StrV overrideHome, StrV configName,
116-
bool forcePortable, StrV org, StrV app
117-
) noexcept {
118-
HDM = HomeDirManager::initialize(overrideHome, configName, forcePortable, org, app);
114+
bool FrontendHost::initApplication(StrV overrideHome, StrV configName, bool forcePortable) noexcept {
115+
HDM = HomeDirManager::initialize(
116+
overrideHome, configName, forcePortable, OrgName, AppName);
119117
if (!HDM) { return false; }
120118

121119
GlobalAudioBase::Settings GAB_settings;

include/frontend/FrontendHost.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ constexpr static inline auto AppVer{ ProjectVersion() };
3636
#else
3737
constexpr auto* AppName{ PROJECT_NAME };
3838
#endif
39+
constexpr auto* OrgName{ "" };
3940

4041
/*==================================================================*/
4142

@@ -83,10 +84,7 @@ class FrontendHost final {
8384
return &self;
8485
}
8586

86-
static bool initApplication(
87-
StrV overrideHome, StrV configName,
88-
bool forcePortable, StrV org, StrV app
89-
) noexcept;
87+
static bool initApplication(StrV overrideHome, StrV configName, bool forcePortable) noexcept;
9088

9189
s32 processEvents(void* event) noexcept;
9290

include/systems/BYTEPUSHER/BytePusher_CoreInterface.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ BytePusher_CoreInterface::BytePusher_CoreInterface() noexcept {
2525
/*==================================================================*/
2626

2727
void BytePusher_CoreInterface::mainSystemLoop() {
28-
if (Pacer->checkTime()) {
29-
if (!isSystemRunning())
30-
[[unlikely]] { return; }
31-
32-
instructionLoop();
33-
renderAudioData();
34-
renderVideoData();
35-
pushOverlayData();
36-
}
28+
if (!isSystemRunning())
29+
[[unlikely]] { return; }
30+
31+
instructionLoop();
32+
renderAudioData();
33+
renderVideoData();
34+
pushOverlayData();
3735
}
3836

3937
void BytePusher_CoreInterface::loadPresetBinds() {

include/systems/CHIP8/Chip8_CoreInterface.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,19 @@ void Chip8_CoreInterface::performProgJump(u32 next) noexcept {
170170
/*==================================================================*/
171171

172172
void Chip8_CoreInterface::mainSystemLoop() {
173-
if (Pacer->checkTime()) {
174-
if (!isSystemRunning())
175-
[[unlikely]] { return; }
173+
if (!isSystemRunning())
174+
[[unlikely]] { return; }
176175

177-
updateKeyStates();
176+
updateKeyStates();
178177

179-
handleTimerTick();
180-
handlePreFrameInterrupt();
181-
instructionLoop();
182-
handleEndFrameInterrupt();
178+
handleTimerTick();
179+
handlePreFrameInterrupt();
180+
instructionLoop();
181+
handleEndFrameInterrupt();
183182

184-
renderAudioData();
185-
renderVideoData();
186-
pushOverlayData();
187-
}
183+
renderAudioData();
184+
renderVideoData();
185+
pushOverlayData();
188186
}
189187

190188
Str* Chip8_CoreInterface::makeOverlayData() {

include/systems/GAMEBOY/GameBoy_CoreInterface.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ GameBoy_CoreInterface::GameBoy_CoreInterface() noexcept {
2626
/*==================================================================*/
2727

2828
void GameBoy_CoreInterface::mainSystemLoop() {
29-
if (Pacer->checkTime()) {
30-
if (!isSystemRunning())
31-
[[unlikely]] { return; }
32-
33-
updateKeyStates();
34-
instructionLoop();
35-
renderAudioData();
36-
renderVideoData();
37-
pushOverlayData();
38-
}
29+
if (!isSystemRunning())
30+
[[unlikely]] { return; }
31+
32+
updateKeyStates();
33+
instructionLoop();
34+
renderAudioData();
35+
renderVideoData();
36+
pushOverlayData();
3937
}
4038

4139
void GameBoy_CoreInterface::loadPresetBinds() {

include/systems/SystemInterface.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ void SystemInterface::stopWorker() noexcept {
2424
}
2525

2626
void SystemInterface::threadEntry(StopToken token) {
27-
thread_affinity::set_affinity(~0b11ull);
28-
SDL_SetCurrentThreadPriority(SDL_THREAD_PRIORITY_HIGH);
27+
SDL_SetCurrentThreadPriority(SDL_THREAD_PRIORITY_TIME_CRITICAL);
28+
static thread_local thread_affinity::Manager thread{ 15, 0b11ull };
2929

30-
while (!token.stop_requested())
31-
[[likely]] { mainSystemLoop(); }
30+
while (!token.stop_requested()) [[likely]] {
31+
thread.refresh_affinity();
32+
if (Pacer->checkTime()) { mainSystemLoop(); }
33+
}
3234
}
3335

3436
SystemInterface::SystemInterface() noexcept

include/utilities/Millis.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010

1111
/*==================================================================*/
1212

13-
static const auto sInitialTimestamp \
13+
static const auto sInitialTimestamp
1414
{ std::chrono::steady_clock::now() };
1515

1616
/*==================================================================*/
1717

18-
auto Millis::get() const noexcept -> return_type {
19-
return std::chrono::duration_cast<std::chrono::milliseconds> \
18+
long long Millis::now() noexcept {
19+
return std::chrono::duration_cast<std::chrono::milliseconds>
2020
(std::chrono::steady_clock::now() - sInitialTimestamp).count();
2121
}
2222

23-
auto Millis::getSince(return_type past_millis) const noexcept -> return_type {
24-
return get() - past_millis;
23+
long long Millis::since(long long past_millis) noexcept {
24+
return now() - past_millis;
2525
}

include/utilities/Millis.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
/*==================================================================*/
1010

1111
struct Millis final {
12-
using return_type = long long;
13-
1412
Millis() = delete;
1513

16-
auto get() const noexcept -> return_type;
17-
auto getSince(return_type past_millis) const noexcept -> return_type;
14+
static long long now() noexcept;
15+
static long long since(long long past_millis) noexcept;
1816
};

0 commit comments

Comments
 (0)