Refactor sync-all a bit
[ghc-hetmet.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 // -----------------------------------------------------------------------------
35 // Message classes
36 // -----------------------------------------------------------------------------
37
38 // debugging flags, set with +RTS -D<something>
39 extern int DEBUG_sched;
40 extern int DEBUG_interp;
41 extern int DEBUG_weak;
42 extern int DEBUG_gccafs;
43 extern int DEBUG_gc;
44 extern int DEBUG_block_alloc;
45 extern int DEBUG_sanity;
46 extern int DEBUG_stable;
47 extern int DEBUG_stm;
48 extern int DEBUG_prof;
49 extern int DEBUG_gran;
50 extern int DEBUG_par;
51 extern int DEBUG_linker;
52 extern int DEBUG_squeeze;
53 extern int DEBUG_hpc;
54 extern int DEBUG_sparks;
55
56 // events
57 extern int TRACE_sched;
58
59 // -----------------------------------------------------------------------------
60 // Posting events
61 //
62 // We use macros rather than inline functions deliberately.  We want
63 // the not-taken case to be as efficient as possible, a simple
64 // test-and-jump, and with inline functions gcc seemed to move some of
65 // the instructions from the branch up before the test.
66 // 
67 // -----------------------------------------------------------------------------
68
69 #ifdef DEBUG
70 void traceBegin (const char *str, ...);
71 void traceEnd (void);
72 #endif
73
74 #ifdef TRACING
75
76 /* 
77  * Record a scheduler event
78  */
79 #define traceSchedEvent(cap, tag, tso, other)   \
80     if (RTS_UNLIKELY(TRACE_sched)) {            \
81         traceSchedEvent_(cap, tag, tso, other, 0); \
82     }
83
84 #define traceSchedEvent2(cap, tag, tso, info1, info2) \
85     if (RTS_UNLIKELY(TRACE_sched)) {            \
86         traceSchedEvent_(cap, tag, tso, info1, info2); \
87     }
88
89 void traceSchedEvent_ (Capability *cap, EventTypeNum tag, 
90                        StgTSO *tso, StgWord info1, StgWord info2);
91
92
93 /*
94  * Record a nullary event
95  */
96 #define traceEvent(cap, tag)                    \
97     if (RTS_UNLIKELY(TRACE_sched)) {            \
98         traceEvent_(cap, tag);                  \
99     }
100
101 void traceEvent_ (Capability *cap, EventTypeNum tag);
102
103 // variadic macros are C99, and supported by gcc.  However, the
104 // ##__VA_ARGS syntax is a gcc extension, which allows the variable
105 // argument list to be empty (see gcc docs for details).
106
107 /* 
108  * Emit a trace message on a particular Capability
109  */
110 #define traceCap(class, cap, msg, ...)          \
111     if (RTS_UNLIKELY(class)) {                  \
112         traceCap_(cap, msg, ##__VA_ARGS__);     \
113     }
114
115 void traceCap_(Capability *cap, char *msg, ...);
116
117 /* 
118  * Emit a trace message
119  */
120 #define trace(class, msg, ...)                  \
121     if (RTS_UNLIKELY(class)) {                  \
122         trace_(msg, ##__VA_ARGS__);             \
123     }
124
125 void trace_(char *msg, ...);
126
127 /* 
128  * A message or event emitted by the program
129  */
130 void traceUserMsg(Capability *cap, char *msg);
131
132 /* 
133  * Emit a debug message (only when DEBUG is defined)
134  */
135 #ifdef DEBUG
136 #define debugTrace(class, msg, ...)             \
137     if (RTS_UNLIKELY(class)) {                  \
138         trace_(msg, ##__VA_ARGS__);             \
139     }
140 #else
141 #define debugTrace(class, str, ...) /* nothing */
142 #endif
143
144 #ifdef DEBUG
145 #define debugTraceCap(class, cap, msg, ...)      \
146     if (RTS_UNLIKELY(class)) {                  \
147         traceCap_(cap, msg, ##__VA_ARGS__);     \
148     }
149 #else
150 #define debugTraceCap(class, cap, str, ...) /* nothing */
151 #endif
152
153 /* 
154  * Emit a message/event describing the state of a thread
155  */
156 #define traceThreadStatus(class, tso)           \
157     if (RTS_UNLIKELY(class)) {                  \
158         traceThreadStatus_(tso);                \
159     }
160
161 void traceThreadStatus_ (StgTSO *tso);
162
163 #else /* !TRACING */
164
165 #define traceSchedEvent(cap, tag, tso, other) /* nothing */
166 #define traceSchedEvent2(cap, tag, tso, other, info) /* nothing */
167 #define traceEvent(cap, tag) /* nothing */
168 #define traceCap(class, cap, msg, ...) /* nothing */
169 #define trace(class, msg, ...) /* nothing */
170 #define debugTrace(class, str, ...) /* nothing */
171 #define debugTraceCap(class, cap, str, ...) /* nothing */
172 #define traceThreadStatus(class, tso) /* nothing */
173
174 #endif /* TRACING */
175
176 // If DTRACE is enabled, but neither DEBUG nor TRACING, we need a C land
177 // wrapper for the user-msg probe (as we can't expand that in PrimOps.cmm)
178 //
179 #if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)
180
181 void dtraceUserMsgWrapper(Capability *cap, char *msg);
182
183 #endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */
184
185 // -----------------------------------------------------------------------------
186 // Aliases for static dtrace probes if dtrace is available
187 // -----------------------------------------------------------------------------
188
189 #if defined(DTRACE)
190
191 #define dtraceCreateThread(cap, tid)                    \
192     HASKELLEVENT_CREATE_THREAD(cap, tid)
193 #define dtraceRunThread(cap, tid)                       \
194     HASKELLEVENT_RUN_THREAD(cap, tid)
195 #define dtraceStopThread(cap, tid, status, info)        \
196     HASKELLEVENT_STOP_THREAD(cap, tid, status, info)
197 #define dtraceThreadRunnable(cap, tid)                  \
198     HASKELLEVENT_THREAD_RUNNABLE(cap, tid)
199 #define dtraceMigrateThread(cap, tid, new_cap)          \
200     HASKELLEVENT_MIGRATE_THREAD(cap, tid, new_cap)
201 #define dtraceRunSpark(cap, tid)                        \
202     HASKELLEVENT_RUN_SPARK(cap, tid)
203 #define dtraceStealSpark(cap, tid, victim_cap)          \
204     HASKELLEVENT_STEAL_SPARK(cap, tid, victim_cap)
205 #define dtraceShutdown(cap)                             \
206     HASKELLEVENT_SHUTDOWN(cap)
207 #define dtraceThreadWakeup(cap, tid, other_cap)         \
208     HASKELLEVENT_THREAD_WAKEUP(cap, tid, other_cap)
209 #define dtraceGcStart(cap)                              \
210     HASKELLEVENT_GC_START(cap)
211 #define dtraceGcEnd(cap)                                \
212     HASKELLEVENT_GC_END(cap)
213 #define dtraceRequestSeqGc(cap)                         \
214     HASKELLEVENT_REQUEST_SEQ_GC(cap)
215 #define dtraceRequestParGc(cap)                         \
216     HASKELLEVENT_REQUEST_PAR_GC(cap)
217 #define dtraceCreateSparkThread(cap, spark_tid)         \
218     HASKELLEVENT_CREATE_SPARK_THREAD(cap, spark_tid)
219 #define dtraceStartup(num_caps)                         \
220     HASKELLEVENT_STARTUP(num_caps)
221 #define dtraceUserMsg(cap, msg)                         \
222     HASKELLEVENT_USER_MSG(cap, msg)
223 #define dtraceGcIdle(cap)                               \
224     HASKELLEVENT_GC_IDLE(cap)
225 #define dtraceGcWork(cap)                               \
226     HASKELLEVENT_GC_WORK(cap)
227 #define dtraceGcDone(cap)                               \
228     HASKELLEVENT_GC_DONE(cap)
229
230 #else /* !defined(DTRACE) */
231
232 #define dtraceCreateThread(cap, tid)                    /* nothing */
233 #define dtraceRunThread(cap, tid)                       /* nothing */
234 #define dtraceStopThread(cap, tid, status, info)        /* nothing */
235 #define dtraceThreadRunnable(cap, tid)                  /* nothing */
236 #define dtraceMigrateThread(cap, tid, new_cap)          /* nothing */
237 #define dtraceRunSpark(cap, tid)                        /* nothing */
238 #define dtraceStealSpark(cap, tid, victim_cap)          /* nothing */
239 #define dtraceShutdown(cap)                             /* nothing */
240 #define dtraceThreadWakeup(cap, tid, other_cap)         /* nothing */
241 #define dtraceGcStart(cap)                              /* nothing */
242 #define dtraceGcEnd(cap)                                /* nothing */
243 #define dtraceRequestSeqGc(cap)                         /* nothing */
244 #define dtraceRequestParGc(cap)                         /* nothing */
245 #define dtraceCreateSparkThread(cap, spark_tid)         /* nothing */
246 #define dtraceStartup(num_caps)                         /* nothing */
247 #define dtraceUserMsg(cap, msg)                         /* nothing */
248 #define dtraceGcIdle(cap)                               /* nothing */
249 #define dtraceGcWork(cap)                               /* nothing */
250 #define dtraceGcDone(cap)                               /* nothing */
251
252 #endif
253
254 // -----------------------------------------------------------------------------
255 // Trace probes dispatching to various tracing frameworks
256 //
257 // In order to avoid accumulating multiple calls to tracing calls at trace
258 // points, we define inline probe functions that contain the various
259 // invocations.
260 //
261 // Dtrace - dtrace probes are unconditionally added as probe activation is
262 //   handled by the dtrace component of the kernel, and inactive probes are
263 //   very cheap — usually, one no-op.  Consequently, dtrace can be used with
264 //   all flavours of the RTS.  In addition, we still support logging events to
265 //   a file, even in the presence of dtrace.  This is, eg, useful when tracing
266 //   on a server, but browsing trace information with ThreadScope on a local
267 //   client.
268 // 
269 // -----------------------------------------------------------------------------
270
271 INLINE_HEADER void traceEventCreateThread(Capability *cap STG_UNUSED, 
272                                           StgTSO     *tso STG_UNUSED)
273 {
274     traceSchedEvent(cap, EVENT_CREATE_THREAD, tso, tso->stackobj->stack_size);
275     dtraceCreateThread((EventCapNo)cap->no, (EventThreadID)tso->id);
276 }
277
278 INLINE_HEADER void traceEventRunThread(Capability *cap STG_UNUSED, 
279                                        StgTSO     *tso STG_UNUSED)
280 {
281     traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
282     dtraceRunThread((EventCapNo)cap->no, (EventThreadID)tso->id);
283 }
284
285 INLINE_HEADER void traceEventStopThread(Capability          *cap    STG_UNUSED, 
286                                         StgTSO              *tso    STG_UNUSED, 
287                                         StgThreadReturnCode  status STG_UNUSED,
288                                         StgWord32           info    STG_UNUSED)
289 {
290     traceSchedEvent2(cap, EVENT_STOP_THREAD, tso, status, info);
291     dtraceStopThread((EventCapNo)cap->no, (EventThreadID)tso->id,
292                      (EventThreadStatus)status, (EventThreadID)info);
293 }
294
295 // needs to be EXTERN_INLINE as it is used in another EXTERN_INLINE function
296 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED, 
297                                             StgTSO     *tso STG_UNUSED);
298
299 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED, 
300                                             StgTSO     *tso STG_UNUSED)
301 {
302     traceSchedEvent(cap, EVENT_THREAD_RUNNABLE, tso, 0);
303     dtraceThreadRunnable((EventCapNo)cap->no, (EventThreadID)tso->id);
304 }
305
306 INLINE_HEADER void traceEventMigrateThread(Capability *cap     STG_UNUSED, 
307                                            StgTSO     *tso     STG_UNUSED,
308                                            nat         new_cap STG_UNUSED)
309 {
310     traceSchedEvent(cap, EVENT_MIGRATE_THREAD, tso, new_cap);
311     dtraceMigrateThread((EventCapNo)cap->no, (EventThreadID)tso->id,
312                         (EventCapNo)new_cap);
313 }
314
315 INLINE_HEADER void traceEventRunSpark(Capability *cap STG_UNUSED, 
316                                       StgTSO     *tso STG_UNUSED)
317 {
318     traceSchedEvent(cap, EVENT_RUN_SPARK, tso, 0);
319     dtraceRunSpark((EventCapNo)cap->no, (EventThreadID)tso->id);
320 }
321
322 INLINE_HEADER void traceEventStealSpark(Capability *cap        STG_UNUSED, 
323                                         StgTSO     *tso        STG_UNUSED,
324                                         nat         victim_cap STG_UNUSED)
325 {
326     traceSchedEvent(cap, EVENT_STEAL_SPARK, tso, victim_cap);
327     dtraceStealSpark((EventCapNo)cap->no, (EventThreadID)tso->id,
328                      (EventCapNo)victim_cap);
329 }
330
331 INLINE_HEADER void traceEventShutdown(Capability *cap STG_UNUSED)
332 {
333     traceSchedEvent(cap, EVENT_SHUTDOWN, 0, 0);
334     dtraceShutdown((EventCapNo)cap->no);
335 }
336
337 INLINE_HEADER void traceEventThreadWakeup(Capability *cap       STG_UNUSED, 
338                                           StgTSO     *tso       STG_UNUSED,
339                                           nat         other_cap STG_UNUSED)
340 {
341     traceSchedEvent(cap, EVENT_THREAD_WAKEUP, tso, other_cap);
342     dtraceThreadWakeup((EventCapNo)cap->no, (EventThreadID)tso->id,
343                        (EventCapNo)other_cap);
344 }
345
346 INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
347 {
348     traceSchedEvent(cap, EVENT_GC_START, 0, 0);
349     dtraceGcStart((EventCapNo)cap->no);
350 }
351
352 INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
353 {
354     traceSchedEvent(cap, EVENT_GC_END, 0, 0);
355     dtraceGcEnd((EventCapNo)cap->no);
356 }
357
358 INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
359 {
360     traceSchedEvent(cap, EVENT_REQUEST_SEQ_GC, 0, 0);
361     dtraceRequestSeqGc((EventCapNo)cap->no);
362 }
363
364 INLINE_HEADER void traceEventRequestParGc(Capability *cap STG_UNUSED)
365 {
366     traceSchedEvent(cap, EVENT_REQUEST_PAR_GC, 0, 0);
367     dtraceRequestParGc((EventCapNo)cap->no);
368 }
369
370 INLINE_HEADER void traceEventCreateSparkThread(Capability  *cap      STG_UNUSED, 
371                                                StgThreadID spark_tid STG_UNUSED)
372 {
373     traceSchedEvent(cap, EVENT_CREATE_SPARK_THREAD, 0, spark_tid);
374     dtraceCreateSparkThread((EventCapNo)cap->no, (EventThreadID)spark_tid);
375 }
376
377 // This applies only to dtrace as EVENT_STARTUP in the logging framework is
378 // handled specially in 'EventLog.c'.
379 //
380 INLINE_HEADER void dtraceEventStartup(void)
381 {
382 #ifdef THREADED_RTS
383     // XXX n_capabilities hasn't been initislised yet
384     dtraceStartup(RtsFlags.ParFlags.nNodes);
385 #else
386     dtraceStartup(1);
387 #endif
388 }
389
390 INLINE_HEADER void traceEventGcIdle(Capability *cap STG_UNUSED)
391 {
392     traceEvent(cap, EVENT_GC_IDLE);
393     dtraceGcIdle((EventCapNo)cap->no);
394 }
395
396 INLINE_HEADER void traceEventGcWork(Capability *cap STG_UNUSED)
397 {
398     traceEvent(cap, EVENT_GC_WORK);
399     dtraceGcWork((EventCapNo)cap->no);
400 }
401
402 INLINE_HEADER void traceEventGcDone(Capability *cap STG_UNUSED)
403 {
404     traceEvent(cap, EVENT_GC_DONE);
405     dtraceGcDone((EventCapNo)cap->no);
406 }
407
408 #include "EndPrivate.h"
409
410 #endif /* TRACE_H */