This is an automated email from the ASF dual-hosted git repository.

hanahmily pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-banyandb.git


The following commit(s) were added to refs/heads/main by this push:
     new 9f423a82  Read cpu quota and limit from the cgroup file system to set 
gomaxprocs (#586)
9f423a82 is described below

commit 9f423a826602b91d305e62f41ae1d6881ab015c9
Author: Gao Hongtao <[email protected]>
AuthorDate: Mon Dec 30 13:52:07 2024 +0800

     Read cpu quota and limit from the cgroup file system to set gomaxprocs 
(#586)
    
    Signed-off-by: Gao Hongtao <[email protected]>
---
 CHANGES.md                                         |  1 +
 banyand/cmd/server/main.go                         |  2 --
 dist/LICENSE                                       |  1 +
 dist/licenses/license-go.uber.org-automaxprocs.txt | 19 +++++++++++
 go.mod                                             |  1 +
 go.sum                                             |  4 +++
 banyand/cmd/server/main.go => pkg/cgroups/cpu.go   | 37 ++++++++++++++++------
 pkg/cmdsetup/root.go                               |  9 +++++-
 pkg/schema/cache.go                                |  4 +--
 9 files changed, 63 insertions(+), 15 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index b77f1dd7..17dc179d 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -15,6 +15,7 @@ Release Notes.
 - Index Mode: Index `measure_nam` and `tags` in `entity` to improve the query 
performance.
 - Encoding: Improve the performance of encoding and decoding the 
variable-length int64.
 - Index: Add a cache to improve the performance of the series index write.
+- Read cpu quota and limit from the cgroup file system to set gomaxprocs.
 
 ### Bug Fixes
 
diff --git a/banyand/cmd/server/main.go b/banyand/cmd/server/main.go
index 3d7b6d77..99827d13 100644
--- a/banyand/cmd/server/main.go
+++ b/banyand/cmd/server/main.go
@@ -21,14 +21,12 @@ package main
 import (
        "fmt"
        "os"
-       "runtime"
 
        "github.com/apache/skywalking-banyandb/pkg/cmdsetup"
        "github.com/apache/skywalking-banyandb/pkg/signal"
 )
 
 func main() {
-       runtime.GOMAXPROCS(runtime.NumCPU() * 2)
        if err := cmdsetup.NewRoot(new(signal.Handler)).Execute(); err != nil {
                _, _ = fmt.Fprintln(os.Stderr, err)
                os.Exit(1)
diff --git a/dist/LICENSE b/dist/LICENSE
index 10c42934..df6478e5 100644
--- a/dist/LICENSE
+++ b/dist/LICENSE
@@ -354,6 +354,7 @@ MIT licenses
     github.com/yusufpapurcu/wmi v1.2.4 MIT
     github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04 MIT
     go.etcd.io/bbolt v1.3.11 MIT
+    go.uber.org/automaxprocs v1.6.0 MIT
     go.uber.org/multierr v1.11.0 MIT
     go.uber.org/zap v1.27.0 MIT
     gopkg.in/natefinch/lumberjack.v2 v2.2.1 MIT
diff --git a/dist/licenses/license-go.uber.org-automaxprocs.txt 
b/dist/licenses/license-go.uber.org-automaxprocs.txt
new file mode 100644
index 00000000..20dcf51d
--- /dev/null
+++ b/dist/licenses/license-go.uber.org-automaxprocs.txt
@@ -0,0 +1,19 @@
+Copyright (c) 2017 Uber Technologies, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
\ No newline at end of file
diff --git a/go.mod b/go.mod
index d8851bcf..cecdc0b8 100644
--- a/go.mod
+++ b/go.mod
@@ -36,6 +36,7 @@ require (
        github.com/zenizh/go-capturer v0.0.0-20211219060012-52ea6c8fed04
        go.etcd.io/etcd/client/v3 v3.5.17
        go.etcd.io/etcd/server/v3 v3.5.17
+       go.uber.org/automaxprocs v1.6.0
        go.uber.org/mock v0.5.0
        go.uber.org/multierr v1.11.0
        golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67
diff --git a/go.sum b/go.sum
index 3e801d93..e1b3faa1 100644
--- a/go.sum
+++ b/go.sum
@@ -234,6 +234,8 @@ github.com/pmezard/go-difflib 
v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
 github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod 
h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 
h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
 github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod 
h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
+github.com/prashantv/gostub v1.1.0 
h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
+github.com/prashantv/gostub v1.1.0/go.mod 
h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
 github.com/prometheus/client_golang v1.20.5 
h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
 github.com/prometheus/client_golang v1.20.5/go.mod 
h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
 github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod 
h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
@@ -355,6 +357,8 @@ go.opentelemetry.io/proto/otlp v0.7.0/go.mod 
h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqe
 go.opentelemetry.io/proto/otlp v1.4.0 
h1:TA9WRvW6zMwP+Ssb6fLoUIuirti1gGbP28GcKG1jgeg=
 go.opentelemetry.io/proto/otlp v1.4.0/go.mod 
h1:PPBWZIP98o2ElSqI35IHfu7hIhSwvc5N38Jw8pXuGFY=
 go.uber.org/atomic v1.7.0/go.mod 
h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
+go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
+go.uber.org/automaxprocs v1.6.0/go.mod 
h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
 go.uber.org/goleak v1.1.10/go.mod 
h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
 go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
 go.uber.org/goleak v1.3.0/go.mod 
h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
diff --git a/banyand/cmd/server/main.go b/pkg/cgroups/cpu.go
similarity index 53%
copy from banyand/cmd/server/main.go
copy to pkg/cgroups/cpu.go
index 3d7b6d77..e4b70b03 100644
--- a/banyand/cmd/server/main.go
+++ b/pkg/cgroups/cpu.go
@@ -15,22 +15,39 @@
 // specific language governing permissions and limitations
 // under the License.
 
-// Package main implements the executable banyandb server named banyand.
-package main
+// Package cgroups provides a way to get the number of CPUs.
+package cgroups
 
 import (
-       "fmt"
        "os"
        "runtime"
 
-       "github.com/apache/skywalking-banyandb/pkg/cmdsetup"
-       "github.com/apache/skywalking-banyandb/pkg/signal"
+       "go.uber.org/automaxprocs/maxprocs"
+
+       "github.com/apache/skywalking-banyandb/pkg/logger"
 )
 
-func main() {
-       runtime.GOMAXPROCS(runtime.NumCPU() * 2)
-       if err := cmdsetup.NewRoot(new(signal.Handler)).Execute(); err != nil {
-               _, _ = fmt.Fprintln(os.Stderr, err)
-               os.Exit(1)
+// CPUs returns the number of CPUs.
+func CPUs() int {
+       return runtime.GOMAXPROCS(-1)
+}
+
+func init() {
+       if maxProcs, exists := os.LookupEnv("GOMAXPROCS"); exists {
+               logger.Infof("honoring GOMAXPROCS=%q as set in environment", 
maxProcs)
+               return
+       }
+       _, err := maxprocs.Set(maxprocs.Logger(logger.Infof))
+       if err != nil {
+               logger.Warningf("failed to set GOMAXPROCS: %v", err)
+       }
+       gomaxprocs := runtime.GOMAXPROCS(-1)
+       if gomaxprocs <= 0 {
+               gomaxprocs = 1
+       }
+       numCPU := runtime.NumCPU()
+       if gomaxprocs > numCPU {
+               gomaxprocs = numCPU
        }
+       runtime.GOMAXPROCS(gomaxprocs)
 }
diff --git a/pkg/cmdsetup/root.go b/pkg/cmdsetup/root.go
index c767ce20..02e1eb4d 100644
--- a/pkg/cmdsetup/root.go
+++ b/pkg/cmdsetup/root.go
@@ -24,6 +24,7 @@ import (
        "github.com/spf13/cobra"
 
        "github.com/apache/skywalking-banyandb/api/common"
+       "github.com/apache/skywalking-banyandb/pkg/cgroups"
        "github.com/apache/skywalking-banyandb/pkg/config"
        "github.com/apache/skywalking-banyandb/pkg/logger"
        "github.com/apache/skywalking-banyandb/pkg/run"
@@ -54,7 +55,13 @@ BanyanDB, as an observability database, aims to ingest, 
analyze and store Metric
                        if err = config.Load("logging", cmd.Flags()); err != 
nil {
                                return err
                        }
-                       return logger.Init(logging)
+
+                       if err = logger.Init(logging); err != nil {
+                               return err
+                       }
+
+                       logger.Infof("CPU Number: %d", cgroups.CPUs())
+                       return nil
                },
        }
        
cmd.PersistentFlags().Var(&nodeIDProviderValue{&common.FlagNodeHostProvider},
diff --git a/pkg/schema/cache.go b/pkg/schema/cache.go
index 3b7e34ac..05c59d81 100644
--- a/pkg/schema/cache.go
+++ b/pkg/schema/cache.go
@@ -21,7 +21,6 @@ import (
        "context"
        "io"
        "path"
-       "runtime"
        "sync"
        "sync/atomic"
        "time"
@@ -34,6 +33,7 @@ import (
        databasev1 
"github.com/apache/skywalking-banyandb/api/proto/banyandb/database/v1"
        "github.com/apache/skywalking-banyandb/banyand/metadata"
        "github.com/apache/skywalking-banyandb/banyand/metadata/schema"
+       "github.com/apache/skywalking-banyandb/pkg/cgroups"
        "github.com/apache/skywalking-banyandb/pkg/logger"
        "github.com/apache/skywalking-banyandb/pkg/run"
 )
@@ -93,7 +93,7 @@ func (rs *resourceSpec) isNewThan(other *resourceSpec) bool {
 const maxWorkerNum = 8
 
 func getWorkerNum() int {
-       maxProcs := runtime.GOMAXPROCS(-1)
+       maxProcs := cgroups.CPUs()
        if maxProcs > maxWorkerNum {
                return maxWorkerNum
        }

Reply via email to