1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 2006-2009
5 * Debug and performance tracing
7 * ---------------------------------------------------------------------------*/
18 #include "eventlog/EventLog.h"
22 // debugging flags, set with +RTS -D<something>
28 int DEBUG_block_alloc;
44 static Mutex trace_utx;
47 /* ---------------------------------------------------------------------------
48 Starting up / shuttting down the tracing facilities
49 --------------------------------------------------------------------------- */
51 void initTracing (void)
54 initMutex(&trace_utx);
58 #define DEBUG_FLAG(name, class) \
59 class = RtsFlags.DebugFlags.name ? 1 : 0;
61 DEBUG_FLAG(scheduler, DEBUG_sched);
62 DEBUG_FLAG(scheduler, TRACE_sched); // -Ds enabled all sched events
64 DEBUG_FLAG(interpreter, DEBUG_interp);
65 DEBUG_FLAG(weak, DEBUG_weak);
66 DEBUG_FLAG(gccafs, DEBUG_gccafs);
67 DEBUG_FLAG(gc, DEBUG_gc);
68 DEBUG_FLAG(block_alloc, DEBUG_block_alloc);
69 DEBUG_FLAG(sanity, DEBUG_sanity);
70 DEBUG_FLAG(stable, DEBUG_stable);
71 DEBUG_FLAG(stm, DEBUG_stm);
72 DEBUG_FLAG(prof, DEBUG_prof);
73 DEBUG_FLAG(linker, DEBUG_linker);
74 DEBUG_FLAG(squeeze, DEBUG_squeeze);
75 DEBUG_FLAG(hpc, DEBUG_hpc);
76 DEBUG_FLAG(sparks, DEBUG_sparks);
79 #define TRACE_FLAG(name, class) \
80 class = RtsFlags.TraceFlags.name ? 1 : 0;
82 TRACE_FLAG(scheduler, TRACE_sched);
87 void endTracing (void)
92 void freeTracing (void)
97 /* ---------------------------------------------------------------------------
98 Emitting trace messages/events
99 --------------------------------------------------------------------------- */
102 static void tracePreface (void)
105 debugBelch("%12lx: ", (unsigned long)osThreadId());
107 if (RtsFlags.TraceFlags.timestamp) {
108 debugBelch("%9" FMT_Word64 ": ", stat_getElapsedTime());
114 static char *thread_stop_reasons[] = {
115 [HeapOverflow] = "heap overflow",
116 [StackOverflow] = "stack overflow",
117 [ThreadYielding] = "yielding",
118 [ThreadBlocked] = "blocked",
119 [ThreadFinished] = "finished",
120 [THREAD_SUSPENDED_FOREIGN_CALL] = "suspended while making a foreign call"
125 static void traceSchedEvent_stderr (Capability *cap, EventTypeNum tag,
127 StgWord64 other STG_UNUSED)
129 ACQUIRE_LOCK(&trace_utx);
133 case EVENT_CREATE_THREAD: // (cap, thread)
134 debugBelch("cap %d: created thread %lu\n",
135 cap->no, (lnat)tso->id);
137 case EVENT_RUN_THREAD: // (cap, thread)
138 debugBelch("cap %d: running thread %lu (%s)\n",
139 cap->no, (lnat)tso->id, what_next_strs[tso->what_next]);
141 case EVENT_THREAD_RUNNABLE: // (cap, thread)
142 debugBelch("cap %d: thread %lu appended to run queue\n",
143 cap->no, (lnat)tso->id);
145 case EVENT_RUN_SPARK: // (cap, thread)
146 debugBelch("cap %d: thread %lu running a spark\n",
147 cap->no, (lnat)tso->id);
149 case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
150 debugBelch("cap %d: creating spark thread %lu\n",
151 cap->no, (long)other);
153 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
154 debugBelch("cap %d: thread %lu migrating to cap %d\n",
155 cap->no, (lnat)tso->id, (int)other);
157 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
158 debugBelch("cap %d: thread %lu stealing a spark from cap %d\n",
159 cap->no, (lnat)tso->id, (int)other);
161 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
162 debugBelch("cap %d: waking up thread %lu on cap %d\n",
163 cap->no, (lnat)tso->id, (int)other);
166 case EVENT_STOP_THREAD: // (cap, thread, status)
167 debugBelch("cap %d: thread %lu stopped (%s)\n",
168 cap->no, (lnat)tso->id, thread_stop_reasons[other]);
170 case EVENT_SHUTDOWN: // (cap)
171 debugBelch("cap %d: shutting down\n", cap->no);
173 case EVENT_REQUEST_SEQ_GC: // (cap)
174 debugBelch("cap %d: requesting sequential GC\n", cap->no);
176 case EVENT_REQUEST_PAR_GC: // (cap)
177 debugBelch("cap %d: requesting parallel GC\n", cap->no);
179 case EVENT_GC_START: // (cap)
180 debugBelch("cap %d: starting GC\n", cap->no);
182 case EVENT_GC_END: // (cap)
183 debugBelch("cap %d: finished GC\n", cap->no);
186 debugBelch("cap %2d: thread %lu: event %d\n\n",
187 cap->no, (lnat)tso->id, tag);
191 RELEASE_LOCK(&trace_utx);
195 void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
196 StgTSO *tso, StgWord64 other)
199 if (RtsFlags.TraceFlags.trace_stderr) {
200 traceSchedEvent_stderr(cap, tag, tso, other);
204 postSchedEvent(cap,tag,tso ? tso->id : 0,other);
209 static void traceCap_stderr(Capability *cap, char *msg, va_list ap)
211 ACQUIRE_LOCK(&trace_utx);
214 debugBelch("cap %2d: ", cap->no);
218 RELEASE_LOCK(&trace_utx);
222 void traceCap_(Capability *cap, char *msg, ...)
228 if (RtsFlags.TraceFlags.trace_stderr) {
229 traceCap_stderr(cap, msg, ap);
233 postCapMsg(cap, msg, ap);
240 static void trace_stderr(char *msg, va_list ap)
242 ACQUIRE_LOCK(&trace_utx);
248 RELEASE_LOCK(&trace_utx);
252 void trace_(char *msg, ...)
258 if (RtsFlags.TraceFlags.trace_stderr) {
259 trace_stderr(msg, ap);
269 void traceThreadStatus_ (StgTSO *tso USED_IF_DEBUG)
272 if (RtsFlags.TraceFlags.trace_stderr) {
273 printThreadStatus(tso);
277 /* nothing - no event for this one yet */
283 void traceBegin (const char *str, ...)
288 ACQUIRE_LOCK(&trace_utx);
297 RELEASE_LOCK(&trace_utx);