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 {
50 "Request sequential GC",
51 "Request parallel GC",
53 "Spark to thread", /* DEPRECATED! */
59 typedef struct _EventType {
60 EventTypeNum etNum; // Event Type number.
61 nat size; // size of the payload in bytes
62 char *desc; // Description
65 EventType eventTypes[NUM_EVENT_TAGS];
67 static void printAndClearEventBuf (EventsBuf *eventsBuf);
68 static void initEventsBuf(EventsBuf* eb, StgWord64 size);
69 static void resetEventsBuf(EventsBuf* eb);
71 static void beginHeader(EventsBuf *eb);
72 static void endHeader(EventsBuf *eb);
74 static void beginData(EventsBuf *eb);
75 static void endData(EventsBuf *eb);
77 static void beginEventTypes(EventsBuf *eb);
78 static void endEventTypes(EventsBuf *eb);
80 static void postEventType(EventsBuf *eb, EventType *et);
82 static StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum);
84 static inline void postWord8(EventsBuf *eb, StgWord8 i)
89 static inline void postWord16(EventsBuf *eb, StgWord16 i)
91 postWord8(eb, (StgWord8)(i >> 8));
92 postWord8(eb, (StgWord8)i);
95 static inline void postWord32(EventsBuf *eb, StgWord32 i)
97 postWord16(eb, (StgWord16)(i >> 16));
98 postWord16(eb, (StgWord16)i);
101 static inline void postWord64(EventsBuf *eb, StgWord64 i)
103 postWord32(eb, (StgWord32)(i >> 32));
104 postWord32(eb, (StgWord32)i);
107 static inline void postEventTypeNum(EventsBuf *eb, EventTypeNum etNum)
108 { postWord16(eb, etNum); }
110 static inline void postTimestamp(EventsBuf *eb, EventTimestamp t)
111 { postWord64(eb,t); }
113 static inline void postThreadID(EventsBuf *eb, EventThreadID id)
114 { postWord32(eb,id); }
116 static inline void postCapNo(EventsBuf *eb, EventCapNo no)
117 { postWord16(eb,no); }
119 static inline void postInt8(EventsBuf *eb, StgInt8 i)
120 { postWord8(eb, (StgWord8)i); }
122 static inline void postInt16(EventsBuf *eb, StgInt16 i)
123 { postWord16(eb, (StgWord16)i); }
125 static inline void postInt32(EventsBuf *eb, StgInt32 i)
126 { postWord32(eb, (StgWord32)i); }
128 static inline void postInt64(EventsBuf *eb, StgInt64 i)
129 { postWord64(eb, (StgWord64)i); }
133 initEventLogging(void)
137 debugTrace(DEBUG_eventlog, "intiEventLog: start");
139 event_log_filename = stgMallocBytes(strlen(prog_name) + 9,
142 if (sizeof(EventDesc) / sizeof(char*) != NUM_EVENT_TAGS) {
143 barf("EventDesc array has the wrong number of elements");
146 sprintf(event_log_filename, "%s.eventlog", prog_name);
148 /* Open event log file for writing. */
149 if ((event_log_file = fopen(event_log_filename, "wb")) == NULL) {
150 sysErrorBelch("initEventLoggin: can't open %s", event_log_filename);
151 stg_exit(EXIT_FAILURE);
155 * Allocate buffer(s) to store events.
156 * Create buffer large enough for the header begin marker, all event
157 * types, and header end marker to prevent checking if buffer has room
158 * for each of these steps, and remove the need to flush the buffer to
159 * disk during initialization.
161 * Use a single buffer to store the header with event types, then flush
162 * the buffer so all buffers are empty for writing events.
164 eventsBuf = stgMallocBytes(n_capabilities * sizeof(EventsBuf),"initEventLogging");
166 for (c = 0; c < n_capabilities; ++c) {
167 // Init buffer for events.
168 initEventsBuf(&eventsBuf[c], EVENT_LOG_SIZE);
171 // Write in buffer: the header begin marker.
172 beginHeader(&eventsBuf[0]);
174 // Mark beginning of event types in the header.
175 beginEventTypes(&eventsBuf[0]);
176 for (t = 0; t < NUM_EVENT_TAGS; ++t) {
178 eventTypes[t].etNum = t;
179 eventTypes[t].desc = EventDesc[t];
182 case EVENT_CREATE_THREAD: // (cap, thread)
183 case EVENT_RUN_THREAD: // (cap, thread)
184 case EVENT_THREAD_RUNNABLE: // (cap, thread)
185 case EVENT_CREATE_SPARK: // (cap, thread)
186 case EVENT_RUN_SPARK: // (cap, thread)
187 case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
188 eventTypes[t].size = sizeof(EventCapNo) + sizeof(EventThreadID);
191 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
192 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
193 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
195 sizeof(EventCapNo) + sizeof(EventThreadID) + sizeof(EventCapNo);
198 case EVENT_STOP_THREAD: // (cap, thread, status)
200 sizeof(EventCapNo) + sizeof(EventThreadID) + sizeof(StgWord16);
203 case EVENT_SHUTDOWN: // (cap)
204 case EVENT_REQUEST_SEQ_GC: // (cap)
205 case EVENT_REQUEST_PAR_GC: // (cap)
206 case EVENT_GC_START: // (cap)
207 case EVENT_GC_END: // (cap)
208 eventTypes[t].size = sizeof(EventCapNo);
212 // Write in buffer: the start event type.
213 postEventType(&eventsBuf[0], &eventTypes[t]);
216 // Mark end of event types in the header.
217 endEventTypes(&eventsBuf[0]);
219 // Write in buffer: the header end marker.
220 endHeader(&eventsBuf[0]);
222 // Prepare event buffer for events (data).
223 beginData(&eventsBuf[0]);
225 // Flush eventsBuf with header.
227 * Flush header and data begin marker to the file, thus preparing the
228 * file to have events written to it.
230 printAndClearEventBuf(&eventsBuf[0]);
232 debugTrace(DEBUG_eventlog, "initEventLog: finish");
236 endEventLogging(void)
240 debugTrace(DEBUG_eventlog,"endEventLog: start");
242 // Flush all events remaining in the buffers.
243 for (c = 0; c < n_capabilities; ++c) {
244 printAndClearEventBuf(&eventsBuf[c]);
247 // Mark end of events (data).
248 endData(&eventsBuf[0]);
250 // Flush the end of data marker.
251 printAndClearEventBuf(&eventsBuf[0]);
253 if (event_log_file != NULL) {
254 fclose(event_log_file);
257 debugTrace(DEBUG_eventlog,"endEventLog: finish");
261 freeEventLogging(void)
265 debugTrace(DEBUG_eventlog,"freeEventLog: start");
267 // Free events buffer.
268 for (c = 0; c < n_capabilities; ++c) {
269 if (eventsBuf[c].begin != NULL)
270 stgFree(eventsBuf[c].begin);
272 if (eventsBuf != NULL) {
275 if (event_log_filename != NULL) {
276 stgFree(event_log_filename);
279 debugTrace(DEBUG_eventlog,"freeEventLog: finish");
283 * Post an event message to the capability's eventlog buffer.
284 * If the buffer is full, prints out the buffer and clears it.
287 postEvent_(Capability *cap, EventTypeNum tag, StgThreadID thread, StgWord64 other)
291 debugTrace(DEBUG_eventlog,"postEvent: start");
293 eb = &eventsBuf[cap->no];
295 if (!hasRoomForEvent(eb, tag)) {
296 // Flush event buffer to make room for new event.
297 printAndClearEventBuf(eb);
300 postEventTypeNum(eb, tag);
301 postTimestamp(eb, stat_getElapsedTime() * (1000000000LL/TICKS_PER_SECOND));
302 postCapNo(eb, cap->no);
305 case EVENT_CREATE_THREAD: // (cap, thread)
306 case EVENT_RUN_THREAD: // (cap, thread)
307 case EVENT_THREAD_RUNNABLE: // (cap, thread)
308 case EVENT_CREATE_SPARK: // (cap, thread)
309 case EVENT_RUN_SPARK: // (cap, thread)
311 postThreadID(eb,thread);
315 case EVENT_CREATE_SPARK_THREAD: // (cap, spark_thread)
317 postThreadID(eb,other /* spark_thread */);
321 case EVENT_MIGRATE_THREAD: // (cap, thread, new_cap)
322 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
323 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
325 postThreadID(eb,thread);
326 postCapNo(eb,other /* new_cap | victim_cap | other_cap */);
330 case EVENT_STOP_THREAD: // (cap, thread, status)
332 postThreadID(eb,thread);
333 postWord16(eb,other /* status */);
337 case EVENT_SHUTDOWN: // (cap)
338 case EVENT_REQUEST_SEQ_GC: // (cap)
339 case EVENT_REQUEST_PAR_GC: // (cap)
340 case EVENT_GC_START: // (cap)
341 case EVENT_GC_END: // (cap)
347 barf("postEvent: unknown event tag %d", tag);
350 debugTrace(DEBUG_eventlog,"postEvent: finish");
353 static void printAndClearEventBuf (EventsBuf *eventsBuf)
355 StgWord64 numBytes = 0, written = 0;
357 if (eventsBuf->begin != NULL && eventsBuf->pos != eventsBuf->begin)
359 numBytes = eventsBuf->pos - eventsBuf->begin;
361 debugTrace(DEBUG_eventlog,
362 "printAndEventLog: numbytes %" FMT_Word64
363 " bytes to fwrite()",
366 written = fwrite(eventsBuf->begin, 1, numBytes, event_log_file);
367 if (written != numBytes) {
369 "printAndClearEventLog: fwrite() failed, written=%" FMT_Word64
370 " doesn't match numBytes=%" FMT_Word64, written, numBytes);
374 debugTrace(DEBUG_eventlog,
375 "printAndClearEventLog: fwrite(): %" FMT_Word64
376 " bytes written", written);
378 resetEventsBuf(eventsBuf);
384 printAndClearEventLog(Capability *cap)
386 debugTrace(DEBUG_eventlog,"printAndClearEventLog: start");
388 printAndClearEventBuf(&eventsBuf[cap->no]);
390 debugTrace(DEBUG_eventlog,"printAndClearEventLog: finish");
393 /* -----------------------------------------------------------------------------
394 Actual event generation below here
395 -------------------------------------------------------------------------- */
397 void initEventsBuf(EventsBuf* eb, StgWord64 size)
399 eb->begin = eb->pos = malloc(size);
403 void resetEventsBuf(EventsBuf* eb)
408 // N.B.: Assuming buffer contains enough space for the header begin marker.
409 void beginHeader(EventsBuf *eb)
411 postInt32(eb, EVENT_HEADER_BEGIN);
414 // N.B.: Assuming buffer contains enough space for the header end marker.
415 void endHeader(EventsBuf *eb)
417 postInt32(eb, EVENT_HEADER_END);
420 void beginData(EventsBuf *eb)
422 postInt32(eb, EVENT_DATA_BEGIN);
425 void endData(EventsBuf *eb)
427 postEventTypeNum(eb, EVENT_DATA_END);
430 void beginEventTypes(EventsBuf *eb)
432 postInt32(eb, EVENT_HET_BEGIN);
435 void endEventTypes(EventsBuf *eb)
437 postInt32(eb, EVENT_HET_END);
440 StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum)
444 size += sizeof(EventTypeNum) + sizeof(EventTimestamp) + eventTypes[eNum].size;
446 if (eb->pos + size > eb->begin + eb->size) {
447 return 0; // Not enough space.
449 return 1; // Buf has enough space for the event.
453 static void postEventType(EventsBuf *eb, EventType *et)
458 postInt32(eb, EVENT_ET_BEGIN);
459 postEventTypeNum(eb, et->etNum);
460 postWord16(eb, (StgWord16)et->size);
461 desclen = strlen(et->desc);
462 postWord32(eb, desclen);
463 for (d = 0; d < desclen; ++d) {
464 postInt8(eb, (StgInt8)et->desc[d]);
466 postWord32(eb, 0); // no extensions yet
467 postInt32(eb, EVENT_ET_END);
470 #endif /* EVENTLOG */