4 * Copyright IBM, Corp. 2010
6 * This work is licensed under the terms of the GNU GPL, version 2. See
7 * the COPYING file in the top-level directory.
19 #include "qemu/timer.h"
21 #include "trace/control.h"
23 /** Trace file header event ID */
24 #define HEADER_EVENT_ID (~(uint64_t)0) /* avoids conflicting with TraceEventIDs */
26 /** Trace file magic number */
27 #define HEADER_MAGIC 0xf2b177cb0aa429b4ULL
29 /** Trace file version number, bump if format changes */
30 #define HEADER_VERSION 2
32 /** Records were dropped event ID */
33 #define DROPPED_EVENT_ID (~(uint64_t)0 - 1)
35 /** Trace record is valid */
36 #define TRACE_RECORD_VALID ((uint64_t)1 << 63)
39 * Trace records are written out by a dedicated thread. The thread waits for
40 * records to become available, writes them out, and then waits again.
42 static GStaticMutex trace_lock
= G_STATIC_MUTEX_INIT
;
44 /* g_cond_new() was deprecated in glib 2.31 but we still need to support it */
45 #if GLIB_CHECK_VERSION(2, 31, 0)
46 static GCond the_trace_available_cond
;
47 static GCond the_trace_empty_cond
;
48 static GCond
*trace_available_cond
= &the_trace_available_cond
;
49 static GCond
*trace_empty_cond
= &the_trace_empty_cond
;
51 static GCond
*trace_available_cond
;
52 static GCond
*trace_empty_cond
;
55 static bool trace_available
;
56 static bool trace_writeout_enabled
;
59 TRACE_BUF_LEN
= 4096 * 64,
60 TRACE_BUF_FLUSH_THRESHOLD
= TRACE_BUF_LEN
/ 4,
63 uint8_t trace_buf
[TRACE_BUF_LEN
];
64 static volatile gint trace_idx
;
65 static unsigned int writeout_idx
;
66 static volatile gint dropped_events
;
67 static FILE *trace_fp
;
68 static char *trace_file_name
;
70 /* * Trace buffer entry */
72 uint64_t event
; /* TraceEventID */
73 uint64_t timestamp_ns
;
74 uint32_t length
; /* in bytes */
75 uint32_t reserved
; /* unused */
80 uint64_t header_event_id
; /* HEADER_EVENT_ID */
81 uint64_t header_magic
; /* HEADER_MAGIC */
82 uint64_t header_version
; /* HEADER_VERSION */
86 static void read_from_buffer(unsigned int idx
, void *dataptr
, size_t size
);
87 static unsigned int write_to_buffer(unsigned int idx
, void *dataptr
, size_t size
);
89 static void clear_buffer_range(unsigned int idx
, size_t len
)
93 if (idx
>= TRACE_BUF_LEN
) {
94 idx
= idx
% TRACE_BUF_LEN
;
101 * Read a trace record from the trace buffer
103 * @idx Trace buffer index
104 * @record Trace record to fill
106 * Returns false if the record is not valid.
108 static bool get_trace_record(unsigned int idx
, TraceRecord
**recordptr
)
110 uint64_t event_flag
= 0;
112 /* read the event flag to see if its a valid record */
113 read_from_buffer(idx
, &record
, sizeof(event_flag
));
115 if (!(record
.event
& TRACE_RECORD_VALID
)) {
119 smp_rmb(); /* read memory barrier before accessing record */
120 /* read the record header to know record length */
121 read_from_buffer(idx
, &record
, sizeof(TraceRecord
));
122 *recordptr
= malloc(record
.length
); /* dont use g_malloc, can deadlock when traced */
123 /* make a copy of record to avoid being overwritten */
124 read_from_buffer(idx
, *recordptr
, record
.length
);
125 smp_rmb(); /* memory barrier before clearing valid flag */
126 (*recordptr
)->event
&= ~TRACE_RECORD_VALID
;
127 /* clear the trace buffer range for consumed record otherwise any byte
128 * with its MSB set may be considered as a valid event id when the writer
129 * thread crosses this range of buffer again.
131 clear_buffer_range(idx
, record
.length
);
136 * Kick writeout thread
138 * @wait Whether to wait for writeout thread to complete
140 static void flush_trace_file(bool wait
)
142 g_static_mutex_lock(&trace_lock
);
143 trace_available
= true;
144 g_cond_signal(trace_available_cond
);
147 g_cond_wait(trace_empty_cond
, g_static_mutex_get_mutex(&trace_lock
));
150 g_static_mutex_unlock(&trace_lock
);
153 static void wait_for_trace_records_available(void)
155 g_static_mutex_lock(&trace_lock
);
156 while (!(trace_available
&& trace_writeout_enabled
)) {
157 g_cond_signal(trace_empty_cond
);
158 g_cond_wait(trace_available_cond
,
159 g_static_mutex_get_mutex(&trace_lock
));
161 trace_available
= false;
162 g_static_mutex_unlock(&trace_lock
);
165 static gpointer
writeout_thread(gpointer opaque
)
167 TraceRecord
*recordptr
;
170 uint8_t bytes
[sizeof(TraceRecord
) + sizeof(uint64_t)];
172 unsigned int idx
= 0;
174 size_t unused
__attribute__ ((unused
));
177 wait_for_trace_records_available();
179 if (g_atomic_int_get(&dropped_events
)) {
180 dropped
.rec
.event
= DROPPED_EVENT_ID
,
181 dropped
.rec
.timestamp_ns
= get_clock();
182 dropped
.rec
.length
= sizeof(TraceRecord
) + sizeof(uint64_t),
183 dropped
.rec
.reserved
= 0;
185 dropped_count
= g_atomic_int_get(&dropped_events
);
186 } while (!g_atomic_int_compare_and_exchange(&dropped_events
,
188 dropped
.rec
.arguments
[0] = dropped_count
;
189 unused
= fwrite(&dropped
.rec
, dropped
.rec
.length
, 1, trace_fp
);
192 while (get_trace_record(idx
, &recordptr
)) {
193 unused
= fwrite(recordptr
, recordptr
->length
, 1, trace_fp
);
194 writeout_idx
+= recordptr
->length
;
195 free(recordptr
); /* dont use g_free, can deadlock when traced */
196 idx
= writeout_idx
% TRACE_BUF_LEN
;
204 void trace_record_write_u64(TraceBufferRecord
*rec
, uint64_t val
)
206 rec
->rec_off
= write_to_buffer(rec
->rec_off
, &val
, sizeof(uint64_t));
209 void trace_record_write_str(TraceBufferRecord
*rec
, const char *s
, uint32_t slen
)
211 /* Write string length first */
212 rec
->rec_off
= write_to_buffer(rec
->rec_off
, &slen
, sizeof(slen
));
213 /* Write actual string now */
214 rec
->rec_off
= write_to_buffer(rec
->rec_off
, (void*)s
, slen
);
217 int trace_record_start(TraceBufferRecord
*rec
, TraceEventID event
, size_t datasize
)
219 unsigned int idx
, rec_off
, old_idx
, new_idx
;
220 uint32_t rec_len
= sizeof(TraceRecord
) + datasize
;
221 uint64_t event_u64
= event
;
222 uint64_t timestamp_ns
= get_clock();
225 old_idx
= g_atomic_int_get(&trace_idx
);
227 new_idx
= old_idx
+ rec_len
;
229 if (new_idx
- writeout_idx
> TRACE_BUF_LEN
) {
230 /* Trace Buffer Full, Event dropped ! */
231 g_atomic_int_inc(&dropped_events
);
234 } while (!g_atomic_int_compare_and_exchange(&trace_idx
, old_idx
, new_idx
));
236 idx
= old_idx
% TRACE_BUF_LEN
;
239 rec_off
= write_to_buffer(rec_off
, &event_u64
, sizeof(event_u64
));
240 rec_off
= write_to_buffer(rec_off
, ×tamp_ns
, sizeof(timestamp_ns
));
241 rec_off
= write_to_buffer(rec_off
, &rec_len
, sizeof(rec_len
));
244 rec
->rec_off
= (idx
+ sizeof(TraceRecord
)) % TRACE_BUF_LEN
;
248 static void read_from_buffer(unsigned int idx
, void *dataptr
, size_t size
)
250 uint8_t *data_ptr
= dataptr
;
253 if (idx
>= TRACE_BUF_LEN
) {
254 idx
= idx
% TRACE_BUF_LEN
;
256 data_ptr
[x
++] = trace_buf
[idx
++];
260 static unsigned int write_to_buffer(unsigned int idx
, void *dataptr
, size_t size
)
262 uint8_t *data_ptr
= dataptr
;
265 if (idx
>= TRACE_BUF_LEN
) {
266 idx
= idx
% TRACE_BUF_LEN
;
268 trace_buf
[idx
++] = data_ptr
[x
++];
270 return idx
; /* most callers wants to know where to write next */
273 void trace_record_finish(TraceBufferRecord
*rec
)
276 read_from_buffer(rec
->tbuf_idx
, &record
, sizeof(TraceRecord
));
277 smp_wmb(); /* write barrier before marking as valid */
278 record
.event
|= TRACE_RECORD_VALID
;
279 write_to_buffer(rec
->tbuf_idx
, &record
, sizeof(TraceRecord
));
281 if (((unsigned int)g_atomic_int_get(&trace_idx
) - writeout_idx
)
282 > TRACE_BUF_FLUSH_THRESHOLD
) {
283 flush_trace_file(false);
287 void st_set_trace_file_enabled(bool enable
)
289 if (enable
== !!trace_fp
) {
290 return; /* no change */
293 /* Halt trace writeout */
294 flush_trace_file(true);
295 trace_writeout_enabled
= false;
296 flush_trace_file(true);
299 static const TraceLogHeader header
= {
300 .header_event_id
= HEADER_EVENT_ID
,
301 .header_magic
= HEADER_MAGIC
,
302 /* Older log readers will check for version at next location */
303 .header_version
= HEADER_VERSION
,
306 trace_fp
= fopen(trace_file_name
, "wb");
311 if (fwrite(&header
, sizeof header
, 1, trace_fp
) != 1) {
317 /* Resume trace writeout */
318 trace_writeout_enabled
= true;
319 flush_trace_file(false);
327 * Set the name of a trace file
329 * @file The trace file name or NULL for the default name-<pid> set at
332 bool st_set_trace_file(const char *file
)
334 st_set_trace_file_enabled(false);
336 g_free(trace_file_name
);
339 trace_file_name
= g_strdup_printf(CONFIG_TRACE_FILE
, getpid());
341 trace_file_name
= g_strdup_printf("%s", file
);
344 st_set_trace_file_enabled(true);
348 void st_print_trace_file_status(FILE *stream
, int (*stream_printf
)(FILE *stream
, const char *fmt
, ...))
350 stream_printf(stream
, "Trace file \"%s\" %s.\n",
351 trace_file_name
, trace_fp
? "on" : "off");
354 void st_flush_trace_buffer(void)
356 flush_trace_file(true);
359 void trace_print_events(FILE *stream
, fprintf_function stream_printf
)
363 for (i
= 0; i
< trace_event_count(); i
++) {
364 TraceEvent
*ev
= trace_event_id(i
);
365 stream_printf(stream
, "%s [Event ID %u] : state %u\n",
366 trace_event_get_name(ev
), i
, trace_event_get_state_dynamic(ev
));
370 void trace_event_set_state_dynamic_backend(TraceEvent
*ev
, bool state
)
375 /* Helper function to create a thread with signals blocked. Use glib's
376 * portable threads since QEMU abstractions cannot be used due to reentrancy in
377 * the tracer. Also note the signal masking on POSIX hosts so that the thread
378 * does not steal signals when the rest of the program wants them blocked.
380 static GThread
*trace_thread_create(GThreadFunc fn
)
384 sigset_t set
, oldset
;
387 pthread_sigmask(SIG_SETMASK
, &set
, &oldset
);
390 #if GLIB_CHECK_VERSION(2, 31, 0)
391 thread
= g_thread_new("trace-thread", fn
, NULL
);
393 thread
= g_thread_create(fn
, NULL
, FALSE
, NULL
);
397 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
403 bool trace_backend_init(const char *events
, const char *file
)
407 if (!g_thread_supported()) {
408 #if !GLIB_CHECK_VERSION(2, 31, 0)
411 fprintf(stderr
, "glib threading failed to initialize.\n");
416 #if !GLIB_CHECK_VERSION(2, 31, 0)
417 trace_available_cond
= g_cond_new();
418 trace_empty_cond
= g_cond_new();
421 thread
= trace_thread_create(writeout_thread
);
423 fprintf(stderr
, "warning: unable to initialize simple trace backend\n");
427 atexit(st_flush_trace_buffer
);
428 trace_backend_init_events(events
);
429 st_set_trace_file(file
);