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