Write Buffer: Difference between revisions

From COMP15212 Wiki
gravatar Yuron [userbureaucratinterface-adminsysopPHRhYmxlIGNsYXNzPSJ0d3BvcHVwIj48dHI+PHRkIGNsYXNzPSJ0d3BvcHVwLWVudHJ5dGl0bGUiPkdyb3Vwczo8L3RkPjx0ZD51c2VyPGJyIC8+YnVyZWF1Y3JhdDxiciAvPmludGVyZmFjZS1hZG1pbjxiciAvPnN5c29wPGJyIC8+PC90ZD48L3RyPjwvdGFibGU+] (talk | contribs)
m (1 revision imported)
pc>Yuron
No edit summary
Line 6: Line 6:
usually happen ‘in their own time’.
usually happen ‘in their own time’.


A <strong>write buffer</strong> is a device where write operations are queued and
A <strong>write buffer</strong> is a device where write operations are queued and performed when there is nothing more urgent to do.
performed when there is nothing more urgent to do.
<blockquote>
<blockquote>
Example: during [[paging]] a ‘dirty’ page may be
Example: during [[paging]] a ‘dirty’ page may be evicted and so need writing to disk, followed by the loading from disk of a replacement page.  Disk operations are slow.  The operating system can keep some pages ‘free’ and choose to load the new page into one of these <em>before</em> performing the write; this can considerably speed up the user’s application.
evicted and so need writing to disk, followed by the loading from
disk of a replacement page.  Disk operations are slow.  The
operating system can keep some pages ‘free’ and choose
to load the new page into one of these <em>before</em> performing the
write; this can considerably speed up the user’s application.
</blockquote>
</blockquote>
The demonstration from the [[paging]] article is reproduced
The demonstration from the [[paging]] article is reproduced here.  (You can find instructions there.)
here.  (You can find instructions there.)


{{#widget:Paging}}
{{#widget:Paging}}


When there’s time we will include a write-buffer option in this too:
When there’s time we will include a write-buffer option in this too: until then you’ll have to be happy with a static diagram – sorry.
until then you’ll have to be happy with a static diagram – sorry.


[[Image:write_buffer.png|link=|alt=write buffer]]
[[Image:write_buffer.png|link=|alt=write buffer]]
Line 35: Line 27:
This is a slightly misleading example.  In practice one wouldn’t <em>copy</em> a page of data first: the write buffering would be done by keeping one or more clean pages in the main store at all times – possibly by writing altered (‘[[Dirty_Bit|dirty]]’) pages back in the background, when the disk would otherwise be idle.  It shows the <em>principle</em> though.
This is a slightly misleading example.  In practice one wouldn’t <em>copy</em> a page of data first: the write buffering would be done by keeping one or more clean pages in the main store at all times – possibly by writing altered (‘[[Dirty_Bit|dirty]]’) pages back in the background, when the disk would otherwise be idle.  It shows the <em>principle</em> though.
</blockquote>
</blockquote>
Write buffering can be applied in various places where reads and
Write buffering can be applied in various places where reads and writes compete for access.  Disk access is a clear example; another is the processor’s access to memory although that is (or is not) managed by hardware.  However, even in that case the operating system may need to be conscious of it: see below.
writes compete for access.  Disk access is a clear example; another is
the processor’s access to memory although that is (or is not) managed
by hardware.  However, even in that case the operating system may need
to be conscious of it: see below.
<blockquote>
<blockquote>
Write buffering is usually controlled on a page-by-page basis with a
Write buffering is usually controlled on a page-by-page basis with a Boolean enable in each page table entry.  (See also [[cacheability]].)
Boolean enable in each page table entry.  (See also [[cacheability]].)
</blockquote>
</blockquote>
----
----


=== Write buffering problems ===
=== Write buffering problems ===
Write buffering allows operations to complete out-of-order – i.e. in
Write buffering allows operations to complete out-of-order – i.e. in a different order from that specified by the programmer.  An issue with write buffering is if a read operation wants to read back write-buffered data.  It is essential to spot this case and <em>forward</em> the (yet unwritten) data to the read to prevent it from reading out-of-date information.
a different order from that specified by the programmer.  An issue
with write buffering is if a read operation wants to read back
write-buffered data.  It is essential to spot this case and <em>forward</em>
the (yet unwritten) data to the read to prevent it from reading
out-of-date information.
<blockquote>
<blockquote>
Note: a write buffer may contain more than one item; all need checking.
Note: a write buffer may contain more than one item; all need checking.
</blockquote>
</blockquote>
With forwarding it is possible to write-buffer ‘simple’ storage safely.  However another issue arises with [[Peripheral devices|I/O devices]] where it may not be obvious that a dependency is present.  In such cases some ordering may need to be enforced.
With forwarding it is possible to write-buffer ‘simple’ storage safely.  However another issue arises with [[Peripheral devices|I/O devices]] where it may not be obvious that a dependency is present.  In such cases some ordering may need to be enforced. Typically it is possible to mark whether write buffering can be performed as an attribute in the [[Memory Management Unit (MMU)|page tables]]; memory pages may be buffered but pages containing memory-mapped I/O may not be.
Typically it is possible to mark whether write buffering can be performed as an attribute in the [[Memory Management Unit (MMU)|page tables]]; memory pages may be buffered but pages containing memory-mapped I/O may not be.


Another means of enforcing ordering, relevant when writing [[Device_Drivers|device drivers]] is the [[Memory_Barrier|memory barrier]].
Another means of enforcing ordering, relevant when writing [[Device_Drivers|device drivers]] is the [[Memory_Barrier|memory barrier]].
----
----
 
{{BookChapter|12.4.2|509-510}}
{{PageGraph}}
{{PageGraph}}
{{Category|IO}}
{{Category|IO}}
{{Category|Memory}}
{{Category|Memory}}

Revision as of 14:51, 4 August 2019

Depends on Memory Management Unit (MMU)Spooling and Buffering

Computers spend a lot of time reading in and writing out data, at various levels of their organisation. Typically, read operations are more urgent because data is wanted now whereas writes can usually happen ‘in their own time’.

A write buffer is a device where write operations are queued and performed when there is nothing more urgent to do.

Example: during paging a ‘dirty’ page may be evicted and so need writing to disk, followed by the loading from disk of a replacement page. Disk operations are slow. The operating system can keep some pages ‘free’ and choose to load the new page into one of these before performing the write; this can considerably speed up the user’s application.

The demonstration from the paging article is reproduced here. (You can find instructions there.)

When there’s time we will include a write-buffer option in this too: until then you’ll have to be happy with a static diagram – sorry.

write buffer

The point being the (urgent) read does not have to wait as long.

This is a slightly misleading example. In practice one wouldn’t copy a page of data first: the write buffering would be done by keeping one or more clean pages in the main store at all times – possibly by writing altered (‘dirty’) pages back in the background, when the disk would otherwise be idle. It shows the principle though.

Write buffering can be applied in various places where reads and writes compete for access. Disk access is a clear example; another is the processor’s access to memory although that is (or is not) managed by hardware. However, even in that case the operating system may need to be conscious of it: see below.

Write buffering is usually controlled on a page-by-page basis with a Boolean enable in each page table entry. (See also cacheability.)


Write buffering problems

Write buffering allows operations to complete out-of-order – i.e. in a different order from that specified by the programmer. An issue with write buffering is if a read operation wants to read back write-buffered data. It is essential to spot this case and forward the (yet unwritten) data to the read to prevent it from reading out-of-date information.

Note: a write buffer may contain more than one item; all need checking.

With forwarding it is possible to write-buffer ‘simple’ storage safely. However another issue arises with I/O devices where it may not be obvious that a dependency is present. In such cases some ordering may need to be enforced. Typically it is possible to mark whether write buffering can be performed as an attribute in the page tables; memory pages may be buffered but pages containing memory-mapped I/O may not be.

Another means of enforcing ordering, relevant when writing device drivers is the memory barrier.


Also refer to: Operating System Concepts, 10th Edition: Chapter 12.4.2, pages 509-510


Articles on IO
Cacheability • Device Drivers • Direct Memory Access (DMA) • IO • Interprocess Communication • Interrupt Controller • Interrupt Service Routines (ISRs) • Interrupts • Libraries • Peripheral devices • Pipes • Queues • Queues Extra • Resources • Shell • Sockets • Spooling and Buffering • Starvation • Streams • System Calls • Thrashing • Timers • Using Peripherals • Virtualisation • Write Buffer