Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(264)

Delta Between Two Patch Sets: utils/gomaxprocs.go

Issue 76120044: cmd/juju/machine: Enable GOMAXPROCS(NumCPU)
Left Patch Set: Created 11 years ago
Right Patch Set: cmd/juju/machine: Enable GOMAXPROCS(NumCPU) Created 11 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « utils/export_test.go ('k') | utils/gomaxprocs_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2014 Canonical Ltd. 1 // Copyright 2014 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 package utils 4 package utils
5 5
6 import ( 6 import (
7 "os" 7 "os"
8 "runtime" 8 "runtime"
9 "sync"
10 ) 9 )
11 10
12 var mu = sync.Mutex{}
rog 2014/03/17 10:02:16 This does not fill me with joy. If our tests reall
jameinel 2014/03/19 09:45:27 Done.
13 var gomaxprocs = runtime.GOMAXPROCS 11 var gomaxprocs = runtime.GOMAXPROCS
14 var numcpu = runtime.NumCPU 12 var numcpu = runtime.NumCPU
rog 2014/03/20 08:43:06 Trivial point that I forgot to mention before: we'
15 var enabledMultiCPUs = false
16 13
17 // OverrideGOMAXPROCSFuncs allows you to override calling runtime.GOMAXPROCS 14 // UseMultipleCPUs sets GOMAXPROCS to the number of CPU cores unless it has
18 // and runtime.NumCPU. This is exposed so that tests can poke at this without 15 // already been overridden by the GOMAXPROCS environment variable.
19 // actually changing the runtime behavior.
20 func OverrideGOMAXPROCSFuncs(newGOMAXPROCS func(int) int, newNumCPU func() int) (cleanup func()) {
21 » mu.Lock()
22 » defer mu.Unlock()
23 » origGOMAXPROCS := gomaxprocs
24 » logger.Debugf("setting GOMAXPROCS to %#v", newGOMAXPROCS)
25 » gomaxprocs = newGOMAXPROCS
26 » origNumCPU := numcpu
27 » numcpu = newNumCPU
28 » return func() {
29 » » mu.Lock()
30 » » defer mu.Unlock()
31 » » gomaxprocs = origGOMAXPROCS
32 » » numcpu = origNumCPU
33 » » enabledMultiCPUs = false
34 » }
35 }
36
37 // EnableMultipleCPUs is called when we want to allow the system to have
38 // GOMAXPROCS>1. By default we only want to enable GOMAXPROCS>1 when running in
39 // the jujud machine agents that are serving the API. We don't want to set it
40 // in the test suite, etc.
41 // So first you call EnableMultipleCPUs() when we determine we are running from
42 // main(), and then later on you call UseMultipleCPUs() when we determine we
43 // are running a machine agent that is serving the API.
44 func EnableMultipleCPUs() {
45 » mu.Lock()
46 » defer mu.Unlock()
47 » // We check to see if GOMAXPROCS is set in the environment. If it is,
48 » // then we will just use the environment variable, and not override it
49 » // ourselves
50 » if os.Getenv("GOMAXPROCS") == "" {
51 » » enabledMultiCPUs = true
52 » }
53 }
54
55 // UseMultipleCPUs is called when we have decided we want to set GOMAXPROCS to
56 // the current number of CPU cores. This will not override the environment
57 // variable if it is set.
58 func UseMultipleCPUs() { 16 func UseMultipleCPUs() {
59 » mu.Lock() 17 » if envGOMAXPROCS := os.Getenv("GOMAXPROCS"); envGOMAXPROCS != "" {
60 » defer mu.Unlock() 18 » » n := gomaxprocs(0)
61 » if !enabledMultiCPUs { 19 » » logger.Debugf("GOMAXPROCS already set in environment to %q, %d i nternally",
62 » » logger.Debugf("multiple CPUs not enabled, calling GOMAXPROCS(0)" ) 20 » » » envGOMAXPROCS, n)
63 » » gomaxprocs(0)
64 return 21 return
65 } 22 }
66 n := numcpu() 23 n := numcpu()
67 logger.Debugf("setting GOMAXPROCS to %d", n) 24 logger.Debugf("setting GOMAXPROCS to %d", n)
68 gomaxprocs(n) 25 gomaxprocs(n)
69 } 26 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b