{"id":489,"date":"2025-02-07T00:25:07","date_gmt":"2025-02-06T16:25:07","guid":{"rendered":"https:\/\/cococat.top\/?p=489"},"modified":"2025-02-07T00:25:07","modified_gmt":"2025-02-06T16:25:07","slug":"brk-mmap-and-memory-management","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2025\/02\/07\/brk-mmap-and-memory-management\/","title":{"rendered":"linux \u52a8\u6001\u5185\u5b58\u7ba1\u7406\uff1abrk\/mmap\/madvise \u7b49"},"content":{"rendered":"<h2>1. brk\/sbrk syscall<\/h2>\n<p>\u8981\u4ecb\u7ecd <code>brk<\/code>\uff0c\u9700\u8981\u5148\u5f15\u5165 program break \u8fd9\u4e00\u6982\u5ff5\u3002\u5b83\u53ef\u4ee5\u76f4\u63a5\u7406\u89e3\u4e3a\u662f\u865a\u5b58\u4e2d\u7684\u4e00\u4e2a\u6307\u9488\uff0c\u6307\u5411\u5806\u7684\u7ec8\u70b9\u3002\u8fdb\u7a0b\u521d\u59cb\u5316\u65f6\uff0c\u7531\u4e8e\u5806\u5927\u5c0f\u4e3a 0\uff0c\u6545 program break \u76f4\u63a5\u6307\u5411 .bss \u6bb5\u7684\u7ec8\u70b9\u3002<\/p>\n<p><code>brk<\/code>\/<code>sbrk<\/code> syscall \u7684\u4f5c\u7528\u5c31\u662f\u79fb\u52a8 program break \u7684\u4f4d\u7f6e\u3002\u5982\u679c\u5411\u9ad8\u5730\u5740\u79fb\u52a8\uff0c\u5219\u5806\u6269\u5927\uff0c\u540c\u65f6 OS \u4f1a\u5411\u8fdb\u7a0b\u5206\u914d\u7269\u7406\u5185\u5b58\uff08\u4e8b\u5b9e\u4e0a\u5148\u53ea\u5206\u914d\u865a\u5b58\uff0c\u7b49\u5230\u7b2c\u4e00\u6b21\u8bbf\u95ee\u65f6\u518d\u901a\u8fc7\u7f3a\u9875\u4e2d\u65ad\u6765\u5206\u914d\u7269\u7406\u5185\u5b58\uff09\uff1b\u53cd\u4e4b\uff0c\u82e5\u5411\u4f4e\u5730\u5740\u79fb\u52a8\uff0c\u5219\u5806\u7f29\u5c0f\uff0c\u540c\u65f6\u8fdb\u7a0b\u4f1a\u91ca\u653e\u5bf9\u5e94\u7684\u7269\u7406\u5185\u5b58\u3002<\/p>\n<h2>2. <code>brk<\/code> \u7684\u95ee\u9898<\/h2>\n<p>\u5806\u4f7f\u7528\u7684\u865a\u5b58\u5730\u5740\u5fc5\u987b\u662f\u8fde\u7eed\u7684\u3002\u5982\u679c\u6211\u4eec\u53ea\u4f7f\u7528 <code>brk<\/code> \u8fd9\u4e00\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u5219\u4f1a\u5728\u5185\u5b58\u91ca\u653e\u4e0a\u9047\u5230\u95ee\u9898\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a<br \/>\n\u5047\u8bbe\u5806\u7684\u5185\u5b58\u4f9d\u6b21\u5b58\u653e\u4e86\u52a8\u6001\u5206\u914d\u7684\u6570\u636e A\u3001B\u3001C \u3002program break \u5219\u5e94\u5f53\u6307\u5411 C \u7684\u7ed3\u5c3e\u3002<br \/>\n\u6b64\u65f6\uff0c\u5373\u4f7f\u6211\u4eec\u8c03\u7528 <code>free(A)<\/code>\u3001<code>free(B)<\/code>\uff0c\u53ea\u8981 C \u4f9d\u7136\u5728\u4f7f\u7528\u5806\u672b\u5c3e\u7684\u8fd9\u5757\u5185\u5b58\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u5c06 program break \u51cf\u5c11\uff0c\u4e5f\u5c31\u662f\u65e0\u6cd5\u7f29\u5c0f\u5806\u7684\u5927\u5c0f\u3002\u8fd9\u5c31\u5bfc\u81f4\u524d\u9762\u7531 A\u3001B \u5360\u7528\u7684\u7269\u7406\u5185\u5b58\u4e5f\u65e0\u6cd5\u91ca\u653e\uff0c\u7559\u4e0b\u4e86\u65e0\u6cd5\u88ab\u91ca\u653e\u7ed9 OS \u7684\u5185\u5b58\u788e\u7247\u3002<\/p>\n<h2>3. <code>mmap<\/code>\uff1a\u4f5c\u4e3a <code>brk<\/code> \u7684\u8865\u5145<\/h2>\n<p>\u4e3a\u4e86\u51cf\u5c11\u5185\u5b58\u788e\u7247\uff0c\u73b0\u4ee3\u7684 glibc <code>malloc<\/code> \u5b9e\u73b0\u662f\u6df7\u5408\u4f7f\u7528 <code>brk<\/code> \u4e0e <code>mmap<\/code> \u7684\u3002\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u9608\u503c <code>MMAP_THRESHOLD<\/code>\uff0c\u7533\u8bf7\u7684\u5185\u5b58\u5927\u5c0f\u8d85\u8fc7\u6b64\u9608\u503c\u65f6\uff0c\u5219\u4f1a\u6362\u7528 <code>mmap<\/code> \u5206\u914d\u5185\u5b58\u3002<br \/>\n<code>mmap<\/code> \u4f1a\u8ba9\u5185\u6838\u5728\u5806\u548c\u6808\u4e4b\u95f4\u7684\u865a\u62df\u5185\u5b58\u5bfb\u627e\u4e00\u6bb5\u53ef\u7528\u7684\u7a7a\u95f4\uff08TODO: \u68c0\u67e5\u6e90\u7801\uff0cmmap \u8c03\u7528\u7684 addr \u662f\u5426\u4e3a NULL\uff1f\uff09\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u7684\u5185\u5b58\u6620\u5c04\u3002\u8fd9\u4e9b\u5185\u5b58\u6620\u5c04\u4e4b\u95f4\u4e0d\u5fc5\u4fdd\u6301\u76f8\u90bb\u548c\u8fde\u7eed\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u5b8c\u540e\u53ef\u4ee5\u7acb\u523b\u91ca\u653e\uff08<code>munmap<\/code>\uff09\uff0c\u4ece\u800c\u907f\u514d\u4e86 <code>brk<\/code> \u7684\u5185\u5b58\u788e\u7247\u95ee\u9898\u3002<\/p>\n<p>\u53c2\u8003 <a href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/malloc.3.html\">https:\/\/man7.org\/linux\/man-pages\/man3\/malloc.3.html<\/a> \u3002<\/p>\n<h2>4. glibc \u7684 <code>free<\/code> \u884c\u4e3a\u4e0e <code>malloc_trim<\/code><\/h2>\n<p>\u524d\u9762\u63d0\u5230\uff0c\u53ea\u8981\u5806\u9876\u7a7a\u95f4\u4e0d\u91ca\u653e\uff0c\u5219\u5806\u5c31\u4e0d\u80fd\u7f29\u51cf\uff0c\u8fdb\u7a0b\u4e5f\u4e0d\u80fd\u5c06\u5360\u7528\u5185\u5b58\u8fd8\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u3002\u6240\u4ee5\uff0c\u5728 <code>free<\/code> \u7684\u65f6\u5019\uff0c\u5e94\u5f53\u9002\u65f6\u5730\u68c0\u67e5\u5806\u9876\u662f\u5426\u6709\u53ef\u4ee5\u91ca\u653e\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<p>glibc \u7684 <code>free<\/code> \u5728\u91ca\u653e\u4e86\u8db3\u591f\u5927\u7684\u5185\u5b58\u65f6\uff0c\u4f1a\u5c1d\u8bd5\u901a\u8fc7 <code>brk<\/code>\u6216\u8005 <code>madvise<\/code> \u6765\u7f29\u51cf\u5806\u3002\u8fd9\u90e8\u5206\u53ef\u4ee5\u53c2\u8003<a href=\"https:\/\/github.com\/bminor\/glibc\/blob\/60f2d6be657aa8c663ee14bd266d343ae0f35afb\/malloc\/malloc.c#L3436\">\u4ee3\u7801<\/a>\uff0c\u8c03\u7528\u6808\u662f<code>__libc_free<\/code> -&gt; <code>_int_free<\/code> -&gt; <code>_int_free_chunk<\/code>-&gt;  <code>_int_free_merge_chunk<\/code> -&gt;  <code>_int_free_maybe_consolidate<\/code> -&gt; <code>heap_trim<\/code> -&gt; <code>shrink_heap<\/code>\u3002<\/p>\n<p>\u8fd9\u4e2a\u8fc7\u7a0b\u7275\u626f\u5230\u4e24\u4e2a const\uff1a<\/p>\n<ul>\n<li><code>FASTBIN_CONSOLIDATION_THRESHOLD<\/code>\uff1a<code>free<\/code> \u7684\u5185\u5b58\u8d85\u8fc7\u8fd9\u4e2a\u5927\u5c0f\u65f6\uff0c\u89e6\u53d1\u5806\u9876\u7a7a\u95f4\u7684\u68c0\u67e5\u4e0e\u91ca\u653e\u3002\u9ed8\u8ba4 64k<\/li>\n<li><code>M_TRIM_THRESHOLD<\/code>\uff1a\u68c0\u67e5\u5806\u9876\u7684\u7a7a\u95f2\u7a7a\u95f4\uff0c\u8d85\u8fc7\u8fd9\u4e2a\u503c\u65f6\uff0c\u4f1a\u7f29\u51cf\u5806\u4ee5\u91ca\u653e\u5185\u5b58\u3002\u9ed8\u8ba4 128k<\/li>\n<\/ul>\n<p>\u5f53\u7136\uff0cglibc \u4e5f\u5bf9\u5916\u66b4\u9732\u4e86\u51fd\u6570 <code>malloc_trim<\/code>\uff0c\u5141\u8bb8\u7528\u6237\u81ea\u884c\u8c03\u7528\uff0c\u7528\u4ee5\u5728\u5806\u9876\u91ca\u653e\u5185\u5b58\u3002<\/p>\n<p>\u53c2\u8003\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/www.cnblogs.com\/zhanggaofeng\/p\/15941542.html\">https:\/\/www.cnblogs.com\/zhanggaofeng\/p\/15941542.html<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/u013259321\/article\/details\/112031002\">https:\/\/blog.csdn.net\/u013259321\/article\/details\/112031002<\/a><\/li>\n<li><a href=\"https:\/\/www.openeuler.org\/zh\/blog\/wangshuo\/Glibc%20Malloc%20Principle\/Glibc_Malloc_Principle\">https:\/\/www.openeuler.org\/zh\/blog\/wangshuo\/Glibc%20Malloc%20Principle\/Glibc_Malloc_Principle<\/a><\/li>\n<\/ul>\n<h2>5. madvise<\/h2>\n<p>syscall\uff0c\u544a\u77e5\u5185\u6838\u67d0\u5757\u5185\u5b58\u7684\u9884\u671f\u4f7f\u7528\u65b9\u5f0f\uff0c\u8ba9\u5185\u6838\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5185\u5b58\u4f18\u5316\u7b56\u7565\uff0c\u4f8b\u5982\u9884\u8bfb\u3001\u7f13\u5b58\u6216\u662f\u91ca\u653e\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u8ba8\u8bba\u5185\u5b58\u91ca\u653e\u65f6\uff0c\u5e38\u7528\u7684 <code>madvise<\/code> \u53c2\u6570\u662f <code>MADV_DONTNEED<\/code>\u3002\u5b83\u8868\u793a\u67d0\u5757\u5185\u5b58\u5728\u77ed\u671f\u5185\u4e0d\u518d\u9700\u8981\u4f7f\u7528\uff0c\u5185\u6838\u4f1a\u5728<strong>\u5408\u9002\u7684\u65f6\u673a<\/strong>\u91ca\u653e\u8fd9\u5757\u865a\u5b58\u6240\u7ed1\u5b9a\u7684\u7269\u7406\u5185\u5b58\u3002\u865a\u5b58\u5730\u5740\u672c\u8eab\u8fd8\u662f\u4f1a\u5f97\u4ee5\u4fdd\u7559\u7684\uff0c\u4e0b\u6b21\u8bbf\u95ee\u7684\u5bf9\u5e94\u5730\u5740\u65f6\u4f1a\u901a\u8fc7\u7f3a\u9875\u4e2d\u65ad\u91cd\u65b0\u52a0\u8f7d\u3002<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u5f3a\u8c03\u4e86\u662f\u5408\u9002\u7684\u65f6\u673a\uff0c\u56e0\u4e3a <code>mdvise<\/code> \u53ea\u662f\u4e00\u79cd\u5efa\u8bae\uff0c\u5185\u6838\u6709\u6743\u5229\u63a8\u8fdf\u751a\u81f3\u662f\u62d2\u7edd\u505a\u51fa\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<h2>6. buddy \u7b97\u6cd5<\/h2>\n<p>\u5728 linux \u5185\u6838\u7684<strong>\u7269\u7406\u5185\u5b58<\/strong>\u7ba1\u7406\u4e2d\u7684\u7b97\u6cd5\uff0c\u5176\u8bbe\u8ba1\u76ee\u7684\u662f\u51cf\u5c11\u788e\u7247\u3002\u8fd9\u4e2a\u7b97\u6cd5\u672c\u8eab\u53ef\u4ee5\u5c31\u7531\u4e09\u6761\u89c4\u5219\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li>\u5c06\u7269\u7406\u5185\u5b58\u5212\u5206\u4e3a\u5927\u5c0f\u4e3a 2 \u7684\u5e42\u4e2a\u9875\uff084KB\uff09\u7684\u5757\u3002<\/li>\n<li>\u5f53\u8fdb\u7a0b\u7533\u8bf7\u5185\u5b58\u65f6\uff0c\u4e3a\u5176\u5206\u914d\u5927\u5c0f\u6700\u63a5\u8fd1\u7684 2 \u7684\u5e42\u7684\u5757\u3002\u82e5\u5f53\u524d\u5e76\u4e0d\u5b58\u5728\u6ee1\u8db3\u9700\u6c42\u7684\u5757\uff0c\u5219\u9012\u5f52\u5411\u4e0a\u67e5\u627e\u66f4\u5927\u7684\u5757\uff0c\u5c06\u66f4\u5927\u7684\u5757\u62c6\u6210\u540c\u6837\u5927\u5c0f\u7684\u4e24\u4efd\uff0c\u8fd9\u4e24\u4efd\u4e92\u4e3a buddy\u3002<\/li>\n<li>\u5f53\u8fdb\u7a0b\u91ca\u653e\u5185\u5b58\u65f6\uff0c\u68c0\u67e5\u8fd9\u5757\u5185\u5b58\u7684 buddy \u662f\u5426\u4e5f\u662f\u7a7a\u95f2\u7684\u3002\u5982\u679c\u662f\uff0c\u5219\u5c06\u4e24\u4e2a buddy \u5408\u5e76\u4e3a\u4e00\u4e2a\u8f83\u5927\u7684\u5757\u3002\u5e76\u5411\u4e0a\u9012\u5f52\u3002<\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u914d\u5408 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Buddy_memory_allocation\">wiki<\/a> \u4e0a\u7684\u4f8b\u5b50\u8bf4\u660e\u6765\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u8fd9\u4e2a\u7b97\u6cd5\u8fd0\u884c\u7684\u89c4\u5219\u3002<\/p>\n<h2>7. slab \u5185\u5b58\u5206\u914d\u5668<\/h2>\n<p>\u4ec5\u4f7f\u7528 buddy \u7b97\u6cd5\u6709\u7740\u4e00\u4e9b\u660e\u663e\u7684\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\u4ece buddy \u7b97\u6cd5\u7684\u63cf\u8ff0\u53ef\u4ee5\u53d1\u73b0\uff0c\u8fd9\u79cd\u7b97\u6cd5\u6bcf\u6b21\u5206\u914d\u5185\u5b58\u7684\u6700\u5c0f\u503c\u662f 4KB\u3002\u800c\u8fdb\u7a0b\u5b9e\u9645\u7533\u8bf7\u7684\u5185\u5b58\u53c8\u5f80\u5f80\u662f\u975e\u5e38\u5c0f\u7684\uff0c\u4f8b\u5982\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u901a\u5e38\u4e5f\u5c31\u51e0\u5341\u5b57\u8282\u3002\u53ef\u89c1\uff0c\u4ec5\u91c7\u7528 buddy \u7b97\u6cd5\u4f1a\u9020\u6210\u6bd4\u8f83\u663e\u8457\u7684\u6d6a\u8d39\u3002<\/li>\n<li>\u8fd9\u4e9b\u7a00\u758f\u7684\u9875\u8fd8\u4f1a\u5360\u636e\u5b9d\u8d35\u7684 CPU \u6570\u636e\u7f13\u5b58\u4e0e TLB\uff0c\u628a\u5b9e\u9645\u5b58\u653e\u4e86\u8f83\u591a\u6570\u636e\u7684\u9875\u7ed9\u62a2\u5360\u51fa\u53bb\uff0c\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n<li>\u518d\u6b21\uff0c\u8c03\u7528 buddy \u7b97\u6cd5\u5206\u914d\u5185\u5b58\u7684\u94fe\u8def\u672c\u8eab\u4e5f\u662f\u76f8\u5f53\u957f\u7684\uff0c\u76f8\u5173\u7684\u4ee3\u7801\u6bb5\u548c\u6570\u636e\u6bb5\u672c\u8eab\u4e5f\u4f1a\u62a2\u5360 CPU \u7684\u6307\u4ee4\u7f13\u5b58\u548c\u6570\u636e\u7f13\u5b58\u3002<\/li>\n<\/ol>\n<p>\u5728\u7cfb\u7edf\u5b9e\u9645\u8fd0\u884c\u4e2d\uff0c\u5185\u6838\u9700\u8981\u9891\u7e41\u5730\u5206\u914d\u4e0e\u91ca\u653e\u4e00\u4e9b\u5c0f\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982 fd\u3001\u8fdb\u7a0b\u63a7\u5236\u5757\uff08PCB\uff0c\u5373 <code>task_struct<\/code> \uff09\u7b49\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u4e0a\u9762\u51e0\u4e2a\u95ee\u9898\u7684\u8d1f\u9762\u5f71\u54cd\u88ab\u4e25\u91cd\u653e\u5927\u3002<\/p>\n<p>\u6240\u4ee5\u600e\u4e48\u529e\u7684\uff1f\u7ecf\u5178\u7684\u6c60\u5316\u601d\u60f3\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\u3002slab \u5185\u5b58\u5206\u914d\u5668\u5c31\u662f\u4e00\u4e2a\u57fa\u4e8e\u6c60\u5316\u601d\u60f3\u7684\u5c0f\u5185\u5b58\u5206\u914d\u5668\u3002slab \u4f1a\u5411\u4f19\u4f34\u7cfb\u7edf\u4e00\u6b21\u6027\u7533\u8bf7\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u7269\u7406\u5185\u5b58\u9875\u9762\uff0c\u7ec4\u6210\u5185\u5b58\u6c60\u3002\u5185\u6838\u5e38\u7528\u7684\u5c0f\u5bf9\u8c61\u90fd\u8d70 slab \u7533\u8bf7 (<code>kmalloc<\/code>, k for kernel)\uff0c\u91ca\u653e\u4e5f\u662f\u5f52\u8fd8\u5230 slab \u4e2d\u3002<\/p>\n<p>\u540c\u6837\u7684\u601d\u60f3\u4e5f\u88ab\u8fd0\u7528\u4e86\u4e00\u4e9b\u7528\u6237\u7a7a\u95f4\u7684\u5185\u5b58\u5206\u914d\u5668\uff0c\u4f8b\u5982 jemalloc \u4e0a\u3002<\/p>\n<p>\u5173\u4e8e slab \u975e\u5e38\u597d\u7684\u6587\u7ae0\u53c2\u8003\uff1a  <a href=\"https:\/\/www.cnblogs.com\/binlovetech\/p\/17288990.html\">https:\/\/www.cnblogs.com\/binlovetech\/p\/17288990.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. brk\/sbrk syscall \u8981\u4ecb\u7ecd brk\uff0c\u9700\u8981\u5148\u5f15\u5165 program break \u8fd9\u4e00\u6982\u5ff5\u3002\u5b83\u53ef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,29],"class_list":["post-489","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-29"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=489"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/489\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}