<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" 
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:admin="http://webns.net/mvcb/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
	<channel>
<title>My RSS Feed</title><link>http://www.provalid.com/index.html</link><description>Hot News&#x21;</description><dc:language>en</dc:language><dc:creator>kentd@provalid.com</dc:creator><dc:rights>Copyright 2011 Kent Dickey</dc:rights><dc:date>2012-02-09T23:28:43-05:00</dc:date><admin:generatorAgent rdf:resource="http://www.realmacsoftware.com/" />
<admin:errorReportsTo rdf:resource="mailto:kentd@provalid.com" /><sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<sy:updateBase>2000-01-01T12:00+00:00</sy:updateBase>
<lastBuildDate>Fri, 10 Feb 2012 00:05:42 -0500</lastBuildDate><item><title>Ordering</title><dc:creator>kentd@provalid.com</dc:creator><category>None</category><dc:date>2012-02-09T23:28:43-05:00</dc:date><link>http://www.provalid.com/blog/files/edb3ec6847647399a9eb626094967a99-5.html#unique-entry-id-5</link><guid isPermaLink="true">http://www.provalid.com/blog/files/edb3ec6847647399a9eb626094967a99-5.html#unique-entry-id-5</guid><content:encoded><![CDATA[<span style="font-size:15px; ">The CPU pipeline design I just laid out in the previous post was the pinnacle of 1989 RISC design.  Mostly.<br /><br />The simple pipeline has a very nice feature: each instruction appears to execute and complete in the order they pass through the EX stage.  Earlier instructions before the current instruction are complete, and instructions not yet in EX have not done any work yet.  The CPU is pipelined, so many instructions are in various stages of execution.  But to software, the single EX pipeline stage makes it appear as if the instructions are executing in order.  And a much stronger order than that: data accesses are strongly ordered.<br /><br />Most programmers have a simplistic model for a CPU (if they have a model at all), and most expect it to execute instructions one at a time in the order of the assembly language instructions.  This is a pretty reasonable mental model, and most CPU architectures go to great lengths to try to achieve it.<br /><br />Every architecture (excluding Alpha, which fortunately is dead now) makes it appear that the instructions executed on a single CPU core fully execute in order.  Storing to address A and immediately loading from address A in the next instruction always gets the right data.  Loading from address B and then storing different data to address B never causes the earlier load to see the later store data.  With no funny synchronization instructions needed.<br /><br />So basically, all architectures have some EX pipeline stage which they order all instructions through, from a single core&rsquo;s perspective.  So why aren&rsquo;t all CPU architectures strongly-ordered, and what are the other ordering models?<br /><br />Unfortunately, there&rsquo;s terrible nomenclature around CPU ordering, and even worse, technical descriptions tend to get long and very abstract.  To put it simply, there are roughly 3 levels of ordering: Strongly Ordered, Store-Ordered (SPARC called this Total-Store-Ordering, and I like the acronym TSO), and Weakly-Ordered.  What&rsquo;s happening is architectures are breaking certain rules just laid down for the EX stage to try to get higher performance.  So I think it&rsquo;s best to think about what rule is being broken to understand what&rsquo;s going on.<br /><br />Let&rsquo;s look at way to move our simple CPU into the 1990&rsquo;s.  One step was &ldquo;dual-issue&rdquo;, where multiple instructions could execute at once.  This generally doesn&rsquo;t affect ordering, so I&rsquo;ll ignore it.  Another step, which is an ordering issue, was called &ldquo;hit-under-miss&rdquo;.<br /><br />Previously, if a Load instruction was a cache miss, we&rsquo;d stall the pipeline (in the MEM or EX stage) and wait for data to return.  Once data returned, the pipeline would restart, and subsequent instructions would then make it to the EX stage.<br /><br />A very very good way to think about a CPU&rsquo;s performance is to look at stalls.  Stalls are any pipeline stalls where no useful work is done.  With this CPU design, each load and store miss stalls the pipeline for the latency to main memory, which can be a fairly long time.  The idea of Hit-Under-Miss is to allow one miss to be pending without stalling the pipeline.  So, if there are no misses currently pending, when a Load instruction misses the cache, let it go through EX and MEM without a pipeline stall.  Instead, mark its target register as &ldquo;busy&rdquo;, and stall any instruction in EX if it tries to read the busy register.  Hardware on the side (not in the main pipeline) waits for the data from main memory to return.  But instructions after the Load can execute and complete, as long as they don&rsquo;t touch the &ldquo;busy&rdquo; register.  For simplicity, if another Load or Store misses the cache, we&rsquo;ll then stall at EX/MEM.<br /><br />This is a nice speed boost.  Let&rsquo;s assume a code sequence which causes a Cache Miss every 6 cycles (which we&rsquo;ll assume is 6 instructions), and main memory latency is 20 cycles.  And let&rsquo;s assume we can, on average, execute 3 instructions (3 cycles) after a Cache Miss before causing a stall (either because of using the Loaded value, or causing another miss).<br /><br />Without Hit-Under-Miss, executing 6 instructions will take 6 cycles plus 1 miss of 20 clocks, for a total of 26 cycles.  With Hit-Under-Miss, after 6 instructions, there will be a miss.  But we can execute 3 instructions in the shadow of the miss, then stall, waiting for the data to come back.  Then, restart and execute 3 more instructions, then miss again.  Then execute 3 more instructions during the memory access, then stall waiting for the memory to come back.  Repeat this pattern, and you can see a miss is started every 23 cycles.  Effectively, the 3 instructions done while waiting for main memory are &ldquo;free&rdquo;, so 6 instructions take just 23 cycles.  Even with high miss traffic, and only able to execute a few instructions before stalling again, Hit-Under-Miss helps noticeably.  (In CPU design, a 10% improvement is pretty big).<br /><br />Hit-Under-Miss doesn&rsquo;t affect a single-CPU core&rsquo;s view of the ordering of its instructions, but it does change the multiprocessor view of main memory.</span>]]></content:encoded></item><item><title>CPU pipelines in 4 easy stages</title><dc:creator>kentd@provalid.com</dc:creator><category>None</category><dc:date>2012-02-09T23:15:00-05:00</dc:date><link>http://www.provalid.com/blog/files/d1afdf74ba7ea0f4911023ab727fb4a4-4.html#unique-entry-id-4</link><guid isPermaLink="true">http://www.provalid.com/blog/files/d1afdf74ba7ea0f4911023ab727fb4a4-4.html#unique-entry-id-4</guid><content:encoded><![CDATA[<span style="font-size:15px; ">I&rsquo;m not a CPU designer, but I know how CPUs work.  And since I have opinions on everything, I have opinions on CPUs.  I want to get to an opinion on system architecture involving CPUs, but before I can get to that, it&rsquo;s going to take several posts of background first.<br /><br />Imagine every possible CPU design.  I&rsquo;ll wait.  Got it yet?<br /><br />Forget that.  I&rsquo;m going to simplify every possible CPU design to a simple in-order RISC pipeline.  I know, your favorite CPU is much more complex.  But for the point I want to make, all CPUs effectively have an EX pipeline stage, where all the magic happens.<br /><br />The classic RISC pipeline consists of the stages IF, ID, EX, MEM, WB.  IF is Instruction Fetch, ID is Instruction Decode, EX is Execute, MEM is memory access, and WB is WriteBack.  The way I like to view it is EX is the main stage, and the other stages are preparation or cleanup for EX.  ID is the important preparation stage, getting the registers ready and handling bypass results from EX.  And MEM and WB exists so EX can be chock full of work, so writing results to the register file is pushed off so the clock frequency can be as high as possible.<br /><br />I should make a drawing here, but that&rsquo;s too much work for me.  Since a RISC CPU instructions generally have 2 input registers and one output register, picture ID ending with flip-flops holding the two input values.  ID is the stage where the register file is read to get data ready.  Then in EX, an ADD instruction (for example) will take the two input values, add them together, and flop the result at the end of EX.  It also will feed that result back to ID&rsquo;s flip-flop inputs (the bypass path), in case the very next instruction wants to use the result value.<br /><br />The thing to note is EX is where the instructions are effectively ordered.  Instructions in IF or ID haven&rsquo;t occurred yet, and if the instruction in EX needs to take a trap or exception, those instructions currently in IF or ID will be tossed.  And the instruction in WB is committed--nothing the instruction in EX can do will make that instruction disappear.  It&rsquo;s already effectively occurred (even though it&rsquo;s still technically in the pipeline).  (Yes, I realize, a more complex pipeline could have many more stages, and even chase down and kill very late in the pipeline instructions due to certain reasons, and do instructions out-of-order...but that&rsquo;s just complexity, all pipelines eventually have a commit point, let&rsquo;s just call it EX).<br /><br />But ADD instructions are not that interesting: loads and stores are interesting.  Let&rsquo;s assume loads and stores are done in two parts across the EX and MEM stage.  Following the rule that before an instruction can get to the WB stage, it has to be committed, we&rsquo;ll force the rule that any possible exception must be handled in EX.  So TLB misses and TLB protection issues must be resolved in the EX stage.<br /><br />But how are cache misses handled?<br /><br />Let&rsquo;s look at how cache accesses work.  A load or store has two separate yet closely related lookups to do: one is to access the tag array to see if the data is valid in the data cache; the second is to actually access that data.  At the level-1 data cache level, generally the load data access can begin without needing the tag lookup to complete (basically, address bits not in the tag are used to index into the data array).  If the cache is associative, the tag results which arrive around the same time as the data results then select which of the associative data entries to choose.  So, do the TLB lookup in EX, start the tag read and the data array read in EX, but let them complete in MEM.  For stores, let the data write occur in MEM, after the tag read results are known.  So exceptions are handled in EX, but actually handling the returned data is done in EX and MEM.  And this is why the original RISC architectures had a load-to-use delay of one clock cycle:  LOAD followed by an ADD using the loaded data would have a 1-cycle stall.<br /><br />For a single-CPU simple design, cache misses would probably stall in MEM for loads and stores.  If a load or store missed the cache (let&rsquo;s assume write-back write-allocate caching only), the CPU would fetch the cacheline, then do the access again.  Note that the stall is past the commit point--it will be done, it just has to wait on memory.  This keeps the design simple, and achieves another important effect: the CPU appears to be strongly-ordered.<br /></span>]]></content:encoded></item><item><title>Spending &#x24;300 to save &#x24;10</title><dc:creator>kentd@provalid.com</dc:creator><category>None</category><dc:date>2011-11-22T23:12:33-05:00</dc:date><link>http://www.provalid.com/blog/files/070dbc904707644dd88dfb1a83241ecf-3.html#unique-entry-id-3</link><guid isPermaLink="true">http://www.provalid.com/blog/files/070dbc904707644dd88dfb1a83241ecf-3.html#unique-entry-id-3</guid><content:encoded><![CDATA[<span style="font-size:15px; ">I live in New England, where frugalness is prized.  Well, to some extent, at least.  How about: I don&rsquo;t like to waste money needlessly, although I do value my own time.<br /><br />When my HP CP2025DN color Laserjet printer ran out of its initial black toner cartridge, I knew how to get more toner out of it.  Lots of years with HP black&white laser printers taught me there&rsquo;s a LOT of toner still in there when the printer thinks it&rsquo;s &ldquo;out&rdquo;, and the failure mechanism of actually running out of toner is the text on the printouts just gets lighter and lighter.  When that happens, just shake the toner cartridge, and get another hundred pages or so.<br /><br />The printer has 4 cartridges, the usual black, magenta, yellow, and cyan.  Each cost about $100 from HP, a little less if you want refilled no-name cartridges.  I bought 4 new cartridges since I&rsquo;ll need them all eventually, but didn&rsquo;t put the black in until I noticed the output fading.  The color cartridges were more than 2/3rd full when the block toner initially ran &ldquo;out&rdquo;.  So after a few hundred pages, I thought the printouts were looking a little less black, and put in a new black cartridge.<br /><br />And...the printer complained it was still out of toner.  What it had done was to drain the color cartridges, probably using all the ink to approximate black.  And so all the color toner cartridges were empty.<br /><br />So to use the last $10 or so of one black cartridge, I&rsquo;d drained $300 of color cartridges.<br /><br />But I still won&rsquo;t replace the color cartridges--I&rsquo;m using the new black cartridge, but not the new color cartridges.  I still get color that looks fine to me even though the printer says all of the color cartridges are completely empty.  But what do I have to lose now?  Now, I&rsquo;m running a new experiment, to see how long the printer will print color even with &ldquo;empty&rdquo; color cartridges.  And to see how HP will try to rip me off this time.  I guess it could needlessly drain the black toner, but where would the black ink go?<br /><br />Moral of the story: Don&rsquo;t run with scissors?  Never start a land war in Asia?  Well, I&rsquo;m not happy HP feels the need to do something underhanded (who would really want to use expensive color ink to approximate black?) to make me buy more toner.  I didn&rsquo;t mention the fact that the printer by default stops and refuses to print when it thinks it still has 6% toner left, although that setting can be overridden on a menu setting (Toner Low Override: On).<br /><br />I had previously bought a Minolta MC2550 color laser printer, and it did colors great (better than HP), but its small black text was blurry, and since 90% of what I want is to print really small text (2-up duplex text), that was just unacceptable.  Then the Minolta began jamming constantly...so I never did buy new toner cartridges for it, I just traded it in towards the HP instead when Staples ran a deal on trade-ins, after I&rsquo;d run the toner down to nearly empty.  Which was a bad deal for Staples since I would have been happy to pay someone to take the Minolta away.<br /><br />But now I no longer trust HP printers, which is probably going to cost HP more than $300 in the long run.</span>]]></content:encoded></item><item><title>As If</title><dc:creator>kentd@provalid.com</dc:creator><dc:subject>Kent&#x27;s Blog</dc:subject><dc:date>2011-09-23T13:31:23-04:00</dc:date><link>http://www.provalid.com/blog/files/58062a422af584938d58cad0b6a25153-0.html#unique-entry-id-0</link><guid isPermaLink="true">http://www.provalid.com/blog/files/58062a422af584938d58cad0b6a25153-0.html#unique-entry-id-0</guid><content:encoded><![CDATA[<span style="font-size:15px; ">I thought I&rsquo;d start with is the </span><span style="font-size:15px; font-weight:bold; ">&ldquo;As If&rdquo;</span><span style="font-size:15px; "> principle.  Many hardware and software requirements are written implying a particular implementation.  But you don&rsquo;t have to do that.  You just have to implement behavior &ldquo;as if&rdquo; you had done that.<br /><br />A good example of this principle is CPU design and in-order execution.  Every general purpose CPU I can think of maintains the illusion that each instruction executes in order serially, at least for single-threaded code.  Modern CPUs generally do out-of-order execution, involving purposefully executing instructions as early as possible, and then fixing things up so that the instructions appear to have executed in order.  But even before that, CPUs were prefetching or doing &ldquo;hit-under-miss&rdquo; and other things to speed up execution by not doing things in a strictly in-order way.<br /><br />This applies to software as well.  For my Apple 2gs emulator, </span><span style="font-size:15px; "><a href="http://kegs.sourceforge.net/" rel="self">KEGS</a></span><span style="font-size:15px; ">, I wanted to implement accurate emulation of video and sounds.  KEGS emulates a machine released in 1986, so the machine design isn&rsquo;t that complex.  Software running on these old machines would often change the display mode while the CRT was still drawing the screen.  To emulate that properly, one solution is to act like the real hardware: execute one instruction, then call the video update routines and draw a pixel or two, and repeat.  This is very slow--a lot of work is done for every instruction emulated.<br /><br />But KEGS takes a different approach.  Instead of drawing pixels after each instruction, KEGS just keeps tables describing what it should do, and does the video update for the whole screen all at once.  For example, video mode changes do nothing right away, other than fill in a table noting the mode changed on line 62.  When the video refresh routine is called, it then calculates the new pixels as needed on the lines which are now being shown in a different mode than they were last refresh.  The pixel memory is tracked with change bits--any write to video memory also sets a bit indicating an 8-byte region has been written to.  The refresh routines only redraw pixels which changed.  So video memory writes are only slightly slower than other memory writes (they have to also set a changed bit), and yet the refresh routines don&rsquo;t have to look at every byte in video memory to determine what bytes changed.  This isn&rsquo;t a perfect example since I suspect a different approach would be even faster for 2GHz host processors, but this was a great solution when my host machine was only running at 60MHz. This lets KEGS behave almost as if it updates the video display after every instruction, at almost no slow down in emulated CPU instruction speed.<br /><br />Implementing &ldquo;as if&rdquo; sometimes is a trade-off of complexity for speed, and my two examples show that tradeoff.  A lot of CPU complexity goes into making it appear as if instructions are executed in order, and KEGS adds data structures to track delayed video updates.<br /></span>]]></content:encoded></item></channel>
</rss>
