Method, device and storage medium for determining usage rate of virtual machine processor
By periodically sampling through host clock interrupts, the running status and clock cycle count of the host processor are obtained. Combined with the scheduling time period of the virtual machine processor, the latency and accuracy issues of virtual machine processor utilization monitoring are resolved, and more accurate virtual machine load assessment is achieved.
Patent Information
- Application Number
- CN202510401904.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-01
- Publication Date
- 2026-02-10
- Estimated Expiration
- 2045-04-01
AI Technical Summary
Existing virtual machine monitoring technologies cannot directly obtain the utilization rate of virtual machine processors, resulting in large monitoring data latency and low accuracy. In particular, it is difficult to accurately obtain the virtual machine CPU utilization rate under high load and multi-core processor environments.
By periodically sampling using the host machine's clock interrupts, the host processor's running status count and clock cycle count are obtained. Combined with the virtual machine processor's scheduling time period, the virtual machine processor's utilization rate is calculated.
It enables precise acquisition of virtual machine processor utilization, reduces monitoring latency and improves accuracy, and can more accurately reflect the real load of virtual machines.
Smart Images

Figure CN120407078B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of cloud computing virtualization technology, and in particular to a method, apparatus, device, and storage medium for determining the utilization rate of a virtual machine processor. Background Technology
[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 utilization rate of virtual machine processors (vCPUs) from the host machine. Some solutions rely on the paravirtualization features provided by the underlying virtualization layer, requiring specific modifications to the virtual machine kernel, or observing vCPU threads through the host operating system to estimate CPU utilization. However, these methods suffer from problems such as high 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 improve the accuracy of obtaining CPU utilization in virtual machines. Summary of the Invention
[0003] Therefore, it is necessary to provide a method, apparatus, computer device, computer-readable storage medium, and computer program product for determining the utilization rate of virtual machine processors, which can accurately obtain the utilization rate of virtual machine processors, in order to address the above-mentioned technical problems.
[0004] Firstly, this application provides a method for determining the utilization rate of a virtual machine processor, including:
[0005] The sampling period is defined as the time interval between consecutive virtual machine processor traps occurring a preset number of times.
[0006] For each sampling period, obtain the first count value of the host processor being in the running state within the sampling period and the first clock cycle number of the host processor;
[0007] Within the sampling period, determine the scheduling time period of the virtual machine processor, and obtain the second count value of the host processor being in the running state and the second clock cycle number of the host processor within the scheduling time period;
[0008] The utilization rate of the virtual machine processor is calculated 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.
[0009] In one embodiment, obtaining a first count value indicating that the host processor is in a running state during the sampling period and a first clock cycle number of the host processor includes:
[0010] At the beginning of the sampling period, acquire the count value of the host processor being in the running state and the number of clock cycles since the host processor started;
[0011] At the end of the sampling period, obtain the count value of the host processor being in the running state and the number of clock cycles since the host processor started.
[0012] The difference between the count value at the end of the sampling period and the count value at the corresponding start time is taken as the first count value when the host processor is in the running state within the sampling period.
[0013] The difference between the number of clock cycles at the end of the sampling period and the number of clock cycles at the corresponding start time is taken as the first clock cycle number of the host processor within the sampling period.
[0014] In one embodiment, obtaining the count value of the host processor being in a running state and the number of clock cycles since the host processor started includes:
[0015] The privileged instruction reads the count value of the host processor's running state from the first register and reads the number of clock cycles since the host processor started from the second register.
[0016] In one embodiment, obtaining a second count value indicating that the host processor is in a running state during the scheduling time period and a second clock cycle count of the host processor includes:
[0017] At the start of the scheduling period, obtain the count value of the host processor being in the running state and the number of clock cycles since the host processor started;
[0018] At the end of the scheduling period, obtain the count value of the host processor being in the running state and the number of clock cycles since the host processor started.
[0019] The difference between the count value at the end of the scheduling period and the count value at the corresponding start time is used as the second count value of the host processor being in the running state during the scheduling period.
[0020] The difference between the number of clock cycles at the end of the scheduling period and the number of clock cycles at the corresponding start time is taken as the second number of clock cycles for the host processor within the scheduling period.
[0021] In one embodiment, the utilization rate of the virtual machine processor is calculated based on a first count value, a first clock cycle count, a second count value, and a second clock cycle count within multiple sampling periods, including:
[0022] 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 of the virtual machine processor in the running state.
[0023] The sum of the first clock cycle counts within multiple sampling periods is subtracted from the sum of the second clock cycle counts within multiple sampling periods to obtain the number of clock cycles for the virtual machine processor.
[0024] The quotient between the count of virtual machine processors in running state and the number of clock cycles of the virtual machine processors is taken as the utilization rate of the virtual machine processors.
[0025] In one embodiment, the method for determining the utilization rate of a virtual machine processor further includes:
[0026] Output the virtual machine processor utilization rate to a preset path.
[0027] Secondly, this application also provides a device for determining the utilization rate of a virtual machine processor, comprising:
[0028] The determination module is used to take the time interval between consecutive virtual machine processor traps occurring a preset number of times as the sampling period;
[0029] The first acquisition module is used to acquire, for each sampling period, the first count value of the host processor being in the running state and the first clock cycle number of the host processor.
[0030] The second acquisition module is used to determine the scheduling time period of the virtual machine processor within the sampling period, and to acquire the second count value of the host processor being in the running state within the scheduling time period and the second clock cycle number of the host processor.
[0031] The calculation module is used to 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.
[0032] Thirdly, this application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:
[0033] The sampling period is defined as the time interval between consecutive virtual machine processor traps occurring a preset number of times.
[0034] For each sampling period, obtain the first count value of the host processor being in the running state within the sampling period and the first clock cycle number of the host processor;
[0035] Within the sampling period, determine the scheduling time period of the virtual machine processor, and obtain the second count value of the host processor being in the running state and the second clock cycle number of the host processor within the scheduling time period;
[0036] The utilization rate of the virtual machine processor is calculated 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.
[0037] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, performs the following steps:
[0038] The sampling period is defined as the time interval between consecutive virtual machine processor traps occurring a preset number of times.
[0039] For each sampling period, obtain the first count value of the host processor being in the running state within the sampling period and the first clock cycle number of the host processor;
[0040] Within the sampling period, determine the scheduling time period of the virtual machine processor, and obtain the second count value of the host processor being in the running state and the second clock cycle number of the host processor within the scheduling time period;
[0041] The utilization rate of the virtual machine processor is calculated 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.
[0042] Fifthly, this application also provides a computer program product, including a computer program that, when executed by a processor, performs the following steps:
[0043] The sampling period is defined as the time interval between consecutive virtual machine processor traps occurring a preset number of times.
[0044] For each sampling period, obtain the first count value of the host processor being in the running state within the sampling period and the first clock cycle number of the host processor;
[0045] Within the sampling period, determine the scheduling time period of the virtual machine processor, and obtain the second count value of the host processor being in the running state and the second clock cycle number of the host processor within the scheduling time period;
[0046] The utilization rate of the virtual machine processor is calculated 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.
[0047] The aforementioned method, apparatus, computer device, computer-readable storage medium, and computer program product for determining virtual machine processor utilization, since clock interrupts on the host machine can cause virtual machine processors to trap out, use the time interval between consecutive virtual machine processor traps occurring a preset number of times as the sampling period. This method of periodic sampling using the host machine's clock interrupts allows the calculated virtual machine processor utilization to more accurately reflect the virtual machine's true load. The first count value reflects the duration the host processor is in a running state within the sampling period, and the second clock cycle number reflects the duration the host processor is started within the sampling period. During the scheduling period, the virtual machine processor is scheduled and used to execute other tasks. Therefore, based on the first count value, the first clock cycle number, the second count value, and the second clock cycle number within the sampling period, the virtual machine processor utilization can be accurately obtained. Attached Figure Description
[0048] To more clearly illustrate the technical solutions in the embodiments of this application or related technologies, the drawings used in the description of the embodiments of this application or related technologies will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0049] Figure 1 This is an application environment diagram of a virtual machine processor utilization determination method in one embodiment;
[0050] Figure 2 This is a flowchart illustrating a method for determining the utilization rate of a virtual machine processor in one embodiment;
[0051] Figure 3 This is a schematic diagram illustrating data interaction between a virtual machine and a host machine in one embodiment;
[0052] Figure 4 This is a structural block diagram of a virtual machine processor utilization determination device in one embodiment;
[0053] Figure 5 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0054] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0055] The virtual machine processor utilization determination method provided in this application embodiment can be applied to, for example, Figure 1In the application environment shown, terminal 102 communicates with server 104 via a network. A data storage system can store the data that server 104 needs to process. The data storage system can be integrated on server 104 or placed on a cloud or other network server. This embodiment uses the method applied to terminal 102 as an example for illustration. It is understood that this method can also be applied to servers, and can also be applied to systems including terminals and servers, and implemented through the interaction between the terminal and the server. Terminal 102 uses the time interval between consecutive virtual machine processor traps occurring a preset number of times as a sampling period; for each sampling period, it obtains a first count value of the host processor being in a running state and a first clock cycle count of the host processor within the sampling period; it determines the scheduling time period of the virtual machine processor within the sampling period, and obtains a second count value of the host processor being in a running state and a second clock cycle count of the host processor within the scheduling time period; based on the first count value, first clock cycle count, second count value, and second clock cycle count within multiple sampling periods, it calculates the utilization rate of the virtual machine processor. The terminal 102 can be, but is not limited to, various personal computers, laptops, smartphones, tablets, IoT devices, and portable wearable devices. IoT devices can include smart speakers, smart TVs, smart air conditioners, smart in-vehicle systems, and projection devices. Portable wearable devices can include smartwatches, smart bracelets, and head-mounted displays. Head-mounted displays can be virtual reality (VR) devices, augmented reality (AR) devices, and smart glasses. The server 104 can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services.
[0056] In one exemplary embodiment, such as Figure 2 As shown, a method for determining the utilization rate of a virtual machine processor is provided, which can be applied to... Figure 1 Taking terminal 102 as an example, the explanation includes the following steps 202 to 208. Wherein:
[0057] Step 202: The time interval during which a preset number of virtual machine processor traps occur consecutively is taken as the sampling period.
[0058] Among them, the virtual machine processor, or virtual central processing unit (vCPU), refers to a logical CPU core simulated in virtualization technology.
[0059] Virtual machine processor trapping (VM Exit) refers to the process in a virtualization environment where, when a virtual machine (VM) encounters a specific event or instruction, it needs to suspend the execution of the current VM and transfer control to the virtual machine monitor (VMM). The virtual machine monitor acts as middleware connecting the host machine and the virtual machines. Running on the host machine, the VM monitor manages and allocates hardware resources to each VM. VMs can run on the same host machine, each with its own private virtualization hardware, including CPU, memory, network interface, and disk, allowing them to independently install and run operating systems and applications.
[0060] Since clock interrupts on the host machine can cause VM Exits in the vCPU, periodic sampling is performed using these interrupts. Sampling is triggered every preset number of VM Exits (e.g., 1000), recording data within each sampling period to calculate virtual machine processor utilization over multiple sampling periods.
[0061] In some embodiments, the timestamp of entering non-privileged mode can be determined within the time period of two adjacent virtual machine processor traps, and the timestamp of entering non-privileged mode can be used as the sampling start time, the timestamp of the subsequent virtual machine processor trap can be used as the sampling end time, and the time interval between the sampling start time and the sampling end time can be used as a sampling period, or the time interval between multiple consecutive sampling start times and sampling end times can be used as a sampling period.
[0062] Because the virtual machine processor alternates between privileged mode (root mode) and non-privileged mode (non-root mode) during operation, this is primarily to isolate the vCPU's operational permissions. For example... Figure 3 The diagram illustrates the data interaction between the virtual machine and the host machine. The virtual machine runs in non-root mode. When a VM Exit occurs, it enters root mode; when a VM Entry occurs, it re-enters non-root mode. When the vCPU is in non-root mode, it executes relevant tasks within the virtual machine and performs certain privileged operations, which triggers a VM Exit to return to root mode and execute related tasks on the host machine. Therefore, the sampling code is placed within the time interval between the vCPU's VM Exit and re-entry into non-root mode and the next VM Exit. This statistical sampling period only calculates the time the vCPU spends in non-root mode, eliminating the time spent executing virtualization layer instructions in root mode. This allows the final virtual machine CPU utilization to more accurately reflect the virtual machine's true load.
[0063] Step 204: For each sampling period, obtain the first count value of the host processor being in the running state within the sampling period and the first clock cycle number of the host processor.
[0064] In this context, the host machine refers to the physical host machine running the virtual machine. The host processor refers to the processor within the host machine. The host processor's states include running and idle states. The running state refers to the state of the processes or programs being processed by the host processor, while the idle state, conversely, refers to the state of processes or programs waiting to be processed.
[0065] The first count value refers to the count value that is incremented by one every clock cycle when the host processor is in the running state during the sampling period, according to the actual operating frequency of the CPU hardware.
[0066] Running state refers to the CPU's active state. For example, the C0 state indicates that no instructions such as HLT or MWAIT, which might lead to power-saving sleep mode, are being executed. The first count value is used to characterize the activity level of the host processor within the sampling cycle.
[0067] The first clock cycle count refers to the number of cycles during which the host processor is in the startup state within a sampling period. The first clock cycle count provides a precise time base for the calculations used by the CPU.
[0068] In some embodiments, the ratio between the first count value and the first clock cycle number within a sampling period is the utilization rate of the host processor within the sampling period.
[0069] Step 206: Determine the scheduling time period of the virtual machine processor within the sampling period, and obtain the second count value of the host processor being in the running state and the second clock cycle number of the host processor within the scheduling time period.
[0070] The scheduling period refers to the time during which the virtual machine processor is scheduled to execute other tasks within the sampling period. Because the threads corresponding to the virtual machine processor are also subject to scheduling and management by the host processor during virtual machine operation, they are in both running and idle states.
[0071] The second count value refers to the count value of the host processor being in a running state during the scheduling period. Specifically, it's the count value that increments by one every clock cycle based on the current actual operating frequency of the CPU hardware when the host processor is running during the scheduling period. Since virtual machine processors are scheduled out during the scheduling period, the second count value is used to characterize the activity level of non-virtual machine processors during the scheduling period.
[0072] The second clock cycle count refers to the number of cycles during which the host processor is in the startup state within the scheduling period, indicating the number of cycles during which non-virtual machine processors are in the startup state. The second clock cycle count provides a precise time base for the computations used by the CPU.
[0073] Step 208: Calculate the virtual machine processor utilization rate 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.
[0074] Specifically, the count of the host processor being in the running state during each sampling period is subtracted from the count of the non-virtual machine processor during the scheduling period to obtain the count of the virtual machine processor being in the running state during the sampling period.
[0075] The number of clock cycles the virtual machine processor starts within a sampling period is obtained by subtracting the number of clock cycles the non-virtual machine processor has in the second clock cycle during the scheduling period from the number of clock cycles the host processor has in the first clock cycle during each sampling period.
[0076] The utilization rate of a virtual machine processor is equal to the ratio between the count of the virtual machine processor being in a running state during the sampling period and the number of clock cycles of the host processor during the sampling period.
[0077] In the above method for determining the utilization rate of virtual machine processors, since clock interrupts on the host machine can cause virtual machine processors to trap out, the sampling period is used by taking the time interval between consecutive virtual machine processor traps that occur a preset number of times. This method of periodic sampling using the host machine's clock interrupts helps to ensure that the calculated virtual machine processor utilization rate more accurately reflects the real load of the virtual machine. The first count value reflects the duration of the host processor being in the running state within the sampling period, and the second clock cycle number reflects the duration of the host processor being started within the sampling period. During the scheduling period, the virtual machine processor is scheduled and used to execute other tasks. Therefore, based on the first count value, the first clock cycle number, the second count value, and the second clock cycle number within the sampling period, the utilization rate of the virtual machine processor can be accurately obtained.
[0078] In an exemplary embodiment, obtaining a first count value indicating that the host processor is in a running state and a first clock cycle count of the host processor within a sampling period includes: at the beginning of the sampling period, obtaining a count value indicating that the host processor is in a running state and a clock cycle count of the host processor since startup; at the end of the sampling period, obtaining a count value indicating that the host processor is in a running state and a clock cycle count of the host processor since startup; using the difference between the count value at the end of the sampling period and the count value at the corresponding beginning of the sampling period as the first count value indicating that the host processor is in a running state within the sampling period; and using the difference between the clock cycle count at the end of the sampling period and the clock cycle count at the corresponding beginning of the sampling period as the first clock cycle count of the host processor within the sampling period.
[0079] In the case where the sampling period includes the time interval between two consecutive virtual machine traps, the moment when the virtual machine enters non-privileged mode within the time interval is taken as the start time of the sampling period, and the moment when the virtual machine traps again within the time interval is taken as the end time of the sampling period. In the case where the sampling period includes multiple time intervals between two consecutive virtual machine traps, the moment when the virtual machine enters non-privileged mode within the first time interval is taken as the start time of the sampling period, and the moment when the virtual machine traps again within the last time interval is taken as the end time of the sampling period.
[0080] The difference between the count value at the end of the sampling period and the count value at the corresponding start time indicates the first count value at which the host processor is in the running state during the sampling period.
[0081] The difference between the number of clock cycles at the end of the sampling period and the number of clock cycles at the corresponding start time indicates the first number of clock cycles of the host processor within the sampling period.
[0082] In this embodiment, the first count value of the host processor being in running state and the first clock cycle number of the host processor are calculated based on the count values at the start and end times within the sampling period and the number of clock cycles. Since each time interval within each sampling period only includes the time in non-privileged mode, the calculated first count value and first clock cycle number can accurately reflect the true load of the host processor, which is beneficial for obtaining accurate virtual machine CPU utilization.
[0083] In one exemplary embodiment, obtaining the count value of the host processor being in a running state and the number of clock cycles since the host processor started includes: using privileged instructions to read the count value of the host processor being in a running state from a first register and reading the number of clock cycles since the host processor started from a second register.
[0084] The first register stores a count of the host processor's running state from startup to the current moment. This count is incremented by one every clock cycle based on the CPU's current operating frequency while the host processor is running. For example, the first register could be the APERF register.
[0085] The second register stores the number of clock cycles since the host processor started up until the current time. For example, the second register could be the TSC register.
[0086] The values of both the first and second registers need to be read using privileged instructions on the host machine. Privileged instructions are instructions executed in privileged mode; for example, a privileged instruction could be the rdmsr instruction.
[0087] In this embodiment, privileged instructions are used to read the count value of the host processor being in running state from the first register and the number of clock cycles since the host processor started from the second register. This is beneficial for recording the corresponding data in privileged mode, which can be used to accurately calculate the virtual machine CPU utilization.
[0088] In an exemplary embodiment, obtaining a second count value indicating that the host processor is in a running state and a second clock cycle count of the host processor during a scheduling time period includes: at the start of the scheduling time period, obtaining a count value indicating that the host processor is in a running state and a clock cycle count of the host processor since startup; at the end of the scheduling time period, obtaining a count value indicating that the host processor is in a running state and a clock cycle count of the host processor since startup; using the difference between the count value at the end of the scheduling time period and the count value at the corresponding start of the scheduling time period as the second count value indicating that the host processor is in a running state during the scheduling time period; and using the difference between the clock cycle count at the end of the scheduling time period and the clock cycle count at the corresponding start of the scheduling time period as the second clock cycle count of the host processor during the scheduling time period.
[0089] The start time of the scheduling period refers to the moment when the virtual machine monitor in the host machine schedules the virtual machine processor out to execute other tasks. For example, the kvm_sched_out() function in the KVM (Kernel-Based VirtualMachine, a virtualization technology based on the Linux kernel) module can be used. In the kvm_sched_out() function, the count value in the first register is read to obtain the count value of the host processor in the running state from the start time to the start time of the scheduling period, and the clock cycle number in the second register is read to obtain the clock cycle number from the start time to the start time of the scheduling period.
[0090] The end time of the scheduling 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, the count value in the first register is read to obtain the count value of the running state of the host processor from the start to the end time of the scheduling period, and the clock cycle number in the second register is read to obtain the clock cycle number of the host processor from the start to the end time of the scheduling period.
[0091] The difference between the count value at the end of the scheduling period and the count value at the beginning of the scheduling period indicates the second count value indicating whether the host processor is in a running state during the scheduling period.
[0092] The difference between the number of clock cycles at the end of the scheduling period and the number of clock cycles at the corresponding start time indicates the second number of clock cycles for the host processor within the scheduling period.
[0093] In this embodiment, by using the count value and clock cycle number at the start and end times of the scheduling time period, the second count value of the host processor being in the running state during the scheduling time period, as well as the second clock cycle number of the host processor, are calculated. This is equivalent to calculating the activity status of the non-virtual machine processor, which is helpful for calculating the accurate utilization rate of the virtual machine processor.
[0094] In an exemplary embodiment, the utilization rate of a virtual machine processor is calculated based on a first count value, a first clock cycle count, a second count value, and a second clock cycle count within multiple sampling periods. This includes: subtracting the sum of the first count values within multiple sampling periods from the sum of the second count values within multiple sampling periods to obtain a count value indicating that the virtual machine processor is in a running state; subtracting the sum of the first clock cycle counts within multiple sampling periods from the sum of the second clock cycle counts within multiple sampling periods to obtain the clock cycle count of the virtual machine processor; and using the quotient between the count value indicating that the virtual machine processor is in a running state and the clock cycle count of the virtual machine processor as the utilization rate of the virtual machine processor.
[0095] Specifically, for the utilization rate of virtual machine processors within multiple sampling periods, the first count value within multiple sampling periods can be summed to obtain the total value of the host processor being in the running state within multiple sampling periods; the second count value within multiple sampling periods can be summed to obtain the count value of the non-virtual machine processors being in the running state within multiple sampling periods; and the difference between the two can be obtained to obtain the count value of the virtual machine processors being in the running state.
[0096] The sum of the first clock cycle counts within multiple sampling periods is taken as the total number of clock cycles for the host processor to start within multiple sampling periods. The sum of the clock cycle counts within multiple sampling periods is taken as the number of clock cycles for the non-virtual machine processor to start within multiple sampling periods. The difference between the two is taken as the number of clock cycles for the virtual machine processor to start.
[0097] The utilization rate of the virtual machine processor is obtained by dividing the count of the virtual machine processor in running state by the number of clock cycles 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 of the virtual machine processor in running state. The sum of the first clock cycle counts within multiple sampling periods is subtracted from the sum of the second clock cycle counts within multiple sampling periods to obtain the clock cycle count of the virtual machine processor. The quotient of the two can obtain the accurate utilization rate of the virtual machine processor.
[0099] In one exemplary embodiment, the virtual machine processor utilization determination method further includes: outputting the virtual machine processor utilization to a preset path.
[0100] The default path is the path where the virtual machine processor usage is stored. For example, a debugfs interface is registered under the / sys / kernel / debug / kvm path to output the vCPU usage.
[0101] In this embodiment, by outputting the virtual machine processor utilization rate to a preset path, it is beneficial to query the virtual machine processor utilization rate in the preset path.
[0102] To illustrate in detail the method and effect of determining the virtual machine processor utilization rate in this solution, a detailed embodiment is provided below:
[0103] refer to Figure 3The virtual machine processor utilization determination method is applied to a virtual machine processor utilization determination system. The system includes a virtual machine and a KVM module. The KVM module includes a count and clock cycle count acquisition module, a utilization calculation module, a scheduling monitoring module, and an output module. The count and clock cycle count acquisition module reads the count and clock cycle count from registers. The scheduling monitoring module schedules the virtual machine processor threads. The utilization calculation module uses the time interval between consecutive virtual machine processor traps occurring a preset number of times as the sampling period. For each sampling period, it acquires a first count value and a first clock cycle count of the host processor being in a running state within the sampling period. It determines the virtual machine processor's scheduling time period within the sampling period and acquires a second count value and a second clock cycle count of the host processor being in a running state within the scheduling time period. Based on the first count value, first clock cycle count, second count value, and second clock cycle count across multiple sampling periods, it calculates the virtual machine processor utilization. The output module outputs the virtual machine processor utilization to a preset path.
[0104] 1. Initialization Phase
[0105] a. Load the KVM module
[0106] Load the KVM module on the host machine to ensure the virtualization environment runs normally.
[0107] Verify whether KVM supports access to the physical CPU's MSR (Model-Specific Register) registers, such as the APERF and TSC registers.
[0108] b. Create data structures
[0109] Define a new data structure in the KVM kernel module to store the following information:
[0110] The increment values of the APERF and TSC registers record the register changes for each vCPU over a fixed time period.
[0111] vCPU scheduling information: including the time points when a vCPU is scheduled out and when it is scheduled back.
[0112] vCPU utilization: The final calculated vCPU utilization.
[0113] Example data structure:
[0114] struct vcpu_usage_stats {
[0115] u64 aperf_diff; / / Incrementing value of the APERF register
[0116] u64 tsc_diff; / / Increment value of the TSC register
[0117] u64 aperf_sched_diff; / / APERF increment value during non-vCPU runtime periods
[0118] u64 tsc_sched_diff; / / TSC increment value during non-vCPU runtime periods
[0119] u64 usage_rate; / / Calculated vCPU utilization
[0120] }
[0121] c. Register the debugfs interface
[0122] Register the debugfs interface under the / sys / kernel / debug / kvm path to output vCPU utilization.
[0123] 2. Data Acquisition Phase
[0124] a. Setting VM Exit sampling points
[0125] Because virtual machine processor (vCPU) threads alternate between two privileged modes during runtime—non-root and root—this is primarily to isolate vCPU operational privileges. When a virtual machine runs in non-root mode, it enters root mode upon a VMExit and re-enters non-root mode upon a VM Entry. When the vCPU is in non-root mode, executing relevant tasks and performing certain privileged operations within the virtual machine will trigger a VM Exit, exiting to root mode to execute related tasks on the host machine. Therefore, placing the sampling code within the time interval between a vCPU exiting the host machine via VM Exit and re-entering non-root mode, and then another VM Exit, ensures that the statistical sampling period only calculates the time the vCPU spends in non-root mode, excluding the time spent executing virtualization layer instructions in root mode. This allows the final virtual machine CPU utilization to more accurately reflect the virtual machine's true load.
[0126] b. Periodic sampling
[0127] Since a clock interrupt on the host machine will inevitably cause a VM Exit in the vCPU, periodic sampling is performed using the host machine's clock interrupt. 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 of the sampling period and the register values APERF_END and TSC_END at the end of the sampling period are recorded.
[0128] 3. Monitor vCPU scheduling information
[0129] During virtual machine operation, the threads corresponding to the virtual machine processor (vCPU) are also managed by the host scheduler, thus existing in two states: running and idle. The purpose of this step is to track the time vCPU threads spend in the idle state.
[0130] a. Setting up scheduling sampling points
[0131] The KVM module contains two hook functions that are called when a vCPU thread switches to the running state and the idle state, respectively: kvm_sched_in and kvm_sched_out. The kvm_sched_out function records the APERF and TSC values (denoted as APERF_OUT and TSC_OUT) when the vCPU thread is scheduled out.
[0132] In the kvm_sched_in function, the APERF and TSC values (denoted as APERF_IN and TSC_IN) are recorded when the vCPU thread is scheduled back.
[0133] b. Calculate the register increment value of a non-virtual machine processor in its running state using the formula:
[0134] APERF_SCHED_DIFF=APERF_IN-APERF_OUT
[0135] TSC_SCHED_DIFF=TSC_IN-TSC_OUT
[0136] 4. Calculate vCPU utilization
[0137] a. Calculate the total growth value
[0138] The total growth value is calculated using APERF and TSC values obtained from sampling points within a fixed time period:
[0139] APERF_DIFF=APERF_END-APERF_START
[0140] TSC_DIFF=TSC_END-SC_START
[0141] b. Subtract the register increments of non-virtual machine processors in the running state:
[0142] APERF_ACTUAL=APERF_DIFF-APERF_SCHED_DIFF
[0143] TSC_ACTUAL=TSC_DIFF-TSC_SCHED_DIFF
[0144] c. Calculate vCPU utilization using the formula:
[0145] USAGE_RATE=(APERF_ACTUAL / TSC_ACTUAL)×100%
[0146] Update data structure
[0147] Update the calculation results to the vcpu_usage_stats data structure.
[0148] 5. Output vCPU utilization
[0149] The calculated vCPU utilization is output to the preset path via the debugfs interface: / sys / kernel / debug / kvm / vcpu_usage.
[0150] The aforementioned method for determining virtual machine processor utilization utilizes the host machine's clock interrupts to cause virtual machine processors to trap out. When a virtual machine processor traps out, it switches from non-privileged mode to privileged mode. Therefore, by using the time interval between consecutive virtual machine processor traps occurring a preset number of times as the sampling period, this method of periodic sampling using the host machine's clock interrupts only includes the time the virtual machine processor spends in non-privileged mode, excluding the time spent in privileged mode. This allows the calculated virtual machine processor utilization to more accurately reflect the virtual machine's true load. The first count value reflects the duration the host processor is running within the sampling period, and the second clock cycle count reflects the duration the host processor is running within the sampling period. During the scheduling 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 virtual machine processor utilization can be accurately obtained. Meanwhile, this solution is based directly on hardware-level data acquisition, avoiding estimation errors at the software level; it does not require frequent access to the virtual machine, reducing additional system overhead; the virtual machine runs seamlessly without affecting its performance and stability; and since it does not require intrusive modifications to 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 of the embodiments described above are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the embodiments described above may include multiple steps or multiple stages. These steps or stages are not necessarily completed 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 performed alternately or in turn with other steps or at least some of the steps or stages of other steps.
[0152] Based on the same inventive concept, this application also provides a virtual machine processor utilization determination apparatus for implementing the virtual machine processor utilization determination method described above. The solution provided by this apparatus is similar to the implementation described in the above method; therefore, the specific limitations in one or more virtual machine processor utilization determination apparatus embodiments provided below can be found in the limitations of the virtual machine processor utilization determination method described above, and will not be repeated here.
[0153] In one exemplary embodiment, such as Figure 4 As shown, a virtual machine processor utilization determination device 400 is provided, comprising: a determination module 420, a first acquisition module 440, a second acquisition module 460, and a calculation module 480, wherein:
[0154] The determination module 420 is used to take the time interval during which a preset number of virtual machine processor traps occur consecutively as the sampling period;
[0155] The first acquisition module 440 is used to acquire, for each sampling period, a first count value of the host processor being in the running state and the first clock cycle number of the host processor.
[0156] The second acquisition module 460 is used to determine the scheduling time period of the virtual machine processor within the sampling period, and to acquire the second count value of the host processor being 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 used to 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.
[0158] The aforementioned virtual machine processor utilization determination device, because a clock interrupt on the host machine causes the virtual machine processor to trap out, switching from non-privileged mode to privileged mode during a trap, uses the time interval between consecutive virtual machine processor traps of a preset number as the sampling period. This method of periodic sampling using the host machine's clock interrupt only includes the time the virtual machine processor spends in non-privileged mode, excluding the time spent in privileged mode. This allows the calculated virtual machine processor utilization to more accurately reflect the virtual machine's true load. The first count value reflects the duration the host processor is in a running state during the sampling period, and the second clock cycle number reflects the duration the host processor is started during the sampling period. During the scheduling period, the virtual machine processor is scheduled and used to execute other tasks. Therefore, based on the first count value, the first clock cycle number, the second count value, and the second clock cycle number within the sampling period, the virtual machine processor utilization can be accurately obtained.
[0159] In one embodiment, the first acquisition module 440 is further configured to: acquire, at the beginning of the sampling period, a count value indicating that the host processor is in a running state and the number of clock cycles since the host processor started; at the end of the sampling period, acquire, a count value indicating that the host processor is in a running state and the number of clock cycles since the host processor started; use the difference between the count value at the end of the sampling period and the count value at the corresponding beginning of the sampling period as the first count value indicating that the host processor is in a running state during the sampling period; and use the difference between the number of clock cycles at the end of the sampling period and the number of clock cycles at the corresponding beginning of the sampling period as the first number of clock cycles of the host processor during the sampling period.
[0160] In one embodiment, the first acquisition module 440 is further configured to: use privileged instructions to read the count value of the host processor being in a running state from a first register, and read the number of clock cycles of the host processor since startup from a second register.
[0161] In one embodiment, the second acquisition module 460 is further configured to: acquire, at the start of the scheduling period, a second count value indicating that the host processor is in a running state and the second clock cycle count of the host processor since startup; at the end of the scheduling period, acquire, a second count value indicating that the host processor is in a running state and the second clock cycle count of the host processor since startup; use the difference between the count value at the end of the scheduling period and the count value at the corresponding start of the scheduling period as the second count value indicating that the host processor is in a running state during the scheduling period; and use the difference between the clock cycle count at the end of the scheduling period and the clock cycle count at the corresponding start of the scheduling period as the second clock cycle count of the host processor during the scheduling period.
[0162] In one embodiment, the utilization rate of the virtual machine processor is calculated based on a first count value, a first clock cycle count, a second count value, and a second clock cycle count within multiple sampling periods. The calculation module 480 is further configured to: subtract the sum of the first count values within multiple sampling periods from the sum of the second count values within multiple sampling periods to obtain a count value indicating that the virtual machine processor is in a running state; subtract the sum of the first clock cycle counts within multiple sampling periods from the sum of the second clock cycle counts within multiple sampling periods to obtain the clock cycle count of the virtual machine processor; and use the quotient between the count value indicating that the virtual machine processor is in a running state and the clock cycle count of the virtual machine processor as the utilization rate of the virtual machine processor.
[0163] In one embodiment, the virtual machine processor utilization determination device 400 further includes an output module, which is used to output the virtual machine processor utilization to a preset path.
[0164] The modules in the aforementioned virtual machine processor utilization determination device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in the processor of a computer device in hardware form or independent of it, or stored in the memory of the computer device in software form, so that the processor can call and execute the operations corresponding to each module.
[0165] In one exemplary embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 5As shown, the computer device includes a processor, memory, input / output interface, communication interface, display unit, and input device. The processor, memory, and input / output interface are connected via a system bus, and the communication interface, display unit, and input device are also connected to the system bus via the input / output interface. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The input / output interface is used for exchanging information between the processor and external devices. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, mobile cellular networks, Near Field Communication (NFC), or other technologies. When the computer program is executed by the processor, it implements a method for determining the utilization rate of a virtual machine processor. The display unit is used to form a visually visible image and can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be an LCD screen or an e-ink screen. The input device of the computer device can be a touch layer covering the display screen, or buttons, trackballs, or touchpads set on the casing of the computer device, or external keyboards, touchpads, or mice, etc.
[0166] Those skilled in the art will understand that Figure 5 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0167] In one exemplary embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps in the above-described method embodiments.
[0168] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon that, when executed by a processor, implements the steps in the above method embodiments.
[0169] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps in the above method embodiments.
[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 used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of the relevant data must comply with relevant regulations.
[0171] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media 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), magnetic 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 take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.
[0172] The technical features of the above embodiments can be combined in any way. For the sake of brevity, 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, they should be considered to be within the scope of this application.
[0173] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. A method for determining the utilization rate of a virtual machine processor, characterized in that, The method includes: The sampling period is defined as the time interval between consecutive virtual machine processor traps occurring a preset number of times. For each sampling period, a first count value of the host processor being in the running state within the sampling period and a first clock cycle number of the host processor are obtained; the first count value refers to the count value that is incremented by one every clock cycle according to the current actual operating frequency of the CPU hardware when the host processor is in the running state within the sampling period; the first clock cycle number refers to the number of cycles in which the host processor is in the startup state within the sampling period. Within the sampling period, the scheduling time period of the virtual machine processor is determined, and a second count value and a second clock cycle number of the host processor in the running state are obtained within the scheduling time period. 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. The second count value refers to the count value of the host processor in the running state within the scheduling time period, which is used to characterize the activity level of non-virtual machine processors within the scheduling time period. The second clock cycle number refers to the number of cycles during which the host processor is in the startup state within the scheduling time period. The utilization rate of the virtual machine processor is calculated 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.
2. The method according to claim 1, characterized in that, The step of obtaining the first count value of the host processor being in running state within the sampling period and the first clock cycle number of the host processor includes: At the beginning of the sampling period, acquire the count value of the host processor being in the running state and the number of clock cycles since the host processor started. At the end of the sampling period, obtain the count value of the host processor being in the running state and the number of clock cycles since the host processor started. The difference between the count value at the end of the sampling period and the count value at the corresponding start time is taken as the first count value of the host processor being in running state during the sampling period. The difference between the number of clock cycles at the end of the sampling period and the number of clock cycles at the corresponding start time is taken as the first clock cycle number of the host processor within the sampling period.
3. The method according to claim 2, characterized in that, The acquisition of the count value of the host processor being in running state and the number of clock cycles since the host processor started includes: The privileged instruction reads the count value of the host processor's running state from the first register and reads the number of clock cycles since the host processor started from the second register.
4. The method according to claim 1, characterized in that, The step of obtaining the second count value of the host processor being in running state during the scheduling time period and the second clock cycle number of the host processor includes: At the start of the scheduled time period, obtain the count value of the host processor being in the running state and the number of clock cycles since the host processor started. At the end of the scheduled time period, obtain the count value of the host processor being in the running state and the number of clock cycles since the host processor started. The difference between the count value at the end of the scheduling period and the count value at the corresponding start time is used as the second count value of the host processor being in running state during the scheduling period. The difference between the number of clock cycles at the end of the scheduling period and the number of clock cycles at the corresponding start time is taken as the second number of clock cycles of the host processor within the scheduling period.
5. The method according to claim 1, characterized in that, The calculation of the virtual machine processor utilization based on a first count value, a first clock cycle count, a second count value, and a second clock cycle count within multiple sampling periods includes: 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 of the virtual machine processor in the running state. The sum of the first clock cycle counts within multiple sampling periods is subtracted from the sum of the second clock cycle counts within multiple sampling periods to obtain the clock cycle count of the virtual machine processor. The quotient between the count of the virtual machine processor being in a running state and the number of clock cycles of the virtual machine processor is taken 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. A device for determining the utilization rate of a virtual machine processor, characterized in that, The device includes: The determination module is used to take the time interval between consecutive virtual machine processor traps occurring a preset number of times as the sampling period; The first acquisition module is used to acquire, for each sampling period, a first count value of the host processor being in the running state within the sampling period and a first clock cycle number of the host processor; the first count value refers to the count value that is incremented by one every clock cycle according to the current actual operating frequency of the CPU hardware when the host processor is in the running state within the sampling period; the first clock cycle number refers to the number of cycles in which the host processor is in the startup state within the sampling period. The second acquisition module is used to determine the scheduling time period of the virtual machine processor within the sampling period, and to acquire a second count value of the host processor being in a running state and a second clock cycle number of the host processor within the scheduling time period; 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; the second count value refers to the count value of the host processor being in a running state within the scheduling time period, used to characterize the activity level of non-virtual machine processors within the scheduling time period; the second clock cycle number refers to the number of cycles during which the host processor is in a startup state within the scheduling time period; The calculation module is used to calculate the utilization rate of the virtual machine processor based on a first count value, a first clock cycle number, a second count value, and a second clock cycle number within multiple sampling periods.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 6.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 6.
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