A construction method, electronic device and storage medium for a real-time virtualization platform interruptible mutex lock
By introducing an interruptible mutex mechanism in the real-time virtualization platform, the problem of waiting delay of BQL lock is solved, and the stable interrupt processing of virtual machines is realized under high load conditions is improved, the system real-time and performance are improved, and the hard real-time application needs are met. .
Patent Information
- Application Number
- CN202410595970.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-14
- Publication Date
- 2024-11-08
- Estimated Expiration
- 2044-05-14
AI Technical Summary
Existing real-time virtualization technology ignores the problem of BQL lock waiting delay, resulting in unstable interrupt latency of virtual machines under high load conditions, affecting system real-time.
A method for constructing interruptable mutex locks for real-time virtualization platform is proposed. By defining data structures and lock arbitration mechanism, HItex's lock variables are used for lock arbitration and wait management, realizing an asynchronous interruptible strategy, allowing virtual machines to When waiting for lock, a virtual interrupt request is injected, and the waiting lock and current I/O instruction processing are abandoned to ensure that the interrupt processing is not affected by waiting for mutex locks.
It effectively reduces the worst-case interrupt delay and tail delay, keeps interrupt delay at a low level, improves system real-time performance, can meet the needs of hard real-time applications, and especially shows excellent performance under long-term high load conditions.
Smart Images

Figure CN118349368B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of real-time virtualization, and in particular relates to a construction method, electronic equipment and storage medium of an interruptible mutual exclusion lock for a real-time virtualization platform. Background Art
[0002] Real-time virtualization technology refers to running a real-time operating system (RTOS) with high timing constraints in a virtual machine (VM) to integrate multiple independent subsystems into a single hardware platform in a highly isolated manner, which saves costs, realizes the coexistence of RTOS and general-purpose operating system (GPOS), and is easy to manage. Real-time virtualization technology has been used in scenarios such as railways, industrial control, and automotive control. At present, QEMU-KVM is the most popular virtualization management platform. Based on the hardware virtualization support provided by the processor, it can achieve Type-II full virtualization support under Linux. From an engineering perspective, it is reasonable to use QEMU-KVM as a real-time virtualization platform because it has a mature and stable software ecosystem and is easy to deploy. However, QEMU-KVM is designed for general computing and does not have real-time guarantees. The additional virtualization overhead introduced by it for virtual machines may cause significant delays and jitters in the operation of virtual machines. Therefore, QEMU-KVM needs to be modified to meet real-time requirements.
[0003] Existing real-time virtualization platforms based on QEMU-KVM generally take the following measures to improve real-time performance:
[0004] 1. Use a Linux kernel with a real-time patch and the CONFIG_PREEMPT_RT option enabled on the physical machine. This will make the physical machine operating system real-time, avoid interrupt or thread preemption shielding, and reduce kernel latency.
[0005] 2. Use the high-priority SCHED_FIFO real-time scheduling policy for the vCPU thread created by QEMU and disable the kernel's real-time task current limiting mechanism to prevent the virtual machine from being subject to the scheduling restrictions of the physical machine.
[0006] 3. Bind the vCPU thread to the physical CPU core, and use the isolcpus parameter to isolate the physical CPU core. Use the rcu_nocbs parameter to disable the RCU worker thread on the corresponding physical CPU core to avoid interference of physical machine system noise on the virtual machine.
[0007] 4. Map the virtual machine's memory as hugepage to reduce the overhead caused by the virtual machine's TLB miss and page fault.
[0008] 5. Use paravirtualization, modify the GuestOS, communicate with the Hypervisor through special instructions (the so-called Hypercall), and enable the physical machine operating system to obtain the real-time scheduling information inside the virtual machine to perform targeted real-time scheduling of the vCPU threads.
[0009] Existing technologies can keep the interrupt latency within the virtual machine at a low us level, which can already meet the needs of some real-time application scenarios. However, they mainly focus on the scheduling of vCPU threads and ignore other adverse interference factors in the virtualization overhead. Summary of the invention
[0010] The problem to be solved by the present invention is to solve the BQL lock waiting delay problem ignored by the existing real-time virtualization technology, and propose a construction method, electronic device and storage medium for a real-time virtualization platform that can interrupt the mutual exclusion lock.
[0011] To achieve the above object, the present invention is implemented through the following technical solutions:
[0012] A method for constructing a real-time virtualization platform interruptible mutex lock comprises the following steps:
[0013] S1. Define data structure;
[0014] S2. Based on the data structure defined in step S1, a lock arbitration mechanism is constructed, and the lock variable lvar of HItex shared by all threads is used to arbitrate multiple threads that initiate lock requests at the same time;
[0015] S3. Based on the thread that cannot immediately hold the lock obtained in step S2, a lock waiting mechanism is constructed, wherein the lock waiting mechanism is implemented in kernel state;
[0016] S4. For the vCPU threads in the waiting queue for the thread lock obtained in step S3, an interruptible waiting mechanism is constructed.
[0017] Furthermore, the data structure defined in step S1 is as follows:
[0018] lvar represents the lock variable;
[0019] f[v i ] indicates vCPUv i The corresponding thread;
[0020] WQ represents the waiting queue of threads for locks;
[0021] w[v i ] indicates vCPUv i Whether an interruptible lock wait is in progress;
[0022] b[vi ] indicates vCPUv i Whether the lock waiting process is interrupted by a virtual interrupt request.
[0023] Furthermore, the method for constructing the lock arbitration mechanism in step S2 includes the following steps:
[0024] S2.1. When the virtual machine is created, the kernel-mode KVM initialization f[v i ]、w[v i ]、b[v i ], WQ, user-mode QEMU initializes lvar;
[0025] S2.2. For any thread of the virtual machine, including the vCPU thread and its auxiliary threads, when a lock request is initiated, first check whether lvar is 0. If it is, the thread successfully holds the lock and lvar is set to 1. Otherwise, the thread cannot hold the lock immediately, so the thread enters the kernel state through a system call to wait. The thread's reading and writing of lvar are all atomic operations.
[0026] Furthermore, the specific implementation method of step S3 includes the following steps:
[0027] S3.1. For vCPUv i The corresponding thread, if it initiates a lock request, vCPUv i The corresponding thread is preparing to perform exception handling for I / O port access in QEMU in user mode, and the interrupt is not masked. It is marked as interruptible waiting before waiting in kernel mode, that is, let w[v i ]=1, otherwise let w[v i ]=0;
[0028] S3.2. Then for vCPUv i The corresponding thread makes b[v i ]=0;
[0029] S3.3. For any thread, when waiting in kernel state, add any thread to the waiting queue WQ for the thread lock, and then enter the blocked state until it is unblocked by another thread;
[0030] S3.4. When the lock-holding thread unlocks, it uses an atomic operation to set lvar to 0, unblocks the thread at the head of the WQ through a system call, and deletes it from the WQ;
[0031] S3.5. For the non-vCPU thread in the unblocked state obtained based on step S3.4, return to the user state and go to step S2.2 to re-initiate the lock request.
[0032] Furthermore, the specific implementation method of step S4 includes the following steps:
[0033] S4.1. For the vCPU thread that cannot hold the lock immediately based on step S2, first determine whether it is injected with a virtual interrupt request. If it is not, continue to try to wait. If it is yes, the lock holding fails completely and returns to the user state.
[0034] S4.2. When another thread requests vCPUv i When a virtual interrupt request is injected, if it is in the process of waiting for an interrupt w[v i ]=1, marking it as waiting for the lock to be interrupted, that is, let b[v i ]=1, and then query to get the corresponding thread f[v i ], f[v i ]Deleted from WQ;
[0035] S4.3. For vCPUv i The corresponding thread, after its blocking state is released, returns to the user state and then determines whether it is marked as waiting for the lock to be interrupted b[v i ] = 1, the lock holding fails completely, otherwise go to step S2.2 to re-initiate the lock holding request;
[0036] S4.4. For vCPUs that completely fail to hold a lock i After its thread returns to user mode, QEMU notifies KVM through shared memory that its lock holding has completely failed;
[0037] S4.5. For vCPUv i Before re-entering the virtual machine running state in kernel state, if its lock holding fails completely, KVM will cancel the impact of the I / O instruction it is executing on the instruction address of the vCPU and its registers.
[0038] An electronic device comprises a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the processor implements any one of the steps of a method for constructing a real-time virtualization platform interruptible mutex lock.
[0039] A computer-readable storage medium stores a computer program, which, when executed by a processor, implements a method for constructing a real-time virtualization platform interruptible mutex lock.
[0040] Beneficial effects of the present invention:
[0041] The invention discloses a method for constructing a real-time virtualization platform interruptible mutex lock, and proposes a virtualization platform interruptible mutex HItex (HypervisorInterruptibleMutex). Based on the improvement of the BQL lock in QEMU, the vCPU thread uses an asynchronous interruptible strategy to wait for the lock, and after a virtual interrupt request is injected while waiting for the lock, the waiting for the lock and the current I / O instruction processing are abandoned, and the virtual machine state is immediately returned to process the interrupt in time, thereby ensuring that the interrupt processing of the virtual machine will not be affected by the waiting for the mutex lock. On the basis of the existing real-time virtualization technology, the interrupt delay of the virtual machine can be kept in a stable range under long-term high-load operation conditions, thereby avoiding the occurrence of uncertain jitter and improving the real-time performance of the system.
[0042] The invention discloses a method for constructing a real-time virtualization platform interruptible mutex lock. HItex, as an optimization technology for enabling virtual machines to achieve precise interruption, can significantly reduce the interruption delay and tail delay in the worst case, and control the distribution of interruption delay at a low level. In addition, HItex can withstand long-term high-load tests, proving that it can meet the requirements of hard real-time applications. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 It is a flow chart of a method for constructing a real-time virtualization platform interruptible mutex lock according to the present invention;
[0044] Figure 2 A schematic diagram of an interruptible mutex lock for a real-time virtualization platform according to the present invention;
[0045] Figure 3 This is a pseudo code diagram of the lock arbitration mechanism;
[0046] Figure 4 This is the interrupt delay distribution curve when HItex is not enabled in the present invention;
[0047] Figure 5 Interrupt latency distribution curve for HItex enabled for the present invention. DETAILED DESCRIPTION
[0048] In order to make the purpose, technical solution and advantages of the present invention more clear, the present invention is further described in detail below in conjunction with the accompanying drawings and specific embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention, that is, the specific embodiments described are only part of the embodiments of the present invention, rather than all of the specific embodiments. The components of the specific embodiments of the present invention described and shown in the drawings herein can be arranged and designed in various different configurations, and the present invention can also have other embodiments.
[0049] Therefore, the following detailed description of the specific embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the invention claimed for protection, but merely represents the selected specific embodiments of the present invention. Based on the specific embodiments of the present invention, all other specific embodiments obtained by those skilled in the art without making creative work are within the scope of protection of the present invention.
[0050] In order to further understand the content, features and effects of the present invention, the following specific implementation methods are given as examples, and the attached Figure 1 -Attached Figure 5 The detailed instructions are as follows: Specific implementation method one:
[0052] A method for constructing a real-time virtualization platform interruptible mutex lock comprises the following steps:
[0053] S1. Define data structure;
[0054] Furthermore, the data structure defined in step S1 is as follows:
[0055] lvar represents the lock variable;
[0056] f[v i ] indicates vCPUv i The corresponding thread;
[0057] WQ represents the waiting queue of threads for locks;
[0058] w[v i ] indicates vCPUv i Whether an interruptible lock wait is in progress;
[0059] b[v i ] indicates vCPUv i Whether the lock waiting process is interrupted by a virtual interrupt request;
[0060] Furthermore, HItex defines the following four basic operations:
[0061] ui_lock() is executed by the uninterruptible lock thread to obtain the lock in an uninterruptible manner.
[0062] i_lock() is executed by an interruptible lock waiting thread to perform interruptible lock acquisition, and the return value is whether the lock is successfully held.
[0063] unlock() is executed by the lock-holding thread to release the lock and deliver it to the earliest waiting thread.
[0064] intr(vcpu) is executed by the interrupt thread when injecting a virtual interrupt request into the vcpu. If the thread corresponding to the vcpu is currently serving as an interruptible waiting thread, its waiting process is canceled.
[0065] S2. Based on the data structure defined in step S1, a lock arbitration mechanism is constructed, and the lock variable lvar of HItex shared by all threads is used to arbitrate multiple threads that initiate lock requests at the same time;
[0066] Furthermore, the method for constructing the lock arbitration mechanism in step S2 includes the following steps:
[0067] S2.1. When the virtual machine is created, the kernel-mode KVM initialization f[v i ]、w[v i ]、b[v i ], WQ, user-mode QEMU initializes lvar;
[0068] Furthermore, lvar can have three values: 0 means there is no lock-holding thread, 1 means there is a lock-holding thread but no lock-waiting thread, 2 means there is both a lock-holding thread and a lock-waiting thread;
[0069] S2.2. For any thread of the virtual machine, including the vCPU thread and its auxiliary threads, when a lock request is initiated, first check whether lvar is 0. If it is, the thread successfully holds the lock and lvar is set to 1. Otherwise, the thread cannot hold the lock immediately and enters the kernel state through a system call to wait. The thread's reading and writing of lvar are all atomic operations;
[0070] Figure 3 The following is a pseudo-code diagram of the lock arbitration mechanism. The code in the diagram is explained as follows:
[0071] atomic_cmpxchg(lvar, oldval, newval) is an atomic compare-exchange operation that returns the original value of lvar. If the original value of lvar is oldval, it is updated to newval.
[0072] atomic_xchg(lvar, newval) is an atomic exchange operation, which returns the original value of lvar and unconditionally updates it to newval;
[0073] wait_in_kernel() is to block and wait in kernel mode until being awakened;
[0074] wake_up_first() wakes up the earliest lock waiter;
[0075] Furthermore, if multiple threads acquire HItex through i_lock() or ui_lock() at the same time, and there is no lock holder (lvar=0), the first thread that performs the lvar atomic operation to 1 or 2 holds the lock. The atomicity of the operation ensures that these threads will access lvar in a certain order, so such threads must be unique.
[0076] S3. Based on the thread that cannot immediately hold the lock obtained in step S2, a lock waiting mechanism is constructed, wherein the lock waiting mechanism is implemented in kernel state;
[0077] Furthermore, the specific implementation method of step S3 includes the following steps:
[0078] S3.1. For vCPUv i The corresponding thread, if it initiates a lock request, vCPUv i The corresponding thread is preparing to perform exception handling for I / O port access in QEMU in user mode, and the interrupt is not masked. It is marked as interruptible waiting before waiting in kernel mode, that is, let w[v i ]=1, otherwise let w[v i ]=0;
[0079] S3.2. Then for vCPUv i The corresponding thread makes b[v i ]=0;
[0080] S3.3. For any thread, when waiting in kernel state, add any thread to the waiting queue WQ for the thread lock, and then enter the blocked state until it is unblocked by another thread;
[0081] S3.4. When the lock-holding thread unlocks, it uses an atomic operation to set lvar to 0, unblocks the thread at the head of the WQ through a system call, and deletes it from the WQ;
[0082] S3.5. For the non-vCPU thread in the unblocked state obtained based on step S3.4, return to the user state and go to step S2.2 to re-initiate the lock request;
[0083] Furthermore, in the lock waiting mechanism, WQ arranges the blocked lock waiters in chronological order and provides three basic operations:
[0084] wait_in_kernel(), adds the current lock-waiting thread to the end of WQ and blocks it;
[0085] wake_up_first(), wake up the first lock holder in WL and delete it from WQ, and execute it by the unlocked lock holder;
[0086] wake_up_t(t), wakes up the specific lock waiter t and deletes it from the WQ, executed by the interrupter;
[0087] S4. For the vCPU threads in the waiting queue for the thread lock obtained in step S3, an interruptible waiting mechanism is constructed.
[0088] Furthermore, the specific implementation method of step S4 includes the following steps:
[0089] S4.1. For the vCPU thread that cannot hold the lock immediately based on step S2, first determine whether it is injected with a virtual interrupt request. If it is not, continue to try to wait. If it is yes, the lock holding fails completely and returns to the user state.
[0090] S4.2. When another thread requests vCPUv i When a virtual interrupt request is injected, if it is in the process of waiting for an interrupt w[v i ]=1, marking it as waiting for the lock to be interrupted, that is, let b[v i ]=1, and then query to get the corresponding thread f[v i ], f[v i ]Deleted from WQ;
[0091] S4.3. For vCPUv i The corresponding thread, after its blocking state is released, returns to the user state and then determines whether it is marked as waiting for the lock to be interrupted b[v i ] = 1, the lock holding fails completely, otherwise go to step S2.2 to re-initiate the lock holding request;
[0092] S4.4. For vCPUs that completely fail to hold a lock i After its thread returns to user mode, QEMU notifies KVM through shared memory that its lock holding has completely failed, and then uses the system call to return vCPUv i Deliver KVM, make vCPUv i Re-enter the virtual machine running state;
[0093] S4.5. For vCPUv i Before re-entering the virtual machine running state in kernel state, if it fails to hold the lock completely, KVM will cancel the impact of the I / O instruction it is executing on the instruction address of the vCPU and its registers;
[0094] Furthermore, KVM executes the state switching instruction, and the vCPU that completely fails to hold the lock i The thread enters the virtual machine state. After the state switching instruction is executed, the vCPUv that completely fails to hold the lock is detected. iIf the thread has an injected virtual interrupt request, the interrupt handler inside the virtual machine is executed first before re-executing the failed I / O instruction;
[0095] Furthermore, after the interrupt returns, the I / O instruction will be re-executed.
[0096] The interruptible waiting mechanism of this embodiment is further described as follows:
[0097] (1) When a virtual machine is created, a one-to-one mapping table f is constructed for each vCPU and its corresponding thread t, and f(vCPU) = t.
[0098] (2) During the operation of the vCPU, an I / O port access instruction i inside the virtual machine is executed, triggering an exception and returning to KVM, and then returning to QEMU for processing.
[0099] (3) Before QEMU starts processing I / O port access, it tries to acquire HItex through i_lock(). If there is another lock holder at this time, it joins the WL blocking wait through wait_in_kernel().
[0100] (4) The vCPU is injected with a virtual interrupt. If the vCPU does not mask interrupts, its corresponding thread is awakened through wake_up_t(f(vCPU)), ending the lock waiting and failing to hold the lock.
[0101] (5) QEMU notifies KVM through shared memory that the I / O port access failed, and calls the KVM_RUN interface to put the vCPU back into the running state.
[0102] (6) KVM cancels the impact of instruction i on the architecture state of the vCPU (PC and other registers). At this time, the vCPU is equivalent to the state before executing i.
[0103] (7) KVM executes a state switching instruction (e.g., the eret instruction in arm, the vmrun instruction in x86), and the vCPU thread enters the virtual machine state. After the instruction is executed, if a virtual interrupt request is detected, the interrupt handler inside the virtual machine is executed first before re-executing instruction i.
[0104] (8) After the interrupt returns, instruction i will be re-executed, which will not affect the correctness of the execution.
[0105] Utilizing the method for constructing a real-time virtualization platform interruptible mutex lock described in this embodiment, HItex was tested on a Huawei TaiShan200-2280 server, and the hardware and software environment configuration used in the test is shown in Table 1. The test virtual machine was connected via a serial terminal, and stress-ng was run in the virtual machine as a disk load. Cyclictest was used to trigger a timer interrupt every 1ms on each vCPU and measure the delay.
[0106] Table 1 Software and hardware environment used for the test
[0107]
[0108]
[0109] After the test runs for 300 seconds, Table 2 shows the interrupt latency results of the cyclictest test with and without HItex enabled. Figure 4 and 5 The delay distribution in two cases is described. It can be seen that HItex can significantly improve the interrupt delay of the virtual machine under high I / O load. Compared with the ordinary QEMU-KVM virtualization platform without HItex, the average delay is shortened to 72.22% of the original, the maximum delay is shortened to 1.47% of the original, and the highest 1% tail delay is shortened to 11.47% of the original. The distribution of interrupt delay is more predictable, and the excessive jitter is completely eliminated. The comparative experiment proves that HItex has a good optimization effect on realizing accurate interruption of virtual machines.
[0110] Table 2 Interrupt delay test results after running for 300 seconds
[0111]
[0112] In addition, keeping the other conditions unchanged, the test was run for 24 hours with HItex enabled, and the results are shown in Table 3. It can be seen that even under extreme conditions of high I / O load and long operation, HItex can still keep the virtual machine interrupt delay below 100us, which is sufficient to meet the requirements of hard real-time application scenarios.
[0113] Table 3 Interrupt delay test results with HItex enabled and running for 24 hours
[0114] Average latency Maximum delay 1% tail latency 0.1% tail delay 0.01% tail delay 13us 96us 25us 34us 43us
[0115] The above comparative experiments show that HItex, as an optimization technology designed to enable virtual machines to achieve precise interruption, can significantly reduce the worst-case interruption delay and tail delay, and control the distribution of interruption delay at a low level. In addition, HItex can withstand long-term high-load tests, proving that it can meet the needs of hard real-time applications. Specific implementation method 2:
[0117] The computer device of the present invention may be a device including a processor and a memory, such as a single chip microcomputer including a central processing unit, etc. Moreover, the processor is used to implement the steps of the method for constructing a real-time virtualization platform interruptible mutex lock when executing the computer program stored in the memory.
[0118] The processor may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.
[0119] The memory may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the mobile phone (such as audio data, a phone book, etc.), etc. In addition, the memory may include a high-speed random access memory, and may also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (SmartMediaCard, SMC), a secure digital (SecureDigital, SD) card, a flash card (FlashCard), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. Specific implementation method three:
[0121] The computer-readable storage medium of the present invention can be any form of storage medium that can be read by a processor of a computer device, including but not limited to non-volatile memory, volatile memory, ferroelectric memory, etc. A computer program is stored on the computer-readable storage medium. When the processor of the computer device reads and executes the computer program stored in the memory, the steps of the above-mentioned method for constructing a real-time virtualization platform interruptible mutex lock can be implemented.
[0122] The computer program includes computer program code, which may be in source code form, object code form, executable file or some intermediate form, etc. The computer readable medium may include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium, etc. It should be noted that the content contained in the computer readable medium may be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer readable media do not include electric carrier signals and telecommunication signals.
[0123] The abbreviations and key terms of the present invention are defined as follows:
[0124] 1.RTOS (Real-Time Operating System): A real-time operating system that can run applications with timing constraints.
[0125] 2. GPOS (General Propose Operating System): General (non-real-time) operating system, refers to an ordinary operating system that does not have real-time guarantees, as distinguished from RTOS.
[0126] 3.VM (Virtual Machine): A virtual machine refers to a complete computer system with complete hardware system functions simulated by software and running in a completely isolated environment.
[0127] 4.Hypervisor: Virtualization management platform, refers to the management program that runs directly on the physical machine and hosts the virtual machines.
[0128] 5. KVM (Kernel-based Virtual Machine): A kernel-based virtual machine, a module of the Linux kernel, implements the basic functions of the Hypervisor in kernel state and provides underlying support for hardware virtualization.
[0129] 6. QEMU (QuickEmulator): A virtualization management platform software running in user mode, which implements all virtual hardware devices of the virtual machine and cooperates with KVM to form the whole virtual machine.
[0130] 7.vCPU: Virtual CPU refers to a logical CPU core in a virtual machine, which is actually implemented as a single thread control flow in a physical machine.
[0131] 8.Guest OS (Guest Operating System): Guest operating system refers to the operating system running in the virtual machine.
[0132] 9. BQL (the Big QEMULock): The only global mutex lock in QEMU used to synchronize machine states, which is contested by vCPU threads and I / O daemon threads.
[0133] It should be noted that relational terms such as "first" and "second" are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0134] Although the present application has been described above with reference to specific embodiments, various modifications may be made thereto and parts thereof may be replaced with equivalents without departing from the scope of the present application. In particular, as long as there is no structural conflict, the various features in the specific embodiments disclosed in the present application may be used in combination with each other in any manner, and the fact that these combinations are not exhaustively described in this specification is only for the sake of omitting space and saving resources. Therefore, the present application is not limited to the specific embodiments disclosed herein, but includes all technical solutions falling within the scope of the claims.
Claims
1. A method for constructing an interruptible mutex lock for a real-time virtualization platform, characterized in that: The steps include: S1. Define data structure; The data structure defined in step S1 is as follows: lvar Indicates a lock variable; f [ v i ] indicates vCPU v i The corresponding thread; WQ Represents the thread's waiting queue for the lock; w [ v i ] indicates vCPU v i Whether an interruptible lock wait is in progress; b [ v i ] indicates vCPU v i Whether the lock waiting process is interrupted by a virtual interrupt request; S2. Based on the data structure defined in step S1, a lock arbitration mechanism is constructed, using the lock variable of the platform interruptible mutex HItex shared by all threads lvar Arbitrate multiple threads that initiate lock requests at the same time; The method for constructing the lock arbitration mechanism in step S2 includes the following steps: S2.
1. KVM initialization in kernel mode when a virtual machine is created f [ v i ]、 w [ v i ]、 b [ v i ]、 WQ , QEMU initialization in user mode lvar ; S2.
2. For any thread of the virtual machine, including the vCPU thread and its auxiliary threads, when a lock request is initiated, the first check is performed lvar Is it 0? If it is, the thread successfully holds the lock. lvar Set to 1, otherwise the thread cannot hold the lock immediately, so the thread enters the kernel state through a system call to wait. lvar All reads and writes are atomic operations; S3 based on the thread that cannot immediately hold the lock obtained in step S2, build a lock waiting mechanism, the lock waiting mechanism is implemented in the kernel state; The specific implementation method of step S3 includes the following steps: S3.
1. For vCPU v i The corresponding thread, if it initiates a lock request, vCPU v i The corresponding thread is preparing to perform exception handling for I / O port access in QEMU in user mode, and the interrupt is not masked, so it is marked as interruptible waiting before waiting in kernel mode, that is, w [ v i ]=1, otherwise let w [ v i ]=0; S3.
2. Then for vCPU v i The corresponding thread waits in kernel state before b [ v i ]=0; S3.
3. For any thread, add any thread to the waiting queue for the lock when waiting in kernel mode WQ , and then enters a blocked state until it is unblocked by another thread; S3.
4. When the thread holding the lock unlocks the lock, use atomic operations to lvar Set to 0, the system call will be in WQ The thread at the head of the queue is unblocked and removed from WQ Delete from; S3.
5. For the non-vCPU thread in the unblocked state obtained in step S3.4, return to the user state and go to step S2.2 to re-initiate the lock request; S4. For the vCPU threads in the thread lock waiting queue obtained in step S3, an interruptible waiting mechanism is constructed.
2. The method for constructing a real-time virtualization platform interruptible mutex lock according to claim 1, characterized in that: The specific implementation method of step S4 includes the following steps: S4.
1. For the vCPU thread that cannot hold the lock immediately based on step S2, first determine whether it has been injected with a virtual interrupt request. If it is not, continue to try to wait. If it is yes, the lock holding fails completely and returns to the user state. S4.
2. When another thread requests vCPU v i When a virtual interrupt request is injected, if it is in an interruptible waiting lock w [ v i ]=1, marking it as waiting for the lock to be interrupted, that is, b [ v i ]=1, and then query to get the corresponding thread f [ v i ],Will f [ v i ]from WQ Delete from; S4.
3. For vCPU v i The corresponding thread, after its blocking state is released, returns to the user state, and then determines whether it is marked as waiting for the lock to be interrupted b [ v i ]=1, the lock holding fails completely, otherwise go to step S2.2 to re-initiate the lock holding request; S4.
4. For vCPUs that completely fail to hold a lock v i After its thread returns to user mode, QEMU notifies KVM through shared memory that its lock holding has completely failed; S4.
5. For vCPU v i Before re-entering the virtual machine running state in kernel state, if its lock holding fails completely, KVM will cancel the impact of the I / O instruction it is executing on the instruction address of the vCPU and its registers.
3. An electronic device, characterized in that: The invention comprises a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of a method for constructing a real-time virtualization platform interruptible mutex lock as described in any one of claims 1 to 2 when executing the computer program.
4. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method for constructing a real-time virtualization platform interruptible mutex lock as described in any one of claims 1-2 is implemented.
