From 98da370173cb7ce5b408deac20ea0c68282796db Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Wed, 23 Aug 2017 19:35:06 +0200 Subject: [PATCH] server: set golang runtime max threads SetMaxThreads from runtime/debug in Golang is called to set max threads value to 90% of /proc/sys/kernel/threads-max Should really help performance. Signed-off-by: Antonio Murdaca --- server/server.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/server/server.go b/server/server.go index 9a812e78..a5699b2c 100644 --- a/server/server.go +++ b/server/server.go @@ -8,6 +8,9 @@ import ( "net/http" "os" "path/filepath" + "runtime/debug" + "strconv" + "strings" "sync" "github.com/fsnotify/fsnotify" @@ -145,6 +148,23 @@ func (s *Server) Shutdown() error { return s.ContainerServer.Shutdown() } +// configureMaxThreads sets the Go runtime max threads threshold +// which is 90% of the kernel setting from /proc/sys/kernel/threads-max +func configureMaxThreads() error { + mt, err := ioutil.ReadFile("/proc/sys/kernel/threads-max") + if err != nil { + return err + } + mtint, err := strconv.Atoi(strings.TrimSpace(string(mt))) + if err != nil { + return err + } + maxThreads := (mtint / 100) * 90 + debug.SetMaxThreads(maxThreads) + logrus.Debugf("Golang's threads limit set to %d", maxThreads) + return nil +} + // New creates a new Server with options provided func New(config *Config) (*Server, error) { if err := os.MkdirAll("/var/run/crio", 0755); err != nil { @@ -199,6 +219,10 @@ func New(config *Config) (*Server, error) { } } + if err := configureMaxThreads(); err != nil { + return nil, err + } + s.restore() s.cleanupSandboxesOnShutdown()