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