Add a way to generate tracing events programmatically
[ghc-hetmet.git] / rts / Trace.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2008-2009
4  *
5  * Support for fast binary event logging.
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef TRACE_H
10 #define TRACE_H
11
12 #include "rts/EventLogFormat.h"
13 #include "Capability.h"
14
15 BEGIN_RTS_PRIVATE
16
17 // -----------------------------------------------------------------------------
18 // EventLog API
19 // -----------------------------------------------------------------------------
20
21 #if defined(TRACING)
22
23 void initTracing (void);
24 void endTracing  (void);
25 void freeTracing (void);
26
27 #endif /* TRACING */
28
29 // -----------------------------------------------------------------------------
30 // Message classes
31 // -----------------------------------------------------------------------------
32
33 // debugging flags, set with +RTS -D<something>
34 extern int DEBUG_sched;
35 extern int DEBUG_interp;
36 extern int DEBUG_weak;
37 extern int DEBUG_gccafs;
38 extern int DEBUG_gc;
39 extern int DEBUG_block_alloc;
40 extern int DEBUG_sanity;
41 extern int DEBUG_stable;
42 extern int DEBUG_stm;
43 extern int DEBUG_prof;
44 extern int DEBUG_gran;
45 extern int DEBUG_par;
46 extern int DEBUG_linker;
47 extern int DEBUG_squeeze;
48 extern int DEBUG_hpc;
49 extern int DEBUG_sparks;
50
51 // events
52 extern int TRACE_sched;
53
54 // -----------------------------------------------------------------------------
55 // Posting events
56 //
57 // We use macros rather than inline functions deliberately.  We want
58 // the not-taken case to be as efficient as possible, a simple
59 // test-and-jump, and with inline functions gcc seemed to move some of
60 // the instructions from the branch up before the test.
61 // 
62 // -----------------------------------------------------------------------------
63
64 #ifdef DEBUG
65 void traceBegin (const char *str, ...);
66 void traceEnd (void);
67 #endif
68
69 #ifdef TRACING
70
71 /* 
72  * Record a scheduler event
73  */
74 #define traceSchedEvent(cap, tag, tso, other)   \
75     if (RTS_UNLIKELY(TRACE_sched)) {            \
76         traceSchedEvent_(cap, tag, tso, other); \
77     }
78
79 void traceSchedEvent_ (Capability *cap, EventTypeNum tag, 
80                        StgTSO *tso, StgWord64 other);
81
82 // variadic macros are C99, and supported by gcc.  However, the
83 // ##__VA_ARGS syntax is a gcc extension, which allows the variable
84 // argument list to be empty (see gcc docs for details).
85
86 /* 
87  * Emit a trace message on a particular Capability
88  */
89 #define traceCap(class, cap, msg, ...)          \
90     if (RTS_UNLIKELY(class)) {                  \
91         traceCap_(cap, msg, ##__VA_ARGS__);     \
92     }
93
94 void traceCap_(Capability *cap, char *msg, ...);
95
96 /* 
97  * Emit a trace message
98  */
99 #define trace(class, msg, ...)                  \
100     if (RTS_UNLIKELY(class)) {                  \
101         trace_(msg, ##__VA_ARGS__);             \
102     }
103
104 void trace_(char *msg, ...);
105
106 /* 
107  * A message or event emitted by the program
108  */
109 void traceUserMsg(Capability *cap, char *msg);
110
111 /* 
112  * Emit a debug message (only when DEBUG is defined)
113  */
114 #ifdef DEBUG
115 #define debugTrace(class, msg, ...)             \
116     if (RTS_UNLIKELY(class)) {                  \
117         trace_(msg, ##__VA_ARGS__);             \
118     }
119 #else
120 #define debugTrace(class, str, ...) /* nothing */
121 #endif
122
123 /* 
124  * Emit a message/event describing the state of a thread
125  */
126 #define traceThreadStatus(class, tso)           \
127     if (RTS_UNLIKELY(class)) {                  \
128         traceThreadStatus_(tso);                \
129     }
130
131 void traceThreadStatus_ (StgTSO *tso);
132
133 #else /* !TRACING */
134
135 #define traceSchedEvent(cap, tag, tso, other) /* nothing */
136 #define traceCap(class, cap, msg, ...) /* nothing */
137 #define trace(class, msg, ...) /* nothing */
138 #define debugTrace(class, str, ...) /* nothing */
139 #define traceThreadStatus(class, tso) /* nothing */
140
141 #endif /* TRACING */
142
143 END_RTS_PRIVATE
144
145 #endif /* TRACE_H */