Disclosure of Invention
In view of the foregoing, it is desirable to provide a method, an apparatus, a computer device, a computer-readable storage medium, and a computer program product for determining a utilization rate of a virtual machine processor, which are capable of accurately acquiring the utilization rate of the virtual machine processor.
In a first aspect, the present application provides a method for determining a usage rate of a virtual machine processor, including:
Taking a time interval for which the virtual machine processor is trapped out continuously for a preset number of times as a sampling period;
for each sampling period, acquiring a first count value of the host processor in an operating state and a first clock cycle number of the host processor in the sampling period;
determining a scheduling time period of the virtual machine processor in the sampling period, and acquiring a second count value of the host processor in an operating state and a second clock cycle number of the host processor in the scheduling time period;
the utilization of the virtual machine processor is calculated based on the first count value, the first number of clock cycles, the second count value, and the second number of clock cycles over the plurality of sampling periods.
In one embodiment, obtaining a first count value of a host processor in an operating state and a first number of clock cycles of the host processor during a sampling period includes:
At the initial time of a sampling period, acquiring a count value of a host processor in an operating state and a clock period number of the host processor since the host processor is started;
At the termination time of the sampling period, acquiring a count value of the host processor in an operation state and a clock period number of the host processor since the host processor is started;
taking the difference value between the count value at the ending time of the sampling period and the count value at the corresponding starting time as a first count value of the host processor in an operating state in the sampling period;
The difference between the number of clock cycles at the ending time of the sampling period and the number of clock cycles at the corresponding starting time is taken as the first number of clock cycles of the host processor in the sampling period.
In one embodiment, obtaining a count value of a host processor in an operating state and a number of clock cycles since the host processor started includes:
A privileged instruction is used to read a count value of the host processor in an operating state from a first register and to read the number of clock cycles since the host processor was started from a second register.
In one embodiment, obtaining a second count value of the host processor in an operating state and a second number of clock cycles of the host processor during the scheduled time period includes:
At the starting time of the scheduling time period, acquiring a count value of the host processor in an operating state and a clock cycle number of the host processor since the host processor is started;
At the termination time of the scheduling time period, acquiring a count value of the host processor in an operation state and a clock cycle number of the host processor since the host processor is started;
taking the difference value between the count value at the ending time of the scheduling time period and the count value at the corresponding starting time as a second count value of the running state of the host processor in the scheduling time period;
The difference between the number of clock cycles at the ending time of the scheduling period and the number of clock cycles at the corresponding starting time is taken as the second number of clock cycles of the host processor in the scheduling period.
In one embodiment, calculating the utilization 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 over a plurality of sampling cycles comprises:
subtracting the sum of the first count values in the sampling periods from the sum of the second count values in the sampling periods to obtain a count value of the virtual machine processor in an operating state;
subtracting the sum of the first clock cycles in the sampling periods from the sum of the second clock cycles in the sampling periods to obtain the clock cycles of the virtual machine processor;
And taking the quotient between the count value of the running state of the virtual machine processor and the clock cycle number of the virtual machine processor as the utilization rate of the virtual machine processor.
In one embodiment, the method for determining the usage rate of the virtual machine processor further includes:
and outputting the utilization rate of the virtual machine processor to a preset path.
In a second aspect, the present application further provides a device for determining a usage rate of a virtual machine processor, including:
the determining module is used for taking a time interval which is continuously generated for a preset number of times and is reserved by the virtual machine processor as a sampling period;
The first acquisition module is used for acquiring a first count value of the host processor in an operating state and a first clock cycle number of the host processor in the sampling period for each sampling period;
the second acquisition module is used for determining a scheduling time period of the virtual machine processor in the sampling period, and acquiring a second count value of the host processor in an operating state and a second clock cycle number of the host processor in the scheduling time period;
the computing module is used for computing 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 in a plurality of sampling cycles.
In a third aspect, the present application also provides a computer device comprising a memory and a processor, the memory storing a computer program, the processor implementing the following steps when executing the computer program:
Taking a time interval for which the virtual machine processor is trapped out continuously for a preset number of times as a sampling period;
for each sampling period, acquiring a first count value of the host processor in an operating state and a first clock cycle number of the host processor in the sampling period;
determining a scheduling time period of the virtual machine processor in the sampling period, and acquiring a second count value of the host processor in an operating state and a second clock cycle number of the host processor in the scheduling time period;
the utilization of the virtual machine processor is calculated based on the first count value, the first number of clock cycles, the second count value, and the second number of clock cycles over the plurality of sampling periods.
In a fourth aspect, the present application also provides a computer readable storage medium having stored thereon a computer program which when executed by a processor performs the steps of:
Taking a time interval for which the virtual machine processor is trapped out continuously for a preset number of times as a sampling period;
for each sampling period, acquiring a first count value of the host processor in an operating state and a first clock cycle number of the host processor in the sampling period;
determining a scheduling time period of the virtual machine processor in the sampling period, and acquiring a second count value of the host processor in an operating state and a second clock cycle number of the host processor in the scheduling time period;
the utilization of the virtual machine processor is calculated based on the first count value, the first number of clock cycles, the second count value, and the second number of clock cycles over the plurality of sampling periods.
In a fifth aspect, the application also provides a computer program product comprising a computer program which, when executed by a processor, performs the steps of:
Taking a time interval for which the virtual machine processor is trapped out continuously for a preset number of times as a sampling period;
for each sampling period, acquiring a first count value of the host processor in an operating state and a first clock cycle number of the host processor in the sampling period;
determining a scheduling time period of the virtual machine processor in the sampling period, and acquiring a second count value of the host processor in an operating state and a second clock cycle number of the host processor in the scheduling time period;
the utilization of the virtual machine processor is calculated based on the first count value, the first number of clock cycles, the second count value, and the second number of clock cycles over the plurality of sampling periods.
According to the method, the device, the computer equipment, the computer readable storage medium and the computer program product for determining the utilization rate of the virtual machine processor, the virtual machine processor is trapped out due to the clock interruption on the host machine, so that the method for periodically sampling by using the clock interruption of the host machine is beneficial to the fact that the calculated utilization rate of the virtual machine processor can more accurately reflect the real load of the virtual machine, the first count value reflects the duration of the host processor in an operating state in the sampling period, the second clock period reflects the duration of the host processor started in the sampling period, and the virtual machine processor is scheduled and used for executing other tasks in a scheduling time period, so that the utilization rate of the virtual machine processor can be accurately acquired based on the first count value, the first clock period number, the second count value and the second clock period in the sampling period.
Detailed Description
The present application will be described in further detail with reference to the drawings and examples, in order to make the objects, technical solutions and advantages of the present application more apparent. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the scope of the application.
The method for determining the utilization rate of the virtual machine processor provided by the embodiment of the application can be applied to an application environment shown in fig. 1. Wherein the terminal 102 communicates with the server 104 via a network. The data storage system may store data that the server 104 needs to process. The data storage system may be integrated on the server 104 or may be located on a cloud or other network server. The embodiment is illustrated with the method applied to the terminal 102, and it is understood that the method may also be applied to a server, and may also be applied to a system including a terminal and a server, and implemented through interaction between the terminal and the server. The terminal 102 takes a time interval for which the trap of the virtual machine processor continuously occurs for a preset number of times as a sampling period, acquires a first count value of the host processor in an operating state and a first clock cycle number of the host processor in the sampling period for each sampling period, determines a scheduling time period of the virtual machine processor in the sampling period, acquires a second count value of the host processor in the operating state and a second clock cycle number of the host processor in the scheduling time period, and calculates 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 in a plurality of sampling periods. The terminal 102 may be, but not limited to, various personal computers, notebook computers, smart phones, tablet computers, internet of things devices and portable wearable devices, where the internet of things devices may be smart speakers, smart televisions, smart air conditioners, smart vehicle devices, projection devices, and the like. The portable wearable device may be a smart watch, smart bracelet, headset, or the like. The head-mounted device may be a Virtual Reality (VR) device, an augmented Reality (Augmented Reality, AR) device, smart glasses, or the like. The server 104 may be an independent physical server, a server cluster or a distributed system formed by a plurality of physical servers, or a cloud server providing cloud computing services.
In an exemplary embodiment, as shown in fig. 2, a method for determining a usage rate of a virtual machine processor is provided, and the method is applied to the terminal 102 in fig. 1, for example, and includes the following steps 202 to 208. Wherein:
Step 202, taking a time interval for which the virtual machine processor is trapped out continuously for a preset number of times as a sampling period.
Wherein a virtual machine processor, i.e. a virtual central processing unit (vCPU, virtual Central Processing Unit), refers to a logical CPU core that is emulated in virtualization technology.
Virtual machine processor trap-out (VM Exit) refers to the process in a virtualized environment that when a Virtual Machine (VM) encounters a particular event or instruction, it is necessary to suspend execution of the current virtual machine and transfer control to a Virtual Machine Monitor (VMM). The virtual machine monitor is a middleware for connecting a host machine and virtual machines, the virtual machine monitor operates on the host machine, manages and distributes hardware resources to each virtual machine, each virtual machine can operate on the same host machine, and each virtual machine has own private virtualization hardware comprising a CPU, a memory, a network interface and a disk, so that an operating system and an application program can be independently installed and operated.
Since a clock interrupt on the host will cause a VM Exit to occur for the vCPU, the clock interrupt of the host is used for periodic sampling. Every other VM Exit (e.g., 1000 times) of preset times, triggering one sampling, and recording data in sampling periods to calculate the utilization rate of the virtual machine processor in a plurality of sampling periods.
In some embodiments, the time stamp of entering the non-privileged mode may be determined during a period when the adjacent two virtual machine processor trapping occurs, the time stamp of entering the non-privileged mode may be taken as a sampling start time, the time stamp of the post virtual machine processor trapping may be taken as a sampling end time, a time interval between the sampling start time and the sampling end time may be taken as a sampling period, or a time interval between a plurality of consecutive sampling start times and sampling end times may be taken as a sampling period.
Since the virtual machine processor is in privileged mode (root mode) and non-privileged mode (non-root mode) at intervals when in run state, this is mainly to isolate the operating rights of the vCPU. Fig. 3 is a schematic diagram illustrating data interaction between a virtual machine and a host machine. Wherein, the virtual machine operates in a non-root mode, when VM Exit occurs, the virtual machine enters the root mode, and when trapping (VM Entry) occurs, the virtual machine reenters the non-root mode. When the vCPU is in the non-root mode, relevant task codes in the virtual machine are executed, and when certain privileged operations are performed, VM Exit to the root mode can occur to execute the relevant task codes on the host. Therefore, the sampling code is placed in a time interval between the occurrence of VM Exit of the vCPU to the host machine and the re-entry of the non-root mode and the occurrence of VM Exit again, so that the sampling period is counted, only the time of the vCPU in the non-root mode is calculated, the time consumed by executing a virtualization layer instruction in the root mode is eliminated, and the finally obtained CPU utilization rate of the virtual machine can more accurately reflect the real load of the virtual machine.
Step 204, for each sampling period, a first count value of the host processor in an operating state and a first number of clock cycles of the host processor in the sampling period are obtained.
The host refers to a physical host running the virtual machine. A host processor refers to a processor in a host machine. The state of the host processor includes an operating state, which refers to the state of the host processor processing a process or program, and an idle state, which, in contrast, refers to the state of waiting for the processing process or degree.
The first count value refers to a count value that is incremented by one every other clock cycle according to the current actual operating frequency of the CPU hardware when the host processor is in an operating state during the sampling period.
The running state refers to an active state of the CPU, e.g., a C0 state, i.e., a state in which no HLT, MWAIT, etc. may enter a power saving sleep mode instruction is executed. The first count value is used to characterize the activity level of the host processor during the sampling period.
The first number of clock cycles refers to the number of cycles the host processor is in a startup state during a sampling period. The first number of clock cycles provides an accurate time reference for the computation used by the CPU.
In some embodiments, the ratio between the first count value and the first number of clock cycles in the sampling period is the usage of the host processor in the sampling period.
In step 206, a scheduling period of the virtual machine processor is determined in the sampling period, and a second count value of the host processor in an operating state and a second clock cycle number of the host processor in the scheduling period are obtained.
The scheduling time period refers to a time period in which the virtual machine processor is scheduled to execute other tasks in the sampling period. And the thread corresponding to the virtual machine processor also receives the scheduling management of the host processor in the running process of the virtual machine, so that the virtual machine processor is in a running state and an idle state.
The second count value refers to a count value of the host processor in an operating state in the scheduling period, that is, a count value of the host processor in an operating state in the scheduling period, which is incremented by one every other clock cycle according to the current actual operating frequency of the CPU hardware. Since the virtual machine processor is scheduled out during the scheduling period, the second count value is used to characterize the activity level of the non-virtual machine processor during the scheduling period.
The second number of clock cycles refers to the number of cycles the host processor is in a boot state within the scheduled time period, indicating the number of cycles the non-virtual machine processor is in a boot state. The second number of clock cycles provides an accurate time reference for the computation used by the CPU.
Step 208, calculating the utilization 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 over the plurality of sampling cycles.
And subtracting the count value of the non-virtual machine processor in the scheduling time period from the count value of the host processor in the running state in each sampling period to obtain the count value of the virtual machine processor in the running state in the sampling period.
Subtracting the second clock cycle number of the non-virtual machine processor in the scheduling time period from the first clock cycle number of the host processor in each sampling period to obtain the clock cycle number of the virtual machine processor started in the sampling period.
The utilization of the virtual machine processor is equal to the ratio between the count value of the virtual machine processor in operation during the sampling period and the number of clock cycles of the host processor during the sampling period.
In the method for determining the utilization rate of the virtual machine processor, the virtual machine processor is trapped out due to the clock interrupt on the host machine, so that a time interval for which the virtual machine processor is trapped out continuously for a preset number of times is used as a sampling period, the method for periodically sampling by using the clock interrupt of the host machine is beneficial to calculating the utilization rate of the virtual machine processor, the calculated utilization rate of the virtual machine processor can more accurately reflect the real load of the virtual machine, the first count value reflects the duration of the host processor in an operating state in the sampling period, the second clock period reflects the duration of the host processor started in the sampling period, and the virtual machine processor is scheduled and used for executing other tasks in a scheduling period, so that the utilization rate of the virtual machine processor can be accurately obtained based on the first count value, the first clock period, the second count value and the second clock period in the sampling period.
In one exemplary embodiment, obtaining a first count value of a host processor in an operating state and a first number of clock cycles of the host processor during a sampling period includes obtaining a count value of the host processor in the operating state and a number of clock cycles since a start of the host processor at a start time of the sampling period, obtaining a count value of the host processor in the operating state and a number of clock cycles since the start of the host processor at an end time of the sampling period, taking a difference between the count value of the host processor at the end time of the sampling period and the count value of the host processor at the corresponding start time as the first count value of the host processor in the operating state during the sampling period, and taking a difference between the number of clock cycles of the host processor at the end time of the sampling period and the number of clock cycles of the host processor at the corresponding start time as the first number of clock cycles of the host processor during the sampling period.
Under the condition that the sampling period comprises a time interval in which two continuous virtual machine trapping occurs, taking the moment of entering the non-privileged mode in the time interval as the starting moment of the sampling period, and taking the moment of later occurrence of the virtual machine trapping in the time interval as the ending moment of the sampling period. In the case that the sampling period includes a plurality of time intervals in which the virtual machine trapping occurs twice consecutively, the time of entering the non-privileged mode in the first time interval is taken as the start time of the sampling period, and the time of the virtual machine trapping occurs later in the last time interval is taken as the end time of the sampling period.
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 operation during the sampling period.
The difference between the number of clock cycles at the ending time of the sampling period and the number of clock cycles at the corresponding starting time indicates the first number of clock cycles of the host processor within the sampling period.
In this embodiment, the first count value of the host processor in the running state and the first clock cycle number of the host processor are calculated according to the count values of the start time and the end time in the sampling period, and because each time interval in each sampling period only includes the time of the non-privileged mode, the calculated first count value and the first clock cycle number can accurately reflect the real load condition of the host processor, which is beneficial to obtaining the accurate CPU utilization rate of the virtual machine.
In one exemplary embodiment, obtaining a count value of the host processor in an operational state and a number of clock cycles since the host processor was started includes reading the count value of the host processor in an operational state from a first register using a privileged instruction and reading the number of clock cycles since the host processor was started from a second register.
The first register stores a count value of the host processor in an operation state from the time when the host processor is started to the current time, wherein the count value is obtained by adding one to the current actual operation frequency of the CPU hardware every other clock cycle when the host processor is in the operation state. For example, the first register may be an APERF register.
The second register stores the number of clock cycles during the current time since the host processor was started. For example, the second register may be a TSC register.
The values of both the first register and the second register need to be read at the host using privileged instructions. A privileged instruction refers to an instruction that executes in a privileged mode, for example, the privileged instruction may be a rdmsr instruction.
In this embodiment, the count value of the host processor in the running state is read from the first register by the privilege instruction, and the clock cycle number of the host processor since the start is read from the second register, which is favorable for recording corresponding data in the privilege mode, and is used for accurately calculating the CPU utilization rate of the virtual machine.
In one exemplary embodiment, obtaining the second count value of the host processor in the running state and the second clock cycle number of the host processor in the scheduling period includes obtaining the count value of the host processor in the running state and the clock cycle number since the host processor started at the starting time of the scheduling period, obtaining the count value of the host processor in the running state and the clock cycle number since the host processor started at the ending time of the scheduling period, taking the difference value between the count value at the ending time and the count value at the corresponding starting time of the scheduling period as the second count value of the host processor in the running state in the scheduling period, and taking the difference value between the clock cycle number at the ending time and the clock cycle number at the corresponding starting time of the scheduling period as the second clock cycle number of the host processor in the scheduling period.
The starting time of the scheduling period refers to the time when the virtual machine monitor in the host machine schedules the virtual machine processor out and executes other tasks, for example, a kvm_ sched _out () function in a KVM (Kernel-based virtualization technology) module may be used, a count value in a first register is obtained in the kvm_ sched _out () function by reading a count value in a first register, and a clock cycle number in a second register is obtained by reading a clock cycle number in the first register, wherein the count value is in an running state from the time when the host processor is started up to the starting time of the scheduling period.
The termination time of the scheduling period refers to the time when the virtual machine monitor in the host machine schedules the virtual machine processor back and executes the task in the virtual machine, for example, the time when the host processor is in an operating state from the start of the host processor to the termination time of the scheduling period can be obtained by reading the count value in the first register in the kvm_ sched _in () function by using the kvm_ sched _in () function in the KVM module, and the clock cycle number in the second register is obtained by reading the clock cycle number in the host processor from the start to the termination time of the scheduling period.
The difference between the count value at the end of the schedule period and the count value at the start of the schedule period indicates a second count value at which the host processor is in an operational state during the schedule period.
The difference between the number of clock cycles at the ending time of the schedule period and the number of clock cycles at the corresponding starting time indicates a second number of clock cycles of the host processor within the schedule period.
In this embodiment, the second count value of the host processor in the running state and the second clock cycle number of the host processor in the scheduling period are calculated by the count values and the clock cycle numbers at the start time and the end time of the scheduling period, which are equivalent to calculating the activity condition of the non-virtual machine processor, so that the accurate utilization rate of the virtual machine processor is calculated.
In one exemplary embodiment, the computing of 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 in the plurality of sampling cycles includes subtracting a sum of the first count value, the second count value, and the second count value in the plurality of sampling cycles to obtain a count value of the virtual machine processor in an operating state, subtracting a sum of the first clock cycle number, the second clock cycle number, and taking a quotient of the count value of the virtual machine processor in the operating state and the clock cycle number of the virtual machine processor as the utilization rate of the virtual machine processor.
The utilization rate of the virtual machine processor in the sampling periods can be summed up by using a first count value in the sampling periods as a total count value of the host processor in an operating state in the sampling periods, a second count value in the sampling periods is summed up by using a second count value in the sampling periods as a count value of the non-virtual machine processor in the sampling periods, and the difference is carried out between the first count value and the second count value to obtain a result serving as the count value of the virtual machine processor in the operating state.
The sum of the first clock cycles in the sampling periods is used as the total clock cycle number started by the host processor in the sampling periods, the sum of the clock cycle numbers in the sampling periods is used as the clock cycle number started by the non-virtual machine processor in the sampling periods, the difference is obtained between the first clock cycle number and the sum of the clock cycle numbers in the sampling periods, and the obtained result is used as the clock cycle number started by the virtual machine processor.
And calculating the quotient of the count value of the running state of the virtual machine processor and the clock cycle number of the virtual machine processor, and taking the obtained result as the utilization rate of the virtual machine processor.
In this embodiment, the sum of the first count values in the multiple sampling periods is subtracted from the sum of the second count values in the multiple sampling periods to obtain the count value of the running state of the virtual machine processor, the sum of the first clock periods in the multiple sampling periods is subtracted from the sum of the second clock periods in the multiple sampling periods to obtain the clock periods of the virtual machine processor, and the two are subjected to quotient to obtain the accurate utilization rate of the virtual machine processor.
In one exemplary embodiment, the method for determining the usage rate of the virtual machine processor further includes outputting the usage rate of the virtual machine processor to a preset path.
The preset path is a path for saving the utilization rate of the virtual machine processor. Illustratively, the debugfs interface is registered under the/sys/kernel/debug/kvm path for outputting vCPU usage.
In this embodiment, the usage rate of the virtual machine processor is output to the preset path, so that the usage rate of the virtual machine processor is advantageously queried in the preset path.
In order to describe the method and effect of determining the utilization rate of the virtual machine processor in this embodiment in detail, the following description will describe one of the most detailed embodiments:
Referring to fig. 3, the usage rate determining method of the virtual machine processor is applied to a usage rate determining system of the virtual machine processor, wherein the system comprises a virtual machine and a KVM module, and the KVM module comprises a count value and clock cycle number acquisition module, a usage rate calculation module, a scheduling monitoring module and an output module. The count value and clock cycle number acquisition module is used for reading the count value and the clock cycle number in the register. The scheduling monitoring module is used for scheduling threads of the virtual machine processor. The utilization rate calculation module is used for taking a time interval for which the virtual machine processor is trapped out continuously for a preset number of times as a sampling period, acquiring a first count value of a host processor in an operating state and a first clock cycle number of the host processor in each sampling period, determining a scheduling time period of the virtual machine processor in the sampling period, acquiring a second count value of the host processor in the operating state and a second clock cycle number of the host processor in the scheduling time period, and calculating 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 in a plurality of sampling periods. The output module is used for outputting the utilization rate of the virtual machine processor to a preset path.
1. Initialization phase
A. load KVM module
And loading a KVM module on the host machine to ensure the normal operation of the virtualized environment.
It is verified whether the KVM supports MSR (Model-SPECIFIC REGISTER) register access to the physical CPU, e.g., APERF and TSC registers.
B. Creating a data structure
Defining a new data structure in the KVM kernel module for storing the following information:
The increment value of the APERF and TSC registers records the register change for each vCPU over a fixed period of time.
And the vCPU scheduling information comprises the time point when the vCPU is scheduled out and scheduled back.
VCPU usage rate, the final calculated vCPU usage rate.
Example data structure:
struct vcpu_usage_stats {
Increment value of u64_ 64 APERF _diff;// APERF register
Increment value of u64 tsc_diff;// TSC register
AAPERF increment value over the U64_ 64 APERF _ sched _diff;// non-vCPU run time period
TSC increment value during the U64 tsc_ sched _diff;// non-vCPU run time period
U64 usage_rate;// calculated vCPU usage
}。
C. registration debugfs interface
The debugfs interface is registered under the/sys/kernel/debug/kvm path for outputting vCPU usage.
2. Data acquisition phase
A. setting VM Exit sampling point
Since virtual machine processor (vCPU) threads are in two privilege modes non-root mode and root mode at intervals during runtime, this is mainly to isolate the operating rights of vCPU. The virtual machine operates in a non-root mode, and enters the root mode when VM Exit occurs, and reenters the non-root mode when VM Entry occurs. When the vCPU is in the non-root mode, relevant task codes in the virtual machine are executed, and when certain privileged operations are performed, VM Exit to the root mode can occur to execute the relevant task codes on the host. Therefore, the sampling code is placed in a time interval between the occurrence of VM Exit of the vCPU to the host machine and the re-entry of the non-root mode and the occurrence of VM Exit again, so that the sampling period is counted, only the time of the vCPU in the non-root mode is calculated, the time consumed by executing a virtualization layer instruction in the root mode is eliminated, and the finally obtained CPU utilization rate of the virtual machine can more accurately reflect the real load of the virtual machine.
B. Periodic sampling
Since a clock interrupt on the host will necessarily cause the vCPU to generate VM Exit, the clock interrupt of the host is used for periodic sampling. Every fixed number of times VM Exit (e.g. 1000 times), a sample is triggered, the register values at the START of the sampling period, aperf_start, tsc_start and at the END of the sampling period, aperf_end, tsc_end are recorded.
3. Monitoring vCPU scheduling information
During the running process of the virtual machine, the thread corresponding to the virtual machine processor (vCPU) also receives the host scheduler management, so that the virtual machine processor is in two states, namely a running state and an idle state. This step aims at counting the time that the vCPU thread is in idle state.
A. setting a dispatch sampling point
There are two hook functions in the KVM module that are invoked when the vCPU thread switches to run and idle states, kvm_ sched _in and kvm_ sched _out, respectively. In the kvm_ sched _out function, the APERF and TSC values (denoted as APERF_OUT and TSC_OUT) are recorded when the vCPU thread is scheduled OUT.
IN the kvm_ sched _in function, the APERF and TSC values (noted as APERF_IN and TSC_IN) when the vCPU thread is scheduled back are recorded.
B. calculating a register increment value of the non-virtual machine processor in an operating state using a formula:
APERF_SCHED_DIFF=APERF_IN-APERF_OUT
TSC_SCHED_DIFF=TSC_IN-TSC_OUT
4. calculating vCPU utilization
A. Calculating the total increment value
In a fixed time period, calculating a total increment value through the APERF and TSC values acquired by sampling points:
APERF_DIFF=APERF_END-APERF_START
TSC_DIFF=TSC_END- SC_START
b. Deducting the register increment value of the non-virtual machine processor in the running state:
APERF_ACTUAL=APERF_DIFF-APERF_SCHED_DIFF
TSC_ACTUAL=TSC_DIFF-TSC_SCHED_DIFF
c. calculating vCPU usage using a formula:
USAGE_RATE=(APERF_ACTUAL/TSC_ACTUAL)×100%
Updating data structures
The calculation result is updated into vcpu _use_stats data structure.
5. Output vCPU utilization
And outputting the calculated vCPU utilization rate to a preset path through debugfs interfaces, wherein the preset path is:/sys/kernel/debug/kvm/vCPU _use.
According to the method for determining the utilization rate of the virtual machine processor, the clock interrupt on the host machine can cause the virtual machine processor to be trapped out, the virtual machine processor can be switched from the non-privileged mode to the privileged mode when the virtual machine processor is trapped out, therefore, a time interval for which the virtual machine processor is trapped out continuously for a preset number of times is used as a sampling period, the method for periodically sampling by using the clock interrupt of the host machine only comprises the time of the virtual machine processor in the non-privileged mode, the time consumption of the virtual machine processor in the privileged mode is eliminated, the calculated utilization rate of the virtual machine processor can be reflected more accurately, the first count value reflects the time length of the host processor in an operation state in the sampling period, the second clock cycle number reflects the time length of the host processor started in the sampling period, and the virtual machine processor is scheduled and used for executing other tasks in a scheduling period. Meanwhile, the scheme is directly based on hardware-level data acquisition, so that estimation errors of a software level are avoided, the virtual machine does not need to enter frequently, extra system overhead is reduced, the virtual machine runs without perception and performance and stability are not affected, and the scheme can be applied to virtual machines of any kernel version because the kernel of the virtual machine does not need to be subjected to invasive modification.
It should be understood that, although the steps in the flowcharts related to the embodiments described above are sequentially shown as indicated by arrows, these steps are not necessarily sequentially performed in the order indicated by the arrows. The steps are not strictly limited to the order of execution unless explicitly recited herein, and the steps may be executed in other orders. Moreover, at least some of the steps in the flowcharts described in the above embodiments may include a plurality of steps or a plurality of stages, which are not necessarily performed at the same time, but may be performed at different times, and the order of the steps or stages is not necessarily performed sequentially, but may be performed alternately or alternately with at least some of the other steps or stages.
Based on the same inventive concept, the embodiment of the application also provides a device for determining the utilization rate of the virtual machine processor, which is used for realizing the method for determining the utilization rate of the virtual machine processor. The implementation of the solution provided by the apparatus is similar to the implementation described in the above method, so the specific limitation in the embodiments of the usage determining apparatus for one or more virtual machine processors provided below may refer to the limitation of the usage determining method for a virtual machine processor hereinabove, and will not be repeated herein.
In one exemplary embodiment, as shown in FIG. 4, there is provided a utilization determination apparatus 400 of a virtual machine processor, including a determination module 420, a first acquisition module 440, a second acquisition module 460, and a calculation module 480, wherein:
A determining module 420, configured to take a time interval for which the virtual machine processor is trapped out continuously for a preset number of times as a sampling period;
a first obtaining module 440, configured to obtain, for each sampling period, a first count value of the host processor in an operating state and a first clock cycle number of the host processor in the sampling period;
A second obtaining module 460, configured to determine a scheduling period of the virtual machine processor in the sampling period, and obtain a second count value of the host processor in an operating state and a second clock cycle number of the host processor in the scheduling period;
a calculating module 480 for calculating a 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 in the plurality of sampling cycles.
According to the utilization rate determining device of the virtual machine processor, the clock interrupt on the host machine can cause the virtual machine processor to be trapped out, the virtual machine processor can be switched from the non-privileged mode to the privileged mode when the virtual machine processor is trapped out, therefore, a time interval for which the virtual machine processor is trapped out continuously for a preset number of times is used as a sampling period, the method for periodically sampling by using the clock interrupt of the host machine only comprises the time of the virtual machine processor in the non-privileged mode, the time consumption of the virtual machine processor in the privileged mode is eliminated, the calculated utilization rate of the virtual machine processor can be reflected more accurately, the first count value reflects the time length of the host processor in an operation state in the sampling period, the second clock cycle number reflects the time length of the host processor started in the sampling period, and the virtual machine processor is scheduled and used for executing other tasks in a scheduling period.
In one embodiment, the first obtaining module 440 is further configured to obtain, at a start time of the sampling period, a first count value of the host processor in an operating state and a first clock cycle number of the host processor, obtain, at an end time of the sampling period, a count value of the host processor in the operating state and a clock cycle number of the host processor since the start, take a difference between the count value at the end time and the count value at the corresponding start time of the sampling period as the first count value of the host processor in the operating state in the sampling period, and take a difference between the clock cycle number at the end time and the clock cycle number at the corresponding start time of the sampling period as the first clock cycle number of the host processor in the sampling period.
In one embodiment, the first obtaining module 440 is further configured to use the privileged instruction to read the count value of the host processor in the running state from the first register and the number of clock cycles since the host processor was started from the second register.
In one embodiment, the second obtaining module 460 is further configured to obtain, at a start time of the scheduling period, a second count value of the host processor in an operating state and a second clock cycle number of the host processor in the scheduling period, obtain, at an end time of the scheduling period, a count value of the host processor in the operating state and a clock cycle number of the host processor since the start, take a difference between the count value of the host processor in the operating state and the count value of the host processor in the corresponding start time in the end time of the scheduling period as the second count value of the host processor in the operating state in the scheduling period, and take a difference between the clock cycle number of the host processor in the end time and the clock cycle number of the corresponding start time in the scheduling period as the second clock cycle number of the host processor in the scheduling period.
In one embodiment, the computing module 480 is further configured to calculate a 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 in the plurality of sampling cycles, subtract the sum of the first count value and the second count value in the plurality of sampling cycles to obtain a count value of the virtual machine processor in an operating state, subtract the sum of the first clock cycle number and the second clock cycle number in the plurality of sampling cycles to obtain a clock cycle number of the virtual machine processor, and take a quotient of the count value of the virtual machine processor in the operating state and the clock cycle number of the virtual machine processor as the utilization rate of the virtual machine processor.
In one embodiment, the utilization rate determining apparatus 400 of the virtual machine processor further includes an output module for outputting the utilization rate of the virtual machine processor to a preset path.
The above-described respective modules in the usage determining device of the virtual machine processor may be implemented in whole or in part by software, hardware, and combinations thereof. The above modules may be embedded in hardware or may be independent of a processor in the computer device, or may be stored in software in a memory in the computer device, so that the processor may call and execute operations corresponding to the above modules.
In one exemplary embodiment, a computer device is provided, which may be a terminal, and an internal structure diagram thereof may be as shown in fig. 5. The computer device includes a processor, a memory, an input/output interface, a communication interface, a display unit, and an input means. 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. Wherein the processor of the computer device is configured 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 a computer program. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage media. The input/output interface of the computer device is used to exchange information between the processor and the external device. The Communication interface of the computer device is used for conducting wired or wireless Communication with an external terminal, and the wireless Communication can be realized through WIFI, a mobile cellular network, near field Communication (NEAR FIELD Communication) or other technologies. The computer program, when executed by a processor, implements a method for determining usage of a virtual machine processor. The display unit of the computer device is used for forming a visual picture, and 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, and the input device of the computer equipment can be a touch layer covered on the display screen, can also be a key, a track ball or a touch pad arranged on the shell of the computer equipment, and can also be an external keyboard, a touch pad or a mouse and the like.
It will be appreciated by those skilled in the art that the structure shown in FIG. 5 is merely a block diagram of some of the structures associated with the present inventive arrangements and is not limiting of the computer device to which the present inventive arrangements may be applied, and that a particular computer device may include more or fewer components than shown, or may combine some of the components, or have a different arrangement of components.
In an exemplary embodiment, a computer device is provided, comprising a memory and a processor, the memory having stored therein a computer program, the processor performing the steps of the method embodiments described above when the computer program is executed.
In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored which, when executed by a processor, carries out the steps of the method embodiments described above.
In an embodiment, a computer program product is provided, comprising a computer program which, when executed by a processor, implements the steps of the method embodiments described above.
It should be noted that, the user information (including but not limited to user equipment information, user personal information, etc.) and the data (including but not limited to data for analysis, stored data, presented data, etc.) related to the present application are both information and data authorized by the user or sufficiently authorized by each party, and the collection, use and processing of the related data are required to meet the related regulations.
Those skilled in the art will appreciate that implementing all or part of the above described methods may be accomplished by way of a computer program stored on a non-transitory computer readable storage medium, which when executed, may comprise the steps of the embodiments of the methods described above. Any reference to memory, database, or other medium used in embodiments provided herein may include at least one of non-volatile memory and volatile memory. The nonvolatile Memory may include Read-Only Memory (ROM), magnetic tape, floppy disk, flash Memory, optical Memory, high density embedded nonvolatile Memory, resistive random access Memory (RESISTIVE RANDOM ACCESS MEMORY, reRAM), magneto-resistive Memory (Magnetoresistive Random Access Memory, MRAM), ferroelectric Memory (Ferroelectric Random Access Memory, FRAM), phase change Memory (PHASE CHANGE Memory, PCM), graphene Memory, and the like. Volatile memory can include random access memory (Random Access Memory, RAM) or external cache memory, and the like. By way of illustration, and not limitation, RAM can be in various forms such as static random access memory (Static Random Access Memory, SRAM) or dynamic random access memory (Dynamic Random Access Memory, DRAM), etc. The databases referred to in the embodiments provided herein may include at least one of a relational database and a non-relational database. The non-relational database may include, but is not limited to, a blockchain-based distributed database, and the like. The processor referred to in the embodiments provided in the present application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic unit, a data processing logic unit based on quantum computation, an artificial intelligence (ARTIFICIAL INTELLIGENCE, AI) processor, or the like, but is not limited thereto.
The technical features of the above embodiments may be arbitrarily combined, and all possible combinations of the technical features in the above embodiments are not described for brevity of description, however, as long as there is no contradiction between the combinations of the technical features, they should be considered as the scope of the present application.
The foregoing examples illustrate only a few embodiments of the application and are described in detail herein without thereby limiting the scope of the application. It should be noted that it will be apparent to those skilled in the art that several variations and modifications can be made without departing from the spirit of the application, which are all within the scope of the application. Accordingly, the scope of the application should be assessed as that of the appended claims.