mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-10 07:11:08 +00:00
lib/osutil: Don't attempt to reduce our niceness level (fixes #4681)
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4682
This commit is contained in:
parent
91210cbb49
commit
c2119c9e62
@ -42,23 +42,52 @@ func ioprioSet(class ioprioClass, value int) error {
|
|||||||
// SetLowPriority lowers the process CPU scheduling priority, and possibly
|
// SetLowPriority lowers the process CPU scheduling priority, and possibly
|
||||||
// I/O priority depending on the platform and OS.
|
// I/O priority depending on the platform and OS.
|
||||||
func SetLowPriority() error {
|
func SetLowPriority() error {
|
||||||
|
// Process zero is "self", niceness value 9 is something between 0
|
||||||
|
// (default) and 19 (worst priority). But then, this is Linux, so of
|
||||||
|
// course we get this to take care of as well:
|
||||||
|
//
|
||||||
|
// "C library/kernel differences
|
||||||
|
//
|
||||||
|
// Within the kernel, nice values are actually represented using the
|
||||||
|
// range 40..1 (since negative numbers are error codes) and these are
|
||||||
|
// the values employed by the setpriority() and getpriority() system
|
||||||
|
// calls. The glibc wrapper functions for these system calls handle the
|
||||||
|
// translations between the user-land and kernel representations of the
|
||||||
|
// nice value according to the formula unice = 20 - knice. (Thus, the
|
||||||
|
// kernel's 40..1 range corresponds to the range -20..19 as seen by user
|
||||||
|
// space.)"
|
||||||
|
|
||||||
|
const (
|
||||||
|
pidSelf = 0
|
||||||
|
wantNiceLevel = 20 - 9
|
||||||
|
)
|
||||||
|
|
||||||
|
// Remember Linux kernel nice levels are upside down.
|
||||||
|
if cur, err := syscall.Getpriority(syscall.PRIO_PROCESS, 0); err == nil && cur <= wantNiceLevel {
|
||||||
|
// We're done here.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Move ourselves to a new process group so that we can use the process
|
// Move ourselves to a new process group so that we can use the process
|
||||||
// group variants of Setpriority etc to affect all of our threads in one
|
// group variants of Setpriority etc to affect all of our threads in one
|
||||||
// go. If this fails, bail, so that we don't affect things we shouldn't.
|
// go. If this fails, bail, so that we don't affect things we shouldn't.
|
||||||
// If we are already the leader of our own process group, do nothing.
|
// If we are already the leader of our own process group, do nothing.
|
||||||
if pgid, err := syscall.Getpgid(0); err != nil {
|
//
|
||||||
|
// Oh and this is because Linux doesn't follow the POSIX threading model
|
||||||
|
// where setting the niceness of the process would actually set the
|
||||||
|
// niceness of the process, instead it just affects the current thread
|
||||||
|
// so we need this workaround...
|
||||||
|
if pgid, err := syscall.Getpgid(pidSelf); err != nil {
|
||||||
// This error really shouldn't happen
|
// This error really shouldn't happen
|
||||||
return errors.Wrap(err, "get process group")
|
return errors.Wrap(err, "get process group")
|
||||||
} else if pgid != os.Getpid() {
|
} else if pgid != os.Getpid() {
|
||||||
// We are not process group leader. Elevate!
|
// We are not process group leader. Elevate!
|
||||||
if err := syscall.Setpgid(0, 0); err != nil {
|
if err := syscall.Setpgid(pidSelf, 0); err != nil {
|
||||||
return errors.Wrap(err, "set process group")
|
return errors.Wrap(err, "set process group")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process zero is "self", niceness value 9 is something between 0
|
if err := syscall.Setpriority(syscall.PRIO_PGRP, pidSelf, wantNiceLevel); err != nil {
|
||||||
// (default) and 19 (worst priority).
|
|
||||||
if err := syscall.Setpriority(syscall.PRIO_PGRP, 0, 9); err != nil {
|
|
||||||
return errors.Wrap(err, "set niceness")
|
return errors.Wrap(err, "set niceness")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,16 @@ import (
|
|||||||
func SetLowPriority() error {
|
func SetLowPriority() error {
|
||||||
// Process zero is "self", niceness value 9 is something between 0
|
// Process zero is "self", niceness value 9 is something between 0
|
||||||
// (default) and 19 (worst priority).
|
// (default) and 19 (worst priority).
|
||||||
err := syscall.Setpriority(syscall.PRIO_PROCESS, 0, 9)
|
const (
|
||||||
|
pidSelf = 0
|
||||||
|
wantNiceLevel = 9
|
||||||
|
)
|
||||||
|
|
||||||
|
if cur, err := syscall.Getpriority(syscall.PRIO_PROCESS, pidSelf); err == nil && cur >= wantNiceLevel {
|
||||||
|
// We're done here.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err := syscall.Setpriority(syscall.PRIO_PROCESS, pidSelf, wantNiceLevel)
|
||||||
return errors.Wrap(err, "set niceness") // wraps nil as nil
|
return errors.Wrap(err, "set niceness") // wraps nil as nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user