github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/runtime/debug.go (about) 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 "unsafe" 8 9 // GOMAXPROCS sets the maximum number of CPUs that can be executing 10 // simultaneously and returns the previous setting. If n < 1, it does not 11 // change the current setting. 12 // The number of logical CPUs on the local machine can be queried with NumCPU. 13 // This call will go away when the scheduler improves. 14 func GOMAXPROCS(n int) int { 15 if n > _MaxGomaxprocs { 16 n = _MaxGomaxprocs 17 } 18 lock(&sched.lock) 19 ret := int(gomaxprocs) 20 unlock(&sched.lock) 21 if n <= 0 || n == ret { 22 return ret 23 } 24 25 semacquire(&worldsema, false) 26 gp := getg() 27 gp.m.gcing = 1 28 systemstack(stoptheworld) 29 30 // newprocs will be processed by starttheworld 31 newprocs = int32(n) 32 33 gp.m.gcing = 0 34 semrelease(&worldsema) 35 systemstack(starttheworld) 36 return ret 37 } 38 39 // NumCPU returns the number of logical CPUs on the local machine. 40 func NumCPU() int { 41 return int(ncpu) 42 } 43 44 // NumCgoCall returns the number of cgo calls made by the current process. 45 func NumCgoCall() int64 { 46 var n int64 47 for mp := (*m)(atomicloadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink { 48 n += int64(mp.ncgocall) 49 } 50 return n 51 } 52 53 // NumGoroutine returns the number of goroutines that currently exist. 54 func NumGoroutine() int { 55 return int(gcount()) 56 }