blob: 3ecaac10bc1a02794619fc5364bcfa338ad87a8a [file] [log] [blame]
Russ Coxe63ae242010-06-21 20:53:49 -07001// 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
5package runtime
6
Russ Cox6179aca2014-08-28 10:46:59 -04007import "unsafe"
8
Russ Coxe63ae242010-06-21 20:53:49 -07009// 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.
Dmitriy Vyukova03c5192012-01-12 22:06:50 +040012// The number of logical CPUs on the local machine can be queried with NumCPU.
Russ Coxe63ae242010-06-21 20:53:49 -070013// This call will go away when the scheduler improves.
Russ Cox6179aca2014-08-28 10:46:59 -040014func GOMAXPROCS(n int) int {
Keith Randallda8cf542014-09-16 17:26:16 -070015 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 }
Russ Cox6179aca2014-08-28 10:46:59 -040024
Keith Randallda8cf542014-09-16 17:26:16 -070025 semacquire(&worldsema, false)
26 gp := getg()
Austin Clements28b51182015-01-30 15:30:41 -050027 gp.m.preemptoff = "GOMAXPROCS"
Russ Cox656be312014-11-12 14:54:31 -050028 systemstack(stoptheworld)
Keith Randallda8cf542014-09-16 17:26:16 -070029
30 // newprocs will be processed by starttheworld
31 newprocs = int32(n)
32
Austin Clements28b51182015-01-30 15:30:41 -050033 gp.m.preemptoff = ""
Keith Randallda8cf542014-09-16 17:26:16 -070034 semrelease(&worldsema)
Russ Cox656be312014-11-12 14:54:31 -050035 systemstack(starttheworld)
Keith Randallda8cf542014-09-16 17:26:16 -070036 return ret
37}
Russ Coxe63ae242010-06-21 20:53:49 -070038
David Symonds0ae9d812012-01-25 14:13:11 +110039// NumCPU returns the number of logical CPUs on the local machine.
Russ Cox6179aca2014-08-28 10:46:59 -040040func NumCPU() int {
41 return int(ncpu)
42}
David Symonds0ae9d812012-01-25 14:13:11 +110043
David Symonds4b171e52012-02-17 08:49:41 +110044// NumCgoCall returns the number of cgo calls made by the current process.
Russ Cox6179aca2014-08-28 10:46:59 -040045func 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}
Russ Coxe63ae242010-06-21 20:53:49 -070052
David Symonds4b171e52012-02-17 08:49:41 +110053// NumGoroutine returns the number of goroutines that currently exist.
Russ Cox6179aca2014-08-28 10:46:59 -040054func NumGoroutine() int {
55 return int(gcount())
56}