summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/scheduling/scx.nix
blob: ba2f895ddb86821063c76f7f890961be208d9db4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{
  lib,
  pkgs,
  config,
  utils,
  ...
}:
let
  cfg = config.services.scx;
in
{
  options.services.scx = {
    enable = lib.mkEnableOption null // {
      description = ''
        Whether to enable SCX service, a daemon to run schedulers from userspace.

        ::: {.note}
        This service requires a kernel with the Sched-ext feature.
        Generally, kernel version 6.12 and later are supported.
        :::
      '';
    };

    package = lib.mkOption {
      type = lib.types.package;
      default = pkgs.scx.full;
      defaultText = lib.literalExpression "pkgs.scx.full";
      example = lib.literalExpression "pkgs.scx.rustscheds";
      description = ''
        `scx` package to use. `scx.full`, which includes all schedulers, is the default.
        You may choose a minimal package, such as `pkgs.scx.rustscheds`.

        ::: {.note}
        Overriding this does not change the default scheduler; you should set `services.scx.scheduler` for it.
        :::
      '';
    };

    scheduler = lib.mkOption {
      type = lib.types.enum cfg.package.schedulers;
      default = "scx_rustland";
      example = "scx_bpfland";
      description = ''
        Which scheduler to use. See [SCX documentation](https://github.com/sched-ext/scx/tree/main/scheds)
        for details on each scheduler and guidance on selecting the most suitable one.
      '';
    };

    extraArgs = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      default = [ ];
      example = [
        "--verbose"
        "--slice-us 5000"
      ];
      description = ''
        Parameters passed to the chosen scheduler at runtime.

        ::: {.note}
        Run `chosen-scx-scheduler --help` to see the available options. Generally,
        each scheduler has its own set of options, and they are incompatible with each other.
        :::
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [ cfg.package ];

    systemd.services.scx = {
      description = "SCX scheduler daemon";

      # SCX service should be started only if the kernel supports sched-ext
      unitConfig.ConditionPathIsDirectory = "/sys/kernel/sched_ext";

      startLimitIntervalSec = 30;
      startLimitBurst = 2;

      serviceConfig = {
        Type = "simple";
        ExecStart = ''
          ${pkgs.runtimeShell} -c 'exec ${cfg.package}/bin/''${SCX_SCHEDULER_OVERRIDE:-$SCX_SCHEDULER} ''${SCX_FLAGS_OVERRIDE:-$SCX_FLAGS}'
        '';
        Restart = "on-failure";
      };

      environment = {
        SCX_SCHEDULER = cfg.scheduler;
        SCX_FLAGS = lib.concatStringsSep " " cfg.extraArgs;
      };

      wantedBy = [ "multi-user.target" ];
    };

    assertions = [
      {
        assertion = lib.versionAtLeast config.boot.kernelPackages.kernel.version "6.12";
        message = "SCX is only supported on kernel version >= 6.12.";
      }
    ];
  };

  meta = {
    inherit (pkgs.scx.full.meta) maintainers;
    buildDocsInSandbox = false;
  };
}