rts: print incorrect prev_what_next
[ghc.git] / rts / Capability.h
1 /* ---------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2001-2006
4 *
5 * Capabilities
6 *
7 * For details on the high-level design, see
8 * http://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Scheduler
9 *
10 * A Capability holds all the state an OS thread/task needs to run
11 * Haskell code: its STG registers, a pointer to its TSO, a nursery
12 * etc. During STG execution, a pointer to the Capabilitity is kept in
13 * a register (BaseReg).
14 *
15 * Only in a THREADED_RTS build will there be multiple capabilities,
16 * in the non-threaded RTS there is one global capability, called
17 * MainCapability.
18 *
19 * --------------------------------------------------------------------------*/
20
21 #ifndef CAPABILITY_H
22 #define CAPABILITY_H
23
24 #include "sm/GC.h" // for evac_fn
25 #include "Task.h"
26 #include "Sparks.h"
27
28 #include "BeginPrivate.h"
29
30 struct Capability_ {
31 // State required by the STG virtual machine when running Haskell
32 // code. During STG execution, the BaseReg register always points
33 // to the StgRegTable of the current Capability (&cap->r).
34 StgFunTable f;
35 StgRegTable r;
36
37 uint32_t no; // capability number.
38
39 // The NUMA node on which this capability resides. This is used to allocate
40 // node-local memory in allocate().
41 //
42 // Note: this is always equal to cap->no % n_numa_nodes.
43 // The reason we slice it this way is that if we add or remove capabilities
44 // via setNumCapabilities(), then we keep the number of capabilities on each
45 // NUMA node balanced.
46 uint32_t node;
47
48 // The Task currently holding this Capability. This task has
49 // exclusive access to the contents of this Capability (apart from
50 // returning_tasks_hd/returning_tasks_tl).
51 // Locks required: cap->lock.
52 Task *running_task;
53
54 // true if this Capability is running Haskell code, used for
55 // catching unsafe call-ins.
56 bool in_haskell;
57
58 // Has there been any activity on this Capability since the last GC?
59 uint32_t idle;
60
61 bool disabled;
62
63 // The run queue. The Task owning this Capability has exclusive
64 // access to its run queue, so can wake up threads without
65 // taking a lock, and the common path through the scheduler is
66 // also lock-free.
67 StgTSO *run_queue_hd;
68 StgTSO *run_queue_tl;
69 uint32_t n_run_queue;
70
71 // Tasks currently making safe foreign calls. Doubly-linked.
72 // When returning, a task first acquires the Capability before
73 // removing itself from this list, so that the GC can find all
74 // the suspended TSOs easily. Hence, when migrating a Task from
75 // the returning_tasks list, we must also migrate its entry from
76 // this list.
77 InCall *suspended_ccalls;
78 uint32_t n_suspended_ccalls;
79
80 // One mutable list per generation, so we don't need to take any
81 // locks when updating an old-generation thunk. This also lets us
82 // keep track of which closures this CPU has been mutating, so we
83 // can traverse them using the right thread during GC and avoid
84 // unnecessarily moving the data from one cache to another.
85 bdescr **mut_lists;
86 bdescr **saved_mut_lists; // tmp use during GC
87
88 // block for allocating pinned objects into
89 bdescr *pinned_object_block;
90 // full pinned object blocks allocated since the last GC
91 bdescr *pinned_object_blocks;
92
93 // per-capability weak pointer list associated with nursery (older
94 // lists stored in generation object)
95 StgWeak *weak_ptr_list_hd;
96 StgWeak *weak_ptr_list_tl;
97
98 // Context switch flag. When non-zero, this means: stop running
99 // Haskell code, and switch threads.
100 int context_switch;
101
102 // Interrupt flag. Like the context_switch flag, this also
103 // indicates that we should stop running Haskell code, but we do
104 // *not* switch threads. This is used to stop a Capability in
105 // order to do GC, for example.
106 //
107 // The interrupt flag is always reset before we start running
108 // Haskell code, unlike the context_switch flag which is only
109 // reset after we have executed the context switch.
110 int interrupt;
111
112 // Total words allocated by this cap since rts start
113 // See [Note allocation accounting] in Storage.c
114 W_ total_allocated;
115
116 #if defined(THREADED_RTS)
117 // Worker Tasks waiting in the wings. Singly-linked.
118 Task *spare_workers;
119 uint32_t n_spare_workers; // count of above
120
121 // This lock protects:
122 // running_task
123 // returning_tasks_{hd,tl}
124 // wakeup_queue
125 // inbox
126 // putMVars
127 Mutex lock;
128
129 // Tasks waiting to return from a foreign call, or waiting to make
130 // a new call-in using this Capability (NULL if empty).
131 // NB. this field needs to be modified by tasks other than the
132 // running_task, so it requires cap->lock to modify. A task can
133 // check whether it is NULL without taking the lock, however.
134 Task *returning_tasks_hd; // Singly-linked, with head/tail
135 Task *returning_tasks_tl;
136 uint32_t n_returning_tasks;
137
138 // Messages, or END_TSO_QUEUE.
139 // Locks required: cap->lock
140 Message *inbox;
141
142 // putMVars are really messages, but they're allocated with malloc() so they
143 // can't go on the inbox queue: the GC would get confused.
144 struct PutMVar_ *putMVars;
145
146 SparkPool *sparks;
147
148 // Stats on spark creation/conversion
149 SparkCounters spark_stats;
150 #if !defined(mingw32_HOST_OS)
151 // IO manager for this cap
152 int io_manager_control_wr_fd;
153 #endif
154 #endif
155
156 // Per-capability STM-related data
157 StgTVarWatchQueue *free_tvar_watch_queues;
158 StgInvariantCheckQueue *free_invariant_check_queues;
159 StgTRecChunk *free_trec_chunks;
160 StgTRecHeader *free_trec_headers;
161 uint32_t transaction_tokens;
162 } // typedef Capability is defined in RtsAPI.h
163 // We never want a Capability to overlap a cache line with anything
164 // else, so round it up to a cache line size:
165 #ifndef mingw32_HOST_OS
166 ATTRIBUTE_ALIGNED(64)
167 #endif
168 ;
169
170 #if defined(THREADED_RTS)
171 #define ASSERT_TASK_ID(task) ASSERT(task->id == osThreadId())
172 #else
173 #define ASSERT_TASK_ID(task) /*empty*/
174 #endif
175
176 // These properties should be true when a Task is holding a Capability
177 #define ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task) \
178 ASSERT(cap->running_task != NULL && cap->running_task == task); \
179 ASSERT(task->cap == cap); \
180 ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task)
181
182 // This assert requires cap->lock to be held, so it can't be part of
183 // ASSERT_PARTIAL_CAPABILITY_INVARIANTS()
184 #if defined(THREADED_RTS)
185 #define ASSERT_RETURNING_TASKS(cap,task) \
186 ASSERT(cap->returning_tasks_hd == NULL ? \
187 cap->returning_tasks_tl == NULL && cap->n_returning_tasks == 0 \
188 : 1);
189 #else
190 #define ASSERT_RETURNING_TASKS(cap,task) /* nothing */
191 #endif
192
193 // Sometimes a Task holds a Capability, but the Task is not associated
194 // with that Capability (ie. task->cap != cap). This happens when
195 // (a) a Task holds multiple Capabilities, and (b) when the current
196 // Task is bound, its thread has just blocked, and it may have been
197 // moved to another Capability.
198 #define ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task) \
199 ASSERT(cap->run_queue_hd == END_TSO_QUEUE ? \
200 cap->run_queue_tl == END_TSO_QUEUE && cap->n_run_queue == 0 \
201 : 1); \
202 ASSERT(cap->suspended_ccalls == NULL ? cap->n_suspended_ccalls == 0 : 1); \
203 ASSERT(myTask() == task); \
204 ASSERT_TASK_ID(task);
205
206 #if defined(THREADED_RTS)
207 bool checkSparkCountInvariant (void);
208 #endif
209
210 // Converts a *StgRegTable into a *Capability.
211 //
212 INLINE_HEADER Capability *
213 regTableToCapability (StgRegTable *reg)
214 {
215 return (Capability *)((void *)((unsigned char*)reg - STG_FIELD_OFFSET(Capability,r)));
216 }
217
218 // Initialise the available capabilities.
219 //
220 void initCapabilities (void);
221
222 // Add and initialise more Capabilities
223 //
224 void moreCapabilities (uint32_t from, uint32_t to);
225
226 // Release a capability. This is called by a Task that is exiting
227 // Haskell to make a foreign call, or in various other cases when we
228 // want to relinquish a Capability that we currently hold.
229 //
230 // ASSUMES: cap->running_task is the current Task.
231 //
232 #if defined(THREADED_RTS)
233 void releaseCapability (Capability* cap);
234 void releaseAndWakeupCapability (Capability* cap);
235 void releaseCapability_ (Capability* cap, bool always_wakeup);
236 // assumes cap->lock is held
237 #else
238 // releaseCapability() is empty in non-threaded RTS
239 INLINE_HEADER void releaseCapability (Capability* cap STG_UNUSED) {};
240 INLINE_HEADER void releaseAndWakeupCapability (Capability* cap STG_UNUSED) {};
241 INLINE_HEADER void releaseCapability_ (Capability* cap STG_UNUSED,
242 bool always_wakeup STG_UNUSED) {};
243 #endif
244
245 // declared in includes/rts/Threads.h:
246 // extern Capability MainCapability;
247
248 // declared in includes/rts/Threads.h:
249 // extern uint32_t n_capabilities;
250 // extern uint32_t enabled_capabilities;
251
252 // Array of all the capabilities
253 extern Capability **capabilities;
254
255 //
256 // Types of global synchronisation
257 //
258 typedef enum {
259 SYNC_OTHER,
260 SYNC_GC_SEQ,
261 SYNC_GC_PAR
262 } SyncType;
263
264 //
265 // Details about a global synchronisation
266 //
267 typedef struct {
268 SyncType type; // The kind of synchronisation
269 bool *idle;
270 Task *task; // The Task performing the sync
271 } PendingSync;
272
273 //
274 // Indicates that the RTS wants to synchronise all the Capabilities
275 // for some reason. All Capabilities should stop and return to the
276 // scheduler.
277 //
278 extern PendingSync * volatile pending_sync;
279
280 // Acquires a capability at a return point. If *cap is non-NULL, then
281 // this is taken as a preference for the Capability we wish to
282 // acquire.
283 //
284 // OS threads waiting in this function get priority over those waiting
285 // in waitForCapability().
286 //
287 // On return, *cap is non-NULL, and points to the Capability acquired.
288 //
289 void waitForCapability (Capability **cap/*in/out*/, Task *task);
290
291 EXTERN_INLINE void recordMutableCap (const StgClosure *p, Capability *cap,
292 uint32_t gen);
293
294 EXTERN_INLINE void recordClosureMutated (Capability *cap, StgClosure *p);
295
296 #if defined(THREADED_RTS)
297
298 // Gives up the current capability IFF there is a higher-priority
299 // thread waiting for it. This happens in one of two ways:
300 //
301 // (a) we are passing the capability to another OS thread, so
302 // that it can run a bound Haskell thread, or
303 //
304 // (b) there is an OS thread waiting to return from a foreign call
305 //
306 // On return: *pCap is NULL if the capability was released. The
307 // current task should then re-acquire it using waitForCapability().
308 //
309 bool yieldCapability (Capability** pCap, Task *task, bool gcAllowed);
310
311 // Wakes up a worker thread on just one Capability, used when we
312 // need to service some global event.
313 //
314 void prodOneCapability (void);
315 void prodCapability (Capability *cap, Task *task);
316
317 // Similar to prodOneCapability(), but prods all of them.
318 //
319 void prodAllCapabilities (void);
320
321 // Attempt to gain control of a Capability if it is free.
322 //
323 bool tryGrabCapability (Capability *cap, Task *task);
324
325 // Try to find a spark to run
326 //
327 StgClosure *findSpark (Capability *cap);
328
329 // True if any capabilities have sparks
330 //
331 bool anySparks (void);
332
333 INLINE_HEADER bool emptySparkPoolCap (Capability *cap);
334 INLINE_HEADER uint32_t sparkPoolSizeCap (Capability *cap);
335 INLINE_HEADER void discardSparksCap (Capability *cap);
336
337 #else // !THREADED_RTS
338
339 // Grab a capability. (Only in the non-threaded RTS; in the threaded
340 // RTS one of the waitFor*Capability() functions must be used).
341 //
342 extern void grabCapability (Capability **pCap);
343
344 #endif /* !THREADED_RTS */
345
346 // Shut down all capabilities.
347 //
348 void shutdownCapabilities(Task *task, bool wait_foreign);
349
350 // cause all capabilities to context switch as soon as possible.
351 void contextSwitchAllCapabilities(void);
352 INLINE_HEADER void contextSwitchCapability(Capability *cap);
353
354 // cause all capabilities to stop running Haskell code and return to
355 // the scheduler as soon as possible.
356 void interruptAllCapabilities(void);
357 INLINE_HEADER void interruptCapability(Capability *cap);
358
359 // Free all capabilities
360 void freeCapabilities (void);
361
362 // For the GC:
363 void markCapability (evac_fn evac, void *user, Capability *cap,
364 bool no_mark_sparks USED_IF_THREADS);
365
366 void markCapabilities (evac_fn evac, void *user);
367
368 void traverseSparkQueues (evac_fn evac, void *user);
369
370 /* -----------------------------------------------------------------------------
371 NUMA
372 -------------------------------------------------------------------------- */
373
374 /* Number of logical NUMA nodes */
375 extern uint32_t n_numa_nodes;
376
377 /* Map logical NUMA node to OS node numbers */
378 extern uint32_t numa_map[MAX_NUMA_NODES];
379
380 #define capNoToNumaNode(n) ((n) % n_numa_nodes)
381
382 /* -----------------------------------------------------------------------------
383 Messages
384 -------------------------------------------------------------------------- */
385
386 typedef struct PutMVar_ {
387 StgStablePtr mvar;
388 struct PutMVar_ *link;
389 } PutMVar;
390
391 #ifdef THREADED_RTS
392
393 INLINE_HEADER bool emptyInbox(Capability *cap);
394
395 #endif // THREADED_RTS
396
397 /* -----------------------------------------------------------------------------
398 * INLINE functions... private below here
399 * -------------------------------------------------------------------------- */
400
401 EXTERN_INLINE void
402 recordMutableCap (const StgClosure *p, Capability *cap, uint32_t gen)
403 {
404 bdescr *bd;
405
406 // We must own this Capability in order to modify its mutable list.
407 // ASSERT(cap->running_task == myTask());
408 // NO: assertion is violated by performPendingThrowTos()
409 bd = cap->mut_lists[gen];
410 if (bd->free >= bd->start + BLOCK_SIZE_W) {
411 bdescr *new_bd;
412 new_bd = allocBlockOnNode_lock(cap->node);
413 new_bd->link = bd;
414 bd = new_bd;
415 cap->mut_lists[gen] = bd;
416 }
417 *bd->free++ = (StgWord)p;
418 }
419
420 EXTERN_INLINE void
421 recordClosureMutated (Capability *cap, StgClosure *p)
422 {
423 bdescr *bd;
424 bd = Bdescr((StgPtr)p);
425 if (bd->gen_no != 0) recordMutableCap(p,cap,bd->gen_no);
426 }
427
428
429 #if defined(THREADED_RTS)
430 INLINE_HEADER bool
431 emptySparkPoolCap (Capability *cap)
432 { return looksEmpty(cap->sparks); }
433
434 INLINE_HEADER uint32_t
435 sparkPoolSizeCap (Capability *cap)
436 { return sparkPoolSize(cap->sparks); }
437
438 INLINE_HEADER void
439 discardSparksCap (Capability *cap)
440 { discardSparks(cap->sparks); }
441 #endif
442
443 INLINE_HEADER void
444 stopCapability (Capability *cap)
445 {
446 // setting HpLim to NULL tries to make the next heap check will
447 // fail, which will cause the thread to return to the scheduler.
448 // It may not work - the thread might be updating HpLim itself
449 // at the same time - so we also have the context_switch/interrupted
450 // flags as a sticky way to tell the thread to stop.
451 cap->r.rHpLim = NULL;
452 }
453
454 INLINE_HEADER void
455 interruptCapability (Capability *cap)
456 {
457 stopCapability(cap);
458 cap->interrupt = 1;
459 }
460
461 INLINE_HEADER void
462 contextSwitchCapability (Capability *cap)
463 {
464 stopCapability(cap);
465 cap->context_switch = 1;
466 }
467
468 #ifdef THREADED_RTS
469
470 INLINE_HEADER bool emptyInbox(Capability *cap)
471 {
472 return (cap->inbox == (Message*)END_TSO_QUEUE &&
473 cap->putMVars == NULL);
474 }
475
476 #endif
477
478 #include "EndPrivate.h"
479
480 #endif /* CAPABILITY_H */