Revision c57c846a

b/Makefile
129 129
qemu-img.o: qemu-img-cmds.h
130 130
qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o: $(GENERATED_HEADERS)
131 131

  
132
qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
132
qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
133 133

  
134
qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
134
qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
135 135

  
136
qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
136
qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
137 137

  
138 138
qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
139 139
	$(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@,"  GEN   $@")
b/Makefile.objs
127 127
common-obj-$(CONFIG_THREAD) += qemu-thread.o
128 128
common-obj-$(CONFIG_IOTHREAD) += compatfd.o
129 129
common-obj-y += notify.o event_notifier.o
130
common-obj-y += qemu-timer.o
130
common-obj-y += qemu-timer.o qemu-timer-common.o
131 131

  
132 132
slirp-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o
133 133
slirp-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
......
278 278
trace-obj-y = trace.o
279 279
ifeq ($(TRACE_BACKEND),simple)
280 280
trace-obj-y += simpletrace.o
281
user-obj-y += qemu-timer-common.o
281 282
endif
282 283

  
283 284
vl.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)
b/block/raw-posix.c
97 97
#define FTYPE_CD     1
98 98
#define FTYPE_FD     2
99 99

  
100
/* if the FD is not accessed during that time (in ms), we try to
100
/* if the FD is not accessed during that time (in ns), we try to
101 101
   reopen it to see if the disk has been changed */
102
#define FD_OPEN_TIMEOUT 1000
102
#define FD_OPEN_TIMEOUT (1000000000)
103 103

  
104 104
#define MAX_BLOCKSIZE	4096
105 105

  
......
908 908
        return 0;
909 909
    last_media_present = (s->fd >= 0);
910 910
    if (s->fd >= 0 &&
911
        (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
911
        (get_clock() - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
912 912
        close(s->fd);
913 913
        s->fd = -1;
914 914
#ifdef DEBUG_FLOPPY
......
917 917
    }
918 918
    if (s->fd < 0) {
919 919
        if (s->fd_got_error &&
920
            (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
920
            (get_clock() - s->fd_error_time) < FD_OPEN_TIMEOUT) {
921 921
#ifdef DEBUG_FLOPPY
922 922
            printf("No floppy (open delayed)\n");
923 923
#endif
......
925 925
        }
926 926
        s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
927 927
        if (s->fd < 0) {
928
            s->fd_error_time = qemu_get_clock(rt_clock);
928
            s->fd_error_time = get_clock();
929 929
            s->fd_got_error = 1;
930 930
            if (last_media_present)
931 931
                s->fd_media_changed = 1;
......
940 940
    }
941 941
    if (!last_media_present)
942 942
        s->fd_media_changed = 1;
943
    s->fd_open_time = qemu_get_clock(rt_clock);
943
    s->fd_open_time = get_clock();
944 944
    s->fd_got_error = 0;
945 945
    return 0;
946 946
}
b/hw/omap_clk.c
20 20
 */
21 21
#include "hw.h"
22 22
#include "omap.h"
23
#include "qemu-timer.h" /* for muldiv64() */
23 24

  
24 25
struct clk {
25 26
    const char *name;
b/qemu-common.h
133 133
int qemu_bh_poll(void);
134 134
void qemu_bh_update_timeout(int *timeout);
135 135

  
136
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
137

  
138 136
void qemu_get_timedate(struct tm *tm, int offset);
139 137
int qemu_timedate_diff(struct tm *tm);
140 138

  
b/qemu-timer-common.c
1
/*
2
 * QEMU System Emulator
3
 *
4
 * Copyright (c) 2003-2008 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include "qemu-timer.h"
25

  
26
/***********************************************************/
27
/* real time host monotonic timer */
28

  
29
#ifdef _WIN32
30

  
31
int64_t clock_freq;
32

  
33
static void __attribute__((constructor)) init_get_clock(void)
34
{
35
    LARGE_INTEGER freq;
36
    int ret;
37
    ret = QueryPerformanceFrequency(&freq);
38
    if (ret == 0) {
39
        fprintf(stderr, "Could not calibrate ticks\n");
40
        exit(1);
41
    }
42
    clock_freq = freq.QuadPart;
43
}
44

  
45
#else
46

  
47
int use_rt_clock;
48

  
49
static void __attribute__((constructor)) init_get_clock(void)
50
{
51
    use_rt_clock = 0;
52
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
53
    || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
54
    {
55
        struct timespec ts;
56
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
57
            use_rt_clock = 1;
58
        }
59
    }
60
#endif
61
}
62
#endif
b/qemu-timer.c
64 64
static QEMUTimer *icount_rt_timer;
65 65
static QEMUTimer *icount_vm_timer;
66 66

  
67

  
68
/***********************************************************/
69
/* real time host monotonic timer */
70

  
71

  
72
static int64_t get_clock_realtime(void)
73
{
74
    struct timeval tv;
75

  
76
    gettimeofday(&tv, NULL);
77
    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
78
}
79

  
80
#ifdef WIN32
81

  
82
static int64_t clock_freq;
83

  
84
static void init_get_clock(void)
85
{
86
    LARGE_INTEGER freq;
87
    int ret;
88
    ret = QueryPerformanceFrequency(&freq);
89
    if (ret == 0) {
90
        fprintf(stderr, "Could not calibrate ticks\n");
91
        exit(1);
92
    }
93
    clock_freq = freq.QuadPart;
94
}
95

  
96
static int64_t get_clock(void)
97
{
98
    LARGE_INTEGER ti;
99
    QueryPerformanceCounter(&ti);
100
    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
101
}
102

  
103
#else
104

  
105
static int use_rt_clock;
106

  
107
static void init_get_clock(void)
108
{
109
    use_rt_clock = 0;
110
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
111
    || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
112
    {
113
        struct timespec ts;
114
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
115
            use_rt_clock = 1;
116
        }
117
    }
118
#endif
119
}
120

  
121
static int64_t get_clock(void)
122
{
123
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
124
	|| defined(__DragonFly__) || defined(__FreeBSD_kernel__)
125
    if (use_rt_clock) {
126
        struct timespec ts;
127
        clock_gettime(CLOCK_MONOTONIC, &ts);
128
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
129
    } else
130
#endif
131
    {
132
        /* XXX: using gettimeofday leads to problems if the date
133
           changes, so it should be avoided. */
134
        return get_clock_realtime();
135
    }
136
}
137
#endif
138

  
139 67
/***********************************************************/
140 68
/* guest cycle counter */
141 69

  
......
614 542

  
615 543
void init_clocks(void)
616 544
{
617
    init_get_clock();
618 545
    rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
619 546
    vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
620 547
    host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
b/qemu-timer.h
2 2
#define QEMU_TIMER_H
3 3

  
4 4
#include "qemu-common.h"
5
#include <time.h>
6
#include <sys/time.h>
7

  
8
#ifdef _WIN32
9
#include <windows.h>
10
#include <mmsystem.h>
11
#endif
5 12

  
6 13
/* timers */
7 14

  
......
52 59
    return 1000000000LL;
53 60
}
54 61

  
62
/* compute with 96 bit intermediate result: (a*b)/c */
63
static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
64
{
65
    union {
66
        uint64_t ll;
67
        struct {
68
#ifdef HOST_WORDS_BIGENDIAN
69
            uint32_t high, low;
70
#else
71
            uint32_t low, high;
72
#endif
73
        } l;
74
    } u, res;
75
    uint64_t rl, rh;
76

  
77
    u.ll = a;
78
    rl = (uint64_t)u.l.low * (uint64_t)b;
79
    rh = (uint64_t)u.l.high * (uint64_t)b;
80
    rh += (rl >> 32);
81
    res.l.high = rh / c;
82
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
83
    return res.ll;
84
}
85

  
86
/* real time host monotonic timer */
87
static inline int64_t get_clock_realtime(void)
88
{
89
    struct timeval tv;
90

  
91
    gettimeofday(&tv, NULL);
92
    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
93
}
94

  
95
/* Warning: don't insert tracepoints into these functions, they are
96
   also used by simpletrace backend and tracepoints would cause
97
   an infinite recursion! */
98
#ifdef _WIN32
99
extern int64_t clock_freq;
100

  
101
static inline int64_t get_clock(void)
102
{
103
    LARGE_INTEGER ti;
104
    QueryPerformanceCounter(&ti);
105
    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
106
}
107

  
108
#else
109

  
110
extern int use_rt_clock;
111

  
112
static inline int64_t get_clock(void)
113
{
114
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
115
    || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
116
    if (use_rt_clock) {
117
        struct timespec ts;
118
        clock_gettime(CLOCK_MONOTONIC, &ts);
119
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
120
    } else
121
#endif
122
    {
123
        /* XXX: using gettimeofday leads to problems if the date
124
           changes, so it should be avoided. */
125
        return get_clock_realtime();
126
    }
127
}
128
#endif
55 129

  
56 130
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
57 131
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
b/qemu-tool.c
110 110
{
111 111
    return 0;
112 112
}
113

  
114
int64_t qemu_get_clock(QEMUClock *clock)
115
{
116
    qemu_timeval tv;
117
    qemu_gettimeofday(&tv);
118
    return (tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000)) / 1000000;
119
}
b/simpletrace.c
12 12
#include <stdint.h>
13 13
#include <stdio.h>
14 14
#include <time.h>
15
#include "qemu-timer.h"
15 16
#include "trace.h"
16 17

  
17 18
/** Trace file header event ID */
......
140 141
                  uint64_t x4, uint64_t x5, uint64_t x6)
141 142
{
142 143
    TraceRecord *rec = &trace_buf[trace_idx];
143
    struct timespec ts;
144

  
145
    /* TODO Windows?  It would be good to use qemu-timer here but that isn't
146
     * linked into qemu-tools.  Also we should avoid recursion in the tracing
147
     * code, therefore it is useful to be self-contained.
148
     */
149
    clock_gettime(CLOCK_MONOTONIC, &ts);
150 144

  
151 145
    if (!trace_list[event].state) {
152 146
        return;
153 147
    }
154 148

  
155 149
    rec->event = event;
156
    rec->timestamp_ns = ts.tv_sec * 1000000000LL + ts.tv_nsec;
150
    rec->timestamp_ns = get_clock();
157 151
    rec->x1 = x1;
158 152
    rec->x2 = x2;
159 153
    rec->x3 = x3;
b/vl.c
289 289
/***********************************************************/
290 290
/* real time host monotonic timer */
291 291

  
292
/* compute with 96 bit intermediate result: (a*b)/c */
293
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
294
{
295
    union {
296
        uint64_t ll;
297
        struct {
298
#ifdef HOST_WORDS_BIGENDIAN
299
            uint32_t high, low;
300
#else
301
            uint32_t low, high;
302
#endif
303
        } l;
304
    } u, res;
305
    uint64_t rl, rh;
306

  
307
    u.ll = a;
308
    rl = (uint64_t)u.l.low * (uint64_t)b;
309
    rh = (uint64_t)u.l.high * (uint64_t)b;
310
    rh += (rl >> 32);
311
    res.l.high = rh / c;
312
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
313
    return res.ll;
314
}
315

  
316 292
/***********************************************************/
317 293
/* host time/date access */
318 294
void qemu_get_timedate(struct tm *tm, int offset)

Also available in: Unified diff