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",
57 typedef struct _EventType {
58 EventTypeNum etNum; // Event Type number.
59 nat size; // size of the payload in bytes
60 char *desc; // Description
63 EventType eventTypes[NUM_EVENT_TAGS];
65 static void printAndClearEventBuf (EventsBuf *eventsBuf);
66 static void initEventsBuf(EventsBuf* eb, StgWord64 size);
67 static void resetEventsBuf(EventsBuf* eb);
69 static void beginHeader(EventsBuf *eb);
70 static void endHeader(EventsBuf *eb);
72 static void beginData(EventsBuf *eb);
73 static void endData(EventsBuf *eb);
75 static void beginEventTypes(EventsBuf *eb);
76 static void endEventTypes(EventsBuf *eb);
78 static void postEventType(EventsBuf *eb, EventType *et);
80 static StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum);
82 static inline void postWord8(EventsBuf *eb, StgWord8 i)
87 static inline void postWord16(EventsBuf *eb, StgWord16 i)
89 postWord8(eb, (StgWord8)(i >> 8));
90 postWord8(eb, (StgWord8)i);
93 static inline void postWord32(EventsBuf *eb, StgWord32 i)
95 postWord16(eb, (StgWord16)(i >> 16));
96 postWord16(eb, (StgWord16)i);
99 static inline void postWord64(EventsBuf *eb, StgWord64 i)
101 postWord32(eb, (StgWord32)(i >> 32));
102 postWord32(eb, (StgWord32)i);
105 static inline void postEventTypeNum(EventsBuf *eb, EventTypeNum etNum)
106 { postWord16(eb, etNum); }
108 static inline void postEventTypeID(EventsBuf *eb, StgWord16 etID)
109 { postWord16(eb, etID); }
111 static inline void postTimestamp(EventsBuf *eb, Timestamp t)
112 { postWord64(eb,t); }
114 static inline void postInt8(EventsBuf *eb, StgInt8 i)
115 { postWord8(eb, (StgWord8)i); }
117 static inline void postInt16(EventsBuf *eb, StgInt16 i)
118 { postWord16(eb, (StgWord16)i); }
120 static inline void postInt32(EventsBuf *eb, StgInt32 i)
121 { postWord32(eb, (StgWord32)i); }
123 static inline void postInt64(EventsBuf *eb, StgInt64 i)
124 { postWord64(eb, (StgWord64)i); }
128 initEventLogging(void)
132 debugTrace(DEBUG_eventlog, "intiEventLog: start");
134 event_log_filename = stgMallocBytes(strlen(prog_name) + 9,
137 if (sizeof(EventDesc) / sizeof(char*) != NUM_EVENT_TAGS) {
138 barf("EventDesc array has the wrong number of elements");
141 sprintf(event_log_filename, "%s.eventlog", prog_name);
143 /* Open event log file for writing. */
144 if ((event_log_file = fopen(event_log_filename, "wb")) == NULL) {
145 sysErrorBelch("initEventLoggin: can't open %s", event_log_filename);
146 stg_exit(EXIT_FAILURE);
150 * Allocate buffer(s) to store events.
151 * Create buffer large enough for the header begin marker, all event
152 * types, and header end marker to prevent checking if buffer has room
153 * for each of these steps, and remove the need to flush the buffer to
154 * disk during initialization.
156 * Use a single buffer to store the header with event types, then flush
157 * the buffer so all buffers are empty for writing events.
159 eventsBuf = stgMallocBytes(n_capabilities * sizeof(EventsBuf),"initEventLogging");
161 for (c = 0; c < n_capabilities; ++c) {
162 // Init buffer for events.
163 initEventsBuf(&eventsBuf[c], EVENT_LOG_SIZE);
166 // Write in buffer: the header begin marker.
167 beginHeader(&eventsBuf[0]);
169 // Mark beginning of event types in the header.
170 beginEventTypes(&eventsBuf[0]);
171 for (t = 0; t < NUM_EVENT_TAGS; ++t) {
173 eventTypes[t].etNum = t;
174 eventTypes[t].desc = EventDesc[t];
177 case EVENT_CREATE_THREAD: // (cap, thread)
178 case EVENT_RUN_THREAD: // (cap, thread)
179 case EVENT_THREAD_RUNNABLE: // (cap, thread)
180 case EVENT_CREATE_SPARK: // (cap, thread)
181 case EVENT_RUN_SPARK: // (cap, thread)
182 eventTypes[t].size = sizeof(CapabilityNum) + sizeof(ThreadID);
185 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
186 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
187 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
189 sizeof(CapabilityNum) + sizeof(ThreadID) +
190 sizeof(CapabilityNum);
193 case EVENT_STOP_THREAD: // (cap, thread, status)
195 sizeof(CapabilityNum) + sizeof(ThreadID) + sizeof(StgWord16);
198 case EVENT_SHUTDOWN: // (cap)
199 case EVENT_REQUEST_SEQ_GC: // (cap)
200 case EVENT_REQUEST_PAR_GC: // (cap)
201 case EVENT_GC_START: // (cap)
202 case EVENT_GC_END: // (cap)
203 eventTypes[t].size = sizeof(CapabilityNum);
207 // Write in buffer: the start event type.
208 postEventType(&eventsBuf[0], &eventTypes[t]);
211 // Mark end of event types in the header.
212 endEventTypes(&eventsBuf[0]);
214 // Write in buffer: the header end marker.
215 endHeader(&eventsBuf[0]);
217 // Prepare event buffer for events (data).
218 beginData(&eventsBuf[0]);
220 // Flush eventsBuf with header.
222 * Flush header and data begin marker to the file, thus preparing the
223 * file to have events written to it.
225 printAndClearEventBuf(&eventsBuf[0]);
227 debugTrace(DEBUG_eventlog, "initEventLog: finish");
231 endEventLogging(void)
235 debugTrace(DEBUG_eventlog,"endEventLog: start");
237 // Flush all events remaining in the buffers.
238 for (c = 0; c < n_capabilities; ++c) {
239 printAndClearEventBuf(&eventsBuf[c]);
242 // Mark end of events (data).
243 endData(&eventsBuf[0]);
245 // Flush the end of data marker.
246 printAndClearEventBuf(&eventsBuf[0]);
248 if (event_log_file != NULL) {
249 fclose(event_log_file);
252 debugTrace(DEBUG_eventlog,"endEventLog: finish");
256 freeEventLogging(void)
260 debugTrace(DEBUG_eventlog,"freeEventLog: start");
262 // Free events buffer.
263 for (c = 0; c < n_capabilities; ++c) {
264 if (eventsBuf[c].begin != NULL)
265 stgFree(eventsBuf[c].begin);
267 if (eventsBuf != NULL) {
270 if (event_log_filename != NULL) {
271 stgFree(event_log_filename);
274 debugTrace(DEBUG_eventlog,"freeEventLog: finish");
278 * Post an event message to the capability's eventlog buffer.
279 * If the buffer is full, prints out the buffer and clears it.
282 postEvent_(Capability *cap, EventTypeNum tag, StgThreadID thread, nat other_cap)
286 debugTrace(DEBUG_eventlog,"postEvent: start");
288 eb = &eventsBuf[cap->no];
290 if (!hasRoomForEvent(eb, tag)) {
291 // Flush event buffer to make room for new event.
292 printAndClearEventBuf(eb);
295 postEventTypeNum(eb, tag);
296 postTimestamp(eb, stat_getElapsedTime() * (1000000000LL/TICKS_PER_SECOND));
297 postWord16(eb, cap->no);
300 case EVENT_CREATE_THREAD: // (cap, thread)
301 case EVENT_RUN_THREAD: // (cap, thread)
302 case EVENT_THREAD_RUNNABLE: // (cap, thread)
303 case EVENT_CREATE_SPARK: // (cap, thread)
304 case EVENT_RUN_SPARK: // (cap, thread)
306 postWord64(eb,thread);
310 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
311 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
312 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
314 postWord64(eb,thread);
315 postWord16(eb,other_cap);
319 case EVENT_STOP_THREAD: // (cap, thread, status)
321 postWord64(eb,thread);
322 postWord16(eb,other_cap);
326 case EVENT_SHUTDOWN: // (cap)
327 case EVENT_REQUEST_SEQ_GC: // (cap)
328 case EVENT_REQUEST_PAR_GC: // (cap)
329 case EVENT_GC_START: // (cap)
330 case EVENT_GC_END: // (cap)
336 barf("postEvent: unknown event tag %d", tag);
339 debugTrace(DEBUG_eventlog,"postEvent: finish");
342 static void printAndClearEventBuf (EventsBuf *eventsBuf)
344 StgWord64 numBytes = 0, written = 0;
346 if (eventsBuf->begin != NULL && eventsBuf->pos != eventsBuf->begin)
348 numBytes = eventsBuf->pos - eventsBuf->begin;
350 debugTrace(DEBUG_eventlog,
351 "printAndEventLog: numbytes %" FMT_Word64
352 " bytes to fwrite()",
355 written = fwrite(eventsBuf->begin, 1, numBytes, event_log_file);
356 if (written != numBytes) {
358 "printAndClearEventLog: fwrite() failed, written=%" FMT_Word64
359 " doesn't match numBytes=%" FMT_Word64, written, numBytes);
363 debugTrace(DEBUG_eventlog,
364 "printAndClearEventLog: fwrite(): %" FMT_Word64
365 " bytes written", written);
367 resetEventsBuf(eventsBuf);
373 printAndClearEventLog(Capability *cap)
375 debugTrace(DEBUG_eventlog,"printAndClearEventLog: start");
377 printAndClearEventBuf(&eventsBuf[cap->no]);
379 debugTrace(DEBUG_eventlog,"printAndClearEventLog: finish");
382 /* -----------------------------------------------------------------------------
383 Actual event generation below here
384 -------------------------------------------------------------------------- */
386 void initEventsBuf(EventsBuf* eb, StgWord64 size)
388 eb->begin = eb->pos = malloc(size);
392 void resetEventsBuf(EventsBuf* eb)
397 // N.B.: Assuming buffer contains enough space for the header begin marker.
398 void beginHeader(EventsBuf *eb)
400 postInt32(eb, EVENT_HEADER_BEGIN);
403 // N.B.: Assuming buffer contains enough space for the header end marker.
404 void endHeader(EventsBuf *eb)
406 postInt32(eb, EVENT_HEADER_END);
409 void beginData(EventsBuf *eb)
411 postInt32(eb, EVENT_DATA_BEGIN);
414 void endData(EventsBuf *eb)
416 postEventTypeNum(eb, EVENT_DATA_END);
419 void beginEventTypes(EventsBuf *eb)
421 postInt32(eb, EVENT_HET_BEGIN);
424 void endEventTypes(EventsBuf *eb)
426 postInt32(eb, EVENT_HET_END);
429 StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum)
433 size += sizeof(EventTypeNum) + sizeof(Timestamp) + eventTypes[eNum].size;
435 if (eb->pos + size > eb->begin + eb->size) {
436 return 0; // Not enough space.
438 return 1; // Buf has enough space for the event.
442 static void postEventType(EventsBuf *eb, EventType *et)
447 postInt32(eb, EVENT_ET_BEGIN);
448 postEventTypeNum(eb, et->etNum);
449 postWord16(eb, (StgWord16)et->size);
450 desclen = strlen(et->desc);
451 postWord32(eb, desclen);
452 for (d = 0; d < desclen; ++d) {
453 postInt8(eb, (StgInt8)et->desc[d]);
455 postWord32(eb, 0); // no extensions yet
456 postInt32(eb, EVENT_ET_END);
459 #endif /* EVENTLOG */