1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 2008-2009
5 * Support for fast binary event logging.
7 * ---------------------------------------------------------------------------*/
13 #include "Capability.h"
20 static char *event_log_filename = NULL;
22 // File for logging events
23 FILE *event_log_file = NULL;
25 #define EVENT_LOG_SIZE 2 * (1024 * 1024) // 2MB
27 static int flushCount;
29 // Struct for record keeping of buffer to store event types and events.
30 typedef struct _EventsBuf {
48 "Request sequential GC",
49 "Request parallel GC",
56 typedef struct _EventType {
57 EventTypeNum etNum; // Event Type number.
58 nat size; // size of the payload in bytes
59 char *desc; // Description
62 EventType eventTypes[NUM_EVENT_TAGS];
64 static void printAndClearEventBuf (EventsBuf *eventsBuf);
65 static void initEventsBuf(EventsBuf* eb, StgWord64 size);
66 static void resetEventsBuf(EventsBuf* eb);
68 static void beginHeader(EventsBuf *eb);
69 static void endHeader(EventsBuf *eb);
71 static void beginData(EventsBuf *eb);
72 static void endData(EventsBuf *eb);
74 static void beginEventTypes(EventsBuf *eb);
75 static void endEventTypes(EventsBuf *eb);
77 static void postEventType(EventsBuf *eb, EventType *et);
79 static StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum);
81 static inline void postWord8(EventsBuf *eb, StgWord8 i)
86 static inline void postWord16(EventsBuf *eb, StgWord16 i)
88 postWord8(eb, (StgWord8)(i >> 8));
89 postWord8(eb, (StgWord8)i);
92 static inline void postWord32(EventsBuf *eb, StgWord32 i)
94 postWord16(eb, (StgWord16)(i >> 16));
95 postWord16(eb, (StgWord16)i);
98 static inline void postWord64(EventsBuf *eb, StgWord64 i)
100 postWord32(eb, (StgWord32)(i >> 32));
101 postWord32(eb, (StgWord32)i);
104 static inline void postEventTypeNum(EventsBuf *eb, EventTypeNum etNum)
105 { postWord16(eb, etNum); }
107 static inline void postEventTypeID(EventsBuf *eb, StgWord16 etID)
108 { postWord16(eb, etID); }
110 static inline void postTimestamp(EventsBuf *eb, Timestamp t)
111 { postWord64(eb,t); }
113 static inline void postInt8(EventsBuf *eb, StgInt8 i)
114 { postWord8(eb, (StgWord8)i); }
116 static inline void postInt16(EventsBuf *eb, StgInt16 i)
117 { postWord16(eb, (StgWord16)i); }
119 static inline void postInt32(EventsBuf *eb, StgInt32 i)
120 { postWord32(eb, (StgWord32)i); }
122 static inline void postInt64(EventsBuf *eb, StgInt64 i)
123 { postWord64(eb, (StgWord64)i); }
127 initEventLogging(void)
131 debugTrace(DEBUG_eventlog, "intiEventLog: start");
133 event_log_filename = stgMallocBytes(strlen(prog_name) + 9,
136 if (sizeof(EventDesc) / sizeof(char*) != NUM_EVENT_TAGS) {
137 barf("EventDesc array has the wrong number of elements");
140 sprintf(event_log_filename, "%s.eventlog", prog_name);
142 /* Open event log file for writing. */
143 if ((event_log_file = fopen(event_log_filename, "wb")) == NULL) {
144 sysErrorBelch("initEventLoggin: can't open %s", event_log_filename);
145 stg_exit(EXIT_FAILURE);
149 * Allocate buffer(s) to store events.
150 * Create buffer large enough for the header begin marker, all event
151 * types, and header end marker to prevent checking if buffer has room
152 * for each of these steps, and remove the need to flush the buffer to
153 * disk during initialization.
155 * Use a single buffer to store the header with event types, then flush
156 * the buffer so all buffers are empty for writing events.
158 eventsBuf = stgMallocBytes(n_capabilities * sizeof(EventsBuf),"initEventLogging");
160 for (c = 0; c < n_capabilities; ++c) {
161 // Init buffer for events.
162 initEventsBuf(&eventsBuf[c], EVENT_LOG_SIZE);
165 // Write in buffer: the header begin marker.
166 beginHeader(&eventsBuf[0]);
168 // Mark beginning of event types in the header.
169 beginEventTypes(&eventsBuf[0]);
170 for (t = 0; t < NUM_EVENT_TAGS; ++t) {
172 eventTypes[t].etNum = t;
173 eventTypes[t].desc = EventDesc[t];
176 case EVENT_CREATE_THREAD: // (cap, thread)
177 case EVENT_RUN_THREAD: // (cap, thread)
178 case EVENT_THREAD_RUNNABLE: // (cap, thread)
179 case EVENT_RUN_SPARK: // (cap, thread)
180 eventTypes[t].size = sizeof(CapabilityNum) + sizeof(ThreadID);
183 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
184 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
185 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
187 sizeof(CapabilityNum) + sizeof(ThreadID) +
188 sizeof(CapabilityNum);
191 case EVENT_STOP_THREAD: // (cap, thread, status)
193 sizeof(CapabilityNum) + sizeof(ThreadID) + sizeof(StgWord16);
196 case EVENT_SHUTDOWN: // (cap)
197 case EVENT_REQUEST_SEQ_GC: // (cap)
198 case EVENT_REQUEST_PAR_GC: // (cap)
199 case EVENT_GC_START: // (cap)
200 case EVENT_GC_END: // (cap)
201 eventTypes[t].size = sizeof(CapabilityNum);
205 // Write in buffer: the start event type.
206 postEventType(&eventsBuf[0], &eventTypes[t]);
209 // Mark end of event types in the header.
210 endEventTypes(&eventsBuf[0]);
212 // Write in buffer: the header end marker.
213 endHeader(&eventsBuf[0]);
215 // Prepare event buffer for events (data).
216 beginData(&eventsBuf[0]);
218 // Flush eventsBuf with header.
220 * Flush header and data begin marker to the file, thus preparing the
221 * file to have events written to it.
223 printAndClearEventBuf(&eventsBuf[0]);
225 debugTrace(DEBUG_eventlog, "initEventLog: finish");
229 endEventLogging(void)
233 debugTrace(DEBUG_eventlog,"endEventLog: start");
235 // Flush all events remaining in the buffers.
236 for (c = 0; c < n_capabilities; ++c) {
237 printAndClearEventBuf(&eventsBuf[c]);
240 // Mark end of events (data).
241 endData(&eventsBuf[0]);
243 // Flush the end of data marker.
244 printAndClearEventBuf(&eventsBuf[0]);
246 if (event_log_file != NULL) {
247 fclose(event_log_file);
250 debugTrace(DEBUG_eventlog,"endEventLog: finish");
254 freeEventLogging(void)
258 debugTrace(DEBUG_eventlog,"freeEventLog: start");
260 // Free events buffer.
261 for (c = 0; c < n_capabilities; ++c) {
262 if (eventsBuf[c].begin != NULL)
263 stgFree(eventsBuf[c].begin);
265 if (eventsBuf != NULL) {
268 if (event_log_filename != NULL) {
269 stgFree(event_log_filename);
272 debugTrace(DEBUG_eventlog,"freeEventLog: finish");
276 * Post an event message to the capability's eventlog buffer.
277 * If the buffer is full, prints out the buffer and clears it.
280 postEvent_(Capability *cap, EventTypeNum tag, StgThreadID thread, nat other_cap)
284 debugTrace(DEBUG_eventlog,"postEvent: start");
286 eb = &eventsBuf[cap->no];
288 if (!hasRoomForEvent(eb, tag)) {
289 // Flush event buffer to make room for new event.
290 printAndClearEventBuf(eb);
293 postEventTypeNum(eb, tag);
294 postTimestamp(eb, stat_getElapsedTime() * (1000000000LL/TICKS_PER_SECOND));
295 postWord16(eb, cap->no);
298 case EVENT_CREATE_THREAD: // (cap, thread)
299 case EVENT_RUN_THREAD: // (cap, thread)
300 case EVENT_THREAD_RUNNABLE: // (cap, thread)
301 case EVENT_RUN_SPARK: // (cap, thread)
303 postWord64(eb,thread);
307 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
308 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
309 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
311 postWord64(eb,thread);
312 postWord16(eb,other_cap);
316 case EVENT_STOP_THREAD: // (cap, thread, status)
318 postWord64(eb,thread);
319 postWord16(eb,other_cap);
323 case EVENT_SHUTDOWN: // (cap)
324 case EVENT_REQUEST_SEQ_GC: // (cap)
325 case EVENT_REQUEST_PAR_GC: // (cap)
326 case EVENT_GC_START: // (cap)
327 case EVENT_GC_END: // (cap)
333 barf("postEvent: unknown event tag %d", tag);
336 debugTrace(DEBUG_eventlog,"postEvent: finish");
339 static void printAndClearEventBuf (EventsBuf *eventsBuf)
341 StgWord64 numBytes = 0, written = 0;
343 if (eventsBuf->begin != NULL && eventsBuf->pos != eventsBuf->begin)
345 numBytes = eventsBuf->pos - eventsBuf->begin;
347 debugTrace(DEBUG_eventlog,
348 "printAndEventLog: numbytes %" FMT_Word64
349 " bytes to fwrite()",
352 written = fwrite(eventsBuf->begin, 1, numBytes, event_log_file);
353 if (written != numBytes) {
355 "printAndClearEventLog: fwrite() failed, written=%" FMT_Word64
356 " doesn't match numBytes=%" FMT_Word64, written, numBytes);
360 debugTrace(DEBUG_eventlog,
361 "printAndClearEventLog: fwrite(): %" FMT_Word64
362 " bytes written", written);
364 resetEventsBuf(eventsBuf);
370 printAndClearEventLog(Capability *cap)
372 debugTrace(DEBUG_eventlog,"printAndClearEventLog: start");
374 printAndClearEventBuf(&eventsBuf[cap->no]);
376 debugTrace(DEBUG_eventlog,"printAndClearEventLog: finish");
379 /* -----------------------------------------------------------------------------
380 Actual event generation below here
381 -------------------------------------------------------------------------- */
383 void initEventsBuf(EventsBuf* eb, StgWord64 size)
385 eb->begin = eb->pos = malloc(size);
389 void resetEventsBuf(EventsBuf* eb)
394 // N.B.: Assuming buffer contains enough space for the header begin marker.
395 void beginHeader(EventsBuf *eb)
397 postInt32(eb, EVENT_HEADER_BEGIN);
400 // N.B.: Assuming buffer contains enough space for the header end marker.
401 void endHeader(EventsBuf *eb)
403 postInt32(eb, EVENT_HEADER_END);
406 void beginData(EventsBuf *eb)
408 postInt32(eb, EVENT_DATA_BEGIN);
411 void endData(EventsBuf *eb)
413 postEventTypeNum(eb, EVENT_DATA_END);
416 void beginEventTypes(EventsBuf *eb)
418 postInt32(eb, EVENT_HET_BEGIN);
421 void endEventTypes(EventsBuf *eb)
423 postInt32(eb, EVENT_HET_END);
426 StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum)
430 size += sizeof(EventTypeNum) + sizeof(Timestamp) + eventTypes[eNum].size;
432 if (eb->pos + size > eb->begin + eb->size) {
433 return 0; // Not enough space.
435 return 1; // Buf has enough space for the event.
439 static void postEventType(EventsBuf *eb, EventType *et)
444 postInt32(eb, EVENT_ET_BEGIN);
445 postEventTypeNum(eb, et->etNum);
446 postWord16(eb, (StgWord16)et->size);
447 desclen = strlen(et->desc);
448 postWord32(eb, desclen);
449 for (d = 0; d < desclen; ++d) {
450 postInt8(eb, (StgInt8)et->desc[d]);
452 postWord32(eb, 0); // no extensions yet
453 postInt32(eb, EVENT_ET_END);
456 #endif /* EVENTLOG */