lowprio_linux.go 3.1 KB

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