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