1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 2008-2009
5 * Support for fast binary event logging.
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
15 #include "Capability.h"
23 static char *event_log_filename = NULL;
25 // File for logging events
26 FILE *event_log_file = NULL;
28 #define EVENT_LOG_SIZE 2 * (1024 * 1024) // 2MB
30 static int flushCount;
32 // Struct for record keeping of buffer to store event types and events.
33 typedef struct _EventsBuf {
38 EventCapNo capno; // which capability this buffer belongs to, or -1
41 EventsBuf *capEventBuf; // one EventsBuf for each Capability
43 EventsBuf eventBuf; // an EventsBuf not associated with any Capability
45 Mutex eventBufMutex; // protected by this mutex
49 [EVENT_CREATE_THREAD] = "Create thread",
50 [EVENT_RUN_THREAD] = "Run thread",
51 [EVENT_STOP_THREAD] = "Stop thread",
52 [EVENT_THREAD_RUNNABLE] = "Thread runnable",
53 [EVENT_MIGRATE_THREAD] = "Migrate thread",
54 [EVENT_RUN_SPARK] = "Run spark",
55 [EVENT_STEAL_SPARK] = "Steal spark",
56 [EVENT_SHUTDOWN] = "Shutdown",
57 [EVENT_THREAD_WAKEUP] = "Wakeup thread",
58 [EVENT_GC_START] = "Starting GC",
59 [EVENT_GC_END] = "Finished GC",
60 [EVENT_REQUEST_SEQ_GC] = "Request sequential GC",
61 [EVENT_REQUEST_PAR_GC] = "Request parallel GC",
62 [EVENT_CREATE_SPARK_THREAD] = "Create spark thread",
63 [EVENT_LOG_MSG] = "Log message",
64 [EVENT_USER_MSG] = "User message",
65 [EVENT_STARTUP] = "Startup",
66 [EVENT_GC_IDLE] = "GC idle",
67 [EVENT_GC_WORK] = "GC working",
68 [EVENT_GC_DONE] = "GC done",
69 [EVENT_BLOCK_MARKER] = "Block marker"
74 typedef struct _EventType {
75 EventTypeNum etNum; // Event Type number.
76 nat size; // size of the payload in bytes
77 char *desc; // Description
80 EventType eventTypes[NUM_EVENT_TAGS];
82 static void initEventsBuf(EventsBuf* eb, StgWord64 size, EventCapNo capno);
83 static void resetEventsBuf(EventsBuf* eb);
84 static void printAndClearEventBuf (EventsBuf *eventsBuf);
86 static void postEventType(EventsBuf *eb, EventType *et);
88 static void postLogMsg(EventsBuf *eb, EventTypeNum type, char *msg, va_list ap);
90 static void postBlockMarker(EventsBuf *eb);
91 static void closeBlockMarker(EventsBuf *ebuf);
93 static StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum);
94 static StgBool hasRoomForVariableEvent(EventsBuf *eb, nat payload_bytes);
96 static inline void postWord8(EventsBuf *eb, StgWord8 i)
101 static inline void postWord16(EventsBuf *eb, StgWord16 i)
103 postWord8(eb, (StgWord8)(i >> 8));
104 postWord8(eb, (StgWord8)i);
107 static inline void postWord32(EventsBuf *eb, StgWord32 i)
109 postWord16(eb, (StgWord16)(i >> 16));
110 postWord16(eb, (StgWord16)i);
113 static inline void postWord64(EventsBuf *eb, StgWord64 i)
115 postWord32(eb, (StgWord32)(i >> 32));
116 postWord32(eb, (StgWord32)i);
119 static inline void postBuf(EventsBuf *eb, StgWord8 *buf, nat size)
121 memcpy(eb->pos, buf, size);
125 static inline StgWord64 time_ns(void)
126 { return stat_getElapsedTime() * (1000000000LL/TICKS_PER_SECOND); }
128 static inline void postEventTypeNum(EventsBuf *eb, EventTypeNum etNum)
129 { postWord16(eb, etNum); }
131 static inline void postTimestamp(EventsBuf *eb)
132 { postWord64(eb, time_ns()); }
134 static inline void postThreadID(EventsBuf *eb, EventThreadID id)
135 { postWord32(eb,id); }
137 static inline void postCapNo(EventsBuf *eb, EventCapNo no)
138 { postWord16(eb,no); }
140 static inline void postPayloadSize(EventsBuf *eb, EventPayloadSize size)
141 { postWord16(eb,size); }
143 static inline void postEventHeader(EventsBuf *eb, EventTypeNum type)
145 postEventTypeNum(eb, type);
149 static inline void postInt8(EventsBuf *eb, StgInt8 i)
150 { postWord8(eb, (StgWord8)i); }
152 static inline void postInt16(EventsBuf *eb, StgInt16 i)
153 { postWord16(eb, (StgWord16)i); }
155 static inline void postInt32(EventsBuf *eb, StgInt32 i)
156 { postWord32(eb, (StgWord32)i); }
158 static inline void postInt64(EventsBuf *eb, StgInt64 i)
159 { postWord64(eb, (StgWord64)i); }
163 initEventLogging(void)
168 event_log_filename = stgMallocBytes(strlen(prog_name) + 10,
171 if (sizeof(EventDesc) / sizeof(char*) != NUM_EVENT_TAGS) {
172 barf("EventDesc array has the wrong number of elements");
175 sprintf(event_log_filename, "%s.eventlog", prog_name);
177 /* Open event log file for writing. */
178 if ((event_log_file = fopen(event_log_filename, "wb")) == NULL) {
179 sysErrorBelch("initEventLogging: can't open %s", event_log_filename);
180 stg_exit(EXIT_FAILURE);
184 * Allocate buffer(s) to store events.
185 * Create buffer large enough for the header begin marker, all event
186 * types, and header end marker to prevent checking if buffer has room
187 * for each of these steps, and remove the need to flush the buffer to
188 * disk during initialization.
190 * Use a single buffer to store the header with event types, then flush
191 * the buffer so all buffers are empty for writing events.
194 // XXX n_capabilities hasn't been initislised yet
195 n_caps = RtsFlags.ParFlags.nNodes;
199 capEventBuf = stgMallocBytes(n_caps * sizeof(EventsBuf),"initEventLogging");
201 for (c = 0; c < n_caps; ++c) {
202 // Init buffer for events.
203 initEventsBuf(&capEventBuf[c], EVENT_LOG_SIZE, c);
205 initEventsBuf(&eventBuf, EVENT_LOG_SIZE, (EventCapNo)(-1));
207 // Write in buffer: the header begin marker.
208 postInt32(&eventBuf, EVENT_HEADER_BEGIN);
210 // Mark beginning of event types in the header.
211 postInt32(&eventBuf, EVENT_HET_BEGIN);
212 for (t = 0; t < NUM_EVENT_TAGS; ++t) {
214 eventTypes[t].etNum = t;
215 eventTypes[t].desc = EventDesc[t];
218 case EVENT_CREATE_THREAD: // (cap, thread)
219 case EVENT_RUN_THREAD: // (cap, thread)
220 case EVENT_THREAD_RUNNABLE: // (cap, thread)
221 case EVENT_RUN_SPARK: // (cap, thread)
222 case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
223 eventTypes[t].size = sizeof(EventThreadID);
226 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
227 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
228 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
230 sizeof(EventThreadID) + sizeof(EventCapNo);
233 case EVENT_STOP_THREAD: // (cap, thread, status)
235 sizeof(EventThreadID) + sizeof(StgWord16);
238 case EVENT_SHUTDOWN: // (cap)
239 case EVENT_REQUEST_SEQ_GC: // (cap)
240 case EVENT_REQUEST_PAR_GC: // (cap)
241 case EVENT_GC_START: // (cap)
242 case EVENT_GC_END: // (cap)
247 eventTypes[t].size = 0;
250 case EVENT_LOG_MSG: // (msg)
251 case EVENT_USER_MSG: // (msg)
252 eventTypes[t].size = 0xffff;
255 case EVENT_BLOCK_MARKER:
256 eventTypes[t].size = sizeof(StgWord32) + sizeof(EventTimestamp) +
261 continue; /* ignore deprecated events */
264 // Write in buffer: the start event type.
265 postEventType(&eventBuf, &eventTypes[t]);
268 // Mark end of event types in the header.
269 postInt32(&eventBuf, EVENT_HET_END);
271 // Write in buffer: the header end marker.
272 postInt32(&eventBuf, EVENT_HEADER_END);
274 // Prepare event buffer for events (data).
275 postInt32(&eventBuf, EVENT_DATA_BEGIN);
277 // Post a STARTUP event with the number of capabilities
278 postEventHeader(&eventBuf, EVENT_STARTUP);
279 postCapNo(&eventBuf, n_caps);
281 // Flush capEventBuf with header.
283 * Flush header and data begin marker to the file, thus preparing the
284 * file to have events written to it.
286 printAndClearEventBuf(&eventBuf);
288 for (c = 0; c < n_caps; ++c) {
289 postBlockMarker(&capEventBuf[c]);
294 endEventLogging(void)
298 // Flush all events remaining in the buffers.
299 for (c = 0; c < n_capabilities; ++c) {
300 printAndClearEventBuf(&capEventBuf[c]);
302 printAndClearEventBuf(&eventBuf);
303 resetEventsBuf(&eventBuf); // we don't want the block marker
305 // Mark end of events (data).
306 postEventTypeNum(&eventBuf, EVENT_DATA_END);
308 // Flush the end of data marker.
309 printAndClearEventBuf(&eventBuf);
311 if (event_log_file != NULL) {
312 fclose(event_log_file);
317 freeEventLogging(void)
321 // Free events buffer.
322 for (c = 0; c < n_capabilities; ++c) {
323 if (capEventBuf[c].begin != NULL)
324 stgFree(capEventBuf[c].begin);
326 if (capEventBuf != NULL) {
327 stgFree(capEventBuf);
329 if (event_log_filename != NULL) {
330 stgFree(event_log_filename);
335 * Post an event message to the capability's eventlog buffer.
336 * If the buffer is full, prints out the buffer and clears it.
339 postSchedEvent (Capability *cap,
346 eb = &capEventBuf[cap->no];
348 if (!hasRoomForEvent(eb, tag)) {
349 // Flush event buffer to make room for new event.
350 printAndClearEventBuf(eb);
353 postEventHeader(eb, tag);
356 case EVENT_CREATE_THREAD: // (cap, thread)
357 case EVENT_RUN_THREAD: // (cap, thread)
358 case EVENT_THREAD_RUNNABLE: // (cap, thread)
359 case EVENT_RUN_SPARK: // (cap, thread)
361 postThreadID(eb,thread);
365 case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
367 postThreadID(eb,other /* spark_thread */);
371 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
372 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
373 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
375 postThreadID(eb,thread);
376 postCapNo(eb,other /* new_cap | victim_cap | other_cap */);
380 case EVENT_STOP_THREAD: // (cap, thread, status)
382 postThreadID(eb,thread);
383 postWord16(eb,other /* status */);
387 case EVENT_SHUTDOWN: // (cap)
388 case EVENT_REQUEST_SEQ_GC: // (cap)
389 case EVENT_REQUEST_PAR_GC: // (cap)
390 case EVENT_GC_START: // (cap)
391 case EVENT_GC_END: // (cap)
397 barf("postEvent: unknown event tag %d", tag);
402 postEvent (Capability *cap, EventTypeNum tag)
406 eb = &capEventBuf[cap->no];
408 if (!hasRoomForEvent(eb, tag)) {
409 // Flush event buffer to make room for new event.
410 printAndClearEventBuf(eb);
413 postEventHeader(eb, tag);
418 void postLogMsg(EventsBuf *eb, EventTypeNum type, char *msg, va_list ap)
423 size = vsnprintf(buf,BUF,msg,ap);
429 if (!hasRoomForVariableEvent(eb, size)) {
430 // Flush event buffer to make room for new event.
431 printAndClearEventBuf(eb);
434 postEventHeader(eb, type);
435 postPayloadSize(eb, size);
436 postBuf(eb,(StgWord8*)buf,size);
439 void postMsg(char *msg, va_list ap)
441 ACQUIRE_LOCK(&eventBufMutex);
442 postLogMsg(&eventBuf, EVENT_LOG_MSG, msg, ap);
443 RELEASE_LOCK(&eventBufMutex);
446 void postCapMsg(Capability *cap, char *msg, va_list ap)
448 postLogMsg(&capEventBuf[cap->no], EVENT_LOG_MSG, msg, ap);
451 void postUserMsg(Capability *cap, char *msg, va_list ap)
453 postLogMsg(&capEventBuf[cap->no], EVENT_USER_MSG, msg, ap);
456 void closeBlockMarker (EventsBuf *ebuf)
462 // (type:16, time:64, size:32, end_time:64)
464 save_pos = ebuf->pos;
465 ebuf->pos = ebuf->marker + sizeof(EventTypeNum) +
466 sizeof(EventTimestamp);
467 postWord32(ebuf, save_pos - ebuf->marker);
469 ebuf->pos = save_pos;
475 void postBlockMarker (EventsBuf *eb)
477 if (!hasRoomForEvent(eb, EVENT_BLOCK_MARKER)) {
478 printAndClearEventBuf(eb);
481 closeBlockMarker(eb);
483 eb->marker = eb->pos;
484 postEventHeader(eb, EVENT_BLOCK_MARKER);
485 postWord32(eb,0); // these get filled in later by closeBlockMarker();
487 postCapNo(eb, eb->capno);
490 void printAndClearEventBuf (EventsBuf *ebuf)
492 StgWord64 numBytes = 0, written = 0;
494 closeBlockMarker(ebuf);
496 if (ebuf->begin != NULL && ebuf->pos != ebuf->begin)
498 numBytes = ebuf->pos - ebuf->begin;
500 written = fwrite(ebuf->begin, 1, numBytes, event_log_file);
501 if (written != numBytes) {
503 "printAndClearEventLog: fwrite() failed, written=%" FMT_Word64
504 " doesn't match numBytes=%" FMT_Word64, written, numBytes);
508 resetEventsBuf(ebuf);
511 postBlockMarker(ebuf);
515 void initEventsBuf(EventsBuf* eb, StgWord64 size, EventCapNo capno)
517 eb->begin = eb->pos = stgMallocBytes(size, "initEventsBuf");
523 void resetEventsBuf(EventsBuf* eb)
529 StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum)
533 size = sizeof(EventTypeNum) + sizeof(EventTimestamp) + eventTypes[eNum].size;
535 if (eb->pos + size > eb->begin + eb->size) {
536 return 0; // Not enough space.
538 return 1; // Buf has enough space for the event.
542 StgBool hasRoomForVariableEvent(EventsBuf *eb, nat payload_bytes)
546 size = sizeof(EventTypeNum) + sizeof(EventTimestamp) +
547 sizeof(EventPayloadSize) + payload_bytes;
549 if (eb->pos + size > eb->begin + eb->size) {
550 return 0; // Not enough space.
552 return 1; // Buf has enough space for the event.
556 void postEventType(EventsBuf *eb, EventType *et)
561 postInt32(eb, EVENT_ET_BEGIN);
562 postEventTypeNum(eb, et->etNum);
563 postWord16(eb, (StgWord16)et->size);
564 desclen = strlen(et->desc);
565 postWord32(eb, desclen);
566 for (d = 0; d < desclen; ++d) {
567 postInt8(eb, (StgInt8)et->desc[d]);
569 postWord32(eb, 0); // no extensions yet
570 postInt32(eb, EVENT_ET_END);