Skip to content

Commit

Permalink
scx: init
Browse files Browse the repository at this point in the history
rustland is not working
  • Loading branch information
JohnRTitor committed Nov 21, 2024
1 parent df453c9 commit ca3282d
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
4 changes: 4 additions & 0 deletions flake/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
adminer-pematon-with-adminer-theme = pkgs.callPackage ../pkgs/adminer-pematon-with-adminer-theme {
inherit (self'.packages) adminer-pematon;
};
scx-cscheds = pkgs.callPackage ../pkgs/scx {};
scx-rustscheds = pkgs.callPackage ../pkgs/scx/rust.nix {
inherit (self'.packages) scx-cscheds;
};
};
};
}
133 changes: 133 additions & 0 deletions pkgs/scx/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
llvmPackages,
lib,
fetchFromGitHub,
writeShellScript,
bash,
meson,
ninja,
jq,
bpftools,
elfutils,
zlib,
zstd,
pkg-config
}:

let
versionInfo = lib.importJSON ./version.json;

# scx needs a specific commit of bpftool
# can be found in meson.build of scx src
# grep 'bpftool_commit =' ./meson.build
bpftools_src = fetchFromGitHub {
owner = "libbpf";
repo = "bpftool";
inherit (versionInfo.bpftool) rev hash;
fetchSubmodules = true;
};

# Won't build with stable libbpf, so use the latest commit
libbpf_src = fetchFromGitHub {
owner = "libbpf";
repo = "libbpf";
inherit (versionInfo.libbpf) rev hash;
fetchSubmodules = true;
};

# scx needs a specific commit of bpftool
# this imitates the fetch_bpftool script in src/meson-scripts
fetchBpftool = writeShellScript "fetch_bpftool" ''
[ "$2" == '${bpftools_src.rev}' ] || exit 1
cd "$1"
cp --no-preserve=mode,owner -r "${bpftools_src}/" ./bpftool
'';
fetchLibbpf = writeShellScript "fetch_libbpf" ''
[ "$2" == '${libbpf_src.rev}' ] || exit 1
cd "$1"
cp --no-preserve=mode,owner -r "${libbpf_src}/" ./libbpf
mkdir -p ./libbpf/src/usr/include
'';

# Fixes a bug with the meson build script where it specifies
# /bin/bash twice in the script
misbehaviorBash = writeShellScript "bash" ''
shift 1
exec ${lib.getExe bash} "$@"
'';

in
llvmPackages.stdenv.mkDerivation {
pname = "scx_cscheds";
version = versionInfo.scx.version;

src = fetchFromGitHub {
owner = "sched-ext";
repo = "scx";
rev = "refs/tags/v${versionInfo.scx.version}";
inherit (versionInfo.scx) hash;
};

postPatch = ''
rm meson-scripts/fetch_bpftool meson-scripts/fetch_libbpf
patchShebangs ./meson-scripts
cp ${fetchBpftool} meson-scripts/fetch_bpftool
cp ${fetchLibbpf} meson-scripts/fetch_libbpf
substituteInPlace meson.build \
--replace-fail '[build_bpftool' "['${misbehaviorBash}', build_bpftool"
'';

nativeBuildInputs = [
meson
ninja
jq
pkg-config
zstd
] ++ bpftools.buildInputs ++ bpftools.nativeBuildInputs;

buildInputs = [
elfutils
zlib
];

mesonFlags = [
(lib.mapAttrsToList lib.mesonEnable {
# systemd unit is implemented in the nixos module
# upstream systemd files are a hassle to patch
"systemd" = false;
"openrc" = false;
# libbpf is already fetched as FOD
#"libbpf_a" = true;
# not for nix
"libalpm" = false;
})
(lib.mapAttrsToList lib.mesonBool {
# needed libs are already fetched as FOD
"offline" = true;
# rust based schedulers are built seperately
"enable_rust" = false;
})
];

hardeningDisable = [
"stackprotector"
"zerocallusedregs"
];

preInstall = ''
mkdir -p ${placeholder "dev"}/libbpf
mkdir -p ${placeholder "dev"}/bpftool
cp -r libbpf/* ${placeholder "dev"}/libbpf/
cp -r bpftool/* ${placeholder "dev"}/bpftool/
'';

outputs = [ "bin" "dev" "out" ];

meta = {
description = "Sched-ext C userspace schedulers";
longDescription = ''
This includes C based schedulers such as scx_central, scx_flatcg,
scx_nest, scx_pair, scx_qmap, scx_simple, scx_userland.
'';
};
}
43 changes: 43 additions & 0 deletions pkgs/scx/rust.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{ lib
, llvmPackages
, elfutils
, zlib
, pkg-config
, rustPlatform
, zstd
, nix-update-script
, scx-cscheds
}:
rustPlatform.buildRustPackage rec {
pname = "scx_rustscheds";
inherit (scx-cscheds) version src;

cargoHash = "sha256-bBDDNP+f9Ss6+IRFk3at+7+pDLMT6kOlTF1nnx6sqSE=";

nativeBuildInputs = [
pkg-config
llvmPackages.clang
];
buildInputs = [
elfutils
zlib
zstd
];

env = {
LIBCLANG_PATH = "${lib.getLib llvmPackages.libclang}/lib";
BPF_CLANG = lib.getExe llvmPackages.clang;
BPF_EXTRA_CFLAGS_PRE_INCL = "-I${scx-cscheds.dev}/libbpf/src/usr/include -I${scx-cscheds.dev}/libbpf/include/uapi";
RUSTFLAGS = "-C relocation-model=pic -C link-args=-lelf -C link-args=-lz -C link-args=-lzstd -L ${scx-cscheds.dev}/libbpf/src";
};

hardeningDisable = [
"stackprotector"
"zerocallusedregs"
];

# Enable this when default kernel in nixpkgs is 6.12+
doCheck = false;

passthru.updateScript = nix-update-script {};
}
14 changes: 14 additions & 0 deletions pkgs/scx/version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"scx": {
"version": "1.0.5",
"hash": "sha256-nb2bzEanPPWTUhMmGw/8/bwOkdgNmwoZX2lMFq5Av5Q="
},
"bpftool": {
"rev": "77a72987353fcae8ce330fd87d4c7afb7677a169",
"hash": "sha256-pItTVewlXgB97AC/WH9rW9J/eYSe2ZdBkJaAgGnDeUU="
},
"libbpf": {
"rev": "686f600bca59e107af4040d0838ca2b02c14ff50",
"hash": "sha256-uIu57gFkPhBCq3RqKb6J4r5OB4X5/V9q8HqIafUc+Do="
}
}

0 comments on commit ca3282d

Please sign in to comment.