testsuite: Assert that testsuite ways are known
[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 * https://gitlab.haskell.org/ghc/ghc/wikis/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 StgTRecChunk *free_trec_chunks;
158 StgTRecHeader *free_trec_headers;
159 uint32_t transaction_tokens;
160 } // typedef Capability is defined in RtsAPI.h
161 // We never want a Capability to overlap a cache line with anything
162 // else, so round it up to a cache line size:
163 #if !defined(mingw32_HOST_OS)
164 ATTRIBUTE_ALIGNED(64)
165 #endif
166 ;
167
168 #if defined(THREADED_RTS)
169 #define ASSERT_TASK_ID(task) ASSERT(task->id == osThreadId())
170 #else
171 #define ASSERT_TASK_ID(task) /*empty*/
172 #endif
173
174 // These properties should be true when a Task is holding a Capability
175 #define ASSERT_FULL_CAPABILITY_INVARIANTS(cap,task) \
176 ASSERT(cap->running_task != NULL && cap->running_task == task); \
177 ASSERT(task->cap == cap); \
178 ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task)
179
180 // This assert requires cap->lock to be held, so it can't be part of
181 // ASSERT_PARTIAL_CAPABILITY_INVARIANTS()
182 #if defined(THREADED_RTS)
183 #define ASSERT_RETURNING_TASKS(cap,task) \
184 ASSERT(cap->returning_tasks_hd == NULL ? \
185 cap->returning_tasks_tl == NULL && cap->n_returning_tasks == 0 \
186 : 1);
187 #else
188 #define ASSERT_RETURNING_TASKS(cap,task) /* nothing */
189 #endif
190
191 // Sometimes a Task holds a Capability, but the Task is not associated
192 // with that Capability (ie. task->cap != cap). This happens when
193 // (a) a Task holds multiple Capabilities, and (b) when the current
194 // Task is bound, its thread has just blocked, and it may have been
195 // moved to another Capability.
196 #define ASSERT_PARTIAL_CAPABILITY_INVARIANTS(cap,task) \
197 ASSERT(cap->run_queue_hd == END_TSO_QUEUE ? \
198 cap->run_queue_tl == END_TSO_QUEUE && cap->n_run_queue == 0 \
199 : 1); \
200 ASSERT(cap->suspended_ccalls == NULL ? cap->n_suspended_ccalls == 0 : 1); \
201 ASSERT(myTask() == task); \
202 ASSERT_TASK_ID(task);
203
204 #if defined(THREADED_RTS)
205 bool checkSparkCountInvariant (void);
206 #endif
207
208 // Converts a *StgRegTable into a *Capability.
209 //
210 INLINE_HEADER Capability *
211 regTableToCapability (StgRegTable *reg)
212 {
213 return (Capability *)((void *)((unsigned char*)reg - STG_FIELD_OFFSET(Capability,r)));
214 }
215
216 // Initialise the available capabilities.
217 //
218 void initCapabilities (void);
219
220 // Add and initialise more Capabilities
221 //
222 void moreCapabilities (uint32_t from, uint32_t to);
223
224 // Release a capability. This is called by a Task that is exiting
225 // Haskell to make a foreign call, or in various other cases when we
226 // want to relinquish a Capability that we currently hold.
227 //
228 // ASSUMES: cap->running_task is the current Task.
229 //
230 #if defined(THREADED_RTS)
231 void releaseCapability (Capability* cap);
232 void releaseAndWakeupCapability (Capability* cap);
233 void releaseCapability_ (Capability* cap, bool always_wakeup);
234 // assumes cap->lock is held
235 #else
236 // releaseCapability() is empty in non-threaded RTS
237 INLINE_HEADER void releaseCapability (Capability* cap STG_UNUSED) {};
238 INLINE_HEADER void releaseAndWakeupCapability (Capability* cap STG_UNUSED) {};
239 INLINE_HEADER void releaseCapability_ (Capability* cap STG_UNUSED,
240 bool always_wakeup STG_UNUSED) {};
241 #endif
242
243 // declared in includes/rts/Threads.h:
244 // extern Capability MainCapability;
245
246 // declared in includes/rts/Threads.h:
247 // extern uint32_t n_capabilities;
248 // extern uint32_t enabled_capabilities;
249
250 // Array of all the capabilities
251 extern Capability **capabilities;
252
253 //
254 // Types of global synchronisation
255 //
256 typedef enum {
257 SYNC_OTHER,
258 SYNC_GC_SEQ,
259 SYNC_GC_PAR
260 } SyncType;
261
262 //
263 // Details about a global synchronisation
264 //
265 typedef struct {
266 SyncType type; // The kind of synchronisation
267 bool *idle; // Array of size n_capabilities. idle[i] is true
268 // if capability i will be idle during this GC
269 // cycle. Only available when doing GC (when
270 // type is SYNC_GC_*).
271 Task *task; // The Task performing the sync
272 } PendingSync;
273
274 //
275 // Indicates that the RTS wants to synchronise all the Capabilities
276 // for some reason. All Capabilities should stop and return to the
277 // scheduler.
278 //
279 extern PendingSync * volatile pending_sync;
280
281 // Acquires a capability at a return point. If *cap is non-NULL, then
282 // this is taken as a preference for the Capability we wish to
283 // acquire.
284 //
285 // OS threads waiting in this function get priority over those waiting
286 // in waitForCapability().
287 //
288 // On return, *cap is non-NULL, and points to the Capability acquired.
289 //
290 void waitForCapability (Capability **cap/*in/out*/, Task *task);
291
292 EXTERN_INLINE void recordMutableCap (const StgClosure *p, Capability *cap,
293 uint32_t gen);
294
295 EXTERN_INLINE void recordClosureMutated (Capability *cap, StgClosure *p);
296
297 #if defined(THREADED_RTS)
298
299 // Gives up the current capability IFF there is a higher-priority
300 // thread waiting for it. This happens in one of two ways:
301 //
302 // (a) we are passing the capability to another OS thread, so
303 // that it can run a bound Haskell thread, or
304 //
305 // (b) there is an OS thread waiting to return from a foreign call
306 //
307 // On return: *pCap is NULL if the capability was released. The
308 // current task should then re-acquire it using waitForCapability().
309 //
310 bool yieldCapability (Capability** pCap, Task *task, bool gcAllowed);
311
312 // Wakes up a worker thread on just one Capability, used when we
313 // need to service some global event.
314 //
315 void prodOneCapability (void);
316 void prodCapability (Capability *cap, Task *task);
317
318 // Similar to prodOneCapability(), but prods all of them.
319 //
320 void prodAllCapabilities (void);
321
322 // Attempt to gain control of a Capability if it is free.
323 //
324 bool tryGrabCapability (Capability *cap, Task *task);
325
326 // Try to find a spark to run
327 //
328 StgClosure *findSpark (Capability *cap);
329
330 // True if any capabilities have sparks
331 //
332 bool anySparks (void);
333
334 INLINE_HEADER bool emptySparkPoolCap (Capability *cap);
335 INLINE_HEADER uint32_t sparkPoolSizeCap (Capability *cap);
336 INLINE_HEADER void discardSparksCap (Capability *cap);
337
338 #else // !THREADED_RTS
339
340 // Grab a capability. (Only in the non-threaded RTS; in the threaded
341 // RTS one of the waitFor*Capability() functions must be used).
342 //
343 extern void grabCapability (Capability **pCap);
344
345 #endif /* !THREADED_RTS */
346
347 // Shut down all capabilities.
348 //
349 void shutdownCapabilities(Task *task, bool wait_foreign);
350
351 // cause all capabilities to context switch as soon as possible.
352 void contextSwitchAllCapabilities(void);
353 INLINE_HEADER void contextSwitchCapability(Capability *cap);
354
355 // cause all capabilities to stop running Haskell code and return to
356 // the scheduler as soon as possible.
357 void interruptAllCapabilities(void);
358 INLINE_HEADER void interruptCapability(Capability *cap);
359
360 // Free all capabilities
361 void freeCapabilities (void);
362
363 // For the GC:
364 void markCapability (evac_fn evac, void *user, Capability *cap,
365 bool no_mark_sparks USED_IF_THREADS);
366
367 void markCapabilities (evac_fn evac, void *user);
368
369 void traverseSparkQueues (evac_fn evac, void *user);
370
371 /* -----------------------------------------------------------------------------
372 NUMA
373 -------------------------------------------------------------------------- */
374
375 /* Number of logical NUMA nodes */
376 extern uint32_t n_numa_nodes;
377
378 /* Map logical NUMA node to OS node numbers */
379 extern uint32_t numa_map[MAX_NUMA_NODES];
380
381 #define capNoToNumaNode(n) ((n) % n_numa_nodes)
382
383 /* -----------------------------------------------------------------------------
384 Messages
385 -------------------------------------------------------------------------- */
386
387 typedef struct PutMVar_ {
388 StgStablePtr mvar;
389 struct PutMVar_ *link;
390 } PutMVar;
391
392 #if defined(THREADED_RTS)
393
394 INLINE_HEADER bool emptyInbox(Capability *cap);
395
396 #endif // THREADED_RTS
397
398 /* -----------------------------------------------------------------------------
399 * INLINE functions... private below here
400 * -------------------------------------------------------------------------- */
401
402 EXTERN_INLINE void
403 recordMutableCap (const StgClosure *p, Capability *cap, uint32_t gen)
404 {
405 bdescr *bd;
406
407 // We must own this Capability in order to modify its mutable list.
408 // ASSERT(cap->running_task == myTask());
409 // NO: assertion is violated by performPendingThrowTos()
410 bd = cap->mut_lists[gen];
411 if (bd->free >= bd->start + BLOCK_SIZE_W) {
412 bdescr *new_bd;
413 new_bd = allocBlockOnNode_lock(cap->node);
414 new_bd->link = bd;
415 bd = new_bd;
416 cap->mut_lists[gen] = bd;
417 }
418 *bd->free++ = (StgWord)p;
419 }
420
421 EXTERN_INLINE void
422 recordClosureMutated (Capability *cap, StgClosure *p)
423 {
424 bdescr *bd;
425 bd = Bdescr((StgPtr)p);
426 if (bd->gen_no != 0) recordMutableCap(p,cap,bd->gen_no);
427 }
428
429
430 #if defined(THREADED_RTS)
431 INLINE_HEADER bool
432 emptySparkPoolCap (Capability *cap)
433 { return looksEmpty(cap->sparks); }
434
435 INLINE_HEADER uint32_t
436 sparkPoolSizeCap (Capability *cap)
437 { return sparkPoolSize(cap->sparks); }
438
439 INLINE_HEADER void
440 discardSparksCap (Capability *cap)
441 { discardSparks(cap->sparks); }
442 #endif
443
444 INLINE_HEADER void
445 stopCapability (Capability *cap)
446 {
447 // setting HpLim to NULL tries to make the next heap check will
448 // fail, which will cause the thread to return to the scheduler.
449 // It may not work - the thread might be updating HpLim itself
450 // at the same time - so we also have the context_switch/interrupted
451 // flags as a sticky way to tell the thread to stop.
452 cap->r.rHpLim = NULL;
453 }
454
455 INLINE_HEADER void
456 interruptCapability (Capability *cap)
457 {
458 stopCapability(cap);
459 cap->interrupt = 1;
460 }
461
462 INLINE_HEADER void
463 contextSwitchCapability (Capability *cap)
464 {
465 stopCapability(cap);
466 cap->context_switch = 1;
467 }
468
469 #if defined(THREADED_RTS)
470
471 INLINE_HEADER bool emptyInbox(Capability *cap)
472 {
473 return (cap->inbox == (Message*)END_TSO_QUEUE &&
474 cap->putMVars == NULL);
475 }
476
477 #endif
478
479 #include "EndPrivate.h"