xref: /openbmc/qemu/include/sysemu/cpu-timers.h (revision b14df228)
1 /*
2  * CPU timers state API
3  *
4  * Copyright 2020 SUSE LLC
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  *
9  */
10 #ifndef SYSEMU_CPU_TIMERS_H
11 #define SYSEMU_CPU_TIMERS_H
12 
13 #include "qemu/timer.h"
14 
15 /* init the whole cpu timers API, including icount, ticks, and cpu_throttle */
16 void cpu_timers_init(void);
17 
18 /* icount - Instruction Counter API */
19 
20 /*
21  * icount enablement state:
22  *
23  * 0 = Disabled - Do not count executed instructions.
24  * 1 = Enabled - Fixed conversion of insn to ns via "shift" option
25  * 2 = Enabled - Runtime adaptive algorithm to compute shift
26  */
27 #ifdef CONFIG_TCG
28 extern int use_icount;
29 #define icount_enabled() (use_icount)
30 #else
31 #define icount_enabled() 0
32 #endif
33 
34 /*
35  * Update the icount with the executed instructions. Called by
36  * cpus-tcg vCPU thread so the main-loop can see time has moved forward.
37  */
38 void icount_update(CPUState *cpu);
39 
40 /* get raw icount value */
41 int64_t icount_get_raw(void);
42 
43 /* return the virtual CPU time in ns, based on the instruction counter. */
44 int64_t icount_get(void);
45 /*
46  * convert an instruction counter value to ns, based on the icount shift.
47  * This shift is set as a fixed value with the icount "shift" option
48  * (precise mode), or it is constantly approximated and corrected at
49  * runtime in adaptive mode.
50  */
51 int64_t icount_to_ns(int64_t icount);
52 
53 /* configure the icount options, including "shift" */
54 void icount_configure(QemuOpts *opts, Error **errp);
55 
56 /* used by tcg vcpu thread to calc icount budget */
57 int64_t icount_round(int64_t count);
58 
59 /* if the CPUs are idle, start accounting real time to virtual clock. */
60 void icount_start_warp_timer(void);
61 void icount_account_warp_timer(void);
62 void icount_notify_exit(void);
63 
64 /*
65  * CPU Ticks and Clock
66  */
67 
68 /* Caller must hold BQL */
69 void cpu_enable_ticks(void);
70 /* Caller must hold BQL */
71 void cpu_disable_ticks(void);
72 
73 /*
74  * return the time elapsed in VM between vm_start and vm_stop.
75  * cpu_get_ticks() uses units of the host CPU cycle counter.
76  */
77 int64_t cpu_get_ticks(void);
78 
79 /*
80  * Returns the monotonic time elapsed in VM, i.e.,
81  * the time between vm_start and vm_stop
82  */
83 int64_t cpu_get_clock(void);
84 
85 void qemu_timer_notify_cb(void *opaque, QEMUClockType type);
86 
87 /* get the VIRTUAL clock and VM elapsed ticks via the cpus accel interface */
88 int64_t cpus_get_virtual_clock(void);
89 int64_t cpus_get_elapsed_ticks(void);
90 
91 #endif /* SYSEMU_CPU_TIMERS_H */
92