{"id":4352,"date":"2020-06-16T17:20:19","date_gmt":"2020-06-16T09:20:19","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4352"},"modified":"2020-06-16T17:20:19","modified_gmt":"2020-06-16T09:20:19","slug":"tcmalloc%e5%8e%9f%e7%90%86%e5%89%96%e6%9e%90","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4352","title":{"rendered":"tcmalloc\u539f\u7406\u5256\u6790"},"content":{"rendered":"<header>\n<p id=\"page-title\" class=\"page__title\"><a href=\"http:\/\/lemon0910.github.io\/%E7%BC%96%E7%A8%8B\/tcmalloc\/#\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>&nbsp;<\/p>\n<p class=\"page__meta\">\u00a0\u5c11\u4e8e 1 \u5206\u949f\u8bfb\u5b8c<\/p>\n<\/header>\n<section class=\"page__content\">Tcmalloc\u662f\u8c37\u6b4c\u5f00\u53d1\u7684\u4e00\u4e2a\u5185\u5b58\u5206\u914d\u5668\uff0c\u7ba1\u7406\u5806\u5185\u5b58\uff0c\u4e3b\u8981\u5f71\u54cdmalloc\u548cfree\uff0c\u7528\u4e8e\u964d\u4f4e\u9891\u7e41\u5206\u914d\u3001\u91ca\u653e\u5185\u5b58\u9020\u6210\u7684\u6027\u80fd\u635f\u8017\uff0c\u5e76\u4e14\u6709\u6548\u5730\u63a7\u5236\u5185\u5b58\u788e\u7247\u3002glibc\u4e2d\u7684\u5185\u5b58\u5206\u914d\u5668\u662fptmalloc2\uff0ctcmalloc\u53f7\u79f0\u8981\u6bd4\u5b83\u5feb\u3002\u4e00\u6b21malloc\u548cfree\u64cd\u4f5c\uff0cptmalloc\u9700\u8981300ns\uff0c\u800ctcmalloc\u53ea\u898150ns\u3002\u540c\u65f6tcmalloc\u4e5f\u4f18\u5316\u4e86\u5c0f\u5bf9\u8c61\u7684\u5b58\u50a8\uff0c\u9700\u8981\u66f4\u5c11\u7684\u7a7a\u95f4\u3002tcmalloc\u7279\u522b\u5bf9\u591a\u7ebf\u7a0b\u505a\u4e86\u4f18\u5316\uff0c\u5bf9\u4e8e\u5c0f\u5bf9\u8c61\u7684\u5206\u914d\u57fa\u672c\u4e0a\u662f\u4e0d\u5b58\u5728\u9501\u7ade\u4e89\uff0c\u800c\u5927\u5bf9\u8c61\u4f7f\u7528\u4e86\u7ec6\u7c92\u5ea6\u3001\u9ad8\u6548\u7684\u81ea\u65cb\u9501\uff08spinlock\uff09\u3002\u5206\u914d\u7ed9\u7ebf\u7a0b\u7684\u672c\u5730\u7f13\u5b58\uff0c\u5728\u957f\u65f6\u95f4\u7a7a\u95f2\u7684\u60c5\u51b5\u4e0b\u4f1a\u88ab\u56de\u6536\uff0c\u4f9b\u5176\u4ed6\u7ebf\u7a0b\u4f7f\u7528\uff0c\u8fd9\u6837\u63d0\u9ad8\u4e86\u5728\u591a\u7ebf\u7a0b\u60c5\u51b5\u4e0b\u7684\u5185\u5b58\u5229\u7528\u7387\uff0c\u4e0d\u4f1a\u6d6a\u8d39\u5185\u5b58\uff0c\u800c\u8fd9\u4e00\u70b9ptmalloc2\u662f\u505a\u4e0d\u5230\u7684\u3002<\/p>\n<h2 id=\"\u6574\u4f53\u67b6\u6784\">\u6574\u4f53\u67b6\u6784<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/lemon0910.github.io\/images\/tcmalloc\/1.png\" alt=\"system\" \/><\/p>\n<p>Tcmalloc\u5305\u62ec\u7ebf\u7a0b\u5185\u5b58\u548c\u4e2d\u5fc3\u5806\u4e24\u90e8\u5206\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u4e00\u4e2aThread Cache\uff0c\u5b83\u8d1f\u8d23\u5206\u914d\u6bcf\u4e2a\u7ebf\u7a0b\u6240\u9700\u8981\u7684\u5c0f\u5185\u5b58\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5c31\u662f\u7ebf\u7a0b\u4e4b\u95f4\u5206\u914d\u5c0f\u5185\u5b58\u4e0d\u5b58\u5728\u9501\u7ade\u4e89\uff0c\u63d0\u9ad8\u4e86\u5c0f\u5185\u5b58\u7684\u5206\u914d\u6548\u7387\u3002\u5bf9\u4e8e\u5927\u5757\u7684\u5185\u5b58\uff0c\u6240\u6709\u7ebf\u7a0b\u90fd\u76f4\u63a5\u627e\u4e2d\u5fc3\u5806\u5206\u914d\uff0c\u4e3a\u4e86\u5e94\u5bf9\u5e76\u53d1\uff0c\u5927\u5bf9\u8c61\u4f7f\u7528\u4e86\u7ec6\u7c92\u5ea6\u9ad8\u6548\u7684\u81ea\u65cb\u9501\u3002<\/p>\n<h2 id=\"\u5c0f\u5185\u5b58\u5206\u914d\">\u5c0f\u5185\u5b58\u5206\u914d<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/lemon0910.github.io\/images\/tcmalloc\/2.png\" alt=\"system\" \/><\/p>\n<p>Thread Cache\u7684\u4e3b\u8981\u6570\u636e\u7ed3\u6784\u5c31\u662f\u4e0a\u56feFree List\uff0c\u603b\u5171\u670960\u591a\u4e2aclass\uff0c\u6bcf\u4e2aclass\u7ba1\u7406\u76f8\u540c\u5927\u5c0f\u7684\u5185\u5b58\u5757\u3002\u4e0d\u540cclass\u7ba1\u7406\u7684\u5185\u5b58\u5757\u5927\u5c0f\u4e0d\u540c\uff0c\u4f9d\u6b21\u63098\u5b57\u8282\u300116\u5b57\u8282\u300132\u5b57\u8282\u7b49\u95f4\u9694\u5f00\u3002\u5728\u5185\u5b58\u5206\u914d\u7684\u65f6\u5019\uff0c\u4f1a\u627e\u5230\u6700\u5c0f\u590d\u5408\u6761\u4ef6\u7684\uff0c\u6bd4\u5982833\u5b57\u8282\u52301024\u5b57\u8282\u7684\u5185\u5b58\u5206\u914d\u8bf7\u6c42\u90fd\u4f1a\u5206\u914d\u4e00\u4e2a1024\u5927\u5c0f\u7684\u5185\u5b58\u5757\u3002\u5982\u679c\u8fd9\u4e9b\u5206\u914d\u5668\u7684\u5269\u4f59\u5185\u5b58\u4e0d\u591f\u4e86\uff0c\u4f1a\u5411\u4e2d\u5fc3\u5806\u7533\u8bf7\u4e00\u4e9b\u5185\u5b58\uff0c\u6253\u788e\u4ee5\u540e\u586b\u5165\u5bf9\u5e94\u5206\u914d\u5668\u4e2d\u3002 \u8fd9\u91cc\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\u5c31\u662f\u6bcf\u4e2aclass\u7ba1\u7406\u7684\u5185\u5b58\u5757\u591a\u5c11\u7684\u95ee\u9898\u3002\u5728\u7ebf\u7a0b\u5185\u5b58\u6c60\u6bcf\u6b21\u4ece\u4e2d\u592e\u5806\u7533\u8bf7\u5185\u5b58\u7684\u65f6\u5019\uff0c\u5206\u914d\u591a\u5c11\u5185\u5b58\u4e5f\u76f4\u63a5\u5f71\u54cd\u5206\u914d\u6027\u80fd\u3002\u7533\u8bf7\u5730\u592a\u5c11\u4f1a\u5bfc\u81f4\u9891\u7e41\u8bbf\u95ee\u4e2d\u5fc3\u5806\uff0c\u4e5f\u5c31\u4f1a\u9891\u7e41\u52a0\u9501\uff0c\u800c\u7533\u8bf7\u5730\u592a\u591a\u4f1a\u5bfc\u81f4\u5185\u5b58\u6d6a\u8d39\u3002\u5728tcmalloc\u91cc\uff0c\u8fd9\u4e2a\u6bcf\u6b21\u7533\u8bf7\u7684\u5185\u5b58\u91cf\u662f\u52a8\u6001\u8c03\u6574\u7684\uff0c\u8c03\u6574\u65b9\u5f0f\u4f7f\u7528\u4e86\u7c7b\u4f3c\u628atcp\u7a97\u53e3\u53cd\u8fc7\u6765\u7528\u7684\u6162\u542f\u52a8\uff08slow start\uff09\u7b97\u6cd5\u8c03\u6574\u3002<\/p>\n<h2 id=\"\u5927\u5185\u5b58\u7684\u5206\u5e03\">\u5927\u5185\u5b58\u7684\u5206\u5e03<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/lemon0910.github.io\/images\/tcmalloc\/3.png\" alt=\"system\" \/><\/p>\n<p>\u5bf9\u4e8e\u5927\u5185\u5b58\u5206\u914d(\u5927\u4e8e8\u4e2a\u5206\u9875, \u537332K)\uff0cTcmalloc\u76f4\u63a5\u5728\u4e2d\u592e\u5806\u91cc\u5206\u914d\u3002\u4e2d\u592e\u5806\u7684\u5185\u5b58\u7ba1\u7406\u662f\u4ee5\u5206\u9875\u4e3a\u5355\u4f4d\u7684\uff0c\u540c\u6837\u6309\u5927\u5c0f\u7ef4\u62a4\u4e86256\u4e2a\u7a7a\u95f2\u7a7a\u95f4\u94fe\u8868\uff0c\u524d255\u4e2a\u5206\u522b\u662f1\u4e2a\u5206\u9875\u30012\u4e2a\u5206\u9875\u5230255\u4e2a\u5206\u9875\u7684\u7a7a\u95f2\u7a7a\u95f4\uff0c\u6700\u540e\u4e00\u4e2a\u662f\u66f4\u591a\u5206\u9875\u7684\u5c0f\u7684\u7a7a\u95f4\u3002\u8fd9\u91cc\u7684\u7a7a\u95f4\u5982\u679c\u4e0d\u591f\u7528\uff0c\u5c31\u4f1a\u76f4\u63a5\u4ece\u7cfb\u7edf\u7533\u8bf7\u4e86\u3002<\/p>\n<h2 id=\"\u5206\u9875\u7ba1\u7406spans\">\u5206\u9875\u7ba1\u7406Spans<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/lemon0910.github.io\/images\/tcmalloc\/4.png\" alt=\"system\" \/><\/p>\n<p>Tcmalloc\u4f7f\u7528Span\u6765\u7ba1\u7406\u5185\u5b58\u5206\u9875\uff0c\u4e00\u4e2aspan\u53ef\u4ee5\u5305\u542b\u51e0\u4e2a\u8fde\u7eed\u5206\u9875\u3002\u4e00\u4e2aspan\u7684\u72b6\u6001\u53ea\u6709\u672a\u5206\u914d(\u8fd9\u65f6\u5019\u5728\u7a7a\u95f2\u94fe\u8868\u4e2d)\uff0c\u4f5c\u4e3a\u5927\u5bf9\u8c61\u5206\u914d\uff0c\u6216\u4f5c\u4e3a\u5c0f\u5bf9\u8c61\u5206\u914d\uff08\u8fd9\u65f6\u5019span\u5185\u8bb0\u5f55\u4e86\u5c0f\u5bf9\u8c61\u7684class size\uff09\u3002\u572832\u4f4d\u7cfb\u7edf\u4e2d\uff0cSpan\u5206\u4e3a\u4e24\u7ea7\u7531\u4e2d\u592e\u5206\u914d\u5668\u7ba1\u7406\u3002\u7b2c\u4e00\u7ea7\u67092^5\u4e2a\u8282\u70b9\uff0c\u7b2c\u4e8c\u7ea7\u662f2^15\u4e2a\u300232\u4f4d\u603b\u5171\u53ea\u80fd\u67092^20\u4e2a\u5206\u9875\uff08\u6bcf\u4e2a\u5206\u98754KB = 2^12\uff09\u3002<\/p>\n<h2 id=\"\u5185\u5b58\u56de\u6536\">\u5185\u5b58\u56de\u6536<\/h2>\n<p>\u5f53\u5185\u5b58\u88ab\u91ca\u653e\u65f6\uff0c\u6211\u4eec\u4f1a\u8ba1\u7b97\u8be5\u5185\u5b58\u6240\u5728\u7684\u9875\u53f7\u5e76\u627e\u5230\u5bf9\u5e94\u7684Span\u3002Span\u4fbf\u53ef\u786e\u5b9a\u8be5\u5185\u5b58\u662f\u5927\u5185\u5b58\u8fd8\u662f\u5c0f\u5185\u5b58\u3002\u5982\u679c\u662f\u5c0f\u5185\u5b58\u5e76\u4e14\u53ef\u4ee5\u63d2\u5165\u5230free list\u4e2d\uff0c\u6211\u4eec\u4fbf\u628a\u5b83\u63d2\u5165\u5230\u5f53\u524dthread cache\u4e2dfree list\u7684\u5408\u9002\u5730\u65b9\u3002\u5982\u679c\u662f\u5c0f\u5185\u5b58\u4f46\u4f46\u5927\u4e8e2M\u5c31\u4f1a\u88ab\u5783\u573e\u56de\u6536\u673a\u5236\u56de\u6536\u5230\u4e2d\u5fc3\u5806\u3002 \u5982\u679c\u662f\u5927\u5185\u5b58\uff0c\u6211\u4eec\u4fbf\u4f1a\u83b7\u53d6\u5b83\u5bf9\u5e94\u7684\u9875\u53f7\uff0c\u5e76\u5c1d\u8bd5\u548c\u5de6\u53f3\u7a7a\u95f2\u7684\u9875\u5408\u5e76\u3002\u7136\u540e\u751f\u6210\u65b0\u7684Span\u7ba1\u7406\u8fd9\u5757\u5185\u5b58\u5e76\u653e\u5230\u9875\u7ba1\u7406\u5668\u4e2d\u3002<\/p>\n<h2 id=\"\u5783\u573e\u56de\u6536\">\u5783\u573e\u56de\u6536<\/h2>\n<p>\u5783\u573e\u56de\u6536\u673a\u5236\u53ef\u4ee5\u4fdd\u8bc1\u6bcf\u4e2aThread Cache\u7684\u5927\u5c0f\u5408\u9002\uff0c\u5982\u679c\u4e00\u4e2aThread Cache\u6709\u592a\u591a\u7684\u7a7a\u95f2\u5185\u5b58\u5c31\u4f1a\u88ab\u6536\u96c6\uff0c\u4ece\u800c\u4fdd\u8bc1\u6bcf\u4e2aThread Cache\u90fd\u53ef\u4ee5\u83b7\u5f97\u76f8\u5e94\u7684\u5185\u5b58\u3002\u5783\u573e\u4f1a\u540e\u673a\u5236\u53ea\u5728\u5185\u5b58\u91ca\u653e\u65f6\u624d\u4f1a\u88ab\u89e6\u53d1\u3002<\/p>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 &nbsp; \u00a0\u5c11\u4e8e 1 \u5206\u949f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-4352","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4352"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4352\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}