remove empty dir
[ghc-hetmet.git] / docs / comm / the-beast / optimistic.html
1 <h2> Architectural stuff </h2>
2
3 New fields in the TSO:
4 <ul>
5 <li> New global speculation-depth register; always counts the number of specuation frames
6 on the stack; incremented when 
7 starting speculation, decremented when finishing.
8 <li> Profiling stuff
9 </ul>
10
11
12 <h2> Speculation frames </h2>
13
14 The info table for a speculation frame points to the static spec-depth configuration
15 for that speculation point. (Points to, because the config is mutable, and the info
16 table has to be adjacent to the (immutable) code.)
17
18  
19
20 <h2> Abortion</h2>
21
22 Abortion is modelled by a special asynchronous exception ThreadAbort.
23
24 <ul>
25 <li> In the scheduler, if a thread returns with ThreadBlocked, and non-zero SpecDepth, send it
26 an asynchronous exception.
27
28 <li> In the implementation of the <tt>catch#</tt> primop, raise an asynchonous exception if 
29 SpecDepth is nonzero.
30
31 <li> Timeout, administered by scheduler.  Current story: abort if a speculation frame lasts from
32 one minor GC to the next.  We detect this by seeing if there's a profiling frame on the stack --- a 
33 profiling frame is added at a minor GC in place of a speculation frame (see Online Profiling).
34 </ul>
35
36
37 When tearing frames off the stack, we start a new chunk at every speculation frame, as well as every
38 update frame.  We proceed down to the deepest speculation frame.  
39 <p>
40 The <tt>AP_STACK</tt> closure built for a speculation frame must be careful <em>not</em> to enter the 
41 next <tt>AP_STACK</tt> closure up, because that would re-enter a possible loop.
42 <p>
43 Delivering an asynch exception to a thread that is speculating.  Invariant: there can be no catch frames
44 inside speculation (we abort in <tt>catch#</tt> when speculating.  So the asynch exception just 
45 tears off frames in the standard way until it gets to a catch frame, just as it would usually do.
46 <p>
47 Abortion can punish one or more of the speculation frames by decrementing their static config variables.
48
49 <h3>Synchronous exceptions</h3>
50  
51 Synchronous exceptions are treated similarly as before.  The stack is discarded up to an update frame; the
52 thunk to be updated is overwritten with "raise x", and the process continues.  Until a catch frame.
53 <p>
54 When we find a spec frame, we allocate a "raise x" object, and resume execution with the return address
55 in the spec frame.  In that way the spec frame is like a catch frame; it stops the unwinding process.
56 <p>
57 It's essential that every hard failure is caught, else speculation is unsafe.  In particular, divide by zero
58 is hard to catch using OS support, so we test explicitly in library code.  You can shoot yourself in the foot 
59 by writing <tt>x `div#` 0</tt>, side-stepping the test.
60
61
62 <h3> Online profiling </h3>
63
64 Sampling can be more frequent than minor GC (by jiggling the end-of-block code) but cannot
65 be less frequent, because GC doesn't expect to see profiling frames.