{"id":4019,"date":"2012-05-18T11:06:20","date_gmt":"2012-05-18T03:06:20","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=4019"},"modified":"2012-05-18T13:04:41","modified_gmt":"2012-05-18T05:04:41","slug":"linux%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86%e4%b8%ad%e7%9a%84slab%e5%88%86%e9%85%8d%e5%99%a8","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/4019","title":{"rendered":"Linux\u5185\u5b58\u7ba1\u7406\u4e2d\u7684slab\u5206\u914d\u5668"},"content":{"rendered":"<p>Linux\u5185\u6838\u4e2d\u57fa\u4e8e\u4f19\u4f34\u7b97\u6cd5\u5b9e\u73b0\u7684\u5206\u533a\u9875\u6846\u5206\u914d\u5668\u9002\u5408\u5927\u5757\u5185\u5b58\u7684\u8bf7\u6c42\uff0c\u5b83\u6240\u5206\u914d\u7684\u5185\u5b58\u533a\u662f\u4ee5\u9875\u6846\u4e3a\u57fa\u672c\u5355\u4f4d\u7684\u3002\u5bf9\u4e8e\u5185\u6838\u4e2d\u5c0f\u5757\u8fde\u7eed\u5185\u5b58\u7684\u8bf7\u6c42\uff0c\u6bd4\u5982\u8bf4\u51e0\u4e2a\u5b57\u8282\u6216\u8005\u51e0\u767e\u4e2a\u5b57\u8282\uff0c\u5982\u679c\u4f9d\u7136\u5206\u914d\u4e00\u4e2a\u9875\u6846\u6765\u6765\u6ee1\u8db3\u8be5\u8bf7\u6c42\uff0c\u90a3\u4e48\u8fd9\u5f88\u660e\u663e\u5c31\u662f\u4e00\u79cd\u6d6a\u8d39\uff0c\u5373\u4ea7\u751f\u5185\u90e8\u788e\u7247\uff08internal fragmentation\uff09<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u5c0f\u5757\u5185\u5b58\u7684\u5206\u914d\uff0cLinux\u5185\u6838\u57fa\u4e8eSolaris 2.4\u4e2d\u7684slab\u5206\u914d\u7b97\u6cd5\u5b9e\u73b0\u4e86\u81ea\u5df1\u7684slab\u5206\u914d\u5668\u3002\u9664\u6b64\u4e4b\u5916\uff0cslab\u5206\u914d\u5668\u53e6\u4e00\u4e2a\u4e3b\u8981\u529f\u80fd\u662f\u4f5c\u4e3a\u4e00\u4e2a\u9ad8\u901f\u7f13\u5b58\uff0c\u5b83\u7528\u6765\u5b58\u50a8\u5185\u6838\u4e2d\u90a3\u4e9b\u7ecf\u5e38\u5206\u914d\u5e76\u91ca\u653e\u7684\u5bf9\u8c61\u3002<\/p>\n<h3>1.slab\u5206\u914d\u5668\u7684\u57fa\u672c\u539f\u7406<\/h3>\n<p>slab\u5206\u914d\u5668\u4e2d\u7528\u5230\u4e86\u5bf9\u8c61\u8fd9\u4e2a\u6982\u5ff5\uff0c\u6240\u8c13\u5bf9\u8c61\u5c31\u662f\u5185\u6838\u4e2d\u7684\u6570\u636e\u7ed3\u6784\u4ee5\u53ca\u5bf9\u8be5\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u521b\u5efa\u548c\u64a4\u9500\u7684\u64cd\u4f5c\u3002\u5b83\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c06\u5185\u6838\u4e2d\u7ecf\u5e38\u4f7f\u7528\u7684\u5bf9\u8c61\u653e\u5230\u9ad8\u901f\u7f13\u5b58\u4e2d\uff0c\u5e76\u4e14\u7531\u7cfb\u7edf\u4fdd\u6301\u4e3a\u521d\u59cb\u7684\u53ef\u5229\u7528\u72b6\u6001\u3002\u6bd4\u5982\u8fdb\u7a0b\u63cf\u8ff0\u7b26\uff0c\u5185\u6838\u4e2d\u4f1a\u9891\u7e41\u5bf9\u6b64\u6570\u636e\u8fdb\u884c\u7533\u8bf7\u548c\u91ca\u653e\u3002\u5f53\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u521b\u5efa\u65f6\uff0c\u5185\u6838\u4f1a\u76f4\u63a5\u4eceslab\u5206\u914d\u5668\u7684\u9ad8\u901f\u7f13\u5b58\u4e2d\u83b7\u53d6\u4e00\u4e2a\u5df2\u7ecf\u521d\u59cb\u5316\u4e86\u7684\u5bf9\u8c61\uff1b\u5f53\u8fdb\u7a0b\u7ed3\u675f\u65f6\uff0c\u8be5\u7ed3\u6784\u6240\u5360\u7684\u9875\u6846\u5e76\u4e0d\u88ab\u91ca\u653e\uff0c\u800c\u662f\u91cd\u65b0\u8fd4\u56deslab\u5206\u914d\u5668\u4e2d\u3002\u5982\u679c\u6ca1\u6709\u57fa\u4e8e\u5bf9\u8c61\u7684slab\u5206\u914d\u5668\uff0c\u5185\u6838\u5c06\u82b1\u8d39\u66f4\u591a\u7684\u65f6\u95f4\u53bb\u5206\u914d\u3001\u521d\u59cb\u5316\u4ee5\u53ca\u91ca\u653e\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>slab\u5206\u914d\u5668\u6709\u4ee5\u4e0b\u4e09\u4e2a\u57fa\u672c\u76ee\u6807\uff1a<\/p>\n<p>1.\u51cf\u5c11\u4f19\u4f34\u7b97\u6cd5\u5728\u5206\u914d\u5c0f\u5757\u8fde\u7eed\u5185\u5b58\u65f6\u6240\u4ea7\u751f\u7684\u5185\u90e8\u788e\u7247\uff1b<\/p>\n<p>2.\u5c06\u9891\u7e41\u4f7f\u7528\u7684\u5bf9\u8c61\u7f13\u5b58\u8d77\u6765\uff0c\u51cf\u5c11\u5206\u914d\u3001\u521d\u59cb\u5316\u548c\u91ca\u653e\u5bf9\u8c61\u7684\u65f6\u95f4\u5f00\u9500\u3002<\/p>\n<p>3.\u901a\u8fc7\u7740\u8272\u6280\u672f\u8c03\u6574\u5bf9\u8c61\u4ee5\u66f4\u597d\u7684\u4f7f\u7528\u786c\u4ef6\u9ad8\u901f\u7f13\u5b58\uff1b<\/p>\n<h3>2.slab\u5206\u914d\u5668\u7684\u7ed3\u6784<\/h3>\n<p>slab\u5206\u914d\u5668\u4e3a\u6bcf\u79cd\u5bf9\u8c61\u5206\u914d\u4e00\u4e2a\u9ad8\u901f\u7f13\u5b58\uff0c\u8fd9\u4e2a\u7f13\u5b58\u53ef\u4ee5\u770b\u505a\u662f\u540c\u7c7b\u578b\u5bf9\u8c61\u7684\u4e00\u79cd\u50a8\u5907\u3002\u6bcf\u4e2a\u9ad8\u901f\u7f13\u5b58\u6240\u5360\u7684\u5185\u5b58\u533a\u53c8\u88ab\u5212\u5206\u591a\u4e2aslab\uff0c\u6bcf\u4e2aslab\u662f\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u8fde\u7eed\u7684\u9875\u6846\u7ec4\u6210\u3002\u6bcf\u4e2a\u9875\u6846\u4e2d\u5305\u542b\u82e5\u5e72\u4e2a\u5bf9\u8c61\uff0c\u65e2\u6709\u5df2\u7ecf\u5206\u914d\u7684\u5bf9\u8c61\uff0c\u4e5f\u5305\u542b\u7a7a\u95f2\u7684\u5bf9\u8c61\u3002slab\u5206\u914d\u5668\u7684\u5927\u81f4\u7ec4\u6210\u56fe\u5982\u4e0b\uff1a<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/edsionte.com\/techblog\/archives\/4019\/slab_easy_struct\" rel=\"attachment wp-att-4026\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-4026\" title=\"slab_easy_struct\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2012\/05\/slab_easy_struct.jpeg\" alt=\"\" width=\"646\" height=\"364\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2012\/05\/slab_easy_struct.jpeg 808w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2012\/05\/slab_easy_struct-300x168.jpg 300w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/a><\/p>\n<p>\u6bcf\u4e2a\u9ad8\u901f\u7f13\u5b58\u901a\u8fc7kmem_cache\u7ed3\u6784\u6765\u63cf\u8ff0\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4e2d\u5305\u542b\u4e86\u5bf9\u5f53\u524d\u9ad8\u901f\u7f13\u5b58\u5404\u79cd\u5c5e\u6027\u4fe1\u606f\u7684\u63cf\u8ff0\u3002\u6240\u6709\u7684\u9ad8\u901f\u7f13\u5b58\u901a\u8fc7\u53cc\u94fe\u8868\u7ec4\u7ec7\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u9ad8\u901f\u7f13\u5b58\u94fe\u8868cache_chain\u3002\u6bcf\u4e2akmem_cache\u7ed3\u6784\u4e2d\u5e76\u4e0d\u5305\u542b\u5bf9\u5177\u4f53slab\u7684\u63cf\u8ff0\uff0c\u800c\u662f\u901a\u8fc7kmem_list3\u7ed3\u6784\u7ec4\u7ec7\u5404\u4e2aslab\u3002\u8be5\u7ed3\u6784\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct kmem_list3 {\r\n        struct list_head slabs_partial;\r\n        struct list_head slabs_full;\r\n        struct list_head slabs_free;\r\n        unsigned long free_objects;\r\n        unsigned int free_limit;\r\n        unsigned int colour_next;\r\n        spinlock_t list_lock;\r\n        struct array_cache *shared;\r\n        struct array_cache **alien;\r\n        unsigned long next_reap;\r\n        int free_touched;\r\n};\r\n<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8be5\u7ed3\u6784\u5c06\u5f53\u524d\u7f13\u5b58\u4e2d\u7684\u6240\u6709slab\u5206\u4e3a\u4e09\u4e2a\u96c6\u5408\uff1a\u7a7a\u95f2\u5bf9\u8c61\u7684slab\u94fe\u8868slabs_free\uff0c\u975e\u7a7a\u95f2\u5bf9\u8c61\u7684slab\u94fe\u8868slabs_full\u4ee5\u53ca\u90e8\u5206\u7a7a\u95f2\u5bf9\u8c61\u7684slab\u94fe\u8868slabs_partial\u3002\u6bcf\u4e2aslab\u6709\u76f8\u5e94\u7684slab\u63cf\u8ff0\u7b26\uff0c\u5373slab\u7ed3\u6784\uff0c\u5b83\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct slab {\r\n        struct list_head list;\r\n        unsigned long colouroff;\r\n        void *s_mem;\r\n        unsigned int inuse;\r\n        kmem_bufctl_t free;\r\n        unsigned short nodeid;\r\n};\r\n<\/pre>\n<p>slab\u63cf\u8ff0\u7b26\u4e2d\u7684list\u5b57\u6bb5\u6807\u660e\u4e86\u5f53\u524dslab\u5904\u4e8e\u4e09\u4e2aslab\u94fe\u8868\u7684\u5176\u4e2d\u4e00\u4e2a\u3002\u6211\u4eec\u5c06\u4e0a\u8ff0\u7684slab\u5206\u914d\u5668\u8fdb\u884c\u7ec6\u5316\uff0c\u53ef\u4ee5\u5f97\u5230\u4e0b\u9762\u7684\u7ed3\u6784\u56fe\uff1a<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/edsionte.com\/techblog\/archives\/4019\/slab_detail_struct\" rel=\"attachment wp-att-4027\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-4027\" title=\"slab_detail_struct\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2012\/05\/slab_detail_struct.jpeg\" alt=\"\" width=\"674\" height=\"294\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2012\/05\/slab_detail_struct.jpeg 842w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2012\/05\/slab_detail_struct-300x131.jpg 300w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><\/a><\/p>\n<h3>3.\u9ad8\u901f\u7f13\u5b58\u7684\u5206\u7c7b<\/h3>\n<p>slab\u9ad8\u901f\u7f13\u5b58\u5206\u4e3a\u4e24\u5927\u7c7b\uff0c\u666e\u901a\u9ad8\u901f\u7f13\u5b58\u548c\u4e13\u7528\u9ad8\u901f\u7f13\u5b58\u3002\u666e\u901a\u9ad8\u901f\u7f13\u5b58\u5e76\u4e0d\u9488\u5bf9\u5185\u6838\u4e2d\u7279\u5b9a\u7684\u5bf9\u8c61\uff0c\u5b83\u9996\u5148\u4f1a\u4e3akmem_cache\u7ed3\u6784\u672c\u8eab\u63d0\u4f9b\u9ad8\u901f\u7f13\u5b58\uff0c\u8fd9\u7c7b\u7f13\u5b58\u4fdd\u5b58\u5728cache_cache\u53d8\u91cf\u4e2d\uff0c\u8be5\u53d8\u91cf\u5373\u4ee3\u8868\u7684\u662fcache_chain\u94fe\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u5b83\u4e3a\u5185\u6838\u63d0\u4f9b\u4e86\u4e00\u79cd\u901a\u7528\u9ad8\u901f\u7f13\u5b58\u3002\u4e13\u7528\u9ad8\u901f\u7f13\u5b58\u662f\u6839\u636e\u5185\u6838\u6240\u9700\uff0c\u901a\u8fc7\u6307\u5b9a\u5177\u4f53\u7684\u5bf9\u8c61\u800c\u521b\u5efa\u3002<\/p>\n<p><strong>3.1 \u666e\u901a\u9ad8\u901f\u7f13\u5b58<\/strong><\/p>\n<p>slab\u5206\u914d\u5668\u4e2dkmem_cache\u662f\u7528\u6765\u63cf\u8ff0\u9ad8\u901f\u7f13\u5b58\u7684\u7ed3\u6784\uff0c\u56e0\u6b64\u5b83\u672c\u8eab\u4e5f\u9700\u8981slab\u5206\u914d\u5668\u5bf9\u5176\u8fdb\u884c\u9ad8\u901f\u7f13\u5b58\u3002cache_cache\u53d8\u91cf\u4fdd\u5b58\u7740\u5bf9\u9ad8\u901f\u7f13\u5b58\u63cf\u8ff0\u7b26\u7684\u9ad8\u901f\u7f13\u5b58\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstatic struct kmem_cache cache_cache = {\r\n        .batchcount = 1,\r\n        .limit = BOOT_CPUCACHE_ENTRIES,\r\n        .shared = 1,\r\n        .buffer_size = sizeof(struct kmem_cache),\r\n        .name = &quot;kmem_cache&quot;,\r\n};\r\n<\/pre>\n<p>slab\u5206\u914d\u5668\u6240\u63d0\u4f9b\u7684\u5c0f\u5757\u8fde\u7eed\u5185\u5b58\u7684\u5206\u914d\u662f\u901a\u8fc7\u901a\u7528\u9ad8\u901f\u7f13\u5b58\u5b9e\u73b0\u7684\u3002\u901a\u7528\u9ad8\u901f\u7f13\u5b58\u6240\u63d0\u4f9b\u7684\u5bf9\u8c61\u5177\u6709\u51e0\u4f55\u5206\u5e03\u7684\u5927\u5c0f\uff0c\u8303\u56f4\u4e3a32\u5230131072\u5b57\u8282\u3002\u5185\u6838\u4e2d\u63d0\u4f9b\u4e86kmalloc()\u548ckfree()\u4e24\u4e2a\u63a5\u53e3\u5206\u522b\u8fdb\u884c\u5185\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u3002<\/p>\n<p><strong>3.2 \u4e13\u7528\u9ad8\u901f\u7f13\u5b58<\/strong><\/p>\n<p>\u5185\u6838\u4e3a\u4e13\u7528\u9ad8\u901f\u7f13\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e\u63d0\u4f9b\u4e86\u4e00\u5957\u5b8c\u6574\u7684\u63a5\u53e3\uff0c\u6839\u636e\u6240\u4f20\u5165\u7684\u53c2\u6570\u4e3a\u5177\u4f53\u7684\u5bf9\u8c61\u5206\u914dslab\u7f13\u5b58\u3002<\/p>\n<p><strong>\u9ad8\u901f\u7f13\u5b58\u7684\u7533\u8bf7\u548c\u91ca\u653e<\/strong><\/p>\n<p>kmem_cache_create()\u7528\u4e8e\u5bf9\u4e00\u4e2a\u6307\u5b9a\u7684\u5bf9\u8c61\u521b\u5efa\u9ad8\u901f\u7f13\u5b58\u3002\u5b83\u4ececache_cache\u666e\u901a\u9ad8\u901f\u7f13\u5b58\u4e2d\u4e3a\u65b0\u7684\u4e13\u6709\u7f13\u5b58\u5206\u914d\u4e00\u4e2a\u9ad8\u901f\u7f13\u5b58\u63cf\u8ff0\u7b26\uff0c\u5e76\u628a\u8fd9\u4e2a\u63cf\u8ff0\u7b26\u63d2\u5165\u5230\u9ad8\u901f\u7f13\u5b58\u63cf\u8ff0\u7b26\u5f62\u6210\u7684cache_chain\u94fe\u8868\u4e2d\u3002kmem_cache_destory()\u7528\u4e8e\u64a4\u9500\u4e00\u4e2a\u9ad8\u901f\u7f13\u5b58\uff0c\u5e76\u5c06\u5b83\u4ececache_chain\u94fe\u8868\u4e0a\u5220\u9664\u3002<\/p>\n<p><strong>slab\u7684\u7533\u8bf7\u548c\u91ca\u653e<\/strong><\/p>\n<p>kmem_cache_alloc()\u5728\u5176\u53c2\u6570\u6240\u6307\u5b9a\u7684\u9ad8\u901f\u7f13\u5b58\u4e2d\u5206\u914d\u4e00\u4e2aslab\u3002\u76f8\u53cd\uff0ckmem_cache_free()\u5728\u5176\u53c2\u6570\u6240\u6307\u5b9a\u7684\u9ad8\u901f\u7f13\u5b58\u4e2d\u91ca\u653e\u4e00\u4e2aslab\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u5185\u6838\u4e2d\u57fa\u4e8e\u4f19\u4f34\u7b97\u6cd5\u5b9e\u73b0\u7684\u5206\u533a\u9875\u6846\u5206\u914d\u5668\u9002\u5408\u5927\u5757\u5185\u5b58\u7684\u8bf7\u6c42\uff0c\u5b83\u6240\u5206\u914d\u7684\u5185\u5b58\u533a\u662f\u4ee5\u9875\u6846\u4e3a\u57fa\u672c\u5355\u4f4d\u7684\u3002\u5bf9\u4e8e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4019","post","type-post","status-publish","format-standard","hentry","category-linuxc"],"views":16121,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=4019"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4019\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=4019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=4019"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=4019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}