-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
262 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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. | ||
|
||
//go:build linux | ||
|
||
package cgroup | ||
|
||
import ( | ||
"runtime" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetCgroupCPU(t *testing.T) { | ||
exit := make(chan struct{}) | ||
var wg sync.WaitGroup | ||
for i := 0; i < 10; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
for { | ||
select { | ||
case <-exit: | ||
return | ||
default: | ||
runtime.Gosched() | ||
} | ||
} | ||
}() | ||
} | ||
cpu, err := GetCgroupCPU() | ||
require.NoError(t, err) | ||
require.NotZero(t, cpu.Period) | ||
require.Less(t, int64(1), cpu.Period) | ||
close(exit) | ||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "cpu", | ||
srcs = ["cpu.go"], | ||
importpath = "github.com/pingcap/tidb/util/cpu", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//util/cgroup", | ||
"//util/mathutil", | ||
"@com_github_cloudfoundry_gosigar//:gosigar", | ||
"@com_github_pingcap_log//:log", | ||
"@org_uber_go_atomic//:atomic", | ||
"@org_uber_go_zap//:zap", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "cpu_test", | ||
srcs = ["cpu_test.go"], | ||
embed = [":cpu"], | ||
deps = ["@com_github_stretchr_testify//require"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// 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 cpu | ||
|
||
import ( | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"github.com/cloudfoundry/gosigar" | ||
"github.com/pingcap/log" | ||
"github.com/pingcap/tidb/util/cgroup" | ||
"github.com/pingcap/tidb/util/mathutil" | ||
"go.uber.org/atomic" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var cpuUsage atomic.Float64 | ||
|
||
// GetCPUUsage returns the cpu usage of the current process. | ||
func GetCPUUsage() float64 { | ||
return cpuUsage.Load() | ||
} | ||
|
||
// Observer is used to observe the cpu usage of the current process. | ||
type Observer struct { | ||
utime int64 | ||
stime int64 | ||
now int64 | ||
exit chan struct{} | ||
cpu mathutil.ExponentialMovingAverage | ||
wg sync.WaitGroup | ||
} | ||
|
||
// NewCPUObserver returns a cpu observer. | ||
func NewCPUObserver() *Observer { | ||
return &Observer{ | ||
exit: make(chan struct{}), | ||
now: time.Now().UnixNano(), | ||
cpu: *mathutil.NewExponentialMovingAverage(0.95, 10), | ||
} | ||
} | ||
|
||
// Start starts the cpu observer. | ||
func (c *Observer) Start() { | ||
ticker := time.NewTicker(100 * time.Millisecond) | ||
defer ticker.Stop() | ||
c.wg.Add(1) | ||
go func() { | ||
defer c.wg.Done() | ||
for { | ||
select { | ||
case <-ticker.C: | ||
curr := c.observe() | ||
c.cpu.Add(curr) | ||
cpuUsage.Store(c.cpu.Get()) | ||
case <-c.exit: | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
// Stop stops the cpu observer. | ||
func (c *Observer) Stop() { | ||
close(c.exit) | ||
c.wg.Wait() | ||
} | ||
|
||
func (c *Observer) observe() float64 { | ||
user, sys, err := getCPUTime() | ||
if err != nil { | ||
log.Error("getCPUTime", zap.Error(err)) | ||
} | ||
cgroupCPU, _ := cgroup.GetCgroupCPU() | ||
cpuShare := cgroupCPU.CPUShares() | ||
now := time.Now().UnixNano() | ||
dur := float64(now - c.now) | ||
utime := user * 1e6 | ||
stime := sys * 1e6 | ||
urate := float64(utime-c.utime) / dur | ||
srate := float64(stime-c.stime) / dur | ||
c.now = now | ||
c.utime = utime | ||
c.stime = stime | ||
return (srate + urate) / cpuShare | ||
} | ||
|
||
// getCPUTime returns the cumulative user/system time (in ms) since the process start. | ||
func getCPUTime() (userTimeMillis, sysTimeMillis int64, err error) { | ||
pid := os.Getpid() | ||
cpuTime := sigar.ProcTime{} | ||
if err := cpuTime.Get(pid); err != nil { | ||
return 0, 0, err | ||
} | ||
return int64(cpuTime.User), int64(cpuTime.Sys), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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 cpu | ||
|
||
import ( | ||
"runtime" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCPUValue(t *testing.T) { | ||
Observer := NewCPUObserver() | ||
Observer.Start() | ||
exit := make(chan struct{}) | ||
var wg sync.WaitGroup | ||
for i := 0; i < 10; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
for { | ||
select { | ||
case <-exit: | ||
return | ||
default: | ||
runtime.Gosched() | ||
} | ||
} | ||
}() | ||
} | ||
time.Sleep(30 * time.Second) | ||
require.Greater(t, Observer.observe(), 0.0) | ||
require.Less(t, Observer.observe(), 1.0) | ||
Observer.Stop() | ||
close(exit) | ||
wg.Wait() | ||
} |