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 24, 2024
1 parent b587943 commit 825bcd0
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/contents/ui/Cpu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ Kirigami.ScrollablePage {
}
}

FgSpinBox {
id: schedRuntime

label: i18n("Scheduler Runtime")
from: 0.1
to: 100
value: FGCpuBackend.schedRuntime
decimals: 1
stepSize: 0.1
unit: "ms"
onValueModified: (v) => {
FGCpuBackend.schedRuntime = v;
}
}

}

FormCard.FormHeader {
Expand Down
13 changes: 13 additions & 0 deletions src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <qstring.h>
#include <qtmetamacros.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <cstddef>
#include <string>
#include <thread>
Expand Down Expand Up @@ -40,6 +41,8 @@ Backend::Backend(QObject* parent) : QObject(parent) {
initFreqGovernor();
initPcieAspm();
init_workqueue_affinity_scope();

setSchedRuntime(util::get_sched_runtime(0, 0) * 0.000001);
}

auto Backend::useSchedBatch() const -> bool {
Expand Down Expand Up @@ -140,6 +143,16 @@ void Backend::setTimerSlack(const int& value) {
Q_EMIT timerSlackChanged();
}

auto Backend::schedRuntime() const -> int {
return _schedRuntime;
}

void Backend::setSchedRuntime(const int& value) {
_schedRuntime = value;

Q_EMIT schedRuntimeChanged();
}

auto Backend::cpuIntensiveThreshold() const -> int {
return _cpuIntensiveThreshold;
}
Expand Down
6 changes: 6 additions & 0 deletions src/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Backend : public QObject {

Q_PROPERTY(int autogroupNiceness READ autogroupNiceness WRITE setAutogroupNiceness NOTIFY autogroupNicenessChanged)

Q_PROPERTY(int schedRuntime READ schedRuntime WRITE setSchedRuntime NOTIFY schedRuntimeChanged)

Q_PROPERTY(QString gameAffinity READ gameAffinity WRITE setGameAffinity NOTIFY gameAffinityChanged)

Q_PROPERTY(
Expand All @@ -51,6 +53,7 @@ class Backend : public QObject {
[[nodiscard]] auto pcieAspmPolicy() -> std::string;
[[nodiscard]] auto workqueueAffinityScope() -> std::string;
[[nodiscard]] auto timerSlack() const -> int;
[[nodiscard]] auto schedRuntime() const -> int;
[[nodiscard]] auto cpuIntensiveThreshold() const -> int;
[[nodiscard]] auto niceness() const -> int;
[[nodiscard]] auto autogroupNiceness() const -> int;
Expand All @@ -65,6 +68,7 @@ class Backend : public QObject {
void setPcieAspmPolicy(const std::string& value);
void setWorkqueueAffinityScope(const std::string& value);
void setTimerSlack(const int& value);
void setSchedRuntime(const int& value);
void setCpuIntensiveThreshold(const int& value);
void setNiceness(const int& value);
void setAutogroupNiceness(const int& value);
Expand All @@ -80,6 +84,7 @@ class Backend : public QObject {
void pcieAspmPolicyChanged();
void workqueueAffinityScopeChanged();
void timerSlackChanged();
void schedRuntimeChanged();
void cpuIntensiveThresholdChanged();
void nicenessChanged();
void autogroupNicenessChanged();
Expand All @@ -96,6 +101,7 @@ class Backend : public QObject {
int _pcieAspmPolicy;
int _workqueueAffinityScope;
int _timerSlack;
int _schedRuntime;
int _cpuIntensiveThreshold;
int _niceness;
int _autogroupNiceness;
Expand Down
12 changes: 12 additions & 0 deletions src/fastgame_apply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ auto main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[]) -> int {
auto use_realtime_wineserver = root.get<bool>("cpu.use-realtime-wineserver", false);
int niceness = root.get<int>("cpu.niceness", 0);
int autogroup_niceness = root.get<int>("cpu.autogroup-niceness", 0);
int sched_runtime = root.get<int>("cpu.sched-runtime", util::get_sched_runtime(0, 0));

update_system_setting("/proc/sys/kernel/watchdog", root.get<bool>("cpu.enable-watchdog", true));

Expand Down Expand Up @@ -306,6 +307,13 @@ 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);

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

if (enable_realtime_io_priority) {
ioprio_set_realtime(game_pid, 7);
}
Expand All @@ -329,6 +337,10 @@ 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);
} else {
util::set_sched_runtime(child_pid, sched_runtime, SCHED_OTHER, 0);
}

setpriority(PRIO_PROCESS, child_pid, niceness);
Expand Down
3 changes: 3 additions & 0 deletions src/presets_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ bool Backend::loadPreset(const QString& name) {
cpuBackend.setCpuIntensiveThreshold(
root.get<int>("cpu.workqueue.cpu-intensive-threshold", cpuBackend.cpuIntensiveThreshold()));

cpuBackend.setSchedRuntime(root.get<int>("cpu.sched-runtime", cpuBackend.schedRuntime()));

// memory

memoryBackend.setThpEnabled(
Expand Down Expand Up @@ -463,6 +465,7 @@ bool Backend::save_preset(const QString& name, const std::filesystem::path& outp
root.put("cpu.wineserver-cores", cpuBackend.wineServerAffinity().toStdString());
root.put("cpu.workqueue.affinity-scope", cpuBackend.workqueueAffinityScope());
root.put("cpu.workqueue.cpu-intensive-threshold", cpuBackend.cpuIntensiveThreshold());
root.put("cpu.sched-runtime", cpuBackend.schedRuntime());

// memory

Expand Down
43 changes: 43 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
#include <qdebug.h>
#include <qlogging.h>
#include <sched.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <cstdint>
#include <cstdio>
#include <ext/string_conversions.h>
#include <filesystem>
#include <fstream>
Expand All @@ -16,6 +20,23 @@

namespace util {

struct sched_attr {
uint32_t size; /* Size of this structure */
uint32_t sched_policy; /* Policy (SCHED_*) */
uint64_t sched_flags; /* Flags */
int32_t sched_nice; /* Nice value (SCHED_OTHER, SCHED_BATCH) */
uint32_t sched_priority; /* Static priority (SCHED_FIFO, SCHED_RR) */

/* For SCHED_DEADLINE */
uint64_t sched_runtime;
uint64_t sched_deadline;
uint64_t sched_period;

/* Utilization hints */
uint32_t sched_util_min;
uint32_t sched_util_max;
};

auto prepare_debug_message(const std::string& message, source_location location) -> std::string {
auto file_path = std::filesystem::path{location.file_name()};

Expand Down Expand Up @@ -172,6 +193,28 @@ 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 uint64_t& value, const int& policy_index, 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

if (syscall(SYS_sched_setattr, pid, &attr, flags) < 0) {
perror("sched_setattr");
}
}

auto get_sched_runtime(const int& pid, const uint& flags) -> uint64_t {
sched_attr attr = {};

attr.size = sizeof(sched_attr);

syscall(SYS_sched_getattr, pid, &attr, attr.size, flags);

return attr.sched_runtime;
}

auto card_is_amdgpu(const int& card_index) -> bool {
auto path =
std::filesystem::path("/sys/class/drm/card" + util::to_string(card_index) + "/device/driver/module/drivers/");
Expand Down
6 changes: 6 additions & 0 deletions src/util.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <sys/types.h>
#include <array>
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <source_location>
#include <string>
Expand Down Expand Up @@ -37,6 +39,10 @@ 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 uint64_t& value, const int& policy_index, const uint& flags);

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

auto card_is_amdgpu(const int& card_index) -> bool;

auto get_amdgpu_indices() -> std::vector<int>;
Expand Down

0 comments on commit 825bcd0

Please sign in to comment.