blob: 5977a864bc644cc9fb7aa10637ab16a8d4f60b87 [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2014 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package utils
5
6import (
7 "os"
8 "runtime"
9)
10
11var gomaxprocs = runtime.GOMAXPROCS
12var numCPU = runtime.NumCPU
13
14// UseMultipleCPUs sets GOMAXPROCS to the number of CPU cores unless it has
15// already been overridden by the GOMAXPROCS environment variable.
16func UseMultipleCPUs() {
17 if envGOMAXPROCS := os.Getenv("GOMAXPROCS"); envGOMAXPROCS != "" {
18 n := gomaxprocs(0)
19 logger.Debugf("GOMAXPROCS already set in environment to %q, %d internally",
20 envGOMAXPROCS, n)
21 return
22 }
23 n := numCPU()
24 logger.Debugf("setting GOMAXPROCS to %d", n)
25 gomaxprocs(n)
26}