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