2010-06-21 20:53:49 -07: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
|
|
|
|
|
|
2015-11-02 14:09:24 -05:00
|
|
|
import (
|
|
|
|
|
"runtime/internal/atomic"
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
2014-08-28 10:46:59 -04:00
|
|
|
|
2010-06-21 20:53:49 -07:00
|
|
|
// GOMAXPROCS sets the maximum number of CPUs that can be executing
|
2016-03-01 23:21:55 +00:00
|
|
|
// simultaneously and returns the previous setting. If n < 1, it does not
|
2010-06-21 20:53:49 -07:00
|
|
|
// change the current setting.
|
2012-01-12 22:06:50 +04:00
|
|
|
// The number of logical CPUs on the local machine can be queried with NumCPU.
|
2010-06-21 20:53:49 -07:00
|
|
|
// This call will go away when the scheduler improves.
|
2014-08-28 10:46:59 -04:00
|
|
|
func GOMAXPROCS(n int) int {
|
2018-03-31 23:14:17 +02:00
|
|
|
if GOARCH == "wasm" && n > 1 {
|
|
|
|
|
n = 1 // WebAssembly has no threads yet, so only one CPU is possible.
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-16 17:26:16 -07:00
|
|
|
lock(&sched.lock)
|
|
|
|
|
ret := int(gomaxprocs)
|
|
|
|
|
unlock(&sched.lock)
|
|
|
|
|
if n <= 0 || n == ret {
|
|
|
|
|
return ret
|
|
|
|
|
}
|
2014-08-28 10:46:59 -04:00
|
|
|
|
2019-06-17 19:03:09 +00:00
|
|
|
stopTheWorldGC("GOMAXPROCS")
|
2014-09-16 17:26:16 -07:00
|
|
|
|
2015-05-15 16:00:50 -04:00
|
|
|
// newprocs will be processed by startTheWorld
|
2014-09-16 17:26:16 -07:00
|
|
|
newprocs = int32(n)
|
|
|
|
|
|
2019-06-17 19:03:09 +00:00
|
|
|
startTheWorldGC()
|
2014-09-16 17:26:16 -07:00
|
|
|
return ret
|
|
|
|
|
}
|
2010-06-21 20:53:49 -07:00
|
|
|
|
2015-07-10 06:20:51 -07:00
|
|
|
// NumCPU returns the number of logical CPUs usable by the current process.
|
2015-12-04 23:16:50 -05:00
|
|
|
//
|
|
|
|
|
// The set of available CPUs is checked by querying the operating system
|
|
|
|
|
// at process startup. Changes to operating system CPU allocation after
|
|
|
|
|
// process startup are not reflected.
|
2014-08-28 10:46:59 -04:00
|
|
|
func NumCPU() int {
|
|
|
|
|
return int(ncpu)
|
|
|
|
|
}
|
2012-01-25 14:13:11 +11:00
|
|
|
|
2012-02-17 08:49:41 +11:00
|
|
|
// NumCgoCall returns the number of cgo calls made by the current process.
|
2014-08-28 10:46:59 -04:00
|
|
|
func NumCgoCall() int64 {
|
|
|
|
|
var n int64
|
2015-11-02 14:09:24 -05:00
|
|
|
for mp := (*m)(atomic.Loadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink {
|
2014-08-28 10:46:59 -04:00
|
|
|
n += int64(mp.ncgocall)
|
|
|
|
|
}
|
|
|
|
|
return n
|
|
|
|
|
}
|
2010-06-21 20:53:49 -07:00
|
|
|
|
2012-02-17 08:49:41 +11:00
|
|
|
// NumGoroutine returns the number of goroutines that currently exist.
|
2014-08-28 10:46:59 -04:00
|
|
|
func NumGoroutine() int {
|
|
|
|
|
return int(gcount())
|
|
|
|
|
}
|
2019-04-22 23:01:26 -04:00
|
|
|
|
|
|
|
|
//go:linkname debug_modinfo runtime/debug.modinfo
|
|
|
|
|
func debug_modinfo() string {
|
|
|
|
|
return modinfo
|
|
|
|
|
}
|