-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathutil_linux.go
More file actions
62 lines (51 loc) · 1.4 KB
/
util_linux.go
File metadata and controls
62 lines (51 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//go:build linux
package fastdns
import (
"context"
"fmt"
"net"
"runtime"
"syscall"
"unsafe"
)
// listen binds a UDP socket with SO_REUSEPORT on Linux.
func listen(network, address string) (*net.UDPConn, error) {
lc := &net.ListenConfig{
Control: func(network, address string, conn syscall.RawConn) error {
return conn.Control(func(fd uintptr) {
const SO_REUSEPORT = 15
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, SO_REUSEPORT, 1)
})
},
}
conn, err := lc.ListenPacket(context.Background(), network, address)
if err != nil {
return nil, err
}
return conn.(*net.UDPConn), nil
}
// taskset applies a CPU affinity mask to the current process.
func taskset(cpu int) error {
const SYS_SCHED_SETAFFINITY = 203
if cpu < 0 {
return fmt.Errorf("taskset: cpu(%d) must be non-negative", cpu)
}
if cpu >= 128*8 {
return fmt.Errorf("taskset: cpu(%d) exceeds mask capacity", cpu)
}
maxCPU := runtime.NumCPU()
if cpu >= maxCPU {
return fmt.Errorf("taskset: cpu(%d) >= runtime.NumCPU(%d)", cpu, maxCPU)
}
var mask [128]byte
mask[cpu/8] = byte(1 << (uint(cpu) % 8))
_, _, e := syscall.RawSyscall(SYS_SCHED_SETAFFINITY, uintptr(0), uintptr(128), uintptr(unsafe.Pointer(&mask[0])))
if e == 0 {
return nil
}
if e == syscall.EPERM || e == syscall.EINVAL {
// Sandboxes without sched_setaffinity permission surface EPERM/EINVAL; treat as best-effort.
return nil
}
return e
}