xref: /qemu/contrib/plugins/ips.c (revision 9761ad5f65d23f080b5a3479e52196fbce2e1506)
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 typedef struct {
133     const char *suffix;
134     unsigned long multipler;
135 } ScaleEntry;
136 
137 /* a bit like units.h but not binary */
138 static const ScaleEntry scales[] = {
139     { "k", 1000 },
140     { "m", 1000 * 1000 },
141     { "g", 1000 * 1000 * 1000 },
142 };
143 
144 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
145                                            const qemu_info_t *info, int argc,
146                                            char **argv)
147 {
148     bool ipq_set = false;
149 
150     for (int i = 0; i < argc; i++) {
151         char *opt = argv[i];
152         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
153         if (g_strcmp0(tokens[0], "ips") == 0) {
154             char *endptr = NULL;
155             max_insn_per_second = g_ascii_strtoull(tokens[1], &endptr, 10);
156             if (!max_insn_per_second && errno) {
157                 fprintf(stderr, "%s: couldn't parse %s (%s)\n",
158                         __func__, tokens[1], g_strerror(errno));
159                 return -1;
160             }
161 
162             if (endptr && *endptr != 0) {
163                 g_autofree gchar *lower = g_utf8_strdown(endptr, -1);
164                 unsigned long scale = 0;
165 
166                 for (int j = 0; j < G_N_ELEMENTS(scales); j++) {
167                     if (g_strcmp0(lower, scales[j].suffix) == 0) {
168                         scale = scales[j].multipler;
169                         break;
170                     }
171                 }
172 
173                 if (scale) {
174                     max_insn_per_second *= scale;
175                 } else {
176                     fprintf(stderr, "bad suffix: %s\n", endptr);
177                     return -1;
178                 }
179             }
180         } else if (g_strcmp0(tokens[0], "ipq") == 0) {
181             max_insn_per_quantum = g_ascii_strtoull(tokens[1], NULL, 10);
182 
183             if (!max_insn_per_quantum) {
184                 fprintf(stderr, "bad ipq value: %s\n", tokens[0]);
185                 return -1;
186             }
187             ipq_set = true;
188         } else {
189             fprintf(stderr, "option parsing failed: %s\n", opt);
190             return -1;
191         }
192     }
193 
194     vcpus = qemu_plugin_scoreboard_new(sizeof(vCPUTime));
195 
196     if (!ipq_set) {
197         max_insn_per_quantum = max_insn_per_second / NUM_TIME_UPDATE_PER_SEC;
198     }
199 
200     if (max_insn_per_quantum == 0) {
201         fprintf(stderr, "minimum of %d instructions per second needed\n",
202                 NUM_TIME_UPDATE_PER_SEC);
203         return -1;
204     }
205 
206     time_handle = qemu_plugin_request_time_control();
207     g_assert(time_handle);
208 
209     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
210     qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
211     qemu_plugin_register_vcpu_exit_cb(id, vcpu_exit);
212     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
213 
214     return 0;
215 }
216