英文:
How to modify the number of logical CPUs used by a process?
问题
GOMAXPROCS函数用于设置可以同时执行的最大CPU数量,并返回先前的设置。它的默认值是runtime.NumCPU的返回值。
NumCPU函数返回当前进程可用的逻辑CPU数量。
以下代码不会改变进程使用的逻辑CPU数量:
package main
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(1)
fmt.Println(runtime.NumCPU()) // 期望输出:1 实际输出:8
}
在我的笔记本电脑上:
$ lscpu # 显示八个虚拟核心,对应八个操作系统线程
...
每个核心的线程数:2
每个插槽的核心数:4
插槽数:1
$ go version
go version go1.20.2 linux/amd64
如何修改程序使用的逻辑CPU数量?
英文:
> GOMAXPROCS sets the maximum number of CPUs that can be executing simultaneously and returns the previous setting. It defaults to the value of runtime.NumCPU.
> NumCPU returns the number of logical CPUs usable by the current process.
Below code does not change the number of logical CPUs used by a process:
package main
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(1)
fmt.Println(runtime.NumCPU()) // Expected: 1 Actual: 8
}
In my laptop:
$ lscpu # shows eight virtual cores corresponding to eight OS threads
...
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
$ go version
go version go1.20.2 linux/amd64
How to modify the logical CPU's used by the program?
答案1
得分: 4
如果你阅读一下关于runtime.GOMAXPROCS和runtime.NumCPU()的文档,你会看到:
func NumCPU
[...]可用CPU的集合是在进程启动时通过查询操作系统来检查的。进程启动后对操作系统CPU分配的更改不会反映出来。
"进程启动后对操作系统CPU分配的更改不会反映出来",这就是为什么你看不到这个值的变化。
现在让我们看一下GOMAXPROCS的文档:
func GOMAXPROCS [...]
GOMAXPROCS设置可以同时执行的最大CPU数量,并返回先前的设置。它默认为runtime.NumCPU的值。如果n < 1,则不会更改当前设置。当调度器改进时,此调用将消失。
因此,你可以通过运行以下代码来检查当前设置的值:
fmt.Println(runtime.GOMAXPROCS(0))
英文:
If you were to read what docs has to say about runtime.GOMAXPROCS and runtime.NumCPU() you would see:
> func NumCPU
> [...]
>
> The set of available CPUs is checked by querying the operating system at process startup. Changes to operating system CPU allocation after process startup are not reflected.
"Changes to operating system CPU allocation after process startup are not reflected", this is why you don't see this value change.
Now lets see the GOMAXPROCS's docs
> func GOMAXPROCS [...]
>
> GOMAXPROCS sets the maximum number of CPUs that can be executing simultaneously and returns the previous setting. It defaults to the value of runtime.NumCPU. If n < 1, it does not change the current setting. This call will go away when the scheduler improves.
So you can check the currently set value by running:
fmt.Println(runtime.GOMAXPROCS(0))
答案2
得分: 3
没有矛盾。
GOMAXPROCS影响Go运行时将使用的进程/线程数量。它们将在哪些CPU核心上执行将根据系统上的其他负载而变化。
NumCPU返回可用的核心数。即使配置Go运行时不利用所有核心,它也不会改变。
NumCPU将反映通过操作系统机制设置的限制,例如:
user@host:~$ go run test.go
4
user@host:~$ taskset -c 0 go run test.go
1
英文:
There is no contradiction.
GOMAXPROCS affects the number of processes/threads that will be used by Go runtime. Exactly which CPU cores they will execute on will vary based on other load on the system.
NumCPU returns the number of cores available. It doesn't change even if Go runtime is configured not to take advantage of all of them.
NumCPU will reflect limits set through operating system mechanisms, e.g.
user@host:~$ go run test.go
4
user@host:~$ taskset -c 0 go run test.go
1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论