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) */
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 * Emit a message/event describing the state of a thread
141 #define traceThreadStatus(class, tso) \
142 if (RTS_UNLIKELY(class)) { \
143 traceThreadStatus_(tso); \
146 void traceThreadStatus_ (StgTSO *tso);
150 #define traceSchedEvent(cap, tag, tso, other) /* nothing */
151 #define traceEvent(cap, tag) /* nothing */
152 #define traceCap(class, cap, msg, ...) /* nothing */
153 #define trace(class, msg, ...) /* nothing */
154 #define debugTrace(class, str, ...) /* nothing */
155 #define traceThreadStatus(class, tso) /* nothing */
159 // If DTRACE is enabled, but neither DEBUG nor TRACING, we need a C land
160 // wrapper for the user-msg probe (as we can't expand that in PrimOps.cmm)
162 #if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)
164 void dtraceUserMsgWrapper(Capability *cap, char *msg);
166 #endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */
168 // -----------------------------------------------------------------------------
169 // Aliases for static dtrace probes if dtrace is available
170 // -----------------------------------------------------------------------------
174 #define dtraceCreateThread(cap, tid) \
175 HASKELLEVENT_CREATE_THREAD(cap, tid)
176 #define dtraceRunThread(cap, tid) \
177 HASKELLEVENT_RUN_THREAD(cap, tid)
178 #define dtraceStopThread(cap, tid, status) \
179 HASKELLEVENT_STOP_THREAD(cap, tid, status)
180 #define dtraceThreadRunnable(cap, tid) \
181 HASKELLEVENT_THREAD_RUNNABLE(cap, tid)
182 #define dtraceMigrateThread(cap, tid, new_cap) \
183 HASKELLEVENT_MIGRATE_THREAD(cap, tid, new_cap)
184 #define dtraceRunSpark(cap, tid) \
185 HASKELLEVENT_RUN_SPARK(cap, tid)
186 #define dtraceStealSpark(cap, tid, victim_cap) \
187 HASKELLEVENT_STEAL_SPARK(cap, tid, victim_cap)
188 #define dtraceShutdown(cap) \
189 HASKELLEVENT_SHUTDOWN(cap)
190 #define dtraceThreadWakeup(cap, tid, other_cap) \
191 HASKELLEVENT_THREAD_WAKEUP(cap, tid, other_cap)
192 #define dtraceGcStart(cap) \
193 HASKELLEVENT_GC_START(cap)
194 #define dtraceGcEnd(cap) \
195 HASKELLEVENT_GC_END(cap)
196 #define dtraceRequestSeqGc(cap) \
197 HASKELLEVENT_REQUEST_SEQ_GC(cap)
198 #define dtraceRequestParGc(cap) \
199 HASKELLEVENT_REQUEST_PAR_GC(cap)
200 #define dtraceCreateSparkThread(cap, spark_tid) \
201 HASKELLEVENT_CREATE_SPARK_THREAD(cap, spark_tid)
202 #define dtraceStartup(num_caps) \
203 HASKELLEVENT_STARTUP(num_caps)
204 #define dtraceUserMsg(cap, msg) \
205 HASKELLEVENT_USER_MSG(cap, msg)
206 #define dtraceGcIdle(cap) \
207 HASKELLEVENT_GC_IDLE(cap)
208 #define dtraceGcWork(cap) \
209 HASKELLEVENT_GC_WORK(cap)
210 #define dtraceGcDone(cap) \
211 HASKELLEVENT_GC_DONE(cap)
213 #else /* !defined(DTRACE) */
215 #define dtraceCreateThread(cap, tid) /* nothing */
216 #define dtraceRunThread(cap, tid) /* nothing */
217 #define dtraceStopThread(cap, tid, status) /* nothing */
218 #define dtraceThreadRunnable(cap, tid) /* nothing */
219 #define dtraceMigrateThread(cap, tid, new_cap) /* nothing */
220 #define dtraceRunSpark(cap, tid) /* nothing */
221 #define dtraceStealSpark(cap, tid, victim_cap) /* nothing */
222 #define dtraceShutdown(cap) /* nothing */
223 #define dtraceThreadWakeup(cap, tid, other_cap) /* nothing */
224 #define dtraceGcStart(cap) /* nothing */
225 #define dtraceGcEnd(cap) /* nothing */
226 #define dtraceRequestSeqGc(cap) /* nothing */
227 #define dtraceRequestParGc(cap) /* nothing */
228 #define dtraceCreateSparkThread(cap, spark_tid) /* nothing */
229 #define dtraceStartup(num_caps) /* nothing */
230 #define dtraceUserMsg(cap, msg) /* nothing */
231 #define dtraceGcIdle(cap) /* nothing */
232 #define dtraceGcWork(cap) /* nothing */
233 #define dtraceGcDone(cap) /* nothing */
237 // -----------------------------------------------------------------------------
238 // Trace probes dispatching to various tracing frameworks
240 // In order to avoid accumulating multiple calls to tracing calls at trace
241 // points, we define inline probe functions that contain the various
244 // Dtrace - dtrace probes are unconditionally added as probe activation is
245 // handled by the dtrace component of the kernel, and inactive probes are
246 // very cheap — usually, one no-op. Consequently, dtrace can be used with
247 // all flavours of the RTS. In addition, we still support logging events to
248 // a file, even in the presence of dtrace. This is, eg, useful when tracing
249 // on a server, but browsing trace information with ThreadScope on a local
252 // -----------------------------------------------------------------------------
254 INLINE_HEADER void traceEventCreateThread(Capability *cap STG_UNUSED,
255 StgTSO *tso STG_UNUSED)
257 traceSchedEvent(cap, EVENT_CREATE_THREAD, tso, tso->stack_size);
258 dtraceCreateThread((EventCapNo)cap->no, (EventThreadID)tso->id);
261 INLINE_HEADER void traceEventRunThread(Capability *cap STG_UNUSED,
262 StgTSO *tso STG_UNUSED)
264 traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
265 dtraceRunThread((EventCapNo)cap->no, (EventThreadID)tso->id);
268 INLINE_HEADER void traceEventStopThread(Capability *cap STG_UNUSED,
269 StgTSO *tso STG_UNUSED,
270 StgThreadReturnCode status STG_UNUSED)
272 traceSchedEvent(cap, EVENT_STOP_THREAD, tso, status);
273 dtraceStopThread((EventCapNo)cap->no, (EventThreadID)tso->id,
274 (EventThreadStatus)status);
277 // needs to be EXTERN_INLINE as it is used in another EXTERN_INLINE function
278 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
279 StgTSO *tso STG_UNUSED);
281 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
282 StgTSO *tso STG_UNUSED)
284 traceSchedEvent(cap, EVENT_THREAD_RUNNABLE, tso, 0);
285 dtraceThreadRunnable((EventCapNo)cap->no, (EventThreadID)tso->id);
288 INLINE_HEADER void traceEventMigrateThread(Capability *cap STG_UNUSED,
289 StgTSO *tso STG_UNUSED,
290 nat new_cap STG_UNUSED)
292 traceSchedEvent(cap, EVENT_MIGRATE_THREAD, tso, new_cap);
293 dtraceMigrateThread((EventCapNo)cap->no, (EventThreadID)tso->id,
294 (EventCapNo)new_cap);
297 INLINE_HEADER void traceEventRunSpark(Capability *cap STG_UNUSED,
298 StgTSO *tso STG_UNUSED)
300 traceSchedEvent(cap, EVENT_RUN_SPARK, tso, 0);
301 dtraceRunSpark((EventCapNo)cap->no, (EventThreadID)tso->id);
304 INLINE_HEADER void traceEventStealSpark(Capability *cap STG_UNUSED,
305 StgTSO *tso STG_UNUSED,
306 nat victim_cap STG_UNUSED)
308 traceSchedEvent(cap, EVENT_STEAL_SPARK, tso, victim_cap);
309 dtraceStealSpark((EventCapNo)cap->no, (EventThreadID)tso->id,
310 (EventCapNo)victim_cap);
313 INLINE_HEADER void traceEventShutdown(Capability *cap STG_UNUSED)
315 traceSchedEvent(cap, EVENT_SHUTDOWN, 0, 0);
316 dtraceShutdown((EventCapNo)cap->no);
319 INLINE_HEADER void traceEventThreadWakeup(Capability *cap STG_UNUSED,
320 StgTSO *tso STG_UNUSED,
321 nat other_cap STG_UNUSED)
323 traceSchedEvent(cap, EVENT_THREAD_WAKEUP, tso, other_cap);
324 dtraceThreadWakeup((EventCapNo)cap->no, (EventThreadID)tso->id,
325 (EventCapNo)other_cap);
328 INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
330 traceSchedEvent(cap, EVENT_GC_START, 0, 0);
331 dtraceGcStart((EventCapNo)cap->no);
334 INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
336 traceSchedEvent(cap, EVENT_GC_END, 0, 0);
337 dtraceGcEnd((EventCapNo)cap->no);
340 INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
342 traceSchedEvent(cap, EVENT_REQUEST_SEQ_GC, 0, 0);
343 dtraceRequestSeqGc((EventCapNo)cap->no);
346 INLINE_HEADER void traceEventRequestParGc(Capability *cap STG_UNUSED)
348 traceSchedEvent(cap, EVENT_REQUEST_PAR_GC, 0, 0);
349 dtraceRequestParGc((EventCapNo)cap->no);
352 INLINE_HEADER void traceEventCreateSparkThread(Capability *cap STG_UNUSED,
353 StgThreadID spark_tid STG_UNUSED)
355 traceSchedEvent(cap, EVENT_CREATE_SPARK_THREAD, 0, spark_tid);
356 dtraceCreateSparkThread((EventCapNo)cap->no, (EventThreadID)spark_tid);
359 // This applies only to dtrace as EVENT_STARTUP in the logging framework is
360 // handled specially in 'EventLog.c'.
362 INLINE_HEADER void dtraceEventStartup(void)
365 // XXX n_capabilities hasn't been initislised yet
366 dtraceStartup(RtsFlags.ParFlags.nNodes);
372 INLINE_HEADER void traceEventGcIdle(Capability *cap STG_UNUSED)
374 traceEvent(cap, EVENT_GC_IDLE);
375 dtraceGcIdle((EventCapNo)cap->no);
378 INLINE_HEADER void traceEventGcWork(Capability *cap STG_UNUSED)
380 traceEvent(cap, EVENT_GC_WORK);
381 dtraceGcWork((EventCapNo)cap->no);
384 INLINE_HEADER void traceEventGcDone(Capability *cap STG_UNUSED)
386 traceEvent(cap, EVENT_GC_DONE);
387 dtraceGcDone((EventCapNo)cap->no);