1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 2006-2009
5 * Debug and performance tracing
7 * ---------------------------------------------------------------------------*/
18 #include "eventlog/EventLog.h"
23 // debugging flags, set with +RTS -D<something>
29 int DEBUG_block_alloc;
46 static Mutex trace_utx;
49 static rtsBool eventlog_enabled;
51 /* ---------------------------------------------------------------------------
52 Starting up / shuttting down the tracing facilities
53 --------------------------------------------------------------------------- */
55 void initTracing (void)
58 initMutex(&trace_utx);
61 #define TRACE_FLAG(name, class) \
62 class = RtsFlags.TraceFlags.name ? 1 : 0;
64 TRACE_FLAG(scheduler, TRACE_sched);
67 #define DEBUG_FLAG(name, class) \
68 class = RtsFlags.DebugFlags.name ? 1 : 0;
70 DEBUG_FLAG(scheduler, DEBUG_sched);
71 DEBUG_FLAG(scheduler, TRACE_sched); // -Ds enabled all sched events
73 DEBUG_FLAG(interpreter, DEBUG_interp);
74 DEBUG_FLAG(weak, DEBUG_weak);
75 DEBUG_FLAG(gccafs, DEBUG_gccafs);
76 DEBUG_FLAG(gc, DEBUG_gc);
77 DEBUG_FLAG(block_alloc, DEBUG_block_alloc);
78 DEBUG_FLAG(sanity, DEBUG_sanity);
79 DEBUG_FLAG(stable, DEBUG_stable);
80 DEBUG_FLAG(stm, DEBUG_stm);
81 DEBUG_FLAG(prof, DEBUG_prof);
82 DEBUG_FLAG(linker, DEBUG_linker);
83 DEBUG_FLAG(squeeze, DEBUG_squeeze);
84 DEBUG_FLAG(hpc, DEBUG_hpc);
85 DEBUG_FLAG(sparks, DEBUG_sparks);
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 /* ---------------------------------------------------------------------------
110 Emitting trace messages/events
111 --------------------------------------------------------------------------- */
114 static void tracePreface (void)
117 debugBelch("%12lx: ", (unsigned long)osThreadId());
119 if (RtsFlags.TraceFlags.timestamp) {
120 debugBelch("%9" FMT_Word64 ": ", stat_getElapsedTime());
126 static char *thread_stop_reasons[] = {
127 [HeapOverflow] = "heap overflow",
128 [StackOverflow] = "stack overflow",
129 [ThreadYielding] = "yielding",
130 [ThreadBlocked] = "blocked",
131 [ThreadFinished] = "finished",
132 [THREAD_SUSPENDED_FOREIGN_CALL] = "suspended while making a foreign call"
137 static void traceSchedEvent_stderr (Capability *cap, EventTypeNum tag,
139 StgWord64 other STG_UNUSED)
141 ACQUIRE_LOCK(&trace_utx);
145 case EVENT_CREATE_THREAD: // (cap, thread)
146 debugBelch("cap %d: created thread %lu\n",
147 cap->no, (lnat)tso->id);
149 case EVENT_RUN_THREAD: // (cap, thread)
150 debugBelch("cap %d: running thread %lu (%s)\n",
151 cap->no, (lnat)tso->id, what_next_strs[tso->what_next]);
153 case EVENT_THREAD_RUNNABLE: // (cap, thread)
154 debugBelch("cap %d: thread %lu appended to run queue\n",
155 cap->no, (lnat)tso->id);
157 case EVENT_RUN_SPARK: // (cap, thread)
158 debugBelch("cap %d: thread %lu running a spark\n",
159 cap->no, (lnat)tso->id);
161 case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
162 debugBelch("cap %d: creating spark thread %lu\n",
163 cap->no, (long)other);
165 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
166 debugBelch("cap %d: thread %lu migrating to cap %d\n",
167 cap->no, (lnat)tso->id, (int)other);
169 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
170 debugBelch("cap %d: thread %lu stealing a spark from cap %d\n",
171 cap->no, (lnat)tso->id, (int)other);
173 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
174 debugBelch("cap %d: waking up thread %lu on cap %d\n",
175 cap->no, (lnat)tso->id, (int)other);
178 case EVENT_STOP_THREAD: // (cap, thread, status)
179 debugBelch("cap %d: thread %lu stopped (%s)\n",
180 cap->no, (lnat)tso->id, thread_stop_reasons[other]);
182 case EVENT_SHUTDOWN: // (cap)
183 debugBelch("cap %d: shutting down\n", cap->no);
185 case EVENT_REQUEST_SEQ_GC: // (cap)
186 debugBelch("cap %d: requesting sequential GC\n", cap->no);
188 case EVENT_REQUEST_PAR_GC: // (cap)
189 debugBelch("cap %d: requesting parallel GC\n", cap->no);
191 case EVENT_GC_START: // (cap)
192 debugBelch("cap %d: starting GC\n", cap->no);
194 case EVENT_GC_END: // (cap)
195 debugBelch("cap %d: finished GC\n", cap->no);
198 debugBelch("cap %2d: thread %lu: event %d\n\n",
199 cap->no, (lnat)tso->id, tag);
203 RELEASE_LOCK(&trace_utx);
207 void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
208 StgTSO *tso, StgWord64 other)
211 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
212 traceSchedEvent_stderr(cap, tag, tso, other);
216 postSchedEvent(cap,tag,tso ? tso->id : 0,other);
221 static void traceCap_stderr(Capability *cap, char *msg, va_list ap)
223 ACQUIRE_LOCK(&trace_utx);
226 debugBelch("cap %2d: ", cap->no);
230 RELEASE_LOCK(&trace_utx);
234 void traceCap_(Capability *cap, char *msg, ...)
240 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
241 traceCap_stderr(cap, msg, ap);
245 postCapMsg(cap, msg, ap);
252 static void trace_stderr(char *msg, va_list ap)
254 ACQUIRE_LOCK(&trace_utx);
260 RELEASE_LOCK(&trace_utx);
264 void trace_(char *msg, ...)
270 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
271 trace_stderr(msg, ap);
281 void traceUserMsg(Capability *cap, char *msg)
284 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
285 traceCap_stderr(cap, msg, NULL);
289 if (eventlog_enabled) {
290 postUserMsg(cap, msg);
295 void traceThreadStatus_ (StgTSO *tso USED_IF_DEBUG)
298 if (RtsFlags.TraceFlags.tracing == TRACE_STDERR) {
299 printThreadStatus(tso);
303 /* nothing - no event for this one yet */
309 void traceBegin (const char *str, ...)
314 ACQUIRE_LOCK(&trace_utx);
323 RELEASE_LOCK(&trace_utx);