lowprio_linux.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. package osutil
  7. import (
  8. "os"
  9. "syscall"
  10. "github.com/pkg/errors"
  11. )
  12. const ioprioClassShift = 13
  13. type ioprioClass int
  14. const (
  15. ioprioClassRT ioprioClass = iota + 1
  16. ioprioClassBE
  17. ioprioClassIdle
  18. )
  19. const (
  20. ioprioWhoProcess = iota + 1
  21. ioprioWhoPGRP
  22. ioprioWhoUser
  23. )
  24. func ioprioSet(class ioprioClass, value int) error {
  25. res, _, err := syscall.Syscall(syscall.SYS_IOPRIO_SET,
  26. uintptr(ioprioWhoProcess), 0,
  27. uintptr(class)<<ioprioClassShift|uintptr(value))
  28. if res == 0 {
  29. return nil
  30. }
  31. return err
  32. }
  33. // SetLowPriority lowers the process CPU scheduling priority, and possibly
  34. // I/O priority depending on the platform and OS.
  35. func SetLowPriority() error {
  36. // Process zero is "self", niceness value 9 is something between 0
  37. // (default) and 19 (worst priority). But then, this is Linux, so of
  38. // course we get this to take care of as well:
  39. //
  40. // "C library/kernel differences
  41. //
  42. // Within the kernel, nice values are actually represented using the
  43. // range 40..1 (since negative numbers are error codes) and these are
  44. // the values employed by the setpriority() and getpriority() system
  45. // calls. The glibc wrapper functions for these system calls handle the
  46. // translations between the user-land and kernel representations of the
  47. // nice value according to the formula unice = 20 - knice. (Thus, the
  48. // kernel's 40..1 range corresponds to the range -20..19 as seen by user
  49. // space.)"
  50. const (
  51. pidSelf = 0
  52. wantNiceLevel = 20 - 9
  53. )
  54. // Remember Linux kernel nice levels are upside down.
  55. if cur, err := syscall.Getpriority(syscall.PRIO_PROCESS, 0); err == nil && cur <= wantNiceLevel {
  56. // We're done here.
  57. return nil
  58. }
  59. // Move ourselves to a new process group so that we can use the process
  60. // group variants of Setpriority etc to affect all of our threads in one
  61. // go. If this fails, bail, so that we don't affect things we shouldn't.
  62. // If we are already the leader of our own process group, do nothing.
  63. //
  64. // Oh and this is because Linux doesn't follow the POSIX threading model
  65. // where setting the niceness of the process would actually set the
  66. // niceness of the process, instead it just affects the current thread
  67. // so we need this workaround...
  68. if pgid, err := syscall.Getpgid(pidSelf); err != nil {
  69. // This error really shouldn't happen
  70. return errors.Wrap(err, "get process group")
  71. } else if pgid != os.Getpid() {
  72. // We are not process group leader. Elevate!
  73. if err := syscall.Setpgid(pidSelf, 0); err != nil {
  74. return errors.Wrap(err, "set process group")
  75. }
  76. }
  77. if err := syscall.Setpriority(syscall.PRIO_PGRP, pidSelf, wantNiceLevel); err != nil {
  78. return errors.Wrap(err, "set niceness")
  79. }
  80. // Best effort, somewhere to the end of the scale (0 through 7 being the
  81. // range).
  82. err := ioprioSet(ioprioClassBE, 5)
  83. return errors.Wrap(err, "set I/O priority") // wraps nil as nil
  84. }