From 54e2edd1be94831eab8dc62b4d0b048825ff5cc3 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 16 Feb 2023 11:20:11 -0800 Subject: [PATCH] support unix-pipes with bigger buffers for performance (#4472) --- .golangci.yml | 3 -- Makefile | 2 +- cmd/pipe-main.go | 10 +++++++ cmd/pipe_supported.go | 64 +++++++++++++++++++++++++++++++++++++++++ cmd/pipe_unsupported.go | 27 +++++++++++++++++ cmd/subnet-utils.go | 4 +-- 6 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 cmd/pipe_supported.go create mode 100644 cmd/pipe_unsupported.go diff --git a/.golangci.yml b/.golangci.yml index 3be18cbb..7f7b24b8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,12 +7,9 @@ linters: - govet - revive - ineffassign - - deadcode - - structcheck - gomodguard - gofmt - unused - - varcheck - gofumpt linters-settings: diff --git a/Makefile b/Makefile index 253855ad..a2b41c2a 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ checks: getdeps: @mkdir -p ${GOPATH}/bin - @echo "Installing golangci-lint" && curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.46.2 + @echo "Installing golangci-lint" && curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin @echo "Installing stringer" && go install -v golang.org/x/tools/cmd/stringer@latest @echo "Installing staticheck" && go install honnef.co/go/tools/cmd/staticcheck@latest diff --git a/cmd/pipe-main.go b/cmd/pipe-main.go index 7d805849..97f759da 100644 --- a/cmd/pipe-main.go +++ b/cmd/pipe-main.go @@ -61,6 +61,11 @@ var pipeFlags = []cli.Flag{ Value: defaultPartSize(), Usage: "customize chunk size for each concurrent upload", }, + cli.IntFlag{ + Name: "pipe-max-size", + Usage: "increase the pipe buffer size to a custom value", + Hidden: true, + }, } // Display contents of a file. @@ -109,6 +114,11 @@ EXAMPLES: } func pipe(ctx *cli.Context, targetURL string, encKeyDB map[string][]prefixSSEPair, meta map[string]string) *probe.Error { + // If possible increase the pipe buffer size + if e := increasePipeBufferSize(os.Stdin, ctx.Int("pipe-max-size")); e != nil { + fatalIf(probe.NewError(e), "Unable to increase custom pipe-max-size") + } + if targetURL == "" { // When no target is specified, pipe cat's stdin to stdout. return catOut(os.Stdin, -1).Trace() diff --git a/cmd/pipe_supported.go b/cmd/pipe_supported.go new file mode 100644 index 00000000..c7b89ad1 --- /dev/null +++ b/cmd/pipe_supported.go @@ -0,0 +1,64 @@ +//go:build linux + +// Copyright (c) 2015-2023 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "fmt" + "os" + "strconv" + "strings" + + "golang.org/x/sys/unix" +) + +const pipeMaxSizeProcFile = "/proc/sys/fs/pipe-max-size" + +func setPipeSize(fd uintptr, size int) error { + _, err := unix.FcntlInt(fd, unix.F_SETPIPE_SZ, size) + return err +} + +func getConfiguredMaxPipeSize() (int, error) { + b, err := os.ReadFile(pipeMaxSizeProcFile) + if err != nil { + return 0, err + } + maxSize, err := strconv.ParseInt(strings.TrimSpace(string(b)), 10, 64) + if err != nil { + return 0, fmt.Errorf("error parsing %s content: %v", pipeMaxSizeProcFile, err) + } + return int(maxSize), nil +} + +// increasePipeBufferSize attempts to increase the pipe size to the the input value +// or system-max, if the provided size is 0 or less. +func increasePipeBufferSize(f *os.File, desiredPipeSize int) error { + fd := f.Fd() + + if desiredPipeSize <= 0 { + maxSize, err := getConfiguredMaxPipeSize() + if err == nil { + setPipeSize(fd, maxSize) + return nil + } + } + + return setPipeSize(fd, desiredPipeSize) +} diff --git a/cmd/pipe_unsupported.go b/cmd/pipe_unsupported.go new file mode 100644 index 00000000..e6b3e93d --- /dev/null +++ b/cmd/pipe_unsupported.go @@ -0,0 +1,27 @@ +//go:build !linux + +// Copyright (c) 2015-2023 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import "os" + +func increasePipeBufferSize(f *os.File, desiredPipeSize int) error { + // this is not supported on non-Linux platforms. + return nil +} diff --git a/cmd/subnet-utils.go b/cmd/subnet-utils.go index 0f4c4c07..3c679a42 100644 --- a/cmd/subnet-utils.go +++ b/cmd/subnet-utils.go @@ -54,12 +54,12 @@ var ( MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEaK31xujr6/rZ7ZfXZh3SlwovjC+X8wGq qkltaKyTLRENd4w3IRktYYCRgzpDLPn/nrf7snV/ERO5qcI7fkEES34IVEr+2Uff JkO2PfyyAYEO/5dBlPh1Undu9WQl6J7B ------END PUBLIC KEY-----` // https://subnet.min.io/downloads/license-pubkey.pem +-----END PUBLIC KEY-----` // https://subnet.min.io/downloads/license-pubkey.pem subnetPublicKeyDev = `-----BEGIN PUBLIC KEY----- MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEbo+e1wpBY4tBq9AONKww3Kq7m6QP/TBQ mr/cKCUyBL7rcAvg0zNq1vcSrUSGlAmY3SEDCu3GOKnjG/U4E7+p957ocWSV+mQU 9NKlTdQFGF3+aO6jbQ4hX/S5qPyF+a3z ------END PUBLIC KEY-----` // https://localhost:9000/downloads/license-pubkey.pem +-----END PUBLIC KEY-----` // https://localhost:9000/downloads/license-pubkey.pem subnetCommonFlags = []cli.Flag{ cli.BoolFlag{ Name: "airgap",