Skip to content

Commit ee57887

Browse files
kolyshkingopherbot
authored andcommitted
unix: add SchedSetAttr and SchedGetAttr for Linux
Add wrappers for sched_getattr(2) and sched_setattr(2), as well as various SCHED_ values usable for these. The kludge in linux/types.go is needed so we can include both linux/sched/types.h (for struct sched_attr) and sched.h (for a few defines from include/bits/cpu-set.h). Unfortunately, they both define struct sched_param, thus the need to mask one of the definitions. Change-Id: I3e13cf49ccef7ae81a75d33826d18de84a52106d Signed-off-by: Kir Kolyshkin <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/sys/+/516756 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]>
1 parent 60ecf13 commit ee57887

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

unix/linux/types.go

+9
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ struct termios2 {
138138
#include <linux/random.h>
139139
#include <linux/rtc.h>
140140
#include <linux/rtnetlink.h>
141+
// This is to avoid a conflict of struct sched_param being defined by
142+
// both the kernel and the glibc (sched.h) headers.
143+
#define sched_param kernel_sched_param
144+
#include <linux/sched/types.h>
145+
#undef kernel_sched_param
141146
#include <linux/shm.h>
142147
#include <linux/socket.h>
143148
#include <linux/stat.h>
@@ -5796,3 +5801,7 @@ const (
57965801
RISCV_HWPROBE_MISALIGNED_UNSUPPORTED = C.RISCV_HWPROBE_MISALIGNED_UNSUPPORTED
57975802
RISCV_HWPROBE_MISALIGNED_MASK = C.RISCV_HWPROBE_MISALIGNED_MASK
57985803
)
5804+
5805+
type SchedAttr C.struct_sched_attr
5806+
5807+
const SizeofSchedAttr = C.sizeof_struct_sched_attr

unix/mkerrors.sh

+1
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ ccflags="$@"
583583
$2 ~ /^PERF_/ ||
584584
$2 ~ /^SECCOMP_MODE_/ ||
585585
$2 ~ /^SEEK_/ ||
586+
$2 ~ /^SCHED_/ ||
586587
$2 ~ /^SPLICE_/ ||
587588
$2 ~ /^SYNC_FILE_RANGE_/ ||
588589
$2 !~ /IOC_MAGIC/ &&

unix/syscall_linux.go

+23
Original file line numberDiff line numberDiff line change
@@ -2471,6 +2471,29 @@ func Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *
24712471
return pselect6(nfd, r, w, e, mutableTimeout, kernelMask)
24722472
}
24732473

2474+
//sys schedSetattr(pid int, attr *SchedAttr, flags uint) (err error)
2475+
//sys schedGetattr(pid int, attr *SchedAttr, size uint, flags uint) (err error)
2476+
2477+
// SchedSetAttr is a wrapper for sched_setattr(2) syscall.
2478+
// https://man7.org/linux/man-pages/man2/sched_setattr.2.html
2479+
func SchedSetAttr(pid int, attr *SchedAttr, flags uint) error {
2480+
if attr == nil {
2481+
return EINVAL
2482+
}
2483+
attr.Size = SizeofSchedAttr
2484+
return schedSetattr(pid, attr, flags)
2485+
}
2486+
2487+
// SchedGetAttr is a wrapper for sched_getattr(2) syscall.
2488+
// https://man7.org/linux/man-pages/man2/sched_getattr.2.html
2489+
func SchedGetAttr(pid int, flags uint) (*SchedAttr, error) {
2490+
attr := &SchedAttr{}
2491+
if err := schedGetattr(pid, attr, SizeofSchedAttr, flags); err != nil {
2492+
return nil, err
2493+
}
2494+
return attr, nil
2495+
}
2496+
24742497
/*
24752498
* Unimplemented
24762499
*/

unix/zerrors_linux.go

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zsyscall_linux.go

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/ztypes_linux.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)