Use snwprintf instead of swprintf in rts/Linker.c.
[ghc.git] / rts / Trace.h
index dcb0b00..31aefcb 100644 (file)
@@ -37,8 +37,9 @@ typedef StgWord16 CapsetType;
 enum CapsetType { CapsetTypeCustom = CAPSET_TYPE_CUSTOM,
                   CapsetTypeOsProcess = CAPSET_TYPE_OSPROCESS,
                   CapsetTypeClockdomain = CAPSET_TYPE_CLOCKDOMAIN };
-#define CAPSET_OSPROCESS_DEFAULT 0
-#define CAPSET_CLOCKDOMAIN_DEFAULT 1
+#define CAPSET_OSPROCESS_DEFAULT   ((CapsetID)0)
+#define CAPSET_HEAP_DEFAULT        ((CapsetID)0)   /* reusing the same capset */
+#define CAPSET_CLOCKDOMAIN_DEFAULT ((CapsetID)1)
 
 // -----------------------------------------------------------------------------
 // Message classes
@@ -113,6 +114,45 @@ void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
 void traceGcEvent_ (Capability *cap, EventTypeNum tag);
 
 /* 
+ * Record a GC event at the explicitly given timestamp
+ */
+#define traceGcEventAtT(cap, ts, tag)   \
+    if (RTS_UNLIKELY(TRACE_gc)) {       \
+        traceGcEventAtT_(cap, ts, tag); \
+    }
+
+void traceGcEventAtT_ (Capability *cap, StgWord64 ts, EventTypeNum tag);
+
+/* 
+ * Record a heap event
+ */
+#define traceHeapEvent(cap, tag, heap_capset, info1) \
+    if (RTS_UNLIKELY(TRACE_gc)) { \
+        traceHeapEvent_(cap, tag, heap_capset, info1);  \
+    }
+void traceHeapEvent_ (Capability   *cap,
+                      EventTypeNum  tag,
+                      CapsetID      heap_capset,
+                      W_          info1);
+
+void traceEventHeapInfo_ (CapsetID    heap_capset,
+                          nat         gens,
+                          W_        maxHeapSize,
+                          W_        allocAreaSize,
+                          W_        mblockSize,
+                          W_        blockSize);
+
+void traceEventGcStats_  (Capability *cap,
+                          CapsetID    heap_capset,
+                          nat         gen,
+                          W_        copied,
+                          W_        slop,
+                          W_        fragmentation,
+                          nat         par_n_threads,
+                          W_        par_max_copied,
+                          W_        par_tot_copied);
+
+/* 
  * Record a spark event
  */
 #define traceSparkEvent(cap, tag)         \
@@ -157,6 +197,12 @@ void trace_(char *msg, ...);
  */
 void traceUserMsg(Capability *cap, char *msg);
 
+/* 
+ * A marker event emitted by the program
+ * Used by Debug.Trace.{traceMarker, traceMarkerIO}
+ */
+void traceUserMarker(Capability *cap, char *msg);
+
 /*
  * An event to record a Haskell thread's label/name
  * Used by GHC.Conc.labelThread
@@ -222,11 +268,28 @@ void traceSparkCounters_ (Capability *cap,
                           SparkCounters counters,
                           StgWord remaining);
 
+void traceTaskCreate_ (Task       *task,
+                       Capability *cap);
+
+void traceTaskMigrate_ (Task       *task,
+                        Capability *cap,
+                        Capability *new_cap);
+
+void traceTaskDelete_ (Task       *task);
+
 #else /* !TRACING */
 
 #define traceSchedEvent(cap, tag, tso, other) /* nothing */
 #define traceSchedEvent2(cap, tag, tso, other, info) /* nothing */
 #define traceGcEvent(cap, tag) /* nothing */
+#define traceGcEventAtT(cap, ts, tag) /* nothing */
+#define traceEventGcStats_(cap, heap_capset, gen, \
+                           copied, slop, fragmentation, \
+                           par_n_threads, par_max_copied, par_tot_copied) /* nothing */
+#define traceHeapEvent(cap, tag, heap_capset, info1) /* nothing */
+#define traceEventHeapInfo_(heap_capset, gens, \
+                            maxHeapSize, allocAreaSize, \
+                            mblockSize, blockSize) /* nothing */
 #define traceSparkEvent(cap, tag) /* nothing */
 #define traceSparkEvent2(cap, tag, other) /* nothing */
 #define traceCap(class, cap, msg, ...) /* nothing */
@@ -241,6 +304,9 @@ INLINE_HEADER void traceEventStartup_ (int n_caps STG_UNUSED) {};
 #define traceWallClockTime_() /* nothing */
 #define traceOSProcessInfo_() /* nothing */
 #define traceSparkCounters_(cap, counters, remaining) /* nothing */
+#define traceTaskCreate_(taskID, cap) /* nothing */
+#define traceTaskMigrate_(taskID, cap, new_cap) /* nothing */
+#define traceTaskDelete_(taskID) /* nothing */
 
 #endif /* TRACING */
 
@@ -250,6 +316,7 @@ INLINE_HEADER void traceEventStartup_ (int n_caps STG_UNUSED) {};
 #if !defined(DEBUG) && !defined(TRACING) && defined(DTRACE)
 
 void dtraceUserMsgWrapper(Capability *cap, char *msg);
+void dtraceUserMarkerWrapper(Capability *cap, char *msg);
 
 #endif /* !defined(DEBUG) && !defined(TRACING) && defined(DTRACE) */
 
@@ -296,12 +363,40 @@ INLINE_HEADER void dtraceStartup (int num_caps) {
     HASKELLEVENT_CAP_DISABLE(cap)
 #define dtraceUserMsg(cap, msg)                         \
     HASKELLEVENT_USER_MSG(cap, msg)
+#define dtraceUserMarker(cap, msg)                      \
+    HASKELLEVENT_USER_MARKER(cap, msg)
 #define dtraceGcIdle(cap)                               \
     HASKELLEVENT_GC_IDLE(cap)
 #define dtraceGcWork(cap)                               \
     HASKELLEVENT_GC_WORK(cap)
 #define dtraceGcDone(cap)                               \
     HASKELLEVENT_GC_DONE(cap)
+#define dtraceGcGlobalSync(cap)                         \
+    HASKELLEVENT_GC_GLOBAL_SYNC(cap)
+#define dtraceEventGcStats(heap_capset, gens,           \
+                           copies, slop, fragmentation, \
+                           par_n_threads,               \
+                           par_max_copied,              \
+                           par_tot_copied)              \
+    HASKELLEVENT_GC_STATS(heap_capset, gens,            \
+                           copies, slop, fragmentation, \
+                           par_n_threads,               \
+                           par_max_copied,              \
+                           par_tot_copied)
+#define dtraceHeapInfo(heap_capset, gens,               \
+                       maxHeapSize, allocAreaSize,      \
+                       mblockSize, blockSize)           \
+    HASKELLEVENT_HEAP_INFO(heap_capset, gens,           \
+                           maxHeapSize, allocAreaSize,  \
+                           mblockSize, blockSize)
+#define dtraceEventHeapAllocated(cap, heap_capset,      \
+                                 allocated)             \
+    HASKELLEVENT_HEAP_ALLOCATED(cap, heap_capset,       \
+                                allocated)
+#define dtraceEventHeapSize(heap_capset, size)          \
+    HASKELLEVENT_HEAP_SIZE(heap_capset, size)
+#define dtraceEventHeapLive(heap_capset, live)          \
+    HASKELLEVENT_HEAP_LIVE(heap_capset, live)
 #define dtraceCapsetCreate(capset, capset_type)         \
     HASKELLEVENT_CAPSET_CREATE(capset, capset_type)
 #define dtraceCapsetDelete(capset)                      \
@@ -326,6 +421,12 @@ INLINE_HEADER void dtraceStartup (int num_caps) {
     HASKELLEVENT_SPARK_FIZZLE(cap)
 #define dtraceSparkGc(cap)                              \
     HASKELLEVENT_SPARK_GC(cap)
+#define dtraceTaskCreate(taskID, cap, tid)              \
+    HASKELLEVENT_TASK_CREATE(taskID, cap, tid)
+#define dtraceTaskMigrate(taskID, cap, new_cap)         \
+    HASKELLEVENT_TASK_MIGRATE(taskID, cap, new_cap)
+#define dtraceTaskDelete(taskID)                        \
+    HASKELLEVENT_TASK_DELETE(taskID)
 
 #else /* !defined(DTRACE) */
 
@@ -343,9 +444,23 @@ INLINE_HEADER void dtraceStartup (int num_caps) {
 #define dtraceThreadLabel(cap, tso, label)              /* nothing */
 INLINE_HEADER void dtraceStartup (int num_caps STG_UNUSED) {};
 #define dtraceUserMsg(cap, msg)                         /* nothing */
+#define dtraceUserMarker(cap, msg)                      /* nothing */
 #define dtraceGcIdle(cap)                               /* nothing */
 #define dtraceGcWork(cap)                               /* nothing */
 #define dtraceGcDone(cap)                               /* nothing */
+#define dtraceGcGlobalSync(cap)                         /* nothing */
+#define dtraceEventGcStats(heap_capset, gens,           \
+                           copies, slop, fragmentation, \
+                           par_n_threads,               \
+                           par_max_copied,              \
+                           par_tot_copied)              /* nothing */
+#define dtraceHeapInfo(heap_capset, gens,               \
+                       maxHeapSize, allocAreaSize,      \
+                       mblockSize, blockSize)           /* nothing */
+#define dtraceEventHeapAllocated(cap, heap_capset,      \
+                                 allocated)             /* nothing */
+#define dtraceEventHeapSize(heap_capset, size)          /* nothing */
+#define dtraceEventHeapLive(heap_capset, live)          /* nothing */
 #define dtraceCapCreate(cap)                            /* nothing */
 #define dtraceCapDelete(cap)                            /* nothing */
 #define dtraceCapEnable(cap)                            /* nothing */
@@ -362,6 +477,9 @@ INLINE_HEADER void dtraceStartup (int num_caps STG_UNUSED) {};
 #define dtraceSparkSteal(cap, victim_cap)               /* nothing */
 #define dtraceSparkFizzle(cap)                          /* nothing */
 #define dtraceSparkGc(cap)                              /* nothing */
+#define dtraceTaskCreate(taskID, cap, tid)              /* nothing */
+#define dtraceTaskMigrate(taskID, cap, new_cap)         /* nothing */
+#define dtraceTaskDelete(taskID)                        /* nothing */
 
 #endif
 
@@ -475,12 +593,26 @@ INLINE_HEADER void traceEventGcStart(Capability *cap STG_UNUSED)
     dtraceGcStart((EventCapNo)cap->no);
 }
 
+INLINE_HEADER void traceEventGcStartAtT(Capability *cap STG_UNUSED,
+                                        StgWord64   ts  STG_UNUSED)
+{
+    traceGcEventAtT(cap, ts, EVENT_GC_START);
+    dtraceGcStart((EventCapNo)cap->no);
+}
+
 INLINE_HEADER void traceEventGcEnd(Capability *cap STG_UNUSED)
 {
     traceGcEvent(cap, EVENT_GC_END);
     dtraceGcEnd((EventCapNo)cap->no);
 }
 
+INLINE_HEADER void traceEventGcEndAtT(Capability *cap STG_UNUSED,
+                                      StgWord64   ts  STG_UNUSED)
+{
+    traceGcEventAtT(cap, ts, EVENT_GC_END);
+    dtraceGcEnd((EventCapNo)cap->no);
+}
+
 INLINE_HEADER void traceEventRequestSeqGc(Capability *cap STG_UNUSED)
 {
     traceGcEvent(cap, EVENT_REQUEST_SEQ_GC);
@@ -511,6 +643,76 @@ INLINE_HEADER void traceEventGcDone(Capability *cap STG_UNUSED)
     dtraceGcDone((EventCapNo)cap->no);
 }
 
+INLINE_HEADER void traceEventGcGlobalSync(Capability *cap STG_UNUSED)
+{
+    traceGcEvent(cap, EVENT_GC_GLOBAL_SYNC);
+    dtraceGcGlobalSync((EventCapNo)cap->no);
+}
+
+INLINE_HEADER void traceEventGcStats(Capability *cap            STG_UNUSED,
+                                     CapsetID    heap_capset    STG_UNUSED,
+                                     nat         gen            STG_UNUSED,
+                                     W_        copied         STG_UNUSED,
+                                     W_        slop           STG_UNUSED,
+                                     W_        fragmentation  STG_UNUSED,
+                                     nat         par_n_threads  STG_UNUSED,
+                                     W_        par_max_copied STG_UNUSED,
+                                     W_        par_tot_copied STG_UNUSED)
+{
+    if (RTS_UNLIKELY(TRACE_gc)) {
+        traceEventGcStats_(cap, heap_capset, gen,
+                           copied, slop, fragmentation,
+                           par_n_threads, par_max_copied, par_tot_copied);
+    }
+    dtraceEventGcStats(heap_capset, gen,
+                       copied, slop, fragmentation,
+                       par_n_threads, par_max_copied, par_tot_copied);
+}
+
+INLINE_HEADER void traceEventHeapInfo(CapsetID    heap_capset   STG_UNUSED,
+                                      nat         gens          STG_UNUSED,
+                                      W_        maxHeapSize   STG_UNUSED,
+                                      W_        allocAreaSize STG_UNUSED,
+                                      W_        mblockSize    STG_UNUSED,
+                                      W_        blockSize     STG_UNUSED)
+{
+    if (RTS_UNLIKELY(TRACE_gc)) {
+        traceEventHeapInfo_(heap_capset, gens,
+                            maxHeapSize, allocAreaSize,
+                            mblockSize, blockSize);
+    }
+    dtraceHeapInfo(heap_capset, gens,
+                   maxHeapSize, allocAreaSize,
+                   mblockSize, blockSize);
+}
+
+INLINE_HEADER void traceEventHeapAllocated(Capability *cap         STG_UNUSED,
+                                           CapsetID    heap_capset STG_UNUSED,
+                                           W_        allocated   STG_UNUSED)
+{
+    traceHeapEvent(cap, EVENT_HEAP_ALLOCATED, heap_capset, allocated);
+    dtraceEventHeapAllocated((EventCapNo)cap->no, heap_capset, allocated);
+}
+
+INLINE_HEADER void traceEventHeapSize(Capability *cap         STG_UNUSED,
+                                      CapsetID    heap_capset STG_UNUSED,
+                                      W_        heap_size   STG_UNUSED)
+{
+    traceHeapEvent(cap, EVENT_HEAP_SIZE, heap_capset, heap_size);
+    dtraceEventHeapSize(heap_capset, heap_size);
+}
+
+INLINE_HEADER void traceEventHeapLive(Capability *cap         STG_UNUSED,
+                                      CapsetID    heap_capset STG_UNUSED,
+                                      W_        heap_live   STG_UNUSED)
+{
+    traceHeapEvent(cap, EVENT_HEAP_LIVE, heap_capset, heap_live);
+    dtraceEventHeapLive(heap_capset, heap_live);
+}
+
+/* TODO: at some point we should remove this event, it's covered by
+ * the cap create/delete events.
+ */
 INLINE_HEADER void traceEventStartup(void)
 {
     int n_caps;
@@ -632,6 +834,49 @@ INLINE_HEADER void traceEventSparkGC(Capability *cap STG_UNUSED)
     dtraceSparkGc((EventCapNo)cap->no);
 }
 
+INLINE_HEADER void traceTaskCreate(Task       *task STG_UNUSED,
+                                   Capability *cap  STG_UNUSED)
+{
+    ASSERT(task->cap == cap);
+    // TODO: asserting task->cap == NULL would be much stronger
+    // (the intention being that the task structure is just created and empty)
+    // but would require large changes of traceTaskCreate calls.
+    ASSERT(cap != NULL);
+    // A new task gets associated with a cap. We also record
+    // the kernel thread id of the task, which should never change.
+    if (RTS_UNLIKELY(TRACE_sched)) {
+        traceTaskCreate_(task, cap);
+    }
+    dtraceTaskCreate(serialisableTaskId(task),
+                     (EventCapNo)cap->no,
+                     kernelThreadId());
+}
+
+INLINE_HEADER void traceTaskMigrate(Task       *task    STG_UNUSED,
+                                    Capability *cap     STG_UNUSED,
+                                    Capability *new_cap STG_UNUSED)
+{
+    ASSERT(task->cap == cap);
+    ASSERT(cap != NULL);
+    ASSERT(cap != new_cap);
+    ASSERT(new_cap != NULL);
+    // A task migrates from a cap to another.
+    if (RTS_UNLIKELY(TRACE_sched)) {
+        traceTaskMigrate_(task, cap, new_cap);
+    }
+    dtraceTaskMigrate(serialisableTaskId(task), (EventCapNo)cap->no,
+                                                (EventCapNo)new_cap->no);
+}
+
+INLINE_HEADER void traceTaskDelete(Task *task STG_UNUSED)
+{
+    ASSERT(task->cap != NULL);
+    if (RTS_UNLIKELY(TRACE_sched)) {
+        traceTaskDelete_(task);
+    }
+    dtraceTaskDelete(serialisableTaskId(task));
+}
+
 #include "EndPrivate.h"
 
 #endif /* TRACE_H */