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);
33 // -----------------------------------------------------------------------------
35 // -----------------------------------------------------------------------------
37 // debugging flags, set with +RTS -D<something>
38 extern int DEBUG_sched;
39 extern int DEBUG_interp;
40 extern int DEBUG_weak;
41 extern int DEBUG_gccafs;
43 extern int DEBUG_block_alloc;
44 extern int DEBUG_sanity;
45 extern int DEBUG_stable;
47 extern int DEBUG_prof;
48 extern int DEBUG_gran;
50 extern int DEBUG_linker;
51 extern int DEBUG_squeeze;
53 extern int DEBUG_sparks;
56 extern int TRACE_sched;
58 // -----------------------------------------------------------------------------
61 // We use macros rather than inline functions deliberately. We want
62 // the not-taken case to be as efficient as possible, a simple
63 // test-and-jump, and with inline functions gcc seemed to move some of
64 // the instructions from the branch up before the test.
66 // -----------------------------------------------------------------------------
69 void traceBegin (const char *str, ...);
76 * Record a scheduler event
78 #define traceSchedEvent(cap, tag, tso, other) \
79 if (RTS_UNLIKELY(TRACE_sched)) { \
80 traceSchedEvent_(cap, tag, tso, other); \
83 void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
84 StgTSO *tso, StgWord64 other);
88 * Record a nullary event
90 #define traceEvent(cap, tag) \
91 if (RTS_UNLIKELY(TRACE_sched)) { \
92 traceEvent_(cap, tag); \
95 void traceEvent_ (Capability *cap, EventTypeNum tag);
97 // variadic macros are C99, and supported by gcc. However, the
98 // ##__VA_ARGS syntax is a gcc extension, which allows the variable
99 // argument list to be empty (see gcc docs for details).
102 * Emit a trace message on a particular Capability
104 #define traceCap(class, cap, msg, ...) \
105 if (RTS_UNLIKELY(class)) { \
106 traceCap_(cap, msg, ##__VA_ARGS__); \
109 void traceCap_(Capability *cap, char *msg, ...);
112 * Emit a trace message
114 #define trace(class, msg, ...) \
115 if (RTS_UNLIKELY(class)) { \
116 trace_(msg, ##__VA_ARGS__); \
119 void trace_(char *msg, ...);
122 * A message or event emitted by the program
124 void traceUserMsg(Capability *cap, char *msg);
127 * Emit a debug message (only when DEBUG is defined)
130 #define debugTrace(class, msg, ...) \
131 if (RTS_UNLIKELY(class)) { \
132 trace_(msg, ##__VA_ARGS__); \
135 #define debugTrace(class, str, ...) /* nothing */
139 #define debugTraceCap(class, cap, msg, ...) \
140 if (RTS_UNLIKELY(class)) { \
141 traceCap_(cap, msg, ##__VA_ARGS__); \
144 #define debugTraceCap(class, cap, str, ...) /* nothing */
148 * Emit a message/event describing the state of a thread
150 #define traceThreadStatus(class, tso) \
151 if (RTS_UNLIKELY(class)) { \
152 traceThreadStatus_(tso); \
155 void traceThreadStatus_ (StgTSO *tso);
159 #define traceSchedEvent(cap, tag, tso, other) /* nothing */
160 #define traceEvent(cap, tag) /* nothing */
161 #define traceCap(class, cap, msg, ...) /* nothing */
162 #define trace(class, msg, ...) /* nothing */
163 #define debugTrace(class, str, ...) /* nothing */
164 #define debugTraceCap(class, cap, str, ...) /* nothing */
165 #define traceThreadStatus(class, tso) /* nothing */
169 // If DTRACE is enabled, but neither DEBUG nor TRACING, we need a C land
170 // wrapper for the user-msg probe (as we can't expand that in PrimOps.cmm)
172 #if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)
174 void dtraceUserMsgWrapper(Capability *cap, char *msg);
176 #endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */
178 // -----------------------------------------------------------------------------
179 // Aliases for static dtrace probes if dtrace is available
180 // -----------------------------------------------------------------------------
184 #define dtraceCreateThread(cap, tid) \
185 HASKELLEVENT_CREATE_THREAD(cap, tid)
186 #define dtraceRunThread(cap, tid) \
187 HASKELLEVENT_RUN_THREAD(cap, tid)
188 #define dtraceStopThread(cap, tid, status) \
189 HASKELLEVENT_STOP_THREAD(cap, tid, status)
190 #define dtraceThreadRunnable(cap, tid) \
191 HASKELLEVENT_THREAD_RUNNABLE(cap, tid)
192 #define dtraceMigrateThread(cap, tid, new_cap) \
193 HASKELLEVENT_MIGRATE_THREAD(cap, tid, new_cap)
194 #define dtraceRunSpark(cap, tid) \
195 HASKELLEVENT_RUN_SPARK(cap, tid)
196 #define dtraceStealSpark(cap, tid, victim_cap) \
197 HASKELLEVENT_STEAL_SPARK(cap, tid, victim_cap)
198 #define dtraceShutdown(cap) \
199 HASKELLEVENT_SHUTDOWN(cap)
200 #define dtraceThreadWakeup(cap, tid, other_cap) \
201 HASKELLEVENT_THREAD_WAKEUP(cap, tid, other_cap)
202 #define dtraceGcStart(cap) \
203 HASKELLEVENT_GC_START(cap)
204 #define dtraceGcEnd(cap) \
205 HASKELLEVENT_GC_END(cap)
206 #define dtraceRequestSeqGc(cap) \
207 HASKELLEVENT_REQUEST_SEQ_GC(cap)
208 #define dtraceRequestParGc(cap) \
209 HASKELLEVENT_REQUEST_PAR_GC(cap)
210 #define dtraceCreateSparkThread(cap, spark_tid) \
211 HASKELLEVENT_CREATE_SPARK_THREAD(cap, spark_tid)
212 #define dtraceStartup(num_caps) \
213 HASKELLEVENT_STARTUP(num_caps)
214 #define dtraceUserMsg(cap, msg) \
215 HASKELLEVENT_USER_MSG(cap, msg)
216 #define dtraceGcIdle(cap) \
217 HASKELLEVENT_GC_IDLE(cap)
218 #define dtraceGcWork(cap) \
219 HASKELLEVENT_GC_WORK(cap)
220 #define dtraceGcDone(cap) \
221 HASKELLEVENT_GC_DONE(cap)
223 #else /* !defined(DTRACE) */
225 #define dtraceCreateThread(cap, tid) /* nothing */
226 #define dtraceRunThread(cap, tid) /* nothing */
227 #define dtraceStopThread(cap, tid, status) /* nothing */
228 #define dtraceThreadRunnable(cap, tid) /* nothing */
229 #define dtraceMigrateThread(cap, tid, new_cap) /* nothing */
230 #define dtraceRunSpark(cap, tid) /* nothing */
231 #define dtraceStealSpark(cap, tid, victim_cap) /* nothing */
232 #define dtraceShutdown(cap) /* nothing */
233 #define dtraceThreadWakeup(cap, tid, other_cap) /* nothing */
234 #define dtraceGcStart(cap) /* nothing */
235 #define dtraceGcEnd(cap) /* nothing */
236 #define dtraceRequestSeqGc(cap) /* nothing */
237 #define dtraceRequestParGc(cap) /* nothing */
238 #define dtraceCreateSparkThread(cap, spark_tid) /* nothing */
239 #define dtraceStartup(num_caps) /* nothing */
240 #define dtraceUserMsg(cap, msg) /* nothing */
241 #define dtraceGcIdle(cap) /* nothing */
242 #define dtraceGcWork(cap) /* nothing */
243 #define dtraceGcDone(cap) /* nothing */
247 // -----------------------------------------------------------------------------
248 // Trace probes dispatching to various tracing frameworks
250 // In order to avoid accumulating multiple calls to tracing calls at trace
251 // points, we define inline probe functions that contain the various
254 // Dtrace - dtrace probes are unconditionally added as probe activation is
255 // handled by the dtrace component of the kernel, and inactive probes are
256 // very cheap — usually, one no-op. Consequently, dtrace can be used with
257 // all flavours of the RTS. In addition, we still support logging events to
258 // a file, even in the presence of dtrace. This is, eg, useful when tracing
259 // on a server, but browsing trace information with ThreadScope on a local
262 // -----------------------------------------------------------------------------
264 INLINE_HEADER void traceEventCreateThread(Capability *cap STG_UNUSED,
265 StgTSO *tso STG_UNUSED)
267 traceSchedEvent(cap, EVENT_CREATE_THREAD, tso, tso->stack_size);
268 dtraceCreateThread((EventCapNo)cap->no, (EventThreadID)tso->id);
271 INLINE_HEADER void traceEventRunThread(Capability *cap STG_UNUSED,
272 StgTSO *tso STG_UNUSED)
274 traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
275 dtraceRunThread((EventCapNo)cap->no, (EventThreadID)tso->id);
278 INLINE_HEADER void traceEventStopThread(Capability *cap STG_UNUSED,
279 StgTSO *tso STG_UNUSED,
280 StgThreadReturnCode status STG_UNUSED)
282 traceSchedEvent(cap, EVENT_STOP_THREAD, tso, status);
283 dtraceStopThread((EventCapNo)cap->no, (EventThreadID)tso->id,
284 (EventThreadStatus)status);
287 // needs to be EXTERN_INLINE as it is used in another EXTERN_INLINE function
288 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
289 StgTSO *tso STG_UNUSED);
291 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
292 StgTSO *tso STG_UNUSED)
294 traceSchedEvent(cap, EVENT_THREAD_RUNNABLE, tso, 0);
295 dtraceThreadRunnable((EventCapNo)cap->no, (EventThreadID)tso->id);
298 INLINE_HEADER void traceEventMigrateThread(Capability *cap STG_UNUSED,
299 StgTSO *tso STG_UNUSED,
300 nat new_cap STG_UNUSED)
302 traceSchedEvent(cap, EVENT_MIGRATE_THREAD, tso, new_cap);
303 dtraceMigrateThread((EventCapNo)cap->no, (EventThreadID)tso->id,
304 (EventCapNo)new_cap);
307 INLINE_HEADER void traceEventRunSpark(Capability *cap STG_UNUSED,
308 StgTSO *tso STG_UNUSED)
310 traceSchedEvent(cap, EVENT_RUN_SPARK, tso, 0);
311 dtraceRunSpark((EventCapNo)cap->no, (EventThreadID)tso->id);
314 INLINE_HEADER void traceEventStealSpark(Capability *cap STG_UNUSED,
315 StgTSO *tso STG_UNUSED,
316 nat victim_cap STG_UNUSED)
318 traceSchedEvent(cap, EVENT_STEAL_SPARK, tso, victim_cap);
319 dtraceStealSpark((EventCapNo)cap->no, (EventThreadID)tso->id,
320 (EventCapNo)victim_cap);
323 INLINE_HEADER void traceEventShutdown(Capability *cap STG_UNUSED)
325 traceSchedEvent(cap, EVENT_SHUTDOWN, 0, 0);
326 dtraceShutdown((EventCapNo)cap->no);
329 INLINE_HEADER void traceEventThreadWakeup(Capability *cap STG_UNUSED,
330 StgTSO *tso STG_UNUSED,
331 nat other_cap STG_UNUSED)
333 traceSchedEvent(cap, EVENT_THREAD_WAKEUP, tso, other_cap);
334 dtraceThreadWakeup((EventCapNo)cap->no, (EventThreadID)tso->id,
335 (EventCapNo)other_cap);
338 INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
340 traceSchedEvent(cap, EVENT_GC_START, 0, 0);
341 dtraceGcStart((EventCapNo)cap->no);
344 INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
346 traceSchedEvent(cap, EVENT_GC_END, 0, 0);
347 dtraceGcEnd((EventCapNo)cap->no);
350 INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
352 traceSchedEvent(cap, EVENT_REQUEST_SEQ_GC, 0, 0);
353 dtraceRequestSeqGc((EventCapNo)cap->no);
356 INLINE_HEADER void traceEventRequestParGc(Capability *cap STG_UNUSED)
358 traceSchedEvent(cap, EVENT_REQUEST_PAR_GC, 0, 0);
359 dtraceRequestParGc((EventCapNo)cap->no);
362 INLINE_HEADER void traceEventCreateSparkThread(Capability *cap STG_UNUSED,
363 StgThreadID spark_tid STG_UNUSED)
365 traceSchedEvent(cap, EVENT_CREATE_SPARK_THREAD, 0, spark_tid);
366 dtraceCreateSparkThread((EventCapNo)cap->no, (EventThreadID)spark_tid);
369 // This applies only to dtrace as EVENT_STARTUP in the logging framework is
370 // handled specially in 'EventLog.c'.
372 INLINE_HEADER void dtraceEventStartup(void)
375 // XXX n_capabilities hasn't been initislised yet
376 dtraceStartup(RtsFlags.ParFlags.nNodes);
382 INLINE_HEADER void traceEventGcIdle(Capability *cap STG_UNUSED)
384 traceEvent(cap, EVENT_GC_IDLE);
385 dtraceGcIdle((EventCapNo)cap->no);
388 INLINE_HEADER void traceEventGcWork(Capability *cap STG_UNUSED)
390 traceEvent(cap, EVENT_GC_WORK);
391 dtraceGcWork((EventCapNo)cap->no);
394 INLINE_HEADER void traceEventGcDone(Capability *cap STG_UNUSED)
396 traceEvent(cap, EVENT_GC_DONE);
397 dtraceGcDone((EventCapNo)cap->no);
400 #include "EndPrivate.h"