{"id":84250,"date":"2020-05-15T17:49:30","date_gmt":"2020-05-15T09:49:30","guid":{"rendered":"http:\/\/4563.org\/?p=84250"},"modified":"2020-05-15T17:49:30","modified_gmt":"2020-05-15T09:49:30","slug":"%e8%af%91-%ef%bd%83%e7%a8%8b%e5%ba%8f%e5%91%98%e8%af%a5%e7%9f%a5%e9%81%93%e7%9a%84%e5%86%85%e5%ad%98%e7%9f%a5%e8%af%86-2","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=84250","title":{"rendered":"[\u8bd1] \uff23\u7a0b\u5e8f\u5458\u8be5\u77e5\u9053\u7684\u5185\u5b58\u77e5\u8bc6 (2)"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [\u8bd1] \uff23\u7a0b\u5e8f\u5458\u8be5\u77e5\u9053\u7684\u5185\u5b58\u77e5\u8bc6 (2)               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : felix021 <\/span>  <span><i><\/i> 10<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u611f\u89c9\u8fd9\u7bc7\u6587\u7ae0\u53ef\u80fd\u662f\u592a\u5e72\u4e86\uff0c\u4e0d\u8fc7\u65e2\u7136\u6316\u4e86\u5751\uff0c\u8fd8\u662f\u52aa\u529b\u586b\u3002\u6709\u4efb\u4f55\u95ee\u9898\u90fd\u6b22\u8fce\u63a2\u8ba8\u3002<\/p>\n<p>\u7eed\u4e0a\u7bc7\uff1a<\/p>\n<ul>\n<li>[\u8bd1] \uff23\u7a0b\u5e8f\u5458\u8be5\u77e5\u9053\u7684\u5185\u5b58\u77e5\u8bc6 (1)<\/li>\n<\/ul>\n<p>\u8fd9\u662f\u672c\u7cfb\u5217\u7684\u7b2c\u4e8c\u7bc7\uff0c\u9884\u8ba1\u8fd8\u4f1a\u6709 2 \u7bc7\uff0c\u611f\u5174\u8da3\u7684\u540c\u5b66\u8bb0\u5f97\u5173\u6ce8\uff0c\u4ee5\u4fbf\u63a5\u6536\u63a8\u9001\uff0c\u7b49\u4e0d\u53ca\u7684\u63a8\u8350\u9605\u8bfb\u539f\u6587\u3002<\/p>\n<p>\u5148\u653e\u56fe\u9547\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200516_5ebf48db2a39a.png\" alt=\"[\u8bd1] \uff23\u7a0b\u5e8f\u5458\u8be5\u77e5\u9053\u7684\u5185\u5b58\u77e5\u8bc6 (2)\" \/><\/p>\n<p>\u6765\u6e90\uff1aLinux \u5730\u5740\u7a7a\u95f4\u5e03\u5c40 &#8211; by Gustavo Duarte<\/p>\n<p>\u5173\u4e8e\u56fe\u7247\u7684\u89e3\u91ca\u53ef\u53c2\u89c1\u4e0a\u7bc7\u3002<\/p>\n<p>\u5f00\u59cb\u5427\u3002<\/p>\n<h1>\u7406\u89e3\u5806\u4e0a\u7684\u5185\u5b58\u5206\u914d<\/h1>\n<p> <\/p>\n<p>\u5de5\u5177\u7bb1\uff1a<\/p>\n<ul>\n<li>brk(), sbrk() &#8211; \u4fee\u6539\u6570\u636e\u6bb5\u7684\u5927\u5c0f<\/li>\n<li>malloc() \u5bb6\u65cf\u00a0&#8211; \u53ef\u79fb\u690d\u7684 libc \u5185\u5b58\u5206\u914d\u5668<\/li>\n<\/ul>\n<p>\u5806\u4e0a\u7684\u5185\u5b58\u5206\u914d\uff0c\u6700\u7b80\u5355\u7684\u5b9e\u73b0\u53ef\u4ee5\u662f\u4fee\u6539 program break[2]\uff08\u8bd1\u6ce8\uff1a\u53c2\u89c1\u4e0a\u56fe\u4e2d\u90e8\u53f3\u4fa7\uff09\u7684\u4e0a\u754c\uff0c\u7533\u8bf7\u5bf9\u539f\u4f4d\u7f6e\u548c\u65b0\u4f4d\u7f6e\u4e4b\u95f4\u5185\u5b58\u7684\u8bbf\u95ee\u6743\u9650\u3002\u5982\u679c\u5c31\u8fd9\u4e48\u641e\u7684\u8bdd\uff0c\u5806\u4e0a\u5185\u5b58\u5206\u914d\u548c\u6808\u4e0a\u5206\u914d\u4e00\u6837\u5feb\uff08\u9664\u4e86\u6362\u9875\u7684\u5f00\u9500\uff0c\u4e00\u822c\u6211\u4eec\u8ba4\u4e3a\u6808\u662f\u88ab\u9501\u5b9a\u5728\u5185\u5b58\u4e2d\uff1b\u8bd1\u6ce8\uff1a\u6307\u6808\u4e0d\u4f1a\u88ab\u6362\u51fa\u5230\u78c1\u76d8\uff0c\u53ef\u4ee5\u7528 mlock \u9650\u5236 OS \u5bf9\u4e00\u6bb5\u5730\u5740\u7a7a\u95f4\u4f7f\u7528 swap \uff09\u3002\u4f46\u8fd9\u91cc\u8fd8\u662f\u6709\u53ea\u732b\uff08 cat \uff09\uff0c\u6211\u662f\u8bf4\uff0c\u6709\u70b9\u6bdb\u75c5\uff08 catch \uff09\uff0c\u89c1\u9b3c\u3002\uff08\u8bd1 tu \u6ce8 cao\uff1a\u8fd9\u4e2a\u771f\u96be\u7ffb\uff09<\/p>\n<pre><code>char\u00a0*block\u00a0=\u00a0sbrk(1024\u00a0*\u00a0sizeof(char)); <\/code><\/pre>\n<ul>\n<li>\u6211\u4eec\u65e0\u6cd5\u56de\u6536\u4e0d\u518d\u4f7f\u7528\u7684\u5185\u5b58\u5757<\/li>\n<li>\u5b83\u4e5f\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u5806\u662f\u5728\u7ebf\u7a0b\u95f4\u5171\u4eab\u7684<\/li>\n<li>\u8fd9\u4e2a\u63a5\u53e3\uff08\u8bd1\u6ce8\uff1asbrk \uff09\u4e5f\u5f88\u96be\u79fb\u690d\uff0c\u56e0\u6b64\u5e93\u51fd\u6570\u88ab\u7981\u6b62\u78b0\u8fd9\u4e2a break \u3002<\/li>\n<\/ul>\n<blockquote>\n<p>man\u00a03\u00a0sbrk\u00a0\u2014 \u5404\u79cd\u7cfb\u7edf\u7684\u00a0sbrk \u4f7f\u7528\u591a\u79cd\u4e0d\u540c\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u5e38\u89c1\u7684\u5305\u62ec int, ssize_t, ptrdiff_t, intptr_t<\/p>\n<\/blockquote>\n<p>\u7531\u4e8e\u8fd9\u4e9b\u95ee\u9898\uff0clibc \u8981\u6c42\u5b9e\u73b0\u7edf\u4e00\u7684\u5185\u5b58\u5206\u914d\u63a5\u53e3\uff0c\u5177\u4f53\u5b9e\u73b0\u6709\u5f88\u591a[3]\uff08\u8bd1\u6ce8\uff1a\u4f8b\u5982 glibc\uff0cjemalloc\uff0ctcmalloc \u7b49\uff09\uff0c\u4f46\u90fd\u7ed9\u4f60\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u5b89\u5168\u3001\u652f\u6301\u4efb\u610f\u5c3a\u5bf8\u7684\u5185\u5b58\u5206\u914d\u5668\u2026\u2026\u53ea\u662f\u8981\u4ed8\u51fa\u70b9\u4ee3\u4ef7\u2014\u2014\u5ef6\u8fdf\uff0c\u56e0\u4e3a\u5f97\u5f15\u5165\u9501\u673a\u5236\uff0c\u4ee5\u53ca\u7528\u6765\u7ef4\u62a4\u5df2\u7528 \/\u53ef\u7528\u5185\u5b58\u7684\u6570\u636e\u7ed3\u6784\uff0c\u548c\u989d\u5916\u7684\u5185\u5b58\u5f00\u9500\u3002\u8fd8\u6709\uff0c\u5806\u4e5f\u4e0d\u662f\u552f\u4e00\u7684\u9009\u9879\uff0c\u5728\u5927\u5757\u5185\u5b58\u5206\u914d\u7684\u60c5\u51b5\u4e0b\u5e38\u5e38\u4e5f\u4f1a\u7528\u5185\u5b58\u6620\u5c04\u6bb5\uff08 Memory Mapping Segment\uff0cMMS \uff09\u3002<\/p>\n<blockquote>\n<p>man 3 malloc \u2014\u2014\u00a0\u4e00\u822c\u6765\u8bf4\uff0cmalloc() \u4ece\u5806\u4e0a\u5206\u914d\u5185\u5b58, \u00a0&#8230; \u5f53\u5206\u914d\u7684\u5185\u5b58\u5757\u5927\u4e8e MMAP_THRESHOLD \u65f6\uff0cglibc \u7684 malloc() \u5b9e\u73b0\u4f7f\u7528\u79c1\u6709\u533f\u540d\u6620\u5c04\u6765\u5206\u914d\u5185\u5b58\u3002<\/p>\n<\/blockquote>\n<p>\uff08\u8bd1\u6ce8\uff1aLinux \u4e0b mmap \u7684 flags \u53c2\u6570\u662f\u4e2a bitmap\uff0c\u5176\u4e2d\u6709\u4e24\u4e2a bit \u5206\u522b\u4e3a MAP_PRIVATE \u3001MAP_ANONYMOUS\uff0c\u5bf9\u5e94\u5f15\u7528\u4e2d\u63d0\u5230\u7684\u201c\u79c1\u6709\u201d\u3001\u201c\u533f\u540d\u201d\uff09<\/p>\n<p>\u7531\u4e8e\u5806\u7a7a\u95f4\u5728 start_brk \u548c brk\u00a0\uff08\u8bd1\u6ce8\uff1a\u5373 heap \u7684\u4e0b\u754c\u548c\u4e0a\u754c\uff0c\u5efa\u8bae\u5bf9\u7167\u56fe\u4e2d\u53f3\u4fa7\u7684\u6807\u6ce8\uff09\u4e4b\u95f4\u603b\u662f\u8fde\u7eed\u7684\uff08\u8bd1\u6ce8\uff1a\u8fd9\u91cc\u6307\u7684\u662f\u865a\u62df\u5730\u5740\u7a7a\u95f4\u8fde\u7eed\uff0c\u4f46\u5176\u4e2d\u6bcf\u4e00\u9875\u90fd\u53ef\u80fd\u6620\u5c04\u5230\u4efb\u4e00\u7269\u7406\u9875\uff09\uff0c\u56e0\u6b64\u4f60\u65e0\u6cd5\u5728\u4e2d\u95f4\u6253\u4e2a\u6d1e\u6765\u51cf\u5c11\u6570\u636e\u6bb5\u7684\u5c3a\u5bf8\u3002\u6bd4\u5982\u8fd9\u4e2a\u573a\u666f\uff1a<\/p>\n<pre><code>char *truck = malloc(1024 * 1024 * sizeof(char)); char *bike  = malloc(sizeof(char)); free(truck); <\/code><\/pre>\n<p>\u5806\u5206\u914d\u5668\u5c06\u4f1a\u8c03\u5927 brk\uff0c\u4ee5\u4fbf\u7ed9 truck \u817e\u51fa\u7a7a\u95f4\u3002\u5bf9\u4e8e bike \u4e5f\u4e00\u6837\u3002\u4f46\u662f\u5f53 truck \u88ab\u91ca\u653e\u540e\uff0cbrk \u4e0d\u80fd\u88ab\u8c03\u5c0f\uff0c\u56e0\u4e3a bike \u6b63\u5360\u7740\u9ad8\u4f4d\u7684\u5730\u5740\u3002\u7ed3\u679c\u662f\uff0c\u4f60\u7684\u8fdb\u7a0b<strong>\u53ef\u4ee5<\/strong>\u91cd\u7528 truck \u7684\u5185\u5b58\uff0c\u4f46<strong>\u4e0d\u80fd<\/strong>\u9000\u8fd8\u7ed9 OS\uff0c\u9664\u975e bike \u4e5f\u88ab\u91ca\u653e\u3002\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u7528 mmap \u6765\u5206\u914d truck \u6240\u9700\u7684\u7a7a\u95f4\uff0c\u4e0d\u653e\u5728\u5806\u5185\u5b58\u6bb5\u91cc\uff0c\u5c31\u53ef\u4ee5\u4e0d\u5f71\u54cd program break\uff0c\u4f46\u8fd9\u4ecd\u7136\u65e0\u6cd5\u89e3\u51b3\u5206\u914d\u5c0f\u5757\u5185\u5b58\u5bfc\u81f4\u7684\u7a7a\u6d1e\uff08\u6362\u53e5\u8bdd\u8bf4\u5c31\u662f\u201c\u5f15\u8d77\u788e\u7247\u5316\u201d\uff09\u3002<\/p>\n<p>\u6ce8\u610f free() \u5e76\u4e0d\u603b\u662f\u4f1a\u7f29\u5c0f\u6570\u636e\u6bb5\uff0c\u56e0\u4e3a\u8fd9\u662f\u4e00\u4e2a\u6709\u5f88\u5927\u6f5c\u5728\u5f00\u9500\u7684\u64cd\u4f5c\uff08\u53c2\u89c1\u540e\u6587\u201c\u5bf9\u6309\u9700\u8c03\u9875\u7684\u89e3\u91ca\u201d\uff09\u3002\u5bf9\u4e8e\u9700\u8981\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u7a0b\u5e8f\uff08\u4f8b\u5982\u5b88\u62a4\u8fdb\u7a0b\uff09\u6765\u8bf4\u8fd9\u4f1a\u662f\u4e2a\u95ee\u9898\u3002\u6709\u4e2a\u53eb\u00a0 malloc_trim() \u7684 GNU \u6269\u5c55\u53ef\u4ee5\u7528\u6765\u4ece\u5806\u9876\u91ca\u653e\u5185\u5b58\uff0c\u4f46\u53ef\u80fd\u4f1a\u6162\u5f97\u4ee4\u4eba\u86cb\u75bc\uff0c\u5c24\u5176\u5bf9\u4e8e\u5927\u91cf\u5c0f\u5bf9\u8c61\u7684\u60c5\u51b5\uff0c\u6240\u4ee5\u5e94\u8be5\u5c3d\u91cf\u5c11\u7528\u3002<\/p>\n<h2>\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u81ea\u5b9a\u4e49\u5206\u914d\u5668<\/h2>\n<p>\u6709\u4e00\u4e9b\u5b9e\u9645\u573a\u666f\u4e2d\u901a\u7528\u5206\u914d\u5668\u6709\u77ed\u677f\uff0c\u4f8b\u5982\u5927\u91cf\u5206\u914d\u56fa\u5b9a\u5c3a\u5bf8\u7684\u5c0f\u5185\u5b58\u3002\u8fd9\u770b\u8d77\u6765\u4e0d\u50cf\u662f\u5178\u578b\u7684\u573a\u666f\uff0c\u4f46\u5b9e\u9645\u4e0a\u51fa\u73b0\u5f97\u5f88\u9891\u7e41 \u3002\u4f8b\u5982\uff0c\u7528\u4e8e\u67e5\u627e\u7684\u6570\u636e\u7ed3\u6784\uff08\u5178\u578b\u5982\u6811\u3001\u5b57\u5178\u6811\uff09\u9700\u8981\u5206\u914d\u5927\u91cf\u8282\u70b9\u7528\u4e8e\u6784\u9020\u5176\u5c42\u6b21\u7ed3\u6784\u3002\u5728\u8fd9\u4e2a\u573a\u666f\u4e0b\uff0c\u4e0d\u4ec5\u788e\u7247\u5316\u4f1a\u662f\u4e2a\u95ee\u9898\uff0c\u6570\u636e\u7684\u5c40\u90e8\u6027\u4e5f\u662f\u3002cache \u6548\u7387\u9ad8\u7684\u6570\u636e\u7ed3\u6784\u4f1a\u5c06 key \u653e\u5728\u4e00\u8d77\uff08\u6700\u597d\u5728\u540c\u4e00\u4e2a\u5185\u5b58\u9875\uff09\uff0c\u800c\u4e0d\u662f\u548c\u6570\u636e\u6df7\u5728\u4e00\u8d77\u3002\u9ed8\u8ba4\u7684\u5206\u914d\u5668\u4e0d\u80fd\u4fdd\u8bc1\u4e0b\u6b21\u5206\u914d\u65f6\u8fd8\u5728\u540c\u4e00\u4e2a block\uff0c\u66f4\u7cdf\u7684\u662f\u5206\u914d\u5c0f\u5355\u5143\u7684\u989d\u5916\u7a7a\u95f4\u5f00\u9500\u3002\u89e3\u51b3\u529e\u6cd5\u5728\u6b64\uff1a<\/p>\n<p>X<\/p>\n<p>\u6765\u6e90: Slab by wadem, on Flickr (CC-BY-SA)<\/p>\n<p>\uff08\u8bd1\u6ce8\uff1a\u539f\u56fe\u65e0\u6cd5\u6253\u5f00\u4e86\uff0c\u53e6\u8d34\u4e00\u5f20\uff09<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200516_5ebf48df9f08f.gif\" alt=\"[\u8bd1] \uff23\u7a0b\u5e8f\u5458\u8be5\u77e5\u9053\u7684\u5185\u5b58\u77e5\u8bc6 (2)\" \/><\/p>\n<p>\u6765\u6e90\uff1aIBM &#8211; Linux slab \u5206\u914d\u5668\u5256\u6790<\/p>\n<h2>Slab \u5206\u914d\u5668<\/h2>\n<p>\u5de5\u5177\u7bb1\uff1a<\/p>\n<ul>\n<li>posix_memalign() &#8211; \u5206\u914d\u5bf9\u9f50\u7684\u5185\u5b58<\/li>\n<\/ul>\n<p>Bonwick\u00a0\u4e3a\u5185\u6838\u5bf9\u8c61\u7f13\u5b58\u5199\u7684\u8fd9\u7bc7\u6587\u7ae0[4]\u4ecb\u7ecd\u4e86 slab \u5206\u914d\u5668\u7684\u539f\u7406\uff0c\u4e5f\u53ef\u7528\u4e8e\u7528\u6237\u7a7a\u95f4\u3002Okay\uff0c\u6211\u4eec\u5bf9\u7ed1\u5b9a\u5728 CPU \u4e0a\u7684 slab \u4e0d\u611f\u5174\u8da3\u00a0\u2014\u2014\u00a0\u5c31\u662f\u4f60\u627e\u5206\u914d\u5668\u8981\u4e00\u5757\u5185\u5b58\uff0c\u4f8b\u5982\u8bf4\u4e00\u6574\u9875\uff0c\u7136\u540e\u5207\u6210\u5f88\u591a\u56fa\u5b9a\u5927\u5c0f\u7684\u5c0f\u5757\u3002\u5982\u679c\u6bcf\u4e2a\u5c0f\u5757\u90fd\u80fd\u4fdd\u5b58\u81f3\u5c11\u4e00\u4e2a\u6307\u9488\u6216\u4e00\u4e2a\u6574\u6570\uff0c\u4f60\u5c31\u53ef\u4ee5\u628a\u4ed6\u4eec\u4e32\u6210\u4e00\u4e2a\u94fe\u8868 \uff0c\u8868\u5934\u6307\u5411\u7b2c\u4e00\u4e2a\u7a7a\u95f2\u5143\u7d20\u3002<\/p>\n<pre><code>\/* Super-simple slab. *\/ struct slab {  void **head; };  \/* Create page-aligned slab *\/ struct slab *slab = NULL; posix_memalign(&amp;slab, page_size, page_size); slab-&gt;head = (void **)((char*)slab + sizeof(struct slab));  \/* Create a NULL-terminated slab freelist *\/ char* item = (char*)slab-&gt;head; for(unsigned i = 0; i &lt; item_count; ++i) {  *((void**)item) = item + item_size;  item += item_size; } *((void**)item) = NULL; <\/code><\/pre>\n<p>\u8bd1\u6ce8\uff1a<\/p>\n<ol>\n<li>\u4ee3\u7801\u91cc\u7528\u7684\u4e8c\u7ea7\u6307\u9488\u53ef\u80fd\u8ba9\u4eba\u6709\u70b9\u6655\uff0c\u8fd9\u662f Linus \u63a8\u5d07\u7684\u94fe\u8868\u5b9e\u73b0\uff0c\u63a8\u8350\u9605\u8bfb&#8221;linus torvalds answers your questions&#8221;5\uff0c\u5728 favorite hack \u8fd9\u4e00\u8282\uff0c\u662f\u4e2a\u5f88\u6709\u8da3\u7684\u601d\u7ef4\u8bad\u7ec3<\/li>\n<li>\u7b2c 8 \u884c <code>posix_memalign<\/code> \u5206\u914d\u4e86\u4e00\u9875\u5185\u5b58\uff0c\u5e76\u4e14\u5bf9\u9f50\u5230\u9875\u8fb9\u754c\uff0c\u8fd9\u610f\u5473\u7740\u6b63\u597d\u62ff\u5230\u4e86\u4e00\u4e2a\u7269\u7406\u9875<\/li>\n<li>\u56e0\u4e3a\u7533\u8bf7\u5230\u7684 slab \u5927\u5c0f\u662f\u4e00\u4e2a page\uff0c\u6240\u4ee5 <code>item_count<\/code> = <code>page_size<\/code> \/ <code>item_size<\/code>\uff1b\u5176\u4e2d <code>item_size<\/code> \u53ef\u4ee5\u6839\u636e\u5e94\u7528\u9700\u8981\u6307\u5b9a\u3002<\/li>\n<\/ol>\n<p>\u7136\u540e\u5185\u5b58\u5206\u914d\u5c31\u7b80\u5355\u5230\u53ea\u8981\u5f39\u51fa\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u5c31\u884c\u4e86\uff0c\u5185\u5b58\u91ca\u653e\u5219\u662f\u63d2\u5165\u5934\u7ed3\u70b9\u3002\u8fd9\u91cc\u8fd8\u6709\u4e2a\u4f18\u96c5\u7684\u5c0f\u6280\u5de7\uff1a\u65e2\u7136 slab \u5bf9\u9f50\u5230\u4e86\u9875\u8fb9\u754c\uff0c\u4f60\u53ea\u8981\u5c06\u6307\u9488\u5411\u4e0b\u53d6\u6574\u5230 page_size \u5c31\u80fd\u5f97\u5230\u8fd9\u4e2a slab \u7684\u6307\u9488\u3002<\/p>\n<pre><code>\/* Free an element *\/ struct slab *slab = (void *)((size_t)ptr &amp; PAGESIZE_BITS); *((void**)ptr) = (void*)slab-&gt;head; slab-&gt;head = (void**)ptr;  \/* Allocate an element *\/ if((item = slab-&gt;head)) {  slab-&gt;head = (void**)*item; } else {  \/* No elements left. *\/ } <\/code><\/pre>\n<p>\u8bd1\u6ce8\uff1a\u5bf9\u4e8e page_size = 4KB \u7684\u9875\u9762\uff0cPAGESIZE_BITS =\u00a00xFFFFF000\uff0cptr &amp; PAGESIZE_BITS \u6e05\u96f6\u4e86\u4f4e 12 \u4f4d\uff0c\u6b63\u597d\u662f\u8fd9\u4e00\u9875\u7684\u5f00\u59cb\uff0c\u4e5f\u5c31\u662f\u8fd9\u4e2a slab \u7684\u8d77\u59cb\u5730\u5740\u3002<\/p>\n<p>\u592a\u68d2\u4e86\uff0c\u4f46\u662f\u8fd8\u6709 binning \uff08\u8bd1\u6ce8\uff1a\u5e94\u8be5\u662f\u6307\u6309\u4e0d\u540c\u7684\u957f\u5ea6\u5206\u6876\uff09\uff0c\u53d8\u957f\u5b58\u50a8\uff0ccache aliasing \uff08\u8bd1\u6ce8\uff1a\u540c\u4e00\u4e2a\u7269\u7406\u5730\u5740\u4e2d\u7684\u6570\u636e\u51fa\u73b0\u5728\u591a\u4e2a\u4e0d\u540c\u7684\u7f13\u5b58\u884c\u4e2d\uff09\uff0c\u5496\u5561\u56e0\uff08\u8bd1\u6ce8\uff1a\u8fd9\u5e94\u8be5\u662f\u4f5c\u8005\u5728\u9017\u903c\u4e86\uff09\uff0c&#8230;\u600e\u4e48\u529e\uff1f\u53ef\u4ee5\u770b\u770b\u6211\u4e4b\u524d\u4e3a Knot DNS \u5199\u7684\u4ee3\u7801[6]\uff0c\u6216\u8005\u5176\u4ed6\u5b9e\u73b0\u4e86\u8fd9\u4e9b\u70b9\u7684\u5e93\u3002\u4f8b\u5982\uff0c\uff08\u5598\u53e3\u6c14\uff09\uff0cglib \u91cc\u6709\u4e2a\u5f88\u6574\u9f50\u7684\u6587\u6863[7]\uff0c\u628a\u5b83\u79f0\u4e3a\u201cmemory slices\u201d\u3002<\/p>\n<p>\u8bd1\u6ce8\uff1aslab \u5206\u914d\u5668\u9002\u5408\u5927\u91cf\u5c0f\u5bf9\u8c61\u7684\u5206\u914d\uff0c\u53ef\u4ee5\u907f\u514d\u5e38\u89c1\u7684\u788e\u7247\u95ee\u9898\uff1b\u5728\u5185\u6838\u4e2d\u7684\u5b9e\u73b0\u8fd8\u53ef\u4ee5\u652f\u6301\u786c\u4ef6\u7f13\u5b58\u5bf9\u9f50\uff0c\u4ece\u800c\u63d0\u9ad8\u7f13\u5b58\u7684\u5229\u7528\u7387\u3002<\/p>\n<h2>\u5185\u5b58\u6c60<\/h2>\n<p>\u5de5\u5177\u7bb1\uff1a<\/p>\n<ul>\n<li>obstack_alloc() &#8211; \u4ece object stack \u4e2d\u5206\u914d\u5185\u5b58 \uff08\u8bd1\u6ce8\uff1a\u6307 GNU \u7684 obstack\uff0c\u7528 stack \u6765\u4fdd\u5b58 object \u7684\u5185\u5b58\u6c60\u5b9e\u73b0\uff09<\/li>\n<\/ul>\n<p>\u6b63\u5982 slab \u5206\u914d\u5668\u4e00\u6837\uff0c\u5185\u5b58\u6c60\u6bd4\u901a\u7528\u5206\u914d\u5668\u597d\u7684\u5730\u65b9\u5728\u4e8e\uff0c\u4f60\u6bcf\u6b21\u7533\u8bf7\u4e00\u5927\u5757\u5185\u5b58\uff0c\u7136\u540e\u50cf\u5207\u86cb\u7cd5\u4e00\u6837\u4e00\u5c0f\u5757\u4e00\u5c0f\u5757\u5207\u51fa\u53bb\uff0c\u76f4\u5230\u4e0d\u591f\u7528\u4e86\uff0c\u7136\u540e\u4f60\u518d\u7533\u8bf7\u4e00\u5927\u5757\u3002\u8fd8\u6709\uff0c\u5f53\u4f60\u90fd\u5904\u7406\u5b8c\u4e86\u4ee5\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u6536\u5de5\uff0c\u4e00\u6b21\u6027\u91ca\u653e\u6240\u6709\u7a7a\u95f4\u3002<\/p>\n<p>\u662f\u4e0d\u662f\u7279\u522b\u50bb\u74dc\u5316\uff1f\u56e0\u4e3a\u786e\u5b9e\u5982\u6b64\uff0c\u4f46\u53ea\u662f\u9488\u5bf9\u7279\u5b9a\u573a\u666f\u5982\u6b64\u3002\u4f60\u4e0d\u9700\u8981\u8003\u8651\u540c\u6b65\uff0c\u4e5f\u4e0d\u9700\u8981\u8003\u8651\u91ca\u653e\u3002\u518d\u6ca1\u6709\u5fd8\u8bb0\u56de\u6536\u7684\u5751\u4e86\uff0c\u6570\u636e\u7684\u5c40\u90e8\u6027\u4e5f\u66f4\u52a0\u7b26\u5408\u9884\u671f\uff0c\u800c\u4e14\u5bf9\u4e8e\u5c0f\u5bf9\u8c61\u7684\u5f00\u9500\u4e5f\u51e0\u4e4e\u4e3a 0 \u3002<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5f0f\u7279\u522b\u9002\u5408\u5f88\u591a\u7c7b\u578b\u7684\u4efb\u52a1\uff0c\u5305\u62ec\u77ed\u751f\u547d\u5468\u671f\u7684\u91cd\u590d\u5206\u914d\uff08\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\u5904\u7406\uff09\uff0c\u548c\u957f\u751f\u547d\u5468\u671f\u7684\u4e0d\u53ef\u53d8\u6570\u636e\uff08\u4f8b\u5982 frozen set \uff1b\u8bd1\u6ce8\uff1a\u521b\u5efa\u540e\u4e0d\u518d\u6539\u53d8\u7684\u96c6\u5408\uff09\u3002\u4f60\u4e0d\u518d\u9700\u8981\u9010\u4e2a\u91ca\u653e\u5bf9\u8c61\uff08\u8bd1\u6ce8\uff1a\u53ef\u4ee5\u6700\u540e\u6279\u91cf\u91ca\u653e\uff09\u3002\u5982\u679c\u4f60\u80fd\u5408\u7406\u63a8\u6d4b\u51fa\u5e73\u5747\u9700\u8981\u591a\u5c11\u5185\u5b58\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5c06\u591a\u4f59\u7684\u5185\u5b58\u91ca\u653e\uff0c\u4ee5\u4fbf\u7528\u4e8e\u5176\u4ed6\u76ee\u7684\u3002\u8fd9\u53ef\u4ee5\u5c06\u5185\u5b58\u5206\u914d\u95ee\u9898\u7b80\u5316\u6210\u7b80\u5355\u7684\u6307\u9488\u8fd0\u7b97\u3002<\/p>\n<p>\u800c\u4e14\u4f60\u5f88\u8d70\u8fd0 \u2014\u2014 GNU libc \u63d0\u4f9b\u4e86\uff0c\u55ec\uff0c\u4e00\u6574\u5957 API \u6765\u5e72\u8fd9\u4e8b\u513f\u3002\u8fd9\u5c31\u662f obstacks\uff0c\u7528\u6808\u6765\u7ba1\u7406\u5bf9\u8c61\u3002\u5b83\u7684 HTML \u6587\u6863[8]\u00a0\u5199\u5f97\u4e0d\u548b\u5730\uff0c\u4e0d\u8fc7\u629b\u5f00\u8fd9\u4e9b\u5c0f\u7f3a\u9677\uff0c\u5b83\u5141\u8bb8\u4f60\u5b8c\u6210\u57fa\u4e8e\u5185\u5b58\u6c60\u7684\u5206\u914d\u548c\u56de\u6536\uff08\u5305\u62ec\u90e8\u5206\u56de\u6536\u548c\u5168\u91cf\u56de\u6536\uff09\u3002<\/p>\n<pre><code>\/* Define block allocator. *\/ #define obstack_chunk_alloc malloc #define obstack_chunk_free free  \/* Initialize obstack and allocate a bunch of animals. *\/ struct obstack animal_stack; obstack_init (&amp;animal_stack); char *bob = obstack_alloc(&amp;animal_stack, sizeof(animal)); char *fred = obstack_alloc(&amp;animal_stack, sizeof(animal)); char *roger = obstack_alloc(&amp;animal_stack, sizeof(animal));  \/* Free everything after fred (i.e. fred and roger). *\/ obstack_free(&amp;animal_stack, fred);  \/* Free everything. *\/ obstack_free(&amp;animal_stack, NULL); <\/code><\/pre>\n<p>\u8bd1\u6ce8\uff1aobstack \u8fd9\u4e9b api \u5b9e\u9645\u90fd\u662f\u5b8f\uff1b\u9700\u8981\u901a\u8fc7\u5b8f\u6765\u6307\u5b9a\u627e OS \u5206\u914d\u548c\u56de\u6536\u6574\u5757\u5185\u5b58\u7528\u7684\u65b9\u6cd5\uff0c\u5982\u4e0a\u7b2c 2 \u30013 \u884c\u6240\u793a\u3002\u7531\u4e8e\u5bf9\u8c61\u4f7f\u7528\u6808\u7684\u65b9\u5f0f\u7ba1\u7406\uff08\u5148\u5206\u914d\u7684\u6700\u540e\u91ca\u653e\uff09\uff0c\u6240\u4ee5\u91ca\u653e fred \u7684\u65f6\u5019\uff0c\u4f1a\u628a fred \u548c\u5728 fred \u4e4b\u540e\u5206\u914d\u7684\u5bf9\u8c61\uff08 roger \uff09\u4e00\u8d77\u91ca\u653e\u6389\u3002<\/p>\n<p>\u8fd8\u6709\u4e2a\u5c0f\u6280\u5de7\uff1a\u4f60\u53ef\u4ee5\u6269\u5c55\u6808\u9876\u7684\u90a3\u4e2a\u5bf9\u8c61\u3002\u4f8b\u5982\u5e26\u7f13\u51b2\u7684\u8f93\u5165\uff0c\u53d8\u957f\u6570\u7ec4\uff0c\u6216\u8005\u7528\u6765\u66ff\u4ee3 realloc()-strcpy()\u00a0\u6a21\u5f0f\uff08\u8bd1\u6ce8\uff1a\u91cd\u65b0\u5206\u914d\u5185\u5b58\uff0c\u7136\u540e\u628a\u539f\u6570\u636e\u62f7\u8d1d\u8fc7\u53bb\uff09\uff1a<\/p>\n<pre><code>\/* This is wrong, I better cancel it. *\/ obstack_grow(&amp;animal_stack, \"long\", 4); obstack_grow(&amp;animal_stack, \"fred\", 5); obstack_free (&amp;animal_stack, obstack_finish(&amp;animal_stack));  \/* This time for real. *\/ obstack_grow(&amp;animal_stack, \"long\", 4); obstack_grow(&amp;animal_stack, \"bob\", 4); char *result = obstack_finish(&amp;animal_stack); printf(\"%sn\", result); \/* \"longbob\" *\/ <\/code><\/pre>\n<p>\u8bd1\u6ce8\uff1a\u524d\u4e09\u884c\u662f\u4f5c\u8005\u9017\u903c\u4e86\uff0c\u770b\u540e\u56db\u884c\u5c31\u884c\uff1b\u7528 obstack_grow \u6269\u5c55\u6808\u9876\u5143\u7d20\u5360\u7528\u7684\u5185\u5b58\uff0c\u6269\u5c55\u7ed3\u675f\u540e\u8c03\u7528 obstack_finish \u7ed3\u675f\u6269\u5c55\uff0c\u5e76\u8fd4\u56de\u6808\u9876\u5143\u7d20\u7684\u5730\u5740\u3002<\/p>\n<h2>\u5bf9\u6309\u9700\u8c03\u9875\uff08 demand paging \uff09\u7684\u89e3\u91ca<\/h2>\n<p>\u5de5\u5177\u7bb1\uff1a<\/p>\n<ul>\n<li>mlock() &#8211;\u00a0\u9501\u5b9a \/\u89e3\u9501\u5185\u5b58\uff08\u907f\u514d\u88ab\u6362\u51fa\u5230 swap \uff09<\/li>\n<li>madvise() &#8211; \u7ed9\uff08\u5185\u6838\uff09\u5efa\u8bae\u6307\u5b9a\u5185\u5b58\u8303\u56f4\u7684\u5904\u7f6e\u65b9\u5f0f<\/li>\n<\/ul>\n<p>\u901a\u7528\u5185\u5b58\u5206\u914d\u5668\u4e0d\u7acb\u5373\u5c06\u5185\u5b58\u8fd4\u56de\u7ed9\u7cfb\u7edf\u7684\u539f\u56e0\u4e4b\u4e00\u662f\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5f00\u9500\u5f88\u5927\u3002\u7cfb\u7edf\u9700\u8981\u505a\u4e24\u4ef6\u4e8b\uff1a(1) \u5efa\u7acb<strong>\u865a\u62df<\/strong>\u9875\u5230<strong>\u771f\u5b9e\uff08 real \uff09<strong>\u9875\u7684\u6620\u5c04\uff0c\u548c (2) \u7ed9\u4f60\u4e00\u4e2a\u6e05\u96f6\u7684<\/strong>\u771f\u5b9e<\/strong>\u9875\u3002\u8fd9\u4e2a\u771f\u5b9e\u9875\u88ab\u79f0\u4e3a<strong>\u5e27\uff08 frame \uff09<\/strong>\uff0c\u73b0\u5728\u4f60\u77e5\u9053\u5b83\u4eec\u7684\u5dee\u522b\u4e86\u3002\u6bcf\u4e00\u5e27\u90fd\u5fc5\u987b\u88ab\u6e05\u7a7a\uff0c\u6bd5\u7adf\u4f60\u4e0d\u5e0c\u671b OS \u6cc4\u6f0f\u5176\u4ed6\u8fdb\u7a0b\u7684\u79d8\u5bc6\uff0c\u5bf9\u5427\u3002\u8fd9\u91cc\u8fd8\u6709\u4e2a\u5c0f\u6280\u5de7\uff0c\u8fd8\u8bb0\u5f97 <strong>overcommit<\/strong> \u5417\uff1f\u865a\u62df\u5185\u5b58\u5206\u914d\u5668\u53ea\u628a\u8fd9\u4e2a\u4ea4\u6613\u521a\u5f00\u59cb\u7684\u90a3\u90e8\u5206\u5f53\u56de\u4e8b\uff0c\u7136\u540e\u5c31\u5f00\u59cb\u53d8\u9b54\u672f\u4e86 \u2014\u2014 \u9875\u8868\u91cc\u7684\u5927\u90e8\u5206\u9875\u9762\u5e76\u4e0d\u6307\u5411\u4e00\u4e2a\u771f\u5b9e\u9875\uff0c\u800c\u662f\u6307\u5411\u4e00\u4e2a\u7279\u6b8a\u7684\u5168 0 \u9875\u9762\u3002<\/p>\n<p>\u6bcf\u6b21\u4f60\u60f3\u8981\u8bbf\u95ee\u8fd9\u4e2a\u9875\u9762\u65f6\uff0c\u5c31\u4f1a\u89e6\u53d1\u4e00\u4e2a page fault\uff0c\u8fd9\u610f\u5473\u7740\u5185\u6838\u4f1a\u6682\u505c \u8fdb\u7a0b\u7684\u6267\u884c\uff0c\u5206\u914d\u4e00\u4e2a\u771f\u5b9e\u9875\u3001\u66f4\u65b0\u9875\u8868\uff0c\u7136\u540e\u6062\u590d\u8fdb\u7a0b\uff0c\u5e76\u5047\u88c5\u4ec0\u4e48\u4e5f\u6ca1\u53d1\u751f\u3002\u8fd9\u662f\u6c47\u603b\u5728\u4e00\u53e5\u8bdd\u91cc\u3001\u6211\u80fd\u505a\u51fa\u7684\u6700\u597d\u89e3\u91ca\u4e86\uff0c\u8fd9\u91cc[9]\u8fd8\u6709\u66f4\u4e2a\u8be6\u7ec6\u7684\u7248\u672c\u3002\u8fd9\u4e5f\u88ab\u79f0\u4f5c**\u201c\u6309\u9700\u8c03\u9875\u201d\uff08 demand paging \uff09** \u6216 <strong>\u201c\u5ef6\u8fdf\u52a0\u8f7d\u201d\uff08 lazy loading \uff09<\/strong>\u3002<\/p>\n<blockquote>\n<p>\u65af\u6ce2\u514b\u8239\u957f\u8bf4\u201c\u4eba\u65e0\u6cd5\u53ec\u5524\u672a\u6765\u201d\uff0c\u4f46\u8fd9\u91cc\u4f60\u53ef\u4ee5\u64cd\u63a7\u5b83\u3002<\/p>\n<\/blockquote>\n<p>\uff08\u8bd1\u6ce8\uff1a\u661f\u9645\u8ff7\u822a\uff0c\u65af\u6ce2\u514b\u8bf4\u201cOne man cannot summon the future.\u201d\uff0c\u67ef\u514b\u8bf4\u201cBut one man can change the present.\u201d\uff09<\/p>\n<p>\u5185\u5b58\u7ba1\u7406\u5668\u4e0d\u662f\u5148\u77e5\uff0c\u4ed6\u53ea\u662f\u4fdd\u5b88\u5730\u9884\u6d4b\u4f60\u8bbf\u95ee\u5185\u5b58\u7684\u65b9\u5f0f\uff0c\u800c\u4f60\u81ea\u5df1\u4e5f\u672a\u5fc5\u66f4\u6e05\u695a\uff08\u4f60\u5c06\u4f1a\u600e\u6837\u8bbf\u95ee\u5185\u5b58\uff09\u3002\uff08\u5982\u679c\u4f60\u77e5\u9053\uff09\u4f60\u53ef\u4ee5\u5c06\u4e00\u6bb5\u8fde\u7eed\u7684\u5185\u5b58\u5757\u9501\u5b9a\u5728<strong>\u7269\u7406<\/strong>\u5185\u5b58\u4e2d\uff0c\u4ee5\u907f\u514d\u540e\u7eed\u7684 page fault\uff1a<\/p>\n<pre><code>char *block = malloc(1024 * sizeof(char)); mlock(block, 1024 * sizeof(char)); <\/code><\/pre>\n<p>\uff08\u8bd1\u6ce8\uff1a\u8bbf\u95ee\u88ab\u6362\u51fa\u5230 swap \u7684\u9875\u9762\u4f1a\u89e6\u53d1 page fault\uff0c\u7136\u540e\u5185\u5b58\u7ba1\u7406\u5668\u4f1a\u4ece\u78c1\u76d8\u4e2d\u8f7d\u5165\u9875\u9762\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u8f83\u4e25\u91cd\u7684\u6027\u80fd\u95ee\u9898\uff1b\u7528 mlock \u5c06\u8fd9\u6bb5\u533a\u57df\u9501\u5b9a\u540e\uff0cOS \u5c31\u4e0d\u4f1a\u88ab\u64cd\u4f5c\u7cfb\u7edf\u6362\u51fa\u5230 swap \uff1b\u4f8b\u5982\uff0c\u5728\u5141\u8bb8\u7684\u60c5\u51b5\u4e0b\uff0cMySQL \u4f1a\u7528 mlock \u5c06\u7d22\u5f15\u4fdd\u6301\u5728\u7269\u7406\u5185\u5b58\u4e2d\uff09<\/p>\n<p>\u6ce8\u610f\uff1a\u4f60\u8fd8\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u5185\u5b58\u4f7f\u7528\u6a21\u5f0f\uff0c\u7ed9\u5185\u6838\u63d0\u51fa\u5efa\u8bae<\/p>\n<pre><code>char\u00a0*block\u00a0=\u00a0malloc(1024\u00a0*\u00a0sizeof(block)); madvise(block, 1024 * sizeof(block), MADV_SEQUENTIAL); <\/code><\/pre>\n<p>\u5bf9\u5efa\u8bae\u7684\u89e3\u91ca\u662f\u5e73\u53f0\u76f8\u5173\u7684\uff0c\u7cfb\u7edf\u751a\u81f3\u53ef\u80fd\u9009\u62e9\u5ffd\u7565\u5b83\uff0c\u4f46\u5927\u90e8\u5206\u5e73\u53f0\u90fd\u5904\u7406\u5f97\u5f88\u597d\u3002\u4f46\u4e0d\u662f\u6240\u6709\u5efa\u8bae\u90fd\u6709\u826f\u597d\u7684\u652f\u6301\uff0c\u6709\u4e9b\u5e73\u53f0\u53ef\u80fd\u4f1a\u6539\u53d8\u5efa\u8bae\u7684\u8bed\u4e49\uff08\u5982 MADV_FREE \u79fb\u9664\u79c1\u6709\u810f\u9875\uff1b\u8bd1\u6ce8\uff1a\u201c\u810f\u9875\u201d\uff0cdirty page\uff0c\u662f\u6307\u5206\u914d\u4ee5\u540e\u6709\u8fc7\u5199\u5165\uff0c\u5176\u4e2d\u53ef\u80fd\u6709\u672a\u4fdd\u5b58\u7684\u6570\u636e\uff09\uff0c\u4f46\u662f\u6700\u5e38\u7528\u7684\u8fd8\u662f MADV_SEQUENTIAL, MADV_WILLNEED, \u548c MADV_DONTNEED \u8fd9\u795e\u5723\u4e09\u4eba\u7ec4\uff08\u8bd1\u6ce8\uff1aholy trinity\uff0c\u5723\u7ecf\u91cc\u7684\u4e09\u4f4d\u4e00\u4f53\uff0c\u4f5c\u8005\u7528\u8bcd\u592a\u8df3\u8131\u2026\u2026\uff09\u3002<\/p>\n<p>\u8bd1\u6ce8\uff1a\u8fd8\u8bb0\u5f97\u300a\u8e29\u5751\u8bb0\uff1ago \u670d\u52a1\u5185\u5b58\u66b4\u6da8\u300b\u91cc\u5bf9 MADV_DONTNEED \u548c MADV_FREE \u7684\u89e3\u91ca\u5417\uff1f\u8fd9\u91cc\u518d\u56de\u987e\u4e0b<\/p>\n<ul>\n<li>MADV_DONTNEED\uff1a\u4e0d\u518d\u9700\u8981\u7684\u9875\u9762\uff0cLinux \u4f1a\u7acb\u5373\u56de\u6536<\/li>\n<li>MADV_FREE\uff1a\u4e0d\u518d\u9700\u8981\u7684\u9875\u9762\uff0cLinux \u4f1a\u5728\u9700\u8981\u65f6\u56de\u6536<\/li>\n<li>MADV_SEQUENTIAL\uff1a\u5c06\u4f1a\u6309\u987a\u5e8f\u8bbf\u95ee\u7684\u9875\u9762\uff0c\u5185\u6838\u53ef\u4ee5\u901a\u8fc7\u9884\u8bfb\u968f\u540e\u7684\u9875\u9762\u6765\u4f18\u5316\uff0c\u5df2\u7ecf\u8bbf\u95ee\u8fc7\u7684\u9875\u9762\u4e5f\u53ef\u4ee5\u63d0\u524d\u56de\u6536<\/li>\n<li>MADV_WILLNEED\uff1a\u5f88\u5feb\u5c06\u8bbf\u95ee\uff0c\u5efa\u8bae\u5185\u6838\u63d0\u524d\u52a0\u8f7d<\/li>\n<\/ul>\n<p>\u53c8\u5230\u4f11\u606f\u70b9\uff0c\u8fd9\u7bc7\u6682\u65f6\u5230\u8fd9\u91cc\u3002<\/p>\n<p>\u4e0b\u4e00\u7bc7\u4f1a\u7ee7\u7eed\u7ffb\u8bd1\u4e0b\u4e00\u8282\u300a Fun with memory mapping \u300b\uff0c\u8fd8\u6709\u5f88\u591a\u6709\u610f\u601d\u7684\u5185\u5bb9\uff0c\u656c\u8bf7\u5173\u6ce8~<\/p>\n<p>\u987a\u4fbf\u518d\u8d34\u4e0b\u4e4b\u524d\u63a8\u9001\u7684\u51e0\u7bc7\u6587\u7ae0\uff0c\u795d\u8fc7\u4e2a\u5145\u5b9e\u7684\u4e94\u4e00\u5047\u671f~<\/p>\n<ul>\n<li>\n<p>\u300a\u8e29\u5751\u8bb0\uff1ago \u670d\u52a1\u5185\u5b58\u66b4\u6da8\u300b<\/p>\n<\/li>\n<li>\n<p>\u300aTCP\uff1a\u5b66\u5f97\u8d8a\u591a\u8d8a\u4e0d\u61c2\u300b<\/p>\n<\/li>\n<li>\n<p>\u300aTCP#2: \u897f\u53a2\u8bb0\u548c\u897f\u53a2\u8ba1\u5212\u300b<\/p>\n<\/li>\n<li>\n<p>\u300aUTF-8\uff1a\u4e00\u4e9b\u597d\u50cf\u6ca1\u4ec0\u4e48\u7528\u7684\u51b7\u77e5\u8bc6\u300b<\/p>\n<\/li>\n<li>\n<p>\u300a\u5173\u4e8e RSA \u7684\u4e00\u4e9b\u8da3\u4e8b\u300b<\/p>\n<\/li>\n<li>\n<p>\u300a\u7a0b\u5e8f\u5458\u9762\u8bd5\u6307\u5317\uff1a\u9762\u8bd5\u5b98\u89c6\u89d2\u300b<\/p>\n<\/li>\n<\/ul>\n<h2>\u6b22\u8fce\u5173\u6ce8<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/05\/20200516_5ebf48e4b4e81.png\" alt=\"[\u8bd1] \uff23\u7a0b\u5e8f\u5458\u8be5\u77e5\u9053\u7684\u5185\u5b58\u77e5\u8bc6 (2)\" \/><\/p>\n<pre><code>   \u2584\u2584\u2584\u2584\u2584\u2584\u2584   \u2584      \u2584\u2584\u2584\u2584 \u2584\u2584\u2584\u2584\u2584\u2584\u2584      \u2588 \u2584\u2584\u2584 \u2588 \u2584\u2580 \u2584 \u2580\u2588\u2588\u2584 \u2580\u2588\u2584 \u2588 \u2584\u2584\u2584 \u2588      \u2588 \u2588\u2588\u2588 \u2588  \u2588  \u2588  \u2588\u2580\u2580\u2580\u2588\u2580 \u2588 \u2588\u2588\u2588 \u2588      \u2588\u2584\u2584\u2584\u2584\u2584\u2588 \u2584 \u2588\u2580\u2588 \u2588\u2580\u2588 \u2584\u2580\u2588 \u2588\u2584\u2584\u2584\u2584\u2584\u2588      \u2584\u2584\u2584 \u2584\u2584\u2584\u2584\u2588  \u2580\u2584\u2588\u2580\u2580\u2580\u2588 \u2584\u2588\u2584\u2584   \u2584        \u2584\u2588\u2584\u2584\u2584\u2584\u2584\u2580\u2584\u2580\u2584\u2588\u2588   \u2580 \u2584  \u2588\u2580\u2584\u2584\u2580\u2584\u2584\u2588      \u2588 \u2588\u2580\u2584\u2580\u2584\u2584\u2580\u2580\u2588\u2584\u2580\u2588\u2584\u2580\u2588\u2588\u2588\u2588\u2588\u2580\u2588\u2580\u2580\u2588 \u2588\u2584       \u2580\u2580  \u2588\u2584\u2588\u2588\u2584\u2588\u2580  \u2588 \u2580\u2588\u2580 \u2580\u2588\u2580 \u2584\u2580\u2580\u2584\u2588      \u2588\u2580 \u2580 \u2584\u2584\u2584\u2584\u2584\u2584\u2580\u2584\u2588\u2588  \u2588 \u2584\u2588\u2588\u2588\u2588\u2580\u2580 \u2588\u2584      \u2584\u2580\u2584\u2584\u2584 \u2584 \u2580\u2580\u2584\u2588\u2588\u2588\u2588\u2580\u2588\u2580  \u2580 \u2588\u2584\u2584\u2584\u2580\u2584\u2588      \u2584\u2580\u2580\u2588\u2588\u2584\u2584  \u2588\u2580\u2584\u2580\u2588\u2580\u2580 \u2588\u2580 \u2584\u2584\u2584\u2588\u2588\u2580 \u2580       \u2584\u2584\u2584\u2584\u2584\u2584\u2584 \u2588 \u2588\u2580 \u2580\u2580   \u2584\u2588\u2588 \u2584 \u2588\u2584\u2580\u2588\u2588      \u2588 \u2584\u2584\u2584 \u2588 \u2588\u2584 \u2580\u2584\u2580 \u2580\u2588\u2588  \u2588\u2584\u2584\u2584\u2588\u2584  \u2580      \u2588 \u2588\u2588\u2588 \u2588 \u2584 \u2588\u2588\u2588\u2580\u2580\u2580\u2588\u2584 \u2588\u2580\u2584 \u2588\u2588\u2584 \u2580\u2588      \u2588\u2584\u2584\u2584\u2584\u2584\u2588 \u2588\u2588 \u2584\u2588\u2580\u2588  \u2588 \u2580\u2588\u2588\u2584\u2584\u2584  \u2588\u2584   <\/code><\/pre>\n<p>\u53c2\u8003\u94fe\u63a5\uff1a<\/p>\n<p>1.\u00a0What a C programmer should know about memory<\/p>\n<p>2.\u00a0sbrk(2) &#8211; Linux man page<\/p>\n<p>3.\u00a0C Programming\/stdlib.h\/malloc<\/p>\n<p>4.\u00a0The Slab Allocator:\u00a0An Object-Caching Kernel Memory Allocator<\/p>\n<p>5.\u00a0linus\u00a0torvalds\u00a0answers\u00a0your\u00a0questions<\/p>\n<p>6.\u00a0Knot DNS &#8211; slab.h<\/p>\n<p>7.\u00a0glib\u00a0 &#8211; memory slices<\/p>\n<ol>\n<li>\n<p>GNU libc &#8211; Obstacks<\/p>\n<\/li>\n<li>\n<p>How the kernel manages your memory<\/p>\n<\/li>\n<\/ol><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1406199\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : teawithlife <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bf7\u6559\u4e00\u4e0b\u4e3b\uff1a\u6309\u7167\u6587\u7ae0\u7684\u8bf4\u6cd5\uff0c\u5bf9\u4e8e\u957f\u671f\u8dd1\u7684 C++\u7a0b\u5e8f\uff0c\u662f\u4e0d\u662f\u5373\u4f7f\u6211\u7528\u4e86\u5404\u79cd\u667a\u80fd\u6307\u9488\uff0c\u4fdd\u8bc1\u53d8\u91cf\u90fd\u80fd\u59a5\u5584\u7684\u91ca\u653e\uff0c\u4f46\u662f\u7531\u4e8e\u201c\u7a7a\u6d1e\u201d\u7684\u5b58\u5728\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5e76\u4e0d\u80fd\u56de\u6536\u8fd9\u90e8\u5206\u5185\u5b58\uff1f\u867d\u7136\u8fd9\u90e8\u5206\u5185\u5b58\uff0c\u6211\u7684\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\uff0c\u4f46\u662f\u7531\u4e8e\u6bcf\u6b21\u7533\u8bf7\u7684\u5185\u5b58\u5927\u5c0f\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u8fd9\u4e9b\u201c\u7a7a\u6d1e\u201d\u4f1a\u8d8a\u6765\u8d8a\u591a\uff0c\u6700\u540e\u7684\u7ed3\u679c\u5c31\u662f\u7cfb\u7edf\u53ef\u7528\u5185\u5b58\u8d8a\u6765\u8d8a\u5c11\uff1f<br \/>\u5982\u679c\u7528\u5176\u4ed6\u8bed\u8a00\uff0c\u6bd4\u5982 golang\uff0cpython\uff0c\u4e5f\u4f1a\u6709\u8fd9\u79cd\u95ee\u9898\u5417\uff0c\u8fd8\u662f gc \u4f1a\u81ea\u884c\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406200\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : felix021 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @teawithlife \u4e0d\u662f\uff0c\u6587\u4e2d\u8bf4\u4e86\uff0cfree\/delete \u4ee5\u540e\u5f97\u7a7a\u6d1e\uff0c\u4f1a\u88ab madvise \u8c03\u7528\u901a\u77e5 os\uff0c\u5bf9\u5e94\u7684 page \u662f\u53ef\u4ee5\u56de\u6536\u7684\uff0c\u5efa\u8bae\u518d\u770b\u4e00\u904d~                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406201\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : felix021 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @teawithlife \u6b63\u7ecf\u7684\u8bed\u8a00\u90fd\u4f1a\u5b9e\u73b0\u76f8\u5e94\u7684\u903b\u8f91(\u7528 madvise \u901a\u77e5\u7cfb\u7edf\u53ef\u4ee5\u56de\u6536\u4e0d\u518d\u7528\u7684\u9875\u9762)\uff0c\u5426\u5219\u65e0\u6cd5\u652f\u6491\u9700\u8981\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\u3002\u4e0d\u591f\u6b63\u7ecf\u7684\uff0c\u6bd4\u5982 php\uff0c\u4f60\u770b php fpm \u7684\u914d\u7f6e\u91cc\u9762\u6709\u4e2a max_requests\uff0c\u5904\u7406 n \u4e2a\u4efb\u52a1\u540e\u5c31\u91cd\u542f\u4e00\u6b21\uff0c\u907f\u514d\u5360\u7740\u8305\u5751\u4e0d\u62c9\u5c4e\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406202\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : teawithlife <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @felix021 #3 \u5443\uff0c\u90a3 C++\u7b97\u6b63\u7ecf\u7684\u8bed\u8a00\u5417\uff1f\u8fd8\u662f\u5f97\u81ea\u5df1\u624b\u52a8\u8c03 madvise \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406203\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : felix021 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @teawithlife \u5f53\u7136\u6b63\u7ecf\uff0cfree\/delete \u4f1a\u5728\u5fc5\u8981\u7684\u65f6\u5019\u8c03\u7528 madvise \u7684\uff0c\u611f\u5174\u8da3\u7684\u8bdd\u53ef\u4ee5\u53bb\u7ffb\u7ffb glibc \u7684\u4ee3\u7801 __libc_free \u7684\u4ee3\u7801\u91cf\u4e0d\u662f\u5f88\u591a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406204\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : teawithlife <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @felix021 #5 \u597d\u7684\uff0c\u975e\u5e38\u611f\u8c22\u3002\u671f\u5f85\u4e0b\u4e00\u7bc7\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406205\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lostpg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6709\u610f\u601d\uff0c\u652f\u6301                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406206\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fixend <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             glibc \u7684\u5185\u5b58\u5206\u914d\u5f88\u6162\uff0c\u5b9e\u9645\u5f00\u53d1\u4e0a\u90fd\u7528 tcmalloc\uff0cjemalloc\uff0c\u4e5f\u53ef\u4ee5\u770b\u770b go \u7684\u8fd0\u884c\u65f6 go\/src\/runtime\/malloc.go                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406207\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : felix021 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @fixend \u55ef \u6211\u4eec\u8fd9\u8fb9\u7528 jemalloc\uff0c\u4e0d\u8fc7\u6709\u9047\u5230 bug\uff0c\u4f1a\u5728\u67d0\u4e9b\u573a\u666f\u5bfc\u81f4 core                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406208\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fixend <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @felix021 tcmalloc \u8fd8\u7b97\u7a33\u5b9a\uff0c\u6ca1\u9047\u8fc7\u4ec0\u4e48\u5927\u95ee\u9898\u3002\u7528\u4e86\u8fd9\u4e9b\u5e93\uff0c\u6709\u4e00\u4e2a\u4e0d\u597d\u7684\u5730\u65b9\uff0c\u5c31\u662f\u5806\u5185\u5b58\u8d8a\u754c\uff0c\u603b\u662f\u5f88\u4e45\u540e\u624d core\uff0c\u5e76\u4e14 call stack \u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u4f1a\u662f tcmalloc \u7684\u4ee3\u7801\u3002\u884c\u4e3a\u4e0a\u53d8\u5f97\u8ddf\u6808\u8d8a\u754c\u4e00\u6837\u96be\u67e5\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406209\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhuyie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6587\u7ae0\u662f\u597d\u6587\u7ae0\uff0c\u53ea\u662f\u6709\u70b9\u6807\u9898\u515a\u3002C \u662f\u8de8\u5e73\u53f0\u7684\uff0c\u5927\u628a\u7a0b\u5e8f\u5458\u5728 Windows \u4e0b\u5199 C\/C++\u7a0b\u5e8f\uff08\u4f8b\u5982\u7edd\u5927\u90e8\u5206\u4e3b\u6d41\u6e38\u620f\u516c\u53f8\uff09\uff0c32bit Windows \u4e0b\u7684\u5730\u5740\u7a7a\u95f4\u5e03\u5c40\u53ef\u4e0d\u662f\u8fd9\u6837\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1406210\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : felix021 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zhuyie \u55ef\uff0c\u6240\u4ee5\u5728\u539f\u6587\u7684\u5f00\u5934\u4f5c\u8005\u5c31\u58f0\u660e\u4e86\u662f\u57fa\u4e8e Linux \u4e0b\u7684 C99 \u5199\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>[\u8bd1] \uff23\u7a0b\u5e8f\u5458\u8be5\u77e5\u9053\u7684\u5185\u5b58\u77e5\u8bc6 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/84250"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=84250"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/84250\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=84250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=84250"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=84250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}