simple.c 9.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Simple trace backend
 *
 * Copyright IBM, Corp. 2010
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
15
#ifndef _WIN32
16 17
#include <signal.h>
#include <pthread.h>
18
#endif
19
#include "qemu-timer.h"
20
#include "trace.h"
21
#include "trace/control.h"
22 23 24 25 26 27 28 29 30 31

/** Trace file header event ID */
#define HEADER_EVENT_ID (~(uint64_t)0) /* avoids conflicting with TraceEventIDs */

/** Trace file magic number */
#define HEADER_MAGIC 0xf2b177cb0aa429b4ULL

/** Trace file version number, bump if format changes */
#define HEADER_VERSION 0

32 33 34 35 36 37
/** Records were dropped event ID */
#define DROPPED_EVENT_ID (~(uint64_t)0 - 1)

/** Trace record is valid */
#define TRACE_RECORD_VALID ((uint64_t)1 << 63)

38 39 40 41 42 43 44 45 46 47 48 49 50
/** Trace buffer entry */
typedef struct {
    uint64_t event;
    uint64_t timestamp_ns;
    uint64_t x1;
    uint64_t x2;
    uint64_t x3;
    uint64_t x4;
    uint64_t x5;
    uint64_t x6;
} TraceRecord;

enum {
51 52
    TRACE_BUF_LEN = 4096,
    TRACE_BUF_FLUSH_THRESHOLD = TRACE_BUF_LEN / 4,
53 54
};

55 56 57 58
/*
 * Trace records are written out by a dedicated thread.  The thread waits for
 * records to become available, writes them out, and then waits again.
 */
59 60 61
static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT;
static GCond *trace_available_cond;
static GCond *trace_empty_cond;
62 63 64
static bool trace_available;
static bool trace_writeout_enabled;

65 66 67
static TraceRecord trace_buf[TRACE_BUF_LEN];
static unsigned int trace_idx;
static FILE *trace_fp;
68
static char *trace_file_name = NULL;
69

70
/**
71 72 73 74 75 76
 * Read a trace record from the trace buffer
 *
 * @idx         Trace buffer index
 * @record      Trace record to fill
 *
 * Returns false if the record is not valid.
77
 */
78
static bool get_trace_record(unsigned int idx, TraceRecord *record)
P
Prerna Saxena 已提交
79
{
80 81
    if (!(trace_buf[idx].event & TRACE_RECORD_VALID)) {
        return false;
P
Prerna Saxena 已提交
82 83
    }

84 85 86 87
    __sync_synchronize(); /* read memory barrier before accessing record */

    *record = trace_buf[idx];
    record->event &= ~TRACE_RECORD_VALID;
88
    return true;
P
Prerna Saxena 已提交
89 90
}

91 92 93 94 95 96
/**
 * Kick writeout thread
 *
 * @wait        Whether to wait for writeout thread to complete
 */
static void flush_trace_file(bool wait)
97
{
98
    g_static_mutex_lock(&trace_lock);
99
    trace_available = true;
100
    g_cond_signal(trace_available_cond);
101

102
    if (wait) {
103
        g_cond_wait(trace_empty_cond, g_static_mutex_get_mutex(&trace_lock));
104
    }
105

106
    g_static_mutex_unlock(&trace_lock);
107 108
}

109
static void wait_for_trace_records_available(void)
110
{
111
    g_static_mutex_lock(&trace_lock);
112
    while (!(trace_available && trace_writeout_enabled)) {
113 114 115
        g_cond_signal(trace_empty_cond);
        g_cond_wait(trace_available_cond,
                    g_static_mutex_get_mutex(&trace_lock));
116
    }
117
    trace_available = false;
118
    g_static_mutex_unlock(&trace_lock);
119 120
}

121
static gpointer writeout_thread(gpointer opaque)
122
{
123 124 125
    TraceRecord record;
    unsigned int writeout_idx = 0;
    unsigned int num_available, idx;
126
    size_t unused __attribute__ ((unused));
127 128 129 130 131 132 133 134 135 136 137 138 139

    for (;;) {
        wait_for_trace_records_available();

        num_available = trace_idx - writeout_idx;
        if (num_available > TRACE_BUF_LEN) {
            record = (TraceRecord){
                .event = DROPPED_EVENT_ID,
                .x1 = num_available,
            };
            unused = fwrite(&record, sizeof(record), 1, trace_fp);
            writeout_idx += num_available;
        }
140

141 142 143 144 145 146
        idx = writeout_idx % TRACE_BUF_LEN;
        while (get_trace_record(idx, &record)) {
            trace_buf[idx].event = 0; /* clear valid bit */
            unused = fwrite(&record, sizeof(record), 1, trace_fp);
            idx = ++writeout_idx % TRACE_BUF_LEN;
        }
147

148
        fflush(trace_fp);
149
    }
150
    return NULL;
151 152 153 154 155
}

static void trace(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3,
                  uint64_t x4, uint64_t x5, uint64_t x6)
{
156 157
    unsigned int idx;
    uint64_t timestamp;
158

159 160 161 162
    if (!trace_list[event].state) {
        return;
    }

163 164
    timestamp = get_clock();

165
    idx = g_atomic_int_exchange_and_add((gint *)&trace_idx, 1) % TRACE_BUF_LEN;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    trace_buf[idx] = (TraceRecord){
        .event = event,
        .timestamp_ns = timestamp,
        .x1 = x1,
        .x2 = x2,
        .x3 = x3,
        .x4 = x4,
        .x5 = x5,
        .x6 = x6,
    };
    __sync_synchronize(); /* write barrier before marking as valid */
    trace_buf[idx].event |= TRACE_RECORD_VALID;

    if ((idx + 1) % TRACE_BUF_FLUSH_THRESHOLD == 0) {
        flush_trace_file(false);
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    }
}

void trace0(TraceEventID event)
{
    trace(event, 0, 0, 0, 0, 0, 0);
}

void trace1(TraceEventID event, uint64_t x1)
{
    trace(event, x1, 0, 0, 0, 0, 0);
}

void trace2(TraceEventID event, uint64_t x1, uint64_t x2)
{
    trace(event, x1, x2, 0, 0, 0, 0);
}

void trace3(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3)
{
    trace(event, x1, x2, x3, 0, 0, 0);
}

void trace4(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4)
{
    trace(event, x1, x2, x3, x4, 0, 0);
}

void trace5(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5)
{
    trace(event, x1, x2, x3, x4, x5, 0);
}

void trace6(TraceEventID event, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4, uint64_t x5, uint64_t x6)
{
    trace(event, x1, x2, x3, x4, x5, x6);
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
void st_set_trace_file_enabled(bool enable)
{
    if (enable == !!trace_fp) {
        return; /* no change */
    }

    /* Halt trace writeout */
    flush_trace_file(true);
    trace_writeout_enabled = false;
    flush_trace_file(true);

    if (enable) {
        static const TraceRecord header = {
            .event = HEADER_EVENT_ID,
            .timestamp_ns = HEADER_MAGIC,
            .x1 = HEADER_VERSION,
        };

237
        trace_fp = fopen(trace_file_name, "wb");
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
        if (!trace_fp) {
            return;
        }

        if (fwrite(&header, sizeof header, 1, trace_fp) != 1) {
            fclose(trace_fp);
            trace_fp = NULL;
            return;
        }

        /* Resume trace writeout */
        trace_writeout_enabled = true;
        flush_trace_file(false);
    } else {
        fclose(trace_fp);
        trace_fp = NULL;
    }
}

257
/**
258 259 260 261
 * Set the name of a trace file
 *
 * @file        The trace file name or NULL for the default name-<pid> set at
 *              config time
262
 */
263
bool st_set_trace_file(const char *file)
264
{
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    st_set_trace_file_enabled(false);

    free(trace_file_name);

    if (!file) {
        if (asprintf(&trace_file_name, CONFIG_TRACE_FILE, getpid()) < 0) {
            trace_file_name = NULL;
            return false;
        }
    } else {
        if (asprintf(&trace_file_name, "%s", file) < 0) {
            trace_file_name = NULL;
            return false;
        }
    }

    st_set_trace_file_enabled(true);
    return true;
}

void st_print_trace_file_status(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
{
    stream_printf(stream, "Trace file \"%s\" %s.\n",
                  trace_file_name, trace_fp ? "on" : "off");
289
}
290 291 292 293 294

void st_print_trace(FILE *stream, int (*stream_printf)(FILE *stream, const char *fmt, ...))
{
    unsigned int i;

295 296 297 298 299 300
    for (i = 0; i < TRACE_BUF_LEN; i++) {
        TraceRecord record;

        if (!get_trace_record(i, &record)) {
            continue;
        }
B
Blue Swirl 已提交
301 302
        stream_printf(stream, "Event %" PRIu64 " : %" PRIx64 " %" PRIx64
                      " %" PRIx64 " %" PRIx64 " %" PRIx64 " %" PRIx64 "\n",
303 304 305
                      record.event, record.x1, record.x2,
                      record.x3, record.x4, record.x5,
                      record.x6);
306 307 308
    }
}

309 310 311 312 313 314
void st_flush_trace_buffer(void)
{
    flush_trace_file(true);
}

void trace_print_events(FILE *stream, fprintf_function stream_printf)
315 316 317 318 319 320 321 322 323
{
    unsigned int i;

    for (i = 0; i < NR_TRACE_EVENTS; i++) {
        stream_printf(stream, "%s [Event ID %u] : state %u\n",
                      trace_list[i].tp_name, i, trace_list[i].state);
    }
}

324
bool trace_event_set_state(const char *name, bool state)
325 326 327 328
{
    unsigned int i;

    for (i = 0; i < NR_TRACE_EVENTS; i++) {
329
        if (!strcmp(trace_list[i].tp_name, name)) {
330
            trace_list[i].state = state;
331
            return true;
332 333
        }
    }
334 335 336
    return false;
}

337 338 339 340 341 342
/* Helper function to create a thread with signals blocked.  Use glib's
 * portable threads since QEMU abstractions cannot be used due to reentrancy in
 * the tracer.  Also note the signal masking on POSIX hosts so that the thread
 * does not steal signals when the rest of the program wants them blocked.
 */
static GThread *trace_thread_create(GThreadFunc fn)
343
{
344 345
    GThread *thread;
#ifndef _WIN32
346
    sigset_t set, oldset;
347

348 349
    sigfillset(&set);
    pthread_sigmask(SIG_SETMASK, &set, &oldset);
350 351 352
#endif
    thread = g_thread_create(writeout_thread, NULL, FALSE, NULL);
#ifndef _WIN32
353
    pthread_sigmask(SIG_SETMASK, &oldset, NULL);
354
#endif
355

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    return thread;
}

bool trace_backend_init(const char *events, const char *file)
{
    GThread *thread;

    if (!g_thread_supported()) {
        g_thread_init(NULL);
    }

    trace_available_cond = g_cond_new();
    trace_empty_cond = g_cond_new();

    thread = trace_thread_create(writeout_thread);
    if (!thread) {
372
        fprintf(stderr, "warning: unable to initialize simple trace backend\n");
373
        return false;
374
    }
375

376 377 378
    atexit(st_flush_trace_buffer);
    trace_backend_init_events(events);
    st_set_trace_file(file);
379
    return true;
380
}