Unverified Commit c42882fc authored by John Titor's avatar John Titor
Browse files

scx: init at 1.05

sched_ext is a Linux kernel feature which enables implementing kernel thread schedulers in BPF and dynamically loading them.
This feature got merged in Linux 6.12, this PR adds rust and C userspace schedulers.
Homepage:- https://github.com/sched-ext/scx

With this following packages are now available:
scx.rustland
scx.rlfifo
scx.layered
scx.lavd
scx.bpfland
scx.csheds
parent e979914d
Loading
Loading
Loading
Loading
+74 −0
Original line number Diff line number Diff line
{
  lib,
  callPackage,
  pkg-config,
  rustPlatform,
  llvmPackages,
  elfutils,
  zlib,
  fetchFromGitHub,
}:
let
  versionInfo = lib.importJSON ./version.json;
  mkScxScheduler =
    packageType:
    args@{ schedulerName, ... }:
    (if packageType == "rust" then rustPlatform.buildRustPackage else llvmPackages.stdenv.mkDerivation)
      (
        args
        // {
          pname = "${schedulerName}";
          version = args.version or versionInfo.scx.version;

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

          nativeBuildInputs = [
            pkg-config
            llvmPackages.clang
          ] ++ (args.nativeBuildInputs or [ ]);
          buildInputs = [
            elfutils
            zlib
          ] ++ (args.buildInputs or [ ]);

          env.LIBCLANG_PATH = args.env.LIBCLANG_PATH or "${llvmPackages.libclang.lib}/lib";

          # Needs to be disabled in BPF builds
          hardeningDisable = [
            "zerocallusedregs"
          ] ++ (args.hardeningDisable or [ ]);

          meta = args.meta // {
            description = args.meta.description or "";
            longDescription =
              (args.meta.longDescription or "")
              + ''
                \n\nSched-ext schedulers are only available on supported kernels
                              (6.12 and above or any kernel with the scx patchset applied).'';

            homepage = args.meta.homepage or "https://github.com/sched-ext/scx";
            license = args.meta.license or lib.licenses.gpl2Only;
            platforms = args.meta.platforms or lib.platforms.linux;
            maintainers = (args.meta.maintainers or [ ]) ++ (with lib.maintainers; [ johnrtitor ]);
          };
        }
      );

  schedulers = lib.mergeAttrsList [
    { bpfland = import ./scx_bpfland; }
    { lavd = import ./scx_lavd; }
    { layered = import ./scx_layered; }
    { rlfifo = import ./scx_rlfifo; }
    { rustland = import ./scx_rustland; }
    { csheds = import ./scx_csheds.nix; }
  ];
in
(lib.mapAttrs (name: scheduler: callPackage scheduler { inherit mkScxScheduler; }) schedulers)
// {
  inherit mkScxScheduler;
}
+1530 −0

File added.

Preview size limit exceeded, changes collapsed.

+38 −0
Original line number Diff line number Diff line
{
  stdenv,
  lib,
  mkScxScheduler,
}:

mkScxScheduler "rust" rec {
  schedulerName = "scx_bpfland";

  cargoRoot = "scheds/rust/scx_bpfland";
  cargoLock.lockFile = ./Cargo.lock;
  postPatch = ''
    rm Cargo.toml Cargo.lock
    ln -fs ${./Cargo.lock} scheds/rust/scx_bpfland/Cargo.lock
  '';

  preBuild = ''
    cd scheds/rust/scx_bpfland
  '';

  installPhase = ''
    runHook preInstall
    mkdir -p $out/bin
    cp target/${stdenv.targetPlatform.config}/release/scx_bpfland $out/bin/
    runHook postInstall
  '';

  meta = {
    description = "Sched-ext Rust userspace scheduler";
    longDescription = ''
      Vruntime-based Sched-ext scheduler that prioritizes interactive workloads. This
      scheduler is derived from scx_rustland, but it is fully implemented in BPF. It
      has a minimal user-space Rust part to process command line options, collect metrics
      and log out scheduling statistics. The BPF part makes all the scheduling decisions.
    '';
    mainProgram = "scx_bpfland";
  };
}
+106 −0
Original line number Diff line number Diff line
{
  stdenv,
  lib,
  mkScxScheduler,
  fetchFromGitHub,
  writeShellScript,
  bash,
  meson,
  ninja,
  jq,
  bpftools,
  elfutils,
  zlib,
  libbpf,
}:

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;
  };

  # 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
  '';

  # 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} "$@"
  '';

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

in
mkScxScheduler "c" rec {
  schedulerName = "scx_csheds";

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

  nativeBuildInputs = [
    meson
    ninja
    jq
  ] ++ bpftools.buildInputs ++ bpftools.nativeBuildInputs;

  buildInputs = [
    elfutils
    zlib
    libbpf-git
  ];

  mesonFlags = [
    (lib.mapAttrsToList lib.mesonEnable {
      "systemd" = false;
      "openrc" = false;
      "libbpf_a" = false;
      # not for nix
      "libalpm" = false;
    })
    (lib.mapAttrsToList lib.mesonBool {
      # needed libs are already fetched as FOD
      "offline" = true;
      # rust schedulers are built seperately
      "enable_rust" = false;
    })
  ];

  hardeningDisable = [
    "stackprotector"
  ];

  meta = {
    description = "Sched-ext C userspace schedulers";
    longDescription = ''
      This includes C based schedulers such as scx_central,
      scx_flatcg, scx_pair, scx_qmap, scx_simple, scx_userland.
    '';
  };
}
+1646 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading