Move GC tracing into a separate trace class
[ghc.git] / rts / Trace.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2008-2009
4 *
5 * Support for fast binary event logging and user-space dtrace probes.
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #ifndef TRACE_H
10 #define TRACE_H
11
12 #include "rts/EventLogFormat.h"
13 #include "Capability.h"
14
15 #if defined(DTRACE)
16 #include "RtsProbes.h"
17 #endif /* defined(DTRACE) */
18
19 #include "BeginPrivate.h"
20
21 // -----------------------------------------------------------------------------
22 // EventLog API
23 // -----------------------------------------------------------------------------
24
25 #if defined(TRACING)
26
27 void initTracing (void);
28 void endTracing (void);
29 void freeTracing (void);
30 void resetTracing (void);
31
32 #endif /* TRACING */
33
34 typedef StgWord32 CapsetID;
35 typedef StgWord16 CapsetType;
36 enum CapsetType { CapsetTypeCustom = CAPSET_TYPE_CUSTOM,
37 CapsetTypeOsProcess = CAPSET_TYPE_OSPROCESS,
38 CapsetTypeClockdomain = CAPSET_TYPE_CLOCKDOMAIN };
39 #define CAPSET_OSPROCESS_DEFAULT 0
40
41 // -----------------------------------------------------------------------------
42 // Message classes
43 // -----------------------------------------------------------------------------
44
45 // debugging flags, set with +RTS -D<something>
46 extern int DEBUG_sched;
47 extern int DEBUG_interp;
48 extern int DEBUG_weak;
49 extern int DEBUG_gccafs;
50 extern int DEBUG_gc;
51 extern int DEBUG_block_alloc;
52 extern int DEBUG_sanity;
53 extern int DEBUG_stable;
54 extern int DEBUG_stm;
55 extern int DEBUG_prof;
56 extern int DEBUG_gran;
57 extern int DEBUG_par;
58 extern int DEBUG_linker;
59 extern int DEBUG_squeeze;
60 extern int DEBUG_hpc;
61 extern int DEBUG_sparks;
62
63 // events
64 extern int TRACE_sched;
65 extern int TRACE_gc;
66 extern int TRACE_spark;
67
68 // -----------------------------------------------------------------------------
69 // Posting events
70 //
71 // We use macros rather than inline functions deliberately. We want
72 // the not-taken case to be as efficient as possible, a simple
73 // test-and-jump, and with inline functions gcc seemed to move some of
74 // the instructions from the branch up before the test.
75 //
76 // -----------------------------------------------------------------------------
77
78 #ifdef DEBUG
79 void traceBegin (const char *str, ...);
80 void traceEnd (void);
81 #endif
82
83 #ifdef TRACING
84
85 /*
86 * Record a scheduler event
87 */
88 #define traceSchedEvent(cap, tag, tso, other) \
89 if (RTS_UNLIKELY(TRACE_sched)) { \
90 traceSchedEvent_(cap, tag, tso, other, 0); \
91 }
92
93 #define traceSchedEvent2(cap, tag, tso, info1, info2) \
94 if (RTS_UNLIKELY(TRACE_sched)) { \
95 traceSchedEvent_(cap, tag, tso, info1, info2); \
96 }
97
98 #define traceSparkEvent(cap, tag, tso, other) \
99 if (RTS_UNLIKELY(TRACE_spark)) { \
100 traceSchedEvent_(cap, tag, tso, other, 0); \
101 }
102
103 void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
104 StgTSO *tso, StgWord info1, StgWord info2);
105
106 /*
107 * Record a GC event
108 */
109 #define traceGcEvent(cap, tag) \
110 if (RTS_UNLIKELY(TRACE_gc)) { \
111 traceGcEvent_(cap, tag); \
112 }
113
114 void traceGcEvent_ (Capability *cap, EventTypeNum tag);
115
116 // variadic macros are C99, and supported by gcc. However, the
117 // ##__VA_ARGS syntax is a gcc extension, which allows the variable
118 // argument list to be empty (see gcc docs for details).
119
120 /*
121 * Emit a trace message on a particular Capability
122 */
123 #define traceCap(class, cap, msg, ...) \
124 if (RTS_UNLIKELY(class)) { \
125 traceCap_(cap, msg, ##__VA_ARGS__); \
126 }
127
128 void traceCap_(Capability *cap, char *msg, ...);
129
130 /*
131 * Emit a trace message
132 */
133 #define trace(class, msg, ...) \
134 if (RTS_UNLIKELY(class)) { \
135 trace_(msg, ##__VA_ARGS__); \
136 }
137
138 void trace_(char *msg, ...);
139
140 /*
141 * A message or event emitted by the program
142 */
143 void traceUserMsg(Capability *cap, char *msg);
144
145 /*
146 * Emit a debug message (only when DEBUG is defined)
147 */
148 #ifdef DEBUG
149 #define debugTrace(class, msg, ...) \
150 if (RTS_UNLIKELY(class)) { \
151 trace_(msg, ##__VA_ARGS__); \
152 }
153 #else
154 #define debugTrace(class, str, ...) /* nothing */
155 #endif
156
157 #ifdef DEBUG
158 #define debugTraceCap(class, cap, msg, ...) \
159 if (RTS_UNLIKELY(class)) { \
160 traceCap_(cap, msg, ##__VA_ARGS__); \
161 }
162 #else
163 #define debugTraceCap(class, cap, str, ...) /* nothing */
164 #endif
165
166 /*
167 * Emit a message/event describing the state of a thread
168 */
169 #define traceThreadStatus(class, tso) \
170 if (RTS_UNLIKELY(class)) { \
171 traceThreadStatus_(tso); \
172 }
173
174 void traceThreadStatus_ (StgTSO *tso);
175
176 void traceEventStartup_ (int n_caps);
177
178 /*
179 * Events for describing capability sets in the eventlog
180 *
181 * Note: unlike other events, these are not conditional on TRACE_sched or
182 * similar because they are not "real" events themselves but provide
183 * information and context for other "real" events. Other events depend on
184 * the capset info events so for simplicity, rather than working out if
185 * they're necessary we always emit them. They should be very low volume.
186 */
187 void traceCapsetModify_ (EventTypeNum tag,
188 CapsetID capset,
189 StgWord32 other);
190
191 void traceOSProcessInfo_ (void);
192
193 void traceSparkCounters_ (Capability *cap,
194 SparkCounters counters,
195 StgWord remaining);
196
197 #else /* !TRACING */
198
199 #define traceSchedEvent(cap, tag, tso, other) /* nothing */
200 #define traceSchedEvent2(cap, tag, tso, other, info) /* nothing */
201 #define traceGcEvent(cap, tag) /* nothing */
202 #define traceSparkEvent(cap, tag, tso, other) /* nothing */
203 #define traceCap(class, cap, msg, ...) /* nothing */
204 #define trace(class, msg, ...) /* nothing */
205 #define debugTrace(class, str, ...) /* nothing */
206 #define debugTraceCap(class, cap, str, ...) /* nothing */
207 #define traceThreadStatus(class, tso) /* nothing */
208 INLINE_HEADER void traceEventStartup_ (int n_caps STG_UNUSED) {};
209 #define traceCapsetModify_(tag, capset, other) /* nothing */
210 #define traceOSProcessInfo_() /* nothing */
211 #define traceSparkCounters_(cap, counters, remaining) /* nothing */
212
213 #endif /* TRACING */
214
215 // If DTRACE is enabled, but neither DEBUG nor TRACING, we need a C land
216 // wrapper for the user-msg probe (as we can't expand that in PrimOps.cmm)
217 //
218 #if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)
219
220 void dtraceUserMsgWrapper(Capability *cap, char *msg);
221
222 #endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */
223
224 // -----------------------------------------------------------------------------
225 // Aliases for static dtrace probes if dtrace is available
226 // -----------------------------------------------------------------------------
227
228 #if defined(DTRACE)
229
230 #define dtraceCreateThread(cap, tid) \
231 HASKELLEVENT_CREATE_THREAD(cap, tid)
232 #define dtraceRunThread(cap, tid) \
233 HASKELLEVENT_RUN_THREAD(cap, tid)
234 #define dtraceStopThread(cap, tid, status, info) \
235 HASKELLEVENT_STOP_THREAD(cap, tid, status, info)
236 #define dtraceThreadRunnable(cap, tid) \
237 HASKELLEVENT_THREAD_RUNNABLE(cap, tid)
238 #define dtraceMigrateThread(cap, tid, new_cap) \
239 HASKELLEVENT_MIGRATE_THREAD(cap, tid, new_cap)
240 #define dtraceRunSpark(cap, tid) \
241 HASKELLEVENT_RUN_SPARK(cap, tid)
242 #define dtraceStealSpark(cap, tid, victim_cap) \
243 HASKELLEVENT_STEAL_SPARK(cap, tid, victim_cap)
244 #define dtraceShutdown(cap) \
245 HASKELLEVENT_SHUTDOWN(cap)
246 #define dtraceThreadWakeup(cap, tid, other_cap) \
247 HASKELLEVENT_THREAD_WAKEUP(cap, tid, other_cap)
248 #define dtraceGcStart(cap) \
249 HASKELLEVENT_GC_START(cap)
250 #define dtraceGcEnd(cap) \
251 HASKELLEVENT_GC_END(cap)
252 #define dtraceRequestSeqGc(cap) \
253 HASKELLEVENT_REQUEST_SEQ_GC(cap)
254 #define dtraceRequestParGc(cap) \
255 HASKELLEVENT_REQUEST_PAR_GC(cap)
256 #define dtraceCreateSparkThread(cap, spark_tid) \
257 HASKELLEVENT_CREATE_SPARK_THREAD(cap, spark_tid)
258 INLINE_HEADER void dtraceStartup (int num_caps) {
259 HASKELLEVENT_STARTUP(num_caps);
260 }
261 #define dtraceUserMsg(cap, msg) \
262 HASKELLEVENT_USER_MSG(cap, msg)
263 #define dtraceGcIdle(cap) \
264 HASKELLEVENT_GC_IDLE(cap)
265 #define dtraceGcWork(cap) \
266 HASKELLEVENT_GC_WORK(cap)
267 #define dtraceGcDone(cap) \
268 HASKELLEVENT_GC_DONE(cap)
269 #define dtraceCapsetCreate(capset, capset_type) \
270 HASKELLEVENT_CAPSET_CREATE(capset, capset_type)
271 #define dtraceCapsetDelete(capset) \
272 HASKELLEVENT_CAPSET_DELETE(capset)
273 #define dtraceCapsetAssignCap(capset, capno) \
274 HASKELLEVENT_CAPSET_ASSIGN_CAP(capset, capno)
275 #define dtraceCapsetRemoveCap(capset, capno) \
276 HASKELLEVENT_CAPSET_REMOVE_CAP(capset, capno)
277 #define dtraceSparkCounters(cap, a, b, c, d, e, f, g) \
278 HASKELLEVENT_SPARK_COUNTERS(cap, a, b, c, d, e, f, g)
279
280 #else /* !defined(DTRACE) */
281
282 #define dtraceCreateThread(cap, tid) /* nothing */
283 #define dtraceRunThread(cap, tid) /* nothing */
284 #define dtraceStopThread(cap, tid, status, info) /* nothing */
285 #define dtraceThreadRunnable(cap, tid) /* nothing */
286 #define dtraceMigrateThread(cap, tid, new_cap) /* nothing */
287 #define dtraceRunSpark(cap, tid) /* nothing */
288 #define dtraceStealSpark(cap, tid, victim_cap) /* nothing */
289 #define dtraceShutdown(cap) /* nothing */
290 #define dtraceThreadWakeup(cap, tid, other_cap) /* nothing */
291 #define dtraceGcStart(cap) /* nothing */
292 #define dtraceGcEnd(cap) /* nothing */
293 #define dtraceRequestSeqGc(cap) /* nothing */
294 #define dtraceRequestParGc(cap) /* nothing */
295 #define dtraceCreateSparkThread(cap, spark_tid) /* nothing */
296 INLINE_HEADER void dtraceStartup (int num_caps STG_UNUSED) {};
297 #define dtraceUserMsg(cap, msg) /* nothing */
298 #define dtraceGcIdle(cap) /* nothing */
299 #define dtraceGcWork(cap) /* nothing */
300 #define dtraceGcDone(cap) /* nothing */
301 #define dtraceCapsetCreate(capset, capset_type) /* nothing */
302 #define dtraceCapsetDelete(capset) /* nothing */
303 #define dtraceCapsetAssignCap(capset, capno) /* nothing */
304 #define dtraceCapsetRemoveCap(capset, capno) /* nothing */
305 #define dtraceSparkCounters(cap, a, b, c, d, e, f, g) /* nothing */
306
307 #endif
308
309 // -----------------------------------------------------------------------------
310 // Trace probes dispatching to various tracing frameworks
311 //
312 // In order to avoid accumulating multiple calls to tracing calls at trace
313 // points, we define inline probe functions that contain the various
314 // invocations.
315 //
316 // Dtrace - dtrace probes are unconditionally added as probe activation is
317 // handled by the dtrace component of the kernel, and inactive probes are
318 // very cheap — usually, one no-op. Consequently, dtrace can be used with
319 // all flavours of the RTS. In addition, we still support logging events to
320 // a file, even in the presence of dtrace. This is, eg, useful when tracing
321 // on a server, but browsing trace information with ThreadScope on a local
322 // client.
323 //
324 // -----------------------------------------------------------------------------
325
326 INLINE_HEADER void traceEventCreateThread(Capability *cap STG_UNUSED,
327 StgTSO *tso STG_UNUSED)
328 {
329 traceSchedEvent(cap, EVENT_CREATE_THREAD, tso, tso->stackobj->stack_size);
330 dtraceCreateThread((EventCapNo)cap->no, (EventThreadID)tso->id);
331 }
332
333 INLINE_HEADER void traceEventRunThread(Capability *cap STG_UNUSED,
334 StgTSO *tso STG_UNUSED)
335 {
336 traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
337 dtraceRunThread((EventCapNo)cap->no, (EventThreadID)tso->id);
338 }
339
340 INLINE_HEADER void traceEventStopThread(Capability *cap STG_UNUSED,
341 StgTSO *tso STG_UNUSED,
342 StgThreadReturnCode status STG_UNUSED,
343 StgWord32 info STG_UNUSED)
344 {
345 traceSchedEvent2(cap, EVENT_STOP_THREAD, tso, status, info);
346 dtraceStopThread((EventCapNo)cap->no, (EventThreadID)tso->id,
347 (EventThreadStatus)status, (EventThreadID)info);
348 }
349
350 // needs to be EXTERN_INLINE as it is used in another EXTERN_INLINE function
351 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
352 StgTSO *tso STG_UNUSED);
353
354 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
355 StgTSO *tso STG_UNUSED)
356 {
357 traceSchedEvent(cap, EVENT_THREAD_RUNNABLE, tso, 0);
358 dtraceThreadRunnable((EventCapNo)cap->no, (EventThreadID)tso->id);
359 }
360
361 INLINE_HEADER void traceEventMigrateThread(Capability *cap STG_UNUSED,
362 StgTSO *tso STG_UNUSED,
363 nat new_cap STG_UNUSED)
364 {
365 traceSchedEvent(cap, EVENT_MIGRATE_THREAD, tso, new_cap);
366 dtraceMigrateThread((EventCapNo)cap->no, (EventThreadID)tso->id,
367 (EventCapNo)new_cap);
368 }
369
370 INLINE_HEADER void traceEventShutdown(Capability *cap STG_UNUSED)
371 {
372 traceSchedEvent(cap, EVENT_SHUTDOWN, 0, 0);
373 dtraceShutdown((EventCapNo)cap->no);
374 }
375
376 INLINE_HEADER void traceEventThreadWakeup(Capability *cap STG_UNUSED,
377 StgTSO *tso STG_UNUSED,
378 nat other_cap STG_UNUSED)
379 {
380 traceSchedEvent(cap, EVENT_THREAD_WAKEUP, tso, other_cap);
381 dtraceThreadWakeup((EventCapNo)cap->no, (EventThreadID)tso->id,
382 (EventCapNo)other_cap);
383 }
384
385 INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
386 {
387 traceGcEvent(cap, EVENT_GC_START);
388 dtraceGcStart((EventCapNo)cap->no);
389 }
390
391 INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
392 {
393 traceGcEvent(cap, EVENT_GC_END);
394 dtraceGcEnd((EventCapNo)cap->no);
395 }
396
397 INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
398 {
399 traceGcEvent(cap, EVENT_REQUEST_SEQ_GC);
400 dtraceRequestSeqGc((EventCapNo)cap->no);
401 }
402
403 INLINE_HEADER void traceEventRequestParGc(Capability *cap STG_UNUSED)
404 {
405 traceGcEvent(cap, EVENT_REQUEST_PAR_GC);
406 dtraceRequestParGc((EventCapNo)cap->no);
407 }
408
409 INLINE_HEADER void traceEventGcIdle(Capability *cap STG_UNUSED)
410 {
411 traceGcEvent(cap, EVENT_GC_IDLE);
412 dtraceGcIdle((EventCapNo)cap->no);
413 }
414
415 INLINE_HEADER void traceEventGcWork(Capability *cap STG_UNUSED)
416 {
417 traceGcEvent(cap, EVENT_GC_WORK);
418 dtraceGcWork((EventCapNo)cap->no);
419 }
420
421 INLINE_HEADER void traceEventGcDone(Capability *cap STG_UNUSED)
422 {
423 traceGcEvent(cap, EVENT_GC_DONE);
424 dtraceGcDone((EventCapNo)cap->no);
425 }
426
427
428 INLINE_HEADER void traceEventRunSpark(Capability *cap STG_UNUSED,
429 StgTSO *tso STG_UNUSED)
430 {
431 traceSparkEvent(cap, EVENT_RUN_SPARK, tso, 0);
432 dtraceRunSpark((EventCapNo)cap->no, (EventThreadID)tso->id);
433 }
434
435 INLINE_HEADER void traceEventStealSpark(Capability *cap STG_UNUSED,
436 StgTSO *tso STG_UNUSED,
437 nat victim_cap STG_UNUSED)
438 {
439 traceSparkEvent(cap, EVENT_STEAL_SPARK, tso, victim_cap);
440 dtraceStealSpark((EventCapNo)cap->no, (EventThreadID)tso->id,
441 (EventCapNo)victim_cap);
442 }
443
444 INLINE_HEADER void traceEventCreateSparkThread(Capability *cap STG_UNUSED,
445 StgThreadID spark_tid STG_UNUSED)
446 {
447 traceSparkEvent(cap, EVENT_CREATE_SPARK_THREAD, 0, spark_tid);
448 dtraceCreateSparkThread((EventCapNo)cap->no, (EventThreadID)spark_tid);
449 }
450
451 INLINE_HEADER void traceEventStartup(void)
452 {
453 int n_caps;
454 #ifdef THREADED_RTS
455 // XXX n_capabilities hasn't been initialised yet
456 n_caps = RtsFlags.ParFlags.nNodes;
457 #else
458 n_caps = 1;
459 #endif
460
461 traceEventStartup_(n_caps);
462 dtraceStartup(n_caps);
463 }
464
465 INLINE_HEADER void traceCapsetCreate(CapsetID capset STG_UNUSED,
466 CapsetType capset_type STG_UNUSED)
467 {
468 traceCapsetModify_(EVENT_CAPSET_CREATE, capset, capset_type);
469 dtraceCapsetCreate(capset, capset_type);
470 }
471
472 INLINE_HEADER void traceCapsetDelete(CapsetID capset STG_UNUSED)
473 {
474 traceCapsetModify_(EVENT_CAPSET_DELETE, capset, 0);
475 dtraceCapsetDelete(capset);
476 }
477
478 INLINE_HEADER void traceCapsetAssignCap(CapsetID capset STG_UNUSED,
479 nat capno STG_UNUSED)
480 {
481 traceCapsetModify_(EVENT_CAPSET_ASSIGN_CAP, capset, capno);
482 dtraceCapsetAssignCap(capset, capno);
483 }
484
485 INLINE_HEADER void traceCapsetRemoveCap(CapsetID capset STG_UNUSED,
486 nat capno STG_UNUSED)
487 {
488 traceCapsetModify_(EVENT_CAPSET_REMOVE_CAP, capset, capno);
489 dtraceCapsetRemoveCap(capset, capno);
490 }
491
492 INLINE_HEADER void traceOSProcessInfo(void)
493 {
494 traceOSProcessInfo_();
495 /* Note: no DTrace equivalent because all this OS process info
496 * is available to DTrace directly */
497 }
498
499 INLINE_HEADER void traceSparkCounters(Capability *cap STG_UNUSED)
500 {
501 #ifdef THREADED_RTS
502 if (RTS_UNLIKELY(TRACE_spark)) {
503 traceSparkCounters_(cap, cap->spark_stats, sparkPoolSize(cap->sparks));
504 }
505 #endif
506 dtraceSparkCounters((EventCapNo)cap->no,
507 cap->spark_stats.created,
508 cap->spark_stats.dud,
509 cap->spark_stats.overflowed,
510 cap->spark_stats.converted,
511 cap->spark_stats.gcd,
512 cap->spark_stats.fizzled,
513 sparkPoolSize(cap->sparks));
514 }
515
516
517 #include "EndPrivate.h"
518
519 #endif /* TRACE_H */