Utilization rate determination method and device of virtual machine processor, equipment and storage medium
Periodic sampling is performed by host clock interrupts to obtain the usage rate of the virtual machine processor, solving the problem of low virtual machine monitoring accuracy in the existing technology, and achieving high-precision virtual machine CPU usage rate calculation.
Patent Information
- Application Number
- CN202510401904.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-01
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2045-04-01
AI Technical Summary
The existing virtual machine monitoring technology cannot directly obtain the usage rate of virtual machine processors, resulting in large latency and low accuracy of monitoring data, especially in high load and multi-core processor environments, which is difficult to accurately obtain the usage rate of virtual machine CPUs.
By using the host's clock interrupt for periodic sampling, the sampling period of the virtual machine processor is determined, the running state count value and clock cycle number of the host processor are obtained, and the usage rate of the virtual machine processor is calculated based on the data of multiple sampling periods.
It realizes accurate acquisition of the virtual machine processor usage, can more accurately reflect the real load of the virtual machine, reduces software-level estimation errors, reduces system overhead, and does not affect the performance and stability of the virtual machine.
Smart Images

Figure CN120407078A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of cloud computing virtualization, and particularly to a method, device, equipment, and storage medium for determining the usage rate of a virtual machine processor. Background Art
[0002] With the development of cloud computing and virtualization technologies, efficient management of virtual resources has become increasingly important. Existing virtual machine monitoring technologies cannot directly obtain the usage rate of a virtual machine processor (vCPU) from the host machine. Some solutions rely on the para-virtualization features provided by the virtualization layer, which require specific modifications to the virtual machine kernel, or estimate the CPU occupancy rate by observing vCPU threads through the host operating system. However, these methods have problems such as large monitoring data latency and low accuracy. Especially in high-load and multi-core processor environments, there is an urgent need for a method that can accurately obtain the CPU usage rate in a virtual machine. Summary of the Invention
[0003] Based on this, in view of the above technical problems, it is necessary to provide a method, device, computer equipment, computer-readable storage medium, and computer program product for determining the usage rate of a virtual machine processor, which can accurately obtain the usage rate of the virtual machine processor.
[0004] In a first aspect, the present application provides a method for determining the usage rate of a virtual machine processor, including:
[0005] Taking the time interval during which a virtual machine processor trap occurs continuously for a preset number of times as a sampling period;
[0006] For each sampling period, obtaining a first count value of the host processor in the running state and a first number of clock cycles of the host processor within the sampling period;
[0007] Determining a scheduling time period of the virtual machine processor within the sampling period, and obtaining a second count value of the host processor in the running state and a second number of clock cycles of the host processor within the scheduling time period;
[0008] Calculating the usage rate of the virtual machine processor based on the first count value, the first number of clock cycles, the second count value, and the second number of clock cycles within multiple sampling periods.
[0009] In one embodiment, obtaining a first count value of the host processor in the running state and a first number of clock cycles of the host processor within the sampling period includes:
[0010] At the start time of the sampling period, obtaining the count value of the host processor in the running state and the number of clock cycles of the host processor since startup;
[0011] At the termination moment of the sampling period, obtain the count value of the host processor in the running state and the number of clock cycles of the host processor since startup;
[0012] Take the difference between the count value at the termination moment of the sampling period and the count value at the corresponding start moment as the first count value of the host processor in the running state within the sampling period;
[0013] Take the difference between the number of clock cycles at the termination moment of the sampling period and the number of clock cycles at the corresponding start moment as the first number of clock cycles of the host processor within the sampling period.
[0014] In one embodiment, obtaining the count value of the host processor in the running state and the number of clock cycles of the host processor since startup includes:
[0015] Use privileged instructions to read the count value of the host processor in the running state from the first register and read the number of clock cycles of the host processor since startup from the second register.
[0016] In one embodiment, obtaining the second count value of the host processor in the running state and the second number of clock cycles of the host processor within the scheduling time period includes:
[0017] At the start moment of the scheduling time period, obtain the count value of the host processor in the running state and the number of clock cycles of the host processor since startup;
[0018] At the termination moment of the scheduling time period, obtain the count value of the host processor in the running state and the number of clock cycles of the host processor since startup;
[0019] Take the difference between the count value at the termination moment of the scheduling time period and the count value at the corresponding start moment as the second count value of the host processor in the running state within the scheduling time period;
[0020] Take the difference between the number of clock cycles at the termination moment of the scheduling time period and the number of clock cycles at the corresponding start moment as the second number of clock cycles of the host processor within the scheduling time period.
[0021] In one embodiment, based on the first count values, the first number of clock cycles, the second count values, and the second number of clock cycles within multiple sampling periods, calculate the utilization rate of the virtual machine processor, including:
[0022] Sum the first count values within multiple sampling periods, subtract the sum of the second count values within multiple sampling periods, to obtain the count value of the virtual machine processor in the running state;
[0023] Subtract the sum of the number of first clock cycles within multiple sampling periods from the sum of the number of second clock cycles within multiple sampling periods to obtain the number of clock cycles of the virtual machine processor;
[0024] Take the quotient between the count value when the virtual machine processor is in the running state and the number of clock cycles of the virtual machine processor as the utilization rate of the virtual machine processor.
[0025] In one embodiment, the method for determining the utilization rate of the virtual machine processor further includes:
[0026] Output the utilization rate of the virtual machine processor to a preset path.
[0027] In a second aspect, the present application further provides a device for determining the utilization rate of a virtual machine processor, including:
[0028] A determination module, configured to use the time interval during which the virtual machine processor trap occurs continuously for a preset number of times as the sampling period;
[0029] A first acquisition module, configured to, for each sampling period, acquire a first count value of the host processor in the running state and the first number of clock cycles of the host processor within the sampling period;
[0030] A second acquisition module, configured to determine the scheduling time period of the virtual machine processor within the sampling period, and acquire a second count value of the host processor in the running state and the second number of clock cycles of the host processor during the scheduling time period;
[0031] A calculation module, configured to calculate the utilization rate of the virtual machine processor based on the first count value, the first number of clock cycles, the second count value, and the second number of clock cycles within multiple sampling periods.
[0032] In a third aspect, the present application further provides a computer device, including a memory and a processor, where the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0033] Use the time interval during which the virtual machine processor trap occurs continuously for a preset number of times as the sampling period;
[0034] For each sampling period, acquire a first count value of the host processor in the running state and the first number of clock cycles of the host processor within the sampling period;
[0035] Determine the scheduling time period of the virtual machine processor within the sampling period, and acquire a second count value of the host processor in the running state and the second number of clock cycles of the host processor during the scheduling time period;
[0036] Calculate the usage rate of the virtual machine processor based on the first count value, the first number of clock cycles, the second count value, and the second number of clock cycles within multiple sampling periods.
[0037] In a fourth aspect, the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the following steps are implemented:
[0038] Use the time interval during which the virtual machine processor trap occurs continuously for a preset number of times as the sampling period;
[0039] For each sampling period, obtain the first count value of the host processor in the running state and the first number of clock cycles of the host processor within the sampling period;
[0040] Determine the scheduling time period of the virtual machine processor within the sampling period, and obtain the second count value of the host processor in the running state and the second number of clock cycles of the host processor during the scheduling time period;
[0041] Calculate the usage rate of the virtual machine processor based on the first count value, the first number of clock cycles, the second count value, and the second number of clock cycles within multiple sampling periods.
[0042] In a fifth aspect, the present application further provides a computer program product, including a computer program. When the computer program is executed by a processor, the following steps are implemented:
[0043] Use the time interval during which the virtual machine processor trap occurs continuously for a preset number of times as the sampling period;
[0044] For each sampling period, obtain the first count value of the host processor in the running state and the first number of clock cycles of the host processor within the sampling period;
[0045] Determine the scheduling time period of the virtual machine processor within the sampling period, and obtain the second count value of the host processor in the running state and the second number of clock cycles of the host processor during the scheduling time period;
[0046] Calculate the usage rate of the virtual machine processor based on the first count value, the first number of clock cycles, the second count value, and the second number of clock cycles within multiple sampling periods.
[0047] The method, apparatus, computer device, computer-readable storage medium, and computer program product for determining the utilization rate of a virtual machine processor. Since clock interrupts on the host machine can cause traps in the virtual machine processor, by using the time interval during which a preset number of consecutive virtual machine processor traps occur as the sampling period, this method of periodic sampling using the host machine's clock interrupt is beneficial for the calculated utilization rate of the virtual machine processor to more accurately reflect the true load of the virtual machine. The first count value reflects the duration during which the host processor is in the running state within the sampling period, and the second clock cycle count reflects the duration during which the host processor is started within the sampling period. During the scheduling time period, the virtual machine processor is scheduled and used to execute other tasks. Therefore, based on the first count value, the first clock cycle count, the second count value, and the second clock cycle count within the sampling period, the utilization rate of the virtual machine processor can be accurately obtained. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] To more clearly illustrate the technical solutions in the embodiments of the present application or the related art, the following briefly introduces the drawings required for use in the description of the embodiments of the present application or the related art. Obviously, the drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other related drawings can also be obtained based on these drawings.
[0049] Figure 1 It is an application environment diagram of the method for determining the utilization rate of a virtual machine processor in an embodiment;
[0050] Figure 2 It is a flowchart of the method for determining the utilization rate of a virtual machine processor in an embodiment;
[0051] Figure 3 It is a schematic diagram of data interaction between a virtual machine and a host machine in an embodiment;
[0052] Figure 4 It is a structural block diagram of the device for determining the utilization rate of a virtual machine processor in an embodiment;
[0053] Figure 5 It is an internal structure diagram of a computer device in an embodiment. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0054] To make the objectives, technical solutions, and advantages of the present application clearer, the following further elaborates on the present application in conjunction with the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0055] The method for determining the utilization rate of a virtual machine processor provided by the embodiments of the present application can be applied to, for example Figure 1In the application environment shown. Among them, the terminal 102 communicates with the server 104 through the network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or can be placed on the cloud or other network servers. In this embodiment, an example is given where this method is applied to the terminal 102. It can be understood that this method can also be applied to the server, and can also be applied to a system including a terminal and a server, and is implemented through the interaction between the terminal and the server. The terminal 102 uses the time interval during which virtual machine processor traps occur continuously for a preset number of times as the sampling period; for each sampling period, obtain the first count value of the host processor in the running state and the first clock cycle number of the host processor within the sampling period; determine the scheduling time period of the virtual machine processor within the sampling period, and obtain the second count value of the host processor in the running state and the second clock cycle number of the host processor within the scheduling time period; calculate the utilization rate of the virtual machine processor based on the first count value, the first clock cycle number, the second count value, and the second clock cycle number within multiple sampling periods. Among them, the terminal 102 can be, but is not limited to, various personal computers, laptop computers, smart phones, tablet computers, Internet of Things devices, and portable wearable devices. The Internet of Things devices can be smart speakers, smart TVs, smart air conditioners, smart in-vehicle devices, projection devices, etc. The portable wearable devices can be smart watches, smart bracelets, head-mounted devices, etc. The head-mounted devices can be virtual reality (VR) devices, augmented reality (AR) devices, smart glasses, etc. The server 104 can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services.
[0056] In an exemplary embodiment, as Figure 2 shown, a method for determining the utilization rate of a virtual machine processor is provided. Taking this method applied to Figure 1 the terminal 102 in
[0057] Step 202, use the time interval during which virtual machine processor traps occur continuously for a preset number of times as the sampling period.
[0058] Among them, the virtual machine processor, that is, the virtual central processing unit (vCPU), refers to a logically simulated CPU core in virtualization technology.
[0059] VM Exit refers to the process in a virtualized environment where, when a virtual machine (VM) encounters a specific event or instruction, the execution of the current virtual machine needs to be paused and control is transferred to the virtual machine monitor (VMM). The virtual machine monitor is a middleware that connects the host machine and the virtual machines. The virtual machine monitor runs on the host machine, manages and allocates hardware resources to each virtual machine. Each virtual machine can run on the same host machine, and each virtual machine has its own private virtualized hardware, including CPU, memory, network interface, and disk, so that the operating system and application programs can be installed and run independently.
[0060] Since the clock interrupt on the host machine can cause a VM Exit of the vCPU, the clock interrupt of the host machine is used for periodic sampling. Every preset number of VM Exits (e.g., 1000 times), a sampling is triggered, and the data within the sampling period is recorded to calculate the virtual machine processor usage rate within multiple sampling periods.
[0061] In some embodiments, within the time period between two adjacent VM Exits of the virtual machine processor, the timestamp of entering the non-privileged mode can be determined. The timestamp of entering the non-privileged mode is used as the sampling start time, the timestamp of the subsequent VM Exit of the virtual machine processor is used as the sampling end time, and the time interval between the sampling start time and the sampling end time is used as a sampling period, or the time intervals between multiple consecutive sampling start times and sampling end times are used as a sampling period.
[0062] Since the virtual machine processor will be in the privileged mode (root mode) and non-privileged mode (non-root mode) at intervals during the running state, this is mainly to isolate the operation permissions of the vCPU. As Figure 3 shown in the schematic diagram of data interaction between the virtual machine and the host machine. Among them, the virtual machine runs in the non-root mode. When a VM Exit occurs, it will enter the root mode, and when a trap (VM Entry) occurs, it will re-enter the non-root mode. When the vCPU is in the non-root mode, the relevant task code in the virtual machine is executed, and a VM Exit will occur when performing certain privileged operations and exit to the root mode to execute the relevant task code on the host machine. Therefore, the sampling code is placed in the time interval between the vCPU exiting to the host machine due to a VM Exit and re-entering the non-root mode and the next VM Exit. In this way, when counting the sampling period, only the time of the vCPU in the non-root mode is calculated, excluding the time-consuming caused by executing virtualization layer instructions in the root mode, which can make the finally obtained CPU usage rate of the virtual machine more accurately reflect the real load of the virtual machine.
[0063] Step 204, for each sampling period, obtain the first count value of the host processor in the running state within the sampling period and the first number of clock cycles of the host processor.
[0064] Among them, the host refers to the physical host running the virtual machine. The host processor refers to the processor in the host. The state of the host processor includes the running state and the idle state. The running state refers to the state where the host processor processes a process or a program, and the idle state is the opposite, referring to the state of waiting to process a process or a program.
[0065] The first count value refers to the count value that the host processor increments by one every clock cycle according to the current actual operating frequency of the CPU hardware when it is in the running state within the sampling period.
[0066] The running state refers to the active state of the CPU. For example, the C0 state, that is, the state where no instructions such as HLT and MWAIT that may enter the energy-saving sleep mode are executed. The first count value is used to characterize the activity level of the host processor within the sampling period.
[0067] The first number of clock cycles refers to the number of cycles that the host processor is in the startup state within the sampling period. The first number of clock cycles provides an accurate time reference for the calculations used by the CPU.
[0068] In some embodiments, within the sampling period, the ratio between the first count value and the first number of clock cycles is the utilization rate of the host processor within the sampling period.
[0069] Step 206, within the sampling period, determine the scheduling time period of the virtual machine processor, and obtain the second count value of the host processor in the running state and the second number of clock cycles of the host processor during the scheduling time period.
[0070] Among them, the scheduling time period refers to the time period during which the virtual machine processor is scheduled to execute other tasks within the sampling period. During the operation of the virtual machine, the thread corresponding to the virtual machine processor will also be under the scheduling management of the host processor, and thus be in the running state and the idle state.
[0071] The second count value refers to the count value of the host processor in the running state during the scheduling time period, that is, the count value that the host processor increments by one every clock cycle according to the current actual operating frequency of the CPU hardware when it is in the running state during the scheduling time period. Since the virtual machine processor is scheduled out during the scheduling time period, the second count value is used to characterize the activity level of the non-virtual machine processor during the scheduling time period.
[0072] The number of second clock cycles refers to the number of cycles in which the host processor is in the startup state during the scheduling time period, indicating the number of cycles in which the non-virtual machine processor is in the startup state. The number of second clock cycles provides an accurate time reference for the calculations used by the CPU.
[0073] Step 208: Calculate the usage rate of the virtual machine processor based on the first count value, the number of first clock cycles, the second count value, and the number of second clock cycles within multiple sampling periods.
[0074] Among them, the count value of the virtual machine processor in the running state within the sampling period is obtained by subtracting the count value of the non-virtual machine processor within the scheduling time period from the count value of the host processor in the running state within each sampling period. [[ID=⑦]]
[0075] The number of clock cycles in which the virtual machine processor starts within the sampling period is obtained by subtracting the number of second clock cycles of the non-virtual machine processor within the scheduling time period from the number of first clock cycles of the host processor within each sampling period.
[0076] The usage rate of the virtual machine processor is equal to the ratio between the count value of the virtual machine processor in the running state within the sampling period and the number of clock cycles of the host processor within the sampling period.
[0077] In the above method for determining the usage rate of the virtual machine processor, since the clock interrupt on the host machine can cause the virtual machine processor to trap, therefore, by using the time interval during which the virtual machine processor traps continuously for a preset number of times as the sampling period, this method of using the clock interrupt on the host machine for periodic sampling is beneficial for the calculated usage rate of the virtual machine processor to more accurately reflect the true load of the virtual machine; the first count value reflects the duration of the host processor in the running state within the sampling period, and the number of second clock cycles reflects the duration of the host processor in the startup state within the sampling period; within the scheduling time period, the virtual machine processor is scheduled and used to execute other tasks, therefore, based on the first count value, the number of first clock cycles, the second count value, and the number of second clock cycles within the sampling period, the usage rate of the virtual machine processor can be accurately obtained.
[0078] In an exemplary embodiment, obtaining a first count value of the host processor in a running state and a first number of clock cycles of the host processor within a sampling period includes: at the start moment of the sampling period, obtaining the count value of the host processor in a running state and the number of clock cycles of the host processor since startup; at the end moment of the sampling period, obtaining the count value of the host processor in a running state and the number of clock cycles of the host processor since startup; using the difference between the count value at the end moment of the sampling period and the count value at the corresponding start moment as the first count value of the host processor in a running state within the sampling period; and using the difference between the number of clock cycles at the end moment of the sampling period and the number of clock cycles at the corresponding start moment as the first number of clock cycles of the host processor within the sampling period.
[0079] Wherein, when the sampling period includes the time interval between two consecutive virtual machine traps, the moment of entering the non-privileged mode within the time interval is used as the start moment of the sampling period, and the moment of the subsequent virtual machine trap within the time interval is used as the end moment of the sampling period. When the sampling period includes multiple time intervals between two consecutive virtual machine traps, the moment of entering the non-privileged mode within the first time interval is used as the start moment of the sampling period, and the moment of the subsequent virtual machine trap within the last time interval is used as the end moment of the sampling period.
[0080] The difference between the count value at the end moment of the sampling period and the count value at the corresponding start moment indicates the first count value of the host processor in a running state within the sampling period.
[0081] The difference between the number of clock cycles at the end moment of the sampling period and the number of clock cycles at the corresponding start moment indicates the first number of clock cycles of the host processor within the sampling period.
[0082] In this embodiment, calculating the first count value of the host processor in a running state and the first number of clock cycles of the host processor according to the count values and the number of clock cycles at the start moment and the end moment within the sampling period. Since each time interval within each sampling period only includes the time of the non-privileged mode, the calculated first count value and the first number of clock cycles can accurately reflect the true load condition of the host processor, which is beneficial to obtaining an accurate virtual machine CPU usage rate.
[0083] In an exemplary embodiment, obtaining the count value of the host processor in a running state and the number of clock cycles of the host processor since startup includes: using a privileged instruction to read the count value of the host processor in a running state from a first register, and reading the number of clock cycles of the host processor since startup from a second register.
[0084] Among them, the first register stores the count value of the host processor that has been in the running state since startup until the current moment. The count value is incremented by one every clock cycle according to the actual current operating frequency of the CPU hardware when the host processor is in the running state. For example, the first register can be the APERF register.
[0085] The second register stores the number of clock cycles of the host processor since startup until the current moment. For example, the second register can be the TSC register.
[0086] The values of the first register and the second register both need to be read on the host machine using privileged instructions. Privileged instructions refer to instructions executed in privileged mode. For example, the privileged instruction can be the rdmsr instruction.
[0087] In this embodiment, by using privileged instructions to read the count value of the host processor in the running state from the first register and the number of clock cycles of the host processor since startup from the second register, it is beneficial to record the corresponding data in privileged mode for accurately calculating the CPU usage rate of the virtual machine.
[0088] In an exemplary embodiment, obtaining the second count value of the host processor in the running state and the second number of clock cycles of the host processor during the scheduling period includes: at the start time of the scheduling period, obtaining the count value of the host processor in the running state and the number of clock cycles of the host processor since startup; at the end time of the scheduling period, obtaining the count value of the host processor in the running state and the number of clock cycles of the host processor since startup; using the difference between the count value at the end time of the scheduling period and the count value at the corresponding start time as the second count value of the host processor in the running state during the scheduling period; using the difference between the number of clock cycles at the end time of the scheduling period and the number of clock cycles at the corresponding start time as the second number of clock cycles of the host processor during the scheduling period.
[0089] Among them, the start time of the scheduling period refers to the moment when the virtual machine monitor in the host machine schedules out the virtual machine processor and executes other tasks. For example, the kvm_sched_out() function in the KVM (Kernel-Based Virtual Machine, virtualization technology based on the Linux kernel) module can be used. In the kvm_sched_out() function, by reading the count value in the first register, the count value of the host processor in the running state from startup until the start time of the scheduling period can be obtained, and by reading the number of clock cycles in the second register, the number of clock cycles of the host processor from startup until the start time of the scheduling period can be obtained.
[0090] The termination moment of the scheduling time period refers to the moment when the virtual machine monitor in the host machine schedules the virtual machine processor back and executes the tasks in this virtual machine. For example, the kvm_sched_in() function in the KVM module can be used. In the kvm_sched_in() function, by reading the count value in the first register, the count value of the host processor in the running state from the start-up to the termination moment of the scheduling time period can be obtained. By reading the number of clock cycles in the second register, the number of clock cycles of the host processor from the start-up to the termination moment of the scheduling time period can be obtained.
[0091] The difference between the count value at the termination moment of the scheduling time period and the count value at the start moment of the scheduling time period indicates the second count value of the host processor in the running state within the scheduling time period.
[0092] The difference between the number of clock cycles at the termination moment of the scheduling time period and the number of clock cycles at the corresponding start moment indicates the second number of clock cycles of the host processor within the scheduling time period.
[0093] In this embodiment, by the count values and the number of clock cycles at the start moment and the termination moment of the scheduling time period, the second count value of the host processor in the running state within the scheduling time period and the second number of clock cycles of the host processor are calculated, which is equivalent to calculating the activity of the non-virtual machine processor and is beneficial to calculating the accurate usage rate of the virtual machine processor.
[0094] In an exemplary embodiment, based on the first count values, the first number of clock cycles, the second count values, and the second number of clock cycles within multiple sampling periods, calculating the usage rate of the virtual machine processor includes: subtracting the sum of the second count values within multiple sampling periods from the sum of the first count values within multiple sampling periods to obtain the count value of the virtual machine processor in the running state; subtracting the sum of the second number of clock cycles within multiple sampling periods from the sum of the first number of clock cycles within multiple sampling periods to obtain the number of clock cycles of the virtual machine processor; taking the quotient between the count value of the virtual machine processor in the running state and the number of clock cycles of the virtual machine processor as the usage rate of the virtual machine processor.
[0095] Among them, for the usage rate of the virtual machine processor within multiple sampling periods, the sum of the first count values within multiple sampling periods can be obtained, and the result is used as the total count value of the host processor in the running state within multiple sampling periods; the sum of the second count values within multiple sampling periods can be obtained, and the result is used as the count value of the non-virtual machine processor in the running state within multiple sampling periods; the difference between the two is obtained, and the result is used as the count value of the virtual machine processor in the running state.
[0096] The sum of the number of first clock cycles within multiple sampling periods is used as the total number of clock cycles started by the host processor within multiple sampling periods. The sum of the number of clock cycles within multiple sampling periods is used as the number of clock cycles started by the non-virtual machine processor within multiple sampling periods. The difference between the two is obtained, and the result is used as the number of clock cycles started by the virtual machine processor.
[0097] The quotient of the count value when the virtual machine processor is in the running state and the number of clock cycles of the virtual machine processor is obtained, and the result is used as the utilization rate of the virtual machine processor.
[0098] In this embodiment, the sum of the first count values within multiple sampling periods is subtracted from the sum of the second count values within multiple sampling periods to obtain the count value when the virtual machine processor is in the running state. The sum of the first clock cycle numbers within multiple sampling periods is subtracted from the sum of the second clock cycle numbers within multiple sampling periods to obtain the number of clock cycles of the virtual machine processor. Taking the quotient of the two can obtain the accurate utilization rate of the virtual machine processor.
[0099] In an exemplary embodiment, the method for determining the utilization rate of the virtual machine processor further includes: outputting the utilization rate of the virtual machine processor to a preset path.
[0100] Wherein, the preset path is the path for saving the utilization rate of the virtual machine processor. Exemplarily, a debugfs interface is registered under the path / sys / kernel / debug / kvm for outputting the vCPU utilization rate.
[0101] In this embodiment, by outputting the utilization rate of the virtual machine processor to the preset path, it is beneficial to query the utilization rate of the virtual machine processor in the preset path.
[0102] To illustrate in detail the method and effect of determining the utilization rate of the virtual machine processor in this solution, the following is described with a most detailed embodiment:
[0103] Refer to Figure 3, The method for determining the usage rate of a virtual machine processor is applied to a system for determining the usage rate of a virtual machine processor. The system includes a virtual machine and a KVM module. The KVM module includes a count value and clock cycle count acquisition module, a usage rate calculation module, a scheduling monitoring module, and an output module. Among them, the count value and clock cycle count acquisition module is used to read the count value and clock cycle count in the register. The scheduling monitoring module is used to schedule the threads of the virtual machine processor. The usage rate calculation module is used to use the time interval during which the virtual machine processor trap occurs continuously for a preset number of times as the sampling period; for each sampling period, obtain the first count value of the host processor in the running state and the first clock cycle count of the host processor within the sampling period; determine the scheduling time period of the virtual machine processor within the sampling period, and obtain the second count value of the host processor in the running state and the second clock cycle count of the host processor during the scheduling time period; calculate the usage rate of the virtual machine processor based on the first count value, the first clock cycle count, the second count value, and the second clock cycle count within multiple sampling periods. The output module is used to output the usage rate of the virtual machine processor to a preset path.
[0104] 1. Initialization stage
[0105] a. Load the KVM module
[0106] Load the KVM module on the host machine to ensure the normal operation of the virtualization environment.
[0107] Verify whether KVM supports accessing the MSR (Model-Specific Register) registers of the physical CPU, such as the APERF and TSC registers.
[0108] b. Create a data structure
[0109] Define a new data structure in the KVM kernel module to store the following information:
[0110] Growth values of the APERF and TSC registers: Record the register changes of each vCPU within a fixed time period.
[0111] vCPU scheduling information: Includes the time points when the vCPU is scheduled out and scheduled back.
[0112] vCPU usage rate: The finally calculated vCPU usage rate.
[0113] Example data structure:
[0114] struct vcpu_usage_stats {
[0115] u64 aperf_diff; / / Growth value of the APERF register
[0116] u64 tsc_diff; / / Increment value of the TSC register
[0117] u64 aperf_sched_diff; / / Increment value of APERF during non-vCPU running time period
[0118] u64 tsc_sched_diff; / / Increment value of TSC during non-vCPU running time period
[0119] u64 usage_rate; / / Calculated vCPU usage rate
[0120] }.
[0121] c. Register the debugfs interface
[0122] Register the debugfs interface under the path / sys / kernel / debug / kvm for outputting the vCPU usage rate.
[0123] 2. Data collection phase
[0124] a. Set the VM Exit sampling point
[0125] Since the virtual machine processor (vCPU) threads will be in two privilege modes, non-root mode and root mode, at intervals during operation, mainly to isolate the operation permissions of the vCPU. The virtual machine runs in non-root mode. When a VMExit occurs, it will enter root mode, and when a VM Entry occurs, it will re-enter non-root mode. When the vCPU is in non-root mode, it executes the relevant task code in the virtual machine, and a VM Exit will occur when performing certain privileged operations and it will exit to root mode to execute the relevant task code on the host machine. Therefore, place the sampling code in the time interval between when the vCPU exits to the host machine after a VM Exit and re-enters non-root mode and when another VM Exit occurs. In this way, when counting the sampling period, only the time of the vCPU in non-root mode is calculated, excluding the time-consuming caused by executing virtualization layer instructions in root mode, which can make the finally obtained CPU usage rate of the virtual machine more accurately reflect the true load of the virtual machine.
[0126] b. Periodic sampling
[0127] Since the clock interrupt on the host machine will necessarily cause a VM Exit for the vCPU, the clock interrupt of the host machine is used for periodic sampling. Every fixed number of VM Exits (e.g., 1000 times), a sampling is triggered, and the register values APERF_START and TSC_START at the start moment of the sampling period and the register values APERF_END and TSC_END at the end moment of the sampling period are recorded.
[0128] 3. Monitor vCPU scheduling information
[0129] During the operation of the virtual machine, the thread corresponding to the virtual machine processor (vCPU) will also be managed by the host scheduler and thus be in two states, the running state and the idle state. The purpose of this step is to count the time when the vCPU thread is in the idle state.
[0130] a. Set scheduling sampling points
[0131] There are two hook functions in the KVM module, which will be called when the vCPU thread switches to the running state and the idle state respectively: kvm_sched_in and kvm_sched_out. In the kvm_sched_out function, record the APERF and TSC values when the vCPU thread is scheduled out (denoted as APERF_OUT and TSC_OUT).
[0132] In the kvm_sched_in function, record the APERF and TSC values when the vCPU thread is scheduled back (denoted as APERF_IN and TSC_IN).
[0133] b. Use the formula to calculate the register growth value of the non-virtual machine processor in the running state:
[0134] APERF_SCHED_DIFF = APERF_IN - APERF_OUT
[0135] TSC_SCHED_DIFF = TSC_IN - TSC_OUT
[0136] 4. Calculate vCPU usage
[0137] a. Calculate the total growth value
[0138] Within a fixed time period, calculate the total growth value through the APERF and TSC values obtained from the sampling points:
[0139] APERF_DIFF = APERF_END - APERF_START
[0140] TSC_DIFF = TSC_END - SC_START
[0141] b. Subtract the register growth value of the non-virtual machine processor in the running state:
[0142] APERF_ACTUAL = APERF_DIFF - APERF_SCHED_DIFF
[0143] TSC_ACTUAL = TSC_DIFF - TSC_SCHED_DIFF
[0144] c. Calculate the vCPU usage rate using the formula:
[0145] USAGE_RATE = (APERF_ACTUAL / TSC_ACTUAL) × 100%
[0146] Update the data structure
[0147] Update the calculation result to the vcpu_usage_stats data structure.
[0148] 5. Output the vCPU usage rate
[0149] Output the calculated vCPU usage rate to the preset path through the debugfs interface: / sys / kernel / debug / kvm / vcpu_usage.
[0150] In the above method for determining the usage rate of the virtual machine processor, since the clock interrupt on the host machine causes the virtual machine processor to trap, and when the virtual machine processor traps, it switches from the non-privileged mode to the privileged mode. Therefore, by using the time interval during which the virtual machine processor traps continuously for a preset number of times as the sampling period, this method of periodic sampling using the host machine's clock interrupt only includes the time of the virtual machine processor in the non-privileged mode in the calculation of the sampling period, excluding the time-consuming in the privileged mode, which is beneficial for the calculated usage rate of the virtual machine processor to more accurately reflect the true load of the virtual machine; the first count value reflects the duration of the host processor in the running state during the sampling period, and the second clock cycle count reflects the startup duration of the host processor during the sampling period; during the scheduling time period, the virtual machine processor is scheduled and used to execute other tasks. Therefore, based on the first count value, the first clock cycle count, the second count value, and the second clock cycle count during the sampling period, the usage rate of the virtual machine processor can be accurately obtained. At the same time, this solution directly performs data collection at the hardware level, avoiding estimation errors at the software level; it does not need to frequently enter the virtual machine, reducing additional system overhead; the virtual machine runs without awareness, without affecting its performance and stability; since it does not require invasive modification of the virtual machine kernel, this solution can be applied to virtual machines with any kernel version.
[0151] It should be understood that although the steps in the flowcharts involved in the above-described embodiments are shown in sequence according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless there is a clear indication in this article, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, at least some of the steps in the flowcharts involved in the above-described embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be executed alternately or in turn with at least some of the steps or stages in other steps or other steps.
[0152] Based on the same inventive concept, an embodiment of the present application further provides a device for determining the usage rate of a virtual machine processor for implementing the method for determining the usage rate of a virtual machine processor described above. The implementation solution provided by this device to solve the problem is similar to the implementation solution described in the above method. Therefore, the specific limitations in one or more embodiments of the device for determining the usage rate of a virtual machine processor provided below can refer to the limitations on the method for determining the usage rate of a virtual machine processor in the above text, and will not be repeated here.
[0153] In an exemplary embodiment, as Figure 4 shown, a device 400 for determining the usage rate of a virtual machine processor is provided, including: a determination module 420, a first acquisition module 440, a second acquisition module 460, and a calculation module 480, where:
[0154] The determination module 420 is configured to use the time interval during which the virtual machine processor trap occurs continuously for a preset number of times as the sampling period;
[0155] The first acquisition module 440 is configured to, for each sampling period, acquire a first count value of the host processor in the running state and the first clock cycle number of the host processor within the sampling period;
[0156] The second acquisition module 460 is configured to determine the scheduling time period of the virtual machine processor within the sampling period, and acquire a second count value of the host processor in the running state and the second clock cycle number of the host processor within the scheduling time period;
[0157] The calculation module 480 is configured to calculate the usage rate of the virtual machine processor based on the first count value, the first clock cycle number, the second count value, and the second clock cycle number within multiple sampling periods.
[0158] For the above virtual machine processor utilization rate determination device, since the clock interrupt on the host machine may cause a trap in the virtual machine processor, and when the virtual machine processor traps, it will switch from the non-privileged mode to the privileged mode. Therefore, by using the time interval during which the virtual machine processor traps continuously for a preset number of times as the sampling period, this method of periodic sampling using the clock interrupt of the host machine only includes the time of the virtual machine processor in the non-privileged mode in the calculation of the sampling period, excluding the time consumed in the privileged mode, which is beneficial for the calculated utilization rate of the virtual machine processor to more accurately reflect the true load of the virtual machine; the first count value reflects the duration of the host processor in the running state during the sampling period, and the second clock cycle count reflects the duration of the host processor since startup during the sampling period; during the scheduling time period, the virtual machine processor is scheduled and used to execute other tasks. Therefore, based on the first count value, the first clock cycle count, the second count value, and the second clock cycle count within the sampling period, the utilization rate of the virtual machine processor can be accurately obtained.
[0159] In one embodiment, to obtain the first count value of the host processor in the running state during the sampling period and the first clock cycle count of the host processor, the first acquisition module 440 is further configured to: at the start moment of the sampling period, obtain the count value of the host processor in the running state and the clock cycle count of the host processor since startup; at the end moment of the sampling period, obtain the count value of the host processor in the running state and the clock cycle count of the host processor since startup; use the difference between the count value at the end moment of the sampling period and the count value at the corresponding start moment as the first count value of the host processor in the running state during the sampling period; use the difference between the clock cycle count at the end moment of the sampling period and the clock cycle count at the corresponding start moment as the first clock cycle count of the host processor during the sampling period.
[0160] In one embodiment, to obtain the count value of the host processor in the running state and the clock cycle count of the host processor since startup, the first acquisition module 440 is further configured to: use privileged instructions to read the count value of the host processor in the running state from the first register and read the clock cycle count of the host processor since startup from the second register.
[0161] In one embodiment, the second count value of the host processor in the running state and the second number of clock cycles of the host processor within the scheduling time period are obtained. The second obtaining module 460 is further configured to: at the start time of the scheduling time period, obtain the count value of the host processor in the running state and the number of clock cycles of the host processor since startup; at the end time of the scheduling time period, obtain the count value of the host processor in the running state and the number of clock cycles of the host processor since startup; use the difference between the count value at the end time of the scheduling time period and the count value at the corresponding start time as the second count value of the host processor in the running state within the scheduling time period; and use the difference between the number of clock cycles at the end time of the scheduling time period and the number of clock cycles at the corresponding start time as the second number of clock cycles of the host processor within the scheduling time period.
[0162] In one embodiment, based on the first count values, the first number of clock cycles, the second count values, and the second number of clock cycles within multiple sampling periods, the usage rate of the virtual machine processor is calculated. The calculating module 480 is further configured to: subtract the sum of the second count values within multiple sampling periods from the sum of the first count values within multiple sampling periods to obtain the count value of the virtual machine processor in the running state; subtract the sum of the second number of clock cycles within multiple sampling periods from the sum of the first number of clock cycles within multiple sampling periods to obtain the number of clock cycles of the virtual machine processor; and use the quotient of the count value of the virtual machine processor in the running state and the number of clock cycles of the virtual machine processor as the usage rate of the virtual machine processor.
[0163] In one embodiment, the apparatus 400 for determining the usage rate of the virtual machine processor further includes an output module, and the output module is configured to output the usage rate of the virtual machine processor to a preset path.
[0164] Each module in the above apparatus for determining the usage rate of the virtual machine processor can be implemented in whole or in part by software, hardware, and their combination. The above modules can be embedded in the processor in the computer device in hardware form or be independent of the processor, or can be stored in the memory in the computer device in software form, so as to be called by the processor to execute the operations corresponding to the above respective modules.
[0165] In an exemplary embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as Figure 5As shown in the figure. The computer device includes a processor, a memory, an input / output interface, a communication interface, a display unit, and an input device. Among them, the processor, the memory, and the input / output interface are connected through a system bus, and the communication interface, the display unit, and the input device are connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and external devices. The communication interface of the computer device is used to communicate with external terminals in a wired or wireless manner, and the wireless manner can be implemented through WIFI, a mobile cellular network, near field communication (NFC), or other technologies. When the computer program is executed by the processor, it implements a method for determining the usage rate of a virtual machine processor. The display unit of the computer device is used to form a visually visible picture, which can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen. The input device of the computer device can be a touch layer covering the display screen, or a button, a trackball, or a touchpad provided on the computer device housing, or an external keyboard, touchpad, or mouse, etc.
[0166] Those skilled in the art can understand that Figure 5 the structure shown in the figure is only a block diagram of some structures related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements.
[0167] In an exemplary embodiment, a computer device is provided, including a memory and a processor. A computer program is stored in the memory, and when the processor executes the computer program, the steps in the above method embodiments are implemented.
[0168] In an embodiment, a computer-readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by the processor, the steps in the above method embodiments are implemented.
[0169] In an embodiment, a computer program product is provided, including a computer program, and when the computer program is executed by the processor, the steps in the above method embodiments are implemented.
[0170] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in this application are all information and data that have been authorized by the user or fully authorized by all parties, and the collection, use, and processing of relevant data need to comply with relevant regulations.
[0171] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above methods. Among them, any reference to a memory, database, or other medium used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The databases involved in the embodiments provided in this application can include at least one of relational databases and non-relational databases. Non-relational databases can include distributed databases based on blockchain, etc., without limitation. The processors involved in the embodiments provided in this application can be general-purpose processors, central processors, graphics processors, digital signal processors, programmable logic devices, data processing logics based on quantum computing, artificial intelligence (AI) processors, etc., without limitation.
[0172] The technical features of the above embodiments can be combined arbitrarily. For the sake of concise description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope recorded in this application.
[0173] The above-described embodiments merely represent several implementation manners of this application. The description is relatively specific and detailed, but it should not be construed as a limitation on the patent scope of this application. It should be noted that for those of ordinary skill in the art, without departing from the concept of this application, several modifications and improvements can still be made, and these all belong to the protection scope of this application. Therefore, the protection scope of this application shall be subject to the appended claims.
Claims
1. A method for determining the usage rate of a virtual machine processor, characterized in that The method includes: Taking the time interval during which virtual machine processor traps occur continuously for a preset number of times as the sampling period; For each sampling period, obtaining a first count value of the host processor in the running state within the sampling period and a first number of clock cycles of the host processor; Determining a scheduling time period of the virtual machine processor within the sampling period, and obtaining a second count value of the host processor in the running state within the scheduling time period and a second number of clock cycles of the host processor; Calculating the utilization rate of the virtual machine processor based on the first count values, the first number of clock cycles, the second count values, and the second number of clock cycles in multiple sampling periods.
2. The method according to claim 1, wherein The obtaining of the first count value of the host processor in the running state within the sampling period and the first number of clock cycles of the host processor includes: At the start time of the sampling period, obtaining the count value of the host processor in the running state and the number of clock cycles of the host processor since startup; At the end time of the sampling period, obtaining the count value of the host processor in the running state and the number of clock cycles of the host processor since startup; Taking the difference between the count value at the end time of the sampling period and the count value at the corresponding start time as the first count value of the host processor in the running state within the sampling period; Taking the difference between the number of clock cycles at the end time of the sampling period and the number of clock cycles at the corresponding start time as the first number of clock cycles of the host processor within the sampling period.
3. The method according to claim 2, wherein The obtaining of the count value of the host processor in the running state and the number of clock cycles of the host processor since startup includes: Using a privileged instruction to read the count value of the host processor in the running state from a first register and read the number of clock cycles of the host processor since startup from a second register.
4. The method according to claim 1, wherein The obtaining of the second count value of the host processor in the running state within the scheduling time period and the second number of clock cycles of the host processor includes: At the start time of the scheduling time period, obtaining the count value of the host processor in the running state and the number of clock cycles of the host processor since startup; At the end time of the scheduling time period, obtaining the count value of the host processor in the running state and the number of clock cycles of the host processor since startup; Taking the difference between the count value at the end time of the scheduling time period and the count value at the corresponding start time as the second count value of the host processor in the running state within the scheduling time period; Taking the difference between the number of clock cycles at the end time of the scheduling time period and the number of clock cycles at the corresponding start time as the second number of clock cycles of the host processor within the scheduling time period.
5. The method according to claim 1, wherein The calculating of the utilization rate of the virtual machine processor based on the first count values, the first number of clock cycles, the second count values, and the second number of clock cycles in multiple sampling periods includes: Subtracting the sum of the second count values in multiple sampling periods from the sum of the first count values in multiple sampling periods to obtain the count value of the virtual machine processor in the running state; Subtract the sum of the number of second clock cycles within multiple sampling periods from the sum of the number of first clock cycles within multiple sampling periods to obtain the number of clock cycles of the virtual machine processor; Use the quotient between the count value of the virtual machine processor in the running state and the number of clock cycles of the virtual machine processor as the utilization rate of the virtual machine processor.
6. The method according to claim 1, characterized in that, The method further includes: Output the utilization rate of the virtual machine processor to a preset path.
7. An apparatus for determining the usage rate of a virtual machine processor, characterized in that, The device includes: A determination module, configured to use the time interval during which the virtual machine processor traps continuously for a preset number of times as the sampling period; A first acquisition module, configured to, for each sampling period, acquire a first count value of the host processor in the running state and the first number of clock cycles of the host processor within the sampling period; A second acquisition module, configured to determine a scheduling time period of the virtual machine processor within the sampling period, and acquire a second count value of the host processor in the running state and the second number of clock cycles of the host processor within the scheduling time period; A calculation module, configured to calculate the utilization rate of the virtual machine processor based on the first count value, the first number of clock cycles, the second count value, and the second number of clock cycles within multiple sampling periods.
8. A computer device, comprising a memory and a processor, the memory storing a computer program, characterized in that, When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Method of calculating CPU utilization
CN104077209A
Method and device for determining vCPU (virtual central processing unit) utilization rate of virtual machine, medium and equipment
CN115794302A
Virtual machine load detection method and device, electronic equipment and storage medium
CN116627592A
A method, an apparatus, an electronic device, and a srotrage medium for detecting virtual machine load
EP4478182A1
Timing compensation for a timestamp counter
US20190042295A1