1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 "runtime/internal/atomic"
12 // GOMAXPROCS sets the maximum number of CPUs that can be executing
13 // simultaneously and returns the previous setting. It defaults to
14 // the value of [runtime.NumCPU]. If n < 1, it does not change the current setting.
15 // This call will go away when the scheduler improves.
16 func GOMAXPROCS(n int) int {
17 if GOARCH == "wasm" && n > 1 {
18 n = 1 // WebAssembly has no threads yet, so only one CPU is possible.
22 ret := int(gomaxprocs)
24 if n <= 0 || n == ret {
28 stopTheWorldGC(stwGOMAXPROCS)
30 // newprocs will be processed by startTheWorld
37 // NumCPU returns the number of logical CPUs usable by the current process.
39 // The set of available CPUs is checked by querying the operating system
40 // at process startup. Changes to operating system CPU allocation after
41 // process startup are not reflected.
46 // NumCgoCall returns the number of cgo calls made by the current process.
47 func NumCgoCall() int64 {
48 var n = int64(atomic.Load64(&ncgocall))
49 for mp := (*m)(atomic.Loadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink {
50 n += int64(mp.ncgocall)
55 // NumGoroutine returns the number of goroutines that currently exist.
56 func NumGoroutine() int {
60 //go:linkname debug_modinfo runtime/debug.modinfo
61 func debug_modinfo() string {
65 // mayMoreStackPreempt is a maymorestack hook that forces a preemption
66 // at every possible cooperative preemption point.
68 // This is valuable to apply to the runtime, which can be sensitive to
69 // preemption points. To apply this to all preemption points in the
70 // runtime and runtime-like code, use the following in bash or zsh:
72 // X=(-{gc,asm}flags={runtime/...,reflect,sync}=-d=maymorestack=runtime.mayMoreStackPreempt) GOFLAGS=${X[@]}
74 // This must be deeply nosplit because it is called from a function
75 // prologue before the stack is set up and because the compiler will
76 // call it from any splittable prologue (leading to infinite
79 // Ideally it should also use very little stack because the linker
80 // doesn't currently account for this in nosplit stack depth checking.
82 // Ensure mayMoreStackPreempt can be called for all ABIs.
85 //go:linkname mayMoreStackPreempt
86 func mayMoreStackPreempt() {
87 // Don't do anything on the g0 or gsignal stack.
89 if gp == gp.m.g0 || gp == gp.m.gsignal {
92 // Force a preemption, unless the stack is already poisoned.
93 if gp.stackguard0 < stackPoisonMin {
94 gp.stackguard0 = stackPreempt
98 // mayMoreStackMove is a maymorestack hook that forces stack movement
99 // at every possible point.
101 // See mayMoreStackPreempt.
104 //go:linkname mayMoreStackMove
105 func mayMoreStackMove() {
106 // Don't do anything on the g0 or gsignal stack.
108 if gp == gp.m.g0 || gp == gp.m.gsignal {
111 // Force stack movement, unless the stack is already poisoned.
112 if gp.stackguard0 < stackPoisonMin {
113 gp.stackguard0 = stackForceMove