2018-01-15 17:11:14 +00:00
|
|
|
// 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
|
|
|
|
|
2018-01-18 17:03:24 +00:00
|
|
|
import (
|
2022-08-16 08:01:49 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-11-27 11:51:46 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2018-01-18 17:03:24 +00:00
|
|
|
)
|
|
|
|
|
2018-01-15 17:11:14 +00:00
|
|
|
// SetLowPriority lowers the process CPU scheduling priority, and possibly
|
|
|
|
// I/O priority depending on the platform and OS.
|
|
|
|
func SetLowPriority() error {
|
2021-11-27 11:51:46 +00:00
|
|
|
handle, err := windows.GetCurrentProcess()
|
2018-01-18 17:03:24 +00:00
|
|
|
if err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return fmt.Errorf("get process handle: %w", err)
|
2018-01-18 17:03:24 +00:00
|
|
|
}
|
2021-11-27 11:51:46 +00:00
|
|
|
defer windows.CloseHandle(handle)
|
2018-01-18 17:03:24 +00:00
|
|
|
|
2023-07-21 04:38:15 +00:00
|
|
|
if cur, err := windows.GetPriorityClass(handle); err == nil && (cur == windows.IDLE_PRIORITY_CLASS || cur == windows.BELOW_NORMAL_PRIORITY_CLASS) {
|
|
|
|
// We're done here.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-16 08:01:49 +00:00
|
|
|
if err := windows.SetPriorityClass(handle, windows.BELOW_NORMAL_PRIORITY_CLASS); err != nil {
|
|
|
|
return fmt.Errorf("set priority class: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2018-01-15 17:11:14 +00:00
|
|
|
}
|