Trace.h 14.1 KB
Newer Older
Simon Marlow's avatar
Simon Marlow committed
1 2
/* -----------------------------------------------------------------------------
 *
3
 * (c) The GHC Team, 2008-2009
Simon Marlow's avatar
Simon Marlow committed
4
 *
5
 * Support for fast binary event logging and user-space dtrace probes.
Simon Marlow's avatar
Simon Marlow committed
6 7 8 9 10 11
 *
 * ---------------------------------------------------------------------------*/

#ifndef TRACE_H
#define TRACE_H

12 13 14
#include "rts/EventLogFormat.h"
#include "Capability.h"

15 16 17 18
#if defined(DTRACE)
#include "RtsProbes.h"
#endif /* defined(DTRACE) */

19
#include "BeginPrivate.h"
20

21 22 23
// -----------------------------------------------------------------------------
// EventLog API
// -----------------------------------------------------------------------------
Simon Marlow's avatar
Simon Marlow committed
24

25
#if defined(TRACING)
Simon Marlow's avatar
Simon Marlow committed
26

27 28 29
void initTracing (void);
void endTracing  (void);
void freeTracing (void);
30
void resetTracing (void);
Simon Marlow's avatar
Simon Marlow committed
31

32
#endif /* TRACING */
Simon Marlow's avatar
Simon Marlow committed
33 34

// -----------------------------------------------------------------------------
35
// Message classes
Simon Marlow's avatar
Simon Marlow committed
36 37 38
// -----------------------------------------------------------------------------

// debugging flags, set with +RTS -D<something>
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
extern int DEBUG_sched;
extern int DEBUG_interp;
extern int DEBUG_weak;
extern int DEBUG_gccafs;
extern int DEBUG_gc;
extern int DEBUG_block_alloc;
extern int DEBUG_sanity;
extern int DEBUG_stable;
extern int DEBUG_stm;
extern int DEBUG_prof;
extern int DEBUG_gran;
extern int DEBUG_par;
extern int DEBUG_linker;
extern int DEBUG_squeeze;
extern int DEBUG_hpc;
extern int DEBUG_sparks;
55 56

// events
57
extern int TRACE_sched;
Simon Marlow's avatar
Simon Marlow committed
58 59

// -----------------------------------------------------------------------------
60 61 62 63 64 65 66
// Posting events
//
// We use macros rather than inline functions deliberately.  We want
// the not-taken case to be as efficient as possible, a simple
// test-and-jump, and with inline functions gcc seemed to move some of
// the instructions from the branch up before the test.
// 
Simon Marlow's avatar
Simon Marlow committed
67 68
// -----------------------------------------------------------------------------

69 70 71 72
#ifdef DEBUG
void traceBegin (const char *str, ...);
void traceEnd (void);
#endif
73

74
#ifdef TRACING
Simon Marlow's avatar
Simon Marlow committed
75

76 77 78 79 80
/* 
 * Record a scheduler event
 */
#define traceSchedEvent(cap, tag, tso, other)   \
    if (RTS_UNLIKELY(TRACE_sched)) {            \
81 82 83 84 85 86
        traceSchedEvent_(cap, tag, tso, other, 0); \
    }

#define traceSchedEvent2(cap, tag, tso, info1, info2) \
    if (RTS_UNLIKELY(TRACE_sched)) {            \
        traceSchedEvent_(cap, tag, tso, info1, info2); \
87
    }
88 89

void traceSchedEvent_ (Capability *cap, EventTypeNum tag, 
90
                       StgTSO *tso, StgWord info1, StgWord info2);
91

92 93 94 95 96 97 98 99 100 101 102

/*
 * Record a nullary event
 */
#define traceEvent(cap, tag)                    \
    if (RTS_UNLIKELY(TRACE_sched)) {            \
        traceEvent_(cap, tag);                  \
    }

void traceEvent_ (Capability *cap, EventTypeNum tag);

103 104 105 106
// variadic macros are C99, and supported by gcc.  However, the
// ##__VA_ARGS syntax is a gcc extension, which allows the variable
// argument list to be empty (see gcc docs for details).

107
/* 
108
 * Emit a trace message on a particular Capability
109
 */
110 111 112
#define traceCap(class, cap, msg, ...)          \
    if (RTS_UNLIKELY(class)) {                  \
        traceCap_(cap, msg, ##__VA_ARGS__);     \
113 114
    }

115
void traceCap_(Capability *cap, char *msg, ...);
116 117

/* 
118
 * Emit a trace message
119
 */
120 121 122
#define trace(class, msg, ...)                  \
    if (RTS_UNLIKELY(class)) {                  \
        trace_(msg, ##__VA_ARGS__);             \
123 124
    }

125
void trace_(char *msg, ...);
126

127 128 129 130 131
/* 
 * A message or event emitted by the program
 */
void traceUserMsg(Capability *cap, char *msg);

132
/* 
133
 * Emit a debug message (only when DEBUG is defined)
134 135
 */
#ifdef DEBUG
136 137 138
#define debugTrace(class, msg, ...)             \
    if (RTS_UNLIKELY(class)) {                  \
        trace_(msg, ##__VA_ARGS__);             \
139 140 141 142 143
    }
#else
#define debugTrace(class, str, ...) /* nothing */
#endif

144 145 146 147 148 149 150 151 152
#ifdef DEBUG
#define debugTraceCap(class, cap, msg, ...)      \
    if (RTS_UNLIKELY(class)) {                  \
        traceCap_(cap, msg, ##__VA_ARGS__);     \
    }
#else
#define debugTraceCap(class, cap, str, ...) /* nothing */
#endif

153 154 155 156 157 158
/* 
 * Emit a message/event describing the state of a thread
 */
#define traceThreadStatus(class, tso)           \
    if (RTS_UNLIKELY(class)) {                  \
        traceThreadStatus_(tso);                \
159 160
    }

161
void traceThreadStatus_ (StgTSO *tso);
162

163
#else /* !TRACING */
164

165
#define traceSchedEvent(cap, tag, tso, other) /* nothing */
166
#define traceSchedEvent2(cap, tag, tso, other, info) /* nothing */
167
#define traceEvent(cap, tag) /* nothing */
168 169
#define traceCap(class, cap, msg, ...) /* nothing */
#define trace(class, msg, ...) /* nothing */
170
#define debugTrace(class, str, ...) /* nothing */
171
#define debugTraceCap(class, cap, str, ...) /* nothing */
172
#define traceThreadStatus(class, tso) /* nothing */
173 174

#endif /* TRACING */
Simon Marlow's avatar
Simon Marlow committed
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
// If DTRACE is enabled, but neither DEBUG nor TRACING, we need a C land
// wrapper for the user-msg probe (as we can't expand that in PrimOps.cmm)
//
#if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)

void dtraceUserMsgWrapper(Capability *cap, char *msg);

#endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */

// -----------------------------------------------------------------------------
// Aliases for static dtrace probes if dtrace is available
// -----------------------------------------------------------------------------

#if defined(DTRACE)

#define dtraceCreateThread(cap, tid)                    \
    HASKELLEVENT_CREATE_THREAD(cap, tid)
#define dtraceRunThread(cap, tid)                       \
    HASKELLEVENT_RUN_THREAD(cap, tid)
195 196
#define dtraceStopThread(cap, tid, status, info)        \
    HASKELLEVENT_STOP_THREAD(cap, tid, status, info)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
#define dtraceThreadRunnable(cap, tid)                  \
    HASKELLEVENT_THREAD_RUNNABLE(cap, tid)
#define dtraceMigrateThread(cap, tid, new_cap)          \
    HASKELLEVENT_MIGRATE_THREAD(cap, tid, new_cap)
#define dtraceRunSpark(cap, tid)                        \
    HASKELLEVENT_RUN_SPARK(cap, tid)
#define dtraceStealSpark(cap, tid, victim_cap)          \
    HASKELLEVENT_STEAL_SPARK(cap, tid, victim_cap)
#define dtraceShutdown(cap)                             \
    HASKELLEVENT_SHUTDOWN(cap)
#define dtraceThreadWakeup(cap, tid, other_cap)         \
    HASKELLEVENT_THREAD_WAKEUP(cap, tid, other_cap)
#define dtraceGcStart(cap)                              \
    HASKELLEVENT_GC_START(cap)
#define dtraceGcEnd(cap)                                \
    HASKELLEVENT_GC_END(cap)
#define dtraceRequestSeqGc(cap)                         \
    HASKELLEVENT_REQUEST_SEQ_GC(cap)
#define dtraceRequestParGc(cap)                         \
    HASKELLEVENT_REQUEST_PAR_GC(cap)
#define dtraceCreateSparkThread(cap, spark_tid)         \
    HASKELLEVENT_CREATE_SPARK_THREAD(cap, spark_tid)
#define dtraceStartup(num_caps)                         \
    HASKELLEVENT_STARTUP(num_caps)
#define dtraceUserMsg(cap, msg)                         \
    HASKELLEVENT_USER_MSG(cap, msg)
#define dtraceGcIdle(cap)                               \
    HASKELLEVENT_GC_IDLE(cap)
#define dtraceGcWork(cap)                               \
    HASKELLEVENT_GC_WORK(cap)
#define dtraceGcDone(cap)                               \
    HASKELLEVENT_GC_DONE(cap)

#else /* !defined(DTRACE) */

#define dtraceCreateThread(cap, tid)                    /* nothing */
#define dtraceRunThread(cap, tid)                       /* nothing */
234
#define dtraceStopThread(cap, tid, status, info)        /* nothing */
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
#define dtraceThreadRunnable(cap, tid)                  /* nothing */
#define dtraceMigrateThread(cap, tid, new_cap)          /* nothing */
#define dtraceRunSpark(cap, tid)                        /* nothing */
#define dtraceStealSpark(cap, tid, victim_cap)          /* nothing */
#define dtraceShutdown(cap)                             /* nothing */
#define dtraceThreadWakeup(cap, tid, other_cap)         /* nothing */
#define dtraceGcStart(cap)                              /* nothing */
#define dtraceGcEnd(cap)                                /* nothing */
#define dtraceRequestSeqGc(cap)                         /* nothing */
#define dtraceRequestParGc(cap)                         /* nothing */
#define dtraceCreateSparkThread(cap, spark_tid)         /* nothing */
#define dtraceStartup(num_caps)                         /* nothing */
#define dtraceUserMsg(cap, msg)                         /* nothing */
#define dtraceGcIdle(cap)                               /* nothing */
#define dtraceGcWork(cap)                               /* nothing */
#define dtraceGcDone(cap)                               /* nothing */

#endif

// -----------------------------------------------------------------------------
// Trace probes dispatching to various tracing frameworks
//
// In order to avoid accumulating multiple calls to tracing calls at trace
// points, we define inline probe functions that contain the various
// invocations.
//
// Dtrace - dtrace probes are unconditionally added as probe activation is
//   handled by the dtrace component of the kernel, and inactive probes are
//   very cheap — usually, one no-op.  Consequently, dtrace can be used with
//   all flavours of the RTS.  In addition, we still support logging events to
//   a file, even in the presence of dtrace.  This is, eg, useful when tracing
//   on a server, but browsing trace information with ThreadScope on a local
//   client.
// 
// -----------------------------------------------------------------------------

INLINE_HEADER void traceEventCreateThread(Capability *cap STG_UNUSED, 
                                          StgTSO     *tso STG_UNUSED)
{
274
    traceSchedEvent(cap, EVENT_CREATE_THREAD, tso, tso->stackobj->stack_size);
275 276 277 278 279 280 281 282 283 284 285 286
    dtraceCreateThread((EventCapNo)cap->no, (EventThreadID)tso->id);
}

INLINE_HEADER void traceEventRunThread(Capability *cap STG_UNUSED, 
                                       StgTSO     *tso STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
    dtraceRunThread((EventCapNo)cap->no, (EventThreadID)tso->id);
}

INLINE_HEADER void traceEventStopThread(Capability          *cap    STG_UNUSED, 
                                        StgTSO              *tso    STG_UNUSED, 
287 288
                                        StgThreadReturnCode  status STG_UNUSED,
                                        StgWord32           info    STG_UNUSED)
289
{
290
    traceSchedEvent2(cap, EVENT_STOP_THREAD, tso, status, info);
291
    dtraceStopThread((EventCapNo)cap->no, (EventThreadID)tso->id,
292
                     (EventThreadStatus)status, (EventThreadID)info);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
}

// needs to be EXTERN_INLINE as it is used in another EXTERN_INLINE function
EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED, 
                                            StgTSO     *tso STG_UNUSED);

EXTERN_INLINE void traceEventThreadRunnable(Capability *cap STG_UNUSED, 
                                            StgTSO     *tso STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_THREAD_RUNNABLE, tso, 0);
    dtraceThreadRunnable((EventCapNo)cap->no, (EventThreadID)tso->id);
}

INLINE_HEADER void traceEventMigrateThread(Capability *cap     STG_UNUSED, 
                                           StgTSO     *tso     STG_UNUSED,
                                           nat         new_cap STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_MIGRATE_THREAD, tso, new_cap);
    dtraceMigrateThread((EventCapNo)cap->no, (EventThreadID)tso->id,
                        (EventCapNo)new_cap);
}

INLINE_HEADER void traceEventRunSpark(Capability *cap STG_UNUSED, 
                                      StgTSO     *tso STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_RUN_SPARK, tso, 0);
    dtraceRunSpark((EventCapNo)cap->no, (EventThreadID)tso->id);
}

INLINE_HEADER void traceEventStealSpark(Capability *cap        STG_UNUSED, 
                                        StgTSO     *tso        STG_UNUSED,
                                        nat         victim_cap STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_STEAL_SPARK, tso, victim_cap);
    dtraceStealSpark((EventCapNo)cap->no, (EventThreadID)tso->id,
                     (EventCapNo)victim_cap);
}

INLINE_HEADER void traceEventShutdown(Capability *cap STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_SHUTDOWN, 0, 0);
    dtraceShutdown((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventThreadWakeup(Capability *cap       STG_UNUSED, 
                                          StgTSO     *tso       STG_UNUSED,
                                          nat         other_cap STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_THREAD_WAKEUP, tso, other_cap);
    dtraceThreadWakeup((EventCapNo)cap->no, (EventThreadID)tso->id,
                       (EventCapNo)other_cap);
}

INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_GC_START, 0, 0);
    dtraceGcStart((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_GC_END, 0, 0);
    dtraceGcEnd((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_REQUEST_SEQ_GC, 0, 0);
    dtraceRequestSeqGc((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventRequestParGc(Capability *cap STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_REQUEST_PAR_GC, 0, 0);
    dtraceRequestParGc((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventCreateSparkThread(Capability  *cap      STG_UNUSED, 
                                               StgThreadID spark_tid STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_CREATE_SPARK_THREAD, 0, spark_tid);
    dtraceCreateSparkThread((EventCapNo)cap->no, (EventThreadID)spark_tid);
}

// This applies only to dtrace as EVENT_STARTUP in the logging framework is
// handled specially in 'EventLog.c'.
//
INLINE_HEADER void dtraceEventStartup(void)
{
#ifdef THREADED_RTS
    // XXX n_capabilities hasn't been initislised yet
    dtraceStartup(RtsFlags.ParFlags.nNodes);
#else
    dtraceStartup(1);
#endif
}

INLINE_HEADER void traceEventGcIdle(Capability *cap STG_UNUSED)
{
    traceEvent(cap, EVENT_GC_IDLE);
    dtraceGcIdle((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventGcWork(Capability *cap STG_UNUSED)
{
    traceEvent(cap, EVENT_GC_WORK);
    dtraceGcWork((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventGcDone(Capability *cap STG_UNUSED)
{
    traceEvent(cap, EVENT_GC_DONE);
    dtraceGcDone((EventCapNo)cap->no);
}

408
#include "EndPrivate.h"
409

Simon Marlow's avatar
Simon Marlow committed
410
#endif /* TRACE_H */