EventLog.c 22.8 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 64 65 66 67 68
  [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_SHUTDOWN]            = "Shutdown",
  [EVENT_THREAD_WAKEUP]       = "Wakeup thread",
  [EVENT_GC_START]            = "Starting GC",
  [EVENT_GC_END]              = "Finished GC",
  [EVENT_REQUEST_SEQ_GC]      = "Request sequential GC",
  [EVENT_REQUEST_PAR_GC]      = "Request parallel GC",
69 70
  [EVENT_CREATE_SPARK_THREAD] = "Create spark thread",
  [EVENT_LOG_MSG]             = "Log message",
71
  [EVENT_USER_MSG]            = "User message",
Simon Marlow's avatar
Simon Marlow committed
72
  [EVENT_STARTUP]             = "Startup",
73 74 75
  [EVENT_GC_IDLE]             = "GC idle",
  [EVENT_GC_WORK]             = "GC working",
  [EVENT_GC_DONE]             = "GC done",
76 77 78 79
  [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",
80 81 82 83 84
  [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
85
  [EVENT_OSPROCESS_PPID]      = "Parent process ID",
86 87 88 89 90 91 92 93
  [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",
Simon Marlow's avatar
Simon Marlow committed
94 95 96 97 98 99 100 101 102 103
};

// Event type. 

typedef struct _EventType {
  EventTypeNum etNum;  // Event Type number.
  nat   size;     // size of the payload in bytes
  char *desc;     // Description
} EventType;

104
EventType eventTypes[NUM_GHC_EVENT_TAGS];
Simon Marlow's avatar
Simon Marlow committed
105

106
static void initEventsBuf(EventsBuf* eb, StgWord64 size, EventCapNo capno);
Simon Marlow's avatar
Simon Marlow committed
107
static void resetEventsBuf(EventsBuf* eb);
Simon Marlow's avatar
Simon Marlow committed
108
static void printAndClearEventBuf (EventsBuf *eventsBuf);
Simon Marlow's avatar
Simon Marlow committed
109 110 111

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

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

Simon Marlow's avatar
Simon Marlow committed
114 115 116
static void postBlockMarker(EventsBuf *eb);
static void closeBlockMarker(EventsBuf *ebuf);

Simon Marlow's avatar
Simon Marlow committed
117
static StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum);
118
static StgBool hasRoomForVariableEvent(EventsBuf *eb, nat payload_bytes);
Simon Marlow's avatar
Simon Marlow committed
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static inline void postWord8(EventsBuf *eb, StgWord8 i)
{
    *(eb->pos++) = i; 
}

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);
}

143 144 145 146 147 148
static inline void postBuf(EventsBuf *eb, StgWord8 *buf, nat size)
{
    memcpy(eb->pos, buf, size);
    eb->pos += size;
}

Simon Marlow's avatar
Simon Marlow committed
149 150 151
static inline StgWord64 time_ns(void)
{ return stat_getElapsedTime() * (1000000000LL/TICKS_PER_SECOND); }

152 153 154
static inline void postEventTypeNum(EventsBuf *eb, EventTypeNum etNum)
{ postWord16(eb, etNum); }

Simon Marlow's avatar
Simon Marlow committed
155 156
static inline void postTimestamp(EventsBuf *eb)
{ postWord64(eb, time_ns()); }
157

158
static inline void postThreadID(EventsBuf *eb, EventThreadID id)
159 160
{ postWord32(eb,id); }

161
static inline void postCapNo(EventsBuf *eb, EventCapNo no)
162 163
{ postWord16(eb,no); }

164 165 166 167 168 169
static inline void postCapsetID(EventsBuf *eb, EventCapsetID id)
{ postWord32(eb,id); }

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

170 171 172
static inline void postPayloadSize(EventsBuf *eb, EventPayloadSize size)
{ postWord16(eb,size); }

Simon Marlow's avatar
Simon Marlow committed
173 174 175 176 177 178
static inline void postEventHeader(EventsBuf *eb, EventTypeNum type)
{
    postEventTypeNum(eb, type);
    postTimestamp(eb);
}    

179 180 181 182 183 184 185 186 187 188 189 190
static inline void postInt8(EventsBuf *eb, StgInt8 i)
{ postWord8(eb, (StgWord8)i); }

static inline void postInt16(EventsBuf *eb, StgInt16 i)
{ postWord16(eb, (StgWord16)i); }

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

static inline void postInt64(EventsBuf *eb, StgInt64 i)
{ postWord64(eb, (StgWord64)i); }

Simon Marlow's avatar
Simon Marlow committed
191 192 193 194 195

void
initEventLogging(void)
{
    StgWord8 t, c;
196
    nat n_caps;
Simon Marlow's avatar
Simon Marlow committed
197

198 199 200
    event_log_filename = stgMallocBytes(strlen(prog_name)
                                        + 10 /* .%d */
                                        + 10 /* .eventlog */,
Simon Marlow's avatar
Simon Marlow committed
201 202
                                        "initEventLogging");

203
    if (sizeof(EventDesc) / sizeof(char*) != NUM_GHC_EVENT_TAGS) {
Simon Marlow's avatar
Simon Marlow committed
204 205
        barf("EventDesc array has the wrong number of elements");
    }
206 207 208 209 210 211 212 213 214 215 216 217

    if (event_log_pid == -1) { // #4512
        // Single process
        sprintf(event_log_filename, "%s.eventlog", prog_name);
        event_log_pid = getpid();
    } else {
        // Forked process, eventlog already started by the parent
        // before fork
        event_log_pid = getpid();
        sprintf(event_log_filename, "%s.%d.eventlog", prog_name, event_log_pid);
    }

Simon Marlow's avatar
Simon Marlow committed
218 219
    /* Open event log file for writing. */
    if ((event_log_file = fopen(event_log_filename, "wb")) == NULL) {
Simon Marlow's avatar
Simon Marlow committed
220
        sysErrorBelch("initEventLogging: can't open %s", event_log_filename);
Simon Marlow's avatar
Simon Marlow committed
221 222 223 224 225 226 227 228 229 230 231 232 233
        stg_exit(EXIT_FAILURE);    
    }

    /* 
     * 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.
     *
     * Use a single buffer to store the header with event types, then flush 
     * the buffer so all buffers are empty for writing events.
     */
234 235 236 237 238 239 240 241 242
#ifdef THREADED_RTS
    // XXX n_capabilities hasn't been initislised yet
    n_caps = RtsFlags.ParFlags.nNodes;
#else
    n_caps = 1;
#endif
    capEventBuf = stgMallocBytes(n_caps * sizeof(EventsBuf),"initEventLogging");

    for (c = 0; c < n_caps; ++c) {
Simon Marlow's avatar
Simon Marlow committed
243
        // Init buffer for events.
244
        initEventsBuf(&capEventBuf[c], EVENT_LOG_SIZE, c);
Simon Marlow's avatar
Simon Marlow committed
245
    }
246
    initEventsBuf(&eventBuf, EVENT_LOG_SIZE, (EventCapNo)(-1));
Simon Marlow's avatar
Simon Marlow committed
247 248

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

    // Mark beginning of event types in the header.
Simon Marlow's avatar
Simon Marlow committed
252
    postInt32(&eventBuf, EVENT_HET_BEGIN);
253
    for (t = 0; t < NUM_GHC_EVENT_TAGS; ++t) {
Simon Marlow's avatar
Simon Marlow committed
254 255 256 257 258 259 260 261

        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)
262
        case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
263
            eventTypes[t].size = sizeof(EventThreadID);
264 265
            break;

Simon Marlow's avatar
Simon Marlow committed
266 267 268
        case EVENT_MIGRATE_THREAD:  // (cap, thread, new_cap)
        case EVENT_THREAD_WAKEUP:   // (cap, thread, other_cap)
            eventTypes[t].size =
269
                sizeof(EventThreadID) + sizeof(EventCapNo);
Simon Marlow's avatar
Simon Marlow committed
270 271 272 273
            break;

        case EVENT_STOP_THREAD:     // (cap, thread, status)
            eventTypes[t].size =
274
                sizeof(EventThreadID) + sizeof(StgWord16) + sizeof(EventThreadID);
Simon Marlow's avatar
Simon Marlow committed
275 276
            break;

277 278 279 280
        case EVENT_STARTUP:         // (cap count)
            eventTypes[t].size = sizeof(EventCapNo);
            break;

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        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;

296 297 298 299 300 301
        case EVENT_OSPROCESS_PID:   // (cap, pid)
        case EVENT_OSPROCESS_PPID:
            eventTypes[t].size =
                sizeof(EventCapsetID) + sizeof(StgWord32);
            break;

302 303 304 305 306
        case EVENT_SPARK_STEAL:     // (cap, victim_cap)
            eventTypes[t].size =
                sizeof(EventCapNo);
            break;

Simon Marlow's avatar
Simon Marlow committed
307 308 309 310 311
        case EVENT_SHUTDOWN:        // (cap)
        case EVENT_REQUEST_SEQ_GC:  // (cap)
        case EVENT_REQUEST_PAR_GC:  // (cap)
        case EVENT_GC_START:        // (cap)
        case EVENT_GC_END:          // (cap)
312 313 314
        case EVENT_GC_IDLE:
        case EVENT_GC_WORK:
        case EVENT_GC_DONE:
315 316 317 318 319 320
        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)
321
            eventTypes[t].size = 0;
Simon Marlow's avatar
Simon Marlow committed
322
            break;
323 324

        case EVENT_LOG_MSG:          // (msg)
325
        case EVENT_USER_MSG:         // (msg)
326 327 328
        case EVENT_RTS_IDENTIFIER:   // (capset, str)
        case EVENT_PROGRAM_ARGS:     // (capset, strvec)
        case EVENT_PROGRAM_ENV:      // (capset, strvec)
329 330 331
            eventTypes[t].size = 0xffff;
            break;

Duncan Coutts's avatar
Duncan Coutts committed
332 333 334 335
        case EVENT_SPARK_COUNTERS:   // (cap, 7*counter)
            eventTypes[t].size = 7 * sizeof(StgWord64);
            break;

Simon Marlow's avatar
Simon Marlow committed
336
        case EVENT_BLOCK_MARKER:
337 338
            eventTypes[t].size = sizeof(StgWord32) + sizeof(EventTimestamp) + 
                sizeof(EventCapNo);
Simon Marlow's avatar
Simon Marlow committed
339 340
            break;

341 342
        default:
            continue; /* ignore deprecated events */
Simon Marlow's avatar
Simon Marlow committed
343 344 345
        }

        // Write in buffer: the start event type.
346
        postEventType(&eventBuf, &eventTypes[t]);
Simon Marlow's avatar
Simon Marlow committed
347 348 349
    }

    // Mark end of event types in the header.
Simon Marlow's avatar
Simon Marlow committed
350
    postInt32(&eventBuf, EVENT_HET_END);
Simon Marlow's avatar
Simon Marlow committed
351 352
    
    // Write in buffer: the header end marker.
Simon Marlow's avatar
Simon Marlow committed
353
    postInt32(&eventBuf, EVENT_HEADER_END);
Simon Marlow's avatar
Simon Marlow committed
354 355
    
    // Prepare event buffer for events (data).
Simon Marlow's avatar
Simon Marlow committed
356
    postInt32(&eventBuf, EVENT_DATA_BEGIN);
357

358
    // Flush capEventBuf with header.
Simon Marlow's avatar
Simon Marlow committed
359 360 361 362
    /*
     * Flush header and data begin marker to the file, thus preparing the
     * file to have events written to it.
     */
363
    printAndClearEventBuf(&eventBuf);
Simon Marlow's avatar
Simon Marlow committed
364 365 366 367

    for (c = 0; c < n_caps; ++c) {
        postBlockMarker(&capEventBuf[c]);
    }
368 369 370 371

#ifdef THREADED_RTS
    initMutex(&eventBufMutex);
#endif
Simon Marlow's avatar
Simon Marlow committed
372 373 374 375 376 377 378 379 380
}

void
endEventLogging(void)
{
    nat c;

    // Flush all events remaining in the buffers.
    for (c = 0; c < n_capabilities; ++c) {
381
        printAndClearEventBuf(&capEventBuf[c]);
Simon Marlow's avatar
Simon Marlow committed
382
    }
383
    printAndClearEventBuf(&eventBuf);
384
    resetEventsBuf(&eventBuf); // we don't want the block marker
Simon Marlow's avatar
Simon Marlow committed
385 386

    // Mark end of events (data).
Simon Marlow's avatar
Simon Marlow committed
387
    postEventTypeNum(&eventBuf, EVENT_DATA_END);
Simon Marlow's avatar
Simon Marlow committed
388 389

    // Flush the end of data marker.
390
    printAndClearEventBuf(&eventBuf);
Simon Marlow's avatar
Simon Marlow committed
391 392 393 394 395 396 397 398 399 400 401 402 403

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

void 
freeEventLogging(void)
{
    StgWord8 c;
    
    // Free events buffer.
    for (c = 0; c < n_capabilities; ++c) {
404 405
        if (capEventBuf[c].begin != NULL) 
            stgFree(capEventBuf[c].begin);
Simon Marlow's avatar
Simon Marlow committed
406
    }
407 408
    if (capEventBuf != NULL)  {
        stgFree(capEventBuf);
Simon Marlow's avatar
Simon Marlow committed
409 410 411 412 413 414
    }
    if (event_log_filename != NULL) {
        stgFree(event_log_filename);
    }
}

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
void 
flushEventLog(void)
{
    if (event_log_file != NULL) {
        fflush(event_log_file);
    }
}

void 
abortEventLogging(void)
{
    freeEventLogging();
    if (event_log_file != NULL) {
        fclose(event_log_file);
    }
}
Simon Marlow's avatar
Simon Marlow committed
431 432 433 434 435
/*
 * Post an event message to the capability's eventlog buffer.
 * If the buffer is full, prints out the buffer and clears it.
 */
void
436 437 438
postSchedEvent (Capability *cap, 
                EventTypeNum tag, 
                StgThreadID thread, 
439 440
                StgWord info1,
                StgWord info2)
Simon Marlow's avatar
Simon Marlow committed
441 442 443
{
    EventsBuf *eb;

444
    eb = &capEventBuf[cap->no];
Simon Marlow's avatar
Simon Marlow committed
445 446 447 448 449 450

    if (!hasRoomForEvent(eb, tag)) {
        // Flush event buffer to make room for new event.
        printAndClearEventBuf(eb);
    }
    
Simon Marlow's avatar
Simon Marlow committed
451
    postEventHeader(eb, tag);
Simon Marlow's avatar
Simon Marlow committed
452 453 454 455 456 457

    switch (tag) {
    case EVENT_CREATE_THREAD:   // (cap, thread)
    case EVENT_RUN_THREAD:      // (cap, thread)
    case EVENT_THREAD_RUNNABLE: // (cap, thread)
    {
458
        postThreadID(eb,thread);
Simon Marlow's avatar
Simon Marlow committed
459 460 461
        break;
    }

462
    case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
463
    {
464
        postThreadID(eb,info1 /* spark_thread */);
465 466 467
        break;
    }

Simon Marlow's avatar
Simon Marlow committed
468 469 470
    case EVENT_MIGRATE_THREAD:  // (cap, thread, new_cap)
    case EVENT_THREAD_WAKEUP:   // (cap, thread, other_cap)
    {
471
        postThreadID(eb,thread);
472
        postCapNo(eb,info1 /* new_cap | victim_cap | other_cap */);
Simon Marlow's avatar
Simon Marlow committed
473
        break;
474
   }
Simon Marlow's avatar
Simon Marlow committed
475 476 477

    case EVENT_STOP_THREAD:     // (cap, thread, status)
    {
478
        postThreadID(eb,thread);
479 480
        postWord16(eb,info1 /* status */);
        postThreadID(eb,info2 /* blocked on thread */);
Simon Marlow's avatar
Simon Marlow committed
481 482 483 484 485 486 487 488 489
        break;
    }

    case EVENT_SHUTDOWN:        // (cap)
    {
        break;
    }

    default:
490
        barf("postSchedEvent: unknown event tag %d", tag);
Simon Marlow's avatar
Simon Marlow committed
491
    }
492 493
}

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
void
postSparkEvent (Capability *cap,
                EventTypeNum tag,
                StgWord info1)
{
    EventsBuf *eb;

    eb = &capEventBuf[cap->no];

    if (!hasRoomForEvent(eb, tag)) {
        // Flush event buffer to make room for new event.
        printAndClearEventBuf(eb);
    }

    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
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
void
postSparkCountersEvent (Capability *cap, 
                        SparkCounters counters,
                        StgWord remaining)
{
    EventsBuf *eb;

    eb = &capEventBuf[cap->no];

    if (!hasRoomForEvent(eb, EVENT_SPARK_COUNTERS)) {
        // Flush event buffer to make room for new event.
        printAndClearEventBuf(eb);
    }
    
    postEventHeader(eb, EVENT_SPARK_COUNTERS);
    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);
}

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
void postCapsetModifyEvent (EventTypeNum tag,
                            EventCapsetID capset,
                            StgWord32 other)
{
    ACQUIRE_LOCK(&eventBufMutex);

    if (!hasRoomForEvent(&eventBuf, tag)) {
        // Flush event buffer to make room for new event.
        printAndClearEventBuf(&eventBuf);
    }

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

    switch (tag) {
    case EVENT_CAPSET_CREATE:   // (capset, capset_type)
    {
        postCapsetType(&eventBuf, other /* capset_type */);
        break;
    }

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

    case EVENT_CAPSET_ASSIGN_CAP:  // (capset, capno)
    case EVENT_CAPSET_REMOVE_CAP:  // (capset, capno)
    {
        postCapNo(&eventBuf, other /* capno */);
        break;
    }
594 595 596 597 598 599
    case EVENT_OSPROCESS_PID:   // (capset, pid)
    case EVENT_OSPROCESS_PPID:  // (capset, parent_pid)
    {
        postWord32(&eventBuf, other);
        break;
    }
600 601 602 603 604 605 606
    default:
        barf("postCapsetModifyEvent: unknown event tag %d", tag);
    }

    RELEASE_LOCK(&eventBufMutex);
}

607 608 609 610 611
void postCapsetStrEvent (EventTypeNum tag,
                         EventCapsetID capset,
                         char *msg)
{
    int strsize = strlen(msg);
612
    int size = strsize + sizeof(EventCapsetID);
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

    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);
}

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
void
postEvent (Capability *cap, EventTypeNum tag)
{
    EventsBuf *eb;

    eb = &capEventBuf[cap->no];

    if (!hasRoomForEvent(eb, tag)) {
        // Flush event buffer to make room for new event.
        printAndClearEventBuf(eb);
    }

    postEventHeader(eb, tag);
}

686 687
#define BUF 512

688
void postLogMsg(EventsBuf *eb, EventTypeNum type, char *msg, va_list ap)
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
{
    char buf[BUF];
    nat size;

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

    if (!hasRoomForVariableEvent(eb, size)) {
        // Flush event buffer to make room for new event.
        printAndClearEventBuf(eb);
    }

704
    postEventHeader(eb, type);
705 706
    postPayloadSize(eb, size);
    postBuf(eb,(StgWord8*)buf,size);
707
}
708

709 710 711
void postMsg(char *msg, va_list ap)
{
    ACQUIRE_LOCK(&eventBufMutex);
712
    postLogMsg(&eventBuf, EVENT_LOG_MSG, msg, ap);
713 714 715 716 717
    RELEASE_LOCK(&eventBufMutex);
}

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

721
void postUserMsg(Capability *cap, char *msg, va_list ap)
722
{
723
    postLogMsg(&capEventBuf[cap->no], EVENT_USER_MSG, msg, ap);
724 725
}    

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
void postEventStartup(EventCapNo n_caps)
{
    ACQUIRE_LOCK(&eventBufMutex);

    if (!hasRoomForEvent(&eventBuf, EVENT_STARTUP)) {
        // Flush event buffer to make room for new event.
        printAndClearEventBuf(&eventBuf);
    }

    // Post a STARTUP event with the number of capabilities
    postEventHeader(&eventBuf, EVENT_STARTUP);
    postCapNo(&eventBuf, n_caps);

    RELEASE_LOCK(&eventBufMutex);
}

Simon Marlow's avatar
Simon Marlow committed
742 743
void closeBlockMarker (EventsBuf *ebuf)
{
744 745
    StgInt8* save_pos;

Simon Marlow's avatar
Simon Marlow committed
746 747 748
    if (ebuf->marker)
    {
        // (type:16, time:64, size:32, end_time:64)
749 750 751 752 753 754 755

        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
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
        ebuf->marker = NULL;
    }
}


void postBlockMarker (EventsBuf *eb)
{
    if (!hasRoomForEvent(eb, EVENT_BLOCK_MARKER)) {
        printAndClearEventBuf(eb);
    }

    closeBlockMarker(eb);

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

void printAndClearEventBuf (EventsBuf *ebuf)
Simon Marlow's avatar
Simon Marlow committed
777 778 779
{
    StgWord64 numBytes = 0, written = 0;

Simon Marlow's avatar
Simon Marlow committed
780 781
    closeBlockMarker(ebuf);

782
    if (ebuf->begin != NULL && ebuf->pos != ebuf->begin)
Simon Marlow's avatar
Simon Marlow committed
783
    {
784
        numBytes = ebuf->pos - ebuf->begin;
Simon Marlow's avatar
Simon Marlow committed
785
        
786
        written = fwrite(ebuf->begin, 1, numBytes, event_log_file);
Simon Marlow's avatar
Simon Marlow committed
787 788 789 790 791 792 793
        if (written != numBytes) {
            debugBelch(
                "printAndClearEventLog: fwrite() failed, written=%" FMT_Word64
                " doesn't match numBytes=%" FMT_Word64, written, numBytes);
            return;
        }
        
794
        resetEventsBuf(ebuf);
Simon Marlow's avatar
Simon Marlow committed
795 796
        flushCount++;

Simon Marlow's avatar
Simon Marlow committed
797 798
        postBlockMarker(ebuf);
    }
Simon Marlow's avatar
Simon Marlow committed
799 800
}

801
void initEventsBuf(EventsBuf* eb, StgWord64 size, EventCapNo capno)
Simon Marlow's avatar
Simon Marlow committed
802
{
803
    eb->begin = eb->pos = stgMallocBytes(size, "initEventsBuf");
Simon Marlow's avatar
Simon Marlow committed
804
    eb->size = size;
Simon Marlow's avatar
Simon Marlow committed
805
    eb->marker = NULL;
806
    eb->capno = capno;
Simon Marlow's avatar
Simon Marlow committed
807 808 809 810 811
}

void resetEventsBuf(EventsBuf* eb)
{
    eb->pos = eb->begin;
812
    eb->marker = NULL;
Simon Marlow's avatar
Simon Marlow committed
813 814 815 816
}

StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum)
{
817
  nat size;
Simon Marlow's avatar
Simon Marlow committed
818

819
  size = sizeof(EventTypeNum) + sizeof(EventTimestamp) + eventTypes[eNum].size;
Simon Marlow's avatar
Simon Marlow committed
820 821 822 823 824 825 826 827

  if (eb->pos + size > eb->begin + eb->size) {
      return 0; // Not enough space.
  } else  {
      return 1; // Buf has enough space for the event.
  }
}

828 829 830 831 832 833 834 835 836 837 838 839 840 841
StgBool hasRoomForVariableEvent(EventsBuf *eb, nat payload_bytes)
{
  nat size;

  size = sizeof(EventTypeNum) + sizeof(EventTimestamp) +
      sizeof(EventPayloadSize) + payload_bytes;

  if (eb->pos + size > eb->begin + eb->size) {
      return 0; // Not enough space.
  } else  {
      return 1; // Buf has enough space for the event.
  }
}    

Simon Marlow's avatar
Simon Marlow committed
842
void postEventType(EventsBuf *eb, EventType *et)
Simon Marlow's avatar
Simon Marlow committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
{
    StgWord8 d;
    nat desclen;

    postInt32(eb, EVENT_ET_BEGIN);
    postEventTypeNum(eb, et->etNum);
    postWord16(eb, (StgWord16)et->size);
    desclen = strlen(et->desc);
    postWord32(eb, desclen);
    for (d = 0; d < desclen; ++d) {
        postInt8(eb, (StgInt8)et->desc[d]);
    }
    postWord32(eb, 0); // no extensions yet
    postInt32(eb, EVENT_ET_END);
}

859
#endif /* TRACING */