qemu/include/qemu/timer.h
<<
>>
Prefs
   1#ifndef QEMU_TIMER_H
   2#define QEMU_TIMER_H
   3
   4#include "qemu-common.h"
   5#include "qemu/notify.h"
   6#include "qemu/host-utils.h"
   7
   8#define NANOSECONDS_PER_SECOND 1000000000LL
   9
  10/* timers */
  11
  12#define SCALE_MS 1000000
  13#define SCALE_US 1000
  14#define SCALE_NS 1
  15
  16/**
  17 * QEMUClockType:
  18 *
  19 * The following clock types are available:
  20 *
  21 * @QEMU_CLOCK_REALTIME: Real time clock
  22 *
  23 * The real time clock should be used only for stuff which does not
  24 * change the virtual machine state, as it is run even if the virtual
  25 * machine is stopped. The real time clock has a frequency of 1000
  26 * Hz.
  27 *
  28 * @QEMU_CLOCK_VIRTUAL: virtual clock
  29 *
  30 * The virtual clock is only run during the emulation. It is stopped
  31 * when the virtual machine is stopped. Virtual timers use a high
  32 * precision clock, usually cpu cycles (use ticks_per_sec).
  33 *
  34 * @QEMU_CLOCK_HOST: host clock
  35 *
  36 * The host clock should be use for device models that emulate accurate
  37 * real time sources. It will continue to run when the virtual machine
  38 * is suspended, and it will reflect system time changes the host may
  39 * undergo (e.g. due to NTP). The host clock has the same precision as
  40 * the virtual clock.
  41 *
  42 * @QEMU_CLOCK_VIRTUAL_RT: realtime clock used for icount warp
  43 *
  44 * Outside icount mode, this clock is the same as @QEMU_CLOCK_VIRTUAL.
  45 * In icount mode, this clock counts nanoseconds while the virtual
  46 * machine is running.  It is used to increase @QEMU_CLOCK_VIRTUAL
  47 * while the CPUs are sleeping and thus not executing instructions.
  48 */
  49
  50typedef enum {
  51    QEMU_CLOCK_REALTIME = 0,
  52    QEMU_CLOCK_VIRTUAL = 1,
  53    QEMU_CLOCK_HOST = 2,
  54    QEMU_CLOCK_VIRTUAL_RT = 3,
  55    QEMU_CLOCK_MAX
  56} QEMUClockType;
  57
  58typedef struct QEMUTimerList QEMUTimerList;
  59
  60struct QEMUTimerListGroup {
  61    QEMUTimerList *tl[QEMU_CLOCK_MAX];
  62};
  63
  64typedef void QEMUTimerCB(void *opaque);
  65typedef void QEMUTimerListNotifyCB(void *opaque);
  66
  67struct QEMUTimer {
  68    int64_t expire_time;        /* in nanoseconds */
  69    QEMUTimerList *timer_list;
  70    QEMUTimerCB *cb;
  71    void *opaque;
  72    QEMUTimer *next;
  73    int scale;
  74};
  75
  76extern QEMUTimerListGroup main_loop_tlg;
  77
  78/*
  79 * QEMUClockType
  80 */
  81
  82/*
  83 * qemu_clock_get_ns;
  84 * @type: the clock type
  85 *
  86 * Get the nanosecond value of a clock with
  87 * type @type
  88 *
  89 * Returns: the clock value in nanoseconds
  90 */
  91int64_t qemu_clock_get_ns(QEMUClockType type);
  92
  93/**
  94 * qemu_clock_get_ms;
  95 * @type: the clock type
  96 *
  97 * Get the millisecond value of a clock with
  98 * type @type
  99 *
 100 * Returns: the clock value in milliseconds
 101 */
 102static inline int64_t qemu_clock_get_ms(QEMUClockType type)
 103{
 104    return qemu_clock_get_ns(type) / SCALE_MS;
 105}
 106
 107/**
 108 * qemu_clock_get_us;
 109 * @type: the clock type
 110 *
 111 * Get the microsecond value of a clock with
 112 * type @type
 113 *
 114 * Returns: the clock value in microseconds
 115 */
 116static inline int64_t qemu_clock_get_us(QEMUClockType type)
 117{
 118    return qemu_clock_get_ns(type) / SCALE_US;
 119}
 120
 121/**
 122 * qemu_clock_has_timers:
 123 * @type: the clock type
 124 *
 125 * Determines whether a clock's default timer list
 126 * has timers attached
 127 *
 128 * Note that this function should not be used when other threads also access
 129 * the timer list.  The return value may be outdated by the time it is acted
 130 * upon.
 131 *
 132 * Returns: true if the clock's default timer list
 133 * has timers attached
 134 */
 135bool qemu_clock_has_timers(QEMUClockType type);
 136
 137/**
 138 * qemu_clock_expired:
 139 * @type: the clock type
 140 *
 141 * Determines whether a clock's default timer list
 142 * has an expired clock.
 143 *
 144 * Returns: true if the clock's default timer list has
 145 * an expired timer
 146 */
 147bool qemu_clock_expired(QEMUClockType type);
 148
 149/**
 150 * qemu_clock_use_for_deadline:
 151 * @type: the clock type
 152 *
 153 * Determine whether a clock should be used for deadline
 154 * calculations. Some clocks, for instance vm_clock with
 155 * use_icount set, do not count in nanoseconds. Such clocks
 156 * are not used for deadline calculations, and are presumed
 157 * to interrupt any poll using qemu_notify/aio_notify
 158 * etc.
 159 *
 160 * Returns: true if the clock runs in nanoseconds and
 161 * should be used for a deadline.
 162 */
 163bool qemu_clock_use_for_deadline(QEMUClockType type);
 164
 165/**
 166 * qemu_clock_deadline_ns_all:
 167 * @type: the clock type
 168 *
 169 * Calculate the deadline across all timer lists associated
 170 * with a clock (as opposed to just the default one)
 171 * in nanoseconds, or -1 if no timer is set to expire.
 172 *
 173 * Returns: time until expiry in nanoseconds or -1
 174 */
 175int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
 176
 177/**
 178 * qemu_clock_get_main_loop_timerlist:
 179 * @type: the clock type
 180 *
 181 * Return the default timer list assocatiated with a clock.
 182 *
 183 * Returns: the default timer list
 184 */
 185QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
 186
 187/**
 188 * qemu_clock_nofify:
 189 * @type: the clock type
 190 *
 191 * Call the notifier callback connected with the default timer
 192 * list linked to the clock, or qemu_notify() if none.
 193 */
 194void qemu_clock_notify(QEMUClockType type);
 195
 196/**
 197 * qemu_clock_enable:
 198 * @type: the clock type
 199 * @enabled: true to enable, false to disable
 200 *
 201 * Enable or disable a clock
 202 * Disabling the clock will wait for related timerlists to stop
 203 * executing qemu_run_timers.  Thus, this functions should not
 204 * be used from the callback of a timer that is based on @clock.
 205 * Doing so would cause a deadlock.
 206 *
 207 * Caller should hold BQL.
 208 */
 209void qemu_clock_enable(QEMUClockType type, bool enabled);
 210
 211/**
 212 * qemu_start_warp_timer:
 213 *
 214 * Starts a timer for virtual clock update
 215 */
 216void qemu_start_warp_timer(void);
 217
 218/**
 219 * qemu_clock_register_reset_notifier:
 220 * @type: the clock type
 221 * @notifier: the notifier function
 222 *
 223 * Register a notifier function to call when the clock
 224 * concerned is reset.
 225 */
 226void qemu_clock_register_reset_notifier(QEMUClockType type,
 227                                        Notifier *notifier);
 228
 229/**
 230 * qemu_clock_unregister_reset_notifier:
 231 * @type: the clock type
 232 * @notifier: the notifier function
 233 *
 234 * Unregister a notifier function to call when the clock
 235 * concerned is reset.
 236 */
 237void qemu_clock_unregister_reset_notifier(QEMUClockType type,
 238                                          Notifier *notifier);
 239
 240/**
 241 * qemu_clock_run_timers:
 242 * @type: clock on which to operate
 243 *
 244 * Run all the timers associated with the default timer list
 245 * of a clock.
 246 *
 247 * Returns: true if any timer ran.
 248 */
 249bool qemu_clock_run_timers(QEMUClockType type);
 250
 251/**
 252 * qemu_clock_run_all_timers:
 253 *
 254 * Run all the timers associated with the default timer list
 255 * of every clock.
 256 *
 257 * Returns: true if any timer ran.
 258 */
 259bool qemu_clock_run_all_timers(void);
 260
 261/*
 262 * QEMUTimerList
 263 */
 264
 265/**
 266 * timerlist_new:
 267 * @type: the clock type to associate with the timerlist
 268 * @cb: the callback to call on notification
 269 * @opaque: the opaque pointer to pass to the callback
 270 *
 271 * Create a new timerlist associated with the clock of
 272 * type @type.
 273 *
 274 * Returns: a pointer to the QEMUTimerList created
 275 */
 276QEMUTimerList *timerlist_new(QEMUClockType type,
 277                             QEMUTimerListNotifyCB *cb, void *opaque);
 278
 279/**
 280 * timerlist_free:
 281 * @timer_list: the timer list to free
 282 *
 283 * Frees a timer_list. It must have no active timers.
 284 */
 285void timerlist_free(QEMUTimerList *timer_list);
 286
 287/**
 288 * timerlist_has_timers:
 289 * @timer_list: the timer list to operate on
 290 *
 291 * Determine whether a timer list has active timers
 292 *
 293 * Note that this function should not be used when other threads also access
 294 * the timer list.  The return value may be outdated by the time it is acted
 295 * upon.
 296 *
 297 * Returns: true if the timer list has timers.
 298 */
 299bool timerlist_has_timers(QEMUTimerList *timer_list);
 300
 301/**
 302 * timerlist_expired:
 303 * @timer_list: the timer list to operate on
 304 *
 305 * Determine whether a timer list has any timers which
 306 * are expired.
 307 *
 308 * Returns: true if the timer list has timers which
 309 * have expired.
 310 */
 311bool timerlist_expired(QEMUTimerList *timer_list);
 312
 313/**
 314 * timerlist_deadline_ns:
 315 * @timer_list: the timer list to operate on
 316 *
 317 * Determine the deadline for a timer_list, i.e.
 318 * the number of nanoseconds until the first timer
 319 * expires. Return -1 if there are no timers.
 320 *
 321 * Returns: the number of nanoseconds until the earliest
 322 * timer expires -1 if none
 323 */
 324int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
 325
 326/**
 327 * timerlist_get_clock:
 328 * @timer_list: the timer list to operate on
 329 *
 330 * Determine the clock type associated with a timer list.
 331 *
 332 * Returns: the clock type associated with the
 333 * timer list.
 334 */
 335QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
 336
 337/**
 338 * timerlist_run_timers:
 339 * @timer_list: the timer list to use
 340 *
 341 * Call all expired timers associated with the timer list.
 342 *
 343 * Returns: true if any timer expired
 344 */
 345bool timerlist_run_timers(QEMUTimerList *timer_list);
 346
 347/**
 348 * timerlist_notify:
 349 * @timer_list: the timer list to use
 350 *
 351 * call the notifier callback associated with the timer list.
 352 */
 353void timerlist_notify(QEMUTimerList *timer_list);
 354
 355/*
 356 * QEMUTimerListGroup
 357 */
 358
 359/**
 360 * timerlistgroup_init:
 361 * @tlg: the timer list group
 362 * @cb: the callback to call when a notify is required
 363 * @opaque: the opaque pointer to be passed to the callback.
 364 *
 365 * Initialise a timer list group. This must already be
 366 * allocated in memory and zeroed. The notifier callback is
 367 * called whenever a clock in the timer list group is
 368 * reenabled or whenever a timer associated with any timer
 369 * list is modified. If @cb is specified as null, qemu_notify()
 370 * is used instead.
 371 */
 372void timerlistgroup_init(QEMUTimerListGroup *tlg,
 373                         QEMUTimerListNotifyCB *cb, void *opaque);
 374
 375/**
 376 * timerlistgroup_deinit:
 377 * @tlg: the timer list group
 378 *
 379 * Deinitialise a timer list group. This must already be
 380 * initialised. Note the memory is not freed.
 381 */
 382void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
 383
 384/**
 385 * timerlistgroup_run_timers:
 386 * @tlg: the timer list group
 387 *
 388 * Run the timers associated with a timer list group.
 389 * This will run timers on multiple clocks.
 390 *
 391 * Returns: true if any timer callback ran
 392 */
 393bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
 394
 395/**
 396 * timerlistgroup_deadline_ns:
 397 * @tlg: the timer list group
 398 *
 399 * Determine the deadline of the soonest timer to
 400 * expire associated with any timer list linked to
 401 * the timer list group. Only clocks suitable for
 402 * deadline calculation are included.
 403 *
 404 * Returns: the deadline in nanoseconds or -1 if no
 405 * timers are to expire.
 406 */
 407int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
 408
 409/*
 410 * QEMUTimer
 411 */
 412
 413/**
 414 * timer_init_tl:
 415 * @ts: the timer to be initialised
 416 * @timer_list: the timer list to attach the timer to
 417 * @scale: the scale value for the timer
 418 * @cb: the callback to be called when the timer expires
 419 * @opaque: the opaque pointer to be passed to the callback
 420 *
 421 * Initialise a new timer and associate it with @timer_list.
 422 * The caller is responsible for allocating the memory.
 423 *
 424 * You need not call an explicit deinit call. Simply make
 425 * sure it is not on a list with timer_del.
 426 */
 427void timer_init_tl(QEMUTimer *ts,
 428                   QEMUTimerList *timer_list, int scale,
 429                   QEMUTimerCB *cb, void *opaque);
 430
 431/**
 432 * timer_init:
 433 * @type: the clock to associate with the timer
 434 * @scale: the scale value for the timer
 435 * @cb: the callback to call when the timer expires
 436 * @opaque: the opaque pointer to pass to the callback
 437 *
 438 * Initialize a timer with the given scale on the default timer list
 439 * associated with the clock.
 440 *
 441 * You need not call an explicit deinit call. Simply make
 442 * sure it is not on a list with timer_del.
 443 */
 444static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
 445                              QEMUTimerCB *cb, void *opaque)
 446{
 447    timer_init_tl(ts, main_loop_tlg.tl[type], scale, cb, opaque);
 448}
 449
 450/**
 451 * timer_init_ns:
 452 * @type: the clock to associate with the timer
 453 * @cb: the callback to call when the timer expires
 454 * @opaque: the opaque pointer to pass to the callback
 455 *
 456 * Initialize a timer with nanosecond scale on the default timer list
 457 * associated with the clock.
 458 *
 459 * You need not call an explicit deinit call. Simply make
 460 * sure it is not on a list with timer_del.
 461 */
 462static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type,
 463                                 QEMUTimerCB *cb, void *opaque)
 464{
 465    timer_init(ts, type, SCALE_NS, cb, opaque);
 466}
 467
 468/**
 469 * timer_init_us:
 470 * @type: the clock to associate with the timer
 471 * @cb: the callback to call when the timer expires
 472 * @opaque: the opaque pointer to pass to the callback
 473 *
 474 * Initialize a timer with microsecond scale on the default timer list
 475 * associated with the clock.
 476 *
 477 * You need not call an explicit deinit call. Simply make
 478 * sure it is not on a list with timer_del.
 479 */
 480static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type,
 481                                 QEMUTimerCB *cb, void *opaque)
 482{
 483    timer_init(ts, type, SCALE_US, cb, opaque);
 484}
 485
 486/**
 487 * timer_init_ms:
 488 * @type: the clock to associate with the timer
 489 * @cb: the callback to call when the timer expires
 490 * @opaque: the opaque pointer to pass to the callback
 491 *
 492 * Initialize a timer with millisecond scale on the default timer list
 493 * associated with the clock.
 494 *
 495 * You need not call an explicit deinit call. Simply make
 496 * sure it is not on a list with timer_del.
 497 */
 498static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
 499                                 QEMUTimerCB *cb, void *opaque)
 500{
 501    timer_init(ts, type, SCALE_MS, cb, opaque);
 502}
 503
 504/**
 505 * timer_new_tl:
 506 * @timer_list: the timer list to attach the timer to
 507 * @scale: the scale value for the timer
 508 * @cb: the callback to be called when the timer expires
 509 * @opaque: the opaque pointer to be passed to the callback
 510 *
 511 * Creeate a new timer and associate it with @timer_list.
 512 * The memory is allocated by the function.
 513 *
 514 * This is not the preferred interface unless you know you
 515 * are going to call timer_free. Use timer_init instead.
 516 *
 517 * Returns: a pointer to the timer
 518 */
 519static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
 520                                      int scale,
 521                                      QEMUTimerCB *cb,
 522                                      void *opaque)
 523{
 524    QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
 525    timer_init_tl(ts, timer_list, scale, cb, opaque);
 526    return ts;
 527}
 528
 529/**
 530 * timer_new:
 531 * @type: the clock type to use
 532 * @scale: the scale value for the timer
 533 * @cb: the callback to be called when the timer expires
 534 * @opaque: the opaque pointer to be passed to the callback
 535 *
 536 * Creeate a new timer and associate it with the default
 537 * timer list for the clock type @type.
 538 *
 539 * Returns: a pointer to the timer
 540 */
 541static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
 542                                   QEMUTimerCB *cb, void *opaque)
 543{
 544    return timer_new_tl(main_loop_tlg.tl[type], scale, cb, opaque);
 545}
 546
 547/**
 548 * timer_new_ns:
 549 * @clock: the clock to associate with the timer
 550 * @callback: the callback to call when the timer expires
 551 * @opaque: the opaque pointer to pass to the callback
 552 *
 553 * Create a new timer with nanosecond scale on the default timer list
 554 * associated with the clock.
 555 *
 556 * Returns: a pointer to the newly created timer
 557 */
 558static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
 559                                      void *opaque)
 560{
 561    return timer_new(type, SCALE_NS, cb, opaque);
 562}
 563
 564/**
 565 * timer_new_us:
 566 * @clock: the clock to associate with the timer
 567 * @callback: the callback to call when the timer expires
 568 * @opaque: the opaque pointer to pass to the callback
 569 *
 570 * Create a new timer with microsecond scale on the default timer list
 571 * associated with the clock.
 572 *
 573 * Returns: a pointer to the newly created timer
 574 */
 575static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
 576                                      void *opaque)
 577{
 578    return timer_new(type, SCALE_US, cb, opaque);
 579}
 580
 581/**
 582 * timer_new_ms:
 583 * @clock: the clock to associate with the timer
 584 * @callback: the callback to call when the timer expires
 585 * @opaque: the opaque pointer to pass to the callback
 586 *
 587 * Create a new timer with millisecond scale on the default timer list
 588 * associated with the clock.
 589 *
 590 * Returns: a pointer to the newly created timer
 591 */
 592static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
 593                                      void *opaque)
 594{
 595    return timer_new(type, SCALE_MS, cb, opaque);
 596}
 597
 598/**
 599 * timer_deinit:
 600 * @ts: the timer to be de-initialised
 601 *
 602 * Deassociate the timer from any timerlist.  You should
 603 * call timer_del before.  After this call, any further
 604 * timer_del call cannot cause dangling pointer accesses
 605 * even if the previously used timerlist is freed.
 606 */
 607void timer_deinit(QEMUTimer *ts);
 608
 609/**
 610 * timer_free:
 611 * @ts: the timer
 612 *
 613 * Free a timer (it must not be on the active list)
 614 */
 615void timer_free(QEMUTimer *ts);
 616
 617/**
 618 * timer_del:
 619 * @ts: the timer
 620 *
 621 * Delete a timer from the active list.
 622 *
 623 * This function is thread-safe but the timer and its timer list must not be
 624 * freed while this function is running.
 625 */
 626void timer_del(QEMUTimer *ts);
 627
 628/**
 629 * timer_mod_ns:
 630 * @ts: the timer
 631 * @expire_time: the expiry time in nanoseconds
 632 *
 633 * Modify a timer to expire at @expire_time
 634 *
 635 * This function is thread-safe but the timer and its timer list must not be
 636 * freed while this function is running.
 637 */
 638void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
 639
 640/**
 641 * timer_mod_anticipate_ns:
 642 * @ts: the timer
 643 * @expire_time: the expiry time in nanoseconds
 644 *
 645 * Modify a timer to expire at @expire_time or the current time,
 646 * whichever comes earlier.
 647 *
 648 * This function is thread-safe but the timer and its timer list must not be
 649 * freed while this function is running.
 650 */
 651void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
 652
 653/**
 654 * timer_mod:
 655 * @ts: the timer
 656 * @expire_time: the expire time in the units associated with the timer
 657 *
 658 * Modify a timer to expiry at @expire_time, taking into
 659 * account the scale associated with the timer.
 660 *
 661 * This function is thread-safe but the timer and its timer list must not be
 662 * freed while this function is running.
 663 */
 664void timer_mod(QEMUTimer *ts, int64_t expire_timer);
 665
 666/**
 667 * timer_mod_anticipate:
 668 * @ts: the timer
 669 * @expire_time: the expiry time in nanoseconds
 670 *
 671 * Modify a timer to expire at @expire_time or the current time, whichever
 672 * comes earlier, taking into account the scale associated with the timer.
 673 *
 674 * This function is thread-safe but the timer and its timer list must not be
 675 * freed while this function is running.
 676 */
 677void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
 678
 679/**
 680 * timer_pending:
 681 * @ts: the timer
 682 *
 683 * Determines whether a timer is pending (i.e. is on the
 684 * active list of timers, whether or not it has not yet expired).
 685 *
 686 * Returns: true if the timer is pending
 687 */
 688bool timer_pending(QEMUTimer *ts);
 689
 690/**
 691 * timer_expired:
 692 * @ts: the timer
 693 *
 694 * Determines whether a timer has expired.
 695 *
 696 * Returns: true if the timer has expired
 697 */
 698bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
 699
 700/**
 701 * timer_expire_time_ns:
 702 * @ts: the timer
 703 *
 704 * Determine the expiry time of a timer
 705 *
 706 * Returns: the expiry time in nanoseconds
 707 */
 708uint64_t timer_expire_time_ns(QEMUTimer *ts);
 709
 710/**
 711 * timer_get:
 712 * @f: the file
 713 * @ts: the timer
 714 *
 715 * Read a timer @ts from a file @f
 716 */
 717void timer_get(QEMUFile *f, QEMUTimer *ts);
 718
 719/**
 720 * timer_put:
 721 * @f: the file
 722 * @ts: the timer
 723 */
 724void timer_put(QEMUFile *f, QEMUTimer *ts);
 725
 726/*
 727 * General utility functions
 728 */
 729
 730/**
 731 * qemu_timeout_ns_to_ms:
 732 * @ns: nanosecond timeout value
 733 *
 734 * Convert a nanosecond timeout value (or -1) to
 735 * a millisecond value (or -1), always rounding up.
 736 *
 737 * Returns: millisecond timeout value
 738 */
 739int qemu_timeout_ns_to_ms(int64_t ns);
 740
 741/**
 742 * qemu_poll_ns:
 743 * @fds: Array of file descriptors
 744 * @nfds: number of file descriptors
 745 * @timeout: timeout in nanoseconds
 746 *
 747 * Perform a poll like g_poll but with a timeout in nanoseconds.
 748 * See g_poll documentation for further details.
 749 *
 750 * Returns: number of fds ready
 751 */
 752int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
 753
 754/**
 755 * qemu_soonest_timeout:
 756 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
 757 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
 758 *
 759 * Calculates the soonest of two timeout values. -1 means infinite, which
 760 * is later than any other value.
 761 *
 762 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
 763 */
 764static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
 765{
 766    /* we can abuse the fact that -1 (which means infinite) is a maximal
 767     * value when cast to unsigned. As this is disgusting, it's kept in
 768     * one inline function.
 769     */
 770    return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
 771}
 772
 773/**
 774 * initclocks:
 775 *
 776 * Initialise the clock & timer infrastructure
 777 */
 778void init_clocks(void);
 779
 780int64_t cpu_get_ticks(void);
 781/* Caller must hold BQL */
 782void cpu_enable_ticks(void);
 783/* Caller must hold BQL */
 784void cpu_disable_ticks(void);
 785
 786static inline int64_t get_max_clock_jump(void)
 787{
 788    /* This should be small enough to prevent excessive interrupts from being
 789     * generated by the RTC on clock jumps, but large enough to avoid frequent
 790     * unnecessary resets in idle VMs.
 791     */
 792    return 60 * NANOSECONDS_PER_SECOND;
 793}
 794
 795/*
 796 * Low level clock functions
 797 */
 798
 799/* real time host monotonic timer */
 800static inline int64_t get_clock_realtime(void)
 801{
 802    struct timeval tv;
 803
 804    gettimeofday(&tv, NULL);
 805    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
 806}
 807
 808/* Warning: don't insert tracepoints into these functions, they are
 809   also used by simpletrace backend and tracepoints would cause
 810   an infinite recursion! */
 811#ifdef _WIN32
 812extern int64_t clock_freq;
 813
 814static inline int64_t get_clock(void)
 815{
 816    LARGE_INTEGER ti;
 817    QueryPerformanceCounter(&ti);
 818    return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
 819}
 820
 821#else
 822
 823extern int use_rt_clock;
 824
 825static inline int64_t get_clock(void)
 826{
 827#ifdef CLOCK_MONOTONIC
 828    if (use_rt_clock) {
 829        struct timespec ts;
 830        clock_gettime(CLOCK_MONOTONIC, &ts);
 831        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
 832    } else
 833#endif
 834    {
 835        /* XXX: using gettimeofday leads to problems if the date
 836           changes, so it should be avoided. */
 837        return get_clock_realtime();
 838    }
 839}
 840#endif
 841
 842/* icount */
 843int64_t cpu_get_icount_raw(void);
 844int64_t cpu_get_icount(void);
 845int64_t cpu_get_clock(void);
 846int64_t cpu_icount_to_ns(int64_t icount);
 847
 848/*******************************************/
 849/* host CPU ticks (if available) */
 850
 851#if defined(_ARCH_PPC)
 852
 853static inline int64_t cpu_get_host_ticks(void)
 854{
 855    int64_t retval;
 856#ifdef _ARCH_PPC64
 857    /* This reads timebase in one 64bit go and includes Cell workaround from:
 858       http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
 859    */
 860    __asm__ __volatile__ ("mftb    %0\n\t"
 861                          "cmpwi   %0,0\n\t"
 862                          "beq-    $-8"
 863                          : "=r" (retval));
 864#else
 865    /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
 866    unsigned long junk;
 867    __asm__ __volatile__ ("mfspr   %1,269\n\t"  /* mftbu */
 868                          "mfspr   %L0,268\n\t" /* mftb */
 869                          "mfspr   %0,269\n\t"  /* mftbu */
 870                          "cmpw    %0,%1\n\t"
 871                          "bne     $-16"
 872                          : "=r" (retval), "=r" (junk));
 873#endif
 874    return retval;
 875}
 876
 877#elif defined(__i386__)
 878
 879static inline int64_t cpu_get_host_ticks(void)
 880{
 881    int64_t val;
 882    asm volatile ("rdtsc" : "=A" (val));
 883    return val;
 884}
 885
 886#elif defined(__x86_64__)
 887
 888static inline int64_t cpu_get_host_ticks(void)
 889{
 890    uint32_t low,high;
 891    int64_t val;
 892    asm volatile("rdtsc" : "=a" (low), "=d" (high));
 893    val = high;
 894    val <<= 32;
 895    val |= low;
 896    return val;
 897}
 898
 899#elif defined(__hppa__)
 900
 901static inline int64_t cpu_get_host_ticks(void)
 902{
 903    int val;
 904    asm volatile ("mfctl %%cr16, %0" : "=r"(val));
 905    return val;
 906}
 907
 908#elif defined(__ia64)
 909
 910static inline int64_t cpu_get_host_ticks(void)
 911{
 912    int64_t val;
 913    asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
 914    return val;
 915}
 916
 917#elif defined(__s390__)
 918
 919static inline int64_t cpu_get_host_ticks(void)
 920{
 921    int64_t val;
 922    asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
 923    return val;
 924}
 925
 926#elif defined(__sparc__)
 927
 928static inline int64_t cpu_get_host_ticks (void)
 929{
 930#if defined(_LP64)
 931    uint64_t        rval;
 932    asm volatile("rd %%tick,%0" : "=r"(rval));
 933    return rval;
 934#else
 935    /* We need an %o or %g register for this.  For recent enough gcc
 936       there is an "h" constraint for that.  Don't bother with that.  */
 937    union {
 938        uint64_t i64;
 939        struct {
 940            uint32_t high;
 941            uint32_t low;
 942        }       i32;
 943    } rval;
 944    asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
 945                 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
 946    return rval.i64;
 947#endif
 948}
 949
 950#elif defined(__mips__) && \
 951    ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
 952/*
 953 * binutils wants to use rdhwr only on mips32r2
 954 * but as linux kernel emulate it, it's fine
 955 * to use it.
 956 *
 957 */
 958#define MIPS_RDHWR(rd, value) {                         \
 959        __asm__ __volatile__ (".set   push\n\t"         \
 960                              ".set mips32r2\n\t"       \
 961                              "rdhwr  %0, "rd"\n\t"     \
 962                              ".set   pop"              \
 963                              : "=r" (value));          \
 964    }
 965
 966static inline int64_t cpu_get_host_ticks(void)
 967{
 968    /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
 969    uint32_t count;
 970    static uint32_t cyc_per_count = 0;
 971
 972    if (!cyc_per_count) {
 973        MIPS_RDHWR("$3", cyc_per_count);
 974    }
 975
 976    MIPS_RDHWR("$2", count);
 977    return (int64_t)(count * cyc_per_count);
 978}
 979
 980#elif defined(__alpha__)
 981
 982static inline int64_t cpu_get_host_ticks(void)
 983{
 984    uint64_t cc;
 985    uint32_t cur, ofs;
 986
 987    asm volatile("rpcc %0" : "=r"(cc));
 988    cur = cc;
 989    ofs = cc >> 32;
 990    return cur - ofs;
 991}
 992
 993#else
 994/* The host CPU doesn't have an easily accessible cycle counter.
 995   Just return a monotonically increasing value.  This will be
 996   totally wrong, but hopefully better than nothing.  */
 997static inline int64_t cpu_get_host_ticks (void)
 998{
 999    static int64_t ticks = 0;
1000    return ticks++;
1001}
1002#endif
1003
1004#ifdef CONFIG_PROFILER
1005static inline int64_t profile_getclock(void)
1006{
1007    return get_clock();
1008}
1009
1010extern int64_t tcg_time;
1011extern int64_t dev_time;
1012#endif
1013
1014#endif
1015