xref: /openbmc/qemu/contrib/plugins/ips.c (revision 29318db1)
1 /*
2  * Instructions Per Second (IPS) rate limiting plugin.
3  *
4  * This plugin can be used to restrict the execution of a system to a
5  * particular number of Instructions Per Second (IPS). This controls
6  * time as seen by the guest so while wall-clock time may be longer
7  * from the guests point of view time will pass at the normal rate.
8  *
9  * This uses the new plugin API which allows the plugin to control
10  * system time.
11  *
12  * Copyright (c) 2023 Linaro Ltd
13  *
14  * SPDX-License-Identifier: GPL-2.0-or-later
15  */
16 
17 #include <stdio.h>
18 #include <glib.h>
19 #include <qemu-plugin.h>
20 
21 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
22 
23 /* how many times do we update time per sec */
24 #define NUM_TIME_UPDATE_PER_SEC 10
25 #define NSEC_IN_ONE_SEC (1000 * 1000 * 1000)
26 
27 static GMutex global_state_lock;
28 
29 static uint64_t max_insn_per_second = 1000 * 1000 * 1000; /* ips per core, per second */
30 static uint64_t max_insn_per_quantum; /* trap every N instructions */
31 static int64_t virtual_time_ns; /* last set virtual time */
32 
33 static const void *time_handle;
34 
35 typedef struct {
36     uint64_t total_insn;
37     uint64_t quantum_insn; /* insn in last quantum */
38     int64_t last_quantum_time; /* time when last quantum started */
39 } vCPUTime;
40 
41 struct qemu_plugin_scoreboard *vcpus;
42 
43 /* return epoch time in ns */
44 static int64_t now_ns(void)
45 {
46     return g_get_real_time() * 1000;
47 }
48 
49 static uint64_t num_insn_during(int64_t elapsed_ns)
50 {
51     double num_secs = elapsed_ns / (double) NSEC_IN_ONE_SEC;
52     return num_secs * (double) max_insn_per_second;
53 }
54 
55 static int64_t time_for_insn(uint64_t num_insn)
56 {
57     double num_secs = (double) num_insn / (double) max_insn_per_second;
58     return num_secs * (double) NSEC_IN_ONE_SEC;
59 }
60 
61 static void update_system_time(vCPUTime *vcpu)
62 {
63     int64_t elapsed_ns = now_ns() - vcpu->last_quantum_time;
64     uint64_t max_insn = num_insn_during(elapsed_ns);
65 
66     if (vcpu->quantum_insn >= max_insn) {
67         /* this vcpu ran faster than expected, so it has to sleep */
68         uint64_t insn_advance = vcpu->quantum_insn - max_insn;
69         uint64_t time_advance_ns = time_for_insn(insn_advance);
70         int64_t sleep_us = time_advance_ns / 1000;
71         g_usleep(sleep_us);
72     }
73 
74     vcpu->total_insn += vcpu->quantum_insn;
75     vcpu->quantum_insn = 0;
76     vcpu->last_quantum_time = now_ns();
77 
78     /* based on total number of instructions, what should be the new time? */
79     int64_t new_virtual_time = time_for_insn(vcpu->total_insn);
80 
81     g_mutex_lock(&global_state_lock);
82 
83     /* Time only moves forward. Another vcpu might have updated it already. */
84     if (new_virtual_time > virtual_time_ns) {
85         qemu_plugin_update_ns(time_handle, new_virtual_time);
86         virtual_time_ns = new_virtual_time;
87     }
88 
89     g_mutex_unlock(&global_state_lock);
90 }
91 
92 static void vcpu_init(qemu_plugin_id_t id, unsigned int cpu_index)
93 {
94     vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index);
95     vcpu->total_insn = 0;
96     vcpu->quantum_insn = 0;
97     vcpu->last_quantum_time = now_ns();
98 }
99 
100 static void vcpu_exit(qemu_plugin_id_t id, unsigned int cpu_index)
101 {
102     vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index);
103     update_system_time(vcpu);
104 }
105 
106 static void every_quantum_insn(unsigned int cpu_index, void *udata)
107 {
108     vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index);
109     g_assert(vcpu->quantum_insn >= max_insn_per_quantum);
110     update_system_time(vcpu);
111 }
112 
113 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
114 {
115     size_t n_insns = qemu_plugin_tb_n_insns(tb);
116     qemu_plugin_u64 quantum_insn =
117         qemu_plugin_scoreboard_u64_in_struct(vcpus, vCPUTime, quantum_insn);
118     /* count (and eventually trap) once per tb */
119     qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
120         tb, QEMU_PLUGIN_INLINE_ADD_U64, quantum_insn, n_insns);
121     qemu_plugin_register_vcpu_tb_exec_cond_cb(
122         tb, every_quantum_insn,
123         QEMU_PLUGIN_CB_NO_REGS, QEMU_PLUGIN_COND_GE,
124         quantum_insn, max_insn_per_quantum, NULL);
125 }
126 
127 static void plugin_exit(qemu_plugin_id_t id, void *udata)
128 {
129     qemu_plugin_scoreboard_free(vcpus);
130 }
131 
132 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
133                                            const qemu_info_t *info, int argc,
134                                            char **argv)
135 {
136     for (int i = 0; i < argc; i++) {
137         char *opt = argv[i];
138         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
139         if (g_strcmp0(tokens[0], "ips") == 0) {
140             max_insn_per_second = g_ascii_strtoull(tokens[1], NULL, 10);
141             if (!max_insn_per_second && errno) {
142                 fprintf(stderr, "%s: couldn't parse %s (%s)\n",
143                         __func__, tokens[1], g_strerror(errno));
144                 return -1;
145             }
146         } else {
147             fprintf(stderr, "option parsing failed: %s\n", opt);
148             return -1;
149         }
150     }
151 
152     vcpus = qemu_plugin_scoreboard_new(sizeof(vCPUTime));
153     max_insn_per_quantum = max_insn_per_second / NUM_TIME_UPDATE_PER_SEC;
154 
155     time_handle = qemu_plugin_request_time_control();
156     g_assert(time_handle);
157 
158     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
159     qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
160     qemu_plugin_register_vcpu_exit_cb(id, vcpu_exit);
161     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
162 
163     return 0;
164 }
165