2010-06-21 21:53:49 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
2014-08-28 08:46:59 -06:00
|
|
|
import "unsafe"
|
|
|
|
|
2010-06-21 21:53:49 -06:00
|
|
|
// GOMAXPROCS sets the maximum number of CPUs that can be executing
|
|
|
|
// simultaneously and returns the previous setting. If n < 1, it does not
|
|
|
|
// change the current setting.
|
2012-01-12 11:06:50 -07:00
|
|
|
// The number of logical CPUs on the local machine can be queried with NumCPU.
|
2010-06-21 21:53:49 -06:00
|
|
|
// This call will go away when the scheduler improves.
|
2014-08-28 08:46:59 -06:00
|
|
|
func GOMAXPROCS(n int) int {
|
2014-09-16 18:26:16 -06:00
|
|
|
if n > _MaxGomaxprocs {
|
|
|
|
n = _MaxGomaxprocs
|
|
|
|
}
|
|
|
|
lock(&sched.lock)
|
|
|
|
ret := int(gomaxprocs)
|
|
|
|
unlock(&sched.lock)
|
|
|
|
if n <= 0 || n == ret {
|
|
|
|
return ret
|
|
|
|
}
|
2014-08-28 08:46:59 -06:00
|
|
|
|
2014-09-16 18:26:16 -06:00
|
|
|
semacquire(&worldsema, false)
|
|
|
|
gp := getg()
|
|
|
|
gp.m.gcing = 1
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 12:54:31 -07:00
|
|
|
systemstack(stoptheworld)
|
2014-09-16 18:26:16 -06:00
|
|
|
|
|
|
|
// newprocs will be processed by starttheworld
|
|
|
|
newprocs = int32(n)
|
|
|
|
|
|
|
|
gp.m.gcing = 0
|
|
|
|
semrelease(&worldsema)
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 12:54:31 -07:00
|
|
|
systemstack(starttheworld)
|
2014-09-16 18:26:16 -06:00
|
|
|
return ret
|
|
|
|
}
|
2010-06-21 21:53:49 -06:00
|
|
|
|
2012-01-24 20:13:11 -07:00
|
|
|
// NumCPU returns the number of logical CPUs on the local machine.
|
2014-08-28 08:46:59 -06:00
|
|
|
func NumCPU() int {
|
|
|
|
return int(ncpu)
|
|
|
|
}
|
2012-01-24 20:13:11 -07:00
|
|
|
|
2012-02-16 14:49:41 -07:00
|
|
|
// NumCgoCall returns the number of cgo calls made by the current process.
|
2014-08-28 08:46:59 -06:00
|
|
|
func NumCgoCall() int64 {
|
|
|
|
var n int64
|
|
|
|
for mp := (*m)(atomicloadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink {
|
|
|
|
n += int64(mp.ncgocall)
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
2010-06-21 21:53:49 -06:00
|
|
|
|
2012-02-16 14:49:41 -07:00
|
|
|
// NumGoroutine returns the number of goroutines that currently exist.
|
2014-08-28 08:46:59 -06:00
|
|
|
func NumGoroutine() int {
|
|
|
|
return int(gcount())
|
|
|
|
}
|