13#include <sys/resource.h>
14#include <sys/syscall.h>
24SetSchedulerPolicy(pid_t pid,
int policy,
int static_priority,
int dynamic_priority) NUMAPP_NOEXCEPT {
26 struct sched_param param = {0};
30 param.sched_priority = static_priority;
35 if (sched_setscheduler(pid, policy, ¶m) != 0) {
36 return std::make_error_code(
static_cast<std::errc
>(errno));
43 if (setpriority(PRIO_PROCESS, pid, dynamic_priority) != 0) {
44 return std::make_error_code(
static_cast<std::errc
>(errno));
56 int* dynamic_priority) NUMAPP_NOEXCEPT {
57 assert(policy !=
nullptr);
58 assert(static_priority !=
nullptr);
59 assert(dynamic_priority !=
nullptr);
60 if (
int ret = sched_getscheduler(pid); ret == -1) {
61 return std::make_error_code(
static_cast<std::errc
>(errno));
68 struct sched_param param = {0};
69 if (sched_getparam(pid, ¶m) != 0) {
70 return std::make_error_code(
static_cast<std::errc
>(errno));
72 *static_priority = param.sched_priority;
78 int prio = getpriority(PRIO_PROCESS, pid);
81 return std::make_error_code(
static_cast<std::errc
>(errno));
83 *dynamic_priority = prio;
88 return std::make_error_code(std::errc::not_supported);
93std::error_code
GetCpuAffinity(pid_t ttid, Cpumask& mask) NUMAPP_NOEXCEPT {
94 if (numa_sched_getaffinity(ttid, mask.GetNative()) < 0) {
95 return std::make_error_code(
static_cast<std::errc
>(errno));
100std::error_code
SetCpuAffinity(pid_t ttid, Cpumask
const& mask) NUMAPP_NOEXCEPT {
101 if (numa_sched_setaffinity(ttid,
const_cast<Cpumask&
>(mask).GetNative()) < 0) {
102 return std::make_error_code(
static_cast<std::errc
>(errno));
108 if (numa_node_to_cpus(node, mask.GetNative()) < 0) {
109 return std::make_error_code(
static_cast<std::errc
>(errno));
114std::error_code
SetMemPolicy(
int mode, Nodemask
const& mask) NUMAPP_NOEXCEPT {
115 if (
auto err = set_mempolicy(mode, mask.GetNative()->maskp, mask.GetNative()->size + 1);
117 return std::make_error_code(
static_cast<std::errc
>(errno));
124SetMemPolicy(
void* addr, std::size_t len,
int mode, Nodemask
const& mask,
unsigned flags)
127 mbind(addr, len, mode, mask.GetNative()->maskp, mask.GetNative()->size + 1, flags);
129 return std::make_error_code(
static_cast<std::errc
>(errno));
136GetMemPolicy(
int& mode, Nodemask& mask,
void* addr,
unsigned flags) NUMAPP_NOEXCEPT {
138 get_mempolicy(&mode, mask.GetNative()->maskp, mask.GetNative()->size + 1, addr, flags);
140 return std::make_error_code(
static_cast<std::errc
>(errno));
Contains low-level functions.
Defines lowlevel functions that should not be used directly.
std::error_code GetCpuAffinity(pid_t ttid, Cpumask &mask) noexcept
Get CPU affinity.
std::error_code SetMemPolicy(int mode, Nodemask const &mask) noexcept
Set active memory policy for calling thread.
std::error_code SetSchedulerPolicy(pid_t pid, int policy, int static_priority, int dynamic_priority) noexcept
A low-level, and error prone function to set policy.
std::error_code GetMemPolicy(int &mode, Nodemask &mask, void *addr, unsigned flags) noexcept
Get memory policy for calling thread or an address.
std::error_code NumaNodeToCpumask(int node, Cpumask &mask) noexcept
Convert NUMA node number to corresponding CPU mask.
std::error_code GetSchedulerPolicy(pid_t pid, int *policy, int *static_priority, int *dynamic_priority) noexcept
A low-level, and error prone function to get policy.
std::error_code SetCpuAffinity(pid_t ttid, Cpumask const &mask) noexcept
Set CPU affinity.