如何找到tcl中的CPU数量?
How to find the number of CPUs in tcl?
我使用的软件支持多线程和TCL接口。所以我不确定如何找到TCL中的CPU数量来限制最大线程。
Tcl 中没有内置任何东西;它已经过讨论,但没有采取行动,因为 CPU 的数量不一定是进程可能使用的 数量 (到目前为止,获得信息的最相关原因) .也就是说,信息是可用的。只是获取信息的方式因平台而异。
proc numberOfCPUs {} {
# Windows puts it in an environment variable
global tcl_platform env
if {$tcl_platform(platform) eq "windows"} {
return $env(NUMBER_OF_PROCESSORS)
}
# Check for sysctl (OSX, BSD)
set sysctl [auto_execok "sysctl"]
if {[llength $sysctl]} {
if {![catch {exec {*}$sysctl -n "hw.ncpu"} cores]} {
return $cores
}
}
# Assume Linux, which has /proc/cpuinfo, but be careful
if {![catch {open "/proc/cpuinfo"} f]} {
set cores [regexp -all -line {^processor\s} [read $f]]
close $f
if {$cores > 0} {
return $cores
}
}
# No idea what the actual number of cores is; exhausted all our options
# Fall back to returning 1; there must be at least that because we're running on it!
return 1
}
请注意,如果您想找出 CPU 到 运行 的数量,您真正想要的是 CPU 亲和力中设置的位数面具。不幸的是,在大多数平台上检索该信息并非易事。
代码原文来自@DonalFellows的回答。但是我仍然遇到 Ubuntu
中存在的 sysctl
的问题,但它没有 hw.ncpu
。
所以我做了一些修改。
proc number_of_processor {} {
global tcl_platform env
switch ${tcl_platform(platform)} {
"windows" {
return $env(NUMBER_OF_PROCESSORS)
}
"unix" {
if {![catch {open "/proc/cpuinfo"} f]} {
set cores [regexp -all -line {^processor\s} [read $f]]
close $f
if {$cores > 0} {
return $cores
}
}
}
"Darwin" {
if {![catch {exec {*}$sysctl -n "hw.ncpu"} cores]} {
return $cores
}
}
default {
puts "Unknown System"
return 1
}
}
}
在 Ubuntu
和 Windows 7
上试过,有效。我身边没有 MacOS。因此,如果有人可以验证是否有效,那就太好了。
以上两种方法我都试过了。 @DonalFellows 几乎是完美的,但在我的 OSX 上不起作用,因为默认的 tclsh 是 8.4,而 exec 的 {*} 语法不起作用。然而 {*} 构造似乎不是必需的,因为删除它可以使它与 tclsh 8.4、8.5 和 8.6 一起工作。
我已经粘贴了下面的代码,因为我没有足够的声誉来评论。
proc numberOfCPUs {} {
# Windows puts it in an environment variable
global tcl_platform env
if {$tcl_platform(platform) eq "windows"} {
return $env(NUMBER_OF_PROCESSORS)
}
# Check for sysctl (OSX, BSD)
set sysctl [auto_execok "sysctl"]
if {[llength $sysctl]} {
if {![catch {exec $sysctl -n "hw.ncpu"} cores]} {
return $cores
}
}
# Assume Linux, which has /proc/cpuinfo, but be careful
if {![catch {open "/proc/cpuinfo"} f]} {
set cores [regexp -all -line {^processor\s} [read $f]]
close $f
if {$cores > 0} {
return $cores
}
}
# No idea what the actual number of cores is; exhausted all our options
# Fall back to returning 1; there must be at least that because we're running on it!
return 1
}
我使用的软件支持多线程和TCL接口。所以我不确定如何找到TCL中的CPU数量来限制最大线程。
Tcl 中没有内置任何东西;它已经过讨论,但没有采取行动,因为 CPU 的数量不一定是进程可能使用的 数量 (到目前为止,获得信息的最相关原因) .也就是说,信息是可用的。只是获取信息的方式因平台而异。
proc numberOfCPUs {} {
# Windows puts it in an environment variable
global tcl_platform env
if {$tcl_platform(platform) eq "windows"} {
return $env(NUMBER_OF_PROCESSORS)
}
# Check for sysctl (OSX, BSD)
set sysctl [auto_execok "sysctl"]
if {[llength $sysctl]} {
if {![catch {exec {*}$sysctl -n "hw.ncpu"} cores]} {
return $cores
}
}
# Assume Linux, which has /proc/cpuinfo, but be careful
if {![catch {open "/proc/cpuinfo"} f]} {
set cores [regexp -all -line {^processor\s} [read $f]]
close $f
if {$cores > 0} {
return $cores
}
}
# No idea what the actual number of cores is; exhausted all our options
# Fall back to returning 1; there must be at least that because we're running on it!
return 1
}
请注意,如果您想找出 CPU 到 运行 的数量,您真正想要的是 CPU 亲和力中设置的位数面具。不幸的是,在大多数平台上检索该信息并非易事。
代码原文来自@DonalFellows的回答。但是我仍然遇到 Ubuntu
中存在的 sysctl
的问题,但它没有 hw.ncpu
。
所以我做了一些修改。
proc number_of_processor {} {
global tcl_platform env
switch ${tcl_platform(platform)} {
"windows" {
return $env(NUMBER_OF_PROCESSORS)
}
"unix" {
if {![catch {open "/proc/cpuinfo"} f]} {
set cores [regexp -all -line {^processor\s} [read $f]]
close $f
if {$cores > 0} {
return $cores
}
}
}
"Darwin" {
if {![catch {exec {*}$sysctl -n "hw.ncpu"} cores]} {
return $cores
}
}
default {
puts "Unknown System"
return 1
}
}
}
在 Ubuntu
和 Windows 7
上试过,有效。我身边没有 MacOS。因此,如果有人可以验证是否有效,那就太好了。
以上两种方法我都试过了。 @DonalFellows 几乎是完美的,但在我的 OSX 上不起作用,因为默认的 tclsh 是 8.4,而 exec 的 {*} 语法不起作用。然而 {*} 构造似乎不是必需的,因为删除它可以使它与 tclsh 8.4、8.5 和 8.6 一起工作。
我已经粘贴了下面的代码,因为我没有足够的声誉来评论。
proc numberOfCPUs {} {
# Windows puts it in an environment variable
global tcl_platform env
if {$tcl_platform(platform) eq "windows"} {
return $env(NUMBER_OF_PROCESSORS)
}
# Check for sysctl (OSX, BSD)
set sysctl [auto_execok "sysctl"]
if {[llength $sysctl]} {
if {![catch {exec $sysctl -n "hw.ncpu"} cores]} {
return $cores
}
}
# Assume Linux, which has /proc/cpuinfo, but be careful
if {![catch {open "/proc/cpuinfo"} f]} {
set cores [regexp -all -line {^processor\s} [read $f]]
close $f
if {$cores > 0} {
return $cores
}
}
# No idea what the actual number of cores is; exhausted all our options
# Fall back to returning 1; there must be at least that because we're running on it!
return 1
}