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);
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 */
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)
179 #if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)
181 void dtraceUserMsgWrapper(Capability *cap, char *msg);
183 #endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */
185 // -----------------------------------------------------------------------------
186 // Aliases for static dtrace probes if dtrace is available
187 // -----------------------------------------------------------------------------
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)
230 #else /* !defined(DTRACE) */
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 */
254 // -----------------------------------------------------------------------------
255 // Trace probes dispatching to various tracing frameworks
257 // In order to avoid accumulating multiple calls to tracing calls at trace
258 // points, we define inline probe functions that contain the various
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
269 // -----------------------------------------------------------------------------
271 INLINE_HEADER void traceEventCreateThread(Capability *cap STG_UNUSED,
272 StgTSO *tso STG_UNUSED)
274 traceSchedEvent(cap, EVENT_CREATE_THREAD, tso, tso->stackobj->stack_size);
275 dtraceCreateThread((EventCapNo)cap->no, (EventThreadID)tso->id);
278 INLINE_HEADER void traceEventRunThread(Capability *cap STG_UNUSED,
279 StgTSO *tso STG_UNUSED)
281 traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
282 dtraceRunThread((EventCapNo)cap->no, (EventThreadID)tso->id);
285 INLINE_HEADER void traceEventStopThread(Capability *cap STG_UNUSED,
286 StgTSO *tso STG_UNUSED,
287 StgThreadReturnCode status STG_UNUSED,
288 StgWord32 info STG_UNUSED)
290 traceSchedEvent2(cap, EVENT_STOP_THREAD, tso, status, info);
291 dtraceStopThread((EventCapNo)cap->no, (EventThreadID)tso->id,
292 (EventThreadStatus)status, (EventThreadID)info);
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);
299 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
300 StgTSO *tso STG_UNUSED)
302 traceSchedEvent(cap, EVENT_THREAD_RUNNABLE, tso, 0);
303 dtraceThreadRunnable((EventCapNo)cap->no, (EventThreadID)tso->id);
306 INLINE_HEADER void traceEventMigrateThread(Capability *cap STG_UNUSED,
307 StgTSO *tso STG_UNUSED,
308 nat new_cap STG_UNUSED)
310 traceSchedEvent(cap, EVENT_MIGRATE_THREAD, tso, new_cap);
311 dtraceMigrateThread((EventCapNo)cap->no, (EventThreadID)tso->id,
312 (EventCapNo)new_cap);
315 INLINE_HEADER void traceEventRunSpark(Capability *cap STG_UNUSED,
316 StgTSO *tso STG_UNUSED)
318 traceSchedEvent(cap, EVENT_RUN_SPARK, tso, 0);
319 dtraceRunSpark((EventCapNo)cap->no, (EventThreadID)tso->id);
322 INLINE_HEADER void traceEventStealSpark(Capability *cap STG_UNUSED,
323 StgTSO *tso STG_UNUSED,
324 nat victim_cap STG_UNUSED)
326 traceSchedEvent(cap, EVENT_STEAL_SPARK, tso, victim_cap);
327 dtraceStealSpark((EventCapNo)cap->no, (EventThreadID)tso->id,
328 (EventCapNo)victim_cap);
331 INLINE_HEADER void traceEventShutdown(Capability *cap STG_UNUSED)
333 traceSchedEvent(cap, EVENT_SHUTDOWN, 0, 0);
334 dtraceShutdown((EventCapNo)cap->no);
337 INLINE_HEADER void traceEventThreadWakeup(Capability *cap STG_UNUSED,
338 StgTSO *tso STG_UNUSED,
339 nat other_cap STG_UNUSED)
341 traceSchedEvent(cap, EVENT_THREAD_WAKEUP, tso, other_cap);
342 dtraceThreadWakeup((EventCapNo)cap->no, (EventThreadID)tso->id,
343 (EventCapNo)other_cap);
346 INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
348 traceSchedEvent(cap, EVENT_GC_START, 0, 0);
349 dtraceGcStart((EventCapNo)cap->no);
352 INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
354 traceSchedEvent(cap, EVENT_GC_END, 0, 0);
355 dtraceGcEnd((EventCapNo)cap->no);
358 INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
360 traceSchedEvent(cap, EVENT_REQUEST_SEQ_GC, 0, 0);
361 dtraceRequestSeqGc((EventCapNo)cap->no);
364 INLINE_HEADER void traceEventRequestParGc(Capability *cap STG_UNUSED)
366 traceSchedEvent(cap, EVENT_REQUEST_PAR_GC, 0, 0);
367 dtraceRequestParGc((EventCapNo)cap->no);
370 INLINE_HEADER void traceEventCreateSparkThread(Capability *cap STG_UNUSED,
371 StgThreadID spark_tid STG_UNUSED)
373 traceSchedEvent(cap, EVENT_CREATE_SPARK_THREAD, 0, spark_tid);
374 dtraceCreateSparkThread((EventCapNo)cap->no, (EventThreadID)spark_tid);
377 // This applies only to dtrace as EVENT_STARTUP in the logging framework is
378 // handled specially in 'EventLog.c'.
380 INLINE_HEADER void dtraceEventStartup(void)
383 // XXX n_capabilities hasn't been initislised yet
384 dtraceStartup(RtsFlags.ParFlags.nNodes);
390 INLINE_HEADER void traceEventGcIdle(Capability *cap STG_UNUSED)
392 traceEvent(cap, EVENT_GC_IDLE);
393 dtraceGcIdle((EventCapNo)cap->no);
396 INLINE_HEADER void traceEventGcWork(Capability *cap STG_UNUSED)
398 traceEvent(cap, EVENT_GC_WORK);
399 dtraceGcWork((EventCapNo)cap->no);
402 INLINE_HEADER void traceEventGcDone(Capability *cap STG_UNUSED)
404 traceEvent(cap, EVENT_GC_DONE);
405 dtraceGcDone((EventCapNo)cap->no);
408 #include "EndPrivate.h"