cmd/nix-tool: flag for idle priority
Mainly for usability. This is often ran on a workstation.
This commit is contained in:
parent
0d5e7f61fc
commit
e2299a57d2
@ -9,6 +9,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"syscall"
|
||||
@ -29,6 +30,7 @@ func main() {
|
||||
|
||||
var (
|
||||
flagStore string
|
||||
flagIdle bool
|
||||
flagNixOS bool
|
||||
flagVerbose bool
|
||||
flagJSON bool
|
||||
@ -36,6 +38,13 @@ func main() {
|
||||
c := command.New(os.Stderr, log.Printf, "nix-tool", func(args []string) error {
|
||||
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
|
||||
flagStore = strings.TrimSpace(flagStore)
|
||||
if flagStore != string(os.PathSeparator) {
|
||||
@ -58,6 +67,7 @@ func main() {
|
||||
return nil
|
||||
}).
|
||||
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(&flagVerbose, "v", command.BoolFlag(false), "Connect nix stderr").
|
||||
Flag(&flagJSON, "json", command.BoolFlag(false), "Serialise output in JSON when applicable")
|
||||
|
25
cmd/nix-tool/sched.go
Normal file
25
cmd/nix-tool/sched.go
Normal 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user