Use snwprintf instead of swprintf in rts/Linker.c.
[ghc.git] / rts / Trace.h
index 8dacb80..31aefcb 100644 (file)
@@ -28,6 +28,7 @@ void initTracing (void);
 void endTracing  (void);
 void freeTracing (void);
 void resetTracing (void);
+void tracingAddCapapilities (nat from, nat to);
 
 #endif /* TRACING */
 
@@ -36,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
@@ -112,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)         \
@@ -156,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
@@ -198,17 +245,20 @@ void traceThreadStatus_ (StgTSO *tso);
 void traceEventStartup_ (int n_caps);
 
 /*
- * Events for describing capability sets in the eventlog
+ * Events for describing capabilities and capability sets in the eventlog
  *
  * Note: unlike other events, these are not conditional on TRACE_sched or
- * similar because they are not "real" events themselves but provide
- * information and context for other "real" events. Other events depend on
- * the capset info events so for simplicity, rather than working out if
- * they're necessary we always emit them. They should be very low volume.
+ * similar because capabilities and capability sets are important
+ * context for other events. Since other events depend on these events
+ * then for simplicity we always emit them, rather than working out if
+ * they're necessary . They should be very low volume.
  */
-void traceCapsetEvent_ (EventTypeNum tag,
-                        CapsetID capset,
-                        StgWord info);
+void traceCapEvent (Capability   *cap,
+                    EventTypeNum  tag);
+
+void traceCapsetEvent (EventTypeNum tag,
+                       CapsetID     capset,
+                       StgWord      info);
 
 void traceWallClockTime_(void);
 
@@ -218,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 */
@@ -232,10 +299,14 @@ void traceSparkCounters_ (Capability *cap,
 #define traceThreadStatus(class, tso) /* nothing */
 #define traceThreadLabel_(cap, tso, label) /* nothing */
 INLINE_HEADER void traceEventStartup_ (int n_caps STG_UNUSED) {};
-#define traceCapsetEvent_(tag, capset, info) /* nothing */
+#define traceCapEvent(cap, tag) /* nothing */
+#define traceCapsetEvent(tag, capset, info) /* nothing */
 #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 */
 
@@ -245,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) */
 
@@ -264,8 +336,6 @@ void dtraceUserMsgWrapper(Capability *cap, char *msg);
     HASKELLEVENT_THREAD_RUNNABLE(cap, tid)
 #define dtraceMigrateThread(cap, tid, new_cap)          \
     HASKELLEVENT_MIGRATE_THREAD(cap, tid, new_cap)
-#define dtraceShutdown(cap)                             \
-    HASKELLEVENT_SHUTDOWN(cap)
 #define dtraceThreadWakeup(cap, tid, other_cap)         \
     HASKELLEVENT_THREAD_WAKEUP(cap, tid, other_cap)
 #define dtraceGcStart(cap)                              \
@@ -283,14 +353,50 @@ void dtraceUserMsgWrapper(Capability *cap, char *msg);
 INLINE_HEADER void dtraceStartup (int num_caps) {
     HASKELLEVENT_STARTUP(num_caps);
 }
+#define dtraceCapCreate(cap)                            \
+    HASKELLEVENT_CAP_CREATE(cap)
+#define dtraceCapDelete(cap)                            \
+    HASKELLEVENT_CAP_DELETE(cap)
+#define dtraceCapEnable(cap)                            \
+    HASKELLEVENT_CAP_ENABLE(cap)
+#define dtraceCapDisable(cap)                           \
+    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)                      \
@@ -315,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) */
 
@@ -323,7 +435,6 @@ INLINE_HEADER void dtraceStartup (int num_caps) {
 #define dtraceStopThread(cap, tid, status, info)        /* nothing */
 #define dtraceThreadRunnable(cap, tid)                  /* nothing */
 #define dtraceMigrateThread(cap, tid, new_cap)          /* nothing */
-#define dtraceShutdown(cap)                             /* nothing */
 #define dtraceThreadWakeup(cap, tid, other_cap)         /* nothing */
 #define dtraceGcStart(cap)                              /* nothing */
 #define dtraceGcEnd(cap)                                /* nothing */
@@ -333,9 +444,27 @@ 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 */
+#define dtraceCapDisable(cap)                           /* nothing */
 #define dtraceCapsetCreate(capset, capset_type)         /* nothing */
 #define dtraceCapsetDelete(capset)                      /* nothing */
 #define dtraceCapsetAssignCap(capset, capno)            /* nothing */
@@ -348,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
 
@@ -412,10 +544,28 @@ INLINE_HEADER void traceEventMigrateThread(Capability *cap     STG_UNUSED,
                         (EventCapNo)new_cap);
 }
 
-INLINE_HEADER void traceEventShutdown(Capability *cap STG_UNUSED)
+INLINE_HEADER void traceCapCreate(Capability *cap STG_UNUSED)
 {
-    traceSchedEvent(cap, EVENT_SHUTDOWN, 0, 0);
-    dtraceShutdown((EventCapNo)cap->no);
+    traceCapEvent(cap, EVENT_CAP_CREATE);
+    dtraceCapCreate((EventCapNo)cap->no);
+}
+
+INLINE_HEADER void traceCapDelete(Capability *cap STG_UNUSED)
+{
+    traceCapEvent(cap, EVENT_CAP_DELETE);
+    dtraceCapDelete((EventCapNo)cap->no);
+}
+
+INLINE_HEADER void traceCapEnable(Capability *cap STG_UNUSED)
+{
+    traceCapEvent(cap, EVENT_CAP_ENABLE);
+    dtraceCapEnable((EventCapNo)cap->no);
+}
+
+INLINE_HEADER void traceCapDisable(Capability *cap STG_UNUSED)
+{
+    traceCapEvent(cap, EVENT_CAP_DISABLE);
+    dtraceCapDisable((EventCapNo)cap->no);
 }
 
 INLINE_HEADER void traceEventThreadWakeup(Capability *cap       STG_UNUSED, 
@@ -443,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);
@@ -479,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;
@@ -496,27 +730,27 @@ INLINE_HEADER void traceEventStartup(void)
 INLINE_HEADER void traceCapsetCreate(CapsetID   capset      STG_UNUSED,
                                      CapsetType capset_type STG_UNUSED)
 {
-    traceCapsetEvent_(EVENT_CAPSET_CREATE, capset, capset_type);
+    traceCapsetEvent(EVENT_CAPSET_CREATE, capset, capset_type);
     dtraceCapsetCreate(capset, capset_type);
 }
 
 INLINE_HEADER void traceCapsetDelete(CapsetID capset STG_UNUSED)
 {
-    traceCapsetEvent_(EVENT_CAPSET_DELETE, capset, 0);
+    traceCapsetEvent(EVENT_CAPSET_DELETE, capset, 0);
     dtraceCapsetDelete(capset);
 }
 
 INLINE_HEADER void traceCapsetAssignCap(CapsetID capset STG_UNUSED,
                                         nat      capno  STG_UNUSED)
 {
-    traceCapsetEvent_(EVENT_CAPSET_ASSIGN_CAP, capset, capno);
+    traceCapsetEvent(EVENT_CAPSET_ASSIGN_CAP, capset, capno);
     dtraceCapsetAssignCap(capset, capno);
 }
 
 INLINE_HEADER void traceCapsetRemoveCap(CapsetID capset STG_UNUSED,
                                         nat      capno  STG_UNUSED)
 {
-    traceCapsetEvent_(EVENT_CAPSET_REMOVE_CAP, capset, capno);
+    traceCapsetEvent(EVENT_CAPSET_REMOVE_CAP, capset, capno);
     dtraceCapsetRemoveCap(capset, capno);
 }
 
@@ -600,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 */