如何查找tcl中的CPU数量?

如何查找tcl中的CPU数量?,tcl,Tcl,我使用的软件支持多线程和TCL接口。因此,我不知道如何找到TCL中的CPU数量来限制最大线程数。TCL中没有内置任何东西;已经讨论过了,但没有采取行动,因为CPU的数量不一定是流程可能使用的数量(到目前为止,这是获得信息的最相关原因)。也就是说,信息是可用的。只是你获取信息的方式因平台而异 proc numberOfCPUs {} { # Windows puts it in an environment variable global tcl_platform env

我使用的软件支持多线程和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的答案。但是我仍然对
sysctl
有问题,它存在于
Ubuntu
中,但是它没有
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
windows7
上试用过,效果很好。我身边没有MacOS。因此,如果有人能验证它是否有效,那就太好了。

我尝试了上述两种解决方案@DonalFellows近乎完美,但在我的OSX上不起作用,因为默认的Tcsh是8.4,exec的{*}语法也不起作用。然而,{*}构造似乎不是必需的,因为删除它使它能够与TCLSH8.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
}

站台?Linux还是Windows?它应该是跨平台的。因为它将同时在windows和linux上使用,而不是舒尔,而是跨平台的。在Linux上,reading/proc/cpuinfo为您提供了信息。试试cat/proc/cpuinfo你是说基于Android的TCL吗?@SilentKnight:什么是TCL?Tcl/Tcl/Tcl是一种编程语言hi@Donal谢谢你的回答。我明白了。但是代码中有两个问题。第一个是输入错误,对于Linux平台,您可以设置变量
cores
,但返回
cpu\u cores
。另一个用于
OSX
检查。我的
Ubuntu
也得到了
sysctl
。所以它不能返回正确的结果。我必须交换检查序列,使其在Linux上工作。@EnzoChi现在应该被修复了。很抱歉。(我在Debian和OSX上进行了测试,但Ubuntu必须有所不同……)