David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2014 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package utils |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "runtime" |
| 9 | ) |
| 10 | |
| 11 | var gomaxprocs = runtime.GOMAXPROCS |
| 12 | var 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. |
| 16 | func 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 | } |