2018-12-11 22:01:50 +01:00
|
|
|
package zfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-03-21 21:46:29 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-12-11 22:01:50 +01:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2021-03-21 21:46:29 +01:00
|
|
|
|
2024-10-18 19:21:17 +02:00
|
|
|
"github.com/zrepl/zrepl/internal/util/envconst"
|
2018-12-11 22:01:50 +01:00
|
|
|
)
|
|
|
|
|
2021-03-21 21:46:29 +01:00
|
|
|
func getPipeCapacityHint(envvar string) int {
|
|
|
|
var capacity int64 = 1 << 25
|
|
|
|
|
|
|
|
// Work around a race condition in Linux >= 5.8 related to pipe resizing.
|
|
|
|
// https://github.com/zrepl/zrepl/issues/424#issuecomment-800370928
|
|
|
|
// https://bugzilla.kernel.org/show_bug.cgi?id=212295
|
|
|
|
if _, err := os.Stat("/proc/sys/fs/pipe-max-size"); err == nil {
|
2024-09-08 15:15:26 +02:00
|
|
|
if dat, err := os.ReadFile("/proc/sys/fs/pipe-max-size"); err == nil {
|
2021-03-21 21:46:29 +01:00
|
|
|
if capacity, err = strconv.ParseInt(strings.TrimSpace(string(dat)), 10, 64); err != nil {
|
|
|
|
capacity = 1 << 25
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(envconst.Int64(envvar, capacity))
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func trySetPipeCapacity(p *os.File, capacity int) {
|
|
|
|
res, err := unix.FcntlInt(p.Fd(), unix.F_SETPIPE_SZ, capacity)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("cannot set pipe capacity to %v", capacity)
|
|
|
|
} else if res == -1 {
|
|
|
|
err = errors.New("cannot set pipe capacity: fcntl returned -1")
|
|
|
|
}
|
|
|
|
if debugEnabled && err != nil {
|
|
|
|
debug("trySetPipeCapacity error: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|