Skip to content

Commit

Permalink
scheds: introduce scx_fair
Browse files Browse the repository at this point in the history
A scheduler that focuses on ensuring fairness among tasks and
performance predictability.

It operates using a vruntime-based policy, where each task is assigned a
"latency" weight. This weight is dynamically adjusted based on how often
a task release the CPU before its full time slice is used. Tasks that
release the CPU early are given a higher latency weight, prioritizing
them over tasks that fully consume their time slice.

The combination of dynamic latency weights and vruntime-based scheduling
ensures responsive and consistent performance, even in overcommitted
systems.

This makes the scheduler particularly well-suited for workloads that
require multimedia or real-time audio processing.

Signed-off-by: Andrea Righi <[email protected]>
  • Loading branch information
arighi committed Nov 1, 2024
1 parent 53e8935 commit 07de4dd
Show file tree
Hide file tree
Showing 17 changed files with 1,387 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = ["rust/scx_stats",
"rust/scx_loader",
"scheds/rust/scx_lavd",
"scheds/rust/scx_bpfland",
"scheds/rust/scx_fair",
"scheds/rust/scx_rustland",
"scheds/rust/scx_rlfifo",
"scheds/rust/scx_rusty",
Expand Down
6 changes: 6 additions & 0 deletions meson-scripts/stress_tests.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ sched_args: -v
stress_cmd: stress-ng -t 14 --aggressive -M -c `nproc` -f `nproc`
timeout_sec: 15

[scx_fair]
sched: scx_fair
sched_args:
stress_cmd: stress-ng -t 14 --aggressive -M -c `nproc` -f `nproc`
timeout_sec: 15

[scx_layered]
sched: scx_layered
sched_args: --run-example -v --stats 1
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ if enable_rust
run_target('fetch', command: [cargo_fetch, cargo], env: cargo_env)

rust_scheds = ['scx_lavd', 'scx_bpfland', 'scx_rustland', 'scx_rlfifo',
'scx_rusty',
'scx_fair', 'scx_rusty',
'scx_layered', 'scx_mitosis']
rust_misc = ['scx_stats', 'scx_stats_derive', 'scx_utils',
'scx_rustland_core',
Expand Down
1 change: 1 addition & 0 deletions scheds/rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ main.rs or \*.bpf.c files.
- [scx_rlfifo](scx_rlfifo/README.md)
- [scx_lavd](scx_lavd/README.md)
- [scx_bpfland](scx_bpfland/README.md)
- [scx_fair](scx_fair/README.md)
26 changes: 26 additions & 0 deletions scheds/rust/scx_fair/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "scx_fair"
version = "1.0.4"
authors = ["Andrea Righi <[email protected]>"]
edition = "2021"
description = "A scheduler designed for multimedia and real-time audio processing workloads. https://github.com/sched-ext/scx/tree/main"
license = "GPL-2.0-only"

[dependencies]
anyhow = "1.0.65"
ctrlc = { version = "3.1", features = ["termination"] }
clap = { version = "4.1", features = ["derive", "env", "unicode", "wrap_help"] }
crossbeam = "0.8.4"
libbpf-rs = "0.24.1"
log = "0.4.17"
scx_stats = { path = "../../../rust/scx_stats", version = "1.0.4" }
scx_stats_derive = { path = "../../../rust/scx_stats/scx_stats_derive", version = "1.0.4" }
scx_utils = { path = "../../../rust/scx_utils", version = "1.0.4" }
serde = { version = "1.0", features = ["derive"] }
simplelog = "0.12"

[build-dependencies]
scx_utils = { path = "../../../rust/scx_utils", version = "1.0.4" }

[features]
enable_backtrace = []
1 change: 1 addition & 0 deletions scheds/rust/scx_fair/LICENSE
26 changes: 26 additions & 0 deletions scheds/rust/scx_fair/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# scx_fair

This is a single user-defined scheduler used within [sched_ext](https://github.com/sched-ext/scx/tree/main), which is a Linux kernel feature which enables implementing kernel thread schedulers in BPF and dynamically loading them. [Read more about sched_ext](https://github.com/sched-ext/scx/tree/main).

## Overview

A scheduler that focuses on ensuring fairness among tasks and performance
predictability.

It operates using a vruntime-based policy, where each task is assigned a
"latency" weight. This weight is dynamically adjusted based on how often a task
release the CPU before its full time slice is used. Tasks that release the CPU
early are given a higher latency weight, prioritizing them over tasks that
fully consume their time slice.

## Typical Use Case

The combination of dynamic latency weights and vruntime-based scheduling
ensures responsive and consistent performance, even in overcommitted systems.

This makes the scheduler particularly well-suited for workloads that require
multimedia or real-time audio processing.

## Production Ready?

Yes.
13 changes: 13 additions & 0 deletions scheds/rust/scx_fair/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Andrea Righi <[email protected]>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2.

fn main() {
scx_utils::BpfBuilder::new()
.unwrap()
.enable_intf("src/bpf/intf.h", "bpf_intf.rs")
.enable_skel("src/bpf/main.bpf.c", "bpf")
.build()
.unwrap();
}
8 changes: 8 additions & 0 deletions scheds/rust/scx_fair/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Get help on options with `rustfmt --help=config`
# Please keep these in alphabetical order.
edition = "2021"
group_imports = "StdExternalCrate"
imports_granularity = "Item"
merge_derives = false
use_field_init_shorthand = true
version = "Two"
42 changes: 42 additions & 0 deletions scheds/rust/scx_fair/src/bpf/intf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2024 Andrea Righi <[email protected]>
*
* This software may be used and distributed according to the terms of the GNU
* General Public License version 2.
*/
#ifndef __INTF_H
#define __INTF_H

#include <limits.h>

#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define CLAMP(val, lo, hi) MIN(MAX(val, lo), hi)

enum consts {
NSEC_PER_USEC = 1000ULL,
NSEC_PER_MSEC = (1000ULL * NSEC_PER_USEC),
NSEC_PER_SEC = (1000ULL * NSEC_PER_MSEC),
};

#ifndef __VMLINUX_H__
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long u64;

typedef signed char s8;
typedef signed short s16;
typedef signed int s32;
typedef signed long s64;

typedef int pid_t;
#endif /* __VMLINUX_H__ */

struct domain_arg {
s32 cpu_id;
s32 sibling_cpu_id;
};

#endif /* __INTF_H */
Loading

0 comments on commit 07de4dd

Please sign in to comment.