From 3491770489fe8c725c72a5f74cdbf3c4b761748d Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 6 Dec 2022 11:27:11 +0800 Subject: [PATCH] resourcemanger: create cpu/memory monitor Signed-off-by: Weizhen Wang --- br/pkg/lightning/common/BUILD.bazel | 44 ++++++++++++- br/pkg/lightning/web/BUILD.bazel | 1 - build/nogo_config.json | 6 +- domain/domain.go | 2 - resourcemanager/BUILD.bazel | 13 ++++ resourcemanager/rm.go | 65 +++++++++++++++++++ .../featuretag/concurrencyddl/BUILD.bazel | 5 +- .../featuretag/distributereorg/BUILD.bazel | 5 +- tidb-server/BUILD.bazel | 1 + tidb-server/main.go | 3 + util/signal/BUILD.bazel | 31 +++++++-- util/sys/linux/BUILD.bazel | 43 +++++++++++- 12 files changed, 199 insertions(+), 20 deletions(-) create mode 100644 resourcemanager/BUILD.bazel create mode 100644 resourcemanager/rm.go diff --git a/br/pkg/lightning/common/BUILD.bazel b/br/pkg/lightning/common/BUILD.bazel index a5245356b2715..880186fe1d700 100644 --- a/br/pkg/lightning/common/BUILD.bazel +++ b/br/pkg/lightning/common/BUILD.bazel @@ -34,9 +34,49 @@ go_library( "@org_golang_google_grpc//codes", "@org_golang_google_grpc//credentials", "@org_golang_google_grpc//status", - "@org_golang_x_sys//unix", "@org_uber_go_zap//:zap", - ], + ] + select({ + "@io_bazel_rules_go//go/platform:aix": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:android": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:darwin": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:dragonfly": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:freebsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:illumos": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:ios": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:js": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:linux": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:netbsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:openbsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:plan9": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:solaris": [ + "@org_golang_x_sys//unix", + ], + "//conditions:default": [], + }), ) go_test( diff --git a/br/pkg/lightning/web/BUILD.bazel b/br/pkg/lightning/web/BUILD.bazel index 842eb48fb3dd3..30dbd2437697b 100644 --- a/br/pkg/lightning/web/BUILD.bazel +++ b/br/pkg/lightning/web/BUILD.bazel @@ -4,7 +4,6 @@ go_library( name = "web", srcs = [ "progress.go", - "res.go", "res_vfsdata.go", ], importpath = "github.com/pingcap/tidb/br/pkg/lightning/web", diff --git a/build/nogo_config.json b/build/nogo_config.json index ce3715232b472..3aecc81b0519e 100644 --- a/build/nogo_config.json +++ b/build/nogo_config.json @@ -404,7 +404,8 @@ "util/": "util code", "parser/": "parser code", "meta/": "parser code", - "extension/": "extension code" + "extension/": "extension code", + "resourcemanager/": "resourcemanager code" } }, "shift": { @@ -765,7 +766,8 @@ "server/conn.go": "server/conn.go", "server/conn_stmt.go": "server/conn_stmt.go", "server/conn_test.go": "server/conn_test.go", - "extension/": "extension code" + "extension/": "extension code", + "resourcemanager/": "resourcemanager code" } }, "SA2000": { diff --git a/domain/domain.go b/domain/domain.go index 82963ed0cebdc..aafc366a47799 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -2016,7 +2016,6 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) loadFeedbackTicker := time.NewTicker(5 * lease) loadLockedTablesTicker := time.NewTicker(5 * lease) dumpColStatsUsageTicker := time.NewTicker(100 * lease) - readMemTricker := time.NewTicker(memory.ReadMemInterval) statsHandle := do.StatsHandle() defer func() { dumpColStatsUsageTicker.Stop() @@ -2024,7 +2023,6 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager) dumpFeedbackTicker.Stop() gcStatsTicker.Stop() deltaUpdateTicker.Stop() - readMemTricker.Stop() do.SetStatsUpdating(false) logutil.BgLogger().Info("updateStatsWorker exited.") }() diff --git a/resourcemanager/BUILD.bazel b/resourcemanager/BUILD.bazel new file mode 100644 index 0000000000000..aceb236e5a9d0 --- /dev/null +++ b/resourcemanager/BUILD.bazel @@ -0,0 +1,13 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "resourcemanage", + srcs = ["rm.go"], + importpath = "github.com/pingcap/tidb/resourcemanager", + visibility = ["//visibility:public"], + deps = [ + "//util", + "//util/cpu", + "//util/memory", + ], +) diff --git a/resourcemanager/rm.go b/resourcemanager/rm.go new file mode 100644 index 0000000000000..41cac9cba85e7 --- /dev/null +++ b/resourcemanager/rm.go @@ -0,0 +1,65 @@ +// Copyright 2022 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package resourcemanager + +import ( + "time" + + tidbutil "github.com/pingcap/tidb/util" + "github.com/pingcap/tidb/util/cpu" + "github.com/pingcap/tidb/util/memory" +) + +// GlobalResourceManager is a global resource manager +var GlobalResourceManager = NewResourceManger() + +// ResourceManager is a resource manager +type ResourceManager struct { + cpuObserver *cpu.Observer + exitCh chan struct{} + wg tidbutil.WaitGroupWrapper +} + +// NewResourceManger is to create a new resource manager +func NewResourceManger() *ResourceManager { + return &ResourceManager{ + cpuObserver: cpu.NewCPUObserver(), + exitCh: make(chan struct{}), + } +} + +// Start is to start resource manager +func (r *ResourceManager) Start() { + r.wg.Run(r.cpuObserver.Start) + r.wg.Run(func() { + readMemTricker := time.NewTicker(memory.ReadMemInterval) + defer readMemTricker.Stop() + for { + select { + case <-readMemTricker.C: + memory.ForceReadMemStats() + case <-r.exitCh: + return + } + } + }) +} + +// Stop is to stop resource manager +func (r *ResourceManager) Stop() { + r.cpuObserver.Stop() + close(r.exitCh) + r.wg.Done() +} diff --git a/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel b/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel index 44c1cede3c2b7..37ac17a1804ec 100644 --- a/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel +++ b/sessionctx/variable/featuretag/concurrencyddl/BUILD.bazel @@ -2,10 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "concurrencyddl", - srcs = [ - "default.go", - "non_default.go", - ], + srcs = ["default.go"], importpath = "github.com/pingcap/tidb/sessionctx/variable/featuretag/concurrencyddl", visibility = ["//visibility:public"], ) diff --git a/sessionctx/variable/featuretag/distributereorg/BUILD.bazel b/sessionctx/variable/featuretag/distributereorg/BUILD.bazel index 153ce052ecbb2..fb3bfb4835c1a 100644 --- a/sessionctx/variable/featuretag/distributereorg/BUILD.bazel +++ b/sessionctx/variable/featuretag/distributereorg/BUILD.bazel @@ -2,10 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "distributereorg", - srcs = [ - "default.go", - "non_default.go", - ], + srcs = ["default.go"], importpath = "github.com/pingcap/tidb/sessionctx/variable/featuretag/distributereorg", visibility = ["//visibility:public"], ) diff --git a/tidb-server/BUILD.bazel b/tidb-server/BUILD.bazel index 960f68a2b597d..493eedfcccbfa 100644 --- a/tidb-server/BUILD.bazel +++ b/tidb-server/BUILD.bazel @@ -21,6 +21,7 @@ go_library( "//planner/core", "//plugin", "//privilege/privileges", + "//resourcemanager:resourcemanage", "//server", "//session", "//session/txninfo", diff --git a/tidb-server/main.go b/tidb-server/main.go index a15d102f9509e..d58e24a531f4c 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -46,6 +46,7 @@ import ( plannercore "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/plugin" "github.com/pingcap/tidb/privilege/privileges" + "github.com/pingcap/tidb/resourcemanager" "github.com/pingcap/tidb/server" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/session/txninfo" @@ -210,6 +211,7 @@ func main() { printInfo() setupBinlogClient() setupMetrics() + resourcemanager.GlobalResourceManager.Start() storage, dom := createStoreAndDomain() svr := createServer(storage, dom) @@ -223,6 +225,7 @@ func main() { svr.Close() cleanup(svr, storage, dom, graceful) cpuprofile.StopCPUProfiler() + resourcemanager.GlobalResourceManager.Stop() close(exited) }) topsql.SetupTopSQL() diff --git a/util/signal/BUILD.bazel b/util/signal/BUILD.bazel index af19da427497c..e2963d6f554bb 100644 --- a/util/signal/BUILD.bazel +++ b/util/signal/BUILD.bazel @@ -9,8 +9,31 @@ go_library( ], importpath = "github.com/pingcap/tidb/util/signal", visibility = ["//visibility:public"], - deps = [ - "//util/logutil", - "@org_uber_go_zap//:zap", - ], + deps = select({ + "@io_bazel_rules_go//go/platform:android": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "@io_bazel_rules_go//go/platform:darwin": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "@io_bazel_rules_go//go/platform:freebsd": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "@io_bazel_rules_go//go/platform:ios": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "@io_bazel_rules_go//go/platform:linux": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "@io_bazel_rules_go//go/platform:windows": [ + "//util/logutil", + "@org_uber_go_zap//:zap", + ], + "//conditions:default": [], + }), ) diff --git a/util/sys/linux/BUILD.bazel b/util/sys/linux/BUILD.bazel index 1212afe2fc3d9..f1363c2cb0d71 100644 --- a/util/sys/linux/BUILD.bazel +++ b/util/sys/linux/BUILD.bazel @@ -9,7 +9,48 @@ go_library( ], importpath = "github.com/pingcap/tidb/util/sys/linux", visibility = ["//visibility:public"], - deps = ["@org_golang_x_sys//unix"], + deps = select({ + "@io_bazel_rules_go//go/platform:aix": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:android": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:darwin": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:dragonfly": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:freebsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:illumos": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:ios": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:js": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:linux": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:netbsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:openbsd": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:plan9": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:solaris": [ + "@org_golang_x_sys//unix", + ], + "//conditions:default": [], + }), ) go_test(