A method for switching VCPU and physical host
By creating a VCPU queue in the physical host and binding it to the thread, the problem of low VCPU switching efficiency in the existing technology is solved, achieving more efficient VCPU switching, reducing switching latency, and meeting high real-time requirements.
Patent Information
- Application Number
- CN201611245500.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2016-12-29
- Publication Date
- 2025-12-30
- Estimated Expiration
- 2036-12-29
AI Technical Summary
In existing technologies, VCPU switching efficiency is low and time overhead is high, mainly due to the long Host OS thread switching time and the limitation of thread scheduling framework and scheduling algorithm.
By creating VCPU queues and binding them to one or more thread bindings, a thread can be bound to multiple VCPU queues, and a VCPU queue can be bound to one thread. A thread can correspond to multiple VCPUs. When a VCPU switch is needed, unlike existing technologies where thread switching is necessary because the target VCPU is unknown, the technical solution provided in this invention, after the thread saves the state of the currently running VCPU, can determine a target VCPU from the VCPU queue where the currently running VCPU resides. Thus, the target VCPU is determined, and its corresponding thread remains the same as the thread in its own VCPU queue. No thread switching is required; the thread simply restores the state of the target VCPU to complete the VCPU switch.
The process of thread switching during VCPU switching is omitted, saving a lot of time and improving VCPU switching efficiency. In actual tests, the VCPU switching latency on the ARM64 platform was reduced from 4.5us to 2.5us, meeting the requirements of high real-time VCPU switching.
Smart Images

Figure CN108255572B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer virtualization technology, and in particular to a VCPU switching method and a physical host. Background Technology
[0002] Virtualization refers to the process of abstracting a single resource on a physical machine into multiple copies through spatial partitioning and time-sharing simulation. A virtual machine (VM) is an independent virtual computer system provided by a virtualization layer. Each VM has its own virtual hardware, including CPU, memory, and I / O (input / output) devices. Through the simulation of the virtualization layer, the VM appears as a real physical device to the upper-layer software.
[0003] The virtualization layer is generally called the Virtual Machine Monitor (VMM). CPU virtualization is the most crucial step in the VMM's virtualization of machine hardware resources. CPU virtualization creates one or more VCPUs (Virtual CPUs) for each virtual machine, and these VCPUs time-sharing / space-sharing the physical CPU. The VMM allocates time slices / spaces to multiple VCPUs and maintains the state of all VCPUs. When a VCPU's time slice / space expires and a switch is needed, the state of the currently running VCPU is saved, and the state of the target VCPU (the VCPU to which it is switching) is restored; that is, the state of the target VCPU is loaded into the physical CPU. The state of a VCPU refers to its context, which refers to the contents of the VCPU's registers and program counter at a specific point in time.
[0004] CPU virtualization maps each VCPU to a Host OS (the operating system of the physical device itself) thread, which then calls the VCPU. Figure 1 The diagram illustrates the VCPU switching process in the prior art. When switching from one VCPU (VCPUA) to another VCPU (VCPUB), the state of VCPUA is first saved in memory. Then, the Host OS switches the thread corresponding to VCPUA to another thread corresponding to the VCPU according to the thread scheduling algorithm. The VCPU corresponding to the thread that is switched to naturally becomes the target VCPU (VCPUB). Then, the context of VCPUB is retrieved and its context is restored. Figure 1The example shown is based on KVM (Kernel-based Virtual Machine) on the LINUX system. Program A corresponds to VCPUA, program B corresponds to VCPUB, the program is on the GUEST side, and the thread is on the HOST side.
[0005] Therefore, the VCPU switching process includes the time to save the VCPUA state and restore the VCPUB state, as well as the time for switching the Host OS threads. The Host OS thread switching time is usually very long, and the Host OS thread switching is managed by the Host OS's thread scheduling policy, which makes the switching latency limited by the Host OS's thread scheduling framework and scheduling algorithm, resulting in low efficiency and high time overhead for VCPU switching. Summary of the Invention
[0006] This invention provides a VCPU switching method and a physical host to solve the problem of low VCPU switching efficiency in the prior art.
[0007] In a first aspect, embodiments of the present invention provide a VCPU switching method. The method is applied to a physical host running at least one VCPU. The physical host includes at least one VCPU queue, each VCPU queue including at least one VCPU, and the at least one VCPU queue is managed by at least one thread. The method includes: saving the state of a first VCPU, which is the currently running VCPU; determining a second VCPU from the VCPU queue where the first VCPU resides, which is the VCPU to be run; and restoring the state of the second VCPU. The method is executed by the thread, which is bound to a VCPU queue. This method does not require thread switching; instead, the thread determines a target VCPU (the second VCPU) from the bound VCPU queue, saves the state of the currently running VCPU, restores the state of the target VCPU, and completes the VCPU switching, thereby significantly reducing the VCPU switching latency and improving VCPU switching efficiency.
[0008] In one possible design, determining the second VCPU from the queue of the first VCPU includes: if there are VCPUs to be run in the queue of the first VCPU, selecting one of the VCPUs to be run as the second VCPU; if there are no VCPUs to be run in the queue of the first VCPU, selecting one VCPU to be run from other VCPU queues and migrating the selected VCPU to be run to the queue of the first VCPU, wherein the selected VCPU to be run is the second VCPU.
[0009] In one possible design, determining the second VCPU from the VCPU queue where the first VCPU resides includes: if there exists a preferred VCPU with a higher priority than all the VCPUs in the VCPU queue where the first VCPU resides, then one of the preferred VCPUs is selected as the selected VCPU, and the selected VCPU is moved to the VCPU queue where the first VCPU resides; the selected VCPU is the second VCPU. If there is no preferred VCPU, then one of the VCPUs in the VCPU queue where the first VCPU resides is selected as the second VCPU. The priority of a VCPU is set when the VCPU is created, and VCPUs with higher priority are used first.
[0010] Specifically, in one possible design, determining the second VCPU from the VCPU queue where the first VCPU is located may include: determining whether there is a VCPU to be run in the VCPU queue where the first VCPU is located; if there is a VCPU to be run in the VCPU queue where the first VCPU is located, determining whether there is a preferred VCPU to be run in other VCPU queues outside the VCPU queue where the first VCPU is located, with a higher priority than all the VCPUs to be run in the VCPU queue where the first VCPU is located; if there is a preferred VCPU to be run, selecting one of the preferred VCPUs to be run as the selected VCPU, and migrating the selected VCPU to the VCPU queue where the first VCPU is located, the selected VCPU being the second VCPU; if there is no preferred VCPU to be run, selecting one of the VCPUs to be run as the second VCPU; if there is no VCPU to be run in the VCPU queue where the first VCPU is located, selecting a VCPU to be run from other VCPU queues, and migrating the selected VCPU to be run to the VCPU queue where the first VCPU is located, the selected VCPU being the target VCPU.
[0011] In one possible design, if there are VCPUs waiting to be run in the VCPU queue where the first VCPU is located, the step of selecting one of the VCPUs waiting to be run as the second VCPU includes: randomly selecting one of the VCPUs waiting to be run as the second VCPU; or selecting the VCPU with the highest priority among the VCPUs waiting to be run as the second VCPU; or selecting the VCPU that first entered the VCPU queue as the second VCPU. This can prevent the VCPU from being idle for too long.
[0012] In one possible design, if the preferred VCPU to run exists, before migrating the selected VCPU to the VCPU queue where the first VCPU resides, the process further includes: if the VCPU queue where the first VCPU resides is full, removing one or more VCPUs from the VCPU queue other than the first VCPU; if the VCPU queue where the first VCPU resides is not full, migrating the selected VCPU to the VCPU queue corresponding to the first VCPU. This prevents the inability to use VCPUs with higher priority than all VCPUs in the VCPU queue due to the VCPU queue being full.
[0013] In one possible design, removing one or more VCPUs from the VCPU queue other than the first VCPU includes: randomly removing one or more VCPUs from the VCPU queue other than the first VCPU; or removing the lowest priority VCPU from the VCPU queue other than the first VCPU; or removing multiple lowest priority VCPUs from the VCPU queue other than the first VCPU; wherein, the removal is either deleting the VCPU or migrating the VCPU to another VCPU queue outside the VCPU queue where the first VCPU is located, and the other VCPU queue is a VCPU queue that is not full.
[0014] In one possible design, to avoid imbalances between VCPU queues—that is, some VCPU queues have too many VCPUs while others have too few, resulting in some VCPU queues having busy threads while others have relatively idle threads—the VCPU switching method further includes: if there is an idle VCPU queue with a number of VCPUs less than a preset lower limit, and a busy VCPU queue with a number of VCPUs greater than a preset upper limit, selecting n VCPUs to be run from the busy VCPU queue; and migrating the selected n VCPUs to be run to the idle VCPU queue; wherein n is equal to the number of VCPUs in the busy VCPU queue minus the preset upper limit.
[0015] In one possible design, selecting n VCPUs to be run from the busy VCPU queue includes: randomly selecting n VCPUs to be run from the busy VCPU queue; or, selecting the first n VCPUs to be run in descending order of priority from the busy VCPU queue.
[0016] Secondly, embodiments of the present invention provide a physical host, the physical host comprising: a hardware layer, a virtual machine monitor (VMM) running on the hardware layer, and virtual machines running on the VMM, the virtual machines including VCPUs, and may also include virtual memory and virtual hard disks, etc. The hardware layer includes at least one processor and a storage device, the storage device including physical memory, and may also include a physical hard disk. The VMM includes at least one VCPU queue and a VCPU queue management module, wherein the VCPU queue stores at least one VCPU, the VCPU being used to run programs using the processor's resources.
[0017] The VCPU queue management module is used to manage the VCPU queue corresponding to the VCPU queue management module. Specifically, the VCPU queue management module is used to: save the state of the first VCPU, which is the currently running VCPU, and save the state of the first VCPU to physical memory; determine the second VCPU from the VCPU queue where the first VCPU is located, which is the VCPU to be run; and restore the state of the second VCPU, and restore the state of the second VCPU saved in physical memory.
[0018] In one possible design, when the VCPU queue management module determines the second VCPU from the VCPU queue where the first VCPU is located, it specifically performs the following: if there is a VCPU to be run in the VCPU queue where the first VCPU is located, select one of the VCPUs to be run as the second VCPU; if there is no VCPU to be run in the VCPU queue where the first VCPU is located, select a VCPU to be run from other VCPU queues and migrate the selected VCPU to be run to the VCPU queue where the first VCPU is located, wherein the selected VCPU to be run is the second VCPU.
[0019] In one possible design, when the VCPU queue management module determines the second VCPU from the VCPU queue where the first VCPU is located, it specifically performs the following: if there is a preferred VCPU with a higher priority than all the VCPUs to be run in the VCPU queue where the first VCPU is located, then select one of the preferred VCPUs as the selected VCPU and migrate the selected VCPU to the VCPU queue where the first VCPU is located, and the selected VCPU is the second VCPU; if there is no preferred VCPU to be run, select one of the VCPUs to be run in the VCPU queue where the first VCPU is located as the second VCPU.
[0020] Specifically, in one possible design, when the VCPU queue management module determines the second VCPU from the VCPU queue where the first VCPU is located, it specifically performs the following steps: determining whether there is a VCPU to be run in the VCPU queue where the first VCPU is located; if there is a VCPU to be run in the VCPU queue where the first VCPU is located, determining whether there is a preferred VCPU to be run with a higher priority than all the VCPUs to be run in the VCPU queue where the first VCPU is located in other VCPU queues; if there is a preferred VCPU to be run, selecting one of the preferred VCPUs to be run as the selected VCPU, and migrating the selected VCPU to the VCPU queue where the first VCPU is located, the selected VCPU is the second VCPU; if there is no preferred VCPU to be run, selecting one of the VCPUs to be run as the second VCPU; if there is no VCPU to be run in the VCPU queue where the first VCPU is located, selecting a VCPU to be run from other VCPU queues, and migrating the selected VCPU to be run to the VCPU queue where the first VCPU is located, the selected VCPU to be run is the target VCPU.
[0021] In one possible design, if there are VCPUs to be run in the VCPU queue where the first VCPU is located, the VCPU queue management module, when selecting one of the VCPUs to be run as the second VCPU, specifically performs the following: randomly selects one of the VCPUs to be run as the second VCPU; or selects the VCPU with the highest priority among the VCPUs to be run as the second VCPU; or selects the VCPU to be run that first enters the VCPU queue as the second VCPU.
[0022] In one possible design, if there is a preferred VCPU to run, the VCPU queue management module is further configured to: if the VCPU queue where the first VCPU is located is full, remove one or more VCPUs other than the first VCPU from the VCPU queue; if the VCPU queue where the first VCPU is located is not full, migrate the selected VCPU to the VCPU queue where the first VCPU is located.
[0023] In one possible design, when the VCPU queue management module removes one or more VCPUs from the VCPU queue other than the first VCPU, it is specifically configured to: randomly remove one or more VCPUs from the VCPU queue other than the first VCPU; or remove the lowest priority VCPU from the VCPU queue other than the first VCPU; or remove multiple VCPUs from the VCPU queue other than the first VCPU; wherein, the removal is to delete the VCPU or migrate the VCPU to another VCPU queue outside the VCPU queue where the first VCPU is located, and the other VCPU queue is a VCPU queue that is not full.
[0024] In one possible design, the VCPU queue management module is further configured to: if there is an idle VCPU queue with a number of VCPUs less than a preset lower limit and a busy VCPU queue with a number of VCPUs greater than a preset upper limit, select n VCPUs to be run from the busy VCPU queue; migrate the selected n VCPUs to be run to the idle VCPU queue; wherein n is equal to the number of VCPUs in the busy VCPU queue minus the preset upper limit.
[0025] In one possible design, when the VCPU queue management module selects n VCPUs to be run from the busy VCPU queue, it specifically performs the following: randomly selects n VCPUs to be run from the busy VCPU queue; or, selects the first n VCPUs to be run according to the order of priority of the VCPUs to be run in the busy VCPU queue from largest to smallest.
[0026] The technical solution provided by this invention creates a VCPU queue and binds the VCPU queue to at least one thread, allowing one thread to correspond to multiple VCPUs. When a VCPU switch is needed, unlike existing technologies where thread switching is necessary due to the unknown target VCPU, the technical solution provided by this invention, after the thread saves the state of the currently running VCPU, can determine a target VCPU from the VCPU queue where the currently running VCPU resides. Thus, the target VCPU is determined, and its corresponding thread remains the same as the thread in its original VCPU queue, eliminating the need for thread switching. The thread restores the state of the target VCPU to complete the VCPU switch. The target VCPU can be a waiting VCPU existing in the VCPU queue or a waiting VCPU migrated from another VCPU queue. The technical solution provided by this invention omits the thread switching process during VCPU switching, thereby saving significant time and improving VCPU switching efficiency. Attached Figure Description
[0027] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0028] Figure 1 This is an example diagram of the existing VCPU switching process;
[0029] Figure 2 This is a schematic diagram of the physical host provided in an embodiment of the present invention;
[0030] Figure 3 This is a schematic diagram illustrating the principle of the VCPU switching method provided in an embodiment of the present invention;
[0031] Figure 4 A flowchart illustrating a VCPU switching method provided in an embodiment of the present invention is shown.
[0032] Figure 5 A flowchart illustrating one embodiment of step S402 is shown;
[0033] Figure 6 A flowchart illustrating another implementation of step S402 is shown;
[0034] Figure 7 A flowchart illustrating another VCPU switching method provided in an embodiment of the present invention is shown;
[0035] Figure 8 This diagram illustrates the structure of a VCPU queue management module provided in an embodiment of the present invention.
[0036] Figure 9 This illustrates one configuration of the target VCPU determination unit U802;
[0037] Figure 10 Another component of the target VCPU determination unit U802 is shown;
[0038] Figure 11 A schematic diagram of another VCPU queue management module provided in an embodiment of the present invention is shown. Detailed Implementation
[0039] In existing technologies, each VCPU is bound to a thread during VCPU creation. Furthermore, during VCPU switching, since the target VCPU is unknown, the system's thread scheduling algorithm must first switch the thread before a target VCPU corresponding to the switched target thread can be obtained. The method and physical host provided in this invention create a VCPU queue, storing VCPUs in it. Each VCPU queue is bound to at least one thread, allowing one thread to correspond to multiple VCPUs. A thread runs only one VCPU at a time, but it can run on multiple VCPUs stored in the VCPU queue. When a VCPU switch occurs, another VCPU is selected as the target VCPU from the queue of the currently running VCPU, thus determining the target VCPU. The thread corresponding to the target VCPU remains the same, eliminating the need for thread switching and saving VCPU switching time, thereby improving VCPU switching efficiency.
[0040] Figure 2This is a schematic diagram of the physical host provided in an embodiment of the present invention. The physical host includes a hardware layer 100, a virtual machine monitor (VMM) 200 running on the hardware layer, and virtual machines (VMs) 300 running on the VMM. The hardware layer includes at least one physical CPU 110, and also includes storage devices, such as physical memory 120 and hard disk 130, and may also include a network interface device 140; the VMM includes at least one VCPU queue management module 210, which manages its corresponding VCPU queue (not shown in the figure); the VM includes at least one VCPU 310, and the VM typically also includes virtual memory 320 and virtual hard disk 330, on which a guest operating system 340 can run. In the physical host, a VCPU queue management module is bound to a code executor, such as a physical CPU, CORE (the physical core of the CPU), or hyper-threading, specifically it can be bound to the smallest code executor of the physical host. The VMM creates a VCPU queue and binds it to a VCPU queue management module. Then, it creates VCPUs and stores them as elements in the VCPU queue. The number of VCPUs stored in each VCPU queue can be randomly allocated by the VMM, or it can be allocated according to a preset allocation rule, such as evenly distributing VCPUs across the queues. Binding the VCPU queue management module to a VCPU queue means that the VCPU queue management module's pointer points to the VCPU queue. When calling a VCPU, the VCPU queue management module accesses a specific element (i.e., VCPU) in the queue through the pointer. The VCPU queue management module is used to execute VCPU switching-related steps; specific steps can be found in the VCPU switching method. The minimum code execution body can be a core or a hyper-threaded entity, depending on the actual minimum code execution body of the current physical host. Binding threads to the minimum code execution body is more beneficial for improving program performance and CPU utilization than binding to a non-minimum code execution body.
[0041] During VCPU switching, the VCPU queue management module saves the state of the VCPUs to be run in physical memory. Then, it determines a target VCPU from the queue containing the VCPUs to be run, restores the target VCPU's state (i.e., retrieves the target VCPU's state from physical memory), and loads it onto the physical CPU. The target VCPU then begins using the physical CPU's resources to run its corresponding program. This program is installed on the virtual machine, and the specific programs corresponding to each VCPU are determined by the physical host system based on program execution algorithms. The VCPU queue management module is a thread of the physical host.
[0042] Figure 3This is a schematic diagram illustrating the principle of the VCPU switching method provided in an embodiment of the present invention, which is applied to the aforementioned physical host. Figure 2 In this context, the CORE represents the smallest executable code unit. Each CORE corresponds to a VCPU Queue and a Host thread (as shown in the diagram). Each VCPU queue is managed by a single thread, and each VCPU queue can contain multiple VCPUs, such as... Figure 3 The first VCPU queue contains VCPU0-VCPU3, the second VCPU queue contains VCPU4-VCPU6, and the third VCPU queue contains VCPU7 and VCPU8. The VCPU queues are managed by the VMM. On the Host side, there may be one VCPU in the VCPU queue that is in use and running. On the Guest side, the VCPU is called by the thread bound to its VCPU queue.
[0043] In existing technologies, when a system determines that a VCPU switch is needed, the system scheduler selects the target thread to switch to based on a scheduling algorithm, then switches the current thread to the target thread, and the target thread calls its bound VCPU. In the physical host provided in this embodiment of the invention, when the system determines that a VCPU switch is needed, the thread calling the currently running VCPU (i.e., the thread bound to the VCPU queue where the currently running VCPU resides) saves the state of the currently running VCPU, determines the target VCPU from the VCPU queue, and since the target VCPU is in the queue, the thread can directly restore the state of the target VCPU, thus completing the VCPU switch. This process does not require thread switching, thereby reducing the time consumption of VCPU switching.
[0044] Specifically, in existing technologies, after receiving a system switch message, a thread directly executes program code that saves the state of its bound VCPU, then switches to the target thread, which then directly executes code to restore the state of its bound VCPU. In the physical host provided in this application embodiment, after saving the state of the currently running VCPU in the thread's execution code, a function is added that uses the VCPU queue and its elements as objects to determine the target VCPU. After determining the target VCPU, the object for restoring the VCPU state and calling the VCPU is the target VCPU, not the previously running VCPU, and both the target VCPU and the currently running VCPU are elements in the VCPU queue. If the thread cannot obtain the target VCPU and cannot determine it, the thread enters a sleep state.
[0045] The VCPU switching method provided in this embodiment of the invention is based on the aforementioned physical host. Figure 4 The following is a flowchart illustrating a VCPU switching method provided by an embodiment of the present invention, as shown below. Figure 4 As shown, the method includes:
[0046] Step S401: Save the state of the first VCPU, which is the currently running VCPU; Step S402: Determine the second VCPU from the VCPU queue where the first VCPU is located, which is the VCPU to be run.
[0047] Step S403: Restore the state of the second VCPU.
[0048] When switching VCPUs, a thread must first save the state of the first VCPU, that is, the contents of the registers and program counter of the first VCPU. The first VCPU is the currently running VCPU. The second VCPU is the target VCPU, which is the VCPU to be run. After the thread determines the target VCPU, it restores the state of the target VCPU to be run. Threads bound to the VCPU queue of the currently running VCPU will stop calling the currently running VCPU and instead call the target VCPU.
[0049] The determination of the target VCPU is achieved in step S402. Figure 5 A flowchart illustrating one embodiment of step S502 is shown. Specifically, in one embodiment, as follows: Figure 5 As shown, determining the second VCPU from the VCPU queue where the first VCPU is located includes:
[0050] Step S5241: Determine whether there is a VCPU to be run in the VCPU queue where the first VCPU is located;
[0051] Step S5242: If there are VCPUs to be run in the VCPU queue where the first VCPU is located, select one of the VCPUs to be run as the second VCPU.
[0052] Step S5243: If there is no VCPU to be run in the VCPU queue where the first VCPU is located, then select a VCPU to be run from other VCPU queues and migrate the selected VCPU to be run to the VCPU queue where the first VCPU is located. The selected VCPU to be run is the second VCPU.
[0053] In this context, the VCPUs to be run are the unused VCPUs in the VCPU queue. When a VCPU switch is to be performed, the VCPU queue may contain multiple VCPUs or only one VCPU. Therefore, it is first determined whether there are any VCPUs to be run in the queue of the first VCPU, which is the currently running VCPU. If the VCPU queue contains only one VCPU, then this VCPU is the currently running VCPU, and there are no VCPUs to be run in the queue. If the VCPU queue contains more than one VCPU, then there are VCPUs to be run in addition to the currently running VCPU. If there are VCPUs to be run in the VCPU queue, then one of the VCPUs to be run is selected as the second VCPU, which is the target VCPU. The target VCPU can be selected randomly from among the VCPUs to be run; or the VCPU with the highest priority among the VCPUs to be run can be selected as the target VCPU; or the VCPU to be run that first entered the VCPU queue can be selected as the target VCPU. The priority of VCPUs in the VCPU queue is set by the system algorithm when a VCPU is created. Higher-priority VCPUs are used first. Therefore, if a VCPU has a priority, the highest-priority VCPU in the VCPU queue can be selected as the target VCPU. The VCPU that first enters the VCPU queue refers to the VCPU that is stored in the VCPU queue first after the VCPU queue is created. Selecting the VCPU that enters the VCPU queue first can prevent VCPUs from being idle for too long.
[0054] If there is no VCPU to be run in the VCPU queue of the currently running VCPU, then a VCPU to be run is selected from other VCPU queues. These other VCPU queues can be any VCPU queue other than the one currently running VCPU. Selecting a VCPU to be run can be done by randomly selecting one of the VCPUs from the other VCPU queues; or by selecting the highest-priority VCPU from the other VCPU queues; or by selecting the VCPU that first entered the VCPU queue from the other VCPU queues. Then, the selected VCPU to be run is moved to the VCPU queue of the currently running VCPU, and the selected VCPU is designated as the target VCPU. This migration can be done by adding the selected VCPU to the linked list of the VCPU queue of the currently running VCPU.
[0055] If a VCPU has a priority, the higher-priority VCPU is used first. If there is a VCPU in another queue with a higher priority than the VCPU waiting to be run in the current VCPU's queue, then even if there is a VCPU waiting to be run in the current VCPU's queue, the higher-priority VCPU in the other queue can be used as the target VCPU. Therefore, in another implementation, step S402 is as follows: Figure 6 As shown, it may include:
[0056] Step S4251: Determine whether there is a VCPU to be run in the VCPU queue where the first VCPU is located;
[0057] Step S4252: If there are VCPUs to be run in the VCPU queue where the first VCPU is located, determine whether there is a preferred VCPU to be run with a higher priority than all the VCPUs to be run in the VCPU queue where the first VCPU is located in other VCPU queues.
[0058] Step S4253: If there is a preferred VCPU to be run, select one of the preferred VCPUs to be run as the selected VCPU, and move the selected VCPU to the VCPU queue where the first VCPU is located. The selected VCPU is the second VCPU.
[0059] If the VCPU queue containing the currently running VCPU has pending VCPUs, but other VCPU queues contain pending VCPUs with a higher priority than any of the pending VCPUs in the currently running VCPU queue, then the higher-priority pending VCPU is selected first. The pending VCPU with a higher priority than the currently running VCPU in its queue is called the preferred pending VCPU. If a preferred pending VCPU exists in other VCPU queues, one of these is selected as the chosen VCPU. Specifically, one can be randomly selected, or the highest-priority preferred pending VCPU can be selected. This selected preferred pending VCPU is then moved to the VCPU queue containing the currently running VCPU. The chosen VCPU is the second VCPU, or the target VCPU. In this way, by switching VCPUs, higher-priority VCPUs can be prioritized, thus better utilizing system resources.
[0060] For step S4253, before migrating the selected VCPU to the VCPU queue where the first VCPU resides, the following steps may also be included:
[0061] (a1) Determine whether the VCPU queue where the first VCPU is located is full;
[0062] (a2) If the VCPU queue where the first VCPU is located is full, remove one or more VCPUs from the VCPU queue other than the first VCPU.
[0063] (a3) If the VCPU queue where the first VCPU is located is not full, the selected VCPU will be moved to the VCPU queue where the first VCPU is located.
[0064] When creating a VCPU queue, you can set the size of the VCPU queue, which is the maximum number of VCPUs that the VCPU queue can store. Under normal circumstances, the VCPU queue will not be full. However, to prevent the VCPU queue from overflowing, before migrating the selected VCPU to the VCPU queue of the first VCPU (i.e., the currently running VCPU), it is first determined whether the VCPU queue of the currently running VCPU is full. For example, it is determined whether the size of the linked list of the VCPU queue of the currently running VCPU is equal to the set size of the VCPU queue. If it is equal, the VCPU queue of the currently running VCPU is full; otherwise, the VCPU queue of the currently running VCPU is not full.
[0065] If the VCPU queue containing the currently running VCPU is full, one or more VCPUs waiting to run need to be removed from the queue in order to migrate the selected VCPU. After removal, the selected VCPU is migrated to the queue. This removal can be done by randomly removing one or more VCPUs from the queue other than the currently running VCPU; or by removing the lowest-priority VCPU from the queue other than the currently running VCPU; or by sequentially removing multiple lowest-priority VCPUs from the queue, removing the lowest-priority waiting VCPU from the queue each time. Specifically, removing a VCPU from the queue can be done by deleting the VCPU or migrating it to another queue that is not full. For the VCPU queue containing the currently running VCPU, removing the waiting VCPUs is equivalent to deleting or migrating the VCPU to another queue that is not full. If the VCPU queue containing the currently running VCPU is not full, the selected VCPU is directly migrated to that queue.
[0066] Step (a1) can also be replaced by determining whether the number of VCPUs in the VCPU queue has reached a preset upper limit. In this way, even if the VCPU queue is not full, if the number of VCPUs has reached the preset upper limit, one or more VCPUs other than the currently running VCPU are removed from the VCPU queue, and then the selected VCPUs are moved to the VCPU queue corresponding to the currently running VCPU. This method can reduce the number of busy VCPU queues, which refer to VCPU queues where the number of VCPUs has reached a certain upper limit. Setting the preset upper limit equal to the upper limit for determining busy VCPU queues can prevent idle VCPU queues from becoming busy VCPU queues.
[0067] Step S4254: If the preferred VCPU to be run does not exist, select one of the VCPUs to be run as the second VCPU;
[0068] Step S4255: If there is no VCPU to be run in the VCPU queue where the first VCPU is located, then select a VCPU to be run from other VCPU queues and migrate the selected VCPU to be run to the VCPU queue where the first VCPU is located. The selected VCPU to be run is the target VCPU.
[0069] If there are no preferred VCPUs to be run in other VCPU queues outside the queue of the first VCPU (i.e., the currently running VCPU), then one of the VCPUs to be run in the queue of the currently running VCPU is directly selected as the second VCPU, which is the target VCPU. For example, one of the VCPUs to be run can be randomly selected as the target VCPU, or the VCPU with the highest priority among the VCPUs to be run can be selected as the target VCPU, or the VCPU to be run that first entered the VCPU queue can be selected as the target VCPU. Step 3455 is the same as step 3443.
[0070] The VCPU switching method provided in this embodiment of the invention places the currently running VCPU and the target VCPU in the same VCPU queue, making it easy to determine the target VCPU. Threads bound to this VCPU queue only need to replace the currently running VCPU with the target VCPU to complete the VCPU switching, without involving thread switching. Therefore, it can reduce the time overhead and latency of VCPU switching. In actual tests on the ARM64 platform, the VCPU switching method provided in this embodiment of the invention can reduce the VCPU switching latency from 4.5us to 2.5us, which can well meet the high real-time VCPU switching requirements.
[0071] The creation of different VCPU queues is not necessarily simultaneous, and the space size of each VCPU queue is not necessarily the same. Moreover, the number of VCPUs in the VCPU queues created according to the needs of different applications is not necessarily the same. In addition, if there is a migration or removal of VCPUs, the number of VCPUs stored in each VCPU queue may also change. All of these situations may lead to an imbalance between VCPU queues, that is, some VCPU queues have too many VCPUs while others have too few VCPUs, causing the threads corresponding to some VCPU queues to be busy while the threads corresponding to other VCPU queues are relatively idle, resulting in an imbalance in the application of system resources. Figure 7 The diagram illustrates another VCPU switching method provided by an embodiment of the present invention. This method further balances the VCPU queue after VCPU switching, such as... Figure 7 As shown, the method includes:
[0072] Step S701: Save the state of the first VCPU, where the first VCPU is the currently running VCPU;
[0073] Step S702: Determine the second VCPU from the VCPU queue where the first VCPU is located. The second VCPU is the VCPU to be run.
[0074] Step S703: Restore the state of the second VCPU.
[0075] Steps S701 to S703 are the same as steps S401 to S403.
[0076] Step S704: Determine whether there is an idle VCPU queue with a VCPU count less than a preset lower limit, and whether there is a busy VCPU queue with a VCPU count greater than a preset upper limit.
[0077] Step S705: If there is an idle VCPU queue with a number of VCPUs less than a preset lower limit and a busy VCPU queue with a number of VCPUs greater than a preset upper limit, select n VCPUs to be run from the busy VCPU queue.
[0078] Step S706: Move the selected n VCPUs to be run to the idle VCPU queue;
[0079] Where n equals the number of VCPUs in the busy VCPU queue minus the preset upper limit.
[0080] In step S704, each VCPU queue is scanned to check the number of VCPUs in each queue, determining whether there are idle VCPU queues with fewer than a preset lower limit and busy VCPU queues with more than a preset upper limit. The scan can be periodic or performed at set time points. The preset lower and upper limits can be set by the user based on the system's processing capacity. To ensure that an idle VCPU queue has sufficient space to receive VCPUs migrating from a busy VCPU queue, the maximum value of the difference between the length of each VCPU queue (i.e., the number of VCPUs in the VCPU queue) and the preset upper limit must be less than the minimum value of the difference between the length of each VCPU queue and the preset lower limit.
[0081] A VCPU queue with fewer than a preset lower limit is called an idle VCPU queue, and a VCPU queue with more than a preset upper limit is called a busy VCPU queue. If only an idle VCPU queue exists and there is no busy VCPU queue, no VCPU queue balancing is needed to reduce the number of balancing operations and prevent excessive operations from consuming too many system resources. If only a busy VCPU queue exists and there is no idle VCPU queue, VCPU queue balancing cannot be performed. VCPU queue balancing is only performed when both busy and idle VCPU queues exist, that is, some of the waiting VCPUs in the busy VCPU queue are moved to the idle VCPU queue.
[0082] If the number of idle VCPU queues is greater than or equal to the number of busy VCPU queues, then for each busy VCPU queue, an idle VCPU queue is randomly selected, and some of the VCPUs waiting to run in that queue are migrated to the selected idle VCPU queue. If the number of idle VCPU queues is less than the number of busy VCPU queues, then m busy VCPU queues are randomly selected, and for each of the m busy VCPU queues, an idle VCPU queue is randomly selected, and some of the VCPUs waiting to run in that busy VCPU queue are migrated to the selected idle VCPU queue, where m is the number of idle VCPU queues. This process involves migrating a portion of the VCPUs waiting to run from the busy VCPU queue to selected idle VCPUs. These spare VCPUs can be k VCPUs, where k is any number less than or equal to "VCPU queue length - 1". However, migrating too many VCPUs may create a new imbalance. Therefore, n spare VCPUs can be selected from the busy VCPU queue, where n equals the number of VCPUs in the busy VCPU queue minus the preset upper limit. This ensures that the number of VCPUs in the busy VCPU queue after migration equals the preset upper limit, thus avoiding an excessively large migration. Specifically, selecting n spare VCPUs from the busy VCPU queue can be done by randomly selecting n spare VCPUs, or by selecting the first n spare VCPUs in descending order of priority from the busy VCPU queue.
[0083] It should be noted that steps S704 to S706 are not limited to being executed after VCPU switching; they can also be executed before VCPU switching. Other balancing methods can also be used for balancing between VCPU queues, such as average balancing. That is, after each scan of the VCPU queues, if the number of VCPUs in each queue is unequal, the total number of VCPUs is calculated by dividing the average number of VCPU queues by the total number of VCPUs. VCPU queues with a number of VCPUs greater than the average are designated as outgoing VCPU queues, and VCPU queues with a number of VCPUs less than the average are designated as incoming VCPU queues. Then, some VCPUs are removed from the outgoing VCPU queues to make the number of VCPUs in the outgoing VCPU queues equal to the average. The removed VCPUs are then migrated to the incoming VCPU queues. The number of VCPUs migrated into each incoming VCPU queue is less than or equal to the average minus the number of VCPUs that migrated into the VCPU queues before the migration.
[0084] Figure 8 This diagram illustrates the structure of a VCPU queue management module provided in an embodiment of the present invention. Figure 8 As shown, the VCPU queue management module includes:
[0085] The state storage unit U801 is used to store the state of the first VCPU, which is the currently running VCPU.
[0086] The target VCPU determination unit U802 is used to determine the second VCPU from the VCPU queue where the first VCPU is located, and the second VCPU is the VCPU to be run;
[0087] The state recovery unit U803 is used to restore the state of the second VCPU.
[0088] The state saving unit U801 first saves the state of the first VCPU (i.e., the currently running VCPU), that is, the contents of the VCPU's registers and program counter, to physical memory. The state restoration unit U803 then restores the state of the target VCPU to be run from physical memory after the target VCPU determination unit determines the second VCPU (i.e., the target VCPU). The target VCPU then begins to use the resources of the physical CPU to run the program corresponding to the target VCPU.
[0089] Figure 9 This illustrates one composition of the target VCPU determination unit U802, such as... Figure 8 As shown, the target VCPU determination unit U802 includes:
[0090] The VCPU waiting to run determination subunit U901 is used to determine whether there is a VCPU waiting to run in the VCPU queue where the first VCPU is located.
[0091] The first target VCPU determination subunit U902 is used to select a VCPU to be run as the target VCPU if there is a VCPU to be run in the VCPU queue where the first VCPU is located.
[0092] The second target VCPU determination subunit U903 is used to select a VCPU to be run from other VCPU queues if there is no VCPU to be run in the VCPU queue where the first VCPU is located, and migrate the selected VCPU to be run to the VCPU queue where the first VCPU is located. The selected VCPU to be run is the second VCPU.
[0093] Among them, the VCPUs to be run are the unused VCPUs in the VCPU queue. The VCPU to be run determination subunit U801 first determines whether there are any VCPUs to be run in the VCPU queue where the first VCPU, i.e., the currently running VCPU, is located. If the VCPU queue only stores one VCPU, then this VCPU is the currently running VCPU, and there are no VCPUs to be run in the VCPU queue. If the VCPU queue stores more than one VCPU, then there are VCPUs to be run in addition to the currently running VCPU. If there are VCPUs to be run in the VCPU queue, the first target VCPU determination subunit U902 randomly selects one of the VCPUs to be run as the second VCPU (i.e., the target VCPU); or selects the VCPU with the highest priority among the VCPUs to be run as the second VCPU to better utilize system resources; or selects the VCPU that first entered the VCPU queue as the second VCPU to prevent the VCPU from being idle for too long.
[0094] If there is no VCPU to be run in the VCPU queue where the first VCPU is located, the second target VCPU determination subunit U903 selects a VCPU to be run from other VCPU queues, then migrates the selected VCPU to be run to the VCPU queue where the first VCPU is located, and uses the selected VCPU to be run as the second VCPU. The migration can be adding the selected VCPU to the linked list of the VCPU queue where the first VCPU is located. The other VCPU queues can be any VCPU queue other than the VCPU queue where the first VCPU is located; the selection of a VCPU to be run can be randomly selected from the other VCPU queues; or the highest priority VCPU among the VCPUs to be run in the other VCPU queues; or the VCPU that entered the VCPU queue first among the other VCPU queues.
[0095] Figure 10 Another component of the target VCPU acquisition unit U1002 is shown, such as... Figure 10 As shown, the target VCPU acquisition unit U802 includes:
[0096] The VCPU waiting to run determination subunit U1001 is used to determine whether there is a VCPU waiting to run in the VCPU queue where the first VCPU is located.
[0097] The preferred VCPU to be run judgment subunit U1002 is used to determine whether there is a preferred VCPU to be run in other VCPU queues outside the VCPU queue where the first VCPU is located if there is a VCPU to be run in the VCPU queue where the first VCPU is located.
[0098] The preferred VCPU selection subunit U1003 is used to select one of the preferred VCPUs as the selected VCPU if the preferred VCPU exists.
[0099] Among them, the preferred VCPU selection subunit U1003 can randomly select one of the preferred VCPUs as the selected VCPU, or select the preferred VCPU with the highest priority as the selected VCPU.
[0100] The third target VCPU determination subunit U1004 is used to migrate the selected VCPU to the VCPU queue where the first VCPU is located, and the selected VCPU is the second VCPU.
[0101] The first target VCPU determination subunit U1005 is used to select one of the VCPUs to be run as the second VCPU if there is a VCPU to be run in the VCPU queue where the first VCPU is located, and there is no preferred VCPU to be run.
[0102] The second target VCPU determination subunit U1006 is used to select a VCPU to be run from other VCPU queues if there is no VCPU to be run in the VCPU queue where the first VCPU is located, and migrate the selected VCPU to be run to the VCPU queue where the first VCPU is located. The selected VCPU to be run is the second VCPU.
[0103] The first target VCPU determination subunit may randomly select one of the VCPUs to be run as the second VCPU, i.e., the target VCPU; or select the VCPU with the highest priority among the VCPUs to be run as the second VCPU; or select the VCPU to be run that first enters the VCPU queue as the second VCPU.
[0104] A VCPU with a higher priority than any other VCPU in the VCPU queue of the second VCPU (i.e., the currently running VCPU) is called a preferred VCPU. If the VCPU queue of the currently running VCPU contains VCPUs, but other VCPU queues contain VCPUs with a higher priority than any of the VCPUs in the currently running VCPU queue, then the higher-priority VCPU is selected. If the preferred VCPU determination subunit U1002 determines that a preferred VCPU exists in another VCPU queue, then the preferred VCPU selection subunit U1003 selects one of the preferred VCPUs as the selected VCPU. Specifically, one can be randomly selected from the preferred VCPUs, or the highest-priority preferred VCPU can be selected. The selected preferred VCPU is then moved to the VCPU queue of the currently running VCPU by the third target VCPU determination subunit U1004, and the selected VCPU is designated as the second VCPU (i.e., the target VCPU). In this way, the system resources can be better utilized by switching VCPUs to prioritize the use of higher-priority VCPUs.
[0105] The third target VCPU determination subunit may include:
[0106] The judgment subunit is used to determine whether the VCPU queue where the first VCPU is located is full;
[0107] The removal subunit is used to remove one or more VCPUs other than the first VCPU from the VCPU queue if the VCPU queue where the first VCPU is located is full.
[0108] The migration subunit is used to migrate the selected VCPU to the VCPU queue where the first VCPU is located if the VCPU queue is not full. The selected VCPU is the second VCPU.
[0109] The removal subunit may randomly remove one or more VCPUs from the VCPU queue containing the first VCPU (i.e., the currently running VCPU); or remove the lowest-priority VCPU from the VCPU queue; or sequentially remove multiple lowest-priority VCPUs from the VCPU queue. The removal by the removal subunit may be either deleting the VCPU or migrating the VCPU to another VCPU queue outside the currently running VCPU queue, where the other VCPU queue is a non-full VCPU queue.
[0110] To prevent VCPU queue overflow, before migrating the selected VCPU to the VCPU queue containing the first VCPU (i.e., the currently running VCPU), the determination subunit first checks whether the VCPU queue containing the first VCPU is full. If the VCPU queue containing the first VCPU is full, in order to migrate the selected VCPU in, the removal subunit removes one or more VCPUs from the queue. After removal, the migration subunit then migrates the selected VCPU to the queue. The removal can be: randomly removing one or more VCPUs from the queue other than the currently running VCPU; or removing the lowest-priority VCPU from the queue other than the currently running VCPU; or sequentially removing multiple lowest-priority VCPUs from the queue, each time removing the lowest-priority VCPU from the queue. Specifically, removing a VCPU from the queue can be done by deleting the VCPU or migrating the VCPU to be removed to another non-full VCPU queue. If the VCPU queue where the first VCPU is located is not full, the migration subunit will directly migrate the selected VCPU to the VCPU queue where the first VCPU is located.
[0111] If there is no preferred VCPU to be run in other VCPU queues outside the VCPU queue where the first VCPU is located, the first target VCPU determination subunit U1005 directly selects one of the VCPUs to be run in the VCPU queue where the first VCPU is located as the second VCPU (i.e., the target VCPU). For example, it randomly selects one of the VCPUs to be run as the second VCPU, or selects the VCPU with the highest priority among the VCPUs to be run as the second VCPU, or selects the VCPU to be run that first enters the VCPU queue as the second VCPU.
[0112] Figure 11 This diagram illustrates the structure of another VCPU queue management module provided in an embodiment of the present invention, as shown below. Figure 11 As shown, the VCPU queue management module includes:
[0113] The state saving unit U1101 is used to save the state of the first VCPU, which is the currently running VCPU.
[0114] The target VCPU determination unit U1102 is used to determine the second VCPU from the VCPU queue where the first VCPU is located, and the second VCPU is the VCPU to be run;
[0115] State recovery unit U1103 is used to restore the state of the second VCPU;
[0116] The judgment unit U1104 is used to determine whether there is an idle VCPU queue with a VCPU count less than a preset lower limit, and whether there is a busy VCPU queue with a VCPU count greater than a preset upper limit.
[0117] VCPU selection unit U1105 is used to select n VCPUs to be run from the busy VCPU queue if there is an idle VCPU queue with a number of VCPUs less than a preset lower limit and a busy VCPU queue with a number of VCPUs greater than a preset upper limit.
[0118] The balancing unit U1106 is used to migrate the selected n VCPUs to be run to the idle VCPU queue;
[0119] Where n equals the number of VCPUs in the busy VCPU queue minus the preset upper limit.
[0120] The VCPU selection unit can randomly select n VCPUs to be run from the busy VCPU queue; or, according to the priority of the VCPUs to be run in the busy VCPU queue from largest to smallest, select the first n VCPUs to be run.
[0121] The judgment unit U1104 scans each VCPU queue to determine the number of VCPUs in each queue, identifying any idle VCPU queues with fewer than a preset lower limit and any busy VCPU queues with more than a preset upper limit. This scanning can be periodic or performed at set time points. The preset lower and upper limits can be set by the user based on the system's processing capacity. The maximum value of the difference between the length of each VCPU queue and the preset upper limit must be less than the minimum value of the difference between the length of each VCPU queue and the preset lower limit to ensure that an idle VCPU queue has sufficient space to receive VCPUs migrated from a busy VCPU queue.
[0122] A VCPU queue with fewer than a preset lower limit is called an idle VCPU queue, and a VCPU queue with more than a preset upper limit is called a busy VCPU queue. If only an idle VCPU queue exists and there is no busy VCPU queue, to reduce the number of balancing operations and prevent excessive operations from consuming too many system resources, it is not necessary to use the VCPU selection unit U1105 and the balancing unit U1106 to balance the VCPU queues. If only a busy VCPU queue exists and there is no idle VCPU queue, VCPU queue balancing cannot be performed. VCPU queue balancing is only performed when both busy and idle VCPU queues exist, that is, some of the VCPUs waiting to run in the busy VCPU queue are migrated to the idle VCPU queue.
[0123] If the number of idle VCPU queues is greater than or equal to the number of busy VCPU queues, then the balancing unit U1106 randomly selects an idle VCPU queue for each busy VCPU queue and migrates some of the VCPUs waiting to run from that queue to the selected idle VCPU queue. If the number of idle VCPU queues is less than the number of busy VCPU queues, then the balancing unit U1106 randomly selects m busy VCPU queues, and for each of the m busy VCPU queues, randomly selects an idle VCPU queue and migrates some of the VCPUs waiting to run from that busy VCPU queue to the selected idle VCPU queue, where m is the number of idle VCPU queues. The portion of VCPUs waiting to run from the busy VCPU queues can be k VCPUs, where k is any number less than or equal to "VCPU queue length - 1". However, if the number of migrations is too large, it may cause new imbalances. Therefore, the VCPU selection unit U1105 can select n VCPUs to be run from the busy VCPU queue, where n is equal to the number of VCPUs in the busy VCPU queue minus the preset upper limit. In this way, the number of VCPUs in the busy VCPU queue after migration is equal to the preset upper limit, thereby avoiding an excessive number of migrations.
[0124] Specifically, the VCPU selection unit U1105 selects n VCPUs to be run from the busy VCPU queue. This can be done by randomly selecting n VCPUs to be run from the busy VCPU queue, or by selecting the first n VCPUs to be run according to their priority from highest to lowest in the busy VCPU queue.
[0125] The judgment unit U1104, VCPU selection unit U1105, and balancing unit U1106 can be connected after the state saving unit U1101, target VCPU determination unit U1102, and state recovery unit U1103, or before them. Balancing between VCPU queues can also use other balancing methods, such as average balancing. That is, after each scan, if the number of VCPUs in the VCPU queues is not equal, the judgment unit U1104 calculates the total number of VCPUs divided by the average number of VCPU queues. VCPU queues with a number of VCPUs greater than the average are designated as outgoing VCPU queues, and VCPU queues with a number of VCPUs less than the average are designated as incoming VCPU queues. Then, the VCPU selection unit U1105 selects a portion of VCPUs from the outgoing VCPU queues, removes the selected VCPUs, and the number of VCPUs in the VCPU queues equals the average. Then, the balancing unit U1106 migrates a portion of the VCPUs selected by the VCPU selection unit U1105 to the migration-in VCPU queue. The number of VCPUs migrated into each migration-in VCPU queue is less than or equal to the average number minus the number of VCPUs that were migrated into the VCPU queue before the migration.
[0126] Those skilled in the art will clearly understand that the techniques in the embodiments of the present invention can be implemented using software plus necessary general-purpose hardware platforms. Based on this understanding, the technical solutions in the embodiments of the present invention, or the parts that contribute to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in various embodiments or some parts of the embodiments.
[0127] The methods, apparatus, and computer program products of this invention are described using flowcharts and / or block diagrams. It should be understood that each block of the flowcharts and / or block diagrams, and combinations of blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowcharts and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 Devices that specify the functions in one or more boxes.
[0128] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including an instruction device, which is implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0129] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0130] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the device embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
Claims
1. A virtual central processor (VCPU) switching method, applied to a physical host running at least one VCPU, characterized in that, The physical host comprises at least one VCPU queue, each VCPU queue comprises at least one VCPU, and the at least one VCPU queue is managed by at least one host operating system (Host OS) thread; the method comprises: saving the state of a first VCPU, the first VCPU being a currently running VCPU; if there is a VCPU to be run in the VCPU queue where the first VCPU is located, selecting one of the VCPU to be run as a second VCPU; if there is no VCPU to be run in the VCPU queue where the first VCPU is located, selecting a VCPU to be run from other VCPU queues, adding the selected VCPU to be run to a linked list of the VCPU queue where the first VCPU is located, the selected VCPU to be run being the second VCPU; restoring the state of the second VCPU.
2. The VCPU switching method of claim 1, wherein, The method further comprises: if there is a preferred VCPU to be run with a priority higher than all VCPUs to be run in the VCPU queue where the first VCPU is located, selecting one of the preferred VCPU to be run as a selected VCPU, and migrating the selected VCPU to the VCPU queue where the first VCPU is located, the selected VCPU being the second VCPU; if there is no preferred VCPU to be run, selecting one of the VCPUs to be run in the VCPU queue where the first VCPU is located as the second VCPU.
3. The VCPU switching method of claim 1, wherein, If there is a VCPU to be run in the VCPU queue where the first VCPU is located, the selecting one of the VCPU to be run as the second VCPU comprises: randomly selecting one of the VCPU to be run as the second VCPU; or selecting a VCPU with the highest priority among the VCPU to be run as the second VCPU; or selecting a VCPU to be run that enters the VCPU queue first as the second VCPU.
4. The VCPU switching method of claim 2, wherein, If there is the preferred VCPU to be run, before migrating the selected VCPU to the VCPU queue where the first VCPU is located, the method further comprises: if the VCPU queue where the first VCPU is located is full, removing one or more VCPUs in the VCPU queue except the first VCPU; if the VCPU queue where the first VCPU is located is not full, migrating the selected VCPU to the VCPU queue corresponding to the first VCPU.
5. The VCPU switching method of claim 4, wherein, The removing one or more VCPUs in the VCPU queue except the first VCPU comprises: randomly removing one or more VCPUs in the VCPU queue except the first VCPU; or removing a VCPU with the lowest priority in the VCPU queue except the first VCPU; or removing multiple VCPUs with the lowest priority in the VCPU queue except the first VCPU; The removing is deleting the VCPU or migrating the VCPU to another VCPU queue other than the VCPU queue where the first VCPU is located, and the another VCPU queue is an unfull VCPU queue.
6. The VCPU switching method of claim 1, wherein, Further comprising: if there is an idle VCPU queue with a VCPU number less than a preset lower limit, and there is a busy VCPU queue with a VCPU number greater than a preset upper limit, selecting n VCPUs to be run in the busy VCPU queue; migrating the selected n VCPUs to be run to the idle VCPU queue; wherein n is equal to the number of VCPUs in the busy VCPU queue minus the preset upper limit.
7. The VCPU switching method of claim 6, wherein, The selecting n VCPUs to be run in the busy VCPU queue comprises: randomly selecting n VCPUs to be run in the busy VCPU queue; or selecting the first n VCPUs to be run in the busy VCPU queue in descending order of priority of the VCPUs to be run in the busy VCPU queue.
8. A physical host comprising: a hardware layer, a virtual machine monitor (VMM) running on the hardware layer, and a virtual machine running on the VMM, the hardware layer comprising at least one processor and a storage device, the VMM comprising at least one VCPU queue and a VCPU queue management module, the VCPU queue management module being a host operating system (Host OS) thread of the physical host, wherein the VCPU queue comprises at least one VCPU, and the VCPU is used to run a program using resources of the processor; the VCPU queue management module is used to manage the VCPU queue corresponding to the VCPU queue management module, and the VCPU queue management module is specifically used to: save a state of a first VCPU, the first VCPU being a currently running VCPU; if there is a VCPU to be run in the VCPU queue where the first VCPU is located, select one of the VCPUs to be run as a second VCPU; if there is no VCPU to be run in the VCPU queue where the first VCPU is located, select a VCPU to be run from another VCPU queue, add the selected VCPU to be run to a linked list of the VCPU queue where the first VCPU is located, and the selected VCPU to be run is the second VCPU; restore the state of the second VCPU.
9. The physical host of claim 8, wherein, The VCPU queue management module is further used to: if there is a preferred VCPU to be run with a priority higher than all VCPUs to be run in the VCPU queue where the first VCPU is located, select one of the preferred VCPUs to be run as a selected VCPU, and migrate the selected VCPU to the VCPU queue where the first VCPU is located, and the selected VCPU is the second VCPU; if there is no preferred VCPU to be run, select one of the VCPUs to be run in the VCPU queue where the first VCPU is located as the second VCPU.
10. The physical host of claim 8, wherein, If there is a VCPU to be run in the VCPU queue where the first VCPU is located, the VCPU queue management module is specifically configured to: randomly select one of the VCPUs to be run as the second VCPU; or select the VCPU with the highest priority among the VCPUs to be run as the second VCPU; or select the VCPU to be run that enters the VCPU queue first as the second VCPU when selecting the second VCPU.
11. The physical host of claim 9, wherein, If there is the preferred VCPU to be run, the VCPU queue management module is further configured to: If the VCPU queue where the first VCPU is located is full, remove one or more VCPUs in the VCPU queue except the first VCPU; If the VCPU queue where the first VCPU is located is not full, migrate the selected VCPU to the VCPU queue where the first VCPU is located.
12. The physical host of claim 11, wherein, The VCPU queue management module is specifically configured to: randomly remove one or more VCPUs in the VCPU queue except the first VCPU; or, remove the VCPU with the lowest priority in the VCPU queue except the first VCPU; or, remove multiple VCPUs with the lowest priority in the VCPU queue except the first VCPU; wherein the removal is deleting the VCPUs or migrating the VCPUs to other VCPU queues except the VCPU queue where the first VCPU is located, and the other VCPU queues are VCPU queues that are not full.
13. The physical host of claim 8, wherein, The VCPU queue management module is specifically further configured to: if there is an idle VCPU queue with a number of VCPUs less than a preset lower limit and there is a busy VCPU queue with a number of VCPUs greater than a preset upper limit, select n VCPUs to be run in the busy VCPU queue; migrate the selected n VCPUs to be run to the idle VCPU queue; wherein n is equal to the number of VCPUs in the busy VCPU queue minus the preset upper limit.
14. The physical host of claim 13, wherein, The VCPU queue management module is specifically configured to: randomly select n VCPUs to be run in the busy VCPU queue; or, select the first n VCPUs to be run in the busy VCPU queue in descending order of priority of the VCPUs to be run in the busy VCPU queue.
Citation Information
Patent Citations
Multiple-path multiple-core server and CPU virtualization processing method thereof
CN100511151C
Apparatus and method for scheduling virtual machine
CN101408853A
Computer system
CN102822809A
Synchronous service processing method and multi-core equipment
CN103226496A
Dynamic interrupt balanced mapping method based on current virtual central processing unit (VCPU) scheduling state
CN103744716A