Sự khác nhau giữa Buffers và Cached

Tram Ho

Cached Là kích thước của page cache. Buffers là kích thước của in-memory block I/O buffers. 2. Trả lời dài: Cả Cached và Buffers đều có ý nghĩa là vùng lưu trữ tạm, nhưng mục đích sử dụng thì khác nhau, tổng quan thì có một số điểm sau: Mục đích của cached là …

1. Trả lời ngắn

  • Cached là kích thước của page cache.
  • Buffers là kích thước của in-memory block I/O buffers.

2. Trả lời dài

Cả Cached và Buffers đều có ý nghĩa là vùng lưu trữ tạm, nhưng mục đích sử dụng thì khác nhau, tổng quan thì có một số điểm sau:

  • Mục đích của cached là tạo ra một vùng nhớ tốc độ cao nhằm tăng tốc quá trình đọc/ghi file ra đĩa, trong khi buffers là tạo ra 1 vùng nhớ tạm có tốc độ bình thường, mục đích để gom data hoặc giữ data để dùng cho mục đích nào đó.
  • Cached được tạo từ static RAM (SRAM) nên nhanh hơn dynamic RAM (DRAM) dùng để tạo ra buffers.
  • Buffers thường dùng cho các tiến trình input/output, trong khi cached chủ yếu được dùng cho các tiến trình đọc/ghi file ra đĩa
  • Cached có thể là một phần của đĩa (đĩa có tốc độ cao) hoặc RAM trong khi buffers chỉ là một phần của RAM (không thể dùng đĩa để tạo ra buffers)

2.1 Cached

Cached là kích thước của Linux page cache, không bao gồm bộ nhớ trong swap cached thường được gọi là SwapCached (như vậy tổng kích thước page cache là Cached + SwapCached). Linux thực thi tất cả các thao tác I/O file thông qua page cache. Vùng nhớ này được dùng để tăng tốc quá trình đọc ghi file theo mô tả dưới đây.

.u74d25158f837ae7a4fd239b2be379cde { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .u74d25158f837ae7a4fd239b2be379cde:active, .u74d25158f837ae7a4fd239b2be379cde:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u74d25158f837ae7a4fd239b2be379cde { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u74d25158f837ae7a4fd239b2be379cde .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u74d25158f837ae7a4fd239b2be379cde .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .u74d25158f837ae7a4fd239b2be379cde:hover .postTitle { text-decoration: underline!important; }

  Tuyệt kỹ: Sử dụng cache để tăng tốc hệ thống

.u3f6597826de56820eff631686af95ffd { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .u3f6597826de56820eff631686af95ffd:active, .u3f6597826de56820eff631686af95ffd:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u3f6597826de56820eff631686af95ffd { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u3f6597826de56820eff631686af95ffd .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u3f6597826de56820eff631686af95ffd .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .u3f6597826de56820eff631686af95ffd:hover .postTitle { text-decoration: underline!important; }

  Session, Cookie và Cache, bạn đã thực sự hiểu chúng?

Write được implement rất đơn giản là đánh dấu các trang tương ứng trong page cache là dirty (dirty corresponding pages), các flusher thread sau đó sẽ định kỳ ghi vô đĩa bất kỳ một dirty page nào. Có thể xem ví dụ sau để hiểu về cơ chế ghi

Giải thích như sau:

  • Lệnh đầu tiên ghi 1 file có kích thước 10MB
  • Như ta thấy thì dữ liệu của file được đánh dấu là dirty trong page cached bằng cách kiểm tra trong lệnh số 2, ở đây là 712 kB được đánh dấu là dirty
  • Lệnh số 3 giúp ta buộc sync dữ liệu từ page cached vào đĩa
  • Lệnh số 4 kiểm tra lại trong page cached xem có dữ liệu nào được đánh dấu dirty hay không, như ta thấy thì không còn (lưu ý là có thể >0 kB do có các tiến trình khác)

Kernel linux có một tiến trình là flush để định kì ghi dữ liệu từ page cached vào đĩa, có thể kiểm tra như sau

Read được implement bằng cách trả về dữ liệu từ page cached, nếu dữ liệu đó không có trong cached, data đó sẽ được di cư lần đầu tiên từ đĩa vào page cache. Trên các hệ thống Linux hiện đại, cached dễ dàng có thể lên tới hàng GB, và nó sẽ co giãn để đáp ứng áp lực của bộ nhớ. Hệ thống sẽ “xóa” page cache cùng với việc swap data từ page cache ra đĩa để làm trống bộ nhớ nhiều hơn nếu cần.

2.2 Buffers

Buffers là vùng đệm mục đích để tổ chức và giữ dữ liệu trong một vùng nhớ nào đó cho đến khi dữ liệu này được chuyển qua nơi khác. Thông thường buffers được dùng trong các quá trình input/output mà tốc độ nhận và xuất dữ liệu khác biệt lớn ( CPU và máy in).

Ví dụ

  • Thao tác in văn bản: CPU là thiết bị có tốc độ cao, trong khi máy in là một thiết bị có tốc độ xử lí chậm. Khi một người ra lệnh in, dữ liệu in sẽ được đẩy vào vùng buffers (điểm khác biệt với cached là vùng này không cần tốc độ cao). Sau đó máy in có thể truy cập vào vùng buffers đó với tốc độ rùa bò trong khi CPU đã đc giải phóng để đi làm nhiệm vụ khác.
  • Thao tác burn đĩa CD: quá trình tương tự máy in nhưng có một điểm là nếu data có kích thước nhỏ, khi được đưa vào vùng buffers thì các data này sẽ được gom lại thành từng block có kích thước bằng kích thước của block size .

Trước Linux kernel 2.4, có 2 loại cached riêng biệt, file thì trong page cache, disk block thì trong buffers cache. Đây là một cách đơn giản để implement, nó khá rõ ràng nhưng kém hiệu quả. Từ phiên bản 2.4, nội dung của 2 loại cache này được hợp nhất.

Như ta thấy dòng -/+ bufferss/cached đã được hợp nhất, nếu cộng cột buffers và cached ở dòng thứ nhất và cột free của dóng số 1 sẽ thấy bằng tổng cột số 3 dòng số 3

1179 + 248 + 10306 = 11733

Next-step: tìm hiểu thuật toán áp dụng cho page cache và buffers (khả năng là LRU với page cache và FIFO với buffers, nên ta thấy buffers khá là giống queue)

3. Ref

  • https://www.quora.com/What-is-the-difference-between-Buffers-and-Cached-columns-in-proc-meminfo-output
  • http://www.differencebetween.net/technology/hardware-technology/difference-between-cache-and-buffer/

code24h

.u50812aeaf7767f36b4456e5d8333e20c { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .u50812aeaf7767f36b4456e5d8333e20c:active, .u50812aeaf7767f36b4456e5d8333e20c:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u50812aeaf7767f36b4456e5d8333e20c { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u50812aeaf7767f36b4456e5d8333e20c .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u50812aeaf7767f36b4456e5d8333e20c .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .u50812aeaf7767f36b4456e5d8333e20c:hover .postTitle { text-decoration: underline!important; }

  Memoization là gì? LRU cache là gì?

.uf8364c9b77631458313795af0e6b92f2 { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .uf8364c9b77631458313795af0e6b92f2:active, .uf8364c9b77631458313795af0e6b92f2:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .uf8364c9b77631458313795af0e6b92f2 { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .uf8364c9b77631458313795af0e6b92f2 .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .uf8364c9b77631458313795af0e6b92f2 .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .uf8364c9b77631458313795af0e6b92f2:hover .postTitle { text-decoration: underline!important; }

  Memcached là gì ? Sử dụng memcached để tăng tốc hệ thống server

Chia sẻ bài viết ngay

Nguồn bài viết : code24h .u50812aeaf7767f36b4456e5d8333e20c { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .u50812aeaf7767f36b4456e5d8333e20c:active, .u50812aeaf7767f36b4456e5d8333e20c:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .u50812aeaf7767f36b4456e5d8333e20c { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .u50812aeaf7767f36b4456e5d8333e20c .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .u50812aeaf7767f36b4456e5d8333e20c .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .u50812aeaf7767f36b4456e5d8333e20c:hover .postTitle { text-decoration: underline!important; } Memoization là gì? LRU cache là gì? .uf8364c9b77631458313795af0e6b92f2 { padding:0px; margin: 0; padding-top:1em!important; padding-bottom:1em!important; width:100%; display: block; font-weight:bold; background-color:#eaeaea; border:0!important; border-left:4px solid #34495E!important; text-decoration:none; } .uf8364c9b77631458313795af0e6b92f2:active, .uf8364c9b77631458313795af0e6b92f2:hover { opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; text-decoration:none; } .uf8364c9b77631458313795af0e6b92f2 { transition: background-color 250ms; webkit-transition: background-color 250ms; opacity: 1; transition: opacity 250ms; webkit-transition: opacity 250ms; } .uf8364c9b77631458313795af0e6b92f2 .ctaText { font-weight:bold; color:inherit; text-decoration:none; font-size: 16px; } .uf8364c9b77631458313795af0e6b92f2 .postTitle { color:#000000; text-decoration: underline!important; font-size: 16px; } .uf8364c9b77631458313795af0e6b92f2:hover .postTitle { text-decoration: underline!important; } Memcached là gì ? Sử dụng memcached để tăng tốc hệ thống server