Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: reduce STW from ReadMemStats #38632

Merged
merged 23 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions br/pkg/glue/BUILD.bazel
Expand Up @@ -5,21 +5,22 @@ go_library(
srcs = [
"console_glue.go",
"glue.go",
"progressing.go",
],
importpath = "github.com/pingcap/tidb/br/pkg/glue",
visibility = ["//visibility:public"],
deps = [
"//br/pkg/logutil",
"//br/pkg/utils",
"//ddl",
"//domain",
"//kv",
"//parser/model",
"//sessionctx",
"@com_github_fatih_color//:color",
"@com_github_pingcap_log//:log",
"@com_github_tikv_pd_client//:client",
"@com_github_vbauerster_mpb_v7//:mpb",
"@com_github_vbauerster_mpb_v7//decor",
"@org_golang_x_term//:term",
"@org_uber_go_zap//:zap",
],
)

Expand Down
1 change: 1 addition & 0 deletions domain/BUILD.bazel
Expand Up @@ -52,6 +52,7 @@ go_library(
"//util/execdetails",
"//util/expensivequery",
"//util/logutil",
"//util/memory",
"//util/memoryusagealarm",
"//util/servermemorylimit",
"//util/sqlexec",
Expand Down
6 changes: 6 additions & 0 deletions domain/domain.go
Expand Up @@ -64,6 +64,7 @@ import (
"github.com/pingcap/tidb/util/engine"
"github.com/pingcap/tidb/util/expensivequery"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/memoryusagealarm"
"github.com/pingcap/tidb/util/servermemorylimit"
"github.com/pingcap/tidb/util/sqlexec"
Expand Down Expand Up @@ -1763,13 +1764,15 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager)
dumpFeedbackTicker := time.NewTicker(200 * lease)
loadFeedbackTicker := time.NewTicker(5 * lease)
dumpColStatsUsageTicker := time.NewTicker(100 * lease)
readMemTricker := time.NewTicker(memory.ReadMemInterval)
statsHandle := do.StatsHandle()
defer func() {
dumpColStatsUsageTicker.Stop()
loadFeedbackTicker.Stop()
dumpFeedbackTicker.Stop()
gcStatsTicker.Stop()
deltaUpdateTicker.Stop()
readMemTricker.Stop()
do.SetStatsUpdating(false)
logutil.BgLogger().Info("updateStatsWorker exited.")
}()
Expand Down Expand Up @@ -1818,6 +1821,9 @@ func (do *Domain) updateStatsWorker(ctx sessionctx.Context, owner owner.Manager)
if err != nil {
logutil.BgLogger().Debug("dump column stats usage failed", zap.Error(err))
}

case <-readMemTricker.C:
memory.ForceReadMemStats()
}
}
}
Expand Down
1 change: 1 addition & 0 deletions executor/BUILD.bazel
Expand Up @@ -16,6 +16,7 @@ go_library(
"analyze_idx.go",
"analyze_incremental.go",
"analyze_utils.go",
"analyze_worker.go",
"apply_cache.go",
"batch_checker.go",
"batch_point_get.go",
Expand Down
3 changes: 1 addition & 2 deletions executor/explain.go
Expand Up @@ -169,8 +169,7 @@ func (h *memoryDebugModeHandler) fetchCurrentMemoryUsage(gc bool) (heapInUse, tr
if gc {
runtime.GC()
}
instanceStats := &runtime.MemStats{}
runtime.ReadMemStats(instanceStats)
instanceStats := memory.ForceReadMemStats()
heapInUse = instanceStats.HeapInuse
trackedMem = uint64(h.memTracker.BytesConsumed())
return
Expand Down
1 change: 1 addition & 0 deletions session/BUILD.bazel
Expand Up @@ -127,6 +127,7 @@ go_test(
deps = [
"//bindinfo",
"//config",
"//ddl",
"//domain",
"//errno",
"//executor",
Expand Down
6 changes: 2 additions & 4 deletions util/gctuner/mem.go
Expand Up @@ -15,12 +15,10 @@
package gctuner

import (
"runtime"
"github.com/pingcap/tidb/util/memory"
)

var memStats runtime.MemStats

func readMemoryInuse() uint64 {
runtime.ReadMemStats(&memStats)
memStats := memory.ForceReadMemStats()
return memStats.HeapInuse
}
4 changes: 1 addition & 3 deletions util/gctuner/memory_limit_tuner.go
Expand Up @@ -16,7 +16,6 @@ package gctuner

import (
"math"
"runtime"
"runtime/debug"
"time"

Expand Down Expand Up @@ -45,8 +44,7 @@ func (t *memoryLimitTuner) tuning() {
if !t.isTuning.Load() {
return
}
r := &runtime.MemStats{}
runtime.ReadMemStats(r)
r := memory.ForceReadMemStats()
wshwsh12 marked this conversation as resolved.
Show resolved Hide resolved
gogc := util.GetGOGC()
ratio := float64(100+gogc) / 100
// This `if` checks whether the **last** GC was triggered by MemoryLimit as far as possible.
Expand Down
1 change: 1 addition & 0 deletions util/memory/BUILD.bazel
Expand Up @@ -5,6 +5,7 @@ go_library(
srcs = [
"action.go",
"meminfo.go",
"memstats.go",
"tracker.go",
],
importpath = "github.com/pingcap/tidb/util/memory",
Expand Down
4 changes: 1 addition & 3 deletions util/memory/meminfo.go
Expand Up @@ -15,7 +15,6 @@
package memory

import (
"runtime"
"sync"
"time"

Expand Down Expand Up @@ -153,8 +152,7 @@ func InstanceMemUsed() (uint64, error) {
return used, nil
}
var memoryUsage uint64
instanceStats := &runtime.MemStats{}
runtime.ReadMemStats(instanceStats)
instanceStats := ReadMemStats()
memoryUsage = instanceStats.HeapAlloc
serverMemUsage.set(memoryUsage, time.Now())
return memoryUsage, nil
Expand Down
49 changes: 49 additions & 0 deletions util/memory/memstats.go
@@ -0,0 +1,49 @@
// Copyright 2018 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 memory

import (
"runtime"
"sync/atomic"
"time"
)

var stats atomic.Pointer[globalMstats]

// ReadMemInterval controls the interval to read memory stats.
const ReadMemInterval = 300 * time.Millisecond

// ReadMemStats read the mem stats from runtime.ReadMemStats
func ReadMemStats() *runtime.MemStats {
s := stats.Load()
if s != nil {
return &s.m
}
return ForceReadMemStats()
}

// ForceReadMemStats is to force read memory stats.
func ForceReadMemStats() *runtime.MemStats {
var g globalMstats
g.ts = time.Now()
runtime.ReadMemStats(&g.m)
stats.Store(&g)
return &g.m
}

type globalMstats struct {
ts time.Time
m runtime.MemStats
}
4 changes: 1 addition & 3 deletions util/memoryusagealarm/memoryusagealarm.go
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
rpprof "runtime/pprof"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -144,8 +143,7 @@ func (record *memoryUsageAlarm) alarm4ExcessiveMemUsage(sm util.SessionManager)
return
}
var memoryUsage uint64
instanceStats := &runtime.MemStats{}
runtime.ReadMemStats(instanceStats)
instanceStats := memory.ReadMemStats()
if record.isServerMemoryLimitSet {
memoryUsage = instanceStats.HeapAlloc
} else {
Expand Down
3 changes: 1 addition & 2 deletions util/servermemorylimit/servermemorylimit.go
Expand Up @@ -83,8 +83,7 @@ func killSessIfNeeded(s *sessionToBeKilled, bt uint64, sm util.SessionManager) {
if bt == 0 {
return
}
instanceStats := &runtime.MemStats{}
runtime.ReadMemStats(instanceStats)
instanceStats := memory.ReadMemStats()
if instanceStats.HeapInuse > bt {
t := memory.MemUsageTop1Tracker.Load()
if t != nil {
Expand Down