EventLog: Factor out ensureRoomFor*Event
authorBen Gamari <ben@smart-cactus.org>
Mon, 31 Aug 2015 20:00:10 +0000 (22:00 +0200)
committerBen Gamari <ben@smart-cactus.org>
Sat, 5 Sep 2015 08:54:44 +0000 (10:54 +0200)
rts/eventlog/EventLog.c

index 5f021a6..c55ac2c 100644 (file)
@@ -133,6 +133,9 @@ static void closeBlockMarker(EventsBuf *ebuf);
 static StgBool hasRoomForEvent(EventsBuf *eb, EventTypeNum eNum);
 static StgBool hasRoomForVariableEvent(EventsBuf *eb, nat payload_bytes);
 
+static void ensureRoomForEvent(EventsBuf *eb, EventTypeNum tag);
+static int ensureRoomForVariableEvent(EventsBuf *eb, StgWord16 size);
+
 static inline void postWord8(EventsBuf *eb, StgWord8 i)
 {
     *(eb->pos++) = i;
@@ -546,6 +549,7 @@ abortEventLogging(void)
         fclose(event_log_file);
     }
 }
+
 /*
  * Post an event message to the capability's eventlog buffer.
  * If the buffer is full, prints out the buffer and clears it.
@@ -560,11 +564,7 @@ postSchedEvent (Capability *cap,
     EventsBuf *eb;
 
     eb = &capEventBuf[cap->no];
-
-    if (!hasRoomForEvent(eb, tag)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(eb);
-    }
+    ensureRoomForEvent(eb, tag);
 
     postEventHeader(eb, tag);
 
@@ -612,11 +612,7 @@ postSparkEvent (Capability *cap,
     EventsBuf *eb;
 
     eb = &capEventBuf[cap->no];
-
-    if (!hasRoomForEvent(eb, tag)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(eb);
-    }
+    ensureRoomForEvent(eb, tag);
 
     postEventHeader(eb, tag);
 
@@ -656,11 +652,7 @@ postSparkCountersEvent (Capability *cap,
     EventsBuf *eb;
 
     eb = &capEventBuf[cap->no];
-
-    if (!hasRoomForEvent(eb, EVENT_SPARK_COUNTERS)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(eb);
-    }
+    ensureRoomForEvent(eb, EVENT_SPARK_COUNTERS);
 
     postEventHeader(eb, EVENT_SPARK_COUNTERS);
     /* EVENT_SPARK_COUNTERS (crt,dud,ovf,cnv,gcd,fiz,rem) */
@@ -678,11 +670,7 @@ postCapEvent (EventTypeNum  tag,
               EventCapNo    capno)
 {
     ACQUIRE_LOCK(&eventBufMutex);
-
-    if (!hasRoomForEvent(&eventBuf, tag)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(&eventBuf);
-    }
+    ensureRoomForEvent(&eventBuf, tag);
 
     postEventHeader(&eventBuf, tag);
 
@@ -708,11 +696,7 @@ void postCapsetEvent (EventTypeNum tag,
                       StgWord info)
 {
     ACQUIRE_LOCK(&eventBufMutex);
-
-    if (!hasRoomForEvent(&eventBuf, tag)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(&eventBuf);
-    }
+    ensureRoomForEvent(&eventBuf, tag);
 
     postEventHeader(&eventBuf, tag);
     postCapsetID(&eventBuf, capset);
@@ -838,11 +822,7 @@ void postWallClockTime (EventCapsetID capset)
 
     getUnixEpochTime(&sec, &nsec);  /* Get the wall clock time */
     ts = time_ns();                 /* Get the eventlog timestamp */
-
-    if (!hasRoomForEvent(&eventBuf, EVENT_WALL_CLOCK_TIME)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(&eventBuf);
-    }
+    ensureRoomForEvent(&eventBuf, EVENT_WALL_CLOCK_TIME);
 
     /* Normally we'd call postEventHeader(), but that generates its own
        timestamp, so we go one level lower so we can write out the
@@ -869,11 +849,7 @@ void postHeapEvent (Capability    *cap,
     EventsBuf *eb;
 
     eb = &capEventBuf[cap->no];
-
-    if (!hasRoomForEvent(eb, tag)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(eb);
-    }
+    ensureRoomForEvent(eb, tag);
 
     postEventHeader(eb, tag);
 
@@ -900,11 +876,7 @@ void postEventHeapInfo (EventCapsetID heap_capset,
                         W_          blockSize)
 {
     ACQUIRE_LOCK(&eventBufMutex);
-
-    if (!hasRoomForEvent(&eventBuf, EVENT_HEAP_INFO_GHC)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(&eventBuf);
-    }
+    ensureRoomForEvent(&eventBuf, EVENT_HEAP_INFO_GHC);
 
     postEventHeader(&eventBuf, EVENT_HEAP_INFO_GHC);
     /* EVENT_HEAP_INFO_GHC (heap_capset, n_generations,
@@ -933,11 +905,7 @@ void postEventGcStats  (Capability    *cap,
     EventsBuf *eb;
 
     eb = &capEventBuf[cap->no];
-
-    if (!hasRoomForEvent(eb, EVENT_GC_STATS_GHC)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(eb);
-    }
+    ensureRoomForEvent(eb, EVENT_GC_STATS_GHC);
 
     postEventHeader(eb, EVENT_GC_STATS_GHC);
     /* EVENT_GC_STATS_GHC (heap_capset, generation,
@@ -958,11 +926,7 @@ void postTaskCreateEvent (EventTaskId taskId,
                           EventKernelThreadId tid)
 {
     ACQUIRE_LOCK(&eventBufMutex);
-
-    if (!hasRoomForEvent(&eventBuf, EVENT_TASK_CREATE)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(&eventBuf);
-    }
+    ensureRoomForEvent(&eventBuf, EVENT_TASK_CREATE);
 
     postEventHeader(&eventBuf, EVENT_TASK_CREATE);
     /* EVENT_TASK_CREATE (taskID, cap, tid) */
@@ -978,11 +942,7 @@ void postTaskMigrateEvent (EventTaskId taskId,
                            EventCapNo new_capno)
 {
     ACQUIRE_LOCK(&eventBufMutex);
-
-    if (!hasRoomForEvent(&eventBuf, EVENT_TASK_MIGRATE)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(&eventBuf);
-    }
+    ensureRoomForEvent(&eventBuf, EVENT_TASK_MIGRATE);
 
     postEventHeader(&eventBuf, EVENT_TASK_MIGRATE);
     /* EVENT_TASK_MIGRATE (taskID, cap, new_cap) */
@@ -996,11 +956,7 @@ void postTaskMigrateEvent (EventTaskId taskId,
 void postTaskDeleteEvent (EventTaskId taskId)
 {
     ACQUIRE_LOCK(&eventBufMutex);
-
-    if (!hasRoomForEvent(&eventBuf, EVENT_TASK_DELETE)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(&eventBuf);
-    }
+    ensureRoomForEvent(&eventBuf, EVENT_TASK_DELETE);
 
     postEventHeader(&eventBuf, EVENT_TASK_DELETE);
     /* EVENT_TASK_DELETE (taskID) */
@@ -1015,12 +971,7 @@ postEvent (Capability *cap, EventTypeNum tag)
     EventsBuf *eb;
 
     eb = &capEventBuf[cap->no];
-
-    if (!hasRoomForEvent(eb, tag)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(eb);
-    }
-
+    ensureRoomForEvent(eb, tag);
     postEventHeader(eb, tag);
 }
 
@@ -1030,11 +981,7 @@ postEventAtTimestamp (Capability *cap, EventTimestamp ts, EventTypeNum tag)
     EventsBuf *eb;
 
     eb = &capEventBuf[cap->no];
-
-    if (!hasRoomForEvent(eb, tag)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(eb);
-    }
+    ensureRoomForEvent(eb, tag);
 
     /* Normally we'd call postEventHeader(), but that generates its own
        timestamp, so we go one level lower so we can write out
@@ -1056,10 +1003,7 @@ void postLogMsg(EventsBuf *eb, EventTypeNum type, char *msg, va_list ap)
         size = BUF;
     }
 
-    if (!hasRoomForVariableEvent(eb, size)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(eb);
-    }
+    ensureRoomForVariableEvent(eb, size);
 
     postEventHeader(eb, type);
     postPayloadSize(eb, size);
@@ -1081,11 +1025,7 @@ void postCapMsg(Capability *cap, char *msg, va_list ap)
 void postEventStartup(EventCapNo n_caps)
 {
     ACQUIRE_LOCK(&eventBufMutex);
-
-    if (!hasRoomForEvent(&eventBuf, EVENT_STARTUP)) {
-        // Flush event buffer to make room for new event.
-        printAndClearEventBuf(&eventBuf);
-    }
+    ensureRoomForEvent(&eventBuf, EVENT_STARTUP);
 
     // Post a STARTUP event with the number of capabilities
     postEventHeader(&eventBuf, EVENT_STARTUP);
@@ -1161,9 +1101,7 @@ void closeBlockMarker (EventsBuf *ebuf)
 
 void postBlockMarker (EventsBuf *eb)
 {
-    if (!hasRoomForEvent(eb, EVENT_BLOCK_MARKER)) {
-        printAndClearEventBuf(eb);
-    }
+    ensureRoomForEvent(eb, EVENT_BLOCK_MARKER);
 
     closeBlockMarker(eb);
 
@@ -1240,6 +1178,26 @@ StgBool hasRoomForVariableEvent(EventsBuf *eb, nat payload_bytes)
   }
 }
 
+void ensureRoomForEvent(EventsBuf *eb, EventTypeNum tag)
+{
+    if (!hasRoomForEvent(eb, tag)) {
+        // Flush event buffer to make room for new event.
+        printAndClearEventBuf(eb);
+    }
+}
+
+int ensureRoomForVariableEvent(EventsBuf *eb, StgWord16 size)
+{
+    if (!hasRoomForVariableEvent(eb, size)) {
+        // Flush event buffer to make room for new event.
+        printAndClearEventBuf(eb);
+        if (!hasRoomForVariableEvent(eb, size))
+            return 1; // Not enough space
+    }
+    return 0;
+}
+
+
 void postEventType(EventsBuf *eb, EventType *et)
 {
     StgWord8 d;