debug.go 1.85 KB
Newer Older
1 2 3 4 5 6
// 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

7 8
import "unsafe"

9
// Breakpoint executes a breakpoint trap.
10 11 12 13 14 15 16 17 18 19 20 21 22 23
func Breakpoint()

// LockOSThread wires the calling goroutine to its current operating system thread.
// Until the calling goroutine exits or calls UnlockOSThread, it will always
// execute in that thread, and no other goroutine can.
func LockOSThread()

// UnlockOSThread unwires the calling goroutine from its fixed operating system thread.
// If the calling goroutine has not called LockOSThread, UnlockOSThread is a no-op.
func UnlockOSThread()

// 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.
24
// The number of logical CPUs on the local machine can be queried with NumCPU.
25
// This call will go away when the scheduler improves.
26
func GOMAXPROCS(n int) int {
27 28 29 30 31 32 33 34 35
	if n > _MaxGomaxprocs {
		n = _MaxGomaxprocs
	}
	lock(&sched.lock)
	ret := int(gomaxprocs)
	unlock(&sched.lock)
	if n <= 0 || n == ret {
		return ret
	}
36

37 38 39 40 41 42 43 44 45 46 47 48 49
	semacquire(&worldsema, false)
	gp := getg()
	gp.m.gcing = 1
	onM(stoptheworld)

	// newprocs will be processed by starttheworld
	newprocs = int32(n)

	gp.m.gcing = 0
	semrelease(&worldsema)
	onM(starttheworld)
	return ret
}
50

51
// NumCPU returns the number of logical CPUs on the local machine.
52 53 54
func NumCPU() int {
	return int(ncpu)
}
55

56
// NumCgoCall returns the number of cgo calls made by the current process.
57 58 59 60 61 62 63
func NumCgoCall() int64 {
	var n int64
	for mp := (*m)(atomicloadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink {
		n += int64(mp.ncgocall)
	}
	return n
}
64

65
// NumGoroutine returns the number of goroutines that currently exist.
66 67 68 69 70
func NumGoroutine() int {
	return int(gcount())
}

func gcount() int32