/* -----------------------------------------------------------------------------
*
- * (c) The GHC Team 2006
+ * (c) The GHC Team, 2008-2009
*
- * Debug and performance tracing.
- *
- * This is a layer over RtsMessages, which provides for generating
- * trace messages with timestamps and thread Ids attached
- * automatically. Also, multiple classes of messages are supported,
- * which can be enabled separately via RTS flags.
- *
- * All debug trace messages go through here. Additionally, we
- * generate timestamped trace messages for consumption by profiling
- * tools using this API.
+ * Support for fast binary event logging.
*
* ---------------------------------------------------------------------------*/
#ifndef TRACE_H
#define TRACE_H
+#include "rts/EventLogFormat.h"
+#include "Capability.h"
+
+BEGIN_RTS_PRIVATE
+
// -----------------------------------------------------------------------------
-// Tracing functions
+// EventLog API
// -----------------------------------------------------------------------------
+#if defined(TRACING)
+
void initTracing (void);
+void endTracing (void);
+void freeTracing (void);
-// The simple way:
-void trace (StgWord32 class, const char *str, ...)
- GNUC3_ATTRIBUTE(format (printf, 2, 3));
+#endif /* TRACING */
-// The harder way: sometimes we want to generate a trace message that
-// consists of multiple components generated by different functions.
-// So we provide the functionality of trace() split into 3 parts:
-// - traceClass(): a check that the required class is enabled
-// - traceBegin(): print the beginning of the trace message
-// - traceEnd(): complete the trace message (release the lock too).
-//
-INLINE_HEADER rtsBool traceClass (StgWord32 class);
+// -----------------------------------------------------------------------------
+// Message classes
+// -----------------------------------------------------------------------------
-void traceBegin (const char *str, ...)
- GNUC3_ATTRIBUTE(format (printf, 1, 2));
+// debugging flags, set with +RTS -D<something>
+extern int DEBUG_sched;
+extern int DEBUG_interp;
+extern int DEBUG_weak;
+extern int DEBUG_gccafs;
+extern int DEBUG_gc;
+extern int DEBUG_block_alloc;
+extern int DEBUG_sanity;
+extern int DEBUG_stable;
+extern int DEBUG_stm;
+extern int DEBUG_prof;
+extern int DEBUG_gran;
+extern int DEBUG_par;
+extern int DEBUG_linker;
+extern int DEBUG_squeeze;
+extern int DEBUG_hpc;
+extern int DEBUG_sparks;
+
+// events
+extern int TRACE_sched;
+// -----------------------------------------------------------------------------
+// Posting events
+//
+// We use macros rather than inline functions deliberately. We want
+// the not-taken case to be as efficient as possible, a simple
+// test-and-jump, and with inline functions gcc seemed to move some of
+// the instructions from the branch up before the test.
+//
+// -----------------------------------------------------------------------------
+
+#ifdef DEBUG
+void traceBegin (const char *str, ...);
void traceEnd (void);
+#endif
+#ifdef TRACING
+
+/*
+ * Record a scheduler event
+ */
+#define traceSchedEvent(cap, tag, tso, other) \
+ if (RTS_UNLIKELY(TRACE_sched)) { \
+ traceSchedEvent_(cap, tag, tso, other); \
+ }
+
+void traceSchedEvent_ (Capability *cap, EventTypeNum tag,
+ StgTSO *tso, StgWord64 other);
+
+
+/*
+ * Record a nullary event
+ */
+#define traceEvent(cap, tag) \
+ if (RTS_UNLIKELY(TRACE_sched)) { \
+ traceEvent_(cap, tag); \
+ }
+
+void traceEvent_ (Capability *cap, EventTypeNum tag);
+
+// variadic macros are C99, and supported by gcc. However, the
+// ##__VA_ARGS syntax is a gcc extension, which allows the variable
+// argument list to be empty (see gcc docs for details).
+
+/*
+ * Emit a trace message on a particular Capability
+ */
+#define traceCap(class, cap, msg, ...) \
+ if (RTS_UNLIKELY(class)) { \
+ traceCap_(cap, msg, ##__VA_ARGS__); \
+ }
+
+void traceCap_(Capability *cap, char *msg, ...);
+
+/*
+ * Emit a trace message
+ */
+#define trace(class, msg, ...) \
+ if (RTS_UNLIKELY(class)) { \
+ trace_(msg, ##__VA_ARGS__); \
+ }
+
+void trace_(char *msg, ...);
+
+/*
+ * A message or event emitted by the program
+ */
+void traceUserMsg(Capability *cap, char *msg);
+
+/*
+ * Emit a debug message (only when DEBUG is defined)
+ */
#ifdef DEBUG
-#define debugTrace(class, str, ...) trace(class,str, ## __VA_ARGS__)
-// variable arg macros are C99, and supported by gcc.
-#define debugTraceBegin(str, ...) traceBegin(str, ## __VA_ARGS__)
-#define debugTraceEnd() traceEnd()
+#define debugTrace(class, msg, ...) \
+ if (RTS_UNLIKELY(class)) { \
+ trace_(msg, ##__VA_ARGS__); \
+ }
#else
#define debugTrace(class, str, ...) /* nothing */
-#define debugTraceBegin(str, ...) /* nothing */
-#define debugTraceEnd() /* nothing */
#endif
+/*
+ * Emit a message/event describing the state of a thread
+ */
+#define traceThreadStatus(class, tso) \
+ if (RTS_UNLIKELY(class)) { \
+ traceThreadStatus_(tso); \
+ }
-// -----------------------------------------------------------------------------
-// Message classes, these may be OR-ed together
-// -----------------------------------------------------------------------------
+void traceThreadStatus_ (StgTSO *tso);
-// debugging flags, set with +RTS -D<something>
-#define DEBUG_sched (1<<0)
-#define DEBUG_interp (1<<1)
-#define DEBUG_weak (1<<2)
-#define DEBUG_gccafs (1<<3)
-#define DEBUG_gc (1<<4)
-#define DEBUG_block_alloc (1<<5)
-#define DEBUG_sanity (1<<6)
-#define DEBUG_stable (1<<7)
-#define DEBUG_stm (1<<8)
-#define DEBUG_prof (1<<9)
-#define DEBUG_gran (1<<10)
-#define DEBUG_par (1<<11)
-#define DEBUG_linker (1<<12)
-#define DEBUG_squeeze (1<<13)
-#define DEBUG_hpc (1<<14)
-
-// Tracing flags
-#define TRACE_sched (1<<20)
-#define TRACE_gc (1<<21)
+#else /* !TRACING */
-// -----------------------------------------------------------------------------
-// PRIVATE below here
-// -----------------------------------------------------------------------------
-
-extern StgWord32 classes_enabled;
+#define traceSchedEvent(cap, tag, tso, other) /* nothing */
+#define traceEvent(cap, tag) /* nothing */
+#define traceCap(class, cap, msg, ...) /* nothing */
+#define trace(class, msg, ...) /* nothing */
+#define debugTrace(class, str, ...) /* nothing */
+#define traceThreadStatus(class, tso) /* nothing */
-INLINE_HEADER rtsBool
-traceClass (StgWord32 class) { return (classes_enabled & class); }
+#endif /* TRACING */
-// -----------------------------------------------------------------------------
+END_RTS_PRIVATE
#endif /* TRACE_H */