Skip to content

chore: add automaxprocs to limit parallel processes inside containers #10594

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ func (r *RootCmd) workspaceAgent() *clibase.Cmd {
sinks = append(sinks, sloghuman.Sink(logWriter))
logger := slog.Make(sinks...).Leveled(slog.LevelDebug)

undoMacProcs, err := limitGoMaxProcs(logger)
if err != nil {
return xerrors.Errorf("set maxprocs: %w", err)
}
defer undoMacProcs()
Comment on lines +165 to +169
Copy link
Member

@mafredri mafredri Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
undoMacProcs, err := limitGoMaxProcs(logger)
if err != nil {
return xerrors.Errorf("set maxprocs: %w", err)
}
defer undoMacProcs()
undoMaxProcs, err := limitGoMaxProcs(logger)
if err != nil {
return xerrors.Errorf("set maxprocs: %w", err)
}
defer undoMaxProcs()

(PS. Also other instances of mac procs 😄)


version := buildinfo.Version()
logger.Info(ctx, "agent is starting now",
slog.F("url", r.agentURL),
Expand Down
14 changes: 14 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/mattn/go-isatty"
"github.com/mitchellh/go-wordwrap"
"go.uber.org/automaxprocs/maxprocs"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -1128,6 +1129,19 @@ func tailLineStyle() pretty.Style {
return pretty.Style{pretty.Nop}
}

// limitGoMaxProcs sets GOMAXPROCS to the number of CPUs available.
// This can improve the performance of Coder inside of a container.
func limitGoMaxProcs(logger slog.Logger) (func(), error) {
// See: https://github.com/uber-go/automaxprocs
undoMacProcs, err := maxprocs.Set(maxprocs.Logger(func(format string, args ...interface{}) {
logger.Debug(context.Background(), fmt.Sprintf(format, args...))
}))
if err != nil {
return nil, xerrors.Errorf("set maxprocs: %w", err)
}
return undoMacProcs, nil
}

//nolint:unused
func SlimUnsupported(w io.Writer, cmd string) {
_, _ = fmt.Fprintf(w, "You are using a 'slim' build of Coder, which does not support the %s subcommand.\n", pretty.Sprint(cliui.DefaultStyles.Code, cmd))
Expand Down
6 changes: 6 additions & 0 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
logger.Debug(ctx, "started debug logging")
logger.Sync()

undoMacProcs, err := limitGoMaxProcs(logger)
if err != nil {
return xerrors.Errorf("set maxprocs: %w", err)
}
defer undoMacProcs()

// Register signals early on so that graceful shutdown can't
// be interrupted by additional signals. Note that we avoid
// shadowing cancel() (from above) here because notifyStop()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ require (
go.opentelemetry.io/contrib v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
go4.org/intern v0.0.0-20230525184215-6c62f75575cb // indirect
go4.org/mem v0.0.0-20220726221520-4f986261bf13 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20230525183740-e7c30c78aeb2 // indirect
Expand Down
Loading