<commit_before>// 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)<<ioprioClassShift|uintptr(value))
	if res == 0 {
		return nil
	}
	return err
}

// SetLowPriority lowers the process CPU scheduling priority, and possibly
// I/O priority depending on the platform and OS.
func SetLowPriority() error {
	// Move ourselves to a new process group so that we can use the process
	// group variants of Setpriority etc to affect all of our threads in one
	// go. If this fails, bail, so that we don't affect things we shouldn't.
	if err := syscall.Setpgid(0, 0); err != nil {
		return errors.Wrap(err, "set process group")
	}

	// Process zero is "self", niceness value 9 is something between 0
	// (default) and 19 (worst priority).
	if err := syscall.Setpriority(syscall.PRIO_PGRP, 0, 9); err != nil {
		return errors.Wrap(err, "set niceness")
	}

	// Best effort, somewhere to the end of the scale (0 through 7 being the
	// range).
	err := ioprioSet(ioprioClassBE, 5)
	return errors.Wrap(err, "set I/O priority") // wraps nil as nil
}
<commit_msg>lib/osutil: Check PGID before trying to set it (fixes #4679)<commit_after>// 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 (
	"os"
	"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)<<ioprioClassShift|uintptr(value))
	if res == 0 {
		return nil
	}
	return err
}

// SetLowPriority lowers the process CPU scheduling priority, and possibly
// I/O priority depending on the platform and OS.
func SetLowPriority() error {
	// Move ourselves to a new process group so that we can use the process
	// group variants of Setpriority etc to affect all of our threads in one
	// go. If this fails, bail, so that we don't affect things we shouldn't.
	// If we are already the leader of our own process group, do nothing.
	if pgid, err := syscall.Getpgid(0); err != nil {
		// This error really shouldn't happen
		return errors.Wrap(err, "get process group")
	} else if pgid != os.Getpid() {
		// We are not process group leader. Elevate!
		if err := syscall.Setpgid(0, 0); err != nil {
			return errors.Wrap(err, "set process group")
		}
	}

	// Process zero is "self", niceness value 9 is something between 0
	// (default) and 19 (worst priority).
	if err := syscall.Setpriority(syscall.PRIO_PGRP, 0, 9); err != nil {
		return errors.Wrap(err, "set niceness")
	}

	// Best effort, somewhere to the end of the scale (0 through 7 being the
	// range).
	err := ioprioSet(ioprioClassBE, 5)
	return errors.Wrap(err, "set I/O priority") // wraps nil as nil
}

{'fraction_non_alphanumeric': 0.10360726878220776, 'fraction_numerical': 0.014917276918904258, 'mean_word_length': 3.592777085927771, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1332447', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1204, 'n_words': 468}