ips.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include <stdio.h>
  17. #include <glib.h>
  18. #include <qemu-plugin.h>
  19. QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
  20. /* how many times do we update time per sec */
  21. #define NUM_TIME_UPDATE_PER_SEC 10
  22. #define NSEC_IN_ONE_SEC (1000 * 1000 * 1000)
  23. static GMutex global_state_lock;
  24. static uint64_t max_insn_per_second = 1000 * 1000 * 1000; /* ips per core, per second */
  25. static uint64_t max_insn_per_quantum; /* trap every N instructions */
  26. static int64_t virtual_time_ns; /* last set virtual time */
  27. static const void *time_handle;
  28. typedef struct {
  29. uint64_t total_insn;
  30. uint64_t quantum_insn; /* insn in last quantum */
  31. int64_t last_quantum_time; /* time when last quantum started */
  32. } vCPUTime;
  33. struct qemu_plugin_scoreboard *vcpus;
  34. /* return epoch time in ns */
  35. static int64_t now_ns(void)
  36. {
  37. return g_get_real_time() * 1000;
  38. }
  39. static uint64_t num_insn_during(int64_t elapsed_ns)
  40. {
  41. double num_secs = elapsed_ns / (double) NSEC_IN_ONE_SEC;
  42. return num_secs * (double) max_insn_per_second;
  43. }
  44. static int64_t time_for_insn(uint64_t num_insn)
  45. {
  46. double num_secs = (double) num_insn / (double) max_insn_per_second;
  47. return num_secs * (double) NSEC_IN_ONE_SEC;
  48. }
  49. static void update_system_time(vCPUTime *vcpu)
  50. {
  51. int64_t elapsed_ns = now_ns() - vcpu->last_quantum_time;
  52. uint64_t max_insn = num_insn_during(elapsed_ns);
  53. if (vcpu->quantum_insn >= max_insn) {
  54. /* this vcpu ran faster than expected, so it has to sleep */
  55. uint64_t insn_advance = vcpu->quantum_insn - max_insn;
  56. uint64_t time_advance_ns = time_for_insn(insn_advance);
  57. int64_t sleep_us = time_advance_ns / 1000;
  58. g_usleep(sleep_us);
  59. }
  60. vcpu->total_insn += vcpu->quantum_insn;
  61. vcpu->quantum_insn = 0;
  62. vcpu->last_quantum_time = now_ns();
  63. /* based on total number of instructions, what should be the new time? */
  64. int64_t new_virtual_time = time_for_insn(vcpu->total_insn);
  65. g_mutex_lock(&global_state_lock);
  66. /* Time only moves forward. Another vcpu might have updated it already. */
  67. if (new_virtual_time > virtual_time_ns) {
  68. qemu_plugin_update_ns(time_handle, new_virtual_time);
  69. virtual_time_ns = new_virtual_time;
  70. }
  71. g_mutex_unlock(&global_state_lock);
  72. }
  73. static void vcpu_init(qemu_plugin_id_t id, unsigned int cpu_index)
  74. {
  75. vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index);
  76. vcpu->total_insn = 0;
  77. vcpu->quantum_insn = 0;
  78. vcpu->last_quantum_time = now_ns();
  79. }
  80. static void vcpu_exit(qemu_plugin_id_t id, unsigned int cpu_index)
  81. {
  82. vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index);
  83. update_system_time(vcpu);
  84. }
  85. static void every_quantum_insn(unsigned int cpu_index, void *udata)
  86. {
  87. vCPUTime *vcpu = qemu_plugin_scoreboard_find(vcpus, cpu_index);
  88. g_assert(vcpu->quantum_insn >= max_insn_per_quantum);
  89. update_system_time(vcpu);
  90. }
  91. static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
  92. {
  93. size_t n_insns = qemu_plugin_tb_n_insns(tb);
  94. qemu_plugin_u64 quantum_insn =
  95. qemu_plugin_scoreboard_u64_in_struct(vcpus, vCPUTime, quantum_insn);
  96. /* count (and eventually trap) once per tb */
  97. qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
  98. tb, QEMU_PLUGIN_INLINE_ADD_U64, quantum_insn, n_insns);
  99. qemu_plugin_register_vcpu_tb_exec_cond_cb(
  100. tb, every_quantum_insn,
  101. QEMU_PLUGIN_CB_NO_REGS, QEMU_PLUGIN_COND_GE,
  102. quantum_insn, max_insn_per_quantum, NULL);
  103. }
  104. static void plugin_exit(qemu_plugin_id_t id, void *udata)
  105. {
  106. qemu_plugin_scoreboard_free(vcpus);
  107. }
  108. QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
  109. const qemu_info_t *info, int argc,
  110. char **argv)
  111. {
  112. for (int i = 0; i < argc; i++) {
  113. char *opt = argv[i];
  114. g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
  115. if (g_strcmp0(tokens[0], "ips") == 0) {
  116. max_insn_per_second = g_ascii_strtoull(tokens[1], NULL, 10);
  117. if (!max_insn_per_second && errno) {
  118. fprintf(stderr, "%s: couldn't parse %s (%s)\n",
  119. __func__, tokens[1], g_strerror(errno));
  120. return -1;
  121. }
  122. } else {
  123. fprintf(stderr, "option parsing failed: %s\n", opt);
  124. return -1;
  125. }
  126. }
  127. vcpus = qemu_plugin_scoreboard_new(sizeof(vCPUTime));
  128. max_insn_per_quantum = max_insn_per_second / NUM_TIME_UPDATE_PER_SEC;
  129. if (max_insn_per_quantum == 0) {
  130. fprintf(stderr, "minimum of %d instructions per second needed\n",
  131. NUM_TIME_UPDATE_PER_SEC);
  132. return -1;
  133. }
  134. time_handle = qemu_plugin_request_time_control();
  135. g_assert(time_handle);
  136. qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
  137. qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
  138. qemu_plugin_register_vcpu_exit_cb(id, vcpu_exit);
  139. qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
  140. return 0;
  141. }