EventLog.c 40.4 KB
Newer Older
Simon Marlow's avatar
Simon Marlow committed
1 2 3 4 5 6 7 8
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team, 2008-2009
 *
 * Support for fast binary event logging.
 *
 * ---------------------------------------------------------------------------*/

Simon Marlow's avatar
Simon Marlow committed
9
#include "PosixSource.h"
Simon Marlow's avatar
Simon Marlow committed
10
#include "Rts.h"
Simon Marlow's avatar
Simon Marlow committed
11

12 13 14
#ifdef TRACING

#include "Trace.h"
Simon Marlow's avatar
Simon Marlow committed
15 16 17
#include "Capability.h"
#include "RtsUtils.h"
#include "Stats.h"
18
#include "EventLog.h"
Simon Marlow's avatar
Simon Marlow committed
19

20
#include <string.h>
Simon Marlow's avatar
Simon Marlow committed
21
#include <stdio.h>
22 23 24 25 26 27 28 29 30
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

// PID of the process that writes to event_log_filename (#4512)
static pid_t event_log_pid = -1;
Simon Marlow's avatar
Simon Marlow committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44

static char *event_log_filename = NULL;

// File for logging events
FILE *event_log_file = NULL;

#define EVENT_LOG_SIZE 2 * (1024 * 1024) // 2MB

static int flushCount;

// Struct for record keeping of buffer to store event types and events.
typedef struct _EventsBuf {
  StgInt8 *begin;
  StgInt8 *pos;
Simon Marlow's avatar
Simon Marlow committed
45
  StgInt8 *marker;
Simon Marlow's avatar
Simon Marlow committed
46
  StgWord64 size;
47
  EventCapNo capno; // which capability this buffer belongs to, or -1
Simon Marlow's avatar
Simon Marlow committed
48 49
} EventsBuf;

50 51 52 53 54 55
EventsBuf *capEventBuf; // one EventsBuf for each Capability

EventsBuf eventBuf; // an EventsBuf not associated with any Capability
#ifdef THREADED_RTS
Mutex eventBufMutex; // protected by this mutex
#endif
Simon Marlow's avatar
Simon Marlow committed
56 57

char *EventDesc[] = {
58 59 60 61 62 63
  [EVENT_CREATE_THREAD]       = "Create thread",
  [EVENT_RUN_THREAD]          = "Run thread",
  [EVENT_STOP_THREAD]         = "Stop thread",
  [EVENT_THREAD_RUNNABLE]     = "Thread runnable",
  [EVENT_MIGRATE_THREAD]      = "Migrate thread",
  [EVENT_THREAD_WAKEUP]       = "Wakeup thread",
64
  [EVENT_THREAD_LABEL]        = "Thread label",
65 66 67 68
  [EVENT_CAP_CREATE]          = "Create capability",
  [EVENT_CAP_DELETE]          = "Delete capability",
  [EVENT_CAP_DISABLE]         = "Disable capability",
  [EVENT_CAP_ENABLE]          = "Enable capability",
69 70 71 72
  [EVENT_GC_START]            = "Starting GC",
  [EVENT_GC_END]              = "Finished GC",
  [EVENT_REQUEST_SEQ_GC]      = "Request sequential GC",
  [EVENT_REQUEST_PAR_GC]      = "Request parallel GC",
73
  [EVENT_GC_GLOBAL_SYNC]      = "Synchronise stop-the-world GC",
74 75 76 77 78
  [EVENT_GC_STATS_GHC]        = "GC statistics",
  [EVENT_HEAP_INFO_GHC]       = "Heap static parameters",
  [EVENT_HEAP_ALLOCATED]      = "Total heap mem ever allocated",
  [EVENT_HEAP_SIZE]           = "Current heap size",
  [EVENT_HEAP_LIVE]           = "Current heap live data",
79 80
  [EVENT_CREATE_SPARK_THREAD] = "Create spark thread",
  [EVENT_LOG_MSG]             = "Log message",
81
  [EVENT_USER_MSG]            = "User message",
82
  [EVENT_USER_MARKER]         = "User marker",
83 84 85
  [EVENT_GC_IDLE]             = "GC idle",
  [EVENT_GC_WORK]             = "GC working",
  [EVENT_GC_DONE]             = "GC done",
86 87 88 89
  [EVENT_BLOCK_MARKER]        = "Block marker",
  [EVENT_CAPSET_CREATE]       = "Create capability set",
  [EVENT_CAPSET_DELETE]       = "Delete capability set",
  [EVENT_CAPSET_ASSIGN_CAP]   = "Add capability to capability set",
90 91 92 93 94
  [EVENT_CAPSET_REMOVE_CAP]   = "Remove capability from capability set",
  [EVENT_RTS_IDENTIFIER]      = "RTS name and version",
  [EVENT_PROGRAM_ARGS]        = "Program arguments",
  [EVENT_PROGRAM_ENV]         = "Program environment variables",
  [EVENT_OSPROCESS_PID]       = "Process ID",
Duncan Coutts's avatar
Duncan Coutts committed
95
  [EVENT_OSPROCESS_PPID]      = "Parent process ID",
96
  [EVENT_WALL_CLOCK_TIME]     = "Wall clock time",
97 98 99 100 101 102 103 104
  [EVENT_SPARK_COUNTERS]      = "Spark counters",
  [EVENT_SPARK_CREATE]        = "Spark create",
  [EVENT_SPARK_DUD]           = "Spark dud",
  [EVENT_SPARK_OVERFLOW]      = "Spark overflow",
  [EVENT_SPARK_RUN]           = "Spark run",
  [EVENT_SPARK_STEAL]         = "Spark steal",
  [EVENT_SPARK_FIZZLE]        = "Spark fizzle",
  [EVENT_SPARK_GC]            = "Spark GC",
105 106 107
  [EVENT_TASK_CREATE]         = "Task create",
  [EVENT_TASK_MIGRATE]        = "Task migrate",
  [EVENT_TASK_DELETE]         = "Task delete",
108
  [EVENT_HACK_BUG_T9003]      = "Empty event for bug #9003",
109 110 111 112 113
  [EVENT_HEAP_PROF_BEGIN]     = "Start of heap profile",
  [EVENT_HEAP_PROF_COST_CENTRE]   = "Cost center definition",
  [EVENT_HEAP_PROF_SAMPLE_BEGIN]  = "Start of heap profile sample",
  [EVENT_HEAP_PROF_SAMPLE_STRING] = "Heap profile string sample",
  [EVENT_HEAP_PROF_SAMPLE_COST_CENTRE] = "Heap profile cost-centre sample",
Simon Marlow's avatar
Simon Marlow committed
114 115
};

116
// Event type.
Simon Marlow's avatar
Simon Marlow committed
117 118 119

typedef struct _EventType {
  EventTypeNum etNum;  // Event Type number.
120
  uint32_t   size;     // size of the payload in bytes
Simon Marlow's avatar
Simon Marlow committed
121 122 123
  char *desc;     // Description
} EventType;

124
EventType eventTypes[NUM_GHC_EVENT_TAGS];
Simon Marlow's avatar
Simon Marlow committed
125

126
static void initEventsBuf(EventsBuf* eb, StgWord64 size, EventCapNo capno);
Simon Marlow's avatar
Simon Marlow committed
127
static void resetEventsBuf(EventsBuf* eb);
Simon Marlow's avatar
Simon Marlow committed
128
static void printAndClearEventBuf (EventsBuf *eventsBuf);
Simon Marlow's avatar
Simon Marlow committed
129 130 131

static void postEventType(EventsBuf *eb, EventType *et);

132
static void postLogMsg(EventsBuf *eb, EventTypeNum type, char *msg, va_list ap);
133

Simon Marlow's avatar
Simon Marlow committed
134 135 136
static void postBlockMarker(EventsBuf *eb);
static void closeBlockMarker(EventsBuf *ebuf);

Simon Marlow's avatar
Simon Marlow committed
137
static StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum);
138
static StgBool hasRoomForVariableEvent(EventsBuf *eb, uint32_t payload_bytes);
Simon Marlow's avatar
Simon Marlow committed
139

140 141 142
static void ensureRoomForEvent(EventsBuf *eb, EventTypeNum tag);
static int ensureRoomForVariableEvent(EventsBuf *eb, StgWord16 size);

143 144
static inline void postWord8(EventsBuf *eb, StgWord8 i)
{
145
    *(eb->pos++) = i;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
}

static inline void postWord16(EventsBuf *eb, StgWord16 i)
{
    postWord8(eb, (StgWord8)(i >> 8));
    postWord8(eb, (StgWord8)i);
}

static inline void postWord32(EventsBuf *eb, StgWord32 i)
{
    postWord16(eb, (StgWord16)(i >> 16));
    postWord16(eb, (StgWord16)i);
}

static inline void postWord64(EventsBuf *eb, StgWord64 i)
{
    postWord32(eb, (StgWord32)(i >> 32));
    postWord32(eb, (StgWord32)i);
}

166
static inline void postBuf(EventsBuf *eb, StgWord8 *buf, uint32_t size)
167 168 169 170 171
{
    memcpy(eb->pos, buf, size);
    eb->pos += size;
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/* Post a null-terminated string to the event log.
 * It is the caller's responsibility to ensure that there is
 * enough room for strlen(buf)+1 bytes.
 */
static inline void postString(EventsBuf *eb, const char *buf)
{
    if (buf) {
        int len = strlen(buf);
        ASSERT(eb->begin + eb->size > eb->pos + len);
        memcpy(eb->pos, buf, len);
        eb->pos += len;
    }
    *eb->pos = 0;
    eb->pos++;
}

Simon Marlow's avatar
Simon Marlow committed
188
static inline StgWord64 time_ns(void)
Simon Marlow's avatar
Simon Marlow committed
189
{ return TimeToNS(stat_getElapsedTime()); }
Simon Marlow's avatar
Simon Marlow committed
190

191 192 193
static inline void postEventTypeNum(EventsBuf *eb, EventTypeNum etNum)
{ postWord16(eb, etNum); }

Simon Marlow's avatar
Simon Marlow committed
194 195
static inline void postTimestamp(EventsBuf *eb)
{ postWord64(eb, time_ns()); }
196

197
static inline void postThreadID(EventsBuf *eb, EventThreadID id)
198 199
{ postWord32(eb,id); }

200
static inline void postCapNo(EventsBuf *eb, EventCapNo no)
201 202
{ postWord16(eb,no); }

203 204 205 206 207 208
static inline void postCapsetID(EventsBuf *eb, EventCapsetID id)
{ postWord32(eb,id); }

static inline void postCapsetType(EventsBuf *eb, EventCapsetType type)
{ postWord16(eb,type); }

209 210 211 212 213 214 215 216 217
static inline void postOSProcessId(EventsBuf *eb, pid_t pid)
{ postWord32(eb, pid); }

static inline void postKernelThreadId(EventsBuf *eb, EventKernelThreadId tid)
{ postWord64(eb, tid); }

static inline void postTaskId(EventsBuf *eb, EventTaskId tUniq)
{ postWord64(eb, tUniq); }

218 219 220
static inline void postPayloadSize(EventsBuf *eb, EventPayloadSize size)
{ postWord16(eb,size); }

Simon Marlow's avatar
Simon Marlow committed
221 222 223 224
static inline void postEventHeader(EventsBuf *eb, EventTypeNum type)
{
    postEventTypeNum(eb, type);
    postTimestamp(eb);
225
}
Simon Marlow's avatar
Simon Marlow committed
226

227 228 229 230 231 232
static inline void postInt8(EventsBuf *eb, StgInt8 i)
{ postWord8(eb, (StgWord8)i); }

static inline void postInt32(EventsBuf *eb, StgInt32 i)
{ postWord32(eb, (StgWord32)i); }

233
#define EVENT_SIZE_DYNAMIC (-1)
Simon Marlow's avatar
Simon Marlow committed
234 235 236 237 238

void
initEventLogging(void)
{
    StgWord8 t, c;
239
    uint32_t n_caps;
240 241 242 243 244 245 246 247 248 249 250 251 252 253
    char *prog;

    prog = stgMallocBytes(strlen(prog_name) + 1, "initEventLogging");
    strcpy(prog, prog_name);
#ifdef mingw32_HOST_OS
    // on Windows, drop the .exe suffix if there is one
    {
        char *suff;
        suff = strrchr(prog,'.');
        if (suff != NULL && !strcmp(suff,".exe")) {
            *suff = '\0';
        }
    }
#endif
Simon Marlow's avatar
Simon Marlow committed
254

255
    event_log_filename = stgMallocBytes(strlen(prog)
256 257
                                        + 10 /* .%d */
                                        + 10 /* .eventlog */,
Simon Marlow's avatar
Simon Marlow committed
258 259
                                        "initEventLogging");

260
    if (sizeof(EventDesc) / sizeof(char*) != NUM_GHC_EVENT_TAGS) {
Simon Marlow's avatar
Simon Marlow committed
261 262
        barf("EventDesc array has the wrong number of elements");
    }
263 264 265

    if (event_log_pid == -1) { // #4512
        // Single process
266
        sprintf(event_log_filename, "%s.eventlog", prog);
267 268 269 270 271
        event_log_pid = getpid();
    } else {
        // Forked process, eventlog already started by the parent
        // before fork
        event_log_pid = getpid();
Ian Lynagh's avatar
Ian Lynagh committed
272 273 274
        // We don't have a FMT* symbol for pid_t, so we go via Word64
        // to be sure of not losing range. It would be nicer to have a
        // FMT* symbol or similar, though.
275 276
        sprintf(event_log_filename, "%s.%" FMT_Word64 ".eventlog",
                prog, (StgWord64)event_log_pid);
277
    }
278
    stgFree(prog);
279

Simon Marlow's avatar
Simon Marlow committed
280 281
    /* Open event log file for writing. */
    if ((event_log_file = fopen(event_log_filename, "wb")) == NULL) {
Simon Marlow's avatar
Simon Marlow committed
282
        sysErrorBelch("initEventLogging: can't open %s", event_log_filename);
283
        stg_exit(EXIT_FAILURE);
Simon Marlow's avatar
Simon Marlow committed
284 285
    }

286
    /*
Simon Marlow's avatar
Simon Marlow committed
287 288 289 290 291 292
     * Allocate buffer(s) to store events.
     * Create buffer large enough for the header begin marker, all event
     * types, and header end marker to prevent checking if buffer has room
     * for each of these steps, and remove the need to flush the buffer to
     * disk during initialization.
     *
293
     * Use a single buffer to store the header with event types, then flush
Simon Marlow's avatar
Simon Marlow committed
294 295
     * the buffer so all buffers are empty for writing events.
     */
296 297
#ifdef THREADED_RTS
    // XXX n_capabilities hasn't been initislised yet
Simon Marlow's avatar
Simon Marlow committed
298
    n_caps = RtsFlags.ParFlags.nCapabilities;
299 300 301
#else
    n_caps = 1;
#endif
302
    moreCapEventBufs(0,n_caps);
303

304
    initEventsBuf(&eventBuf, EVENT_LOG_SIZE, (EventCapNo)(-1));
Simon Marlow's avatar
Simon Marlow committed
305 306

    // Write in buffer: the header begin marker.
Simon Marlow's avatar
Simon Marlow committed
307
    postInt32(&eventBuf, EVENT_HEADER_BEGIN);
Simon Marlow's avatar
Simon Marlow committed
308 309

    // Mark beginning of event types in the header.
Simon Marlow's avatar
Simon Marlow committed
310
    postInt32(&eventBuf, EVENT_HET_BEGIN);
311
    for (t = 0; t < NUM_GHC_EVENT_TAGS; ++t) {
Simon Marlow's avatar
Simon Marlow committed
312 313 314 315 316 317 318 319

        eventTypes[t].etNum = t;
        eventTypes[t].desc = EventDesc[t];

        switch (t) {
        case EVENT_CREATE_THREAD:   // (cap, thread)
        case EVENT_RUN_THREAD:      // (cap, thread)
        case EVENT_THREAD_RUNNABLE: // (cap, thread)
320
        case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
321
            eventTypes[t].size = sizeof(EventThreadID);
322 323
            break;

Simon Marlow's avatar
Simon Marlow committed
324 325 326
        case EVENT_MIGRATE_THREAD:  // (cap, thread, new_cap)
        case EVENT_THREAD_WAKEUP:   // (cap, thread, other_cap)
            eventTypes[t].size =
327
                sizeof(EventThreadID) + sizeof(EventCapNo);
Simon Marlow's avatar
Simon Marlow committed
328 329 330
            break;

        case EVENT_STOP_THREAD:     // (cap, thread, status)
331 332 333
            eventTypes[t].size = sizeof(EventThreadID)
                               + sizeof(StgWord16)
                               + sizeof(EventThreadID);
Simon Marlow's avatar
Simon Marlow committed
334 335
            break;

336 337 338 339
        case EVENT_CAP_CREATE:      // (cap)
        case EVENT_CAP_DELETE:      // (cap)
        case EVENT_CAP_ENABLE:      // (cap)
        case EVENT_CAP_DISABLE:     // (cap)
340 341 342
            eventTypes[t].size = sizeof(EventCapNo);
            break;

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
        case EVENT_CAPSET_CREATE:   // (capset, capset_type)
            eventTypes[t].size =
                sizeof(EventCapsetID) + sizeof(EventCapsetType);
            break;

        case EVENT_CAPSET_DELETE:   // (capset)
            eventTypes[t].size = sizeof(EventCapsetID);
            break;

        case EVENT_CAPSET_ASSIGN_CAP:  // (capset, cap)
        case EVENT_CAPSET_REMOVE_CAP:
            eventTypes[t].size =
                sizeof(EventCapsetID) + sizeof(EventCapNo);
            break;

358 359 360 361 362 363
        case EVENT_OSPROCESS_PID:   // (cap, pid)
        case EVENT_OSPROCESS_PPID:
            eventTypes[t].size =
                sizeof(EventCapsetID) + sizeof(StgWord32);
            break;

364 365 366 367 368
        case EVENT_WALL_CLOCK_TIME: // (capset, unix_epoch_seconds, nanoseconds)
            eventTypes[t].size =
                sizeof(EventCapsetID) + sizeof(StgWord64) + sizeof(StgWord32);
            break;

369 370 371 372 373
        case EVENT_SPARK_STEAL:     // (cap, victim_cap)
            eventTypes[t].size =
                sizeof(EventCapNo);
            break;

Simon Marlow's avatar
Simon Marlow committed
374 375 376 377
        case EVENT_REQUEST_SEQ_GC:  // (cap)
        case EVENT_REQUEST_PAR_GC:  // (cap)
        case EVENT_GC_START:        // (cap)
        case EVENT_GC_END:          // (cap)
378 379 380
        case EVENT_GC_IDLE:
        case EVENT_GC_WORK:
        case EVENT_GC_DONE:
381
        case EVENT_GC_GLOBAL_SYNC:  // (cap)
382 383 384 385 386 387
        case EVENT_SPARK_CREATE:    // (cap)
        case EVENT_SPARK_DUD:       // (cap)
        case EVENT_SPARK_OVERFLOW:  // (cap)
        case EVENT_SPARK_RUN:       // (cap)
        case EVENT_SPARK_FIZZLE:    // (cap)
        case EVENT_SPARK_GC:        // (cap)
388
            eventTypes[t].size = 0;
Simon Marlow's avatar
Simon Marlow committed
389
            break;
390 391

        case EVENT_LOG_MSG:          // (msg)
392
        case EVENT_USER_MSG:         // (msg)
393
        case EVENT_USER_MARKER:      // (markername)
394 395 396
        case EVENT_RTS_IDENTIFIER:   // (capset, str)
        case EVENT_PROGRAM_ARGS:     // (capset, strvec)
        case EVENT_PROGRAM_ENV:      // (capset, strvec)
397
        case EVENT_THREAD_LABEL:     // (thread, str)
398 399 400
            eventTypes[t].size = 0xffff;
            break;

Duncan Coutts's avatar
Duncan Coutts committed
401 402 403 404
        case EVENT_SPARK_COUNTERS:   // (cap, 7*counter)
            eventTypes[t].size = 7 * sizeof(StgWord64);
            break;

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        case EVENT_HEAP_ALLOCATED:    // (heap_capset, alloc_bytes)
        case EVENT_HEAP_SIZE:         // (heap_capset, size_bytes)
        case EVENT_HEAP_LIVE:         // (heap_capset, live_bytes)
            eventTypes[t].size = sizeof(EventCapsetID) + sizeof(StgWord64);
            break;

        case EVENT_HEAP_INFO_GHC:     // (heap_capset, n_generations,
                                      //  max_heap_size, alloc_area_size,
                                      //  mblock_size, block_size)
            eventTypes[t].size = sizeof(EventCapsetID)
                               + sizeof(StgWord16)
                               + sizeof(StgWord64) * 4;
            break;

        case EVENT_GC_STATS_GHC:      // (heap_capset, generation,
                                      //  copied_bytes, slop_bytes, frag_bytes,
                                      //  par_n_threads,
                                      //  par_max_copied, par_tot_copied)
            eventTypes[t].size = sizeof(EventCapsetID)
                               + sizeof(StgWord16)
                               + sizeof(StgWord64) * 3
                               + sizeof(StgWord32)
                               + sizeof(StgWord64) * 2;
            break;

430
        case EVENT_TASK_CREATE:   // (taskId, cap, tid)
431 432 433
            eventTypes[t].size = sizeof(EventTaskId)
                               + sizeof(EventCapNo)
                               + sizeof(EventKernelThreadId);
434 435 436 437 438 439 440 441 442 443 444
            break;

        case EVENT_TASK_MIGRATE:   // (taskId, cap, new_cap)
            eventTypes[t].size =
                sizeof(EventTaskId) + sizeof(EventCapNo) + sizeof(EventCapNo);
            break;

        case EVENT_TASK_DELETE:   // (taskId)
            eventTypes[t].size = sizeof(EventTaskId);
            break;

Simon Marlow's avatar
Simon Marlow committed
445
        case EVENT_BLOCK_MARKER:
446
            eventTypes[t].size = sizeof(StgWord32) + sizeof(EventTimestamp) +
447
                sizeof(EventCapNo);
Simon Marlow's avatar
Simon Marlow committed
448 449
            break;

450 451 452 453
        case EVENT_HACK_BUG_T9003:
            eventTypes[t].size = 0;
            break;

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
        case EVENT_HEAP_PROF_BEGIN:
            eventTypes[t].size = EVENT_SIZE_DYNAMIC;
            break;

        case EVENT_HEAP_PROF_COST_CENTRE:
            eventTypes[t].size = EVENT_SIZE_DYNAMIC;
            break;

        case EVENT_HEAP_PROF_SAMPLE_BEGIN:
            eventTypes[t].size = 8;
            break;

        case EVENT_HEAP_PROF_SAMPLE_STRING:
            eventTypes[t].size = EVENT_SIZE_DYNAMIC;
            break;

        case EVENT_HEAP_PROF_SAMPLE_COST_CENTRE:
            eventTypes[t].size = EVENT_SIZE_DYNAMIC;
            break;

474 475
        default:
            continue; /* ignore deprecated events */
Simon Marlow's avatar
Simon Marlow committed
476 477 478
        }

        // Write in buffer: the start event type.
479
        postEventType(&eventBuf, &eventTypes[t]);
Simon Marlow's avatar
Simon Marlow committed
480 481 482
    }

    // Mark end of event types in the header.
Simon Marlow's avatar
Simon Marlow committed
483
    postInt32(&eventBuf, EVENT_HET_END);
484

Simon Marlow's avatar
Simon Marlow committed
485
    // Write in buffer: the header end marker.
Simon Marlow's avatar
Simon Marlow committed
486
    postInt32(&eventBuf, EVENT_HEADER_END);
487

Simon Marlow's avatar
Simon Marlow committed
488
    // Prepare event buffer for events (data).
Simon Marlow's avatar
Simon Marlow committed
489
    postInt32(&eventBuf, EVENT_DATA_BEGIN);
490

491
    // Flush capEventBuf with header.
Simon Marlow's avatar
Simon Marlow committed
492 493 494 495
    /*
     * Flush header and data begin marker to the file, thus preparing the
     * file to have events written to it.
     */
496
    printAndClearEventBuf(&eventBuf);
Simon Marlow's avatar
Simon Marlow committed
497 498 499 500

    for (c = 0; c < n_caps; ++c) {
        postBlockMarker(&capEventBuf[c]);
    }
501 502 503 504

#ifdef THREADED_RTS
    initMutex(&eventBufMutex);
#endif
Simon Marlow's avatar
Simon Marlow committed
505 506 507 508 509
}

void
endEventLogging(void)
{
510
    uint32_t c;
Simon Marlow's avatar
Simon Marlow committed
511 512 513

    // Flush all events remaining in the buffers.
    for (c = 0; c < n_capabilities; ++c) {
514
        printAndClearEventBuf(&capEventBuf[c]);
Simon Marlow's avatar
Simon Marlow committed
515
    }
516
    printAndClearEventBuf(&eventBuf);
517
    resetEventsBuf(&eventBuf); // we don't want the block marker
Simon Marlow's avatar
Simon Marlow committed
518 519

    // Mark end of events (data).
Simon Marlow's avatar
Simon Marlow committed
520
    postEventTypeNum(&eventBuf, EVENT_DATA_END);
Simon Marlow's avatar
Simon Marlow committed
521 522

    // Flush the end of data marker.
523
    printAndClearEventBuf(&eventBuf);
Simon Marlow's avatar
Simon Marlow committed
524 525 526 527 528 529

    if (event_log_file != NULL) {
        fclose(event_log_file);
    }
}

530
void
531
moreCapEventBufs (uint32_t from, uint32_t to)
532
{
533
    uint32_t c;
534 535 536 537 538 539 540 541 542 543 544 545

    if (from > 0) {
        capEventBuf = stgReallocBytes(capEventBuf, to * sizeof(EventsBuf),
                                      "moreCapEventBufs");
    } else {
        capEventBuf = stgMallocBytes(to * sizeof(EventsBuf),
                                     "moreCapEventBufs");
    }

    for (c = from; c < to; ++c) {
        initEventsBuf(&capEventBuf[c], EVENT_LOG_SIZE, c);
    }
546 547 548 549 550 551 552 553 554

    // The from == 0 already covered in initEventLogging, so we are interested
    // only in case when we are increasing capabilities number
    if (from > 0) {
        for (c = from; c < to; ++c) {
           postBlockMarker(&capEventBuf[c]);
        }
    }

555 556 557 558
}


void
Simon Marlow's avatar
Simon Marlow committed
559 560 561
freeEventLogging(void)
{
    StgWord8 c;
562

Simon Marlow's avatar
Simon Marlow committed
563 564
    // Free events buffer.
    for (c = 0; c < n_capabilities; ++c) {
565
        if (capEventBuf[c].begin != NULL)
566
            stgFree(capEventBuf[c].begin);
Simon Marlow's avatar
Simon Marlow committed
567
    }
568 569
    if (capEventBuf != NULL)  {
        stgFree(capEventBuf);
Simon Marlow's avatar
Simon Marlow committed
570 571 572 573 574 575
    }
    if (event_log_filename != NULL) {
        stgFree(event_log_filename);
    }
}

576
void
577 578 579 580 581 582 583
flushEventLog(void)
{
    if (event_log_file != NULL) {
        fflush(event_log_file);
    }
}

584
void
585 586 587 588 589 590 591
abortEventLogging(void)
{
    freeEventLogging();
    if (event_log_file != NULL) {
        fclose(event_log_file);
    }
}
592

Simon Marlow's avatar
Simon Marlow committed
593 594 595 596 597
/*
 * Post an event message to the capability's eventlog buffer.
 * If the buffer is full, prints out the buffer and clears it.
 */
void
598 599 600
postSchedEvent (Capability *cap,
                EventTypeNum tag,
                StgThreadID thread,
601 602
                StgWord info1,
                StgWord info2)
Simon Marlow's avatar
Simon Marlow committed
603 604 605
{
    EventsBuf *eb;

606
    eb = &capEventBuf[cap->no];
607
    ensureRoomForEvent(eb, tag);
608

Simon Marlow's avatar
Simon Marlow committed
609
    postEventHeader(eb, tag);
Simon Marlow's avatar
Simon Marlow committed
610 611 612 613 614 615

    switch (tag) {
    case EVENT_CREATE_THREAD:   // (cap, thread)
    case EVENT_RUN_THREAD:      // (cap, thread)
    case EVENT_THREAD_RUNNABLE: // (cap, thread)
    {
616
        postThreadID(eb,thread);
Simon Marlow's avatar
Simon Marlow committed
617 618 619
        break;
    }

620
    case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
621
    {
622
        postThreadID(eb,info1 /* spark_thread */);
623 624 625
        break;
    }

Simon Marlow's avatar
Simon Marlow committed
626 627 628
    case EVENT_MIGRATE_THREAD:  // (cap, thread, new_cap)
    case EVENT_THREAD_WAKEUP:   // (cap, thread, other_cap)
    {
629
        postThreadID(eb,thread);
630
        postCapNo(eb,info1 /* new_cap | victim_cap | other_cap */);
Simon Marlow's avatar
Simon Marlow committed
631
        break;
632
   }
Simon Marlow's avatar
Simon Marlow committed
633 634 635

    case EVENT_STOP_THREAD:     // (cap, thread, status)
    {
636
        postThreadID(eb,thread);
637 638
        postWord16(eb,info1 /* status */);
        postThreadID(eb,info2 /* blocked on thread */);
Simon Marlow's avatar
Simon Marlow committed
639 640 641 642
        break;
    }

    default:
643
        barf("postSchedEvent: unknown event tag %d", tag);
Simon Marlow's avatar
Simon Marlow committed
644
    }
645 646
}

647 648 649 650 651 652 653 654
void
postSparkEvent (Capability *cap,
                EventTypeNum tag,
                StgWord info1)
{
    EventsBuf *eb;

    eb = &capEventBuf[cap->no];
655
    ensureRoomForEvent(eb, tag);
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686

    postEventHeader(eb, tag);

    switch (tag) {
    case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
    {
        postThreadID(eb,info1 /* spark_thread */);
        break;
    }

    case EVENT_SPARK_STEAL:         // (cap, victim_cap)
    {
        postCapNo(eb,info1 /* victim_cap */);
        break;
   }

    case EVENT_SPARK_CREATE:        // (cap)
    case EVENT_SPARK_DUD:           // (cap)
    case EVENT_SPARK_OVERFLOW:      // (cap)
    case EVENT_SPARK_RUN:           // (cap)
    case EVENT_SPARK_FIZZLE:        // (cap)
    case EVENT_SPARK_GC:            // (cap)
    {
        break;
    }

    default:
        barf("postSparkEvent: unknown event tag %d", tag);
    }
}

Duncan Coutts's avatar
Duncan Coutts committed
687
void
688
postSparkCountersEvent (Capability *cap,
Duncan Coutts's avatar
Duncan Coutts committed
689 690 691 692 693 694
                        SparkCounters counters,
                        StgWord remaining)
{
    EventsBuf *eb;

    eb = &capEventBuf[cap->no];
695
    ensureRoomForEvent(eb, EVENT_SPARK_COUNTERS);
696

Duncan Coutts's avatar
Duncan Coutts committed
697
    postEventHeader(eb, EVENT_SPARK_COUNTERS);
698
    /* EVENT_SPARK_COUNTERS (crt,dud,ovf,cnv,gcd,fiz,rem) */
Duncan Coutts's avatar
Duncan Coutts committed
699 700 701 702 703 704 705 706 707
    postWord64(eb,counters.created);
    postWord64(eb,counters.dud);
    postWord64(eb,counters.overflowed);
    postWord64(eb,counters.converted);
    postWord64(eb,counters.gcd);
    postWord64(eb,counters.fizzled);
    postWord64(eb,remaining);
}

708 709 710 711 712
void
postCapEvent (EventTypeNum  tag,
              EventCapNo    capno)
{
    ACQUIRE_LOCK(&eventBufMutex);
713
    ensureRoomForEvent(&eventBuf, tag);
714

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
    postEventHeader(&eventBuf, tag);

    switch (tag) {
    case EVENT_CAP_CREATE:   // (cap)
    case EVENT_CAP_DELETE:   // (cap)
    case EVENT_CAP_ENABLE:   // (cap)
    case EVENT_CAP_DISABLE:  // (cap)
    {
        postCapNo(&eventBuf,capno);
        break;
    }

    default:
        barf("postCapEvent: unknown event tag %d", tag);
    }

    RELEASE_LOCK(&eventBufMutex);
}

734 735 736
void postCapsetEvent (EventTypeNum tag,
                      EventCapsetID capset,
                      StgWord info)
737 738
{
    ACQUIRE_LOCK(&eventBufMutex);
739
    ensureRoomForEvent(&eventBuf, tag);
740 741 742 743 744 745 746

    postEventHeader(&eventBuf, tag);
    postCapsetID(&eventBuf, capset);

    switch (tag) {
    case EVENT_CAPSET_CREATE:   // (capset, capset_type)
    {
747
        postCapsetType(&eventBuf, info /* capset_type */);
748 749 750 751 752 753 754 755 756 757 758
        break;
    }

    case EVENT_CAPSET_DELETE:   // (capset)
    {
        break;
    }

    case EVENT_CAPSET_ASSIGN_CAP:  // (capset, capno)
    case EVENT_CAPSET_REMOVE_CAP:  // (capset, capno)
    {
759
        postCapNo(&eventBuf, info /* capno */);
760 761
        break;
    }
762 763 764
    case EVENT_OSPROCESS_PID:   // (capset, pid)
    case EVENT_OSPROCESS_PPID:  // (capset, parent_pid)
    {
765
        postOSProcessId(&eventBuf, info);
766 767
        break;
    }
768
    default:
769
        barf("postCapsetEvent: unknown event tag %d", tag);
770 771 772 773 774
    }

    RELEASE_LOCK(&eventBufMutex);
}

775 776 777 778 779
void postCapsetStrEvent (EventTypeNum tag,
                         EventCapsetID capset,
                         char *msg)
{
    int strsize = strlen(msg);
780
    int size = strsize + sizeof(EventCapsetID);
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 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 825 826 827 828 829 830 831 832 833 834 835 836 837 838

    ACQUIRE_LOCK(&eventBufMutex);

    if (!hasRoomForVariableEvent(&eventBuf, size)){
        printAndClearEventBuf(&eventBuf);

        if (!hasRoomForVariableEvent(&eventBuf, size)){
            // Event size exceeds buffer size, bail out:
            RELEASE_LOCK(&eventBufMutex);
            return;
        }
    }

    postEventHeader(&eventBuf, tag);
    postPayloadSize(&eventBuf, size);
    postCapsetID(&eventBuf, capset);

    postBuf(&eventBuf, (StgWord8*) msg, strsize);

    RELEASE_LOCK(&eventBufMutex);
}

void postCapsetVecEvent (EventTypeNum tag,
                         EventCapsetID capset,
                         int argc,
                         char *argv[])
{
    int i, size = sizeof(EventCapsetID);

    for (i = 0; i < argc; i++) {
        // 1 + strlen to account for the trailing \0, used as separator
        size += 1 + strlen(argv[i]);
    }

    ACQUIRE_LOCK(&eventBufMutex);

    if (!hasRoomForVariableEvent(&eventBuf, size)){
        printAndClearEventBuf(&eventBuf);

        if(!hasRoomForVariableEvent(&eventBuf, size)){
            // Event size exceeds buffer size, bail out:
            RELEASE_LOCK(&eventBufMutex);
            return;
        }
    }

    postEventHeader(&eventBuf, tag);
    postPayloadSize(&eventBuf, size);
    postCapsetID(&eventBuf, capset);

    for( i = 0; i < argc; i++ ) {
        // again, 1 + to account for \0
        postBuf(&eventBuf, (StgWord8*) argv[i], 1 + strlen(argv[i]));
    }

    RELEASE_LOCK(&eventBufMutex);
}

839 840 841 842 843 844 845
void postWallClockTime (EventCapsetID capset)
{
    StgWord64 ts;
    StgWord64 sec;
    StgWord32 nsec;

    ACQUIRE_LOCK(&eventBufMutex);
846

847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
    /* The EVENT_WALL_CLOCK_TIME event is intended to allow programs
       reading the eventlog to match up the event timestamps with wall
       clock time. The normal event timestamps measure time since the
       start of the program. To align eventlogs from concurrent
       processes we need to be able to match up the timestamps. One way
       to do this is if we know how the timestamps and wall clock time
       match up (and of course if both processes have sufficiently
       synchronised clocks).

       So we want to make sure that the timestamp that we generate for
       this event matches up very closely with the wall clock time.
       Unfortunately we currently have to use two different APIs to get
       the elapsed time vs the wall clock time. So to minimise the
       difference we just call them very close together.
     */
862

863 864
    getUnixEpochTime(&sec, &nsec);  /* Get the wall clock time */
    ts = time_ns();                 /* Get the eventlog timestamp */
865
    ensureRoomForEvent(&eventBuf, EVENT_WALL_CLOCK_TIME);
866 867 868 869 870 871

    /* Normally we'd call postEventHeader(), but that generates its own
       timestamp, so we go one level lower so we can write out the
       timestamp we already generated above. */
    postEventTypeNum(&eventBuf, EVENT_WALL_CLOCK_TIME);
    postWord64(&eventBuf, ts);
872

873 874 875 876 877 878 879 880
    /* EVENT_WALL_CLOCK_TIME (capset, unix_epoch_seconds, nanoseconds) */
    postCapsetID(&eventBuf, capset);
    postWord64(&eventBuf, sec);
    postWord32(&eventBuf, nsec);

    RELEASE_LOCK(&eventBufMutex);
}

881 882 883 884 885 886
/*
 * Various GC and heap events
 */
void postHeapEvent (Capability    *cap,
                    EventTypeNum   tag,
                    EventCapsetID  heap_capset,
887
                    W_           info1)
888 889 890 891
{
    EventsBuf *eb;

    eb = &capEventBuf[cap->no];
892
    ensureRoomForEvent(eb, tag);
893

894 895 896 897 898 899 900 901
    postEventHeader(eb, tag);

    switch (tag) {
    case EVENT_HEAP_ALLOCATED:     // (heap_capset, alloc_bytes)
    case EVENT_HEAP_SIZE:          // (heap_capset, size_bytes)
    case EVENT_HEAP_LIVE:          // (heap_capset, live_bytes)
    {
        postCapsetID(eb, heap_capset);
902
        postWord64(eb, info1 /* alloc/size/live_bytes */);
903 904 905 906 907 908 909 910 911
        break;
    }

    default:
        barf("postHeapEvent: unknown event tag %d", tag);
    }
}

void postEventHeapInfo (EventCapsetID heap_capset,
912
                        uint32_t    gens,
913 914 915 916
                        W_          maxHeapSize,
                        W_          allocAreaSize,
                        W_          mblockSize,
                        W_          blockSize)
917 918
{
    ACQUIRE_LOCK(&eventBufMutex);
919
    ensureRoomForEvent(&eventBuf, EVENT_HEAP_INFO_GHC);
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936

    postEventHeader(&eventBuf, EVENT_HEAP_INFO_GHC);
    /* EVENT_HEAP_INFO_GHC (heap_capset, n_generations,
                            max_heap_size, alloc_area_size,
                            mblock_size, block_size) */
    postCapsetID(&eventBuf, heap_capset);
    postWord16(&eventBuf, gens);
    postWord64(&eventBuf, maxHeapSize);
    postWord64(&eventBuf, allocAreaSize);
    postWord64(&eventBuf, mblockSize);
    postWord64(&eventBuf, blockSize);

    RELEASE_LOCK(&eventBufMutex);
}

void postEventGcStats  (Capability    *cap,
                        EventCapsetID  heap_capset,
937
                        uint32_t     gen,
938 939 940
                        W_           copied,
                        W_           slop,
                        W_           fragmentation,
941
                        uint32_t     par_n_threads,
942 943
                        W_           par_max_copied,
                        W_           par_tot_copied)
944 945 946 947
{
    EventsBuf *eb;

    eb = &capEventBuf[cap->no];
948
    ensureRoomForEvent(eb, EVENT_GC_STATS_GHC);
949

950 951 952 953 954 955 956 957 958 959 960 961 962 963
    postEventHeader(eb, EVENT_GC_STATS_GHC);
    /* EVENT_GC_STATS_GHC (heap_capset, generation,
                           copied_bytes, slop_bytes, frag_bytes,
                           par_n_threads, par_max_copied, par_tot_copied) */
    postCapsetID(eb, heap_capset);
    postWord16(eb, gen);
    postWord64(eb, copied);
    postWord64(eb, slop);
    postWord64(eb, fragmentation);
    postWord32(eb, par_n_threads);
    postWord64(eb, par_max_copied);
    postWord64(eb, par_tot_copied);
}

964 965 966 967 968
void postTaskCreateEvent (EventTaskId taskId,
                          EventCapNo capno,
                          EventKernelThreadId tid)
{
    ACQUIRE_LOCK(&eventBufMutex);
969
    ensureRoomForEvent(&eventBuf, EVENT_TASK_CREATE);
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984

    postEventHeader(&eventBuf, EVENT_TASK_CREATE);
    /* EVENT_TASK_CREATE (taskID, cap, tid) */
    postTaskId(&eventBuf, taskId);
    postCapNo(&eventBuf, capno);
    postKernelThreadId(&eventBuf, tid);

    RELEASE_LOCK(&eventBufMutex);
}

void postTaskMigrateEvent (EventTaskId taskId,
                           EventCapNo capno,
                           EventCapNo new_capno)
{
    ACQUIRE_LOCK(&eventBufMutex);
985
    ensureRoomForEvent(&eventBuf, EVENT_TASK_MIGRATE);
986 987 988 989 990 991 992 993 994 995 996 997 998

    postEventHeader(&eventBuf, EVENT_TASK_MIGRATE);
    /* EVENT_TASK_MIGRATE (taskID, cap, new_cap) */
    postTaskId(&eventBuf, taskId);
    postCapNo(&eventBuf, capno);
    postCapNo(&eventBuf, new_capno);

    RELEASE_LOCK(&eventBufMutex);
}

void postTaskDeleteEvent (EventTaskId taskId)
{
    ACQUIRE_LOCK(&eventBufMutex);
999
    ensureRoomForEvent(&eventBuf, EVENT_TASK_DELETE);
1000 1001 1002 1003 1004 1005 1006 1007

    postEventHeader(&eventBuf, EVENT_TASK_DELETE);
    /* EVENT_TASK_DELETE (taskID) */
    postTaskId(&eventBuf, taskId);

    RELEASE_LOCK(&eventBufMutex);
}

1008 1009 1010 1011 1012 1013
void
postEvent (Capability *cap, EventTypeNum tag)
{
    EventsBuf *eb;

    eb = &capEventBuf[cap->no];
1014
    ensureRoomForEvent(eb, tag);
1015 1016 1017
    postEventHeader(eb, tag);
}

1018 1019 1020 1021 1022 1023
void
postEventAtTimestamp (Capability *cap, EventTimestamp ts, EventTypeNum tag)
{
    EventsBuf *eb;

    eb = &capEventBuf[cap->no];
1024
    ensureRoomForEvent(eb, tag);
1025 1026 1027 1028 1029 1030 1031 1032

    /* Normally we'd call postEventHeader(), but that generates its own
       timestamp, so we go one level lower so we can write out
       the timestamp we received as an argument. */
    postEventTypeNum(eb, tag);
    postWord64(eb, ts);
}

1033 1034
#define BUF 512

1035
void postLogMsg(EventsBuf *eb, EventTypeNum type, char *msg, va_list ap)
1036 1037
{
    char buf[BUF];
1038
    uint32_t size;
1039 1040 1041 1042 1043 1044 1045

    size = vsnprintf(buf,BUF,msg,ap);
    if (size > BUF) {
        buf[BUF-1] = '\0';
        size = BUF;
    }

1046
    ensureRoomForVariableEvent(eb, size);
1047

1048
    postEventHeader(eb, type);
1049 1050
    postPayloadSize(eb, size);
    postBuf(eb,(StgWord8*)buf,size);
1051
}
1052

1053 1054 1055
void postMsg(char *msg, va_list ap)
{
    ACQUIRE_LOCK(&eventBufMutex);
1056
    postLogMsg(&eventBuf, EVENT_LOG_MSG, msg, ap);
1057 1058 1059 1060 1061
    RELEASE_LOCK(&eventBufMutex);
}

void postCapMsg(Capability *cap, char *msg, va_list ap)
{
1062
    postLogMsg(&capEventBuf[cap->no], EVENT_LOG_MSG, msg, ap);
Simon Marlow's avatar
Simon Marlow committed
1063 1064
}

1065
void postUserEvent(Capability *cap, EventTypeNum type, char *msg)
1066 1067
{
    EventsBuf *eb;
1068
    int size = strlen(msg);
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

    eb = &capEventBuf[cap->no];

    if (!hasRoomForVariableEvent(eb, size)){
        printAndClearEventBuf(eb);

        if (!hasRoomForVariableEvent(eb, size)){
            // Event size exceeds buffer size, bail out:
            return;
        }
    }

1081
    postEventHeader(eb, type);
1082
    postPayloadSize(eb, size);
1083
    postBuf(eb, (StgWord8*) msg, size);
1084 1085
}

1086 1087 1088 1089 1090 1091
void postThreadLabel(Capability    *cap,
                     EventThreadID  id,
                     char          *label)
{
    EventsBuf *eb;
    int strsize = strlen(label);
1092
    int size = strsize + sizeof(EventThreadID);
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

    eb = &capEventBuf[cap->no];

    if (!hasRoomForVariableEvent(eb, size)){
        printAndClearEventBuf(eb);

        if (!hasRoomForVariableEvent(eb, size)){
            // Event size exceeds buffer size, bail out:
            return;
        }
    }

    postEventHeader(eb, EVENT_THREAD_LABEL);
    postPayloadSize(eb, size);
    postThreadID(eb, id);
    postBuf(eb, (StgWord8*) label, strsize);
}

Simon Marlow's avatar
Simon Marlow committed
1111 1112
void closeBlockMarker (EventsBuf *ebuf)
{
1113 1114
    StgInt8* save_pos;

Simon Marlow's avatar
Simon Marlow committed
1115 1116 1117
    if (ebuf->marker)
    {
        // (type:16, time:64, size:32, end_time:64)
1118 1119 1120 1121 1122 1123 1124

        save_pos = ebuf->pos;
        ebuf->pos = ebuf->marker + sizeof(EventTypeNum) +
                    sizeof(EventTimestamp);
        postWord32(ebuf, save_pos - ebuf->marker);
        postTimestamp(ebuf);
        ebuf->pos = save_pos;
Simon Marlow's avatar
Simon Marlow committed
1125 1126 1127 1128 1129 1130 1131
        ebuf->marker = NULL;
    }
}


void postBlockMarker (EventsBuf *eb)
{
1132
    ensureRoomForEvent(eb, EVENT_BLOCK_MARKER);
Simon Marlow's avatar
Simon Marlow committed
1133 1134 1135 1136 1137 1138 1139

    closeBlockMarker(eb);

    eb->marker = eb->pos;
    postEventHeader(eb, EVENT_BLOCK_MARKER);
    postWord32(eb,0); // these get filled in later by closeBlockMarker();
    postWord64(eb,0);
1140
    postCapNo(eb, eb->capno);
Simon Marlow's avatar
Simon Marlow committed
1141 1142
}

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
typedef enum {
    HEAP_PROF_BREAKDOWN_COST_CENTRE = 0x1,
    HEAP_PROF_BREAKDOWN_MODULE,
    HEAP_PROF_BREAKDOWN_CLOSURE_DESCR,
    HEAP_PROF_BREAKDOWN_TYPE_DESCR,
    HEAP_PROF_BREAKDOWN_RETAINER,
    HEAP_PROF_BREAKDOWN_BIOGRAPHY,
} HeapProfBreakdown;

static HeapProfBreakdown getHeapProfBreakdown(void)
{
    switch (RtsFlags.ProfFlags.doHeapProfile) {
    case HEAP_BY_CCS:
        return HEAP_PROF_BREAKDOWN_COST_CENTRE;
    case HEAP_BY_MOD:
        return HEAP_PROF_BREAKDOWN_MODULE;
    case HEAP_BY_DESCR:
        return HEAP_PROF_BREAKDOWN_CLOSURE_DESCR;
    case HEAP_BY_TYPE:
        return HEAP_PROF_BREAKDOWN_TYPE_DESCR;
    case HEAP_BY_RETAINER:
        return HEAP_PROF_BREAKDOWN_RETAINER;
    case HEAP_BY_LDV:
        return HEAP_PROF_BREAKDOWN_BIOGRAPHY;
    default:
        barf("getHeapProfBreakdown: unknown heap profiling mode");
    }
}

void postHeapProfBegin(StgWord8 profile_id)
{
    ACQUIRE_LOCK(&eventBufMutex);
    PROFILING_FLAGS *flags = &RtsFlags.ProfFlags;
    StgWord modSelector_len   =
        flags->modSelector ? strlen(flags->modSelector) : 0;
    StgWord descrSelector_len =
        flags->descrSelector ? strlen(flags->descrSelector) : 0;
    StgWord typeSelector_len  =
        flags->typeSelector ? strlen(flags->typeSelector) : 0;
    StgWord ccSelector_len    =
        flags->ccSelector ? strlen(flags->ccSelector) : 0;
    StgWord ccsSelector_len   =
        flags->ccsSelector ? strlen(flags->ccsSelector) : 0;
    StgWord retainerSelector_len =
        flags->retainerSelector ? strlen(flags->retainerSelector) : 0;
    StgWord bioSelector_len   =
        flags->bioSelector ? strlen(flags->bioSelector) : 0;
    StgWord len =
        1+8+4 + modSelector_len + descrSelector_len +
        typeSelector_len + ccSelector_len + ccsSelector_len +
        retainerSelector_len + bioSelector_len + 7;
    ensureRoomForVariableEvent(&eventBuf, len);
    postEventHeader(&eventBuf, EVENT_HEAP_PROF_BEGIN);
    postPayloadSize(&eventBuf, len);
    postWord8(&eventBuf, profile_id);
    postWord64(&eventBuf, TimeToNS(flags->heapProfileInterval));
    postWord32(&eventBuf, getHeapProfBreakdown());
    postString(&eventBuf, flags->modSelector);
    postString(&eventBuf, flags->descrSelector);
    postString(&eventBuf, flags->typeSelector);
    postString(&eventBuf, flags->ccSelector);
    postString(&eventBuf, flags->ccsSelector);
    postString(&eventBuf, flags->retainerSelector);
    postString(&eventBuf, flags->bioSelector);
    RELEASE_LOCK(&eventBufMutex);
}

void postHeapProfSampleBegin(StgInt era)
{
    ACQUIRE_LOCK(&eventBufMutex);
    ensureRoomForEvent(&eventBuf, EVENT_HEAP_PROF_SAMPLE_BEGIN);
    postEventHeader(&eventBuf, EVENT_HEAP_PROF_SAMPLE_BEGIN);
    postWord64(&eventBuf, era);
    RELEASE_LOCK(&eventBufMutex);
}

void postHeapProfSampleString(StgWord8 profile_id,
                              const char *label,
                              StgWord64 residency)
{
    ACQUIRE_LOCK(&eventBufMutex);
    StgWord label_len = strlen(label);
    StgWord len = 1+8+label_len+1;
    ensureRoomForVariableEvent(&eventBuf, len);