1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2006
5 * Tidying up a thread when it stops running
7 * ---------------------------------------------------------------------------*/
11 #include "LdvProfile.h"
13 #include "RaiseAsync.h"
17 #include <string.h> // for memmove()
19 /* -----------------------------------------------------------------------------
22 * Code largely pinched from old RTS, then hacked to bits. We also do
23 * lazy black holing here.
25 * -------------------------------------------------------------------------- */
27 struct stack_gap { StgWord gap_size; struct stack_gap *next_gap; };
30 stackSqueeze(StgTSO *tso, StgPtr bottom)
33 rtsBool prev_was_update_frame;
34 StgClosure *updatee = NULL;
35 StgRetInfoTable *info;
36 StgWord current_gap_size;
37 struct stack_gap *gap;
40 // Traverse the stack upwards, replacing adjacent update frames
41 // with a single update frame and a "stack gap". A stack gap
42 // contains two values: the size of the gap, and the distance
43 // to the next gap (or the stack top).
47 ASSERT(frame < bottom);
49 prev_was_update_frame = rtsFalse;
51 gap = (struct stack_gap *) (tso->sp - sizeofW(StgUpdateFrame));
53 while (frame <= bottom) {
55 info = get_ret_itbl((StgClosure *)frame);
56 switch (info->i.type) {
60 StgUpdateFrame *upd = (StgUpdateFrame *)frame;
62 if (prev_was_update_frame) {
65 /* wasn't there something about update squeezing and ticky to be
66 * sorted out? oh yes: we aren't counting each enter properly
67 * in this case. See the log somewhere. KSW 1999-04-21
69 * Check two things: that the two update frames don't point to
70 * the same object, and that the updatee_bypass isn't already an
71 * indirection. Both of these cases only happen when we're in a
72 * block hole-style loop (and there are multiple update frames
73 * on the stack pointing to the same closure), but they can both
74 * screw us up if we don't check.
76 if (upd->updatee != updatee && !closure_IND(upd->updatee)) {
77 UPD_IND_NOLOCK(upd->updatee, updatee);
80 // now mark this update frame as a stack gap. The gap
81 // marker resides in the bottom-most update frame of
82 // the series of adjacent frames, and covers all the
83 // frames in this series.
84 current_gap_size += sizeofW(StgUpdateFrame);
85 ((struct stack_gap *)frame)->gap_size = current_gap_size;
86 ((struct stack_gap *)frame)->next_gap = gap;
88 frame += sizeofW(StgUpdateFrame);
92 // single update frame, or the topmost update frame in a series
94 prev_was_update_frame = rtsTrue;
95 updatee = upd->updatee;
96 frame += sizeofW(StgUpdateFrame);
102 prev_was_update_frame = rtsFalse;
104 // we're not in a gap... check whether this is the end of a gap
105 // (an update frame can't be the end of a gap).
106 if (current_gap_size != 0) {
107 gap = (struct stack_gap *) (frame - sizeofW(StgUpdateFrame));
109 current_gap_size = 0;
111 frame += stack_frame_sizeW((StgClosure *)frame);
116 if (current_gap_size != 0) {
117 gap = (struct stack_gap *) (frame - sizeofW(StgUpdateFrame));
120 // Now we have a stack with gaps in it, and we have to walk down
121 // shoving the stack up to fill in the gaps. A diagram might
125 // | ********* | <- sp
129 // | stack_gap | <- gap | chunk_size
131 // | ......... | <- gap_end v
137 // 'sp' points the the current top-of-stack
138 // 'gap' points to the stack_gap structure inside the gap
139 // ***** indicates real stack data
140 // ..... indicates gap
141 // <empty> indicates unused
145 void *gap_start, *next_gap_start, *gap_end;
148 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
151 while ((StgPtr)gap > tso->sp) {
153 // we're working in *bytes* now...
154 gap_start = next_gap_start;
155 gap_end = (void*) ((unsigned char*)gap_start - gap->gap_size * sizeof(W_));
158 next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
160 chunk_size = (unsigned char*)gap_end - (unsigned char*)next_gap_start;
162 memmove(sp, next_gap_start, chunk_size);
165 tso->sp = (StgPtr)sp;
169 /* -----------------------------------------------------------------------------
172 * We have to prepare for GC - this means doing lazy black holing
173 * here. We also take the opportunity to do stack squeezing if it's
175 * -------------------------------------------------------------------------- */
177 threadPaused(Capability *cap, StgTSO *tso)
180 StgRetInfoTable *info;
181 const StgInfoTable *bh_info;
182 const StgInfoTable *cur_bh_info USED_IF_THREADS;
185 nat words_to_squeeze = 0;
187 nat weight_pending = 0;
188 rtsBool prev_was_update_frame = rtsFalse;
190 // Check to see whether we have threads waiting to raise
191 // exceptions, and we're not blocking exceptions, or are blocked
192 // interruptibly. This is important; if a thread is running with
193 // TSO_BLOCKEX and becomes blocked interruptibly, this is the only
194 // place we ensure that the blocked_exceptions get a chance.
195 maybePerformBlockedException (cap, tso);
196 if (tso->what_next == ThreadKilled) { return; }
198 stack_end = &tso->stack[tso->stack_size];
200 frame = (StgClosure *)tso->sp;
203 // If we've already marked this frame, then stop here.
204 if (frame->header.info == (StgInfoTable *)&stg_marked_upd_frame_info) {
205 if (prev_was_update_frame) {
206 words_to_squeeze += sizeofW(StgUpdateFrame);
207 weight += weight_pending;
213 info = get_ret_itbl(frame);
215 switch (info->i.type) {
219 SET_INFO(frame, (StgInfoTable *)&stg_marked_upd_frame_info);
221 bh = ((StgUpdateFrame *)frame)->updatee;
222 bh_info = bh->header.info;
227 if (closure_flags[INFO_PTR_TO_STRUCT(bh_info)->type] & _IND
228 || bh_info == &stg_BLACKHOLE_info) {
229 debugTrace(DEBUG_squeeze,
230 "suspending duplicate work: %ld words of stack",
231 (long)((StgPtr)frame - tso->sp));
233 // If this closure is already an indirection, then
234 // suspend the computation up to this point:
235 suspendComputation(cap,tso,(StgPtr)frame);
237 // Now drop the update frame, and arrange to return
238 // the value to the frame underneath:
239 tso->sp = (StgPtr)frame + sizeofW(StgUpdateFrame) - 2;
240 tso->sp[1] = (StgWord)bh;
241 tso->sp[0] = (W_)&stg_enter_info;
243 // And continue with threadPaused; there might be
244 // yet more computation to suspend.
245 threadPaused(cap,tso);
249 if (bh->header.info != &stg_CAF_BLACKHOLE_info) {
250 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
251 debugBelch("Unexpected lazy BHing required at 0x%04lx\n",(long)bh);
253 // zero out the slop so that the sanity checker can tell
254 // where the next closure is.
258 // We pretend that bh is now dead.
259 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
263 cur_bh_info = (const StgInfoTable *)
264 cas((StgVolatilePtr)&bh->header.info,
266 (StgWord)&stg_BLACKHOLE_info);
268 if (cur_bh_info != bh_info) {
269 bh_info = cur_bh_info;
273 SET_INFO(bh,&stg_BLACKHOLE_info);
276 // We pretend that bh has just been created.
277 LDV_RECORD_CREATE(bh);
280 frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
281 if (prev_was_update_frame) {
282 words_to_squeeze += sizeofW(StgUpdateFrame);
283 weight += weight_pending;
286 prev_was_update_frame = rtsTrue;
292 // normal stack frames; do nothing except advance the pointer
295 nat frame_size = stack_frame_sizeW(frame);
296 weight_pending += frame_size;
297 frame = (StgClosure *)((StgPtr)frame + frame_size);
298 prev_was_update_frame = rtsFalse;
304 debugTrace(DEBUG_squeeze,
305 "words_to_squeeze: %d, weight: %d, squeeze: %s",
306 words_to_squeeze, weight,
307 weight < words_to_squeeze ? "YES" : "NO");
309 // Should we squeeze or not? Arbitrary heuristic: we squeeze if
310 // the number of words we have to shift down is less than the
311 // number of stack words we squeeze away by doing so.
312 if (RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue &&
313 ((weight <= 5 && words_to_squeeze > 0) || weight < words_to_squeeze)) {
314 stackSqueeze(tso, (StgPtr)frame);