1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 2008-2009
5 * Support for fast binary event logging and user-space dtrace probes.
7 * ---------------------------------------------------------------------------*/
12 #include "rts/EventLogFormat.h"
13 #include "Capability.h"
16 #include "RtsProbes.h"
17 #endif /* defined(DTRACE) */
19 #include "BeginPrivate.h"
21 // -----------------------------------------------------------------------------
23 // -----------------------------------------------------------------------------
27 void initTracing (void);
28 void endTracing (void);
29 void freeTracing (void);
30 void resetTracing (void);
34 // -----------------------------------------------------------------------------
36 // -----------------------------------------------------------------------------
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;
44 extern int DEBUG_block_alloc;
45 extern int DEBUG_sanity;
46 extern int DEBUG_stable;
48 extern int DEBUG_prof;
49 extern int DEBUG_gran;
51 extern int DEBUG_linker;
52 extern int DEBUG_squeeze;
54 extern int DEBUG_sparks;
57 extern int TRACE_sched;
59 // -----------------------------------------------------------------------------
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.
67 // -----------------------------------------------------------------------------
70 void traceBegin (const char *str, ...);
77 * Record a scheduler event
79 #define traceSchedEvent(cap, tag, tso, other) \
80 if (RTS_UNLIKELY(TRACE_sched)) { \
81 traceSchedEvent_(cap, tag, tso, other, 0); \
84 #define traceSchedEvent2(cap, tag, tso, info1, info2) \
85 if (RTS_UNLIKELY(TRACE_sched)) { \
86 traceSchedEvent_(cap, tag, tso, info1, info2); \
89 void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
90 StgTSO *tso, StgWord info1, StgWord info2);
94 * Record a nullary event
96 #define traceEvent(cap, tag) \
97 if (RTS_UNLIKELY(TRACE_sched)) { \
98 traceEvent_(cap, tag); \
101 void traceEvent_ (Capability *cap, EventTypeNum tag);
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).
108 * Emit a trace message on a particular Capability
110 #define traceCap(class, cap, msg, ...) \
111 if (RTS_UNLIKELY(class)) { \
112 traceCap_(cap, msg, ##__VA_ARGS__); \
115 void traceCap_(Capability *cap, char *msg, ...);
118 * Emit a trace message
120 #define trace(class, msg, ...) \
121 if (RTS_UNLIKELY(class)) { \
122 trace_(msg, ##__VA_ARGS__); \
125 void trace_(char *msg, ...);
128 * A message or event emitted by the program
130 void traceUserMsg(Capability *cap, char *msg);
133 * Emit a debug message (only when DEBUG is defined)
136 #define debugTrace(class, msg, ...) \
137 if (RTS_UNLIKELY(class)) { \
138 trace_(msg, ##__VA_ARGS__); \
141 #define debugTrace(class, str, ...) /* nothing */
145 #define debugTraceCap(class, cap, msg, ...) \
146 if (RTS_UNLIKELY(class)) { \
147 traceCap_(cap, msg, ##__VA_ARGS__); \
150 #define debugTraceCap(class, cap, str, ...) /* nothing */
154 * Emit a message/event describing the state of a thread
156 #define traceThreadStatus(class, tso) \
157 if (RTS_UNLIKELY(class)) { \
158 traceThreadStatus_(tso); \
161 void traceThreadStatus_ (StgTSO *tso);
163 void traceEventStartup_ (int n_caps);
167 #define traceSchedEvent(cap, tag, tso, other) /* nothing */
168 #define traceSchedEvent2(cap, tag, tso, other, info) /* nothing */
169 #define traceEvent(cap, tag) /* nothing */
170 #define traceCap(class, cap, msg, ...) /* nothing */
171 #define trace(class, msg, ...) /* nothing */
172 #define debugTrace(class, str, ...) /* nothing */
173 #define debugTraceCap(class, cap, str, ...) /* nothing */
174 #define traceThreadStatus(class, tso) /* nothing */
175 #define traceEventStartup_(n_caps) /* nothing */
179 // If DTRACE is enabled, but neither DEBUG nor TRACING, we need a C land
180 // wrapper for the user-msg probe (as we can't expand that in PrimOps.cmm)
182 #if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)
184 void dtraceUserMsgWrapper(Capability *cap, char *msg);
186 #endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */
188 // -----------------------------------------------------------------------------
189 // Aliases for static dtrace probes if dtrace is available
190 // -----------------------------------------------------------------------------
194 #define dtraceCreateThread(cap, tid) \
195 HASKELLEVENT_CREATE_THREAD(cap, tid)
196 #define dtraceRunThread(cap, tid) \
197 HASKELLEVENT_RUN_THREAD(cap, tid)
198 #define dtraceStopThread(cap, tid, status, info) \
199 HASKELLEVENT_STOP_THREAD(cap, tid, status, info)
200 #define dtraceThreadRunnable(cap, tid) \
201 HASKELLEVENT_THREAD_RUNNABLE(cap, tid)
202 #define dtraceMigrateThread(cap, tid, new_cap) \
203 HASKELLEVENT_MIGRATE_THREAD(cap, tid, new_cap)
204 #define dtraceRunSpark(cap, tid) \
205 HASKELLEVENT_RUN_SPARK(cap, tid)
206 #define dtraceStealSpark(cap, tid, victim_cap) \
207 HASKELLEVENT_STEAL_SPARK(cap, tid, victim_cap)
208 #define dtraceShutdown(cap) \
209 HASKELLEVENT_SHUTDOWN(cap)
210 #define dtraceThreadWakeup(cap, tid, other_cap) \
211 HASKELLEVENT_THREAD_WAKEUP(cap, tid, other_cap)
212 #define dtraceGcStart(cap) \
213 HASKELLEVENT_GC_START(cap)
214 #define dtraceGcEnd(cap) \
215 HASKELLEVENT_GC_END(cap)
216 #define dtraceRequestSeqGc(cap) \
217 HASKELLEVENT_REQUEST_SEQ_GC(cap)
218 #define dtraceRequestParGc(cap) \
219 HASKELLEVENT_REQUEST_PAR_GC(cap)
220 #define dtraceCreateSparkThread(cap, spark_tid) \
221 HASKELLEVENT_CREATE_SPARK_THREAD(cap, spark_tid)
222 #define dtraceStartup(num_caps) \
223 HASKELLEVENT_STARTUP(num_caps)
224 #define dtraceUserMsg(cap, msg) \
225 HASKELLEVENT_USER_MSG(cap, msg)
226 #define dtraceGcIdle(cap) \
227 HASKELLEVENT_GC_IDLE(cap)
228 #define dtraceGcWork(cap) \
229 HASKELLEVENT_GC_WORK(cap)
230 #define dtraceGcDone(cap) \
231 HASKELLEVENT_GC_DONE(cap)
233 #else /* !defined(DTRACE) */
235 #define dtraceCreateThread(cap, tid) /* nothing */
236 #define dtraceRunThread(cap, tid) /* nothing */
237 #define dtraceStopThread(cap, tid, status, info) /* nothing */
238 #define dtraceThreadRunnable(cap, tid) /* nothing */
239 #define dtraceMigrateThread(cap, tid, new_cap) /* nothing */
240 #define dtraceRunSpark(cap, tid) /* nothing */
241 #define dtraceStealSpark(cap, tid, victim_cap) /* nothing */
242 #define dtraceShutdown(cap) /* nothing */
243 #define dtraceThreadWakeup(cap, tid, other_cap) /* nothing */
244 #define dtraceGcStart(cap) /* nothing */
245 #define dtraceGcEnd(cap) /* nothing */
246 #define dtraceRequestSeqGc(cap) /* nothing */
247 #define dtraceRequestParGc(cap) /* nothing */
248 #define dtraceCreateSparkThread(cap, spark_tid) /* nothing */
249 #define dtraceStartup(num_caps) /* nothing */
250 #define dtraceUserMsg(cap, msg) /* nothing */
251 #define dtraceGcIdle(cap) /* nothing */
252 #define dtraceGcWork(cap) /* nothing */
253 #define dtraceGcDone(cap) /* nothing */
257 // -----------------------------------------------------------------------------
258 // Trace probes dispatching to various tracing frameworks
260 // In order to avoid accumulating multiple calls to tracing calls at trace
261 // points, we define inline probe functions that contain the various
264 // Dtrace - dtrace probes are unconditionally added as probe activation is
265 // handled by the dtrace component of the kernel, and inactive probes are
266 // very cheap — usually, one no-op. Consequently, dtrace can be used with
267 // all flavours of the RTS. In addition, we still support logging events to
268 // a file, even in the presence of dtrace. This is, eg, useful when tracing
269 // on a server, but browsing trace information with ThreadScope on a local
272 // -----------------------------------------------------------------------------
274 INLINE_HEADER void traceEventCreateThread(Capability *cap STG_UNUSED,
275 StgTSO *tso STG_UNUSED)
277 traceSchedEvent(cap, EVENT_CREATE_THREAD, tso, tso->stackobj->stack_size);
278 dtraceCreateThread((EventCapNo)cap->no, (EventThreadID)tso->id);
281 INLINE_HEADER void traceEventRunThread(Capability *cap STG_UNUSED,
282 StgTSO *tso STG_UNUSED)
284 traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
285 dtraceRunThread((EventCapNo)cap->no, (EventThreadID)tso->id);
288 INLINE_HEADER void traceEventStopThread(Capability *cap STG_UNUSED,
289 StgTSO *tso STG_UNUSED,
290 StgThreadReturnCode status STG_UNUSED,
291 StgWord32 info STG_UNUSED)
293 traceSchedEvent2(cap, EVENT_STOP_THREAD, tso, status, info);
294 dtraceStopThread((EventCapNo)cap->no, (EventThreadID)tso->id,
295 (EventThreadStatus)status, (EventThreadID)info);
298 // needs to be EXTERN_INLINE as it is used in another EXTERN_INLINE function
299 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
300 StgTSO *tso STG_UNUSED);
302 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
303 StgTSO *tso STG_UNUSED)
305 traceSchedEvent(cap, EVENT_THREAD_RUNNABLE, tso, 0);
306 dtraceThreadRunnable((EventCapNo)cap->no, (EventThreadID)tso->id);
309 INLINE_HEADER void traceEventMigrateThread(Capability *cap STG_UNUSED,
310 StgTSO *tso STG_UNUSED,
311 nat new_cap STG_UNUSED)
313 traceSchedEvent(cap, EVENT_MIGRATE_THREAD, tso, new_cap);
314 dtraceMigrateThread((EventCapNo)cap->no, (EventThreadID)tso->id,
315 (EventCapNo)new_cap);
318 INLINE_HEADER void traceEventRunSpark(Capability *cap STG_UNUSED,
319 StgTSO *tso STG_UNUSED)
321 traceSchedEvent(cap, EVENT_RUN_SPARK, tso, 0);
322 dtraceRunSpark((EventCapNo)cap->no, (EventThreadID)tso->id);
325 INLINE_HEADER void traceEventStealSpark(Capability *cap STG_UNUSED,
326 StgTSO *tso STG_UNUSED,
327 nat victim_cap STG_UNUSED)
329 traceSchedEvent(cap, EVENT_STEAL_SPARK, tso, victim_cap);
330 dtraceStealSpark((EventCapNo)cap->no, (EventThreadID)tso->id,
331 (EventCapNo)victim_cap);
334 INLINE_HEADER void traceEventShutdown(Capability *cap STG_UNUSED)
336 traceSchedEvent(cap, EVENT_SHUTDOWN, 0, 0);
337 dtraceShutdown((EventCapNo)cap->no);
340 INLINE_HEADER void traceEventThreadWakeup(Capability *cap STG_UNUSED,
341 StgTSO *tso STG_UNUSED,
342 nat other_cap STG_UNUSED)
344 traceSchedEvent(cap, EVENT_THREAD_WAKEUP, tso, other_cap);
345 dtraceThreadWakeup((EventCapNo)cap->no, (EventThreadID)tso->id,
346 (EventCapNo)other_cap);
349 INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
351 traceSchedEvent(cap, EVENT_GC_START, 0, 0);
352 dtraceGcStart((EventCapNo)cap->no);
355 INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
357 traceSchedEvent(cap, EVENT_GC_END, 0, 0);
358 dtraceGcEnd((EventCapNo)cap->no);
361 INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
363 traceSchedEvent(cap, EVENT_REQUEST_SEQ_GC, 0, 0);
364 dtraceRequestSeqGc((EventCapNo)cap->no);
367 INLINE_HEADER void traceEventRequestParGc(Capability *cap STG_UNUSED)
369 traceSchedEvent(cap, EVENT_REQUEST_PAR_GC, 0, 0);
370 dtraceRequestParGc((EventCapNo)cap->no);
373 INLINE_HEADER void traceEventCreateSparkThread(Capability *cap STG_UNUSED,
374 StgThreadID spark_tid STG_UNUSED)
376 traceSchedEvent(cap, EVENT_CREATE_SPARK_THREAD, 0, spark_tid);
377 dtraceCreateSparkThread((EventCapNo)cap->no, (EventThreadID)spark_tid);
380 INLINE_HEADER void traceEventStartup(void)
384 // XXX n_capabilities hasn't been initislised yet
385 n_caps = RtsFlags.ParFlags.nNodes;
390 traceEventStartup_(n_caps);
391 dtraceStartup(n_caps);
394 INLINE_HEADER void traceEventGcIdle(Capability *cap STG_UNUSED)
396 traceEvent(cap, EVENT_GC_IDLE);
397 dtraceGcIdle((EventCapNo)cap->no);
400 INLINE_HEADER void traceEventGcWork(Capability *cap STG_UNUSED)
402 traceEvent(cap, EVENT_GC_WORK);
403 dtraceGcWork((EventCapNo)cap->no);
406 INLINE_HEADER void traceEventGcDone(Capability *cap STG_UNUSED)
408 traceEvent(cap, EVENT_GC_DONE);
409 dtraceGcDone((EventCapNo)cap->no);
412 #include "EndPrivate.h"