Skip to content

Commit

Permalink
cpu: sched_runtime can be set. This need at least kernel 6.12
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmm committed Nov 30, 2024
1 parent 2f68587 commit 62455b4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/fastgame_apply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,10 @@ auto main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) -> int {
update_system_setting("/proc/" + util::to_string(game_pid) + "/autogroup", autogroup_niceness);

if (use_batch_scheduler) {
util::set_sched_runtime(game_pid, sched_runtime, SCHED_BATCH, 0);
util::set_sched_runtime(game_pid, sched_runtime, SCHED_BATCH, niceness, 0);

} else {
util::set_sched_runtime(game_pid, sched_runtime, SCHED_OTHER, 0);
util::set_sched_runtime(game_pid, sched_runtime, SCHED_OTHER, niceness, 0);
}

if (enable_realtime_io_priority) {
Expand All @@ -338,9 +338,9 @@ auto main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) -> int {
if (use_batch_scheduler) {
util::set_process_scheduler(child_pid, SCHED_BATCH, 0);

util::set_sched_runtime(child_pid, sched_runtime, SCHED_BATCH, 0);
util::set_sched_runtime(child_pid, sched_runtime, SCHED_BATCH, niceness, 0);
} else {
util::set_sched_runtime(child_pid, sched_runtime, SCHED_OTHER, 0);
util::set_sched_runtime(child_pid, sched_runtime, SCHED_OTHER, niceness, 0);
}

setpriority(PRIO_PROCESS, child_pid, niceness);
Expand Down
5 changes: 3 additions & 2 deletions src/fastgame_launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ auto main(int argc, char* argv[]) -> int {
util::apply_cpu_affinity(0, cpu_affinity);

double sched_runtime = root.get<double>("cpu.sched-runtime", util::get_sched_runtime(0, 0));
int niceness = root.get<int>("cpu.niceness", 0);

if (root.get<bool>("cpu.use-batch-scheduler")) {
util::set_process_scheduler(0, SCHED_BATCH, 0);

util::set_sched_runtime(0, sched_runtime, SCHED_BATCH, 0);
util::set_sched_runtime(0, sched_runtime, SCHED_BATCH, niceness, 0);
} else {
util::set_sched_runtime(0, sched_runtime, SCHED_OTHER, 0);
util::set_sched_runtime(0, sched_runtime, SCHED_OTHER, niceness, 0);
}

int timer_slack_ns = root.get<int>("cpu.timer-slack", 50000);
Expand Down
9 changes: 7 additions & 2 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,19 @@ void set_process_scheduler(const int& pid, const int& policy_index, const int& p
sched_setscheduler(pid, policy_index, &policy_params);
}

void set_sched_runtime(const int& pid, const double& value, const int& policy_index, const uint& flags) {
void set_sched_runtime(const int& pid,
const double& value,
const int& policy_index,
const int& priority,
const uint& flags) {
sched_attr attr = {};

attr.size = sizeof(sched_attr);
attr.sched_policy = policy_index;
attr.sched_runtime = value * 1000 * 1000; // ms in nanoseconds
attr.sched_priority = priority;

if (syscall(SYS_sched_setattr, pid, &attr, flags) < 0) {
if (syscall(SYS_sched_setattr, pid, &attr, flags) != 0) {
perror("sched_setattr");
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ void clear_cpu_affinity(const int& pid);

void set_process_scheduler(const int& pid, const int& policy_index, const int& priority);

void set_sched_runtime(const int& pid, const double& value, const int& policy_index, const uint& flags);
void set_sched_runtime(const int& pid,
const double& value,
const int& policy_index,
const int& priority,
const uint& flags);

auto get_sched_runtime(const int& pid, const uint& flags) -> uint64_t;

Expand Down

0 comments on commit 62455b4

Please sign in to comment.