doubu7134 2016-12-03 00:46
浏览 315
已采纳

Golang:为什么runtime.GOMAXPROCS限制为256?

I was playing with golang 1.7.3 on MacBook and Ubuntu and found that runtime.GOMAXPROCS is limited to 256. Does anyone know where this limit comes from? Is this documented anywhere and why would there be a limit? Is this an implementation optimization?

Only reference to 256 I could find is on this page that describes golang's runtime package: https://golang.org/pkg/runtime/. The runtime.MemStats struct has a couple of stat arrays of size 256:

type MemStats struct {
    ...
    PauseNs       [256]uint64 // circular buffer of recent GC pause durations, most recent at [(NumGC+255)%256]
    PauseEnd      [256]uint64 // circular buffer of recent GC pause end times

Here's example golang code I used:

func main() {
    runtime.GOMAXPROCS(1000)
log.Printf("GOMAXPROCS %d
", runtime.GOMAXPROCS(-1))

}

Prints GOMAXPROCS 256

P.S. Also, can someone point me to documentation on how this GOMAXPROCS relate to OS thread count used by golang scheduler (if at all). Shall we observe go-compiled code running GOMAXPROCS OS threads?

EDIT: Thanks @twotwotwo for pointing out how GOMAXPROCS relate to OS threads. Still it's interesting that documentation does not mention this 256 limit (other that in the MemStats struct which may or may not be related).

I wonder if anyone is aware of the true reason for this 256 number.

  • 写回答

2条回答 默认 最新

  • dongyashun2559 2017-09-28 21:10
    关注

    Note that, starting the next Go 1.10 (Q1 2018), GOMAXPROCS will be limited by ... nothing.

    The runtime no longer artificially limits GOMAXPROCS (previously it was limited to 1024).

    See commit ee55000 by Austin Clements (aclements), which fixes issue 15131.

    Now that allp is dynamically allocated, there's no need for a hard cap on GOMAXPROCS.


    allp is defined here.

    See also commit e900e27:

    runtime: clean up loops over allp

    allp now has length gomaxprocs, which means none of allp[i] are nil or in state _Pdead.
    This lets replace several different styles of loops over allp with normal range loops.

    for i := 0; i < gomaxprocs; i++ { ... } loops can simply range over allp.
    Likewise, range loops over allp[:gomaxprocs] can just range over allp.

    Loops that check for p == nil || p.state == _Pdead don't need to check this any more.

    Loops that check for p == nil don't have to check this if dead Ps don't affect them. I checked that all such loops are, in fact, unaffected by dead Ps. One loop was potentially affected, which this fixes by zeroing p.gcAssistTime in procresize.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab