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

release-20.2: backport cgroup related changes to improve CPU and memory accounting #59184

Merged
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
6 changes: 6 additions & 0 deletions pkg/cli/start.go
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/cgroups"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/grpcutil"
Expand Down Expand Up @@ -354,6 +355,11 @@ func runStart(cmd *cobra.Command, args []string, disableReplication bool) (retur
// but when actually starting a server, we enable them.
grpcutil.SetSeverity(log.Severity_WARNING)

// Tweak GOMAXPROCS if we're in a cgroup / container that has cpu limits set.
// The GO default for GOMAXPROCS is runtime.NumCPU(), however this is less
// than ideal if the cgruop is limited to a number lower than that.
cgroups.AdjustMaxProcs(ctx)

// Check the --join flag.
if !flagSetForCmd(cmd).Lookup(cliflags.Join.Name).Changed {
err := errors.WithHint(
Expand Down
5 changes: 4 additions & 1 deletion pkg/server/status/runtime.go
Expand Up @@ -19,6 +19,7 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/util/cgroups"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
Expand Down Expand Up @@ -440,6 +441,8 @@ func (rsr *RuntimeStatSampler) SampleEnvironment(
if err := cpuTime.Get(pid); err != nil {
log.Errorf(ctx, "unable to get cpu usage: %v", err)
}
cgroupCPU, _ := cgroups.GetCgroupCPU()
cpuShare := cgroupCPU.CPUShares()

fds := gosigar.ProcFDUsage{}
if err := fds.Get(pid); err != nil {
Expand Down Expand Up @@ -500,7 +503,7 @@ func (rsr *RuntimeStatSampler) SampleEnvironment(
stime := int64(cpuTime.Sys) * 1e6
uPerc := float64(utime-rsr.last.utime) / dur
sPerc := float64(stime-rsr.last.stime) / dur
combinedNormalizedPerc := (sPerc + uPerc) / float64(runtime.NumCPU())
combinedNormalizedPerc := (sPerc + uPerc) / cpuShare
gcPausePercent := float64(uint64(gc.PauseTotal)-rsr.last.gcPauseTime) / dur
rsr.last.now = now
rsr.last.utime = utime
Expand Down
1 change: 1 addition & 0 deletions pkg/testutils/lint/lint_test.go
Expand Up @@ -446,6 +446,7 @@ func TestLint(t *testing.T) {
":!ccl/workloadccl/fixture_test.go",
":!internal/gopath/gopath.go",
":!cmd",
":!util/cgroups/cgroups.go",
":!nightly",
":!testutils/lint",
":!util/envutil/env.go",
Expand Down