Skip to content

Commit

Permalink
encore-service: Implement renice and ionice utility on here natively
Browse files Browse the repository at this point in the history
Signed-off-by: Rem01Gaming <[email protected]>
  • Loading branch information
Rem01Gaming committed Oct 11, 2024
1 parent e5ce17e commit d2e8c7a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
1 change: 0 additions & 1 deletion module/customize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ extract "$ZIPFILE" 'system/bin/encore-perfcommon' $MODPATH
extract "$ZIPFILE" 'system/bin/encore-normal' $MODPATH
extract "$ZIPFILE" 'system/bin/encore-powersave' $MODPATH
extract "$ZIPFILE" 'system/bin/encore-performance' $MODPATH
extract "$ZIPFILE" 'system/bin/encore-setpriority' $MODPATH

# Extract executables
if [ $ARCH = "arm64" ]; then
Expand Down
42 changes: 29 additions & 13 deletions src/jni/encore-service.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>

Expand All @@ -27,12 +29,12 @@ char *timern(void) {
struct tm *tm = localtime(&t);
char *s = malloc(64 * sizeof(char));
if (s == NULL) {
printf("error: memory allocation failed in timern()\n");
perror("error: memory allocation failed in timern()\n");
return NULL;
}
size_t ret = strftime(s, 64, "%c", tm);
if (ret == 0) {
printf("error: strftime failed in timern()\n");
perror("error: strftime failed in timern()\n");
free(s);
return NULL;
}
Expand All @@ -47,15 +49,15 @@ char *execute_command(const char *command) {

fp = popen(command, "r");
if (fp == NULL) {
printf("error: can't exec command %s\n", command);
perror("error: can't exec command %s\n", command);
return NULL;
}

while (fgets(buffer, sizeof(buffer), fp) != NULL) {
size_t buffer_length = strlen(buffer);
char *new_result = realloc(result, result_length + buffer_length + 1);
if (new_result == NULL) {
printf("error: memory allocation error.\n");
perror("error: memory allocation error.\n");
free(result);
pclose(fp);
return NULL;
Expand All @@ -70,7 +72,7 @@ char *execute_command(const char *command) {
}

if (pclose(fp) == -1) {
printf("error: closing command stream.\n");
perror("error: closing command stream.\n");
}

return result;
Expand All @@ -85,10 +87,10 @@ void append2file(const char *file_path, const char *content) {
fclose(file);
chmod(file_path, 0444);
} else {
printf("error: can't open %s\n", file_path);
perror("error: can't open %s\n", file_path);
}
} else {
printf("error: %s does not exist or is not accessible\n", file_path);
perror("error: %s does not exist or is not accessible\n", file_path);
}
}

Expand All @@ -103,8 +105,22 @@ void log_error(const char *message) {
}

void setPriorities(const char *pid) {
snprintf(command, sizeof(command), "su -c encore-setpriority %s", pid);
system(command);
int prio = -20; // Niceness
int io_class = 1; // I/O class
int io_prio = 0; // I/O priority

pid_t process_id = atoi(pid);

if (setpriority(PRIO_PROCESS, process_id, prio) == -1) {
perror("Failed to set nice priority");
log_error("Failed to set nice priority");
}

if (syscall(SYS_ioprio_set, 1, process_id, (io_class << 13) | io_prio) ==
-1) {
perror("Failed to set IO priority");
log_error("Failed to set IO priority");
}
}

void perf_common(void) { system("su -c encore-perfcommon"); }
Expand Down Expand Up @@ -153,7 +169,7 @@ int main(void) {
"su -c dumpsys power | grep -Eo "
"'mWakefulness=Awake|mWakefulness=Asleep' | awk -F'=' '{print $2}'");

/* In rare cases, some device fails to give mWakefulness info. */
/* In some cases, some device fails to give mWakefulness info. */
if (screenstate == NULL) {
screenstate = execute_command(
"su -c dumpsys window displays | grep -Eo 'mAwake=true|mAwake=false' "
Expand All @@ -162,7 +178,7 @@ int main(void) {

// Handle null screenstate
if (screenstate == NULL) {
printf("error: screenstate is null!\n");
perror("error: screenstate is null!\n");
log_error("screenstate is null!");
} else if (gamestart && (strcmp(trim_newline(screenstate), "Awake") == 0 ||
strcmp(trim_newline(screenstate), "true") == 0)) {
Expand All @@ -183,7 +199,7 @@ int main(void) {
if (pid != NULL) {
setPriorities(trim_newline(pid));
} else {
printf("error: Game PID is null!\n");
perror("error: Game PID is null!\n");
log_error("Game PID is null!");
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/scripts/encore-setpriority

This file was deleted.

0 comments on commit d2e8c7a

Please sign in to comment.