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",
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 postThreadID(EventsBuf *eb, ThreadID id)
115 { postWord32(eb,id); }
117 static inline void postCapNo(EventsBuf *eb, CapNo no)
118 { postWord16(eb,no); }
120 static inline void postInt8(EventsBuf *eb, StgInt8 i)
121 { postWord8(eb, (StgWord8)i); }
123 static inline void postInt16(EventsBuf *eb, StgInt16 i)
124 { postWord16(eb, (StgWord16)i); }
126 static inline void postInt32(EventsBuf *eb, StgInt32 i)
127 { postWord32(eb, (StgWord32)i); }
129 static inline void postInt64(EventsBuf *eb, StgInt64 i)
130 { postWord64(eb, (StgWord64)i); }
134 initEventLogging(void)
138 debugTrace(DEBUG_eventlog, "intiEventLog: start");
140 event_log_filename = stgMallocBytes(strlen(prog_name) + 9,
143 if (sizeof(EventDesc) / sizeof(char*) != NUM_EVENT_TAGS) {
144 barf("EventDesc array has the wrong number of elements");
147 sprintf(event_log_filename, "%s.eventlog", prog_name);
149 /* Open event log file for writing. */
150 if ((event_log_file = fopen(event_log_filename, "wb")) == NULL) {
151 sysErrorBelch("initEventLoggin: can't open %s", event_log_filename);
152 stg_exit(EXIT_FAILURE);
156 * Allocate buffer(s) to store events.
157 * Create buffer large enough for the header begin marker, all event
158 * types, and header end marker to prevent checking if buffer has room
159 * for each of these steps, and remove the need to flush the buffer to
160 * disk during initialization.
162 * Use a single buffer to store the header with event types, then flush
163 * the buffer so all buffers are empty for writing events.
165 eventsBuf = stgMallocBytes(n_capabilities * sizeof(EventsBuf),"initEventLogging");
167 for (c = 0; c < n_capabilities; ++c) {
168 // Init buffer for events.
169 initEventsBuf(&eventsBuf[c], EVENT_LOG_SIZE);
172 // Write in buffer: the header begin marker.
173 beginHeader(&eventsBuf[0]);
175 // Mark beginning of event types in the header.
176 beginEventTypes(&eventsBuf[0]);
177 for (t = 0; t < NUM_EVENT_TAGS; ++t) {
179 eventTypes[t].etNum = t;
180 eventTypes[t].desc = EventDesc[t];
183 case EVENT_CREATE_THREAD: // (cap, thread)
184 case EVENT_RUN_THREAD: // (cap, thread)
185 case EVENT_THREAD_RUNNABLE: // (cap, thread)
186 case EVENT_CREATE_SPARK: // (cap, thread)
187 case EVENT_RUN_SPARK: // (cap, thread)
188 eventTypes[t].size = sizeof(CapNo) + sizeof(ThreadID);
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(CapNo) + sizeof(ThreadID) + sizeof(CapNo);
198 case EVENT_STOP_THREAD: // (cap, thread, status)
200 sizeof(CapNo) + sizeof(ThreadID) + 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(CapNo);
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_MIGRATE_THREAD: // (cap, thread, new_cap)
316 case EVENT_STEAL_SPARK: // (cap, thread, victim_cap)
317 case EVENT_THREAD_WAKEUP: // (cap, thread, other_cap)
319 postThreadID(eb,thread);
320 postCapNo(eb,other /* new_cap | victim_cap | other_cap */);
324 case EVENT_STOP_THREAD: // (cap, thread, status)
326 postThreadID(eb,thread);
327 postWord16(eb,other /* status */);
331 case EVENT_SHUTDOWN: // (cap)
332 case EVENT_REQUEST_SEQ_GC: // (cap)
333 case EVENT_REQUEST_PAR_GC: // (cap)
334 case EVENT_GC_START: // (cap)
335 case EVENT_GC_END: // (cap)
341 barf("postEvent: unknown event tag %d", tag);
344 debugTrace(DEBUG_eventlog,"postEvent: finish");
347 static void printAndClearEventBuf (EventsBuf *eventsBuf)
349 StgWord64 numBytes = 0, written = 0;
351 if (eventsBuf->begin != NULL && eventsBuf->pos != eventsBuf->begin)
353 numBytes = eventsBuf->pos - eventsBuf->begin;
355 debugTrace(DEBUG_eventlog,
356 "printAndEventLog: numbytes %" FMT_Word64
357 " bytes to fwrite()",
360 written = fwrite(eventsBuf->begin, 1, numBytes, event_log_file);
361 if (written != numBytes) {
363 "printAndClearEventLog: fwrite() failed, written=%" FMT_Word64
364 " doesn't match numBytes=%" FMT_Word64, written, numBytes);
368 debugTrace(DEBUG_eventlog,
369 "printAndClearEventLog: fwrite(): %" FMT_Word64
370 " bytes written", written);
372 resetEventsBuf(eventsBuf);
378 printAndClearEventLog(Capability *cap)
380 debugTrace(DEBUG_eventlog,"printAndClearEventLog: start");
382 printAndClearEventBuf(&eventsBuf[cap->no]);
384 debugTrace(DEBUG_eventlog,"printAndClearEventLog: finish");
387 /* -----------------------------------------------------------------------------
388 Actual event generation below here
389 -------------------------------------------------------------------------- */
391 void initEventsBuf(EventsBuf* eb, StgWord64 size)
393 eb->begin = eb->pos = malloc(size);
397 void resetEventsBuf(EventsBuf* eb)
402 // N.B.: Assuming buffer contains enough space for the header begin marker.
403 void beginHeader(EventsBuf *eb)
405 postInt32(eb, EVENT_HEADER_BEGIN);
408 // N.B.: Assuming buffer contains enough space for the header end marker.
409 void endHeader(EventsBuf *eb)
411 postInt32(eb, EVENT_HEADER_END);
414 void beginData(EventsBuf *eb)
416 postInt32(eb, EVENT_DATA_BEGIN);
419 void endData(EventsBuf *eb)
421 postEventTypeNum(eb, EVENT_DATA_END);
424 void beginEventTypes(EventsBuf *eb)
426 postInt32(eb, EVENT_HET_BEGIN);
429 void endEventTypes(EventsBuf *eb)
431 postInt32(eb, EVENT_HET_END);
434 StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum)
438 size += sizeof(EventTypeNum) + sizeof(Timestamp) + eventTypes[eNum].size;
440 if (eb->pos + size > eb->begin + eb->size) {
441 return 0; // Not enough space.
443 return 1; // Buf has enough space for the event.
447 static void postEventType(EventsBuf *eb, EventType *et)
452 postInt32(eb, EVENT_ET_BEGIN);
453 postEventTypeNum(eb, et->etNum);
454 postWord16(eb, (StgWord16)et->size);
455 desclen = strlen(et->desc);
456 postWord32(eb, desclen);
457 for (d = 0; d < desclen; ++d) {
458 postInt8(eb, (StgInt8)et->desc[d]);
460 postWord32(eb, 0); // no extensions yet
461 postInt32(eb, EVENT_ET_END);
464 #endif /* EVENTLOG */