Feat[affinity]: added a way to force the MC rendering thread to run only on the highest-clocked core

This commit is contained in:
artdeell 2023-06-19 18:55:52 +03:00
parent 5c4f214a1d
commit 57c79d8c6b
7 changed files with 68 additions and 0 deletions

View File

@ -58,6 +58,7 @@ public class LauncherPreferences {
public static boolean PREF_BUTTON_ALL_CAPS = true; public static boolean PREF_BUTTON_ALL_CAPS = true;
public static boolean PREF_DUMP_SHADERS = false; public static boolean PREF_DUMP_SHADERS = false;
public static float PREF_DEADZONE_SCALE = 1f; public static float PREF_DEADZONE_SCALE = 1f;
public static boolean PREF_BIG_CORE_AFFINITY = false;
public static void loadPreferences(Context ctx) { public static void loadPreferences(Context ctx) {
@ -98,6 +99,7 @@ public class LauncherPreferences {
PREF_BUTTON_ALL_CAPS = DEFAULT_PREF.getBoolean("buttonAllCaps", true); PREF_BUTTON_ALL_CAPS = DEFAULT_PREF.getBoolean("buttonAllCaps", true);
PREF_DUMP_SHADERS = DEFAULT_PREF.getBoolean("dump_shaders", false); PREF_DUMP_SHADERS = DEFAULT_PREF.getBoolean("dump_shaders", false);
PREF_DEADZONE_SCALE = DEFAULT_PREF.getInt("gamepad_deadzone_scale", 100)/100f; PREF_DEADZONE_SCALE = DEFAULT_PREF.getInt("gamepad_deadzone_scale", 100)/100f;
PREF_BIG_CORE_AFFINITY = DEFAULT_PREF.getBoolean("bigCoreAffinity", false);
String argLwjglLibname = "-Dorg.lwjgl.opengl.libname="; String argLwjglLibname = "-Dorg.lwjgl.opengl.libname=";
for (String arg : JREUtils.parseJavaArguments(PREF_CUSTOM_JAVA_ARGS)) { for (String arg : JREUtils.parseJavaArguments(PREF_CUSTOM_JAVA_ARGS)) {

View File

@ -218,6 +218,7 @@ public class JREUtils {
envMap.put("POJAVEXEC_EGL","libEGL_angle.so"); // Use ANGLE EGL envMap.put("POJAVEXEC_EGL","libEGL_angle.so"); // Use ANGLE EGL
} }
} }
if(LauncherPreferences.PREF_BIG_CORE_AFFINITY) envMap.put("POJAV_BIG_CORE_AFFINITY", "1");
envMap.put("AWTSTUB_WIDTH", Integer.toString(CallbackBridge.windowWidth > 0 ? CallbackBridge.windowWidth : CallbackBridge.physicalWidth)); envMap.put("AWTSTUB_WIDTH", Integer.toString(CallbackBridge.windowWidth > 0 ? CallbackBridge.windowWidth : CallbackBridge.physicalWidth));
envMap.put("AWTSTUB_HEIGHT", Integer.toString(CallbackBridge.windowHeight > 0 ? CallbackBridge.windowHeight : CallbackBridge.physicalHeight)); envMap.put("AWTSTUB_HEIGHT", Integer.toString(CallbackBridge.windowHeight > 0 ? CallbackBridge.windowHeight : CallbackBridge.physicalHeight));

View File

@ -43,6 +43,7 @@ LOCAL_MODULE := pojavexec
# LOCAL_CFLAGS += -DDEBUG # LOCAL_CFLAGS += -DDEBUG
# -DGLES_TEST # -DGLES_TEST
LOCAL_SRC_FILES := \ LOCAL_SRC_FILES := \
bigcoreaffinity.c \
egl_bridge.c \ egl_bridge.c \
ctxbridges/gl_bridge.c \ ctxbridges/gl_bridge.c \
ctxbridges/egl_loader.c \ ctxbridges/egl_loader.c \

View File

@ -0,0 +1,55 @@
//
// Created by maks on 19.06.2023.
//
#define _GNU_SOURCE // we are GNU GPLv3
#include <linux/limits.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sched.h>
#include <string.h>
#define FREQ_MAX 256
void bigcore_format_cpu_path(char* buffer, unsigned int cpu_core) {
snprintf(buffer, PATH_MAX, "/sys/devices/system/cpu/cpu%i/cpufreq/cpuinfo_max_freq", cpu_core);
}
void bigcore_set_affinity() {
char path_buffer[PATH_MAX];
char freq_buffer[FREQ_MAX];
char* discard;
unsigned long core_freq;
unsigned long max_freq = 0;
unsigned int corecnt = 0;
unsigned int big_core_id = 0;
while(1) {
bigcore_format_cpu_path(path_buffer, corecnt);
int corefreqfd = open(path_buffer, O_RDONLY);
if(corefreqfd != -1) {
ssize_t read_count = read(corefreqfd, freq_buffer, FREQ_MAX);
close(corefreqfd);
freq_buffer[read_count] = 0;
core_freq = strtoul(freq_buffer, &discard, 10);
if(core_freq >= max_freq) {
max_freq = core_freq;
big_core_id = corecnt;
}
}else{
break;
}
corecnt++;
}
printf("bigcore: big CPU number is %u, frequency %lu Hz\n", big_core_id, max_freq);
cpu_set_t bigcore_affinity_set;
CPU_ZERO(&bigcore_affinity_set);
CPU_SET_S(big_core_id, CPU_SETSIZE, &bigcore_affinity_set);
int result = sched_setaffinity(0, CPU_SETSIZE, &bigcore_affinity_set);
if(result != 0) {
printf("bigcore: setting affinity failed: %s\n", strerror(result));
}else{
printf("bigcore: forced current thread onto big core\n");
}
}

View File

@ -598,6 +598,7 @@ struct PotatoBridge potatoBridge;
#include "ctxbridges/osmesa_loader.h" #include "ctxbridges/osmesa_loader.h"
int (*vtest_main_p) (int argc, char** argv); int (*vtest_main_p) (int argc, char** argv);
void (*vtest_swap_buffers_p) (void); void (*vtest_swap_buffers_p) (void);
void bigcore_set_affinity();
#define RENDERER_GL4ES 1 #define RENDERER_GL4ES 1
#define RENDERER_VK_ZINK 2 #define RENDERER_VK_ZINK 2
@ -926,6 +927,7 @@ void* egl_make_current(void* window) {
bool locked = false; bool locked = false;
void pojavMakeCurrent(void* window) { void pojavMakeCurrent(void* window) {
if(getenv("POJAV_BIG_CORE_AFFINITY") != NULL) bigcore_set_affinity();
//if(OSMesaGetCurrentContext_p() != NULL) { //if(OSMesaGetCurrentContext_p() != NULL) {
// printf("OSMDroid: skipped context reset\n"); // printf("OSMDroid: skipped context reset\n");
// return JNI_TRUE; // return JNI_TRUE;

View File

@ -370,4 +370,6 @@
<string name="preference_wipe_controller_description">Allow you to remap the controller buttons</string> <string name="preference_wipe_controller_description">Allow you to remap the controller buttons</string>
<string name="preference_deadzone_scale_title">Joystick deadzone scale</string> <string name="preference_deadzone_scale_title">Joystick deadzone scale</string>
<string name="preference_deadzone_scale_description">Increase it if the joystick drifts</string> <string name="preference_deadzone_scale_description">Increase it if the joystick drifts</string>
<string name="preference_force_big_core_title">Force renderer to run on the big core</string>
<string name="preference_force_big_core_desc">Forces the Minecraft render thread to run on the core with the highest max frequency</string>
</resources> </resources>

View File

@ -9,6 +9,11 @@
android:title="@string/preference_shader_dump_title" android:title="@string/preference_shader_dump_title"
android:summary="@string/preference_shader_dump_description" android:summary="@string/preference_shader_dump_description"
/> />
<SwitchPreference
android:defaultValue="false"
android:key="bigCoreAffinity"
android:title="@string/preference_force_big_core_title"
android:summary="@string/preference_force_big_core_desc" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>