{"id":10006497,"date":"2022-09-16T10:27:38","date_gmt":"2022-09-16T02:27:38","guid":{"rendered":"https:\/\/ityet.com\/?p=10006497"},"modified":"2022-09-16T10:27:38","modified_gmt":"2022-09-16T02:27:38","slug":"%e9%b9%85%e5%8e%82%e5%90%8e%e5%8f%b0%e5%a4%a7%e4%bd%ac%e6%95%99%e4%bd%a0go%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006497","title":{"rendered":"\u9e45\u5382\u540e\u53f0\u5927\u4f6c\u6559\u4f60Go\u5185\u5b58\u7ba1\u7406\uff01"},"content":{"rendered":"<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487989\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u5bfc\u8bed\u00a0|\u00a0\u672c\u6587\u63a8\u9009\u81ea\u817e\u8baf\u4e91\u5f00\u53d1\u8005\u793e\u533a-\u3010\u6280\u601d\u5e7f\u76ca \u00b7 \u817e\u8baf\u6280\u672f\u4eba\u539f\u521b\u96c6\u3011\u4e13\u680f\u3002\u8be5\u4e13\u680f\u662f\u817e\u8baf\u4e91\u5f00\u53d1\u8005\u793e\u533a\u4e3a\u817e\u8baf\u6280\u672f\u4eba\u4e0e\u5e7f\u6cdb\u5f00\u53d1\u8005\u6253\u9020\u7684\u5206\u4eab\u4ea4\u6d41\u7a97\u53e3\u3002\u680f\u76ee\u9080\u7ea6\u817e\u8baf\u6280\u672f\u4eba\u5206\u4eab\u539f\u521b\u7684\u6280\u672f\u79ef\u6dc0\uff0c\u4e0e\u5e7f\u6cdb\u5f00\u53d1\u8005\u4e92\u542f\u8fea\u5171\u6210\u957f\u3002\u672c\u6587\u4f5c\u8005\u662f\u817e\u8baf\u540e\u53f0\u5f00\u53d1\u5de5\u7a0b\u5e08\u7f57\u5143\u56fd\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487990\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u6808\u5185\u5b58\u6808\u533a\u7684\u5185\u5b58\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u8fdb\u884c\u5206\u914d\u548c\u91ca\u653e\uff0c\u6808\u533a\u4e2d\u5b58\u50a8\u7740\u51fd\u6570\u7684\u53c2\u6570\u4ee5\u53ca\u5c40\u90e8\u53d8\u91cf\uff0c\u5b83\u4eec\u4f1a\u968f\u7740\u51fd\u6570\u7684\u521b\u5efa\u800c\u521b\u5efa\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u800c\u9500\u6bc1\u3002<\/p>\n<p>\u6bcf\u4e2agoroutine\u90fd\u7ef4\u62a4\u7740\u4e00\u4e2a\u81ea\u5df1\u7684\u6808\u533a\uff0c\u8fd9\u4e2a\u6808\u533a\u53ea\u80fd\u81ea\u5df1\u4f7f\u7528\u4e0d\u80fd\u88ab\u5176\u4ed6goroutine\u4f7f\u7528\u3002<\/p>\n<p>\u6808\u533a\u7684\u521d\u59cb\u5927\u5c0f\u662f2KB\u3002\u6808\u5185\u5b58\u7a7a\u95f4\u3001\u7ed3\u6784\u548c\u521d\u59cb\u5927\u5c0f\u7ecf\u8fc7\u4e86\u51e0\u4e2a\u7248\u672c\u7684\u66f4\u8fed\uff1a<\/p>\n<p>v1.0~v1.1\uff1a\u6700\u5c0f\u6808\u5185\u5b58\u7a7a\u95f4\u4e3a4KB\u3002<\/p>\n<p>v1.2\uff1a\u5c06\u6700\u5c0f\u6808\u5185\u5b58\u63d0\u5347\u5230\u4e868KB\u3002<\/p>\n<p>v1.3: \u4f7f\u7528\u8fde\u7eed\u6808\u66ff\u6362\u4e4b\u524d\u7248\u672c\u7684\u5206\u6bb5\u6808\u3002<\/p>\n<p>v1.4~v1.19\uff1a\u5c06\u6700\u5c0f\u6808\u5185\u5b58\u964d\u4f4e\u5230\u4e862KB\u3002<\/p>\n<p>\u6808\u7ed3\u6784\u7ecf\u8fc7\u4e86\u5206\u6bb5\u6808\u5230\u8fde\u7eed\u6808\u7684\u53d1\u5c55\u8fc7\u7a0b\uff0c\u4ecb\u7ecd\u5982\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487991\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u5206\u6bb5\u6808\u968f\u7740goroutine\u8c03\u7528\u7684\u51fd\u6570\u5c42\u7ea7\u7684\u6df1\u5165\u6216\u8005\u5c40\u90e8\u53d8\u91cf\u9700\u8981\u7684\u8d8a\u6765\u8d8a\u591a\u65f6\uff0c\u8fd0\u884c\u65f6\u4f1a\u8c03\u7528runtime.morestack\u548cruntime.newstack\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6808\u7a7a\u95f4\uff0c\u8fd9\u4e9b\u6808\u7a7a\u95f4\u662f\u4e0d\u8fde\u7eed\u7684\uff0c\u4f46\u662f\u5f53\u524dgoroutine\u7684\u591a\u4e2a\u6808\u7a7a\u95f4\u4f1a\u4ee5\u53cc\u5411\u94fe\u8868\u7684\u5f62\u5f0f\u4e32\u8054\u8d77\u6765\uff0c\u8fd0\u884c\u65f6\u4f1a\u901a\u8fc7\u6307\u9488\u627e\u5230\u8fde\u7eed\u7684\u6808\u7247\u6bb5\u3002\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487992\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u4f18\u70b9\uff1a\u6309\u9700\u4e3a\u5f53\u524dgoroutine\u5206\u914d\u5185\u5b58\u5e76\u4e14\u53ca\u65f6\u51cf\u5c11\u5185\u5b58\u7684\u5360\u7528\u3002<\/p>\n<p>\u7f3a\u70b9\uff1a\u5982\u679c\u5f53\u524dgoroutine\u7684\u6808\u51e0\u4e4e\u5145\u6ee1\uff0c\u90a3\u4e48\u4efb\u610f\u7684\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u89e6\u53d1\u6808\u7684\u6269\u5bb9\uff0c\u5f53\u51fd\u6570\u8fd4\u56de\u540e\u53c8\u4f1a\u89e6\u53d1\u6808\u7684\u6536\u7f29\uff0c\u5982\u679c\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u8c03\u7528\u51fd\u6570\uff0c\u6808\u7684\u5206\u914d\u548c\u91ca\u653e\u5c31\u4f1a\u9020\u6210\u5de8\u5927\u7684\u989d\u5916\u5f00\u9500\uff0c\u8fd9\u88ab\u79f0\u4e3a\u70ed\u5206\u88c2\u95ee\u9898\uff08Hot split\uff09\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cGo\u57281.2\u7248\u672c\u7684\u65f6\u5019\u4e0d\u5f97\u4e0d\u5c06\u6808\u7684\u521d\u59cb\u5316\u5185\u5b58\u4ece4KB\u589e\u5927\u5230\u4e868KB\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487993\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u8fde\u7eed\u6808\u8fde\u7eed\u6808\u53ef\u4ee5\u89e3\u51b3\u5206\u6bb5\u6808\u4e2d\u5b58\u5728\u7684\u4e24\u4e2a\u95ee\u9898\uff0c\u5176\u6838\u5fc3\u539f\u7406\u5c31\u662f\u6bcf\u5f53\u7a0b\u5e8f\u7684\u6808\u7a7a\u95f4\u4e0d\u8db3\u65f6\uff0c\u521d\u59cb\u5316\u4e00\u7247\u6bd4\u65e7\u6808\u5927\u4e24\u500d\u7684\u65b0\u6808\u5e76\u5c06\u539f\u6808\u4e2d\u7684\u6240\u6709\u503c\u90fd\u8fc1\u79fb\u5230\u65b0\u7684\u6808\u4e2d\uff0c\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\u6216\u8005\u51fd\u6570\u8c03\u7528\u5c31\u6709\u4e86\u5145\u8db3\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487994\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u6808\u7a7a\u95f4\u4e0d\u8db3\u5bfc\u81f4\u7684\u6269\u5bb9\u4f1a\u7ecf\u5386\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<p>\u8c03\u7528runtime.newstack\u7528\u5728\u5185\u5b58\u7a7a\u95f4\u4e2d\u5206\u914d\u66f4\u5927\u7684\u6808\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<p>\u4f7f\u7528runtime.copystack\u5c06\u65e7\u6808\u4e2d\u7684\u6240\u6709\u5185\u5bb9\u590d\u5236\u5230\u65b0\u7684\u6808\u4e2d\u3002<\/p>\n<p>\u5c06\u6307\u5411\u65e7\u6808\u5bf9\u5e94\u53d8\u91cf\u7684\u6307\u9488\u91cd\u65b0\u6307\u5411\u65b0\u6808\u3002<\/p>\n<p>\u8c03\u7528runtime.stackfree\u9500\u6bc1\u5e76\u56de\u6536\u65e7\u6808\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487995\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u6808\u7ba1\u7406Span\u9664\u4e86\u7528\u4f5c\u5806\u5185\u5b58\u5206\u914d\u5916\uff0c\u4e5f\u7528\u4e8e\u6808\u5185\u5b58\u5206\u914d\uff0c\u53ea\u662f\u7528\u9014\u4e0d\u540c\u7684Span\u5bf9\u5e94\u7684mSpan\u72b6\u6001\u4e0d\u540c\u3002\u7528\u505a\u5806\u5185\u5b58\u7684mSpan\u72b6\u6001\u4e3amSpanInUse\uff0c\u800c\u7528\u505a\u6808\u5185\u5b58\u7684\u72b6\u6001\u4e3amSpanManual\u3002<\/p>\n<p>\u6808\u7a7a\u95f4\u5728\u8fd0\u884c\u65f6\u4e2d\u5305\u542b\u4e24\u4e2a\u91cd\u8981\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5206\u522b\u662fruntime.stackpool\u548cruntime.stackLarge\uff0c\u8fd9\u4e24\u4e2a\u53d8\u91cf\u5206\u522b\u8868\u793a\u5168\u5c40\u7684\u6808\u7f13\u5b58\u548c\u5927\u6808\u7f13\u5b58\uff0c\u524d\u8005\u53ef\u4ee5\u5206\u914d\u5c0f\u4e8e32KB\u7684\u5185\u5b58\uff0c\u540e\u8005\u7528\u6765\u5206\u914d\u5927\u4e8e32KB\u7684\u6808\u7a7a\u95f4\u3002<\/p>\n<p>\u4e3a\u63d0\u9ad8\u6808\u5185\u5b58\u5206\u914d\u6548\u7387\uff0c\u8c03\u5ea6\u5668\u521d\u59cb\u5316\u65f6\u4f1a\u521d\u59cb\u5316\u4e24\u4e2a\u7528\u4e8e\u6808\u5206\u914d\u7684\u5168\u5c40\u5bf9\u8c61\uff1astackpool\u548cstackLarge\u3002<\/p>\n<p>\u4ecb\u7ecd\u5982\u4e0b\uff1a<\/p>\n<p>\uff08\u4e00\uff09StackPoolstackpool\u9762\u541132KB\u4ee5\u4e0b\u7684\u6808\u5206\u914d\uff0c\u6808\u5927\u5c0f\u5fc5\u987b\u662f2\u7684\u5e42\uff0c\u6700\u5c0f2KB\uff0c\u5728Linux\u73af\u5883\u4e0b\uff0cstackpool\u63d0\u4f9b\u4e862kB\u30014KB\u30018KB\u300116KB\u56db\u79cd\u89c4\u683c\u7684mSpan\u94fe\u8868\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487996\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>stackpool\u7ed3\u6784\u5b9a\u4e49\u5982\u4e0b<\/p>\n<pre><code>\/\/ Global pool of spans that have free stacks.\n\/\/ Stacks are assigned an order according to size.\n\/\/\n\/\/  order = log_2(size\/FixedStack)\n\/\/\n\/\/ There is a free list for each order.\nvar stackpool [_NumStackOrders]struct {\n  item stackpoolItem\n  _    [cpu.CacheLinePadSize - unsafe.Sizeof(stackpoolItem{})%cpu.CacheLinePadSize]byte\n}\n\n\/\/go:notinheap\ntype stackpoolItem struct {\n  mu   mutex\n  span mSpanList\n}\n\n\/\/ mSpanList heads a linked list of spans.\n\/\/\n\/\/go:notinheap\ntype mSpanList struct {\n  first *mspan \/\/ first span in list, or nil if none\n  last  *mspan \/\/ last span in list, or nil if none\n}<\/code><\/pre>\n<p>\uff08\u4e8c\uff09StackLarge\u5927\u4e8e\u7b49\u4e8e32KB\u7684\u6808\uff0c\u7531stackLarge\u6765\u5206\u914d\uff0c\u8fd9\u4e5f\u662f\u4e2amSpan\u94fe\u8868\u7684\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a25\u3002mSpan\u89c4\u683c\u4ece8KB\u5f00\u59cb\uff0c\u4e4b\u540e\u6bcf\u4e2a\u94fe\u8868\u7684mSpan\u89c4\u683c\u90fd\u662f\u524d\u4e00\u4e2a\u7684\u4e24\u500d\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487997\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>8KB\u548c16KB\u8fd9\u4e24\u4e2a\u94fe\u8868\uff0c\u5b9e\u9645\u4e0a\u4f1a\u4e00\u76f4\u662f\u7a7a\u7684\uff0c\u7559\u7740\u5b83\u4eec\u662f\u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528mSpan\u5305\u542b\u9875\u9762\u6570\u7684\uff08\u4ee52\u4e3a\u5e95\uff09\u5bf9\u6570\u4f5c\u4e3a\u6570\u7ec4\u4e0b\u6807\u3002stackLarge\u7ed3\u6784\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre><code>\n\/\/ Global pool of large stack spans.\nvar stackLarge struct {\n  lock mutex\n  free [heapAddrBits - pageShift]mSpanList \/\/ free lists by log_2(s.npages)\n}\n\n\/\/ mSpanList heads a linked list of spans.\n\/\/\n\/\/go:notinheap\ntype mSpanList struct {\n  first *mspan \/\/ first span in list, or nil if none\n  last  *mspan \/\/ last span in list, or nil if none\n}<\/code><\/pre>\n<p>\uff08\u4e09\uff09\u5185\u5b58\u5206\u914d\u5982\u679c\u8fd0\u884c\u65f6\u53ea\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6765\u5206\u914d\u5185\u5b58\u7684\u8bdd\uff0c\u52bf\u5fc5\u4f1a\u9020\u6210\u7ebf\u7a0b\u4e4b\u95f4\u7684\u9501\u7ade\u4e89\u8fdb\u800c\u5f71\u54cd\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u6808\u5185\u5b58\u7531\u4e8e\u4e0e\u7ebf\u7a0b\u5173\u7cfb\u6bd4\u8f83\u5bc6\u5207\uff0c\u6240\u4ee5\u5728\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u7f13\u5b58runtime.mcache\u4e2d\u90fd\u52a0\u5165\u4e86\u6808\u7f13\u5b58\u51cf\u5c11\u9501\u7ade\u4e89\u5f71\u54cd\u3002<\/p>\n<p>\u540c\u5806\u5185\u5b58\u5206\u914d\u4e00\u6837\uff0c\u6bcf\u4e2aP\u4e5f\u6709\u7528\u4e8e\u6808\u5206\u914d\u7684\u672c\u5730\u7f13\u5b58(mcache.stackcache)\uff0c\u8fd9\u76f8\u5f53\u4e8e\u662fstackpool\u7684\u672c\u5730\u7f13\u5b58\uff0c\u5728mcache\u4e2d\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/go:notinheap\ntype mcache struct {\n  \/\/ The following members are accessed on every malloc,\n  \/\/ so they are grouped here for better caching.\n  nextSample uintptr \/\/ trigger heap sample after allocating this many bytes\n  scanAlloc  uintptr \/\/ bytes of scannable heap allocated\n\n  \/\/ Allocator cache for tiny objects w\/o pointers.\n  \/\/ See &quot;Tiny allocator&quot; comment in malloc.go.\n\n  \/\/ tiny points to the beginning of the current tiny block, or\n  \/\/ nil if there is no current tiny block.\n  \/\/\n  \/\/ tiny is a heap pointer. Since mcache is in non-GC'd memory,\n  \/\/ we handle it by clearing it in releaseAll during mark\n  \/\/ termination.\n  \/\/\n  \/\/ tinyAllocs is the number of tiny allocations performed\n  \/\/ by the P that owns this mcache.\n  tiny       uintptr\n  tinyoffset uintptr\n  tinyAllocs uintptr\n\n  \/\/ The rest is not accessed on every malloc.\n\n  alloc [numSpanClasses]*mspan \/\/ spans to allocate from, indexed by spanClass\n\n  stackcache [_NumStackOrders]stackfreelist\n\n  \/\/ flushGen indicates the sweepgen during which this mcache\n  \/\/ was last flushed. If flushGen != mheap_.sweepgen, the spans\n  \/\/ in this mcache are stale and need to the flushed so they\n  \/\/ can be swept. This is done in acquirep.\n  flushGen uint32\n}<\/code><\/pre>\n<p>stackcache [_NumStackOrders]stackfreelist\u5373\u4e3a\u6808\u7684\u672c\u5730\u7f13\u5b58\uff0c\u5728Linux\u73af\u5883\u4e0b\uff0c\u6bcf\u4e2aP\u672c\u5730\u7f13\u5b58\u67094\uff08_NumStackOrders\uff09\u79cd\u89c4\u683c\u7684\u7a7a\u95f2\u5185\u5b58\u5757\u94fe\u8868\uff1a2KB\uff0c4KB\uff0c8KB\uff0c16KB\uff0c\u5b9a\u4e49\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>\n\/\/ Number of orders that get caching. Order 0 is FixedStack\n\/\/ and each successive order is twice as large.\n\/\/ We want to cache 2KB, 4KB, 8KB, and 16KB stacks. Larger stacks\n\/\/ will be allocated directly.\n\/\/ Since FixedStack is different on different systems, we\n\/\/ must vary NumStackOrders to keep the same maximum cached size.\n\/\/   OS               | FixedStack | NumStackOrders\n\/\/   -----------------+------------+---------------\n\/\/   linux\/darwin\/bsd | 2KB        | 4\n\/\/   windows\/32       | 4KB        | 3\n\/\/   windows\/64       | 8KB        | 2\n\/\/   plan9            | 4KB        | 3\n_NumStackOrders = 4 - goarch.PtrSize\/4*goos.IsWindows - 1*goos.IsPlan9<\/code><\/pre>\n<p>\u5c0f\u4e8e32KB\u7684\u6808\u5206\u914d\uff1a<\/p>\n<p>\u5bf9\u4e8e\u5c0f\u4e8e32KB\u7684\u6808\u7a7a\u95f4\uff0c\u4f1a\u4f18\u5148\u4f7f\u7528\u5f53\u524dP\u7684\u672c\u5730\u7f13\u5b58\u3002<\/p>\n<p>\u5982\u679c\u672c\u5730\u7f13\u5b58\u4e2d\uff0c\u5bf9\u5e94\u89c4\u683c\u7684\u5185\u5b58\u5757\u94fe\u8868\u4e3a\u7a7a\uff0c\u5c31\u4ecestackpool\u8fd9\u91cc\u5206\u914d16KB\u7684\u5185\u5b58\u653e\u5230\u672c\u5730\u7f13\u5b58\uff08stackcache\uff09\u4e2d\uff0c\u7136\u540e\u7ee7\u7eed\u4ece\u672c\u5730\u7f13\u5b58\u5206\u914d\u3002<\/p>\n<p>\u82e5\u662fstackpool\u4e2d\u5bf9\u5e94\u94fe\u8868\u4e5f\u4e3a\u7a7a\uff0c\u5c31\u4ece\u5806\u5185\u5b58\u76f4\u63a5\u5206\u914d\u4e00\u4e2a32KB\u7684span\u5212\u5206\u6210\u5bf9\u5e94\u7684\u5185\u5b58\u5757\u5927\u5c0f\u653e\u5230stackpool\u4e2d\u3002<\/p>\n<p>\u4e0d\u8fc7\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u662f\u65e0\u6cd5\u4f7f\u7528\u672c\u5730\u7f13\u5b58\u7684\uff0c\u5728\u4e0d\u80fd\u4f7f\u7528\u672c\u5730\u7f13\u5b58\u7684\u60c5\u51b5\u4e0b\uff0c\u5c31\u76f4\u63a5\u4ecestackpool\u5206\u914d\u3002<\/p>\n<p>\u5927\u4e8e\u7b49\u4e8e32KB\u7684\u6808\u5206\u914d\uff1a<\/p>\n<p>\u8ba1\u7b97\u9700\u8981\u7684page\u6570\u76ee\uff0c\u5e76\u4ee52\u4e3a\u5e95\u6c42\u5bf9\u6570\uff08log2page\uff09\uff0c\u5c06\u5f97\u5230\u7684\u7ed3\u679c\u4f5c\u4e3astackLarge\u6570\u7ec4\u7684\u4e0b\u6807\uff0c\u627e\u5230\u5bf9\u5e94\u7684\u7a7a\u95f2mSpan\u94fe\u8868\u3002<\/p>\n<p>\u82e5\u94fe\u8868\u4e0d\u4e3a\u7a7a\uff0c\u5c31\u62ff\u4e00\u4e2a\u8fc7\u6765\u7528\u3002\u5982\u679c\u94fe\u8868\u4e3a\u7a7a\uff0c\u5c31\u76f4\u63a5\u4ece\u5806\u5185\u5b58\u5206\u914d\u4e00\u4e2a\u62e5\u6709\u8fd9\u4e48\u591a\u4e2a\u9875\u9762\u7684span\uff0c\u5e76\u628a\u5b83\u6574\u4e2a\u7528\u4e8e\u5206\u914d\u6808\u5185\u5b58\u3002<\/p>\n<p>\u4f8b\u5982\u60f3\u8981\u5206\u914d64KB\u7684\u6808\uff0c68\/8\u662f8\u4e2apage\uff0clog2page=log2(8)=3\u3002<\/p>\n<p>\uff08\u56db\uff09\u5185\u5b58\u91ca\u653e\u4ec0\u4e48\u65f6\u5019\u91ca\u653e\u6808\uff1f<br \/>\u5982\u679c\u534f\u7a0b\u6808\u6ca1\u6709\u589e\u957f\u8fc7\uff08\u8fd8\u662f2KB\uff09\uff0c\u5c31\u628a\u8fd9\u4e2a\u534f\u7a0b\u653e\u5230\u6709\u6808\u7684\u7a7a\u95f2G\u961f\u5217\u4e2d\u3002<\/p>\n<p>\u5982\u679c\u534f\u7a0b\u6808\u589e\u957f\u8fc7\uff0c\u5c31\u628a\u534f\u7a0b\u6808\u91ca\u653e\u6389\uff0c\u518d\u628a\u534f\u7a0b\u653e\u5165\u5230\u6ca1\u6709\u6808\u7684\u7a7a\u95f2G\u961f\u5217\u4e2d\u3002<\/p>\n<p>\u800c\u8fd9\u4e9b\u7a7a\u95f2\u534f\u7a0b\u7684\u6808\uff0c\u4e5f\u4f1a\u5728GC\u6267\u884cmarkroot\u65f6\u88ab\u91ca\u653e\u6389\uff0c\u5230\u65f6\u5019\u8fd9\u4e9b\u534f\u7a0b\u4e5f\u4f1a\u52a0\u5165\u5230\u6ca1\u6709\u6808\u7684\u7a7a\u95f2\u534f\u7a0b\u961f\u5217\u4e2d\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487998\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u6240\u4ee5\uff0c\u5e38\u89c4goroutine\u6808\u7684\u91ca\u653e\uff0c<\/p>\n<p>\u4e00\u662f\u53d1\u751f\u5728\u534f\u7a0b\u8fd0\u884c\u7ed3\u675f\u65f6\uff0cgfput\u4f1a\u628a\u589e\u957f\u8fc7\u7684\u6808\u91ca\u653e\u6389\uff0c\u6808\u6ca1\u6709\u589e\u957f\u8fc7\u7684g\u4f1a\u88ab\u653e\u5165sched.gFree.stack\u4e2d\uff1b<\/p>\n<p>\u4e8c\u662fGC\u4f1a\u5904\u7406sched.gFree.stack\u94fe\u8868\uff0c\u628a\u8fd9\u91cc\u9762\u6240\u6709g\u7684\u6808\u90fd\u91ca\u653e\u6389\uff0c\u7136\u540e\u628a\u5b83\u4eec\u653e\u5165sched.gFree.noStack\u94fe\u8868\u4e2d\u3002<\/p>\n<p>\u534f\u7a0b\u6808\u91ca\u653e\u65f6\u662f\u653e\u56de\u5f53\u524dP\u7684\u672c\u5730\u7f13\u5b58\uff1f\u8fd8\u662f\u653e\u56de\u5168\u5c40\u6808\u7f13\u5b58\uff1f\u8fd8\u662f\u76f4\u63a5\u8fd8\u7ed9\u5806\u5185\u5b58\uff1f\u5176\u5b9e\u90fd\u6709\u53ef\u80fd\uff0c\u8981\u89c6\u60c5\u51b5\u800c\u5b9a\uff0c\u540c\u6808\u5206\u914d\u65f6\u4e00\u6837\uff0c\u5c0f\u4e8e32KB\u548c\u5927\u4e8e\u7b49\u4e8e32KB\u7684\u6808\uff0c\u5728\u91ca\u653e\u7684\u65f6\u5019\u4e5f\u4f1a\u533a\u522b\u5bf9\u5f85\u3002<\/p>\n<p>\u5c0f\u4e8e32KB\u7684\u6808\uff0c\u91ca\u653e\u65f6\u4f1a\u5148\u653e\u56de\u5230\u672c\u5730\u7f13\u5b58\u4e2d\u3002\u5982\u679c\u672c\u5730\u7f13\u5b58\u5bf9\u5e94\u94fe\u8868\u4e2d\u6808\u7a7a\u95f4\u603b\u548c\u5927\u4e8e32KB\u4e86\uff0c\u5c31\u628a\u4e00\u90e8\u5206\u653e\u56destackpool\u4e2d\uff0c\u672c\u5730\u8fd9\u4e2a\u94fe\u8868\u53ea\u4fdd\u755916KB\u3002<\/p>\n<p>\u5982\u679c\u672c\u5730\u7f13\u5b58\u4e0d\u53ef\u7528\uff0c\u4e5f\u4f1a\u76f4\u63a5\u653e\u56destackpool\u4e2d\u3002\u800c\u4e14\uff0c\u5982\u679c\u53d1\u73b0\u8fd9\u4e2amSpan\u4e2d\u6240\u6709\u5185\u5b58\u5757\u90fd\u88ab\u91ca\u653e\u4e86\uff0c\u5c31\u4f1a\u628a\u5b83\u5f52\u8fd8\u7ed9\u5806\u5185\u5b58\u3002<\/p>\n<p>\u5bf9\u4e8e\u5927\u4e8e\u7b49\u4e8e32KB\u7684\u6808\u91ca\u653e\uff0c\u5982\u679c\u5f53\u524d\u5904\u5728GC\u6e05\u7406\u9636\u6bb5\uff08gcphase==_GCoff\uff09\uff0c\u5c31\u76f4\u63a5\u91ca\u653e\u5230\u5806\u5185\u5b58\uff0c\u5426\u5219\u5c31\u5148\u628a\u5b83\u653e\u56deStackLarge\u8fd9\u91cc\u3002<\/p>\n<p>\uff08\u4e94\uff09\u6808\u6269\u5bb9\u5728goroutine\u8fd0\u884c\u7684\u65f6\u5019\u6808\u533a\u4f1a\u6309\u7167\u9700\u8981\u589e\u957f\u548c\u6536\u7f29\uff0c\u5360\u7528\u7684\u5185\u5b58\u6700\u5927\u9650\u5236\u7684\u9ed8\u8ba4\u503c\u572864\u4f4d\u7cfb\u7edf\u4e0a\u662f1GB\u3002\u6808\u5927\u5c0f\u7684\u521d\u59cb\u503c\u548c\u4e0a\u9650\u8fd9\u90e8\u5206\u7684\u8bbe\u7f6e\u90fd\u53ef\u4ee5\u5728Go\u7684\u6e90\u7801runtime\/stack.go\u67e5\u770b\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042487999\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u6269\u5bb9\u6d41\u7a0b\u7f16\u8bd1\u5668\u4f1a\u4e3a\u51fd\u6570\u8c03\u7528\u63d2\u5165\u8fd0\u884c\u65f6\u68c0\u67e5runtime.morestack\uff0c\u5b83\u4f1a\u5728\u51e0\u4e4e\u6240\u6709\u7684\u51fd\u6570\u8c03\u7528\u4e4b\u524d\u68c0\u67e5\u5f53\u524dgoroutine\u7684\u6808\u5185\u5b58\u662f\u5426\u5145\u8db3\uff0c\u5982\u679c\u5f53\u524d\u6808\u9700\u8981\u6269\u5bb9\uff0c\u4f1a\u8c03\u7528runtime.newstack\u521b\u5efa\u65b0\u7684\u6808 \u3002<\/p>\n<p>\u65e7\u6808\u7684\u5927\u5c0f\u662f\u901a\u8fc7\u6211\u4eec\u4e0a\u9762\u8bf4\u7684\u4fdd\u5b58\u5728goroutine\u4e2d\u7684stack\u4fe1\u606f\u91cc\u8bb0\u5f55\u7684\u6808\u533a\u5185\u5b58\u8fb9\u754c\u8ba1\u7b97\u51fa\u6765\u7684\uff0c\u7136\u540e\u7528\u65e7\u6808\u4e24\u500d\u7684\u5927\u5c0f\u521b\u5efa\u65b0\u6808\uff0c\u521b\u5efa\u524d\u4f1a\u68c0\u67e5\u662f\u65b0\u6808\u7684\u5927\u5c0f\u662f\u5426\u8d85\u8fc7\u4e86\u5355\u4e2a\u6808\u7684\u5185\u5b58\u4e0a\u9650\u3002<\/p>\n<p>\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u6700\u590d\u6742\u7684\u5730\u65b9\u662f\u5c06\u6307\u5411\u6e90\u6808\u4e2d\u5185\u5b58\u7684\u6307\u9488\u8c03\u6574\u4e3a\u6307\u5411\u65b0\u7684\u6808\uff0c\u8fd9\u4e00\u6b65\u5b8c\u6210\u540e\u5c31\u4f1a\u91ca\u653e\u6389\u65e7\u6808\u7684\u5185\u5b58\u7a7a\u95f4\u4e86<\/p>\n<p>\uff08\u516d\uff09\u6808\u7f29\u5bb9\u5728goroutine\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u6808\u533a\u7684\u7a7a\u95f4\u4f7f\u7528\u7387\u4e0d\u8d85\u8fc71\/4\uff0c\u90a3\u4e48\u5728\u5783\u573e\u56de\u6536\u7684\u65f6\u5019\u4f7f\u7528runtime.shrinkstack\u8fdb\u884c\u6808\u7f29\u5bb9\uff0c\u5f53\u7136\u8fdb\u884c\u7f29\u5bb9\u524d\u4f1a\u6267\u884c\u4e00\u5806\u524d\u7f6e\u68c0\u67e5\uff0c\u90fd\u901a\u8fc7\u4e86\u624d\u4f1a\u8fdb\u884c\u7f29\u5bb9\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042488000\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u7f29\u5bb9\u6d41\u7a0b\uff1a<\/p>\n<p>\u5982\u679c\u8981\u89e6\u53d1\u6808\u7684\u7f29\u5bb9\uff0c\u65b0\u6808\u7684\u5927\u5c0f\u4f1a\u662f\u539f\u59cb\u6808\u7684\u4e00\u534a\uff0c\u5982\u679c\u65b0\u6808\u7684\u5927\u5c0f\u4f4e\u4e8e\u7a0b\u5e8f\u7684\u6700\u4f4e\u9650\u52362KB\uff0c\u90a3\u4e48\u7f29\u5bb9\u7684\u8fc7\u7a0b\u5c31\u4f1a\u505c\u6b62\u3002<\/p>\n<p>\u7f29\u5bb9\u4e5f\u4f1a\u8c03\u7528\u6269\u5bb9\u65f6\u4f7f\u7528\u7684runtime.copystack\u51fd\u6570\u5f00\u8f9f\u65b0\u7684\u6808\u7a7a\u95f4\uff0c\u5c06\u65e7\u6808\u7684\u6570\u636e\u62f7\u8d1d\u5230\u65b0\u6808\u4ee5\u53ca\u8c03\u6574\u539f\u6765\u6307\u9488\u7684\u6307\u5411\u3002<\/p>\n<p>\u552f\u4e00\u53d1\u8d77\u6808\u6536\u7f29\u7684\u5730\u65b9\u5c31\u662fGC\u3002GC\u901a\u8fc7scanstack\u51fd\u6570\u5bfb\u627e\u6807\u8bb0root\u8282\u70b9\u65f6\uff0c\u5982\u679c\u53d1\u73b0\u53ef\u4ee5\u5b89\u5168\u7684\u6536\u7f29\u6808\uff0c\u5c31\u4f1a\u6267\u884c\u6808\u6536\u7f29\uff0c\u4e0d\u80fd\u9a6c\u4e0a\u6267\u884c\u65f6\uff0c\u5c31\u8bbe\u7f6e\u6808\u6536\u7f29\u6807\u8bc6\uff08g.preemptShrink=true\uff09\uff0c\u7b49\u5230\u534f\u7a0b\u68c0\u6d4b\u5230\u62a2\u5360\u6807\u8bc6\uff08stackPreempt\uff09\u3002<\/p>\n<p>\u5728\u8ba9\u51faCPU\u4e4b\u524d\u4f1a\u68c0\u67e5\u8fd9\u4e2a\u6808\u6536\u7f29\u6807\u8bc6\uff0c\u4e3atrue\u7684\u8bdd\u5c31\u4f1a\u5148\u8fdb\u884c\u6808\u6536\u7f29\uff0c\u518d\u8ba9\u51faCPU\u3002<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<p>1.GoLang\u4e4b\u6808\u5185\u5b58\u7ba1\u7406<\/p>\n<p>2.\u767d\u8bddGo\u8bed\u8a00\u5185\u5b58\u7ba1\u7406\u4e09\u90e8\u66f2\uff08\u4e8c\uff09\u89e3\u5bc6\u6808\u5185\u5b58\u7ba1\u7406<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042479986\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u5982\u679c\u4f60\u662f\u817e\u8baf\u6280\u672f\u5185\u5bb9\u521b\u4f5c\u8005\uff0c\u817e\u8baf\u4e91\u5f00\u53d1\u8005\u793e\u533a\u8bda\u9080\u60a8\u52a0\u5165<a href=\"https:\/\/link.segmentfault.com\/?enc=HNnaEi2LyBmJKZ1bYEKAuQ%3D%3D.TXlAf6Qt%2B%2FWCto0wbzS8g1pDvAz9w41DPOlw9C9hRZHUXdTRaj3ZngGlla%2F6p2MlotgbLE%2BkUS%2BRhDE2Qrf%2BCw%3D%3D\" rel=\"nofollow\">\u3010\u817e\u8baf\u4e91\u539f\u521b\u5206\u4eab\u8ba1\u5212\u3011<\/a>\uff0c\u9886\u53d6\u793c\u54c1\uff0c\u52a9\u529b\u804c\u7ea7\u664b\u5347\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042487987\">https:\/\/segmentfault.com\/a\/1190000042487987<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bfc\u8bed\u00a0|\u00a0\u672c\u6587\u63a8\u9009\u81ea\u817e\u8baf\u4e91\u5f00\u53d1\u8005\u793e\u533a-\u3010\u6280\u601d\u5e7f\u76ca \u00b7 \u817e\u8baf\u6280\u672f\u4eba\u539f\u521b\u96c6\u3011\u4e13\u680f\u3002\u8be5\u4e13\u680f\u662f\u817e\u8baf\u4e91\u5f00\u53d1\u8005\u793e\u533a\u4e3a\u817e\u8baf\u6280 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006497","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006497"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006497\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}