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 typedef StgWord32 CapsetID;
35 typedef StgWord16 CapsetType;
36 enum CapsetType { CapsetTypeCustom = CAPSET_TYPE_CUSTOM,
37 CapsetTypeOsProcess = CAPSET_TYPE_OSPROCESS,
38 CapsetTypeClockdomain = CAPSET_TYPE_CLOCKDOMAIN };
39 #define CAPSET_OSPROCESS_DEFAULT 0
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
45 // debugging flags, set with +RTS -D<something>
46 extern int DEBUG_sched;
47 extern int DEBUG_interp;
48 extern int DEBUG_weak;
49 extern int DEBUG_gccafs;
51 extern int DEBUG_block_alloc;
52 extern int DEBUG_sanity;
53 extern int DEBUG_stable;
55 extern int DEBUG_prof;
56 extern int DEBUG_gran;
58 extern int DEBUG_linker;
59 extern int DEBUG_squeeze;
61 extern int DEBUG_sparks;
64 extern int TRACE_sched;
66 // -----------------------------------------------------------------------------
69 // We use macros rather than inline functions deliberately. We want
70 // the not-taken case to be as efficient as possible, a simple
71 // test-and-jump, and with inline functions gcc seemed to move some of
72 // the instructions from the branch up before the test.
74 // -----------------------------------------------------------------------------
77 void traceBegin (const char *str, ...);
84 * Record a scheduler event
86 #define traceSchedEvent(cap, tag, tso, other) \
87 if (RTS_UNLIKELY(TRACE_sched)) { \
88 traceSchedEvent_(cap, tag, tso, other, 0); \
91 #define traceSchedEvent2(cap, tag, tso, info1, info2) \
92 if (RTS_UNLIKELY(TRACE_sched)) { \
93 traceSchedEvent_(cap, tag, tso, info1, info2); \
96 void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
97 StgTSO *tso, StgWord info1, StgWord info2);
101 * Record a nullary event
103 #define traceEvent(cap, tag) \
104 if (RTS_UNLIKELY(TRACE_sched)) { \
105 traceEvent_(cap, tag); \
108 void traceEvent_ (Capability *cap, EventTypeNum tag);
110 // variadic macros are C99, and supported by gcc. However, the
111 // ##__VA_ARGS syntax is a gcc extension, which allows the variable
112 // argument list to be empty (see gcc docs for details).
115 * Emit a trace message on a particular Capability
117 #define traceCap(class, cap, msg, ...) \
118 if (RTS_UNLIKELY(class)) { \
119 traceCap_(cap, msg, ##__VA_ARGS__); \
122 void traceCap_(Capability *cap, char *msg, ...);
125 * Emit a trace message
127 #define trace(class, msg, ...) \
128 if (RTS_UNLIKELY(class)) { \
129 trace_(msg, ##__VA_ARGS__); \
132 void trace_(char *msg, ...);
135 * A message or event emitted by the program
137 void traceUserMsg(Capability *cap, char *msg);
140 * Emit a debug message (only when DEBUG is defined)
143 #define debugTrace(class, msg, ...) \
144 if (RTS_UNLIKELY(class)) { \
145 trace_(msg, ##__VA_ARGS__); \
148 #define debugTrace(class, str, ...) /* nothing */
152 #define debugTraceCap(class, cap, msg, ...) \
153 if (RTS_UNLIKELY(class)) { \
154 traceCap_(cap, msg, ##__VA_ARGS__); \
157 #define debugTraceCap(class, cap, str, ...) /* nothing */
161 * Emit a message/event describing the state of a thread
163 #define traceThreadStatus(class, tso) \
164 if (RTS_UNLIKELY(class)) { \
165 traceThreadStatus_(tso); \
168 void traceThreadStatus_ (StgTSO *tso);
170 void traceEventStartup_ (int n_caps);
173 * Events for describing capability sets in the eventlog
175 * Note: unlike other events, these are not conditional on TRACE_sched or
176 * similar because they are not "real" events themselves but provide
177 * information and context for other "real" events. Other events depend on
178 * the capset info events so for simplicity, rather than working out if
179 * they're necessary we always emit them. They should be very low volume.
181 void traceCapsetModify_ (EventTypeNum tag,
187 #define traceSchedEvent(cap, tag, tso, other) /* nothing */
188 #define traceSchedEvent2(cap, tag, tso, other, info) /* nothing */
189 #define traceEvent(cap, tag) /* nothing */
190 #define traceCap(class, cap, msg, ...) /* nothing */
191 #define trace(class, msg, ...) /* nothing */
192 #define debugTrace(class, str, ...) /* nothing */
193 #define debugTraceCap(class, cap, str, ...) /* nothing */
194 #define traceThreadStatus(class, tso) /* nothing */
195 #define traceEventStartup_(n_caps) /* nothing */
196 #define traceCapsetModify_(tag, capset, other) /* nothing */
200 // If DTRACE is enabled, but neither DEBUG nor TRACING, we need a C land
201 // wrapper for the user-msg probe (as we can't expand that in PrimOps.cmm)
203 #if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)
205 void dtraceUserMsgWrapper(Capability *cap, char *msg);
207 #endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */
209 // -----------------------------------------------------------------------------
210 // Aliases for static dtrace probes if dtrace is available
211 // -----------------------------------------------------------------------------
215 #define dtraceCreateThread(cap, tid) \
216 HASKELLEVENT_CREATE_THREAD(cap, tid)
217 #define dtraceRunThread(cap, tid) \
218 HASKELLEVENT_RUN_THREAD(cap, tid)
219 #define dtraceStopThread(cap, tid, status, info) \
220 HASKELLEVENT_STOP_THREAD(cap, tid, status, info)
221 #define dtraceThreadRunnable(cap, tid) \
222 HASKELLEVENT_THREAD_RUNNABLE(cap, tid)
223 #define dtraceMigrateThread(cap, tid, new_cap) \
224 HASKELLEVENT_MIGRATE_THREAD(cap, tid, new_cap)
225 #define dtraceRunSpark(cap, tid) \
226 HASKELLEVENT_RUN_SPARK(cap, tid)
227 #define dtraceStealSpark(cap, tid, victim_cap) \
228 HASKELLEVENT_STEAL_SPARK(cap, tid, victim_cap)
229 #define dtraceShutdown(cap) \
230 HASKELLEVENT_SHUTDOWN(cap)
231 #define dtraceThreadWakeup(cap, tid, other_cap) \
232 HASKELLEVENT_THREAD_WAKEUP(cap, tid, other_cap)
233 #define dtraceGcStart(cap) \
234 HASKELLEVENT_GC_START(cap)
235 #define dtraceGcEnd(cap) \
236 HASKELLEVENT_GC_END(cap)
237 #define dtraceRequestSeqGc(cap) \
238 HASKELLEVENT_REQUEST_SEQ_GC(cap)
239 #define dtraceRequestParGc(cap) \
240 HASKELLEVENT_REQUEST_PAR_GC(cap)
241 #define dtraceCreateSparkThread(cap, spark_tid) \
242 HASKELLEVENT_CREATE_SPARK_THREAD(cap, spark_tid)
243 #define dtraceStartup(num_caps) \
244 HASKELLEVENT_STARTUP(num_caps)
245 #define dtraceUserMsg(cap, msg) \
246 HASKELLEVENT_USER_MSG(cap, msg)
247 #define dtraceGcIdle(cap) \
248 HASKELLEVENT_GC_IDLE(cap)
249 #define dtraceGcWork(cap) \
250 HASKELLEVENT_GC_WORK(cap)
251 #define dtraceGcDone(cap) \
252 HASKELLEVENT_GC_DONE(cap)
253 #define dtraceCapsetCreate(capset, capset_type) \
254 HASKELLEVENT_CAPSET_CREATE(capset, capset_type)
255 #define dtraceCapsetDelete(capset) \
256 HASKELLEVENT_CAPSET_DELETE(capset)
257 #define dtraceCapsetAssignCap(capset, capno) \
258 HASKELLEVENT_CAPSET_ASSIGN_CAP(capset, capno)
259 #define dtraceCapsetRemoveCap(capset, capno) \
260 HASKELLEVENT_CAPSET_REMOVE_CAP(capset, capno)
262 #else /* !defined(DTRACE) */
264 #define dtraceCreateThread(cap, tid) /* nothing */
265 #define dtraceRunThread(cap, tid) /* nothing */
266 #define dtraceStopThread(cap, tid, status, info) /* nothing */
267 #define dtraceThreadRunnable(cap, tid) /* nothing */
268 #define dtraceMigrateThread(cap, tid, new_cap) /* nothing */
269 #define dtraceRunSpark(cap, tid) /* nothing */
270 #define dtraceStealSpark(cap, tid, victim_cap) /* nothing */
271 #define dtraceShutdown(cap) /* nothing */
272 #define dtraceThreadWakeup(cap, tid, other_cap) /* nothing */
273 #define dtraceGcStart(cap) /* nothing */
274 #define dtraceGcEnd(cap) /* nothing */
275 #define dtraceRequestSeqGc(cap) /* nothing */
276 #define dtraceRequestParGc(cap) /* nothing */
277 #define dtraceCreateSparkThread(cap, spark_tid) /* nothing */
278 #define dtraceStartup(num_caps) /* nothing */
279 #define dtraceUserMsg(cap, msg) /* nothing */
280 #define dtraceGcIdle(cap) /* nothing */
281 #define dtraceGcWork(cap) /* nothing */
282 #define dtraceGcDone(cap) /* nothing */
283 #define dtraceCapsetCreate(capset, capset_type) /* nothing */
284 #define dtraceCapsetDelete(capset) /* nothing */
285 #define dtraceCapsetAssignCap(capset, capno) /* nothing */
286 #define dtraceCapsetRemoveCap(capset, capno) /* nothing */
290 // -----------------------------------------------------------------------------
291 // Trace probes dispatching to various tracing frameworks
293 // In order to avoid accumulating multiple calls to tracing calls at trace
294 // points, we define inline probe functions that contain the various
297 // Dtrace - dtrace probes are unconditionally added as probe activation is
298 // handled by the dtrace component of the kernel, and inactive probes are
299 // very cheap — usually, one no-op. Consequently, dtrace can be used with
300 // all flavours of the RTS. In addition, we still support logging events to
301 // a file, even in the presence of dtrace. This is, eg, useful when tracing
302 // on a server, but browsing trace information with ThreadScope on a local
305 // -----------------------------------------------------------------------------
307 INLINE_HEADER void traceEventCreateThread(Capability *cap STG_UNUSED,
308 StgTSO *tso STG_UNUSED)
310 traceSchedEvent(cap, EVENT_CREATE_THREAD, tso, tso->stackobj->stack_size);
311 dtraceCreateThread((EventCapNo)cap->no, (EventThreadID)tso->id);
314 INLINE_HEADER void traceEventRunThread(Capability *cap STG_UNUSED,
315 StgTSO *tso STG_UNUSED)
317 traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
318 dtraceRunThread((EventCapNo)cap->no, (EventThreadID)tso->id);
321 INLINE_HEADER void traceEventStopThread(Capability *cap STG_UNUSED,
322 StgTSO *tso STG_UNUSED,
323 StgThreadReturnCode status STG_UNUSED,
324 StgWord32 info STG_UNUSED)
326 traceSchedEvent2(cap, EVENT_STOP_THREAD, tso, status, info);
327 dtraceStopThread((EventCapNo)cap->no, (EventThreadID)tso->id,
328 (EventThreadStatus)status, (EventThreadID)info);
331 // needs to be EXTERN_INLINE as it is used in another EXTERN_INLINE function
332 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
333 StgTSO *tso STG_UNUSED);
335 EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED,
336 StgTSO *tso STG_UNUSED)
338 traceSchedEvent(cap, EVENT_THREAD_RUNNABLE, tso, 0);
339 dtraceThreadRunnable((EventCapNo)cap->no, (EventThreadID)tso->id);
342 INLINE_HEADER void traceEventMigrateThread(Capability *cap STG_UNUSED,
343 StgTSO *tso STG_UNUSED,
344 nat new_cap STG_UNUSED)
346 traceSchedEvent(cap, EVENT_MIGRATE_THREAD, tso, new_cap);
347 dtraceMigrateThread((EventCapNo)cap->no, (EventThreadID)tso->id,
348 (EventCapNo)new_cap);
351 INLINE_HEADER void traceEventRunSpark(Capability *cap STG_UNUSED,
352 StgTSO *tso STG_UNUSED)
354 traceSchedEvent(cap, EVENT_RUN_SPARK, tso, 0);
355 dtraceRunSpark((EventCapNo)cap->no, (EventThreadID)tso->id);
358 INLINE_HEADER void traceEventStealSpark(Capability *cap STG_UNUSED,
359 StgTSO *tso STG_UNUSED,
360 nat victim_cap STG_UNUSED)
362 traceSchedEvent(cap, EVENT_STEAL_SPARK, tso, victim_cap);
363 dtraceStealSpark((EventCapNo)cap->no, (EventThreadID)tso->id,
364 (EventCapNo)victim_cap);
367 INLINE_HEADER void traceEventShutdown(Capability *cap STG_UNUSED)
369 traceSchedEvent(cap, EVENT_SHUTDOWN, 0, 0);
370 dtraceShutdown((EventCapNo)cap->no);
373 INLINE_HEADER void traceEventThreadWakeup(Capability *cap STG_UNUSED,
374 StgTSO *tso STG_UNUSED,
375 nat other_cap STG_UNUSED)
377 traceSchedEvent(cap, EVENT_THREAD_WAKEUP, tso, other_cap);
378 dtraceThreadWakeup((EventCapNo)cap->no, (EventThreadID)tso->id,
379 (EventCapNo)other_cap);
382 INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
384 traceSchedEvent(cap, EVENT_GC_START, 0, 0);
385 dtraceGcStart((EventCapNo)cap->no);
388 INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
390 traceSchedEvent(cap, EVENT_GC_END, 0, 0);
391 dtraceGcEnd((EventCapNo)cap->no);
394 INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
396 traceSchedEvent(cap, EVENT_REQUEST_SEQ_GC, 0, 0);
397 dtraceRequestSeqGc((EventCapNo)cap->no);
400 INLINE_HEADER void traceEventRequestParGc(Capability *cap STG_UNUSED)
402 traceSchedEvent(cap, EVENT_REQUEST_PAR_GC, 0, 0);
403 dtraceRequestParGc((EventCapNo)cap->no);
406 INLINE_HEADER void traceEventCreateSparkThread(Capability *cap STG_UNUSED,
407 StgThreadID spark_tid STG_UNUSED)
409 traceSchedEvent(cap, EVENT_CREATE_SPARK_THREAD, 0, spark_tid);
410 dtraceCreateSparkThread((EventCapNo)cap->no, (EventThreadID)spark_tid);
413 INLINE_HEADER void traceEventStartup(void)
417 // XXX n_capabilities hasn't been initislised yet
418 n_caps = RtsFlags.ParFlags.nNodes;
423 traceEventStartup_(n_caps);
424 dtraceStartup(n_caps);
427 INLINE_HEADER void traceEventGcIdle(Capability *cap STG_UNUSED)
429 traceEvent(cap, EVENT_GC_IDLE);
430 dtraceGcIdle((EventCapNo)cap->no);
433 INLINE_HEADER void traceEventGcWork(Capability *cap STG_UNUSED)
435 traceEvent(cap, EVENT_GC_WORK);
436 dtraceGcWork((EventCapNo)cap->no);
439 INLINE_HEADER void traceEventGcDone(Capability *cap STG_UNUSED)
441 traceEvent(cap, EVENT_GC_DONE);
442 dtraceGcDone((EventCapNo)cap->no);
445 INLINE_HEADER void traceCapsetCreate(CapsetID capset STG_UNUSED,
446 CapsetType capset_type STG_UNUSED)
448 traceCapsetModify_(EVENT_CAPSET_CREATE, capset, capset_type);
449 dtraceCapsetCreate(capset, capset_type);
452 INLINE_HEADER void traceCapsetDelete(CapsetID capset STG_UNUSED)
454 traceCapsetModify_(EVENT_CAPSET_DELETE, capset, 0);
455 dtraceCapsetDelete(capset);
458 INLINE_HEADER void traceCapsetAssignCap(CapsetID capset STG_UNUSED,
459 nat capno STG_UNUSED)
461 traceCapsetModify_(EVENT_CAPSET_ASSIGN_CAP, capset, capno);
462 dtraceCapsetAssignCap(capset, capno);
465 INLINE_HEADER void traceCapsetRemoveCap(CapsetID capset STG_UNUSED,
466 nat capno STG_UNUSED)
468 traceCapsetModify_(EVENT_CAPSET_REMOVE_CAP, capset, capno);
469 dtraceCapsetRemoveCap(capset, capno);
472 #include "EndPrivate.h"