xref: /qemu/include/exec/icount.h (revision fc524567087c2537b5103cdfc1d41e4f442892b6)
1 /*
2  * icount - Instruction Counter API
3  * CPU timers state API
4  *
5  * Copyright 2020 SUSE LLC
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #ifndef EXEC_ICOUNT_H
10 #define EXEC_ICOUNT_H
11 
12 /**
13  * ICountMode: icount enablement state:
14  *
15  * @ICOUNT_DISABLED: Disabled - Do not count executed instructions.
16  * @ICOUNT_PRECISE: Enabled - Fixed conversion of insn to ns via "shift" option
17  * @ICOUNT_ADAPTATIVE: Enabled - Runtime adaptive algorithm to compute shift
18  */
19 typedef enum {
20     ICOUNT_DISABLED = 0,
21     ICOUNT_PRECISE,
22     ICOUNT_ADAPTATIVE,
23 } ICountMode;
24 
25 #ifdef CONFIG_TCG
26 extern ICountMode use_icount;
27 #define icount_enabled() (use_icount)
28 #else
29 #define icount_enabled() ICOUNT_DISABLED
30 #endif
31 
32 /* Protect the CONFIG_USER_ONLY test vs poisoning. */
33 #if defined(COMPILING_PER_TARGET) || defined(COMPILING_SYSTEM_VS_USER)
34 # ifdef CONFIG_USER_ONLY
35 #  undef  icount_enabled
36 #  define icount_enabled() ICOUNT_DISABLED
37 # endif
38 #endif
39 
40 /*
41  * Update the icount with the executed instructions. Called by
42  * cpus-tcg vCPU thread so the main-loop can see time has moved forward.
43  */
44 void icount_update(CPUState *cpu);
45 
46 /* get raw icount value */
47 int64_t icount_get_raw(void);
48 
49 /* return the virtual CPU time in ns, based on the instruction counter. */
50 int64_t icount_get(void);
51 /*
52  * convert an instruction counter value to ns, based on the icount shift.
53  * This shift is set as a fixed value with the icount "shift" option
54  * (precise mode), or it is constantly approximated and corrected at
55  * runtime in adaptive mode.
56  */
57 int64_t icount_to_ns(int64_t icount);
58 
59 /**
60  * icount_configure: configure the icount options, including "shift"
61  * @opts: Options to parse
62  * @errp: pointer to a NULL-initialized error object
63  *
64  * Return: true on success, else false setting @errp with error
65  */
66 bool icount_configure(QemuOpts *opts, Error **errp);
67 
68 /* used by tcg vcpu thread to calc icount budget */
69 int64_t icount_round(int64_t count);
70 
71 /* if the CPUs are idle, start accounting real time to virtual clock. */
72 void icount_start_warp_timer(void);
73 void icount_account_warp_timer(void);
74 void icount_notify_exit(void);
75 
76 #endif /* EXEC_ICOUNT_H */
77