{"id":60,"date":"2024-03-09T15:09:56","date_gmt":"2024-03-09T07:09:56","guid":{"rendered":"http:\/\/104.46.213.183\/?p=60"},"modified":"2024-03-09T15:09:56","modified_gmt":"2024-03-09T07:09:56","slug":"lab-locks","status":"publish","type":"post","link":"https:\/\/www.yewpo.top\/index.php\/60\/","title":{"rendered":"Lab locks"},"content":{"rendered":"<h1>Lab locks<\/h1>\n<h2>1 Memory allocator (moderate)<\/h2>\n<blockquote>\n<p>\u4fee\u6539kalloc\u673a\u5236\uff0c\u4f7f\u5f97\u6bcf\u4e2aCPU\u62e5\u6709\u81ea\u5df1\u7684\u5185\u5b58\u7a7a\u95f2\u5217\u8868\uff0c\u800c\u4e0d\u662f\u5171\u7528\u4e00\u4e2a\u7a7a\u95f2\u5217\u8868\u800c\u964d\u4f4e\u6548\u7387<\/p>\n<\/blockquote>\n<h3>1.1 \u673a\u5236\u8bbe\u8ba1<\/h3>\n<ul>\n<li>\u521d\u59cb\u5316\uff1a\u5c06\u6574\u4e2a\u7269\u7406\u7684\u7a7a\u95f2\u9875\u9762\u5168\u90e8\u5206\u914d\u7ed9CPU0\u3002<\/li>\n<li>\u5206\u914d\uff1a\u67e5\u8be2\u5f53\u524dCPU\u7684\u7a7a\u95f2\u5217\u8868\u3002\u5982\u679c\u5217\u8868\u4e0d\u4e3a\u7a7a\uff0c\u5219\u6211\u4eec\u62ff\u51fa\u4e00\u4e2a\u7a7a\u95f2\u9875\u9762\u3002\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u6211\u4eec\u67e5\u8be2\u5176\u4ed6CPU\u7684\u7a7a\u95f2\u5217\u8868\uff0c\u76f4\u5230\u67e5\u904d\u6240\u6709CPU\u7684\u5217\u8868\u3002\u5982\u679c\u6240\u4ee5CPU\u5217\u8868\u4e3a\u7a7a\uff0c\u5219\u5206\u914d\u5931\u8d25\u3002<\/li>\n<li>\u91ca\u653e\uff1a\u5c06\u8981\u91ca\u653e\u7684\u9875\u9762\u63d2\u5165\u5230\u5f53\u524dCPU\u7684\u7a7a\u95f2\u5217\u8868\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>\u5173\u952e\u90e8\u5206\u662f\u5982\u4f55\uff08\u62a2\u52ab\uff09\u5176\u4ed6CPU\u7684\u7a7a\u95f4<\/strong><\/p>\n<h3>1.2 \u521d\u59cb\u5316<\/h3>\n<p>\u521d\u59cb\u5316\u6240\u6709CPU\u7a7a\u95f2\u94fe\u8868\u7684\u9501\u3002\u5e76\u5c06\u6240\u6709\u7a7a\u95f2\u9875\u9762\u5206\u914d\u7ed9CPU0,\uff08\u6700\u521d\u53ea\u6709CPU0\u5728\u8fd0\u884c\uff0c\u6240\u4ee5kalloc\u65f6\u7684cpuid\u4e00\u5b9a\u662f0\uff0c\u6240\u4ee5\u4e0d\u7528\u5728\u610ffreerange\uff09\uff0c\u53ea\u7528\u4fee\u6539kinit\u51fd\u6570\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-C\">void\nkinit()\n{\n  for (int i = 0; i &lt; NCPU; ++i) {\n    initlock(&amp;kmem[i].lock, &quot;kmem&quot;);\n  }\n  freerange(end, (void *)PHYSTOP);\n}<\/code><\/pre>\n<h3>1.3 \u5206\u914d<\/h3>\n<p>\u4ece\u5f53\u524d\u8fdb\u7a0b\u6240\u5728\u7684CPU\u5f00\u59cb\uff0c\u67e5\u8be2\u6240\u6709\u7684CPU\u7684\u7a7a\u95f2\u5217\u8868\uff0c\u5982\u679c\u5b58\u5728\u7a7a\u95f2\u9875\u9762\uff0c\u5219\u5206\u914d\u6210\u529f\uff0c\u7ed3\u675f\u5faa\u73af\u3002\u56e0\u4e3a\u5b58\u5728\u7ade\u4e89\u95ee\u9898\uff0c\u6240\u4ee5\u67e5\u8be2\u7a7a\u95f2\u5217\u8868\u7684\u65f6\u5019\u9700\u8981\u76f8\u5e94CPU\u5217\u8868\u7684\u9501\u3002<\/p>\n<p>\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-C\">void *\nkalloc(void)\n{\n  struct run *r;\n\n  push_off();\n  int cpuindex = cpuid();\n  int index = cpuindex;\n  pop_off();\n\n  for (int i = 0; i &lt; NCPU; ++i) {\n    acquire(&amp;kmem[index].lock);\n    r = kmem[index].freelist;\n    if(r)\n      kmem[index].freelist = r-&gt;next;\n    release(&amp;kmem[index].lock);\n\n    if (r) {\n      break;\n    }\n\n    index = (index + 1) % NCPU;\n  }\n\n  if(r)\n    memset((char*)r, 5, PGSIZE); \/\/ fill with junk\n\n  return (void*)r;\n}<\/code><\/pre>\n<h3>1.4 \u91ca\u653e<\/h3>\n<p>\u5c06\u8981\u91ca\u653e\u7684\u9875\u9762\u63d2\u5165\u5230\u5f53\u524d\u8fdb\u7a0b\u6240\u5728\u7684CPU\u7a7a\u95f2\u94fe\u8868\u4e2d\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-C\">void\nkfree(void *pa)\n{\n  struct run *r;\n\n  if(((uint64)pa % PGSIZE) != 0 || (char*)pa &lt; end || (uint64)pa &gt;= PHYSTOP)\n    panic(&quot;kfree&quot;);\n\n  push_off();\n  int cpuindex = cpuid();\n  pop_off();\n\n  \/\/ Fill with junk to catch dangling refs.\n  memset(pa, 1, PGSIZE);\n\n  r = (struct run*)pa;\n\n  acquire(&amp;kmem[cpuindex].lock);\n  r-&gt;next = kmem[cpuindex].freelist;\n  kmem[cpuindex].freelist = r;\n  release(&amp;kmem[cpuindex].lock);\n}<\/code><\/pre>\n<h2>2 buffer cache (hard)<\/h2>\n<blockquote>\n<p>\u5728xv6\u7684\u6587\u4ef6\u7cfb\u7edf\u5b9e\u73b0\u4e2d\uff0c\u5c06\u6587\u4ef6\u7cfb\u7edf\u5206\u6210\u4e867\u5c42\u3002<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/raw.githubusercontent.com\/YEWPO\/yewpoblogonlinePic\/main\/image-20221120142235419.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/raw.githubusercontent.com\/YEWPO\/yewpoblogonlinePic\/main\/image-20221120142235419.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20221120142235419\" \/><\/div><\/p>\n<p>\u800c\u4f5c\u4e3a\u7b2c\u4e8c\u5c42\u7684<code>buffer cache<\/code>\u5c42\uff0c\u9700\u8981\u5b8c\u6210\u4ee5\u4e0b\u4e24\u4e2a\u5de5\u4f5c\uff1a<\/p>\n<ul>\n<li>\u786e\u4fdd\u6bcf\u65f6\u6bcf\u523b\u6bcf\u4e2a\u78c1\u76d8\u5757\u53ea\u6709\u4e00\u4e2a\u7f13\u5b58\u5728\u7f13\u5b58\u5c42\uff0c\u800c\u4e14\u53ea\u80fd\u6709\u4e00\u4e2a\u5185\u6838\u7ebf\u7a0b\u5728\u4f7f\u7528\u8fd9\u4e2a\u7f13\u5b58\u5757\u3002<\/li>\n<li>\u7f13\u5b58\u7ecf\u5e38\u4f7f\u7528\u7684\u5757\uff0c\u8fd9\u6837\u8fd9\u4e9b\u5757\u5c31\u4e0d\u9700\u8981\u6bcf\u6b21\u4ece\u78c1\u76d8\u8bfb\u53d6\u3002<\/li>\n<\/ul>\n<p>\u672c\u5b9e\u9a8c\u9700\u8981\u4f60\u4f18\u5316\u73b0\u6709\u7684\u7f13\u5b58\u5c42\u673a\u5236\uff0c\u51cf\u5c11\u9501\u7ade\u4e89\uff0c\u4f18\u5316\u67e5\u627e\u901f\u5ea6\u3002<\/p>\n<p>\u73b0\u6709\u7684\u673a\u5236\u4e0d\u8db3\u4e4b\u5904\u662f\uff0c\u67e5\u627e\u4e00\u4e2a\u5757\u662f\u5426\u7f13\u5b58\u9700\u8981\u67e5\u904d\u6574\u4e2a\u7f13\u5b58\u533a\uff0c\u4e0d\u4ec5\u67e5\u627e\u6548\u7387\u4f4e\uff0c\u800c\u4e14\u5360\u7528\u9501\u7684\u65f6\u95f4\u957f\uff0c\u5bb9\u6613\u4ea7\u751f\u9501\u7ade\u4e89\u3002<\/p>\n<p>\u672c\u5b9e\u9a8c\u91c7\u7528\u7684\u4f18\u5316\u65b9\u6cd5\u662f\uff0c\u4f7f\u7528\u54c8\u5e0c\u6876\u3002\u6bcf\u4e2a\u5757\u53f7\u5bf9\u5e94\u4e00\u4e2a\u54c8\u5e0c\u6876\uff0c\u76f4\u63a5\u5728\u54c8\u5e0c\u6876\u91cc\u67e5\u627e\uff0c\u4e0d\u4ec5\u51cf\u5c0f\u4e86\u641c\u7d22\u91cf\uff0c\u800c\u4e14\u6bcf\u6b21\u67e5\u627e\u53ea\u9700\u8981\u8fd9\u4e00\u4e2a\u54c8\u5e0c\u6876\u7684\u9501\uff0c\u5927\u5927\u964d\u4f4e\u4e86\u9501\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<p>\u7531\u4e8e\u672c\u5b9e\u9a8c\u548c\u4e0a\u534a\u4e2a\u5b9e\u9a8c\u95f4\u9694\u6709\u70b9\uff0c\u5b8c\u6210\u7684\u65ad\u65ad\u7eed\u7eed\uff0c\u601d\u8def\u4e0d\u591f\u8fde\u7eed\u3002<\/p>\n<p>\u603b\u4f53\u672c\u5b9e\u9a8c\u4ee3\u7801\u7ecf\u8fc7\u4e86\u56db\u4e2a\u7248\u672c\uff0c\u91cd\u5199\u4e86\u5f88\u591a\u6b21\uff0c\u4e3b\u8981\u5361\u5728\u4e86\u7b2c\u4e09\u4e2a\u7248\u672c\uff0c\u81ea\u5df1\u611f\u89c9\u5f88\u5408\u7406\uff0c\u4f46\u662f\u6ca1\u6709\u9075\u5faa\u7f13\u5b58\u5c42\u7684\u7b2c\u4e00\u8981\u6c42\u3002\u4e5f\u901a\u8fc7\u8fd9\u4e00\u4e2a\u7248\u672c\uff0c\u5927\u81f4\u660e\u767d\u4e86\u8fd9\u4e2a\u5b9e\u9a8c\u7684hints\u662f\u4ec0\u4e48\u610f\u601d\u3002<\/p>\n<\/blockquote>\n<h3>2.1 V1.0<\/h3>\n<h4>2.1.1 \u601d\u8def<\/h4>\n<p>\u76f4\u63a5\u4e3a\u6bcf\u4e2a\u6876\u5efa\u7acb\u94fe\u8868\uff0c\u5e76\u6ca1\u6709\u7ef4\u62a4\u5220\u9664\u5931\u6548\u7684\u8282\u70b9\u3002<\/p>\n<h4>2.1.2 \u95ee\u9898<\/h4>\n<p>\u53ef\u60f3\u800c\u77e5\uff0c\u8fd9\u4e2a\u7248\u672c\u601d\u7ef4\u6f0f\u6d1e\u5f88\u591a\u3002\u6240\u4ee5\u6211\u5199\u5b8c\u53d1\u73b0\u6ca1\u6709\u6d4b\u8bd5\u901a\u8fc7\u76f4\u63a5\u653e\u5f03\u4e86\u3002<\/p>\n<h3>2.2 V2.0<\/h3>\n<h4>2.2.1 \u601d\u8def<\/h4>\n<p>\u7ecf\u8fc71.0\u4e4b\u540e\uff0c\u6211\u89c9\u5f97\u5e76\u6ca1\u6709\u5fc5\u8981\u7ef4\u62a4\u4e00\u4e2abuf\u94fe\u8868\u5440\uff0c\u6211\u53ef\u4ee5\u4ee5\u78c1\u76d8\u5757\u53f7\u4e3a\u7b2c\u4e00\u5173\u952e\u5b57\uff0c\u8bbe\u5907\u53f7\u4e3a\u7b2c2\u5173\u952e\u5b57\uff0c\u7ef4\u62a4\u4e00\u4e2a\u78c1\u76d8\u5757\u5bf9\u5e94\u7684buf\u4e0b\u6807\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<h4>2.2.2 \u95ee\u9898<\/h4>\n<p>\u6700\u5927\u95ee\u9898\u8fd8\u662f\u81ea\u5df1\u5077\u61d2\u6ca1\u6709\u7ef4\u62a4\u5931\u6548\u7684\u4fe1\u606f\u3002\u540e\u6765\u6211\u4e5f\u53d1\u73b0\u8fd9\u662f\u4e00\u4e2a\u5927\u95ee\u9898\uff0c\u5bfc\u81f4\u6700\u540e\u5373\u4f7f\u901a\u8fc7\u54c8\u5e0c\u8868\u627e\u5230\u7684\u5bf9\u5e94buf\u7684\u7f16\u53f7\uff0c\u8fd9\u4e2abuf\u4e5f\u53ef\u80fd\u662f\u5931\u6548\u7684\u3002<\/p>\n<p>\u518d\u6b21\u5c1d\u8bd5\u4e86\u4e00\u4e0b\u4e24\u4e2a\u65b9\u5411\u7684\u4fee\u6539\uff1a<\/p>\n<ul>\n<li>\u5373\u4f7f\u5728\u54c8\u5e0c\u8868\u4e2d\u627e\u5230\u5bf9\u5e94\u7684buf\u7684\u4e0b\u6807\uff0c\u4e5f\u8981\u68c0\u67e5\u8fd9\u4e2a\u4e0b\u6807\u662f\u4e0d\u662f\u6709\u6548\u7684\uff08\u901a\u8fc7\u68c0\u67e5buf\u7684\u5757\u53f7\u548c\u8bbe\u5907\u53f7\uff09<\/li>\n<li>\u6216\u8bb8\u53ef\u4ee5\u7ef4\u62a4\u4e00\u4e2abuf\u5bf9\u5e94\u7684\u6876\u6807\u7b7e\uff0c\u6216\u8bb8\u901a\u8fc7\u68c0\u67e5\u8fd9\u4e2a\u6876\u6807\u7b7e\u6765\u5224\u65ad\u662f\u4e0d\u662f\u6709\u6548\u7684<\/li>\n<\/ul>\n<p>\u4f46\u662f\u8fd9\u4e2a\u4fee\u6539\u5e26\u6765\u7684\u95ee\u9898\u5f88\u591a\uff0c\u8fd8\u662f\u4e0d\u80fd\u786e\u5b9a\u6709\u6548\u7684\u786e\u5b9a\u8fd9\u4e2abuf\u662f\u4e0d\u662f\u6709\u6548\u7684\uff08\u8fd8\u662f\u6ca1\u6709\u4ece\u94fe\u8868\u4e2d\u5220\u9664\uff0c\u53ef\u80fd\u8be5\u5757\u88ab\u4f7f\u7528\u540e\u53c8\u88ab\u53e6\u4e00\u4e2a\u5757\u4f7f\u7528\u540e\u518d\u88ab\u8fd9\u4e2a\u5757\u4f7f\u7528\uff0c1-2-1\uff09\uff0c\u800c\u4e14\u8282\u70b9\u4fe1\u606f\u91cd\u590d\u3002\u603b\u4e4b\uff0c\u611f\u89c9\u95ee\u9898\u86ee\u591a\u3002<\/p>\n<h3>2.3 V3.0<\/h3>\n<h4>2.3.1 \u601d\u8def<\/h4>\n<p>\u8fd9\u4e2a\u601d\u8def\u66f4\u5077\u61d2\u4e86\uff0c\u5927\u81f4\u601d\u8def\u8fd8\u662f\u4f9d\u71672.0\u7248\u672c\u7684\uff0c\u60f3\u5230\u6bcf\u4e2a\u6876\u4e2d\u6709\u91cd\u590d\u7684\u8282\u70b9\uff0c\u800c\u4e14\u611f\u89c9\u4e0d\u597d\u7406\u89e3\uff0c\u4e0d\u5982\u6bcf\u4e2a\u6876\u4e0d\u7ef4\u62a4\u94fe\u8868\u4e86\uff0c\u76f4\u63a5\u6bcf\u4e2a\u6876\u53ea\u80fd\u653e\u4e00\u4e2a\u5143\u7d20\uff0c\u5f53\u6709\u65b0\u5143\u7d20\u8981\u4f7f\u7528\u8fd9\u4e2a\u6876\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u8986\u76d6\u6389\u539f\u6765\u7684\u4fe1\u606f\u5373\u53ef\u3002<\/p>\n<h4>2.3.2 \u95ee\u9898<\/h4>\n<p>\u5199\u4e86\u597d\u591a\u597d\u591a\u904d\uff0c\u611f\u89c9\u6ca1\u6709\u6ca1\u6709\u95ee\u9898\uff0c\u5982\u679c\u5728\u4e0d\u5728\u6876\u4e2d\u610f\u601d\u662f\u6700\u8fd1\u6ca1\u6709\u4f7f\u7528\u4e86\u5440\uff0c\u90a3\u4e48\u6211\u76f4\u63a5\u904d\u5386\u6240\u6709buf\u5757\uff0c\u770b\u770b\u6709\u6ca1\u6709\u6ca1\u6709\u6ca1\u88ab\u4f7f\u7528\uff0c\u7136\u540e\u5206\u914d\u8fd9\u4e2a\u7a7a\u95f2\u5757\u7ed9\u4ed6\uff0c\u7136\u540e\u66f4\u65b0\u54c8\u5e0c\u6876\u5c31\u884c\u4e86\u5440\u3002<\/p>\n<p>\u4f46\u662f\u5f97\u5230\u7684\u53cd\u9988\u662f\uff0c\u64cd\u4f5c\u7cfb\u7edf\u80fd\u591f\u6b63\u5e38\u7684\u542f\u52a8\uff0c\u4f46\u662f\uff0c\u4e00\u505a\u6d4b\u8bd5\u5c31\u5f97\u5230<code>bget<\/code>\u7684<code>panic<\/code>\uff0c\u6ca1\u6709\u53ef\u4ee5\u4f7f\u7528\u7684buf\u4e86\uff0c\u600e\u4e48\u56de\u4e8b\u5462\u3002<\/p>\n<p>\u540e\u6765\u60f3\u60f3\uff0c\u5982\u679c\u5728\u6876\u4e2d\u6ca1\u6709\u5339\u914d\u5230\uff0c\u90a3\u4e48\u6211\u627e\u5230\u65b0\u7684\u5757\u4e4b\u540e\uff0c\u66b4\u529b\u9a71\u9010\u6876\u4e2d\u7684\u5757\uff08\u8fd9\u4e2a\u5757\u53ef\u80fd\u8fd8\u5728\u88ab\u4f7f\u7528\uff09\uff0c\u66f4\u65b0\u4e3a\u65b0\u7684\u5757\uff0c\u90a3\u4e48\u5f53\u539f\u6765\u7684\u5757\u53c8\u8981\u518d\u6b21\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u6876\u4e2d\u6ca1\u6709\u4e86\uff0c\u4ee5\u4e3a\u4e0d\u5728\u7f13\u5b58\u5c42\u4e2d\u4e86\uff0c\u7136\u540e\u53bb\u627e\u7684\u65b0\u7684\u5757\uff0c\u91cd\u65b0\u4ece\u78c1\u76d8\u4e2d\u83b7\u53d6\u8fd9\u4e2a\u5757\u3002\u6b64\u65f6\uff0c\u7f13\u5b58\u5c42\u4e2d\u5c31\u6709\u4e24\u4e2a\u76f8\u540c\u7684\u78c1\u76d8\u5757\uff0c\u8fdd\u80cc\u4e86\u7f13\u5b58\u5c42\u7684\u7b2c\u4e00\u4e2a\u5de5\u4f5c\u8981\u6c42\uff0c\u6bcf\u4e2a\u78c1\u76d8\u5757\u53ea\u80fd\u6709\u4e00\u4e2a\u7f13\u5b58\u5728\u7f13\u5b58\u5c42\u4e2d\u3002<\/p>\n<p>\u8fd9\u4e2a\u601d\u8def\u5ba3\u544a\u7834\u4ea7<\/p>\n<h4>2.3.3 \u603b\u7ed3<\/h4>\n<ul>\n<li>\u770b\u6765\u8fd8\u662f\u5f97\u50cf\u7b2c\u4e00\u7248\u4e00\u6837\u7ef4\u62a4\u4e00\u4e2abuf\u94fe\u8868\u3002<\/li>\n<li>\u5fc5\u987b\u5b58\u5728\u5220\u9664\u5931\u6548\u5757\uff0c\u5e76\u4e14\u91cd\u65b0\u5206\u914d\u8fd9\u4e2a\u5931\u6548\u5757\u5230\u94fe\u8868\u4e2d\u3002<\/li>\n<\/ul>\n<h3>2.4 V4.0<\/h3>\n<p>\u653e\u5f03\u5077\u61d2\u60f3\u6cd5\uff0c\u56de\u5f52\u7248\u672c\u4e00\u3002<\/p>\n<h4>2.4.1 \u521d\u59cb\u5316\u8868<\/h4>\n<p>\u521d\u59cb\u5316\u54c8\u5e0c\u8868\u5982\u4e0b\uff1a<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/raw.githubusercontent.com\/YEWPO\/yewpoblogonlinePic\/main\/image-20221120151123247.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/raw.githubusercontent.com\/YEWPO\/yewpoblogonlinePic\/main\/image-20221120151123247.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20221120151123247\" \/><\/div><\/p>\n<p>\u4e00\u822c\u800c\u8a00\uff0c\u54c8\u5e0c\u6876\u7684\u4e2a\u6570\u4e3a\u8d28\u6570\u4e2a\uff0c\u8fd9\u91cc\u9009\u7528\u4e86hints\u4e2d\u7684\u4e2a\u657013\u3002<\/p>\n<p>\u6bcf\u4e2a\u6876\u5206\u914d\u4e09\u4e2a\u7a7a\u95f2\u8282\u70b9\uff0c\u8fd9\u4e9b\u8282\u70b9\u7684\u4f9d\u6b21\u5bf9\u5e94buf\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<strong>\u8fd9\u91cc\u9700\u898139\u8282\u70b9\uff0c\u800c<code>param.h<\/code>\u4e2d\u5b8f\u5b9a\u4e49\u768430\u4e2a\u4e0d\u591f\uff0c\u6240\u4ee5\u6211\u4eec\u628a\u4ed6\u6539\u621039<\/strong>\u3002<\/p>\n<h4>2.4.2 \u67e5\u627e\u7f13\u5b58<\/h4>\n<p>\u627e\u5230\u5757\u53f7\u5bf9\u5e94\u7684\u6876\uff0c\u83b7\u53d6\u6876\u9501\uff0c\u904d\u5386\u6574\u4e2a\u6876\uff0c\u5982\u679c\u627e\u5230\u4e86\u5bf9\u5e94\u7684\u7f13\u5b58\uff0c\u5757\u7684\u5f15\u7528\u8ba1\u6570\u52a0\u4e00\uff0c\u91ca\u653e\u6876\u9501\uff0c\u83b7\u53d6\u5757\u9501\uff0c\u8fd4\u56de\u8be5\u5757\u3002<\/p>\n<h4>2.4.3 \u672a\u547d\u4e2d<\/h4>\n<p>\u82e5\u6ca1\u6709\u67e5\u8be2\u5230\u7f13\u5b58\uff0c\u5219\u67e5\u627e\u8be5\u6876\u4e2d\u6709\u6ca1\u6709\u7a7a\u95f2\u7684\u5757\uff0c\u82e5\u6709\uff0c\u4fee\u6539\u8be5\u5757\u7684\u4fe1\u606f\uff08\u5757\u53f7\uff0c\u8bbe\u5907\u53f7\uff0c\u6709\u6548\u4f4d\uff09\uff0c\u5f15\u7528\u8ba1\u6570\u8bbe\u7f6e\u4e3a1\uff0c\u91ca\u653e\u6876\u9501\uff0c\u83b7\u53d6\u5757\u9501\uff0c\u8fd4\u56de\u8be5\u5757\u3002<\/p>\n<p>\u5982\u679c\u8be5\u6876\u4e2d\u6ca1\u6709\u7a7a\u95f2\u5757\uff0c\u90a3\u53ea\u80fd\u4ece\u5176\u4ed6\u6876\u4e2d\u91ca\u653e\u7a7a\u95f2\u5757\u4e86\u3002<\/p>\n<p><strong>\u6b64\u65f6\u6211\u4eec\u8981\u91ca\u653e\u8be5\u6876\u7684\u9501<\/strong>\uff0c\u56e0\u4e3a\u540e\u9762\u540c\u65f6\u9700\u8981\u4e24\u4e2a\u9501\uff0c\u6211\u4eec\u5148\u91ca\u653e\u8fd9\u4e2a\u9501\uff0c\u4e00\u904d\u5176\u4ed6\u5185\u6838\u7ebf\u7a0b\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6876\u3002\u8fd8\u6709\u4e00\u4e2a\u539f\u56e0\u662f\uff0c\u540e\u9762\u6211\u4eec\u67e5\u627e\u5176\u4ed6\u6876\u7684\u7a7a\u95f2\u5757\u7684\u65f6\u5019\uff0c\u4e5f\u4e0d\u9700\u8981\u4e00\u76f4\u62ff\u5230\u8fd9\u4e2a\u9501\uff0c\u76f4\u5230\u67e5\u627e\u5230\u4e00\u4e2a\u7a7a\u95f2\u5757\u540e\uff0c\u6211\u4eec\u624d\u9700\u8981\u8fd9\u4e2a\u9501\uff0c\u5c06\u8fd9\u4e2a\u7a7a\u95f2\u5757\uff0c\u63d2\u5165\u5230\u8fd9\u4e2a\u6876\u7684\u94fe\u8868\u4e2d\u3002<\/p>\n<h4>2.4.4 \u8de8\u6876\u67e5\u627e\u5e76\u9a71\u9010<\/h4>\n<p><strong>\uff01important<\/strong>\u8003\u8651\u8fd9\u6837\u4e00\u79cd\u6b7b\u9501\u60c5\u51b5\uff1a\u5f53\u68761\u5728\u68762\u4e2d\u67e5\u627e\u7a7a\u95f2\u5757\u7684\u65f6\u5019\u9700\u8981\u68762\u7684\u9501\uff0c\u800c\u8fd9\u4e2a\u65f6\u5019\u68762\u8981\u67e5\u627e\u68761\u4e2d\u7684\u7a7a\u95f2\u5757\u7684\u65f6\u5019\uff0c\u9700\u8981\u68761\u7684\u9501\uff0c\u8fd9\u4e2a\u65f6\u5019\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u4e92\u76f8\u7b49\u5f85\uff0c\u5bfc\u81f4\u6b7b\u9501\u3002\u6240\u4ee5\u6211\u95e8\u9700\u8981\u4e00\u4e2a\u9501\uff0c\u4fdd\u8bc1\u4e00\u4e2a\u65f6\u523b\uff0c\u53ea\u6709\u4e00\u4e2a\u5185\u6838\u7ebf\u7a0b\u5728\u8de8\u6876\u67e5\u627e\u3002<\/p>\n<p>\u6216\u8bb8\u8fd9\u4e2a\u5c31\u662fhints\u91cc\u7684<code>bcache lock<\/code>\u7684\u7528\u5904\u5427\uff0c\u6211\u5230\u8fd9\u4e00\u6b65\u624d\u7406\u89e3\u8fd9\u4e2a\u610f\u601d\u3002\uff08\u4e4b\u524d\u60f3\u4e0d\u61c2\u6709bucket\u9501\u540e\u6211\u8fd8\u9700\u8981bcache\u9501\u5e72\u4ec0\u4e48\u5462\uff09<\/p>\n<p>\u5f53\u4e4b\u524d\u7684\u6b65\u9aa4\u6ca1\u6709\u67e5\u627e\u5230\u7a7a\u95f2\u5757\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5c31\u9700\u8981\u8de8\u6876\u67e5\u627e\uff0c\u6b64\u65f6\u6211\u4eec\u8981\u5148\u83b7\u5f97\u4e00\u4e2a\u8de8\u6876\u67e5\u627e\u9501\u3002\u4e0d\u5982\u5c31\u4f7f\u7528<code>bcache<\/code>\u9501\u3002<\/p>\n<p>\u67e5\u627e\u6bcf\u4e00\u4e2a\u6876\uff1a\u83b7\u53d6\u5f53\u524d\u6876\u7684\u6876\u9501\uff0c\u67e5\u8be2\u6709\u65e0\u7a7a\u95f2\u5757\u3002\u82e5\u6709\uff0c\u6211\u4eec\u4ece\u6876\u4e2d\u5220\u9664\u8fd9\u4e2a\u5757\uff0c\u91ca\u653e\u6876\u9501\u3002\u83b7\u53d6\u539f\u6765\u7684\u6876\u9501\uff0c\u5c06\u8be5\u5757\u63d2\u5165\u5230\u94fe\u8868\u4e2d\uff0c\u4fee\u6539\u8be5\u5757\u7684\u4fe1\u606f\uff0c\u5f15\u7528\u8ba1\u6570\u8bbe\u7f6e\u4e3a1\uff0c\u91ca\u653e\u6876\u9501\uff0c\u91ca\u653e\u8de8\u6876\u67e5\u627e\u9501\uff0c\u83b7\u5f97\u5757\u9501\uff0c\u8fd4\u56de\u8be5\u5757\u3002\u82e5\u5728\u8be5\u6876\u4e2d\u6ca1\u6709\u67e5\u8be2\u5230\u7a7a\u95f2\u5757\uff0c\u5219\u91ca\u653e\u8be5\u6876\u9501\uff0c\u7ee7\u7eed\u4e0b\u4e00\u4e2a\u6876\u3002<\/p>\n<p>\u82e5\u6240\u6709\u7684\u6876\u5747\u6ca1\u6709\u7a7a\u95f2\u5757\uff0c\u5219\u771f\u5c31\u6ca1\u6709\u7a7a\u95f2\u5757\u4e86\uff0c\u5c31\u7b80\u5355\u7684panic\u5427\u3002<\/p>\n<h4>2.4.5 \u4fee\u6539\u5176\u4ed6\u51fd\u6570<\/h4>\n<p>\u6309\u7167hints\u7684\u8bf4\u6cd5\uff0c\u5220\u53bb\u6240\u6709\u7684LRU\u7b97\u6cd5\u76f8\u5173\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u4e86\u89e3\u6bcf\u4e2a\u9501\u4fdd\u62a4\u7684\u5bf9\u8c61<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u8de8\u6876\u67e5\u627e\u9501\uff1a\u4fdd\u8bc1\u4e86\u6bcf\u4e2a\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u5185\u6838\u7ebf\u7a0b\u8fdb\u884c\u8de8\u6876\u67e5\u627e\u3002<\/li>\n<li>\u5bf9\u4e8e\u6876\u9501\uff1a\u4fdd\u62a4\u6574\u4e2a\u6876\u7684\u94fe\u8868\uff0c\u800c\u4e14\u4fdd\u8bc1\u6bcf\u4e2abuf\u53ea\u80fd\u6709\u4e00\u4e2a\u5185\u6838\u7ebf\u7a0b\u8fdb\u884c\u8bfb\u6216\u5199\u3002<\/li>\n<\/ul>\n<p>\u6240\u4ee5\uff0c\u4fee\u6539\u5176\u4ed6\u51fd\u6570\uff0c\u5f53\u4ed6\u4eec\u8981\u4fee\u6539buf\u76f8\u5173\u4fe1\u606f\u65f6\uff0c\u6216\u5f97\u5bf9\u5e94\u7684\u6876\u9501\u3002<\/p>\n<h3>2.5 \u4ee3\u7801<\/h3>\n<p><code>bio.c<\/code><\/p>\n<pre><code class=\"language-c\">\/\/ Buffer cache.\n\/\/\n\/\/ The buffer cache is a linked list of buf structures holding\n\/\/ cached copies of disk block contents.  Caching disk blocks\n\/\/ in memory reduces the number of disk reads and also provides\n\/\/ a synchronization point for disk blocks used by multiple processes.\n\/\/\n\/\/ Interface:\n\/\/ * To get a buffer for a particular disk block, call bread.\n\/\/ * After changing buffer data, call bwrite to write it to disk.\n\/\/ * When done with the buffer, call brelse.\n\/\/ * Do not use the buffer after calling brelse.\n\/\/ * Only one process at a time can use a buffer,\n\/\/     so do not keep them longer than necessary.\n\n#include &quot;types.h&quot;\n#include &quot;param.h&quot;\n#include &quot;spinlock.h&quot;\n#include &quot;sleeplock.h&quot;\n#include &quot;riscv.h&quot;\n#include &quot;defs.h&quot;\n#include &quot;fs.h&quot;\n#include &quot;buf.h&quot;\n\n#define HASHMAP(number) (number % NBUCKET)\n\nstruct {\n  struct spinlock lock;\n  struct buf buf[NBUF];\n\n  struct {\n    struct spinlock lock;\n    struct buf head;\n  } bucket[NBUCKET];\n} bcache;\n\nvoid\nbinit(void)\n{\n  struct buf *b;\n\n  initlock(&amp;bcache.lock, &quot;bcache.eviction&quot;);\n\n  for (int i = 0; i &lt; NBUCKET; ++i) {\n    initlock(&amp;bcache.bucket[i].lock, &quot;bcache.bucket&quot;);\n    bcache.bucket[i].head.next = 0;\n\n    b = &amp;bcache.bucket[i].head;\n    for (int j = i * 3; j &lt; i * 3 + 3; j++) {\n      initsleeplock(&amp;bcache.buf[j].lock, &quot;buffer&quot;);\n      bcache.buf[j].next = b-&gt;next;\n      b-&gt;next = &amp;bcache.buf[j];\n      b = b-&gt;next;\n    }\n  }\n}\n\n\/\/ Look through buffer cache for block on device dev.\n\/\/ If not found, allocate a buffer.\n\/\/ In either case, return locked buffer.\nstatic struct buf*\nbget(uint dev, uint blockno)\n{\n  struct buf *b;\n\n  int index = HASHMAP(blockno);\n\n  acquire(&amp;bcache.bucket[index].lock);\n  for (b = bcache.bucket[index].head.next; b; b = b-&gt;next) {\n    if (b-&gt;blockno == blockno &amp;&amp; b-&gt;dev == dev) {\n      b-&gt;refcnt++;\n      release(&amp;bcache.bucket[index].lock);\n      acquiresleep(&amp;b-&gt;lock);\n      return b;\n    }\n  }\n\n  for (b = bcache.bucket[index].head.next; b; b = b-&gt;next) {\n    if (b-&gt;refcnt == 0) {\n      b-&gt;blockno = blockno;\n      b-&gt;dev = dev;\n      b-&gt;valid = 0;\n      b-&gt;refcnt = 1;\n      release(&amp;bcache.bucket[index].lock);\n      acquiresleep(&amp;b-&gt;lock);\n      return b;\n    }\n  }\n  release(&amp;bcache.bucket[index].lock);\n\n  acquire(&amp;bcache.lock);\n  for (int i = 0; i &lt; NBUCKET; ++i) {\n    if (i != index) {\n      struct buf *pre;\n      acquire(&amp;bcache.bucket[i].lock);\n      for (pre = &amp;bcache.bucket[i].head, b = bcache.bucket[i].head.next; b; pre = b, b = b-&gt;next) {\n        if (b-&gt;refcnt == 0) {\n          pre-&gt;next = b-&gt;next;\n          release(&amp;bcache.bucket[i].lock);\n\n          acquire(&amp;bcache.bucket[index].lock);\n          b-&gt;next = bcache.bucket[index].head.next;\n          bcache.bucket[index].head.next = b;\n          b-&gt;blockno = blockno;\n          b-&gt;dev = dev;\n          b-&gt;valid = 0;\n          b-&gt;refcnt = 1;\n          release(&amp;bcache.bucket[index].lock);\n          acquiresleep(&amp;b-&gt;lock);\n          release(&amp;bcache.lock);\n          return b;\n        }\n      }\n      release(&amp;bcache.bucket[i].lock);\n    }\n  }\n  release(&amp;bcache.lock);\n\n  panic(&quot;bget: no buffers&quot;);\n}\n\n\/\/ Return a locked buf with the contents of the indicated block.\nstruct buf*\nbread(uint dev, uint blockno)\n{\n  struct buf *b;\n\n  b = bget(dev, blockno);\n  if(!b-&gt;valid) {\n    virtio_disk_rw(b, 0);\n    b-&gt;valid = 1;\n  }\n  return b;\n}\n\n\/\/ Write b&#039;s contents to disk.  Must be locked.\nvoid\nbwrite(struct buf *b)\n{\n  if(!holdingsleep(&amp;b-&gt;lock))\n    panic(&quot;bwrite&quot;);\n  virtio_disk_rw(b, 1);\n}\n\n\/\/ Release a locked buffer.\n\/\/ Move to the head of the most-recently-used list.\nvoid\nbrelse(struct buf *b)\n{\n  if(!holdingsleep(&amp;b-&gt;lock))\n    panic(&quot;brelse&quot;);\n\n  int index = HASHMAP(b-&gt;blockno);\n\n  releasesleep(&amp;b-&gt;lock);\n\n  acquire(&amp;bcache.bucket[index].lock);\n  b-&gt;refcnt--;\n  release(&amp;bcache.bucket[index].lock);\n}\n\nvoid\nbpin(struct buf *b) {\n  int index = HASHMAP(b-&gt;blockno);\n\n  acquire(&amp;bcache.bucket[index].lock);\n  b-&gt;refcnt++;\n  release(&amp;bcache.bucket[index].lock);\n}\n\nvoid\nbunpin(struct buf *b) {\n  int index = HASHMAP(b-&gt;blockno);\n\n  acquire(&amp;bcache.bucket[index].lock);\n  b-&gt;refcnt--;\n  release(&amp;bcache.bucket[index].lock);\n}<\/code><\/pre>\n<p><code>buf.h<\/code><\/p>\n<pre><code class=\"language-C\">struct buf {\n  int valid;   \/\/ has data been read from disk?\n  int disk;    \/\/ does disk &quot;own&quot; buf?\n  uint dev;\n  uint blockno;\n  struct sleeplock lock;\n  uint refcnt;\n  struct buf *next;\n  uchar data[BSIZE];\n};<\/code><\/pre>\n<p><code>param.h<\/code><\/p>\n<pre><code class=\"language-C\">#define NPROC        64  \/\/ maximum number of processes\n#define NCPU          8  \/\/ maximum number of CPUs\n#define NOFILE       16  \/\/ open files per process\n#define NFILE       100  \/\/ open files per system\n#define NINODE       50  \/\/ maximum number of active i-nodes\n#define NDEV         10  \/\/ maximum major device number\n#define ROOTDEV       1  \/\/ device number of file system root disk\n#define MAXARG       32  \/\/ max exec arguments\n#define MAXOPBLOCKS  10  \/\/ max # of blocks any FS op writes\n#define LOGSIZE      (MAXOPBLOCKS*3)  \/\/ max data blocks in on-disk log\n#define NBUF         (MAXOPBLOCKS*3 + 9)  \/\/ size of disk block cache\n#define FSSIZE       2000  \/\/ size of file system in blocks\n#define MAXPATH      128   \/\/ maximum file path name\n#define NBUCKET      13<\/code><\/pre>\n<h2>3 END<\/h2>\n<p>\u65ad\u65ad\u7eed\u7eed\uff0c\u957f\u8fbe\u4e00\u4e2a\u4fe1\u606f\u624d\u5199\u5b8c\u8fd9\u4e2a\u5b9e\u9a8c\uff0c\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\u3002<\/p>\n<pre><code class=\"language-C\">== Test running kalloctest == \n$ make qemu-gdb\n(116.6s) \n== Test   kalloctest: test1 == \n  kalloctest: test1: OK \n== Test   kalloctest: test2 == \n  kalloctest: test2: OK \n== Test   kalloctest: test3 == \n  kalloctest: test3: OK \n== Test kalloctest: sbrkmuch == \n$ make qemu-gdb\nkalloctest: sbrkmuch: OK (14.0s) \n== Test running bcachetest == \n$ make qemu-gdb\n(22.4s) \n== Test   bcachetest: test0 == \n  bcachetest: test0: OK \n== Test   bcachetest: test1 == \n  bcachetest: test1: OK \n== Test usertests == \n$ make qemu-gdb\nusertests: OK (78.4s) \n== Test time == \ntime: OK \nScore: 80\/80\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>xv6\u64cd\u4f5c\u7cfb\u7edf\u5b9e\u9a8c8\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[14],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry","category-os","tag-xv6"],"_links":{"self":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":0,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}