lowprio_linux.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //go:build !android
  7. // +build !android
  8. package osutil
  9. import (
  10. "fmt"
  11. "os"
  12. "syscall"
  13. )
  14. // SetLowPriority lowers the process CPU scheduling priority, and possibly
  15. // I/O priority depending on the platform and OS.
  16. func SetLowPriority() error {
  17. // Process zero is "self", niceness value 9 is something between 0
  18. // (default) and 19 (worst priority). But then, this is Linux, so of
  19. // course we get this to take care of as well:
  20. //
  21. // "C library/kernel differences
  22. //
  23. // Within the kernel, nice values are actually represented using the
  24. // range 40..1 (since negative numbers are error codes) and these are
  25. // the values employed by the setpriority() and getpriority() system
  26. // calls. The glibc wrapper functions for these system calls handle the
  27. // translations between the user-land and kernel representations of the
  28. // nice value according to the formula unice = 20 - knice. (Thus, the
  29. // kernel's 40..1 range corresponds to the range -20..19 as seen by user
  30. // space.)"
  31. const (
  32. pidSelf = 0
  33. wantNiceLevel = 20 - 9
  34. )
  35. // Remember Linux kernel nice levels are upside down.
  36. if cur, err := syscall.Getpriority(syscall.PRIO_PROCESS, 0); err == nil && cur <= wantNiceLevel {
  37. // We're done here.
  38. return nil
  39. }
  40. // Move ourselves to a new process group so that we can use the process
  41. // group variants of Setpriority etc to affect all of our threads in one
  42. // go. If this fails, bail, so that we don't affect things we shouldn't.
  43. // If we are already the leader of our own process group, do nothing.
  44. //
  45. // Oh and this is because Linux doesn't follow the POSIX threading model
  46. // where setting the niceness of the process would actually set the
  47. // niceness of the process, instead it just affects the current thread
  48. // so we need this workaround...
  49. if pgid, err := syscall.Getpgid(pidSelf); err != nil {
  50. // This error really shouldn't happen
  51. return fmt.Errorf("get process group: %w", err)
  52. } else if pgid != os.Getpid() {
  53. // We are not process group leader. Elevate!
  54. if err := syscall.Setpgid(pidSelf, 0); err != nil {
  55. return fmt.Errorf("set process group: %w", err)
  56. }
  57. }
  58. // For any new process, the default is to be assigned the IOPRIO_CLASS_BE
  59. // scheduling class. This class directly maps the BE prio level to the
  60. // niceness of a process, determined as: io_nice = (cpu_nice + 20) / 5.
  61. // For example, a niceness of 11 results in an I/O priority of B6.
  62. // https://www.kernel.org/doc/Documentation/block/ioprio.txt
  63. if err := syscall.Setpriority(syscall.PRIO_PGRP, pidSelf, wantNiceLevel); err != nil {
  64. return fmt.Errorf("set niceness: %w", err)
  65. }
  66. return nil
  67. }