2007-11-17 17:14:51 +00:00
|
|
|
#ifndef QEMU_TIMER_H
|
|
|
|
#define QEMU_TIMER_H
|
|
|
|
|
2018-10-17 08:24:19 +00:00
|
|
|
#include "qemu/bitops.h"
|
2012-12-17 17:20:00 +00:00
|
|
|
#include "qemu/notify.h"
|
2015-08-19 15:20:20 +00:00
|
|
|
#include "qemu/host-utils.h"
|
2010-03-29 19:24:00 +00:00
|
|
|
|
2015-07-08 14:10:09 +00:00
|
|
|
#define NANOSECONDS_PER_SECOND 1000000000LL
|
2015-06-12 13:01:29 +00:00
|
|
|
|
2007-11-17 17:14:51 +00:00
|
|
|
/* timers */
|
|
|
|
|
2011-03-11 15:46:02 +00:00
|
|
|
#define SCALE_MS 1000000
|
|
|
|
#define SCALE_US 1000
|
|
|
|
#define SCALE_NS 1
|
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
/**
|
|
|
|
* QEMUClockType:
|
|
|
|
*
|
|
|
|
* The following clock types are available:
|
|
|
|
*
|
|
|
|
* @QEMU_CLOCK_REALTIME: Real time clock
|
|
|
|
*
|
|
|
|
* The real time clock should be used only for stuff which does not
|
2016-07-29 11:05:36 +00:00
|
|
|
* change the virtual machine state, as it runs even if the virtual
|
|
|
|
* machine is stopped.
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
|
|
|
* @QEMU_CLOCK_VIRTUAL: virtual clock
|
|
|
|
*
|
2016-07-29 11:05:36 +00:00
|
|
|
* The virtual clock only runs during the emulation. It stops
|
|
|
|
* when the virtual machine is stopped.
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
|
|
|
* @QEMU_CLOCK_HOST: host clock
|
|
|
|
*
|
2016-07-29 11:05:36 +00:00
|
|
|
* The host clock should be used for device models that emulate accurate
|
2013-08-21 15:02:46 +00:00
|
|
|
* real time sources. It will continue to run when the virtual machine
|
|
|
|
* is suspended, and it will reflect system time changes the host may
|
2016-07-29 11:05:36 +00:00
|
|
|
* undergo (e.g. due to NTP).
|
2014-11-26 10:40:50 +00:00
|
|
|
*
|
|
|
|
* @QEMU_CLOCK_VIRTUAL_RT: realtime clock used for icount warp
|
|
|
|
*
|
|
|
|
* Outside icount mode, this clock is the same as @QEMU_CLOCK_VIRTUAL.
|
|
|
|
* In icount mode, this clock counts nanoseconds while the virtual
|
2014-11-26 10:40:55 +00:00
|
|
|
* machine is running. It is used to increase @QEMU_CLOCK_VIRTUAL
|
|
|
|
* while the CPUs are sleeping and thus not executing instructions.
|
2013-08-21 15:02:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
QEMU_CLOCK_REALTIME = 0,
|
|
|
|
QEMU_CLOCK_VIRTUAL = 1,
|
|
|
|
QEMU_CLOCK_HOST = 2,
|
2014-11-26 10:40:50 +00:00
|
|
|
QEMU_CLOCK_VIRTUAL_RT = 3,
|
2013-08-21 15:02:46 +00:00
|
|
|
QEMU_CLOCK_MAX
|
|
|
|
} QEMUClockType;
|
2013-08-21 15:02:40 +00:00
|
|
|
|
2018-10-17 08:24:19 +00:00
|
|
|
/**
|
|
|
|
* QEMU Timer attributes:
|
|
|
|
*
|
|
|
|
* An individual timer may be given one or multiple attributes when initialized.
|
|
|
|
* Each attribute corresponds to one bit. Attributes modify the processing
|
|
|
|
* of timers when they fire.
|
|
|
|
*
|
2018-10-17 08:24:20 +00:00
|
|
|
* The following attributes are available:
|
|
|
|
*
|
|
|
|
* QEMU_TIMER_ATTR_EXTERNAL: drives external subsystem
|
2019-07-25 08:44:26 +00:00
|
|
|
* QEMU_TIMER_ATTR_ALL: mask for all existing attributes
|
2018-10-17 08:24:20 +00:00
|
|
|
*
|
|
|
|
* Timers with this attribute do not recorded in rr mode, therefore it could be
|
|
|
|
* used for the subsystems that operate outside the guest core. Applicable only
|
|
|
|
* with virtual clock type.
|
2018-10-17 08:24:19 +00:00
|
|
|
*/
|
|
|
|
|
2019-07-25 08:44:26 +00:00
|
|
|
#define QEMU_TIMER_ATTR_EXTERNAL ((int)BIT(0))
|
|
|
|
#define QEMU_TIMER_ATTR_ALL 0xffffffff
|
2018-10-17 08:24:20 +00:00
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
typedef struct QEMUTimerList QEMUTimerList;
|
2013-08-21 15:02:48 +00:00
|
|
|
|
|
|
|
struct QEMUTimerListGroup {
|
|
|
|
QEMUTimerList *tl[QEMU_CLOCK_MAX];
|
|
|
|
};
|
|
|
|
|
2007-11-17 17:14:51 +00:00
|
|
|
typedef void QEMUTimerCB(void *opaque);
|
2017-03-03 10:50:29 +00:00
|
|
|
typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
|
2007-11-17 17:14:51 +00:00
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
struct QEMUTimer {
|
|
|
|
int64_t expire_time; /* in nanoseconds */
|
|
|
|
QEMUTimerList *timer_list;
|
|
|
|
QEMUTimerCB *cb;
|
|
|
|
void *opaque;
|
|
|
|
QEMUTimer *next;
|
2018-10-17 08:24:19 +00:00
|
|
|
int attributes;
|
2013-08-21 15:02:46 +00:00
|
|
|
int scale;
|
|
|
|
};
|
|
|
|
|
2013-08-21 15:02:48 +00:00
|
|
|
extern QEMUTimerListGroup main_loop_tlg;
|
2007-11-17 17:14:51 +00:00
|
|
|
|
2013-08-21 15:03:09 +00:00
|
|
|
/*
|
2013-08-21 15:02:58 +00:00
|
|
|
* qemu_clock_get_ns;
|
|
|
|
* @type: the clock type
|
|
|
|
*
|
|
|
|
* Get the nanosecond value of a clock with
|
|
|
|
* type @type
|
|
|
|
*
|
|
|
|
* Returns: the clock value in nanoseconds
|
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
int64_t qemu_clock_get_ns(QEMUClockType type);
|
2013-08-21 15:02:58 +00:00
|
|
|
|
2013-08-21 15:03:01 +00:00
|
|
|
/**
|
|
|
|
* qemu_clock_get_ms;
|
|
|
|
* @type: the clock type
|
|
|
|
*
|
|
|
|
* Get the millisecond value of a clock with
|
|
|
|
* type @type
|
|
|
|
*
|
|
|
|
* Returns: the clock value in milliseconds
|
|
|
|
*/
|
|
|
|
static inline int64_t qemu_clock_get_ms(QEMUClockType type)
|
|
|
|
{
|
|
|
|
return qemu_clock_get_ns(type) / SCALE_MS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qemu_clock_get_us;
|
|
|
|
* @type: the clock type
|
|
|
|
*
|
|
|
|
* Get the microsecond value of a clock with
|
|
|
|
* type @type
|
|
|
|
*
|
|
|
|
* Returns: the clock value in microseconds
|
|
|
|
*/
|
|
|
|
static inline int64_t qemu_clock_get_us(QEMUClockType type)
|
|
|
|
{
|
|
|
|
return qemu_clock_get_ns(type) / SCALE_US;
|
|
|
|
}
|
|
|
|
|
2013-08-21 15:02:58 +00:00
|
|
|
/**
|
|
|
|
* qemu_clock_has_timers:
|
2013-08-21 15:03:02 +00:00
|
|
|
* @type: the clock type
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
|
|
|
* Determines whether a clock's default timer list
|
|
|
|
* has timers attached
|
|
|
|
*
|
2013-09-12 09:02:19 +00:00
|
|
|
* Note that this function should not be used when other threads also access
|
|
|
|
* the timer list. The return value may be outdated by the time it is acted
|
|
|
|
* upon.
|
|
|
|
*
|
2013-08-21 15:02:58 +00:00
|
|
|
* Returns: true if the clock's default timer list
|
|
|
|
* has timers attached
|
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
bool qemu_clock_has_timers(QEMUClockType type);
|
2013-08-21 15:02:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* qemu_clock_expired:
|
2013-08-21 15:03:02 +00:00
|
|
|
* @type: the clock type
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
|
|
|
* Determines whether a clock's default timer list
|
2016-12-01 04:30:38 +00:00
|
|
|
* has an expired timer.
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
|
|
|
* Returns: true if the clock's default timer list has
|
|
|
|
* an expired timer
|
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
bool qemu_clock_expired(QEMUClockType type);
|
2013-08-21 15:02:41 +00:00
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
/**
|
|
|
|
* qemu_clock_use_for_deadline:
|
2013-08-21 15:03:02 +00:00
|
|
|
* @type: the clock type
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
|
|
|
* Determine whether a clock should be used for deadline
|
|
|
|
* calculations. Some clocks, for instance vm_clock with
|
2020-08-19 11:17:19 +00:00
|
|
|
* icount_enabled() set, do not count in nanoseconds.
|
|
|
|
* Such clocks are not used for deadline calculations, and are presumed
|
2013-08-21 15:02:46 +00:00
|
|
|
* to interrupt any poll using qemu_notify/aio_notify
|
|
|
|
* etc.
|
|
|
|
*
|
|
|
|
* Returns: true if the clock runs in nanoseconds and
|
|
|
|
* should be used for a deadline.
|
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
bool qemu_clock_use_for_deadline(QEMUClockType type);
|
2013-08-21 15:02:46 +00:00
|
|
|
|
2013-08-21 15:02:57 +00:00
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* qemu_clock_deadline_ns_all:
|
|
|
|
* @type: the clock type
|
2019-07-25 08:44:26 +00:00
|
|
|
* @attr_mask: mask for the timer attributes that are included
|
|
|
|
* in deadline calculation
|
2013-08-21 15:02:57 +00:00
|
|
|
*
|
|
|
|
* Calculate the deadline across all timer lists associated
|
|
|
|
* with a clock (as opposed to just the default one)
|
|
|
|
* in nanoseconds, or -1 if no timer is set to expire.
|
|
|
|
*
|
|
|
|
* Returns: time until expiry in nanoseconds or -1
|
|
|
|
*/
|
2019-07-25 08:44:26 +00:00
|
|
|
int64_t qemu_clock_deadline_ns_all(QEMUClockType type, int attr_mask);
|
2013-08-21 15:02:57 +00:00
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
/**
|
|
|
|
* qemu_clock_get_main_loop_timerlist:
|
2013-08-21 15:03:02 +00:00
|
|
|
* @type: the clock type
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
2016-07-07 10:15:58 +00:00
|
|
|
* Return the default timer list associated with a clock.
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
|
|
|
* Returns: the default timer list
|
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
|
2013-08-21 15:02:46 +00:00
|
|
|
|
2013-08-21 15:02:55 +00:00
|
|
|
/**
|
|
|
|
* qemu_clock_nofify:
|
2013-08-21 15:03:02 +00:00
|
|
|
* @type: the clock type
|
2013-08-21 15:02:55 +00:00
|
|
|
*
|
|
|
|
* Call the notifier callback connected with the default timer
|
|
|
|
* list linked to the clock, or qemu_notify() if none.
|
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
void qemu_clock_notify(QEMUClockType type);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qemu_clock_enable:
|
|
|
|
* @type: the clock type
|
|
|
|
* @enabled: true to enable, false to disable
|
|
|
|
*
|
|
|
|
* Enable or disable a clock
|
2013-09-25 06:21:00 +00:00
|
|
|
* Disabling the clock will wait for related timerlists to stop
|
|
|
|
* executing qemu_run_timers. Thus, this functions should not
|
|
|
|
* be used from the callback of a timer that is based on @clock.
|
|
|
|
* Doing so would cause a deadlock.
|
|
|
|
*
|
|
|
|
* Caller should hold BQL.
|
2013-08-21 15:03:02 +00:00
|
|
|
*/
|
|
|
|
void qemu_clock_enable(QEMUClockType type, bool enabled);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qemu_clock_run_timers:
|
|
|
|
* @type: clock on which to operate
|
|
|
|
*
|
|
|
|
* Run all the timers associated with the default timer list
|
|
|
|
* of a clock.
|
|
|
|
*
|
|
|
|
* Returns: true if any timer ran.
|
|
|
|
*/
|
|
|
|
bool qemu_clock_run_timers(QEMUClockType type);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qemu_clock_run_all_timers:
|
|
|
|
*
|
|
|
|
* Run all the timers associated with the default timer list
|
|
|
|
* of every clock.
|
|
|
|
*
|
|
|
|
* Returns: true if any timer ran.
|
|
|
|
*/
|
|
|
|
bool qemu_clock_run_all_timers(void);
|
|
|
|
|
2018-02-27 09:52:26 +00:00
|
|
|
|
2013-08-21 15:03:02 +00:00
|
|
|
/*
|
|
|
|
* QEMUTimerList
|
|
|
|
*/
|
2013-08-21 15:02:55 +00:00
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
/**
|
|
|
|
* timerlist_new:
|
|
|
|
* @type: the clock type to associate with the timerlist
|
2013-08-21 15:02:50 +00:00
|
|
|
* @cb: the callback to call on notification
|
|
|
|
* @opaque: the opaque pointer to pass to the callback
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
|
|
|
* Create a new timerlist associated with the clock of
|
|
|
|
* type @type.
|
|
|
|
*
|
|
|
|
* Returns: a pointer to the QEMUTimerList created
|
|
|
|
*/
|
2013-08-21 15:02:50 +00:00
|
|
|
QEMUTimerList *timerlist_new(QEMUClockType type,
|
|
|
|
QEMUTimerListNotifyCB *cb, void *opaque);
|
2013-08-21 15:02:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* timerlist_free:
|
|
|
|
* @timer_list: the timer list to free
|
|
|
|
*
|
|
|
|
* Frees a timer_list. It must have no active timers.
|
|
|
|
*/
|
|
|
|
void timerlist_free(QEMUTimerList *timer_list);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* timerlist_has_timers:
|
|
|
|
* @timer_list: the timer list to operate on
|
|
|
|
*
|
|
|
|
* Determine whether a timer list has active timers
|
|
|
|
*
|
2013-09-12 09:02:19 +00:00
|
|
|
* Note that this function should not be used when other threads also access
|
|
|
|
* the timer list. The return value may be outdated by the time it is acted
|
|
|
|
* upon.
|
|
|
|
*
|
2013-08-21 15:02:46 +00:00
|
|
|
* Returns: true if the timer list has timers.
|
|
|
|
*/
|
|
|
|
bool timerlist_has_timers(QEMUTimerList *timer_list);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* timerlist_expired:
|
|
|
|
* @timer_list: the timer list to operate on
|
|
|
|
*
|
|
|
|
* Determine whether a timer list has any timers which
|
|
|
|
* are expired.
|
|
|
|
*
|
|
|
|
* Returns: true if the timer list has timers which
|
|
|
|
* have expired.
|
|
|
|
*/
|
|
|
|
bool timerlist_expired(QEMUTimerList *timer_list);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* timerlist_deadline_ns:
|
|
|
|
* @timer_list: the timer list to operate on
|
|
|
|
*
|
|
|
|
* Determine the deadline for a timer_list, i.e.
|
|
|
|
* the number of nanoseconds until the first timer
|
|
|
|
* expires. Return -1 if there are no timers.
|
|
|
|
*
|
|
|
|
* Returns: the number of nanoseconds until the earliest
|
|
|
|
* timer expires -1 if none
|
|
|
|
*/
|
|
|
|
int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
|
|
|
|
|
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* timerlist_get_clock:
|
2013-08-21 15:02:46 +00:00
|
|
|
* @timer_list: the timer list to operate on
|
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Determine the clock type associated with a timer list.
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Returns: the clock type associated with the
|
|
|
|
* timer list.
|
2013-08-21 15:02:46 +00:00
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
|
2013-08-21 15:02:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* timerlist_run_timers:
|
|
|
|
* @timer_list: the timer list to use
|
|
|
|
*
|
|
|
|
* Call all expired timers associated with the timer list.
|
|
|
|
*
|
|
|
|
* Returns: true if any timer expired
|
|
|
|
*/
|
|
|
|
bool timerlist_run_timers(QEMUTimerList *timer_list);
|
|
|
|
|
2013-08-21 15:02:50 +00:00
|
|
|
/**
|
|
|
|
* timerlist_notify:
|
|
|
|
* @timer_list: the timer list to use
|
|
|
|
*
|
|
|
|
* call the notifier callback associated with the timer list.
|
|
|
|
*/
|
|
|
|
void timerlist_notify(QEMUTimerList *timer_list);
|
|
|
|
|
2013-08-21 15:03:02 +00:00
|
|
|
/*
|
|
|
|
* QEMUTimerListGroup
|
|
|
|
*/
|
|
|
|
|
2013-08-21 15:02:48 +00:00
|
|
|
/**
|
|
|
|
* timerlistgroup_init:
|
|
|
|
* @tlg: the timer list group
|
2013-08-21 15:02:50 +00:00
|
|
|
* @cb: the callback to call when a notify is required
|
|
|
|
* @opaque: the opaque pointer to be passed to the callback.
|
2013-08-21 15:02:48 +00:00
|
|
|
*
|
|
|
|
* Initialise a timer list group. This must already be
|
2013-08-21 15:02:50 +00:00
|
|
|
* allocated in memory and zeroed. The notifier callback is
|
|
|
|
* called whenever a clock in the timer list group is
|
|
|
|
* reenabled or whenever a timer associated with any timer
|
|
|
|
* list is modified. If @cb is specified as null, qemu_notify()
|
|
|
|
* is used instead.
|
2013-08-21 15:02:48 +00:00
|
|
|
*/
|
2013-08-21 15:02:50 +00:00
|
|
|
void timerlistgroup_init(QEMUTimerListGroup *tlg,
|
|
|
|
QEMUTimerListNotifyCB *cb, void *opaque);
|
2013-08-21 15:02:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* timerlistgroup_deinit:
|
|
|
|
* @tlg: the timer list group
|
|
|
|
*
|
|
|
|
* Deinitialise a timer list group. This must already be
|
|
|
|
* initialised. Note the memory is not freed.
|
|
|
|
*/
|
|
|
|
void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* timerlistgroup_run_timers:
|
|
|
|
* @tlg: the timer list group
|
|
|
|
*
|
|
|
|
* Run the timers associated with a timer list group.
|
|
|
|
* This will run timers on multiple clocks.
|
|
|
|
*
|
|
|
|
* Returns: true if any timer callback ran
|
|
|
|
*/
|
|
|
|
bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
|
|
|
|
|
|
|
|
/**
|
2013-08-21 15:02:58 +00:00
|
|
|
* timerlistgroup_deadline_ns:
|
2013-08-21 15:02:48 +00:00
|
|
|
* @tlg: the timer list group
|
|
|
|
*
|
|
|
|
* Determine the deadline of the soonest timer to
|
|
|
|
* expire associated with any timer list linked to
|
|
|
|
* the timer list group. Only clocks suitable for
|
|
|
|
* deadline calculation are included.
|
|
|
|
*
|
|
|
|
* Returns: the deadline in nanoseconds or -1 if no
|
|
|
|
* timers are to expire.
|
|
|
|
*/
|
|
|
|
int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
|
|
|
|
|
2013-08-21 15:03:02 +00:00
|
|
|
/*
|
|
|
|
* QEMUTimer
|
2013-08-21 15:02:58 +00:00
|
|
|
*/
|
2013-08-21 15:02:46 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-17 08:24:19 +00:00
|
|
|
* timer_init_full:
|
2013-08-21 15:02:46 +00:00
|
|
|
* @ts: the timer to be initialised
|
2018-10-17 08:24:19 +00:00
|
|
|
* @timer_list_group: (optional) the timer list group to attach the timer to
|
|
|
|
* @type: the clock type to use
|
2014-01-07 00:04:25 +00:00
|
|
|
* @scale: the scale value for the timer
|
2018-10-17 08:24:19 +00:00
|
|
|
* @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
|
2013-08-21 15:02:46 +00:00
|
|
|
* @cb: the callback to be called when the timer expires
|
|
|
|
* @opaque: the opaque pointer to be passed to the callback
|
|
|
|
*
|
2018-10-17 08:24:19 +00:00
|
|
|
* Initialise a timer with the given scale and attributes,
|
|
|
|
* and associate it with timer list for given clock @type in @timer_list_group
|
|
|
|
* (or default timer list group, if NULL).
|
2013-08-21 15:02:46 +00:00
|
|
|
* The caller is responsible for allocating the memory.
|
|
|
|
*
|
|
|
|
* You need not call an explicit deinit call. Simply make
|
|
|
|
* sure it is not on a list with timer_del.
|
|
|
|
*/
|
2018-10-17 08:24:19 +00:00
|
|
|
void timer_init_full(QEMUTimer *ts,
|
|
|
|
QEMUTimerListGroup *timer_list_group, QEMUClockType type,
|
|
|
|
int scale, int attributes,
|
|
|
|
QEMUTimerCB *cb, void *opaque);
|
2013-08-21 15:02:46 +00:00
|
|
|
|
2014-12-23 20:40:55 +00:00
|
|
|
/**
|
|
|
|
* timer_init:
|
2016-07-07 13:27:35 +00:00
|
|
|
* @ts: the timer to be initialised
|
2014-12-23 20:40:55 +00:00
|
|
|
* @type: the clock to associate with the timer
|
|
|
|
* @scale: the scale value for the timer
|
|
|
|
* @cb: the callback to call when the timer expires
|
|
|
|
* @opaque: the opaque pointer to pass to the callback
|
|
|
|
*
|
|
|
|
* Initialize a timer with the given scale on the default timer list
|
|
|
|
* associated with the clock.
|
2018-10-17 08:24:19 +00:00
|
|
|
* See timer_init_full for details.
|
2014-12-23 20:40:55 +00:00
|
|
|
*/
|
|
|
|
static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
|
|
|
|
QEMUTimerCB *cb, void *opaque)
|
|
|
|
{
|
2018-10-17 08:24:19 +00:00
|
|
|
timer_init_full(ts, NULL, type, scale, 0, cb, opaque);
|
2014-12-23 20:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* timer_init_ns:
|
2016-07-07 13:27:35 +00:00
|
|
|
* @ts: the timer to be initialised
|
2014-12-23 20:40:55 +00:00
|
|
|
* @type: the clock to associate with the timer
|
|
|
|
* @cb: the callback to call when the timer expires
|
|
|
|
* @opaque: the opaque pointer to pass to the callback
|
|
|
|
*
|
|
|
|
* Initialize a timer with nanosecond scale on the default timer list
|
|
|
|
* associated with the clock.
|
2018-10-17 08:24:19 +00:00
|
|
|
* See timer_init_full for details.
|
2014-12-23 20:40:55 +00:00
|
|
|
*/
|
|
|
|
static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type,
|
|
|
|
QEMUTimerCB *cb, void *opaque)
|
|
|
|
{
|
|
|
|
timer_init(ts, type, SCALE_NS, cb, opaque);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* timer_init_us:
|
2016-07-07 13:27:35 +00:00
|
|
|
* @ts: the timer to be initialised
|
2014-12-23 20:40:55 +00:00
|
|
|
* @type: the clock to associate with the timer
|
|
|
|
* @cb: the callback to call when the timer expires
|
|
|
|
* @opaque: the opaque pointer to pass to the callback
|
|
|
|
*
|
|
|
|
* Initialize a timer with microsecond scale on the default timer list
|
|
|
|
* associated with the clock.
|
2018-10-17 08:24:19 +00:00
|
|
|
* See timer_init_full for details.
|
2014-12-23 20:40:55 +00:00
|
|
|
*/
|
|
|
|
static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type,
|
|
|
|
QEMUTimerCB *cb, void *opaque)
|
|
|
|
{
|
|
|
|
timer_init(ts, type, SCALE_US, cb, opaque);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* timer_init_ms:
|
2016-07-07 13:27:35 +00:00
|
|
|
* @ts: the timer to be initialised
|
2014-12-23 20:40:55 +00:00
|
|
|
* @type: the clock to associate with the timer
|
|
|
|
* @cb: the callback to call when the timer expires
|
|
|
|
* @opaque: the opaque pointer to pass to the callback
|
|
|
|
*
|
|
|
|
* Initialize a timer with millisecond scale on the default timer list
|
|
|
|
* associated with the clock.
|
2018-10-17 08:24:19 +00:00
|
|
|
* See timer_init_full for details.
|
2014-12-23 20:40:55 +00:00
|
|
|
*/
|
|
|
|
static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
|
|
|
|
QEMUTimerCB *cb, void *opaque)
|
|
|
|
{
|
|
|
|
timer_init(ts, type, SCALE_MS, cb, opaque);
|
|
|
|
}
|
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
/**
|
2018-10-17 08:24:19 +00:00
|
|
|
* timer_new_full:
|
|
|
|
* @timer_list_group: (optional) the timer list group to attach the timer to
|
|
|
|
* @type: the clock type to use
|
2014-01-07 00:04:25 +00:00
|
|
|
* @scale: the scale value for the timer
|
2018-10-17 08:24:19 +00:00
|
|
|
* @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
|
2013-08-21 15:02:46 +00:00
|
|
|
* @cb: the callback to be called when the timer expires
|
|
|
|
* @opaque: the opaque pointer to be passed to the callback
|
|
|
|
*
|
2018-10-17 08:24:19 +00:00
|
|
|
* Create a new timer with the given scale and attributes,
|
|
|
|
* and associate it with timer list for given clock @type in @timer_list_group
|
|
|
|
* (or default timer list group, if NULL).
|
2013-08-21 15:02:46 +00:00
|
|
|
* The memory is allocated by the function.
|
|
|
|
*
|
|
|
|
* This is not the preferred interface unless you know you
|
2018-10-17 08:24:19 +00:00
|
|
|
* are going to call timer_free. Use timer_init or timer_init_full instead.
|
|
|
|
*
|
|
|
|
* The default timer list has one special feature: in icount mode,
|
|
|
|
* %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
|
|
|
|
* not true of other timer lists, which are typically associated
|
|
|
|
* with an AioContext---each of them runs its timer callbacks in its own
|
|
|
|
* AioContext thread.
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
|
|
|
* Returns: a pointer to the timer
|
|
|
|
*/
|
2018-10-17 08:24:19 +00:00
|
|
|
static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group,
|
|
|
|
QEMUClockType type,
|
|
|
|
int scale, int attributes,
|
|
|
|
QEMUTimerCB *cb, void *opaque)
|
2013-08-21 15:02:46 +00:00
|
|
|
{
|
2023-01-22 20:17:37 +00:00
|
|
|
QEMUTimer *ts = (QEMUTimer *)g_new0(QEMUTimer, 1);
|
2018-10-17 08:24:19 +00:00
|
|
|
timer_init_full(ts, timer_list_group, type, scale, attributes, cb, opaque);
|
2013-08-21 15:02:46 +00:00
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
2013-08-21 15:02:56 +00:00
|
|
|
/**
|
|
|
|
* timer_new:
|
|
|
|
* @type: the clock type to use
|
2014-01-07 00:04:25 +00:00
|
|
|
* @scale: the scale value for the timer
|
2013-08-21 15:02:56 +00:00
|
|
|
* @cb: the callback to be called when the timer expires
|
|
|
|
* @opaque: the opaque pointer to be passed to the callback
|
|
|
|
*
|
2018-10-17 08:24:19 +00:00
|
|
|
* Create a new timer with the given scale,
|
|
|
|
* and associate it with the default timer list for the clock type @type.
|
|
|
|
* See timer_new_full for details.
|
2017-03-02 18:56:40 +00:00
|
|
|
*
|
2013-08-21 15:02:56 +00:00
|
|
|
* Returns: a pointer to the timer
|
|
|
|
*/
|
|
|
|
static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
|
|
|
|
QEMUTimerCB *cb, void *opaque)
|
|
|
|
{
|
2018-10-17 08:24:19 +00:00
|
|
|
return timer_new_full(NULL, type, scale, 0, cb, opaque);
|
2013-08-21 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 15:02:58 +00:00
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* timer_new_ns:
|
2016-07-07 13:27:35 +00:00
|
|
|
* @type: the clock type to associate with the timer
|
|
|
|
* @cb: the callback to call when the timer expires
|
2013-08-21 15:03:02 +00:00
|
|
|
* @opaque: the opaque pointer to pass to the callback
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Create a new timer with nanosecond scale on the default timer list
|
|
|
|
* associated with the clock.
|
2018-10-17 08:24:19 +00:00
|
|
|
* See timer_new_full for details.
|
2017-03-02 18:56:40 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Returns: a pointer to the newly created timer
|
2013-08-21 15:02:46 +00:00
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
|
|
|
|
void *opaque)
|
2013-08-21 15:02:46 +00:00
|
|
|
{
|
2013-08-21 15:03:02 +00:00
|
|
|
return timer_new(type, SCALE_NS, cb, opaque);
|
2013-08-21 15:02:46 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 15:02:58 +00:00
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* timer_new_us:
|
2016-07-07 13:27:35 +00:00
|
|
|
* @type: the clock type to associate with the timer
|
|
|
|
* @cb: the callback to call when the timer expires
|
2013-08-21 15:03:02 +00:00
|
|
|
* @opaque: the opaque pointer to pass to the callback
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Create a new timer with microsecond scale on the default timer list
|
|
|
|
* associated with the clock.
|
2018-10-17 08:24:19 +00:00
|
|
|
* See timer_new_full for details.
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Returns: a pointer to the newly created timer
|
2013-08-21 15:02:58 +00:00
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
return timer_new(type, SCALE_US, cb, opaque);
|
|
|
|
}
|
2013-08-21 15:02:58 +00:00
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* timer_new_ms:
|
2016-07-07 13:27:35 +00:00
|
|
|
* @type: the clock type to associate with the timer
|
|
|
|
* @cb: the callback to call when the timer expires
|
2013-08-21 15:03:02 +00:00
|
|
|
* @opaque: the opaque pointer to pass to the callback
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Create a new timer with millisecond scale on the default timer list
|
|
|
|
* associated with the clock.
|
2018-10-17 08:24:19 +00:00
|
|
|
* See timer_new_full for details.
|
2013-08-21 15:03:02 +00:00
|
|
|
*
|
|
|
|
* Returns: a pointer to the newly created timer
|
2013-08-21 15:02:46 +00:00
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
|
|
|
|
void *opaque)
|
2013-08-21 15:02:46 +00:00
|
|
|
{
|
2013-08-21 15:03:02 +00:00
|
|
|
return timer_new(type, SCALE_MS, cb, opaque);
|
2013-08-21 15:02:46 +00:00
|
|
|
}
|
|
|
|
|
2014-12-24 09:57:04 +00:00
|
|
|
/**
|
|
|
|
* timer_deinit:
|
|
|
|
* @ts: the timer to be de-initialised
|
|
|
|
*
|
|
|
|
* Deassociate the timer from any timerlist. You should
|
|
|
|
* call timer_del before. After this call, any further
|
|
|
|
* timer_del call cannot cause dangling pointer accesses
|
|
|
|
* even if the previously used timerlist is freed.
|
|
|
|
*/
|
|
|
|
void timer_deinit(QEMUTimer *ts);
|
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* timer_del:
|
|
|
|
* @ts: the timer
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Delete a timer from the active list.
|
2013-09-12 09:02:19 +00:00
|
|
|
*
|
|
|
|
* This function is thread-safe but the timer and its timer list must not be
|
|
|
|
* freed while this function is running.
|
2013-08-21 15:02:46 +00:00
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
void timer_del(QEMUTimer *ts);
|
2013-08-21 15:02:46 +00:00
|
|
|
|
2020-12-15 15:41:04 +00:00
|
|
|
/**
|
|
|
|
* timer_free:
|
|
|
|
* @ts: the timer
|
|
|
|
*
|
|
|
|
* Free a timer. This will call timer_del() for you to remove
|
|
|
|
* the timer from the active list if it was still active.
|
|
|
|
*/
|
|
|
|
static inline void timer_free(QEMUTimer *ts)
|
|
|
|
{
|
2021-03-10 15:38:48 +00:00
|
|
|
if (ts) {
|
|
|
|
timer_del(ts);
|
|
|
|
g_free(ts);
|
|
|
|
}
|
2020-12-15 15:41:04 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 15:02:58 +00:00
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* timer_mod_ns:
|
|
|
|
* @ts: the timer
|
|
|
|
* @expire_time: the expiry time in nanoseconds
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Modify a timer to expire at @expire_time
|
2013-09-12 09:02:19 +00:00
|
|
|
*
|
|
|
|
* This function is thread-safe but the timer and its timer list must not be
|
|
|
|
* freed while this function is running.
|
2013-08-21 15:02:58 +00:00
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
|
2013-08-21 15:02:58 +00:00
|
|
|
|
2013-10-03 13:11:43 +00:00
|
|
|
/**
|
|
|
|
* timer_mod_anticipate_ns:
|
|
|
|
* @ts: the timer
|
|
|
|
* @expire_time: the expiry time in nanoseconds
|
|
|
|
*
|
|
|
|
* Modify a timer to expire at @expire_time or the current time,
|
|
|
|
* whichever comes earlier.
|
|
|
|
*
|
|
|
|
* This function is thread-safe but the timer and its timer list must not be
|
|
|
|
* freed while this function is running.
|
|
|
|
*/
|
|
|
|
void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
|
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
/**
|
|
|
|
* timer_mod:
|
2013-08-21 15:03:02 +00:00
|
|
|
* @ts: the timer
|
|
|
|
* @expire_time: the expire time in the units associated with the timer
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Modify a timer to expiry at @expire_time, taking into
|
|
|
|
* account the scale associated with the timer.
|
2013-09-12 09:02:19 +00:00
|
|
|
*
|
|
|
|
* This function is thread-safe but the timer and its timer list must not be
|
|
|
|
* freed while this function is running.
|
2013-08-21 15:02:46 +00:00
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
void timer_mod(QEMUTimer *ts, int64_t expire_timer);
|
2013-08-21 15:02:46 +00:00
|
|
|
|
2013-10-03 13:11:43 +00:00
|
|
|
/**
|
|
|
|
* timer_mod_anticipate:
|
|
|
|
* @ts: the timer
|
2020-09-20 15:50:42 +00:00
|
|
|
* @expire_time: the expire time in the units associated with the timer
|
2013-10-03 13:11:43 +00:00
|
|
|
*
|
|
|
|
* Modify a timer to expire at @expire_time or the current time, whichever
|
|
|
|
* comes earlier, taking into account the scale associated with the timer.
|
|
|
|
*
|
|
|
|
* This function is thread-safe but the timer and its timer list must not be
|
|
|
|
* freed while this function is running.
|
|
|
|
*/
|
|
|
|
void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
|
|
|
|
|
2013-08-21 15:02:58 +00:00
|
|
|
/**
|
|
|
|
* timer_pending:
|
2013-08-21 15:03:02 +00:00
|
|
|
* @ts: the timer
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
|
|
|
* Determines whether a timer is pending (i.e. is on the
|
|
|
|
* active list of timers, whether or not it has not yet expired).
|
|
|
|
*
|
|
|
|
* Returns: true if the timer is pending
|
|
|
|
*/
|
|
|
|
bool timer_pending(QEMUTimer *ts);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* timer_expired:
|
2013-08-21 15:03:02 +00:00
|
|
|
* @ts: the timer
|
2016-07-07 13:27:35 +00:00
|
|
|
* @current_time: the current time
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
|
|
|
* Determines whether a timer has expired.
|
|
|
|
*
|
|
|
|
* Returns: true if the timer has expired
|
|
|
|
*/
|
|
|
|
bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* timer_expire_time_ns:
|
2013-08-21 15:03:02 +00:00
|
|
|
* @ts: the timer
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Determine the expiry time of a timer
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Returns: the expiry time in nanoseconds
|
2013-08-21 15:02:58 +00:00
|
|
|
*/
|
|
|
|
uint64_t timer_expire_time_ns(QEMUTimer *ts);
|
|
|
|
|
2013-08-21 15:02:45 +00:00
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* timer_get:
|
|
|
|
* @f: the file
|
|
|
|
* @ts: the timer
|
2013-08-21 15:02:45 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Read a timer @ts from a file @f
|
2013-08-21 15:02:45 +00:00
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
void timer_get(QEMUFile *f, QEMUTimer *ts);
|
2013-08-21 15:02:45 +00:00
|
|
|
|
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* timer_put:
|
|
|
|
* @f: the file
|
|
|
|
* @ts: the timer
|
|
|
|
*/
|
|
|
|
void timer_put(QEMUFile *f, QEMUTimer *ts);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* General utility functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qemu_timeout_ns_to_ms:
|
|
|
|
* @ns: nanosecond timeout value
|
2013-08-21 15:02:45 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Convert a nanosecond timeout value (or -1) to
|
|
|
|
* a millisecond value (or -1), always rounding up.
|
2013-08-21 15:02:45 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Returns: millisecond timeout value
|
2013-08-21 15:02:45 +00:00
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
int qemu_timeout_ns_to_ms(int64_t ns);
|
2013-08-21 15:02:45 +00:00
|
|
|
|
2013-08-21 15:02:58 +00:00
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* qemu_poll_ns:
|
|
|
|
* @fds: Array of file descriptors
|
|
|
|
* @nfds: number of file descriptors
|
|
|
|
* @timeout: timeout in nanoseconds
|
2013-08-21 15:02:58 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Perform a poll like g_poll but with a timeout in nanoseconds.
|
|
|
|
* See g_poll documentation for further details.
|
|
|
|
*
|
|
|
|
* Returns: number of fds ready
|
2013-08-21 15:02:58 +00:00
|
|
|
*/
|
2013-08-21 15:03:02 +00:00
|
|
|
int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
|
2011-03-27 15:45:39 +00:00
|
|
|
|
2013-08-21 15:02:41 +00:00
|
|
|
/**
|
|
|
|
* qemu_soonest_timeout:
|
|
|
|
* @timeout1: first timeout in nanoseconds (or -1 for infinite)
|
|
|
|
* @timeout2: second timeout in nanoseconds (or -1 for infinite)
|
|
|
|
*
|
|
|
|
* Calculates the soonest of two timeout values. -1 means infinite, which
|
|
|
|
* is later than any other value.
|
|
|
|
*
|
|
|
|
* Returns: soonest timeout value in nanoseconds (or -1 for infinite)
|
|
|
|
*/
|
|
|
|
static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
|
|
|
|
{
|
|
|
|
/* we can abuse the fact that -1 (which means infinite) is a maximal
|
|
|
|
* value when cast to unsigned. As this is disgusting, it's kept in
|
|
|
|
* one inline function.
|
|
|
|
*/
|
|
|
|
return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
|
|
|
|
}
|
|
|
|
|
2013-08-21 15:02:46 +00:00
|
|
|
/**
|
2013-08-21 15:03:02 +00:00
|
|
|
* initclocks:
|
2013-08-21 15:02:46 +00:00
|
|
|
*
|
2013-08-21 15:03:02 +00:00
|
|
|
* Initialise the clock & timer infrastructure
|
|
|
|
*/
|
2017-03-03 10:50:29 +00:00
|
|
|
void init_clocks(QEMUTimerListNotifyCB *notify_cb);
|
2013-08-21 15:03:02 +00:00
|
|
|
|
2015-06-12 14:08:45 +00:00
|
|
|
static inline int64_t get_max_clock_jump(void)
|
|
|
|
{
|
|
|
|
/* This should be small enough to prevent excessive interrupts from being
|
|
|
|
* generated by the RTC on clock jumps, but large enough to avoid frequent
|
|
|
|
* unnecessary resets in idle VMs.
|
|
|
|
*/
|
2016-03-21 16:02:30 +00:00
|
|
|
return 60 * NANOSECONDS_PER_SECOND;
|
2015-06-12 14:08:45 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 15:03:02 +00:00
|
|
|
/*
|
|
|
|
* Low level clock functions
|
|
|
|
*/
|
2007-11-17 17:14:51 +00:00
|
|
|
|
2016-07-08 10:31:37 +00:00
|
|
|
/* get host real time in nanosecond */
|
2010-10-23 15:24:07 +00:00
|
|
|
static inline int64_t get_clock_realtime(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
|
|
|
|
}
|
|
|
|
|
2021-01-08 22:42:54 +00:00
|
|
|
extern int64_t clock_start;
|
|
|
|
|
2010-10-23 15:24:07 +00:00
|
|
|
/* Warning: don't insert tracepoints into these functions, they are
|
|
|
|
also used by simpletrace backend and tracepoints would cause
|
|
|
|
an infinite recursion! */
|
|
|
|
#ifdef _WIN32
|
|
|
|
extern int64_t clock_freq;
|
|
|
|
|
|
|
|
static inline int64_t get_clock(void)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER ti;
|
|
|
|
QueryPerformanceCounter(&ti);
|
2016-03-21 16:02:30 +00:00
|
|
|
return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
|
2010-10-23 15:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
extern int use_rt_clock;
|
|
|
|
|
|
|
|
static inline int64_t get_clock(void)
|
|
|
|
{
|
|
|
|
if (use_rt_clock) {
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return ts.tv_sec * 1000000000LL + ts.tv_nsec;
|
2020-02-01 17:22:52 +00:00
|
|
|
} else {
|
2010-10-23 15:24:07 +00:00
|
|
|
/* XXX: using gettimeofday leads to problems if the date
|
|
|
|
changes, so it should be avoided. */
|
|
|
|
return get_clock_realtime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-03-10 10:38:55 +00:00
|
|
|
|
2010-03-29 19:24:00 +00:00
|
|
|
/*******************************************/
|
|
|
|
/* host CPU ticks (if available) */
|
|
|
|
|
|
|
|
#if defined(_ARCH_PPC)
|
|
|
|
|
2015-09-25 14:42:21 +00:00
|
|
|
static inline int64_t cpu_get_host_ticks(void)
|
2010-03-29 19:24:00 +00:00
|
|
|
{
|
|
|
|
int64_t retval;
|
|
|
|
#ifdef _ARCH_PPC64
|
|
|
|
/* This reads timebase in one 64bit go and includes Cell workaround from:
|
|
|
|
http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
|
|
|
|
*/
|
|
|
|
__asm__ __volatile__ ("mftb %0\n\t"
|
|
|
|
"cmpwi %0,0\n\t"
|
|
|
|
"beq- $-8"
|
|
|
|
: "=r" (retval));
|
|
|
|
#else
|
|
|
|
/* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
|
|
|
|
unsigned long junk;
|
2010-04-03 09:37:26 +00:00
|
|
|
__asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
|
|
|
|
"mfspr %L0,268\n\t" /* mftb */
|
|
|
|
"mfspr %0,269\n\t" /* mftbu */
|
2010-03-29 19:24:00 +00:00
|
|
|
"cmpw %0,%1\n\t"
|
|
|
|
"bne $-16"
|
|
|
|
: "=r" (retval), "=r" (junk));
|
|
|
|
#endif
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__i386__)
|
|
|
|
|
2015-09-25 14:42:21 +00:00
|
|
|
static inline int64_t cpu_get_host_ticks(void)
|
2010-03-29 19:24:00 +00:00
|
|
|
{
|
|
|
|
int64_t val;
|
|
|
|
asm volatile ("rdtsc" : "=A" (val));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
|
2015-09-25 14:42:21 +00:00
|
|
|
static inline int64_t cpu_get_host_ticks(void)
|
2010-03-29 19:24:00 +00:00
|
|
|
{
|
|
|
|
uint32_t low,high;
|
|
|
|
int64_t val;
|
|
|
|
asm volatile("rdtsc" : "=a" (low), "=d" (high));
|
|
|
|
val = high;
|
|
|
|
val <<= 32;
|
|
|
|
val |= low;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__hppa__)
|
|
|
|
|
2015-09-25 14:42:21 +00:00
|
|
|
static inline int64_t cpu_get_host_ticks(void)
|
2010-03-29 19:24:00 +00:00
|
|
|
{
|
|
|
|
int val;
|
|
|
|
asm volatile ("mfctl %%cr16, %0" : "=r"(val));
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__s390__)
|
|
|
|
|
2015-09-25 14:42:21 +00:00
|
|
|
static inline int64_t cpu_get_host_ticks(void)
|
2010-03-29 19:24:00 +00:00
|
|
|
{
|
|
|
|
int64_t val;
|
|
|
|
asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-09-21 17:34:21 +00:00
|
|
|
#elif defined(__sparc__)
|
2010-03-29 19:24:00 +00:00
|
|
|
|
2015-09-25 14:42:21 +00:00
|
|
|
static inline int64_t cpu_get_host_ticks (void)
|
2010-03-29 19:24:00 +00:00
|
|
|
{
|
|
|
|
#if defined(_LP64)
|
|
|
|
uint64_t rval;
|
|
|
|
asm volatile("rd %%tick,%0" : "=r"(rval));
|
|
|
|
return rval;
|
|
|
|
#else
|
2012-09-21 17:34:21 +00:00
|
|
|
/* We need an %o or %g register for this. For recent enough gcc
|
|
|
|
there is an "h" constraint for that. Don't bother with that. */
|
2010-03-29 19:24:00 +00:00
|
|
|
union {
|
|
|
|
uint64_t i64;
|
|
|
|
struct {
|
|
|
|
uint32_t high;
|
|
|
|
uint32_t low;
|
|
|
|
} i32;
|
|
|
|
} rval;
|
2012-09-21 17:34:21 +00:00
|
|
|
asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
|
|
|
|
: "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
|
2010-03-29 19:24:00 +00:00
|
|
|
return rval.i64;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__mips__) && \
|
|
|
|
((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
|
|
|
|
/*
|
|
|
|
* binutils wants to use rdhwr only on mips32r2
|
|
|
|
* but as linux kernel emulate it, it's fine
|
|
|
|
* to use it.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#define MIPS_RDHWR(rd, value) { \
|
|
|
|
__asm__ __volatile__ (".set push\n\t" \
|
|
|
|
".set mips32r2\n\t" \
|
|
|
|
"rdhwr %0, "rd"\n\t" \
|
|
|
|
".set pop" \
|
|
|
|
: "=r" (value)); \
|
|
|
|
}
|
|
|
|
|
2015-09-25 14:42:21 +00:00
|
|
|
static inline int64_t cpu_get_host_ticks(void)
|
2010-03-29 19:24:00 +00:00
|
|
|
{
|
|
|
|
/* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
|
|
|
|
uint32_t count;
|
|
|
|
static uint32_t cyc_per_count = 0;
|
|
|
|
|
|
|
|
if (!cyc_per_count) {
|
|
|
|
MIPS_RDHWR("$3", cyc_per_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
MIPS_RDHWR("$2", count);
|
|
|
|
return (int64_t)(count * cyc_per_count);
|
|
|
|
}
|
|
|
|
|
2010-04-12 23:19:26 +00:00
|
|
|
#elif defined(__alpha__)
|
|
|
|
|
2015-09-25 14:42:21 +00:00
|
|
|
static inline int64_t cpu_get_host_ticks(void)
|
2010-04-12 23:19:26 +00:00
|
|
|
{
|
|
|
|
uint64_t cc;
|
|
|
|
uint32_t cur, ofs;
|
|
|
|
|
|
|
|
asm volatile("rpcc %0" : "=r"(cc));
|
|
|
|
cur = cc;
|
|
|
|
ofs = cc >> 32;
|
|
|
|
return cur - ofs;
|
|
|
|
}
|
|
|
|
|
2010-03-29 19:24:00 +00:00
|
|
|
#else
|
|
|
|
/* The host CPU doesn't have an easily accessible cycle counter.
|
|
|
|
Just return a monotonically increasing value. This will be
|
|
|
|
totally wrong, but hopefully better than nothing. */
|
2017-06-13 13:56:59 +00:00
|
|
|
static inline int64_t cpu_get_host_ticks(void)
|
2010-03-29 19:24:00 +00:00
|
|
|
{
|
2017-06-13 13:56:59 +00:00
|
|
|
return get_clock();
|
2010-03-29 19:24:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-04-17 16:25:10 +00:00
|
|
|
#ifdef CONFIG_PROFILER
|
|
|
|
static inline int64_t profile_getclock(void)
|
|
|
|
{
|
2015-03-16 03:57:38 +00:00
|
|
|
return get_clock();
|
2010-04-17 16:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern int64_t dev_time;
|
|
|
|
#endif
|
|
|
|
|
2007-11-17 17:14:51 +00:00
|
|
|
#endif
|