1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 2006-2009
5 * Debug and performance tracing
7 * ---------------------------------------------------------------------------*/
19 #include "eventlog/EventLog.h"
24 // debugging flags, set with +RTS -D<something>
30 int DEBUG_block_alloc;
47 static Mutex trace_utx;
50 static rtsBool eventlog_enabled;
52 /* ---------------------------------------------------------------------------
53 Starting up / shuttting down the tracing facilities
54 --------------------------------------------------------------------------- */
56 void initTracing (void)
59 initMutex(&trace_utx);
63 #define DEBUG_FLAG(name, class) \
64 class = RtsFlags.DebugFlags.name ? 1 : 0;
66 DEBUG_FLAG(scheduler, DEBUG_sched);
68 DEBUG_FLAG(interpreter, DEBUG_interp);
69 DEBUG_FLAG(weak, DEBUG_weak);
70 DEBUG_FLAG(gccafs, DEBUG_gccafs);
71 DEBUG_FLAG(gc, DEBUG_gc);
72 DEBUG_FLAG(block_alloc, DEBUG_block_alloc);
73 DEBUG_FLAG(sanity, DEBUG_sanity);
74 DEBUG_FLAG(stable, DEBUG_stable);
75 DEBUG_FLAG(stm, DEBUG_stm);
76 DEBUG_FLAG(prof, DEBUG_prof);
77 DEBUG_FLAG(linker, DEBUG_linker);
78 DEBUG_FLAG(squeeze, DEBUG_squeeze);
79 DEBUG_FLAG(hpc, DEBUG_hpc);
80 DEBUG_FLAG(sparks, DEBUG_sparks);
83 // -Ds turns on scheduler tracing too
85 RtsFlags.TraceFlags.scheduler ||
86 RtsFlags.DebugFlags.scheduler;
88 eventlog_enabled = RtsFlags.TraceFlags.tracing == TRACE_EVENTLOG;
90 if (eventlog_enabled) {
95 void endTracing (void)
97 if (eventlog_enabled) {
102 void freeTracing (void)
104 if (eventlog_enabled) {
109 void resetTracing (void)
111 if (eventlog_enabled) {
112 abortEventLogging(); // abort eventlog inherited from parent
113 initEventLogging(); // child starts its own eventlog
117 /* ---------------------------------------------------------------------------
118 Emitting trace messages/events
119 --------------------------------------------------------------------------- */
122 static void tracePreface (void)
125 debugBelch("%12lx: ", (unsigned long)osThreadId());
127 if (RtsFlags.TraceFlags.timestamp) {
128 debugBelch("%9" FMT_Word64 ": ", stat_getElapsedTime());
134 static char *thread_stop_reasons[] = {
135 [HeapOverflow] = "heap overflow",
136 [StackOverflow] = "stack overflow",
137 [ThreadYielding] = "yielding",
138 [ThreadBlocked] = "blocked",
139 [ThreadFinished] = "finished",
140 [THREAD_SUSPENDED_FOREIGN_CALL] = "suspended while making a foreign call"
145 static void traceSchedEvent_stderr (Capability *cap, EventTypeNum tag,
147 StgWord64 other STG_UNUSED)
149 ACQUIRE_LOCK(&trace_utx);
153 case EVENT_CREATE_THREAD: // (cap, thread)
154 debugBelch("cap %d: created thread %lu\n",
155 cap->no, (lnat)tso->id);
157 case EVENT_RUN_THREAD: // (cap, thread)
158 debugBelch("cap %d: running thread %lu (%s)\n",
159 cap->no, (lnat)tso->id, what_next_strs[tso->what_next]);
161 case EVENT_THREAD_RUNNABLE: // (cap, thread)
162 debugBelch("cap %d: thread %lu appended to run queue\n",
163 cap->no, (lnat)tso->id);
165 case EVENT_RUN_SPARK: // (cap, thread)
166 debugBelch("cap %d: thread %lu running a spark\n",
167 cap->no, (lnat)tso->id);
169 case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
170 debugBelch("cap %d: creating spark thread %lu\n",
171 cap->no, (long)other);
173 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
174 debugBelch("cap %d: thread %lu migrating to cap %d\n",
175 cap->no, (lnat)tso->id, (int)other);
177 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
178 debugBelch("cap %d: thread %lu stealing a spark from cap %d\n",
179 cap->no, (lnat)tso->id, (int)other);
181 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
182 debugBelch("cap %d: waking up thread %lu on cap %d\n",
183 cap->no, (lnat)tso->id, (int)other);
186 case EVENT_STOP_THREAD: // (cap, thread, status)
187 debugBelch("cap %d: thread %lu stopped (%s)\n",
188 cap->no, (lnat)tso->id, thread_stop_reasons[other]);
190 case EVENT_SHUTDOWN: // (cap)
191 debugBelch("cap %d: shutting down\n", cap->no);
193 case EVENT_REQUEST_SEQ_GC: // (cap)
194 debugBelch("cap %d: requesting sequential GC\n", cap->no);
196 case EVENT_REQUEST_PAR_GC: // (cap)
197 debugBelch("cap %d: requesting parallel GC\n", cap->no);
199 case EVENT_GC_START: // (cap)
200 debugBelch("cap %d: starting GC\n", cap->no);
202 case EVENT_GC_END: // (cap)
203 debugBelch("cap %d: finished GC\n", cap->no);
205 case EVENT_GC_IDLE: // (cap)
206 debugBelch("cap %d: GC idle\n", cap->no);
208 case EVENT_GC_WORK: // (cap)
209 debugBelch("cap %d: GC working\n", cap->no);
211 case EVENT_GC_DONE: // (cap)
212 debugBelch("cap %d: GC done\n", cap->no);
215 debugBelch("cap %d: thread %lu: event %d\n\n",
216 cap->no, (lnat)tso->id, tag);
220 RELEASE_LOCK(&trace_utx);
224 void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
225 StgTSO *tso, StgWord64 other)
228 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
229 traceSchedEvent_stderr(cap, tag, tso, other);
233 postSchedEvent(cap,tag,tso ? tso->id : 0,other);
237 void traceEvent_ (Capability *cap, EventTypeNum tag)
240 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
241 traceSchedEvent_stderr(cap, tag, 0, 0);
250 static void traceCap_stderr(Capability *cap, char *msg, va_list ap)
252 ACQUIRE_LOCK(&trace_utx);
255 debugBelch("cap %d: ", cap->no);
259 RELEASE_LOCK(&trace_utx);
263 void traceCap_(Capability *cap, char *msg, ...)
269 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
270 traceCap_stderr(cap, msg, ap);
274 postCapMsg(cap, msg, ap);
281 static void trace_stderr(char *msg, va_list ap)
283 ACQUIRE_LOCK(&trace_utx);
289 RELEASE_LOCK(&trace_utx);
293 void trace_(char *msg, ...)
299 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
300 trace_stderr(msg, ap);
310 static void traceFormatUserMsg(Capability *cap, char *msg, ...)
316 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
317 traceCap_stderr(cap, msg, ap);
321 if (eventlog_enabled) {
322 postUserMsg(cap, msg, ap);
325 dtraceUserMsg(cap->no, msg);
328 void traceUserMsg(Capability *cap, char *msg)
330 traceFormatUserMsg(cap, "%s", msg);
333 void traceThreadStatus_ (StgTSO *tso USED_IF_DEBUG)
336 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
337 printThreadStatus(tso);
341 /* nothing - no event for this one yet */
347 void traceBegin (const char *str, ...)
352 ACQUIRE_LOCK(&trace_utx);
361 RELEASE_LOCK(&trace_utx);
367 // If DTRACE is enabled, but neither DEBUG nor TRACING, we need a C land
368 // wrapper for the user-msg probe (as we can't expand that in PrimOps.cmm)
370 #if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)
372 void dtraceUserMsgWrapper(Capability *cap, char *msg)
374 dtraceUserMsg(cap->no, msg);
377 #endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */