| 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. |
| 4 |
|
| 5 |
package runtime |
| 6 |
|
| 7 |
import ( |
| 8 |
"runtime/internal/atomic" |
| 9 |
"unsafe" |
| 10 |
) |
| 11 |
|
| 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. |
| 19 |
} |
| 20 |
|
| 21 |
lock(&sched.lock) |
| 22 |
ret := int(gomaxprocs) |
| 23 |
unlock(&sched.lock) |
| 24 |
if n <= 0 || n == ret { |
| 25 |
return ret |
| 26 |
} |
| 27 |
|
| 28 |
stopTheWorldGC("GOMAXPROCS") |
| 29 |
|
| 30 |
// newprocs will be processed by startTheWorld |
| 31 |
newprocs = int32(n) |
| 32 |
|
| 33 |
startTheWorldGC() |
| 34 |
return ret |
| 35 |
} |
| 36 |
|
| 37 |
// NumCPU returns the number of logical CPUs usable by the current process. |
| 38 |
// |
| 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. |
| 42 |
func NumCPU() int { |
| 43 |
return int(ncpu) |
| 44 |
} |
| 45 |
|
| 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) |
| 51 |
} |
| 52 |
return n |
| 53 |
} |
| 54 |
|
| 55 |
// NumGoroutine returns the number of goroutines that currently exist. |
| 56 |
func NumGoroutine() int { |
| 57 |
waitForSystemGoroutines() |
| 58 |
return int(gcount()) |
| 59 |
} |
| 60 |
|
| 61 |
// Get field tracking information. Only fields with a tag go:"track" |
| 62 |
// are tracked. This function will add every such field that is |
| 63 |
// referenced to the map. The keys in the map will be |
| 64 |
// PkgPath.Name.FieldName. The value will be true for each field |
| 65 |
// added. |
| 66 |
func Fieldtrack(map[string]bool) |
| 67 |
|
| 68 |
//go:linkname debug_modinfo runtime_1debug.modinfo |
| 69 |
func debug_modinfo() string { |
| 70 |
return modinfo |
| 71 |
} |
| 72 |
|
| 73 |
// setmodinfo is visible to code generated by cmd/go/internal/modload.ModInfoProg. |
| 74 |
//go:linkname setmodinfo runtime.setmodinfo |
| 75 |
func setmodinfo(s string) { |
| 76 |
modinfo = s |
| 77 |
} |