Russ Cox | e63ae24 | 2010-06-21 20:53:49 -0700 | [diff] [blame] | 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 | |
Russ Cox | 6179aca | 2014-08-28 10:46:59 -0400 | [diff] [blame] | 7 | import "unsafe" |
| 8 | |
Russ Cox | e63ae24 | 2010-06-21 20:53:49 -0700 | [diff] [blame] | 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. |
Dmitriy Vyukov | a03c519 | 2012-01-12 22:06:50 +0400 | [diff] [blame] | 12 | // The number of logical CPUs on the local machine can be queried with NumCPU. |
Russ Cox | e63ae24 | 2010-06-21 20:53:49 -0700 | [diff] [blame] | 13 | // This call will go away when the scheduler improves. |
Russ Cox | 6179aca | 2014-08-28 10:46:59 -0400 | [diff] [blame] | 14 | func GOMAXPROCS(n int) int { |
Keith Randall | da8cf54 | 2014-09-16 17:26:16 -0700 | [diff] [blame] | 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 | } |
Russ Cox | 6179aca | 2014-08-28 10:46:59 -0400 | [diff] [blame] | 24 | |
Keith Randall | da8cf54 | 2014-09-16 17:26:16 -0700 | [diff] [blame] | 25 | semacquire(&worldsema, false) |
| 26 | gp := getg() |
Austin Clements | 28b5118 | 2015-01-30 15:30:41 -0500 | [diff] [blame] | 27 | gp.m.preemptoff = "GOMAXPROCS" |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 28 | systemstack(stoptheworld) |
Keith Randall | da8cf54 | 2014-09-16 17:26:16 -0700 | [diff] [blame] | 29 | |
| 30 | // newprocs will be processed by starttheworld |
| 31 | newprocs = int32(n) |
| 32 | |
Austin Clements | 28b5118 | 2015-01-30 15:30:41 -0500 | [diff] [blame] | 33 | gp.m.preemptoff = "" |
Keith Randall | da8cf54 | 2014-09-16 17:26:16 -0700 | [diff] [blame] | 34 | semrelease(&worldsema) |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 35 | systemstack(starttheworld) |
Keith Randall | da8cf54 | 2014-09-16 17:26:16 -0700 | [diff] [blame] | 36 | return ret |
| 37 | } |
Russ Cox | e63ae24 | 2010-06-21 20:53:49 -0700 | [diff] [blame] | 38 | |
David Symonds | 0ae9d81 | 2012-01-25 14:13:11 +1100 | [diff] [blame] | 39 | // NumCPU returns the number of logical CPUs on the local machine. |
Russ Cox | 6179aca | 2014-08-28 10:46:59 -0400 | [diff] [blame] | 40 | func NumCPU() int { |
| 41 | return int(ncpu) |
| 42 | } |
David Symonds | 0ae9d81 | 2012-01-25 14:13:11 +1100 | [diff] [blame] | 43 | |
David Symonds | 4b171e5 | 2012-02-17 08:49:41 +1100 | [diff] [blame] | 44 | // NumCgoCall returns the number of cgo calls made by the current process. |
Russ Cox | 6179aca | 2014-08-28 10:46:59 -0400 | [diff] [blame] | 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 | } |
Russ Cox | e63ae24 | 2010-06-21 20:53:49 -0700 | [diff] [blame] | 52 | |
David Symonds | 4b171e5 | 2012-02-17 08:49:41 +1100 | [diff] [blame] | 53 | // NumGoroutine returns the number of goroutines that currently exist. |
Russ Cox | 6179aca | 2014-08-28 10:46:59 -0400 | [diff] [blame] | 54 | func NumGoroutine() int { |
| 55 | return int(gcount()) |
| 56 | } |