From c554ffccc9c8fd54c34dc1b1ff01226fed79c31e Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 15 Jan 2018 17:11:14 +0000 Subject: [PATCH] cmd/syncthing, lib/config, lib/osutil: Lower process priority (fixes #4628) GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4675 --- cmd/syncthing/main.go | 6 +++ lib/config/config_test.go | 2 + lib/config/optionsconfiguration.go | 1 + lib/config/testdata/overridenvalues.xml | 1 + lib/osutil/lowprio_linux.go | 61 +++++++++++++++++++++++++ lib/osutil/lowprio_unix.go | 24 ++++++++++ lib/osutil/lowprio_windows.go | 47 +++++++++++++++++++ 7 files changed, 142 insertions(+) create mode 100644 lib/osutil/lowprio_linux.go create mode 100644 lib/osutil/lowprio_unix.go create mode 100644 lib/osutil/lowprio_windows.go diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index b392bcaf..0372b22f 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -915,6 +915,12 @@ func syncthingMain(runtimeOptions RuntimeOptions) { cleanConfigDirectory() + if cfg.Options().SetLowPriority { + if err := osutil.SetLowPriority(); err != nil { + l.Warnln("Failed to lower process priority:", err) + } + } + code := <-stop mainService.Stop() diff --git a/lib/config/config_test.go b/lib/config/config_test.go index 38ad0e2c..5245f6eb 100644 --- a/lib/config/config_test.go +++ b/lib/config/config_test.go @@ -76,6 +76,7 @@ func TestDefaultValues(t *testing.T) { KCPUpdateIntervalMs: 25, KCPFastResend: false, DefaultFolderPath: "~", + SetLowPriority: true, } cfg := New(device1) @@ -224,6 +225,7 @@ func TestOverriddenValues(t *testing.T) { KCPUpdateIntervalMs: 1000, KCPFastResend: true, DefaultFolderPath: "/media/syncthing", + SetLowPriority: false, } os.Unsetenv("STNOUPGRADE") diff --git a/lib/config/optionsconfiguration.go b/lib/config/optionsconfiguration.go index 74e1df4a..578bd6ad 100644 --- a/lib/config/optionsconfiguration.go +++ b/lib/config/optionsconfiguration.go @@ -143,6 +143,7 @@ type OptionsConfiguration struct { KCPSendWindowSize int `xml:"kcpSendWindowSize" json:"kcpSendWindowSize" default:"128"` KCPReceiveWindowSize int `xml:"kcpReceiveWindowSize" json:"kcpReceiveWindowSize" default:"128"` DefaultFolderPath string `xml:"defaultFolderPath" json:"defaultFolderPath" default:"~"` + SetLowPriority bool `xml:"setLowPriority" json:"setLowPriority" default:"true"` DeprecatedUPnPEnabled bool `xml:"upnpEnabled,omitempty" json:"-"` DeprecatedUPnPLeaseM int `xml:"upnpLeaseMinutes,omitempty" json:"-"` diff --git a/lib/config/testdata/overridenvalues.xml b/lib/config/testdata/overridenvalues.xml index 1ab0f2d6..d8387aa4 100644 --- a/lib/config/testdata/overridenvalues.xml +++ b/lib/config/testdata/overridenvalues.xml @@ -44,5 +44,6 @@ 1000 true /media/syncthing + false diff --git a/lib/osutil/lowprio_linux.go b/lib/osutil/lowprio_linux.go new file mode 100644 index 00000000..c3b6ab48 --- /dev/null +++ b/lib/osutil/lowprio_linux.go @@ -0,0 +1,61 @@ +// 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/. + +package osutil + +import ( + "syscall" + + "github.com/pkg/errors" +) + +const ioprioClassShift = 13 + +type ioprioClass int + +const ( + ioprioClassRT ioprioClass = iota + 1 + ioprioClassBE + ioprioClassIdle +) + +const ( + ioprioWhoProcess = iota + 1 + ioprioWhoPGRP + ioprioWhoUser +) + +func ioprioSet(class ioprioClass, value int) error { + res, _, err := syscall.Syscall(syscall.SYS_IOPRIO_SET, + uintptr(ioprioWhoProcess), 0, + uintptr(class)<