Trace.h 33.3 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
#pragma once
Simon Marlow's avatar
Simon Marlow committed
10

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

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

18
#include "BeginPrivate.h"
19

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

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

26 27 28
void initTracing (void);
void endTracing  (void);
void freeTracing (void);
29
void resetTracing (void);
30
void tracingAddCapapilities (uint32_t from, uint32_t to);
Simon Marlow's avatar
Simon Marlow committed
31

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

34 35 36 37 38
typedef StgWord32 CapsetID;
typedef StgWord16 CapsetType;
enum CapsetType { CapsetTypeCustom = CAPSET_TYPE_CUSTOM,
                  CapsetTypeOsProcess = CAPSET_TYPE_OSPROCESS,
                  CapsetTypeClockdomain = CAPSET_TYPE_CLOCKDOMAIN };
39 40 41
#define CAPSET_OSPROCESS_DEFAULT   ((CapsetID)0)
#define CAPSET_HEAP_DEFAULT        ((CapsetID)0)   /* reusing the same capset */
#define CAPSET_CLOCKDOMAIN_DEFAULT ((CapsetID)1)
42

Simon Marlow's avatar
Simon Marlow committed
43
// -----------------------------------------------------------------------------
44
// Message classes
Simon Marlow's avatar
Simon Marlow committed
45 46
// -----------------------------------------------------------------------------

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
// shorthand for RtsFlags.DebugFlags.<blah>, useful with debugTrace()
#define DEBUG_sched       RtsFlags.DebugFlags.scheduler
#define DEBUG_interp      RtsFlags.DebugFlags.interp
#define DEBUG_weak        RtsFlags.DebugFlags.weak
#define DEBUG_gccafs      RtsFlags.DebugFlags.gccafs
#define DEBUG_gc          RtsFlags.DebugFlags.gc
#define DEBUG_block_alloc RtsFlags.DebugFlags.alloc
#define DEBUG_sanity      RtsFlags.DebugFlags.sanity
#define DEBUG_stable      RtsFlags.DebugFlags.stable
#define DEBUG_stm         RtsFlags.DebugFlags.stm
#define DEBUG_prof        RtsFlags.DebugFlags.prof
#define DEBUG_gran        RtsFlags.DebugFlags.gran
#define DEBUG_par         RtsFlags.DebugFlags.par
#define DEBUG_linker      RtsFlags.DebugFlags.linker
#define DEBUG_squeeze     RtsFlags.DebugFlags.squeeze
#define DEBUG_hpc         RtsFlags.DebugFlags.hpc
#define DEBUG_sparks      RtsFlags.DebugFlags.sparks
64
#define DEBUG_compact     RtsFlags.DebugFlags.compact
65 66

// events
67
extern int TRACE_sched;
68
extern int TRACE_gc;
69 70
extern int TRACE_spark_sampled;
extern int TRACE_spark_full;
71
/* extern int TRACE_user; */  // only used in Trace.c
72
extern int TRACE_cap;
Simon Marlow's avatar
Simon Marlow committed
73 74

// -----------------------------------------------------------------------------
75 76 77 78 79 80
// 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.
81
//
Simon Marlow's avatar
Simon Marlow committed
82 83
// -----------------------------------------------------------------------------

Ben Gamari's avatar
Ben Gamari committed
84
#if defined(DEBUG)
85 86 87
void traceBegin (const char *str, ...);
void traceEnd (void);
#endif
88

Ben Gamari's avatar
Ben Gamari committed
89
#if defined(TRACING)
Simon Marlow's avatar
Simon Marlow committed
90

91
/*
92 93 94 95
 * Record a scheduler event
 */
#define traceSchedEvent(cap, tag, tso, other)   \
    if (RTS_UNLIKELY(TRACE_sched)) {            \
96 97 98 99 100 101
        traceSchedEvent_(cap, tag, tso, other, 0); \
    }

#define traceSchedEvent2(cap, tag, tso, info1, info2) \
    if (RTS_UNLIKELY(TRACE_sched)) {            \
        traceSchedEvent_(cap, tag, tso, info1, info2); \
102
    }
103

104
void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
105
                       StgTSO *tso, StgWord info1, StgWord info2);
106

107
/*
108
 * Record a GC event
109
 */
110 111 112
#define traceGcEvent(cap, tag)    \
    if (RTS_UNLIKELY(TRACE_gc)) { \
        traceGcEvent_(cap, tag);  \
113 114
    }

115
void traceGcEvent_ (Capability *cap, EventTypeNum tag);
116

117
/*
118 119 120 121 122 123 124 125 126
 * Record a GC event at the explicitly given timestamp
 */
#define traceGcEventAtT(cap, ts, tag)   \
    if (RTS_UNLIKELY(TRACE_gc)) {       \
        traceGcEventAtT_(cap, ts, tag); \
    }

void traceGcEventAtT_ (Capability *cap, StgWord64 ts, EventTypeNum tag);

127
/*
128 129 130 131 132 133 134 135 136
 * Record a heap event
 */
#define traceHeapEvent(cap, tag, heap_capset, info1) \
    if (RTS_UNLIKELY(TRACE_gc)) { \
        traceHeapEvent_(cap, tag, heap_capset, info1);  \
    }
void traceHeapEvent_ (Capability   *cap,
                      EventTypeNum  tag,
                      CapsetID      heap_capset,
137
                      W_          info1);
138 139

void traceEventHeapInfo_ (CapsetID    heap_capset,
140
                          uint32_t  gens,
141 142 143 144
                          W_        maxHeapSize,
                          W_        allocAreaSize,
                          W_        mblockSize,
                          W_        blockSize);
145 146 147

void traceEventGcStats_  (Capability *cap,
                          CapsetID    heap_capset,
148
                          uint32_t  gen,
149 150 151
                          W_        copied,
                          W_        slop,
                          W_        fragmentation,
152
                          uint32_t  par_n_threads,
153
                          W_        par_max_copied,
154 155
                          W_        par_tot_copied,
                          W_        par_balanced_copied);
156

157
/*
158 159 160 161 162 163 164 165 166 167 168 169 170 171
 * Record a spark event
 */
#define traceSparkEvent(cap, tag)         \
    if (RTS_UNLIKELY(TRACE_spark_full)) { \
        traceSparkEvent_(cap, tag, 0);    \
    }

#define traceSparkEvent2(cap, tag, other)  \
    if (RTS_UNLIKELY(TRACE_spark_full)) {  \
        traceSparkEvent_(cap, tag, other); \
    }

void traceSparkEvent_ (Capability *cap, EventTypeNum tag, StgWord info1);

172 173 174 175
// 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).

176
/*
177
 * Emit a trace message on a particular Capability
178
 */
179 180 181
#define traceCap(class, cap, msg, ...)          \
    if (RTS_UNLIKELY(class)) {                  \
        traceCap_(cap, msg, ##__VA_ARGS__);     \
182 183
    }

184
void traceCap_(Capability *cap, char *msg, ...);
185

186
/*
187
 * Emit a trace message
188
 */
189 190 191
#define trace(class, msg, ...)                  \
    if (RTS_UNLIKELY(class)) {                  \
        trace_(msg, ##__VA_ARGS__);             \
192 193
    }

194
void trace_(char *msg, ...);
195

196
/*
197
 * A message or event emitted by the program
198
 * Used by Debug.Trace.{traceEvent, traceEventIO}
199 200 201
 */
void traceUserMsg(Capability *cap, char *msg);

202
/*
203 204 205 206 207
 * A marker event emitted by the program
 * Used by Debug.Trace.{traceMarker, traceMarkerIO}
 */
void traceUserMarker(Capability *cap, char *msg);

Mitsutoshi Aoe's avatar
Mitsutoshi Aoe committed
208 209 210 211 212
/*
 * A binary message or event emitted by the program
 */
void traceUserBinaryMsg(Capability *cap, uint8_t *msg, size_t size);

213 214 215 216 217 218 219 220
/*
 * An event to record a Haskell thread's label/name
 * Used by GHC.Conc.labelThread
 */
void traceThreadLabel_(Capability *cap,
                       StgTSO     *tso,
                       char       *label);

221
/*
222
 * Emit a debug message (only when DEBUG is defined)
223
 */
Ben Gamari's avatar
Ben Gamari committed
224
#if defined(DEBUG)
225 226 227
#define debugTrace(class, msg, ...)             \
    if (RTS_UNLIKELY(class)) {                  \
        trace_(msg, ##__VA_ARGS__);             \
228 229 230 231 232
    }
#else
#define debugTrace(class, str, ...) /* nothing */
#endif

Ben Gamari's avatar
Ben Gamari committed
233
#if defined(DEBUG)
234 235 236 237 238 239 240 241
#define debugTraceCap(class, cap, msg, ...)      \
    if (RTS_UNLIKELY(class)) {                  \
        traceCap_(cap, msg, ##__VA_ARGS__);     \
    }
#else
#define debugTraceCap(class, cap, str, ...) /* nothing */
#endif

242
/*
243 244 245 246 247
 * Emit a message/event describing the state of a thread
 */
#define traceThreadStatus(class, tso)           \
    if (RTS_UNLIKELY(class)) {                  \
        traceThreadStatus_(tso);                \
248 249
    }

250
void traceThreadStatus_ (StgTSO *tso);
251

252
/*
253
 * Events for describing capabilities and capability sets in the eventlog
254
 */
255 256 257 258 259 260
#define traceCapEvent(cap, tag)                 \
    if (RTS_UNLIKELY(TRACE_cap)) {              \
        traceCapEvent_(cap, tag);               \
    }

void traceCapEvent_ (Capability   *cap,
261 262
                    EventTypeNum  tag);

263 264 265 266 267 268 269 270
#define traceCapsetEvent(cap, capset, info)     \
    if (RTS_UNLIKELY(TRACE_cap)) {              \
        traceCapsetEvent_(cap, capset, info);   \
    }

void traceCapsetEvent_ (EventTypeNum tag,
                        CapsetID     capset,
                        StgWord      info);
271

272 273
void traceWallClockTime_(void);

274 275
void traceOSProcessInfo_ (void);

Duncan Coutts's avatar
Duncan Coutts committed
276 277 278 279
void traceSparkCounters_ (Capability *cap,
                          SparkCounters counters,
                          StgWord remaining);

280 281 282 283 284 285 286 287 288
void traceTaskCreate_ (Task       *task,
                       Capability *cap);

void traceTaskMigrate_ (Task       *task,
                        Capability *cap,
                        Capability *new_cap);

void traceTaskDelete_ (Task       *task);

289 290 291 292
void traceHeapProfBegin(StgWord8 profile_id);
void traceHeapProfSampleBegin(StgInt era);
void traceHeapProfSampleString(StgWord8 profile_id,
                               const char *label, StgWord residency);
Ben Gamari's avatar
Ben Gamari committed
293
#if defined(PROFILING)
294 295 296 297 298 299 300 301 302
void traceHeapProfCostCentre(StgWord32 ccID,
                             const char *label,
                             const char *module,
                             const char *srcloc,
                             StgBool is_caf);
void traceHeapProfSampleCostCentre(StgWord8 profile_id,
                                   CostCentreStack *stack, StgWord residency);
#endif /* PROFILING */

303 304
void flushTrace(void);

305
#else /* !TRACING */
306

307
#define traceSchedEvent(cap, tag, tso, other) /* nothing */
308
#define traceSchedEvent2(cap, tag, tso, other, info) /* nothing */
309
#define traceGcEvent(cap, tag) /* nothing */
310
#define traceGcEventAtT(cap, ts, tag) /* nothing */
311 312
#define traceEventGcStats_(cap, heap_capset, gen, \
                           copied, slop, fragmentation, \
313 314
                           par_n_threads, par_max_copied, \
                           par_tot_copied, par_balanced_copied) /* nothing */
315 316 317 318
#define traceHeapEvent(cap, tag, heap_capset, info1) /* nothing */
#define traceEventHeapInfo_(heap_capset, gens, \
                            maxHeapSize, allocAreaSize, \
                            mblockSize, blockSize) /* nothing */
319 320
#define traceSparkEvent(cap, tag) /* nothing */
#define traceSparkEvent2(cap, tag, other) /* nothing */
321 322
#define traceCap(class, cap, msg, ...) /* nothing */
#define trace(class, msg, ...) /* nothing */
323
#define debugTrace(class, str, ...) /* nothing */
324
#define debugTraceCap(class, cap, str, ...) /* nothing */
325
#define traceThreadStatus(class, tso) /* nothing */
326
#define traceThreadLabel_(cap, tso, label) /* nothing */
327 328
#define traceCapEvent(cap, tag) /* nothing */
#define traceCapsetEvent(tag, capset, info) /* nothing */
329
#define traceWallClockTime_() /* nothing */
330
#define traceOSProcessInfo_() /* nothing */
Duncan Coutts's avatar
Duncan Coutts committed
331
#define traceSparkCounters_(cap, counters, remaining) /* nothing */
332 333 334
#define traceTaskCreate_(taskID, cap) /* nothing */
#define traceTaskMigrate_(taskID, cap, new_cap) /* nothing */
#define traceTaskDelete_(taskID) /* nothing */
335 336 337 338 339
#define traceHeapProfBegin(profile_id) /* nothing */
#define traceHeapProfCostCentre(ccID, label, module, srcloc, is_caf) /* nothing */
#define traceHeapProfSampleBegin(era) /* nothing */
#define traceHeapProfSampleCostCentre(profile_id, stack, residency) /* nothing */
#define traceHeapProfSampleString(profile_id, label, residency) /* nothing */
340

341 342
#define flushTrace() /* nothing */

343
#endif /* TRACING */
Simon Marlow's avatar
Simon Marlow committed
344

345 346 347 348 349 350
// 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);
351
void dtraceUserMarkerWrapper(Capability *cap, char *msg);
352 353 354 355 356 357 358 359 360 361 362 363 364

#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)
365 366
#define dtraceStopThread(cap, tid, status, info)        \
    HASKELLEVENT_STOP_THREAD(cap, tid, status, info)
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
#define dtraceThreadRunnable(cap, tid)                  \
    HASKELLEVENT_THREAD_RUNNABLE(cap, tid)
#define dtraceMigrateThread(cap, tid, new_cap)          \
    HASKELLEVENT_MIGRATE_THREAD(cap, tid, new_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)
383 384
#define dtraceThreadLabel(cap, tso, label)              \
    HASKELLEVENT_THREAD_LABEL(cap, tso, label)
385 386 387 388 389 390 391 392
#define dtraceCapCreate(cap)                            \
    HASKELLEVENT_CAP_CREATE(cap)
#define dtraceCapDelete(cap)                            \
    HASKELLEVENT_CAP_DELETE(cap)
#define dtraceCapEnable(cap)                            \
    HASKELLEVENT_CAP_ENABLE(cap)
#define dtraceCapDisable(cap)                           \
    HASKELLEVENT_CAP_DISABLE(cap)
393 394
#define dtraceUserMsg(cap, msg)                         \
    HASKELLEVENT_USER_MSG(cap, msg)
395 396
#define dtraceUserMarker(cap, msg)                      \
    HASKELLEVENT_USER_MARKER(cap, msg)
397 398 399 400 401 402
#define dtraceGcIdle(cap)                               \
    HASKELLEVENT_GC_IDLE(cap)
#define dtraceGcWork(cap)                               \
    HASKELLEVENT_GC_WORK(cap)
#define dtraceGcDone(cap)                               \
    HASKELLEVENT_GC_DONE(cap)
403 404
#define dtraceGcGlobalSync(cap)                         \
    HASKELLEVENT_GC_GLOBAL_SYNC(cap)
405 406 407 408
#define dtraceEventGcStats(heap_capset, gens,           \
                           copies, slop, fragmentation, \
                           par_n_threads,               \
                           par_max_copied,              \
409
                           par_tot_copied,              \
Moritz Angermann's avatar
Moritz Angermann committed
410
                           par_balanced_copied)         \
411 412 413 414
    HASKELLEVENT_GC_STATS(heap_capset, gens,            \
                           copies, slop, fragmentation, \
                           par_n_threads,               \
                           par_max_copied,              \
415
                           par_balanced_copied,         \
Ian Lynagh's avatar
Ian Lynagh committed
416
                           par_tot_copied)
417 418 419 420 421 422 423 424 425 426 427
#define dtraceHeapInfo(heap_capset, gens,               \
                       maxHeapSize, allocAreaSize,      \
                       mblockSize, blockSize)           \
    HASKELLEVENT_HEAP_INFO(heap_capset, gens,           \
                           maxHeapSize, allocAreaSize,  \
                           mblockSize, blockSize)
#define dtraceEventHeapAllocated(cap, heap_capset,      \
                                 allocated)             \
    HASKELLEVENT_HEAP_ALLOCATED(cap, heap_capset,       \
                                allocated)
#define dtraceEventHeapSize(heap_capset, size)          \
chak@cse.unsw.edu.au.'s avatar
chak@cse.unsw.edu.au. committed
428
    HASKELLEVENT_HEAP_SIZE(heap_capset, size)
429 430
#define dtraceEventHeapLive(heap_capset, live)          \
    HASKELLEVENT_HEAP_LIVE(heap_capset, live)
431 432 433 434 435 436 437 438
#define dtraceCapsetCreate(capset, capset_type)         \
    HASKELLEVENT_CAPSET_CREATE(capset, capset_type)
#define dtraceCapsetDelete(capset)                      \
    HASKELLEVENT_CAPSET_DELETE(capset)
#define dtraceCapsetAssignCap(capset, capno)            \
    HASKELLEVENT_CAPSET_ASSIGN_CAP(capset, capno)
#define dtraceCapsetRemoveCap(capset, capno)            \
    HASKELLEVENT_CAPSET_REMOVE_CAP(capset, capno)
Duncan Coutts's avatar
Duncan Coutts committed
439 440
#define dtraceSparkCounters(cap, a, b, c, d, e, f, g) \
    HASKELLEVENT_SPARK_COUNTERS(cap, a, b, c, d, e, f, g)
441 442 443 444 445 446 447 448 449 450 451 452 453 454
#define dtraceSparkCreate(cap)                         \
    HASKELLEVENT_SPARK_CREATE(cap)
#define dtraceSparkDud(cap)                             \
    HASKELLEVENT_SPARK_DUD(cap)
#define dtraceSparkOverflow(cap)                        \
    HASKELLEVENT_SPARK_OVERFLOW(cap)
#define dtraceSparkRun(cap)                             \
    HASKELLEVENT_SPARK_RUN(cap)
#define dtraceSparkSteal(cap, victim_cap)               \
    HASKELLEVENT_SPARK_STEAL(cap, victim_cap)
#define dtraceSparkFizzle(cap)                          \
    HASKELLEVENT_SPARK_FIZZLE(cap)
#define dtraceSparkGc(cap)                              \
    HASKELLEVENT_SPARK_GC(cap)
Ian Lynagh's avatar
Ian Lynagh committed
455 456
#define dtraceTaskCreate(taskID, cap, tid)              \
    HASKELLEVENT_TASK_CREATE(taskID, cap, tid)
457 458 459 460
#define dtraceTaskMigrate(taskID, cap, new_cap)         \
    HASKELLEVENT_TASK_MIGRATE(taskID, cap, new_cap)
#define dtraceTaskDelete(taskID)                        \
    HASKELLEVENT_TASK_DELETE(taskID)
461 462 463 464 465

#else /* !defined(DTRACE) */

#define dtraceCreateThread(cap, tid)                    /* nothing */
#define dtraceRunThread(cap, tid)                       /* nothing */
466
#define dtraceStopThread(cap, tid, status, info)        /* nothing */
467 468 469 470 471 472 473 474
#define dtraceThreadRunnable(cap, tid)                  /* nothing */
#define dtraceMigrateThread(cap, tid, new_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 */
475
#define dtraceThreadLabel(cap, tso, label)              /* nothing */
476
#define dtraceUserMsg(cap, msg)                         /* nothing */
477
#define dtraceUserMarker(cap, msg)                      /* nothing */
478 479 480
#define dtraceGcIdle(cap)                               /* nothing */
#define dtraceGcWork(cap)                               /* nothing */
#define dtraceGcDone(cap)                               /* nothing */
481
#define dtraceGcGlobalSync(cap)                         /* nothing */
482 483 484 485
#define dtraceEventGcStats(heap_capset, gens,           \
                           copies, slop, fragmentation, \
                           par_n_threads,               \
                           par_max_copied,              \
486 487
                           par_tot_copied,              \
                           par_balanced_copied)         /* nothing */
488 489 490 491 492 493 494
#define dtraceHeapInfo(heap_capset, gens,               \
                       maxHeapSize, allocAreaSize,      \
                       mblockSize, blockSize)           /* nothing */
#define dtraceEventHeapAllocated(cap, heap_capset,      \
                                 allocated)             /* nothing */
#define dtraceEventHeapSize(heap_capset, size)          /* nothing */
#define dtraceEventHeapLive(heap_capset, live)          /* nothing */
495 496 497 498
#define dtraceCapCreate(cap)                            /* nothing */
#define dtraceCapDelete(cap)                            /* nothing */
#define dtraceCapEnable(cap)                            /* nothing */
#define dtraceCapDisable(cap)                           /* nothing */
499 500 501 502
#define dtraceCapsetCreate(capset, capset_type)         /* nothing */
#define dtraceCapsetDelete(capset)                      /* nothing */
#define dtraceCapsetAssignCap(capset, capno)            /* nothing */
#define dtraceCapsetRemoveCap(capset, capno)            /* nothing */
Duncan Coutts's avatar
Duncan Coutts committed
503
#define dtraceSparkCounters(cap, a, b, c, d, e, f, g)   /* nothing */
504 505 506 507 508 509 510
#define dtraceSparkCreate(cap)                          /* nothing */
#define dtraceSparkDud(cap)                             /* nothing */
#define dtraceSparkOverflow(cap)                        /* nothing */
#define dtraceSparkRun(cap)                             /* nothing */
#define dtraceSparkSteal(cap, victim_cap)               /* nothing */
#define dtraceSparkFizzle(cap)                          /* nothing */
#define dtraceSparkGc(cap)                              /* nothing */
Ian Lynagh's avatar
Ian Lynagh committed
511
#define dtraceTaskCreate(taskID, cap, tid)              /* nothing */
512 513
#define dtraceTaskMigrate(taskID, cap, new_cap)         /* nothing */
#define dtraceTaskDelete(taskID)                        /* nothing */
514 515 516 517 518 519 520 521 522 523 524 525

#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
526
//   very cheap - usually, one no-op.  Consequently, dtrace can be used with
527 528 529 530
//   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.
531
//
532 533
// -----------------------------------------------------------------------------

534
INLINE_HEADER void traceEventCreateThread(Capability *cap STG_UNUSED,
535 536
                                          StgTSO     *tso STG_UNUSED)
{
537
    traceSchedEvent(cap, EVENT_CREATE_THREAD, tso, tso->stackobj->stack_size);
538 539 540
    dtraceCreateThread((EventCapNo)cap->no, (EventThreadID)tso->id);
}

541
INLINE_HEADER void traceEventRunThread(Capability *cap STG_UNUSED,
542 543 544 545 546 547
                                       StgTSO     *tso STG_UNUSED)
{
    traceSchedEvent(cap, EVENT_RUN_THREAD, tso, tso->what_next);
    dtraceRunThread((EventCapNo)cap->no, (EventThreadID)tso->id);
}

548 549
INLINE_HEADER void traceEventStopThread(Capability          *cap    STG_UNUSED,
                                        StgTSO              *tso    STG_UNUSED,
550 551
                                        StgThreadReturnCode  status STG_UNUSED,
                                        StgWord32           info    STG_UNUSED)
552
{
553
    traceSchedEvent2(cap, EVENT_STOP_THREAD, tso, status, info);
554
    dtraceStopThread((EventCapNo)cap->no, (EventThreadID)tso->id,
555
                     (EventThreadStatus)status, (EventThreadID)info);
556 557
}

558
INLINE_HEADER void traceEventMigrateThread(Capability *cap     STG_UNUSED,
559
                                           StgTSO     *tso     STG_UNUSED,
560
                                           uint32_t    new_cap STG_UNUSED)
561 562 563 564 565 566
{
    traceSchedEvent(cap, EVENT_MIGRATE_THREAD, tso, new_cap);
    dtraceMigrateThread((EventCapNo)cap->no, (EventThreadID)tso->id,
                        (EventCapNo)new_cap);
}

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
INLINE_HEADER void traceCapCreate(Capability *cap STG_UNUSED)
{
    traceCapEvent(cap, EVENT_CAP_CREATE);
    dtraceCapCreate((EventCapNo)cap->no);
}

INLINE_HEADER void traceCapDelete(Capability *cap STG_UNUSED)
{
    traceCapEvent(cap, EVENT_CAP_DELETE);
    dtraceCapDelete((EventCapNo)cap->no);
}

INLINE_HEADER void traceCapEnable(Capability *cap STG_UNUSED)
{
    traceCapEvent(cap, EVENT_CAP_ENABLE);
    dtraceCapEnable((EventCapNo)cap->no);
}

INLINE_HEADER void traceCapDisable(Capability *cap STG_UNUSED)
586
{
587 588
    traceCapEvent(cap, EVENT_CAP_DISABLE);
    dtraceCapDisable((EventCapNo)cap->no);
589 590
}

591
INLINE_HEADER void traceEventThreadWakeup(Capability *cap       STG_UNUSED,
592
                                          StgTSO     *tso       STG_UNUSED,
593
                                          uint32_t    other_cap STG_UNUSED)
594 595 596 597 598 599
{
    traceSchedEvent(cap, EVENT_THREAD_WAKEUP, tso, other_cap);
    dtraceThreadWakeup((EventCapNo)cap->no, (EventThreadID)tso->id,
                       (EventCapNo)other_cap);
}

600 601 602 603 604 605 606 607 608 609
INLINE_HEADER void traceThreadLabel(Capability *cap   STG_UNUSED,
                                    StgTSO     *tso   STG_UNUSED,
                                    char       *label STG_UNUSED)
{
    if (RTS_UNLIKELY(TRACE_sched)) {
        traceThreadLabel_(cap, tso, label);
    }
    dtraceThreadLabel((EventCapNo)cap->no, (EventThreadID)tso->id, label);
}

610 611
INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
{
612
    traceGcEvent(cap, EVENT_GC_START);
613 614 615
    dtraceGcStart((EventCapNo)cap->no);
}

616 617 618 619 620 621 622
INLINE_HEADER void traceEventGcStartAtT(Capability *cap STG_UNUSED,
                                        StgWord64   ts  STG_UNUSED)
{
    traceGcEventAtT(cap, ts, EVENT_GC_START);
    dtraceGcStart((EventCapNo)cap->no);
}

623 624
INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
{
625
    traceGcEvent(cap, EVENT_GC_END);
626 627 628
    dtraceGcEnd((EventCapNo)cap->no);
}

629 630 631 632 633 634 635
INLINE_HEADER void traceEventGcEndAtT(Capability *cap STG_UNUSED,
                                      StgWord64   ts  STG_UNUSED)
{
    traceGcEventAtT(cap, ts, EVENT_GC_END);
    dtraceGcEnd((EventCapNo)cap->no);
}

636 637
INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
{
638
    traceGcEvent(cap, EVENT_REQUEST_SEQ_GC);
639 640 641 642 643
    dtraceRequestSeqGc((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventRequestParGc(Capability *cap STG_UNUSED)
{
644
    traceGcEvent(cap, EVENT_REQUEST_PAR_GC);
645 646 647
    dtraceRequestParGc((EventCapNo)cap->no);
}

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
INLINE_HEADER void traceEventGcIdle(Capability *cap STG_UNUSED)
{
    traceGcEvent(cap, EVENT_GC_IDLE);
    dtraceGcIdle((EventCapNo)cap->no);
}

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

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

666 667 668 669 670 671
INLINE_HEADER void traceEventGcGlobalSync(Capability *cap STG_UNUSED)
{
    traceGcEvent(cap, EVENT_GC_GLOBAL_SYNC);
    dtraceGcGlobalSync((EventCapNo)cap->no);
}

672 673
INLINE_HEADER void traceEventGcStats(Capability *cap            STG_UNUSED,
                                     CapsetID    heap_capset    STG_UNUSED,
674
                                     uint32_t    gen            STG_UNUSED,
675 676 677
                                     W_        copied         STG_UNUSED,
                                     W_        slop           STG_UNUSED,
                                     W_        fragmentation  STG_UNUSED,
678
                                     uint32_t  par_n_threads  STG_UNUSED,
679
                                     W_        par_max_copied STG_UNUSED,
680 681
                                     W_        par_tot_copied STG_UNUSED,
                                     W_        par_balanced_copied STG_UNUSED)
682 683 684 685
{
    if (RTS_UNLIKELY(TRACE_gc)) {
        traceEventGcStats_(cap, heap_capset, gen,
                           copied, slop, fragmentation,
686 687
                           par_n_threads, par_max_copied,
                           par_tot_copied, par_balanced_copied);
688
    }
Ian Lynagh's avatar
Ian Lynagh committed
689 690
    dtraceEventGcStats(heap_capset, gen,
                       copied, slop, fragmentation,
691 692
                       par_n_threads, par_max_copied,
                       par_tot_copied, par_balanced_copied);
693 694 695
}

INLINE_HEADER void traceEventHeapInfo(CapsetID    heap_capset   STG_UNUSED,
696
                                      uint32_t  gens          STG_UNUSED,
697 698 699 700
                                      W_        maxHeapSize   STG_UNUSED,
                                      W_        allocAreaSize STG_UNUSED,
                                      W_        mblockSize    STG_UNUSED,
                                      W_        blockSize     STG_UNUSED)
701 702 703 704 705 706 707 708 709 710 711 712 713
{
    if (RTS_UNLIKELY(TRACE_gc)) {
        traceEventHeapInfo_(heap_capset, gens,
                            maxHeapSize, allocAreaSize,
                            mblockSize, blockSize);
    }
    dtraceHeapInfo(heap_capset, gens,
                   maxHeapSize, allocAreaSize,
                   mblockSize, blockSize);
}

INLINE_HEADER void traceEventHeapAllocated(Capability *cap         STG_UNUSED,
                                           CapsetID    heap_capset STG_UNUSED,
714
                                           W_        allocated   STG_UNUSED)
715 716 717 718 719 720 721
{
    traceHeapEvent(cap, EVENT_HEAP_ALLOCATED, heap_capset, allocated);
    dtraceEventHeapAllocated((EventCapNo)cap->no, heap_capset, allocated);
}

INLINE_HEADER void traceEventHeapSize(Capability *cap         STG_UNUSED,
                                      CapsetID    heap_capset STG_UNUSED,
722
                                      W_        heap_size   STG_UNUSED)
723 724 725 726 727 728 729
{
    traceHeapEvent(cap, EVENT_HEAP_SIZE, heap_capset, heap_size);
    dtraceEventHeapSize(heap_capset, heap_size);
}

INLINE_HEADER void traceEventHeapLive(Capability *cap         STG_UNUSED,
                                      CapsetID    heap_capset STG_UNUSED,
730
                                      W_        heap_live   STG_UNUSED)
731 732 733 734 735
{
    traceHeapEvent(cap, EVENT_HEAP_LIVE, heap_capset, heap_live);
    dtraceEventHeapLive(heap_capset, heap_live);
}

736 737 738
INLINE_HEADER void traceCapsetCreate(CapsetID   capset      STG_UNUSED,
                                     CapsetType capset_type STG_UNUSED)
{
739
    traceCapsetEvent(EVENT_CAPSET_CREATE, capset, capset_type);
740 741 742 743 744
    dtraceCapsetCreate(capset, capset_type);
}

INLINE_HEADER void traceCapsetDelete(CapsetID capset STG_UNUSED)
{
745
    traceCapsetEvent(EVENT_CAPSET_DELETE, capset, 0);
746 747 748 749
    dtraceCapsetDelete(capset);
}

INLINE_HEADER void traceCapsetAssignCap(CapsetID capset STG_UNUSED,
750
                                        uint32_t capno  STG_UNUSED)
751
{
752
    traceCapsetEvent(EVENT_CAPSET_ASSIGN_CAP, capset, capno);
753 754 755 756
    dtraceCapsetAssignCap(capset, capno);
}

INLINE_HEADER void traceCapsetRemoveCap(CapsetID capset STG_UNUSED,
757
                                        uint32_t capno  STG_UNUSED)
758
{
759
    traceCapsetEvent(EVENT_CAPSET_REMOVE_CAP, capset, capno);
760 761 762
    dtraceCapsetRemoveCap(capset, capno);
}

763 764 765 766 767 768
INLINE_HEADER void traceWallClockTime(void)
{
    traceWallClockTime_();
    /* Note: no DTrace equivalent because it is available to DTrace directly */
}

769 770 771 772 773 774 775
INLINE_HEADER void traceOSProcessInfo(void)
{
    traceOSProcessInfo_();
    /* Note: no DTrace equivalent because all this OS process info
     * is available to DTrace directly */
}

776
INLINE_HEADER void traceEventCreateSparkThread(Capability  *cap      STG_UNUSED,
777 778 779 780 781 782
                                               StgThreadID spark_tid STG_UNUSED)
{
    traceSparkEvent2(cap, EVENT_CREATE_SPARK_THREAD, spark_tid);
    dtraceCreateSparkThread((EventCapNo)cap->no, (EventThreadID)spark_tid);
}

Duncan Coutts's avatar
Duncan Coutts committed
783 784
INLINE_HEADER void traceSparkCounters(Capability *cap STG_UNUSED)
{
Ben Gamari's avatar
Ben Gamari committed
785
#if defined(THREADED_RTS)
786
    if (RTS_UNLIKELY(TRACE_spark_sampled)) {
Duncan Coutts's avatar
Duncan Coutts committed
787 788 789 790 791 792 793 794 795 796
        traceSparkCounters_(cap, cap->spark_stats, sparkPoolSize(cap->sparks));
    }
    dtraceSparkCounters((EventCapNo)cap->no,
                        cap->spark_stats.created,
                        cap->spark_stats.dud,
                        cap->spark_stats.overflowed,
                        cap->spark_stats.converted,
                        cap->spark_stats.gcd,
                        cap->spark_stats.fizzled,
                        sparkPoolSize(cap->sparks));
797
#endif
Duncan Coutts's avatar
Duncan Coutts committed
798 799
}

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
INLINE_HEADER void traceEventSparkCreate(Capability *cap STG_UNUSED)
{
    traceSparkEvent(cap, EVENT_SPARK_CREATE);
    dtraceSparkCreate((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventSparkDud(Capability *cap STG_UNUSED)
{
    traceSparkEvent(cap, EVENT_SPARK_DUD);
    dtraceSparkDud((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventSparkOverflow(Capability *cap STG_UNUSED)
{
    traceSparkEvent(cap, EVENT_SPARK_OVERFLOW);
    dtraceSparkOverflow((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventSparkRun(Capability *cap STG_UNUSED)
{
    traceSparkEvent(cap, EVENT_SPARK_RUN);
    dtraceSparkRun((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventSparkSteal(Capability *cap STG_UNUSED,
825
                                        uint32_t    victim_cap STG_UNUSED)
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
{
    traceSparkEvent2(cap, EVENT_SPARK_STEAL, victim_cap);
    dtraceSparkSteal((EventCapNo)cap->no, (EventCapNo)victim_cap);
}

INLINE_HEADER void traceEventSparkFizzle(Capability *cap STG_UNUSED)
{
    traceSparkEvent(cap, EVENT_SPARK_FIZZLE);
    dtraceSparkFizzle((EventCapNo)cap->no);
}

INLINE_HEADER void traceEventSparkGC(Capability *cap STG_UNUSED)
{
    traceSparkEvent(cap, EVENT_SPARK_GC);
    dtraceSparkGc((EventCapNo)cap->no);
}
Duncan Coutts's avatar
Duncan Coutts committed
842

843 844 845 846 847 848 849 850 851 852 853 854 855
INLINE_HEADER void traceTaskCreate(Task       *task STG_UNUSED,
                                   Capability *cap  STG_UNUSED)
{
    ASSERT(task->cap == cap);
    // TODO: asserting task->cap == NULL would be much stronger
    // (the intention being that the task structure is just created and empty)
    // but would require large changes of traceTaskCreate calls.
    ASSERT(cap != NULL);
    // A new task gets associated with a cap. We also record
    // the kernel thread id of the task, which should never change.
    if (RTS_UNLIKELY(TRACE_sched)) {
        traceTaskCreate_(task, cap);
    }
Ian Lynagh's avatar
Ian Lynagh committed
856 857 858
    dtraceTaskCreate(serialisableTaskId(task),
                     (EventCapNo)cap->no,
                     kernelThreadId());
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
}

INLINE_HEADER void traceTaskMigrate(Task       *task    STG_UNUSED,
                                    Capability *cap     STG_UNUSED,
                                    Capability *new_cap STG_UNUSED)
{
    ASSERT(task->cap == cap);
    ASSERT(cap != NULL);
    ASSERT(cap != new_cap);
    ASSERT(new_cap != NULL);
    // A task migrates from a cap to another.
    if (RTS_UNLIKELY(TRACE_sched)) {
        traceTaskMigrate_(task, cap, new_cap);
    }
    dtraceTaskMigrate(serialisableTaskId(task), (EventCapNo)cap->no,
                                                (EventCapNo)new_cap->no);
}

INLINE_HEADER void traceTaskDelete(Task *task STG_UNUSED)
{
    ASSERT(task->cap != NULL);
    if (RTS_UNLIKELY(TRACE_sched)) {
        traceTaskDelete_(task);
    }
    dtraceTaskDelete(serialisableTaskId(task));
}

886
#include "EndPrivate.h"