rts: Add --internal-counters RTS flag and several counters
[ghc.git] / rts / Messages.c
1 /* ---------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2010
4 *
5 * Inter-Capability message passing
6 *
7 * --------------------------------------------------------------------------*/
8
9 #include "Rts.h"
10 #include "Messages.h"
11 #include "Trace.h"
12 #include "Capability.h"
13 #include "Schedule.h"
14 #include "Threads.h"
15 #include "RaiseAsync.h"
16 #include "sm/Storage.h"
17
18 /* ----------------------------------------------------------------------------
19 Send a message to another Capability
20 ------------------------------------------------------------------------- */
21
22 #if defined(THREADED_RTS)
23
24 void sendMessage(Capability *from_cap, Capability *to_cap, Message *msg)
25 {
26 ACQUIRE_LOCK(&to_cap->lock);
27
28 #if defined(DEBUG)
29 {
30 const StgInfoTable *i = msg->header.info;
31 if (i != &stg_MSG_THROWTO_info &&
32 i != &stg_MSG_BLACKHOLE_info &&
33 i != &stg_MSG_TRY_WAKEUP_info &&
34 i != &stg_IND_info && // can happen if a MSG_BLACKHOLE is revoked
35 i != &stg_WHITEHOLE_info) {
36 barf("sendMessage: %p", i);
37 }
38 }
39 #endif
40
41 msg->link = to_cap->inbox;
42 to_cap->inbox = msg;
43
44 recordClosureMutated(from_cap,(StgClosure*)msg);
45
46 if (to_cap->running_task == NULL) {
47 to_cap->running_task = myTask();
48 // precond for releaseCapability_()
49 releaseCapability_(to_cap,false);
50 } else {
51 interruptCapability(to_cap);
52 }
53
54 RELEASE_LOCK(&to_cap->lock);
55 }
56
57 #endif /* THREADED_RTS */
58
59 /* ----------------------------------------------------------------------------
60 Handle a message
61 ------------------------------------------------------------------------- */
62
63 #if defined(THREADED_RTS)
64
65 void
66 executeMessage (Capability *cap, Message *m)
67 {
68 const StgInfoTable *i;
69
70 loop:
71 write_barrier(); // allow m->header to be modified by another thread
72 i = m->header.info;
73 if (i == &stg_MSG_TRY_WAKEUP_info)
74 {
75 StgTSO *tso = ((MessageWakeup *)m)->tso;
76 debugTraceCap(DEBUG_sched, cap, "message: try wakeup thread %ld",
77 (W_)tso->id);
78 tryWakeupThread(cap, tso);
79 }
80 else if (i == &stg_MSG_THROWTO_info)
81 {
82 MessageThrowTo *t = (MessageThrowTo *)m;
83 uint32_t r;
84 const StgInfoTable *i;
85
86 i = lockClosure((StgClosure*)m);
87 if (i != &stg_MSG_THROWTO_info) {
88 unlockClosure((StgClosure*)m, i);
89 goto loop;
90 }
91
92 debugTraceCap(DEBUG_sched, cap, "message: throwTo %ld -> %ld",
93 (W_)t->source->id, (W_)t->target->id);
94
95 ASSERT(t->source->why_blocked == BlockedOnMsgThrowTo);
96 ASSERT(t->source->block_info.closure == (StgClosure *)m);
97
98 r = throwToMsg(cap, t);
99
100 switch (r) {
101 case THROWTO_SUCCESS: {
102 // this message is done
103 StgTSO *source = t->source;
104 doneWithMsgThrowTo(t);
105 tryWakeupThread(cap, source);
106 break;
107 }
108 case THROWTO_BLOCKED:
109 // unlock the message
110 unlockClosure((StgClosure*)m, &stg_MSG_THROWTO_info);
111 break;
112 }
113 }
114 else if (i == &stg_MSG_BLACKHOLE_info)
115 {
116 uint32_t r;
117 MessageBlackHole *b = (MessageBlackHole*)m;
118
119 r = messageBlackHole(cap, b);
120 if (r == 0) {
121 tryWakeupThread(cap, b->tso);
122 }
123 return;
124 }
125 else if (i == &stg_IND_info || i == &stg_MSG_NULL_info)
126 {
127 // message was revoked
128 return;
129 }
130 else if (i == &stg_WHITEHOLE_info)
131 {
132 #if defined(PROF_SPIN)
133 ++whitehole_executeMessage_spin;
134 #endif
135 goto loop;
136 }
137 else
138 {
139 barf("executeMessage: %p", i);
140 }
141 }
142
143 #endif
144
145 /* ----------------------------------------------------------------------------
146 Handle a MSG_BLACKHOLE message
147
148 This is called from two places: either we just entered a BLACKHOLE
149 (stg_BLACKHOLE_info), or we received a MSG_BLACKHOLE in our
150 cap->inbox.
151
152 We need to establish whether the BLACKHOLE belongs to
153 this Capability, and
154 - if so, arrange to block the current thread on it
155 - otherwise, forward the message to the right place
156
157 Returns:
158 - 0 if the blocked thread can be woken up by the caller
159 - 1 if the thread is still blocked, and we promise to send a MSG_TRY_WAKEUP
160 at some point in the future.
161
162 ------------------------------------------------------------------------- */
163
164 uint32_t messageBlackHole(Capability *cap, MessageBlackHole *msg)
165 {
166 const StgInfoTable *info;
167 StgClosure *p;
168 StgBlockingQueue *bq;
169 StgClosure *bh = UNTAG_CLOSURE(msg->bh);
170 StgTSO *owner;
171
172 debugTraceCap(DEBUG_sched, cap, "message: thread %d blocking on "
173 "blackhole %p", (W_)msg->tso->id, msg->bh);
174
175 info = bh->header.info;
176
177 // If we got this message in our inbox, it might be that the
178 // BLACKHOLE has already been updated, and GC has shorted out the
179 // indirection, so the pointer no longer points to a BLACKHOLE at
180 // all.
181 if (info != &stg_BLACKHOLE_info &&
182 info != &stg_CAF_BLACKHOLE_info &&
183 info != &__stg_EAGER_BLACKHOLE_info &&
184 info != &stg_WHITEHOLE_info) {
185 // if it is a WHITEHOLE, then a thread is in the process of
186 // trying to BLACKHOLE it. But we know that it was once a
187 // BLACKHOLE, so there is at least a valid pointer in the
188 // payload, so we can carry on.
189 return 0;
190 }
191
192 // The blackhole must indirect to a TSO, a BLOCKING_QUEUE, an IND,
193 // or a value.
194 loop:
195 // NB. VOLATILE_LOAD(), because otherwise gcc hoists the load
196 // and turns this into an infinite loop.
197 p = UNTAG_CLOSURE((StgClosure*)VOLATILE_LOAD(&((StgInd*)bh)->indirectee));
198 info = p->header.info;
199
200 if (info == &stg_IND_info)
201 {
202 // This could happen, if e.g. we got a BLOCKING_QUEUE that has
203 // just been replaced with an IND by another thread in
204 // updateThunk(). In which case, if we read the indirectee
205 // again we should get the value.
206 goto loop;
207 }
208
209 else if (info == &stg_TSO_info)
210 {
211 owner = (StgTSO*)p;
212
213 #if defined(THREADED_RTS)
214 if (owner->cap != cap) {
215 sendMessage(cap, owner->cap, (Message*)msg);
216 debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d",
217 owner->cap->no);
218 return 1;
219 }
220 #endif
221 // owner is the owner of the BLACKHOLE, and resides on this
222 // Capability. msg->tso is the first thread to block on this
223 // BLACKHOLE, so we first create a BLOCKING_QUEUE object.
224
225 bq = (StgBlockingQueue*)allocate(cap, sizeofW(StgBlockingQueue));
226
227 // initialise the BLOCKING_QUEUE object
228 SET_HDR(bq, &stg_BLOCKING_QUEUE_DIRTY_info, CCS_SYSTEM);
229 bq->bh = bh;
230 bq->queue = msg;
231 bq->owner = owner;
232
233 msg->link = (MessageBlackHole*)END_TSO_QUEUE;
234
235 // All BLOCKING_QUEUES are linked in a list on owner->bq, so
236 // that we can search through them in the event that there is
237 // a collision to update a BLACKHOLE and a BLOCKING_QUEUE
238 // becomes orphaned (see updateThunk()).
239 bq->link = owner->bq;
240 owner->bq = bq;
241 dirty_TSO(cap, owner); // we modified owner->bq
242
243 // If the owner of the blackhole is currently runnable, then
244 // bump it to the front of the run queue. This gives the
245 // blocked-on thread a little boost which should help unblock
246 // this thread, and may avoid a pile-up of other threads
247 // becoming blocked on the same BLACKHOLE (#3838).
248 //
249 // NB. we check to make sure that the owner is not the same as
250 // the current thread, since in that case it will not be on
251 // the run queue.
252 if (owner->why_blocked == NotBlocked && owner->id != msg->tso->id) {
253 promoteInRunQueue(cap, owner);
254 }
255
256 // point to the BLOCKING_QUEUE from the BLACKHOLE
257 write_barrier(); // make the BQ visible
258 ((StgInd*)bh)->indirectee = (StgClosure *)bq;
259 recordClosureMutated(cap,bh); // bh was mutated
260
261 debugTraceCap(DEBUG_sched, cap, "thread %d blocked on thread %d",
262 (W_)msg->tso->id, (W_)owner->id);
263
264 return 1; // blocked
265 }
266 else if (info == &stg_BLOCKING_QUEUE_CLEAN_info ||
267 info == &stg_BLOCKING_QUEUE_DIRTY_info)
268 {
269 StgBlockingQueue *bq = (StgBlockingQueue *)p;
270
271 ASSERT(bq->bh == bh);
272
273 owner = bq->owner;
274
275 ASSERT(owner != END_TSO_QUEUE);
276
277 #if defined(THREADED_RTS)
278 if (owner->cap != cap) {
279 sendMessage(cap, owner->cap, (Message*)msg);
280 debugTraceCap(DEBUG_sched, cap, "forwarding message to cap %d",
281 owner->cap->no);
282 return 1;
283 }
284 #endif
285
286 msg->link = bq->queue;
287 bq->queue = msg;
288 recordClosureMutated(cap,(StgClosure*)msg);
289
290 if (info == &stg_BLOCKING_QUEUE_CLEAN_info) {
291 bq->header.info = &stg_BLOCKING_QUEUE_DIRTY_info;
292 recordClosureMutated(cap,(StgClosure*)bq);
293 }
294
295 debugTraceCap(DEBUG_sched, cap,
296 "thread %d blocked on existing BLOCKING_QUEUE "
297 "owned by thread %d",
298 (W_)msg->tso->id, (W_)owner->id);
299
300 // See above, #3838
301 if (owner->why_blocked == NotBlocked && owner->id != msg->tso->id) {
302 promoteInRunQueue(cap, owner);
303 }
304
305 return 1; // blocked
306 }
307
308 return 0; // not blocked
309 }
310
311 // A shorter version of messageBlackHole(), that just returns the
312 // owner (or NULL if the owner cannot be found, because the blackhole
313 // has been updated in the meantime).
314
315 StgTSO * blackHoleOwner (StgClosure *bh)
316 {
317 const StgInfoTable *info;
318 StgClosure *p;
319
320 info = bh->header.info;
321
322 if (info != &stg_BLACKHOLE_info &&
323 info != &stg_CAF_BLACKHOLE_info &&
324 info != &__stg_EAGER_BLACKHOLE_info &&
325 info != &stg_WHITEHOLE_info) {
326 return NULL;
327 }
328
329 // The blackhole must indirect to a TSO, a BLOCKING_QUEUE, an IND,
330 // or a value.
331 loop:
332 // NB. VOLATILE_LOAD(), because otherwise gcc hoists the load
333 // and turns this into an infinite loop.
334 p = UNTAG_CLOSURE((StgClosure*)VOLATILE_LOAD(&((StgInd*)bh)->indirectee));
335 info = p->header.info;
336
337 if (info == &stg_IND_info) goto loop;
338
339 else if (info == &stg_TSO_info)
340 {
341 return (StgTSO*)p;
342 }
343 else if (info == &stg_BLOCKING_QUEUE_CLEAN_info ||
344 info == &stg_BLOCKING_QUEUE_DIRTY_info)
345 {
346 StgBlockingQueue *bq = (StgBlockingQueue *)p;
347 return bq->owner;
348 }
349
350 return NULL; // not blocked
351 }