Add new fully-accurate per-spark trace/eventlog events
[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_sampled;
67 extern int TRACE_spark_full;
68
69 // -----------------------------------------------------------------------------
70 // Posting events
71 //
72 // We use macros rather than inline functions deliberately. We want
73 // the not-taken case to be as efficient as possible, a simple
74 // test-and-jump, and with inline functions gcc seemed to move some of
75 // the instructions from the branch up before the test.
76 //
77 // -----------------------------------------------------------------------------
78
79 #ifdef DEBUG
80 void traceBegin (const char *str, ...);
81 void traceEnd (void);
82 #endif
83
84 #ifdef TRACING
85
86 /*
87 * Record a scheduler event
88 */
89 #define traceSchedEvent(cap, tag, tso, other) \
90 if (RTS_UNLIKELY(TRACE_sched)) { \
91 traceSchedEvent_(cap, tag, tso, other, 0); \
92 }
93
94 #define traceSchedEvent2(cap, tag, tso, info1, info2) \
95 if (RTS_UNLIKELY(TRACE_sched)) { \
96 traceSchedEvent_(cap, tag, tso, info1, info2); \
97 }
98
99 void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
100 StgTSO *tso, StgWord info1, StgWord info2);
101
102 /*
103 * Record a GC event
104 */
105 #define traceGcEvent(cap, tag) \
106 if (RTS_UNLIKELY(TRACE_gc)) { \
107 traceGcEvent_(cap, tag); \
108 }
109
110 void traceGcEvent_ (Capability *cap, EventTypeNum tag);
111
112 /*
113 * Record a spark event
114 */
115 #define traceSparkEvent(cap, tag) \
116 if (RTS_UNLIKELY(TRACE_spark_full)) { \
117 traceSparkEvent_(cap, tag, 0); \
118 }
119
120 #define traceSparkEvent2(cap, tag, other) \
121 if (RTS_UNLIKELY(TRACE_spark_full)) { \
122 traceSparkEvent_(cap, tag, other); \
123 }
124
125 void traceSparkEvent_ (Capability *cap, EventTypeNum tag, StgWord info1);
126
127 // variadic macros are C99, and supported by gcc. However, the
128 // ##__VA_ARGS syntax is a gcc extension, which allows the variable
129 // argument list to be empty (see gcc docs for details).
130
131 /*
132 * Emit a trace message on a particular Capability
133 */
134 #define traceCap(class, cap, msg, ...) \
135 if (RTS_UNLIKELY(class)) { \
136 traceCap_(cap, msg, ##__VA_ARGS__); \
137 }
138
139 void traceCap_(Capability *cap, char *msg, ...);
140
141 /*
142 * Emit a trace message
143 */
144 #define trace(class, msg, ...) \
145 if (RTS_UNLIKELY(class)) { \
146 trace_(msg, ##__VA_ARGS__); \
147 }
148
149 void trace_(char *msg, ...);
150
151 /*
152 * A message or event emitted by the program
153 */
154 void traceUserMsg(Capability *cap, char *msg);
155
156 /*
157 * Emit a debug message (only when DEBUG is defined)
158 */
159 #ifdef DEBUG
160 #define debugTrace(class, msg, ...) \
161 if (RTS_UNLIKELY(class)) { \
162 trace_(msg, ##__VA_ARGS__); \
163 }
164 #else
165 #define debugTrace(class, str, ...) /* nothing */
166 #endif
167
168 #ifdef DEBUG
169 #define debugTraceCap(class, cap, msg, ...) \
170 if (RTS_UNLIKELY(class)) { \
171 traceCap_(cap, msg, ##__VA_ARGS__); \
172 }
173 #else
174 #define debugTraceCap(class, cap, str, ...) /* nothing */
175 #endif
176
177 /*
178 * Emit a message/event describing the state of a thread
179 */
180 #define traceThreadStatus(class, tso) \
181 if (RTS_UNLIKELY(class)) { \
182 traceThreadStatus_(tso); \
183 }
184
185 void traceThreadStatus_ (StgTSO *tso);
186
187 void traceEventStartup_ (int n_caps);
188
189 /*
190 * Events for describing capability sets in the eventlog
191 *
192 * Note: unlike other events, these are not conditional on TRACE_sched or
193 * similar because they are not "real" events themselves but provide
194 * information and context for other "real" events. Other events depend on
195 * the capset info events so for simplicity, rather than working out if
196 * they're necessary we always emit them. They should be very low volume.
197 */
198 void traceCapsetModify_ (EventTypeNum tag,
199 CapsetID capset,
200 StgWord32 other);
201
202 void traceOSProcessInfo_ (void);
203
204 void traceSparkCounters_ (Capability *cap,
205 SparkCounters counters,
206 StgWord remaining);
207
208 #else /* !TRACING */
209
210 #define traceSchedEvent(cap, tag, tso, other) /* nothing */
211 #define traceSchedEvent2(cap, tag, tso, other, info) /* nothing */
212 #define traceGcEvent(cap, tag) /* nothing */
213 #define traceSparkEvent(cap, tag) /* nothing */
214 #define traceSparkEvent2(cap, tag, other) /* nothing */
215 #define traceCap(class, cap, msg, ...) /* nothing */
216 #define trace(class, msg, ...) /* nothing */
217 #define debugTrace(class, str, ...) /* nothing */
218 #define debugTraceCap(class, cap, str, ...) /* nothing */
219 #define traceThreadStatus(class, tso) /* nothing */
220 INLINE_HEADER void traceEventStartup_ (int n_caps STG_UNUSED) {};
221 #define traceCapsetModify_(tag, capset, other) /* nothing */
222 #define traceOSProcessInfo_() /* nothing */
223 #define traceSparkCounters_(cap, counters, remaining) /* nothing */
224
225 #endif /* TRACING */
226
227 // If DTRACE is enabled, but neither DEBUG nor TRACING, we need a C land
228 // wrapper for the user-msg probe (as we can't expand that in PrimOps.cmm)
229 //
230 #if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)
231
232 void dtraceUserMsgWrapper(Capability *cap, char *msg);
233
234 #endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */
235
236 // -----------------------------------------------------------------------------
237 // Aliases for static dtrace probes if dtrace is available
238 // -----------------------------------------------------------------------------
239
240 #if defined(DTRACE)
241
242 #define dtraceCreateThread(cap, tid) \
243 HASKELLEVENT_CREATE_THREAD(cap, tid)
244 #define dtraceRunThread(cap, tid) \
245 HASKELLEVENT_RUN_THREAD(cap, tid)
246 #define dtraceStopThread(cap, tid, status, info) \
247 HASKELLEVENT_STOP_THREAD(cap, tid, status, info)
248 #define dtraceThreadRunnable(cap, tid) \
249 HASKELLEVENT_THREAD_RUNNABLE(cap, tid)
250 #define dtraceMigrateThread(cap, tid, new_cap) \
251 HASKELLEVENT_MIGRATE_THREAD(cap, tid, new_cap)
252 #define dtraceShutdown(cap) \
253 HASKELLEVENT_SHUTDOWN(cap)
254 #define dtraceThreadWakeup(cap, tid, other_cap) \
255 HASKELLEVENT_THREAD_WAKEUP(cap, tid, other_cap)
256 #define dtraceGcStart(cap) \
257 HASKELLEVENT_GC_START(cap)
258 #define dtraceGcEnd(cap) \
259 HASKELLEVENT_GC_END(cap)
260 #define dtraceRequestSeqGc(cap) \
261 HASKELLEVENT_REQUEST_SEQ_GC(cap)
262 #define dtraceRequestParGc(cap) \
263 HASKELLEVENT_REQUEST_PAR_GC(cap)
264 #define dtraceCreateSparkThread(cap, spark_tid) \
265 HASKELLEVENT_CREATE_SPARK_THREAD(cap, spark_tid)
266 INLINE_HEADER void dtraceStartup (int num_caps) {
267 HASKELLEVENT_STARTUP(num_caps);
268 }
269 #define dtraceUserMsg(cap, msg) \
270 HASKELLEVENT_USER_MSG(cap, msg)
271 #define dtraceGcIdle(cap) \
272 HASKELLEVENT_GC_IDLE(cap)
273 #define dtraceGcWork(cap) \
274 HASKELLEVENT_GC_WORK(cap)
275 #define dtraceGcDone(cap) \
276 HASKELLEVENT_GC_DONE(cap)
277 #define dtraceCapsetCreate(capset, capset_type) \
278 HASKELLEVENT_CAPSET_CREATE(capset, capset_type)
279 #define dtraceCapsetDelete(capset) \
280 HASKELLEVENT_CAPSET_DELETE(capset)
281 #define dtraceCapsetAssignCap(capset, capno) \
282 HASKELLEVENT_CAPSET_ASSIGN_CAP(capset, capno)
283 #define dtraceCapsetRemoveCap(capset, capno) \
284 HASKELLEVENT_CAPSET_REMOVE_CAP(capset, capno)
285 #define dtraceSparkCounters(cap, a, b, c, d, e, f, g) \
286 HASKELLEVENT_SPARK_COUNTERS(cap, a, b, c, d, e, f, g)
287 #define dtraceSparkCreate(cap) \
288 HASKELLEVENT_SPARK_CREATE(cap)
289 #define dtraceSparkDud(cap) \
290 HASKELLEVENT_SPARK_DUD(cap)
291 #define dtraceSparkOverflow(cap) \
292 HASKELLEVENT_SPARK_OVERFLOW(cap)
293 #define dtraceSparkRun(cap) \
294 HASKELLEVENT_SPARK_RUN(cap)
295 #define dtraceSparkSteal(cap, victim_cap) \
296 HASKELLEVENT_SPARK_STEAL(cap, victim_cap)
297 #define dtraceSparkFizzle(cap) \
298 HASKELLEVENT_SPARK_FIZZLE(cap)
299 #define dtraceSparkGc(cap) \
300 HASKELLEVENT_SPARK_GC(cap)
301
302 #else /* !defined(DTRACE) */
303
304 #define dtraceCreateThread(cap, tid) /* nothing */
305 #define dtraceRunThread(cap, tid) /* nothing */
306 #define dtraceStopThread(cap, tid, status, info) /* nothing */
307 #define dtraceThreadRunnable(cap, tid) /* nothing */
308 #define dtraceMigrateThread(cap, tid, new_cap) /* nothing */
309 #define dtraceShutdown(cap) /* nothing */
310 #define dtraceThreadWakeup(cap, tid, other_cap) /* nothing */
311 #define dtraceGcStart(cap) /* nothing */
312 #define dtraceGcEnd(cap) /* nothing */
313 #define dtraceRequestSeqGc(cap) /* nothing */
314 #define dtraceRequestParGc(cap) /* nothing */
315 #define dtraceCreateSparkThread(cap, spark_tid) /* nothing */
316 INLINE_HEADER void dtraceStartup (int num_caps STG_UNUSED) {};
317 #define dtraceUserMsg(cap, msg) /* nothing */
318 #define dtraceGcIdle(cap) /* nothing */
319 #define dtraceGcWork(cap) /* nothing */
320 #define dtraceGcDone(cap) /* nothing */
321 #define dtraceCapsetCreate(capset, capset_type) /* nothing */
322 #define dtraceCapsetDelete(capset) /* nothing */
323 #define dtraceCapsetAssignCap(capset, capno) /* nothing */
324 #define dtraceCapsetRemoveCap(capset, capno) /* nothing */
325 #define dtraceSparkCounters(cap, a, b, c, d, e, f, g) /* nothing */
326 #define dtraceSparkCreate(cap) /* nothing */
327 #define dtraceSparkDud(cap) /* nothing */
328 #define dtraceSparkOverflow(cap) /* nothing */
329 #define dtraceSparkRun(cap) /* nothing */
330 #define dtraceSparkSteal(cap, victim_cap) /* nothing */
331 #define dtraceSparkFizzle(cap) /* nothing */
332 #define dtraceSparkGc(cap) /* nothing */
333
334 #endif
335
336 // -----------------------------------------------------------------------------
337 // Trace probes dispatching to various tracing frameworks
338 //
339 // In order to avoid accumulating multiple calls to tracing calls at trace
340 // points, we define inline probe functions that contain the various
341 // invocations.
342 //
343 // Dtrace - dtrace probes are unconditionally added as probe activation is
344 // handled by the dtrace component of the kernel, and inactive probes are
345 // very cheap — usually, one no-op. Consequently, dtrace can be used with
346 // all flavours of the RTS. In addition, we still support logging events to
347 // a file, even in the presence of dtrace. This is, eg, useful when tracing
348 // on a server, but browsing trace information with ThreadScope on a local
349 // client.
350 //
351 // -----------------------------------------------------------------------------
352
353 INLINE_HEADER void traceEventCreateThread(Capability *cap STG_UNUSED,
354 StgTSO *tso STG_UNUSED)
355 {
356 traceSchedEvent(cap, EVENT_CREATE_THREAD, tso, tso->stackobj->stack_size);
357 dtraceCreateThread((EventCapNo)cap->no, (EventThreadID)tso->id);
358 }
359
360 INLINE_HEADER void traceEventRunThread(Capability *cap STG_UNUSED,
361 StgTSO *tso STG_UNUSED)
362 {
363 traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
364 dtraceRunThread((EventCapNo)cap->no, (EventThreadID)tso->id);
365 }
366
367 INLINE_HEADER void traceEventStopThread(Capability *cap STG_UNUSED,
368 StgTSO *tso STG_UNUSED,
369 StgThreadReturnCode status STG_UNUSED,
370 StgWord32 info STG_UNUSED)
371 {
372 traceSchedEvent2(cap, EVENT_STOP_THREAD, tso, status, info);
373 dtraceStopThread((EventCapNo)cap->no, (EventThreadID)tso->id,
374 (EventThreadStatus)status, (EventThreadID)info);
375 }
376
377 // needs to be EXTERN_INLINE as it is used in another EXTERN_INLINE function
378 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
379 StgTSO *tso STG_UNUSED);
380
381 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
382 StgTSO *tso STG_UNUSED)
383 {
384 traceSchedEvent(cap, EVENT_THREAD_RUNNABLE, tso, 0);
385 dtraceThreadRunnable((EventCapNo)cap->no, (EventThreadID)tso->id);
386 }
387
388 INLINE_HEADER void traceEventMigrateThread(Capability *cap STG_UNUSED,
389 StgTSO *tso STG_UNUSED,
390 nat new_cap STG_UNUSED)
391 {
392 traceSchedEvent(cap, EVENT_MIGRATE_THREAD, tso, new_cap);
393 dtraceMigrateThread((EventCapNo)cap->no, (EventThreadID)tso->id,
394 (EventCapNo)new_cap);
395 }
396
397 INLINE_HEADER void traceEventShutdown(Capability *cap STG_UNUSED)
398 {
399 traceSchedEvent(cap, EVENT_SHUTDOWN, 0, 0);
400 dtraceShutdown((EventCapNo)cap->no);
401 }
402
403 INLINE_HEADER void traceEventThreadWakeup(Capability *cap STG_UNUSED,
404 StgTSO *tso STG_UNUSED,
405 nat other_cap STG_UNUSED)
406 {
407 traceSchedEvent(cap, EVENT_THREAD_WAKEUP, tso, other_cap);
408 dtraceThreadWakeup((EventCapNo)cap->no, (EventThreadID)tso->id,
409 (EventCapNo)other_cap);
410 }
411
412 INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
413 {
414 traceGcEvent(cap, EVENT_GC_START);
415 dtraceGcStart((EventCapNo)cap->no);
416 }
417
418 INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
419 {
420 traceGcEvent(cap, EVENT_GC_END);
421 dtraceGcEnd((EventCapNo)cap->no);
422 }
423
424 INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
425 {
426 traceGcEvent(cap, EVENT_REQUEST_SEQ_GC);
427 dtraceRequestSeqGc((EventCapNo)cap->no);
428 }
429
430 INLINE_HEADER void traceEventRequestParGc(Capability *cap STG_UNUSED)
431 {
432 traceGcEvent(cap, EVENT_REQUEST_PAR_GC);
433 dtraceRequestParGc((EventCapNo)cap->no);
434 }
435
436 INLINE_HEADER void traceEventGcIdle(Capability *cap STG_UNUSED)
437 {
438 traceGcEvent(cap, EVENT_GC_IDLE);
439 dtraceGcIdle((EventCapNo)cap->no);
440 }
441
442 INLINE_HEADER void traceEventGcWork(Capability *cap STG_UNUSED)
443 {
444 traceGcEvent(cap, EVENT_GC_WORK);
445 dtraceGcWork((EventCapNo)cap->no);
446 }
447
448 INLINE_HEADER void traceEventGcDone(Capability *cap STG_UNUSED)
449 {
450 traceGcEvent(cap, EVENT_GC_DONE);
451 dtraceGcDone((EventCapNo)cap->no);
452 }
453
454 INLINE_HEADER void traceEventStartup(void)
455 {
456 int n_caps;
457 #ifdef THREADED_RTS
458 // XXX n_capabilities hasn't been initialised yet
459 n_caps = RtsFlags.ParFlags.nNodes;
460 #else
461 n_caps = 1;
462 #endif
463
464 traceEventStartup_(n_caps);
465 dtraceStartup(n_caps);
466 }
467
468 INLINE_HEADER void traceCapsetCreate(CapsetID capset STG_UNUSED,
469 CapsetType capset_type STG_UNUSED)
470 {
471 traceCapsetModify_(EVENT_CAPSET_CREATE, capset, capset_type);
472 dtraceCapsetCreate(capset, capset_type);
473 }
474
475 INLINE_HEADER void traceCapsetDelete(CapsetID capset STG_UNUSED)
476 {
477 traceCapsetModify_(EVENT_CAPSET_DELETE, capset, 0);
478 dtraceCapsetDelete(capset);
479 }
480
481 INLINE_HEADER void traceCapsetAssignCap(CapsetID capset STG_UNUSED,
482 nat capno STG_UNUSED)
483 {
484 traceCapsetModify_(EVENT_CAPSET_ASSIGN_CAP, capset, capno);
485 dtraceCapsetAssignCap(capset, capno);
486 }
487
488 INLINE_HEADER void traceCapsetRemoveCap(CapsetID capset STG_UNUSED,
489 nat capno STG_UNUSED)
490 {
491 traceCapsetModify_(EVENT_CAPSET_REMOVE_CAP, capset, capno);
492 dtraceCapsetRemoveCap(capset, capno);
493 }
494
495 INLINE_HEADER void traceOSProcessInfo(void)
496 {
497 traceOSProcessInfo_();
498 /* Note: no DTrace equivalent because all this OS process info
499 * is available to DTrace directly */
500 }
501
502 INLINE_HEADER void traceEventCreateSparkThread(Capability *cap STG_UNUSED,
503 StgThreadID spark_tid STG_UNUSED)
504 {
505 traceSparkEvent2(cap, EVENT_CREATE_SPARK_THREAD, spark_tid);
506 dtraceCreateSparkThread((EventCapNo)cap->no, (EventThreadID)spark_tid);
507 }
508
509 INLINE_HEADER void traceSparkCounters(Capability *cap STG_UNUSED)
510 {
511 #ifdef THREADED_RTS
512 if (RTS_UNLIKELY(TRACE_spark_sampled)) {
513 traceSparkCounters_(cap, cap->spark_stats, sparkPoolSize(cap->sparks));
514 }
515 #endif
516 dtraceSparkCounters((EventCapNo)cap->no,
517 cap->spark_stats.created,
518 cap->spark_stats.dud,
519 cap->spark_stats.overflowed,
520 cap->spark_stats.converted,
521 cap->spark_stats.gcd,
522 cap->spark_stats.fizzled,
523 sparkPoolSize(cap->sparks));
524 }
525
526 INLINE_HEADER void traceEventSparkCreate(Capability *cap STG_UNUSED)
527 {
528 traceSparkEvent(cap, EVENT_SPARK_CREATE);
529 dtraceSparkCreate((EventCapNo)cap->no);
530 }
531
532 INLINE_HEADER void traceEventSparkDud(Capability *cap STG_UNUSED)
533 {
534 traceSparkEvent(cap, EVENT_SPARK_DUD);
535 dtraceSparkDud((EventCapNo)cap->no);
536 }
537
538 INLINE_HEADER void traceEventSparkOverflow(Capability *cap STG_UNUSED)
539 {
540 traceSparkEvent(cap, EVENT_SPARK_OVERFLOW);
541 dtraceSparkOverflow((EventCapNo)cap->no);
542 }
543
544 INLINE_HEADER void traceEventSparkRun(Capability *cap STG_UNUSED)
545 {
546 traceSparkEvent(cap, EVENT_SPARK_RUN);
547 dtraceSparkRun((EventCapNo)cap->no);
548 }
549
550 INLINE_HEADER void traceEventSparkSteal(Capability *cap STG_UNUSED,
551 nat victim_cap STG_UNUSED)
552 {
553 traceSparkEvent2(cap, EVENT_SPARK_STEAL, victim_cap);
554 dtraceSparkSteal((EventCapNo)cap->no, (EventCapNo)victim_cap);
555 }
556
557 INLINE_HEADER void traceEventSparkFizzle(Capability *cap STG_UNUSED)
558 {
559 traceSparkEvent(cap, EVENT_SPARK_FIZZLE);
560 dtraceSparkFizzle((EventCapNo)cap->no);
561 }
562
563 INLINE_HEADER void traceEventSparkGC(Capability *cap STG_UNUSED)
564 {
565 traceSparkEvent(cap, EVENT_SPARK_GC);
566 dtraceSparkGc((EventCapNo)cap->no);
567 }
568
569 #include "EndPrivate.h"
570
571 #endif /* TRACE_H */