Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "qemu/osdep.h" |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 26 | #include "qemu/thread.h" |
| 27 | #include "hw/core/cpu.h" |
| 28 | #include "qemu/main-loop.h" |
| 29 | #include "sysemu/cpus.h" |
| 30 | #include "sysemu/cpu-throttle.h" |
Denis V. Lunev | 89bccec | 2024-09-05 21:19:41 +0200 | [diff] [blame] | 31 | #include "trace.h" |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 32 | |
| 33 | /* vcpu throttling controls */ |
| 34 | static QEMUTimer *throttle_timer; |
| 35 | static unsigned int throttle_percentage; |
| 36 | |
| 37 | #define CPU_THROTTLE_PCT_MIN 1 |
| 38 | #define CPU_THROTTLE_PCT_MAX 99 |
| 39 | #define CPU_THROTTLE_TIMESLICE_NS 10000000 |
| 40 | |
| 41 | static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) |
| 42 | { |
| 43 | double pct; |
| 44 | double throttle_ratio; |
| 45 | int64_t sleeptime_ns, endtime_ns; |
| 46 | |
| 47 | if (!cpu_throttle_get_percentage()) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | pct = (double)cpu_throttle_get_percentage() / 100; |
| 52 | throttle_ratio = pct / (1 - pct); |
| 53 | /* Add 1ns to fix double's rounding error (like 0.9999999...) */ |
| 54 | sleeptime_ns = (int64_t)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS + 1); |
| 55 | endtime_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + sleeptime_ns; |
| 56 | while (sleeptime_ns > 0 && !cpu->stop) { |
| 57 | if (sleeptime_ns > SCALE_MS) { |
Stefan Hajnoczi | 7c754c7 | 2024-01-02 10:35:27 -0500 | [diff] [blame] | 58 | qemu_cond_timedwait_bql(cpu->halt_cond, |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 59 | sleeptime_ns / SCALE_MS); |
| 60 | } else { |
Stefan Hajnoczi | 195801d | 2024-01-02 10:35:25 -0500 | [diff] [blame] | 61 | bql_unlock(); |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 62 | g_usleep(sleeptime_ns / SCALE_US); |
Stefan Hajnoczi | 195801d | 2024-01-02 10:35:25 -0500 | [diff] [blame] | 63 | bql_lock(); |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 64 | } |
| 65 | sleeptime_ns = endtime_ns - qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
| 66 | } |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 67 | qatomic_set(&cpu->throttle_thread_scheduled, 0); |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void cpu_throttle_timer_tick(void *opaque) |
| 71 | { |
| 72 | CPUState *cpu; |
| 73 | double pct; |
| 74 | |
| 75 | /* Stop the timer if needed */ |
| 76 | if (!cpu_throttle_get_percentage()) { |
| 77 | return; |
| 78 | } |
| 79 | CPU_FOREACH(cpu) { |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 80 | if (!qatomic_xchg(&cpu->throttle_thread_scheduled, 1)) { |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 81 | async_run_on_cpu(cpu, cpu_throttle_thread, |
| 82 | RUN_ON_CPU_NULL); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | pct = (double)cpu_throttle_get_percentage() / 100; |
| 87 | timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) + |
| 88 | CPU_THROTTLE_TIMESLICE_NS / (1 - pct)); |
| 89 | } |
| 90 | |
| 91 | void cpu_throttle_set(int new_throttle_pct) |
| 92 | { |
Utkarsh Tripathi | 33c38f8 | 2020-12-31 13:13:04 +0000 | [diff] [blame] | 93 | /* |
| 94 | * boolean to store whether throttle is already active or not, |
| 95 | * before modifying throttle_percentage |
| 96 | */ |
| 97 | bool throttle_active = cpu_throttle_active(); |
| 98 | |
Denis V. Lunev | 89bccec | 2024-09-05 21:19:41 +0200 | [diff] [blame] | 99 | trace_cpu_throttle_set(new_throttle_pct); |
| 100 | |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 101 | /* Ensure throttle percentage is within valid range */ |
| 102 | new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX); |
| 103 | new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN); |
| 104 | |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 105 | qatomic_set(&throttle_percentage, new_throttle_pct); |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 106 | |
Utkarsh Tripathi | 33c38f8 | 2020-12-31 13:13:04 +0000 | [diff] [blame] | 107 | if (!throttle_active) { |
| 108 | cpu_throttle_timer_tick(NULL); |
| 109 | } |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void cpu_throttle_stop(void) |
| 113 | { |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 114 | qatomic_set(&throttle_percentage, 0); |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | bool cpu_throttle_active(void) |
| 118 | { |
| 119 | return (cpu_throttle_get_percentage() != 0); |
| 120 | } |
| 121 | |
| 122 | int cpu_throttle_get_percentage(void) |
| 123 | { |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 124 | return qatomic_read(&throttle_percentage); |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void cpu_throttle_init(void) |
| 128 | { |
| 129 | throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT, |
| 130 | cpu_throttle_timer_tick, NULL); |
| 131 | } |