cmd/nix-tool: flag for idle priority

Mainly for usability. This is often ran on a workstation.
This commit is contained in:
Ophestra 2025-07-20 19:17:08 +09:00
parent 0d5e7f61fc
commit e2299a57d2
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 35 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"runtime"
"slices" "slices"
"strings" "strings"
"syscall" "syscall"
@ -29,6 +30,7 @@ func main() {
var ( var (
flagStore string flagStore string
flagIdle bool
flagNixOS bool flagNixOS bool
flagVerbose bool flagVerbose bool
flagJSON bool flagJSON bool
@ -36,6 +38,13 @@ func main() {
c := command.New(os.Stderr, log.Printf, "nix-tool", func(args []string) error { c := command.New(os.Stderr, log.Printf, "nix-tool", func(args []string) error {
log.SetFlags(0) log.SetFlags(0)
if flagIdle {
runtime.LockOSThread()
if err := schedSetPolicy(0, SCHED_IDLE); err != nil {
return commandHandlerError(fmt.Sprintf("cannot set policy: %v", err))
}
}
var extraArgs []string var extraArgs []string
flagStore = strings.TrimSpace(flagStore) flagStore = strings.TrimSpace(flagStore)
if flagStore != string(os.PathSeparator) { if flagStore != string(os.PathSeparator) {
@ -58,6 +67,7 @@ func main() {
return nil return nil
}). }).
Flag(&flagStore, "store", command.StringFlag("/"), "Path to the nix root"). Flag(&flagStore, "store", command.StringFlag("/"), "Path to the nix root").
Flag(&flagIdle, "idle", command.BoolFlag(false), "Whether to set SCHED_IDLE policy").
Flag(&flagNixOS, "nixos", command.BoolFlag(false), "Interpret input as NixOS flake installable"). Flag(&flagNixOS, "nixos", command.BoolFlag(false), "Interpret input as NixOS flake installable").
Flag(&flagVerbose, "v", command.BoolFlag(false), "Connect nix stderr"). Flag(&flagVerbose, "v", command.BoolFlag(false), "Connect nix stderr").
Flag(&flagJSON, "json", command.BoolFlag(false), "Serialise output in JSON when applicable") Flag(&flagJSON, "json", command.BoolFlag(false), "Serialise output in JSON when applicable")

25
cmd/nix-tool/sched.go Normal file
View File

@ -0,0 +1,25 @@
package main
/*
#ifndef _GNU_SOURCE
#define _GNU_SOURCE // SCHED_IDLE
#endif
#include <sched.h>
const struct sched_param param = { .sched_priority = 0 };
const int NIX_TOOL_SCHED_IDLE = SCHED_IDLE;
*/
import "C"
var (
SCHED_IDLE = int(C.NIX_TOOL_SCHED_IDLE)
)
func schedSetPolicy(pid, policy int) error {
r, err := C.sched_setscheduler(C.pid_t(pid), C.int(policy), &C.param)
if r != 0 {
return err
}
return nil
}