2018-01-15 17:11:14 +00:00
|
|
|
// Copyright (C) 2018 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
2024-07-31 05:31:14 +00:00
|
|
|
//go:build (!windows && !linux && !ios) || android
|
|
|
|
// +build !windows,!linux,!ios android
|
2018-01-15 17:11:14 +00:00
|
|
|
|
|
|
|
package osutil
|
|
|
|
|
|
|
|
import (
|
2022-08-16 08:01:49 +00:00
|
|
|
"fmt"
|
2018-01-15 17:11:14 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetLowPriority lowers the process CPU scheduling priority, and possibly
|
|
|
|
// I/O priority depending on the platform and OS.
|
|
|
|
func SetLowPriority() error {
|
|
|
|
// Process zero is "self", niceness value 9 is something between 0
|
|
|
|
// (default) and 19 (worst priority).
|
2018-01-18 08:34:56 +00:00
|
|
|
const (
|
|
|
|
pidSelf = 0
|
|
|
|
wantNiceLevel = 9
|
|
|
|
)
|
|
|
|
|
|
|
|
if cur, err := syscall.Getpriority(syscall.PRIO_PROCESS, pidSelf); err == nil && cur >= wantNiceLevel {
|
|
|
|
// We're done here.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-16 08:01:49 +00:00
|
|
|
if err := syscall.Setpriority(syscall.PRIO_PROCESS, pidSelf, wantNiceLevel); err != nil {
|
|
|
|
return fmt.Errorf("set niceness: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2018-01-15 17:11:14 +00:00
|
|
|
}
|