1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 2008-2009
5 * Support for fast binary event logging.
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
15 #include "Capability.h"
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
29 // PID of the process that writes to event_log_filename (#4512)
30 static pid_t event_log_pid = -1;
32 static char *event_log_filename = NULL;
34 // File for logging events
35 FILE *event_log_file = NULL;
37 #define EVENT_LOG_SIZE 2 * (1024 * 1024) // 2MB
39 static int flushCount;
41 // Struct for record keeping of buffer to store event types and events.
42 typedef struct _EventsBuf {
47 EventCapNo capno; // which capability this buffer belongs to, or -1
50 EventsBuf *capEventBuf; // one EventsBuf for each Capability
52 EventsBuf eventBuf; // an EventsBuf not associated with any Capability
54 Mutex eventBufMutex; // protected by this mutex
58 [EVENT_CREATE_THREAD] = "Create thread",
59 [EVENT_RUN_THREAD] = "Run thread",
60 [EVENT_STOP_THREAD] = "Stop thread",
61 [EVENT_THREAD_RUNNABLE] = "Thread runnable",
62 [EVENT_MIGRATE_THREAD] = "Migrate thread",
63 [EVENT_RUN_SPARK] = "Run spark",
64 [EVENT_STEAL_SPARK] = "Steal spark",
65 [EVENT_SHUTDOWN] = "Shutdown",
66 [EVENT_THREAD_WAKEUP] = "Wakeup thread",
67 [EVENT_GC_START] = "Starting GC",
68 [EVENT_GC_END] = "Finished GC",
69 [EVENT_REQUEST_SEQ_GC] = "Request sequential GC",
70 [EVENT_REQUEST_PAR_GC] = "Request parallel GC",
71 [EVENT_CREATE_SPARK_THREAD] = "Create spark thread",
72 [EVENT_LOG_MSG] = "Log message",
73 [EVENT_USER_MSG] = "User message",
74 [EVENT_STARTUP] = "Startup",
75 [EVENT_GC_IDLE] = "GC idle",
76 [EVENT_GC_WORK] = "GC working",
77 [EVENT_GC_DONE] = "GC done",
78 [EVENT_BLOCK_MARKER] = "Block marker",
79 [EVENT_CAPSET_CREATE] = "Create capability set",
80 [EVENT_CAPSET_DELETE] = "Delete capability set",
81 [EVENT_CAPSET_ASSIGN_CAP] = "Add capability to capability set",
82 [EVENT_CAPSET_REMOVE_CAP] = "Remove capability from capability set",
83 [EVENT_RTS_IDENTIFIER] = "RTS name and version",
84 [EVENT_PROGRAM_ARGS] = "Program arguments",
85 [EVENT_PROGRAM_ENV] = "Program environment variables",
86 [EVENT_OSPROCESS_PID] = "Process ID",
87 [EVENT_OSPROCESS_PPID] = "Parent process ID"
92 typedef struct _EventType {
93 EventTypeNum etNum; // Event Type number.
94 nat size; // size of the payload in bytes
95 char *desc; // Description
98 EventType eventTypes[NUM_EVENT_TAGS];
100 static void initEventsBuf(EventsBuf* eb, StgWord64 size, EventCapNo capno);
101 static void resetEventsBuf(EventsBuf* eb);
102 static void printAndClearEventBuf (EventsBuf *eventsBuf);
104 static void postEventType(EventsBuf *eb, EventType *et);
106 static void postLogMsg(EventsBuf *eb, EventTypeNum type, char *msg, va_list ap);
108 static void postBlockMarker(EventsBuf *eb);
109 static void closeBlockMarker(EventsBuf *ebuf);
111 static StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum);
112 static StgBool hasRoomForVariableEvent(EventsBuf *eb, nat payload_bytes);
114 static inline void postWord8(EventsBuf *eb, StgWord8 i)
119 static inline void postWord16(EventsBuf *eb, StgWord16 i)
121 postWord8(eb, (StgWord8)(i >> 8));
122 postWord8(eb, (StgWord8)i);
125 static inline void postWord32(EventsBuf *eb, StgWord32 i)
127 postWord16(eb, (StgWord16)(i >> 16));
128 postWord16(eb, (StgWord16)i);
131 static inline void postWord64(EventsBuf *eb, StgWord64 i)
133 postWord32(eb, (StgWord32)(i >> 32));
134 postWord32(eb, (StgWord32)i);
137 static inline void postBuf(EventsBuf *eb, StgWord8 *buf, nat size)
139 memcpy(eb->pos, buf, size);
143 static inline StgWord64 time_ns(void)
144 { return stat_getElapsedTime() * (1000000000LL/TICKS_PER_SECOND); }
146 static inline void postEventTypeNum(EventsBuf *eb, EventTypeNum etNum)
147 { postWord16(eb, etNum); }
149 static inline void postTimestamp(EventsBuf *eb)
150 { postWord64(eb, time_ns()); }
152 static inline void postThreadID(EventsBuf *eb, EventThreadID id)
153 { postWord32(eb,id); }
155 static inline void postCapNo(EventsBuf *eb, EventCapNo no)
156 { postWord16(eb,no); }
158 static inline void postCapsetID(EventsBuf *eb, EventCapsetID id)
159 { postWord32(eb,id); }
161 static inline void postCapsetType(EventsBuf *eb, EventCapsetType type)
162 { postWord16(eb,type); }
164 static inline void postPayloadSize(EventsBuf *eb, EventPayloadSize size)
165 { postWord16(eb,size); }
167 static inline void postEventHeader(EventsBuf *eb, EventTypeNum type)
169 postEventTypeNum(eb, type);
173 static inline void postInt8(EventsBuf *eb, StgInt8 i)
174 { postWord8(eb, (StgWord8)i); }
176 static inline void postInt16(EventsBuf *eb, StgInt16 i)
177 { postWord16(eb, (StgWord16)i); }
179 static inline void postInt32(EventsBuf *eb, StgInt32 i)
180 { postWord32(eb, (StgWord32)i); }
182 static inline void postInt64(EventsBuf *eb, StgInt64 i)
183 { postWord64(eb, (StgWord64)i); }
187 initEventLogging(void)
192 event_log_filename = stgMallocBytes(strlen(prog_name)
194 + 10 /* .eventlog */,
197 if (sizeof(EventDesc) / sizeof(char*) != NUM_EVENT_TAGS) {
198 barf("EventDesc array has the wrong number of elements");
201 if (event_log_pid == -1) { // #4512
203 sprintf(event_log_filename, "%s.eventlog", prog_name);
204 event_log_pid = getpid();
206 // Forked process, eventlog already started by the parent
208 event_log_pid = getpid();
209 sprintf(event_log_filename, "%s.%d.eventlog", prog_name, event_log_pid);
212 /* Open event log file for writing. */
213 if ((event_log_file = fopen(event_log_filename, "wb")) == NULL) {
214 sysErrorBelch("initEventLogging: can't open %s", event_log_filename);
215 stg_exit(EXIT_FAILURE);
219 * Allocate buffer(s) to store events.
220 * Create buffer large enough for the header begin marker, all event
221 * types, and header end marker to prevent checking if buffer has room
222 * for each of these steps, and remove the need to flush the buffer to
223 * disk during initialization.
225 * Use a single buffer to store the header with event types, then flush
226 * the buffer so all buffers are empty for writing events.
229 // XXX n_capabilities hasn't been initislised yet
230 n_caps = RtsFlags.ParFlags.nNodes;
234 capEventBuf = stgMallocBytes(n_caps * sizeof(EventsBuf),"initEventLogging");
236 for (c = 0; c < n_caps; ++c) {
237 // Init buffer for events.
238 initEventsBuf(&capEventBuf[c], EVENT_LOG_SIZE, c);
240 initEventsBuf(&eventBuf, EVENT_LOG_SIZE, (EventCapNo)(-1));
242 // Write in buffer: the header begin marker.
243 postInt32(&eventBuf, EVENT_HEADER_BEGIN);
245 // Mark beginning of event types in the header.
246 postInt32(&eventBuf, EVENT_HET_BEGIN);
247 for (t = 0; t < NUM_EVENT_TAGS; ++t) {
249 eventTypes[t].etNum = t;
250 eventTypes[t].desc = EventDesc[t];
253 case EVENT_CREATE_THREAD: // (cap, thread)
254 case EVENT_RUN_THREAD: // (cap, thread)
255 case EVENT_THREAD_RUNNABLE: // (cap, thread)
256 case EVENT_RUN_SPARK: // (cap, thread)
257 case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
258 eventTypes[t].size = sizeof(EventThreadID);
261 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
262 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
263 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
265 sizeof(EventThreadID) + sizeof(EventCapNo);
268 case EVENT_STOP_THREAD: // (cap, thread, status)
270 sizeof(EventThreadID) + sizeof(StgWord16) + sizeof(EventThreadID);
273 case EVENT_STARTUP: // (cap count)
274 eventTypes[t].size = sizeof(EventCapNo);
277 case EVENT_CAPSET_CREATE: // (capset, capset_type)
279 sizeof(EventCapsetID) + sizeof(EventCapsetType);
282 case EVENT_CAPSET_DELETE: // (capset)
283 eventTypes[t].size = sizeof(EventCapsetID);
286 case EVENT_CAPSET_ASSIGN_CAP: // (capset, cap)
287 case EVENT_CAPSET_REMOVE_CAP:
289 sizeof(EventCapsetID) + sizeof(EventCapNo);
292 case EVENT_OSPROCESS_PID: // (cap, pid)
293 case EVENT_OSPROCESS_PPID:
295 sizeof(EventCapsetID) + sizeof(StgWord32);
298 case EVENT_SHUTDOWN: // (cap)
299 case EVENT_REQUEST_SEQ_GC: // (cap)
300 case EVENT_REQUEST_PAR_GC: // (cap)
301 case EVENT_GC_START: // (cap)
302 case EVENT_GC_END: // (cap)
306 eventTypes[t].size = 0;
309 case EVENT_LOG_MSG: // (msg)
310 case EVENT_USER_MSG: // (msg)
311 case EVENT_RTS_IDENTIFIER: // (capset, str)
312 case EVENT_PROGRAM_ARGS: // (capset, strvec)
313 case EVENT_PROGRAM_ENV: // (capset, strvec)
314 eventTypes[t].size = 0xffff;
317 case EVENT_BLOCK_MARKER:
318 eventTypes[t].size = sizeof(StgWord32) + sizeof(EventTimestamp) +
323 continue; /* ignore deprecated events */
326 // Write in buffer: the start event type.
327 postEventType(&eventBuf, &eventTypes[t]);
330 // Mark end of event types in the header.
331 postInt32(&eventBuf, EVENT_HET_END);
333 // Write in buffer: the header end marker.
334 postInt32(&eventBuf, EVENT_HEADER_END);
336 // Prepare event buffer for events (data).
337 postInt32(&eventBuf, EVENT_DATA_BEGIN);
339 // Flush capEventBuf with header.
341 * Flush header and data begin marker to the file, thus preparing the
342 * file to have events written to it.
344 printAndClearEventBuf(&eventBuf);
346 for (c = 0; c < n_caps; ++c) {
347 postBlockMarker(&capEventBuf[c]);
351 initMutex(&eventBufMutex);
356 endEventLogging(void)
360 // Flush all events remaining in the buffers.
361 for (c = 0; c < n_capabilities; ++c) {
362 printAndClearEventBuf(&capEventBuf[c]);
364 printAndClearEventBuf(&eventBuf);
365 resetEventsBuf(&eventBuf); // we don't want the block marker
367 // Mark end of events (data).
368 postEventTypeNum(&eventBuf, EVENT_DATA_END);
370 // Flush the end of data marker.
371 printAndClearEventBuf(&eventBuf);
373 if (event_log_file != NULL) {
374 fclose(event_log_file);
379 freeEventLogging(void)
383 // Free events buffer.
384 for (c = 0; c < n_capabilities; ++c) {
385 if (capEventBuf[c].begin != NULL)
386 stgFree(capEventBuf[c].begin);
388 if (capEventBuf != NULL) {
389 stgFree(capEventBuf);
391 if (event_log_filename != NULL) {
392 stgFree(event_log_filename);
399 if (event_log_file != NULL) {
400 fflush(event_log_file);
405 abortEventLogging(void)
408 if (event_log_file != NULL) {
409 fclose(event_log_file);
413 * Post an event message to the capability's eventlog buffer.
414 * If the buffer is full, prints out the buffer and clears it.
417 postSchedEvent (Capability *cap,
425 eb = &capEventBuf[cap->no];
427 if (!hasRoomForEvent(eb, tag)) {
428 // Flush event buffer to make room for new event.
429 printAndClearEventBuf(eb);
432 postEventHeader(eb, tag);
435 case EVENT_CREATE_THREAD: // (cap, thread)
436 case EVENT_RUN_THREAD: // (cap, thread)
437 case EVENT_THREAD_RUNNABLE: // (cap, thread)
438 case EVENT_RUN_SPARK: // (cap, thread)
440 postThreadID(eb,thread);
444 case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
446 postThreadID(eb,info1 /* spark_thread */);
450 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
451 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
452 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
454 postThreadID(eb,thread);
455 postCapNo(eb,info1 /* new_cap | victim_cap | other_cap */);
459 case EVENT_STOP_THREAD: // (cap, thread, status)
461 postThreadID(eb,thread);
462 postWord16(eb,info1 /* status */);
463 postThreadID(eb,info2 /* blocked on thread */);
467 case EVENT_SHUTDOWN: // (cap)
468 case EVENT_REQUEST_SEQ_GC: // (cap)
469 case EVENT_REQUEST_PAR_GC: // (cap)
470 case EVENT_GC_START: // (cap)
471 case EVENT_GC_END: // (cap)
477 barf("postEvent: unknown event tag %d", tag);
481 void postCapsetModifyEvent (EventTypeNum tag,
482 EventCapsetID capset,
485 ACQUIRE_LOCK(&eventBufMutex);
487 if (!hasRoomForEvent(&eventBuf, tag)) {
488 // Flush event buffer to make room for new event.
489 printAndClearEventBuf(&eventBuf);
492 postEventHeader(&eventBuf, tag);
493 postCapsetID(&eventBuf, capset);
496 case EVENT_CAPSET_CREATE: // (capset, capset_type)
498 postCapsetType(&eventBuf, other /* capset_type */);
502 case EVENT_CAPSET_DELETE: // (capset)
507 case EVENT_CAPSET_ASSIGN_CAP: // (capset, capno)
508 case EVENT_CAPSET_REMOVE_CAP: // (capset, capno)
510 postCapNo(&eventBuf, other /* capno */);
513 case EVENT_OSPROCESS_PID: // (capset, pid)
514 case EVENT_OSPROCESS_PPID: // (capset, parent_pid)
516 postWord32(&eventBuf, other);
520 barf("postCapsetModifyEvent: unknown event tag %d", tag);
523 RELEASE_LOCK(&eventBufMutex);
526 void postCapsetStrEvent (EventTypeNum tag,
527 EventCapsetID capset,
530 int strsize = strlen(msg);
531 int size = strsize + sizeof(EventCapsetID);
533 ACQUIRE_LOCK(&eventBufMutex);
535 if (!hasRoomForVariableEvent(&eventBuf, size)){
536 printAndClearEventBuf(&eventBuf);
538 if (!hasRoomForVariableEvent(&eventBuf, size)){
539 // Event size exceeds buffer size, bail out:
540 RELEASE_LOCK(&eventBufMutex);
545 postEventHeader(&eventBuf, tag);
546 postPayloadSize(&eventBuf, size);
547 postCapsetID(&eventBuf, capset);
549 postBuf(&eventBuf, (StgWord8*) msg, strsize);
551 RELEASE_LOCK(&eventBufMutex);
554 void postCapsetVecEvent (EventTypeNum tag,
555 EventCapsetID capset,
559 int i, size = sizeof(EventCapsetID);
561 for (i = 0; i < argc; i++) {
562 // 1 + strlen to account for the trailing \0, used as separator
563 size += 1 + strlen(argv[i]);
566 ACQUIRE_LOCK(&eventBufMutex);
568 if (!hasRoomForVariableEvent(&eventBuf, size)){
569 printAndClearEventBuf(&eventBuf);
571 if(!hasRoomForVariableEvent(&eventBuf, size)){
572 // Event size exceeds buffer size, bail out:
573 RELEASE_LOCK(&eventBufMutex);
578 postEventHeader(&eventBuf, tag);
579 postPayloadSize(&eventBuf, size);
580 postCapsetID(&eventBuf, capset);
582 for( i = 0; i < argc; i++ ) {
583 // again, 1 + to account for \0
584 postBuf(&eventBuf, (StgWord8*) argv[i], 1 + strlen(argv[i]));
587 RELEASE_LOCK(&eventBufMutex);
591 postEvent (Capability *cap, EventTypeNum tag)
595 eb = &capEventBuf[cap->no];
597 if (!hasRoomForEvent(eb, tag)) {
598 // Flush event buffer to make room for new event.
599 printAndClearEventBuf(eb);
602 postEventHeader(eb, tag);
607 void postLogMsg(EventsBuf *eb, EventTypeNum type, char *msg, va_list ap)
612 size = vsnprintf(buf,BUF,msg,ap);
618 if (!hasRoomForVariableEvent(eb, size)) {
619 // Flush event buffer to make room for new event.
620 printAndClearEventBuf(eb);
623 postEventHeader(eb, type);
624 postPayloadSize(eb, size);
625 postBuf(eb,(StgWord8*)buf,size);
628 void postMsg(char *msg, va_list ap)
630 ACQUIRE_LOCK(&eventBufMutex);
631 postLogMsg(&eventBuf, EVENT_LOG_MSG, msg, ap);
632 RELEASE_LOCK(&eventBufMutex);
635 void postCapMsg(Capability *cap, char *msg, va_list ap)
637 postLogMsg(&capEventBuf[cap->no], EVENT_LOG_MSG, msg, ap);
640 void postUserMsg(Capability *cap, char *msg, va_list ap)
642 postLogMsg(&capEventBuf[cap->no], EVENT_USER_MSG, msg, ap);
645 void postEventStartup(EventCapNo n_caps)
647 ACQUIRE_LOCK(&eventBufMutex);
649 if (!hasRoomForEvent(&eventBuf, EVENT_STARTUP)) {
650 // Flush event buffer to make room for new event.
651 printAndClearEventBuf(&eventBuf);
654 // Post a STARTUP event with the number of capabilities
655 postEventHeader(&eventBuf, EVENT_STARTUP);
656 postCapNo(&eventBuf, n_caps);
658 RELEASE_LOCK(&eventBufMutex);
661 void closeBlockMarker (EventsBuf *ebuf)
667 // (type:16, time:64, size:32, end_time:64)
669 save_pos = ebuf->pos;
670 ebuf->pos = ebuf->marker + sizeof(EventTypeNum) +
671 sizeof(EventTimestamp);
672 postWord32(ebuf, save_pos - ebuf->marker);
674 ebuf->pos = save_pos;
680 void postBlockMarker (EventsBuf *eb)
682 if (!hasRoomForEvent(eb, EVENT_BLOCK_MARKER)) {
683 printAndClearEventBuf(eb);
686 closeBlockMarker(eb);
688 eb->marker = eb->pos;
689 postEventHeader(eb, EVENT_BLOCK_MARKER);
690 postWord32(eb,0); // these get filled in later by closeBlockMarker();
692 postCapNo(eb, eb->capno);
695 void printAndClearEventBuf (EventsBuf *ebuf)
697 StgWord64 numBytes = 0, written = 0;
699 closeBlockMarker(ebuf);
701 if (ebuf->begin != NULL && ebuf->pos != ebuf->begin)
703 numBytes = ebuf->pos - ebuf->begin;
705 written = fwrite(ebuf->begin, 1, numBytes, event_log_file);
706 if (written != numBytes) {
708 "printAndClearEventLog: fwrite() failed, written=%" FMT_Word64
709 " doesn't match numBytes=%" FMT_Word64, written, numBytes);
713 resetEventsBuf(ebuf);
716 postBlockMarker(ebuf);
720 void initEventsBuf(EventsBuf* eb, StgWord64 size, EventCapNo capno)
722 eb->begin = eb->pos = stgMallocBytes(size, "initEventsBuf");
728 void resetEventsBuf(EventsBuf* eb)
734 StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum)
738 size = sizeof(EventTypeNum) + sizeof(EventTimestamp) + eventTypes[eNum].size;
740 if (eb->pos + size > eb->begin + eb->size) {
741 return 0; // Not enough space.
743 return 1; // Buf has enough space for the event.
747 StgBool hasRoomForVariableEvent(EventsBuf *eb, nat payload_bytes)
751 size = sizeof(EventTypeNum) + sizeof(EventTimestamp) +
752 sizeof(EventPayloadSize) + payload_bytes;
754 if (eb->pos + size > eb->begin + eb->size) {
755 return 0; // Not enough space.
757 return 1; // Buf has enough space for the event.
761 void postEventType(EventsBuf *eb, EventType *et)
766 postInt32(eb, EVENT_ET_BEGIN);
767 postEventTypeNum(eb, et->etNum);
768 postWord16(eb, (StgWord16)et->size);
769 desclen = strlen(et->desc);
770 postWord32(eb, desclen);
771 for (d = 0; d < desclen; ++d) {
772 postInt8(eb, (StgInt8)et->desc[d]);
774 postWord32(eb, 0); // no extensions yet
775 postInt32(eb, EVENT_ET_END);