Memory page exchange method and system for microkernel operating system
By running the swap page management system service in the microkernel operating system, the memory page switching process is optimized, and the memory page switching process is solved, and the memory page switching performance overhead is achieved in the microkernel environment, and the efficient memory page switching function is realized, supporting the application of large memory requirements. run.
Patent Information
- Application Number
- CN202310700443.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-13
- Publication Date
- 2024-12-13
AI Technical Summary
In the microkernel environment, the memory page switching mechanism has large performance overhead, large code volume, lack of universality and adaptability, making it difficult to support the operation of applications with large memory requirements.
In the microkernel operating system, a switching page management system service is run, providing communication between the user state and the kernel through unified data encapsulation and delivery, and generating switching page identifiers, reducing the number of memory copies and optimizing the memory page exchange process.
It realizes the memory page switching function under the microkernel architecture, reduces the amount of kernel code, improves the operation efficiency of the switching system, and supports software that requires large memory when hardware resources are limited.
Smart Images

Figure CN119127738A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer operating systems, and in particular to a memory page swapping method and system for a microkernel operating system. Background Art
[0002] The memory page swap system is part of the memory page recycling system. When the system faces a memory shortage, a series of strategies and mechanisms for recycling the allocated physical memory pages are needed to maintain the normal operation of the system and meet new memory allocation requirements. The goal of the memory page recycling mechanism is to obtain the allocated physical memory pages and make them free. The swap mechanism can swap out the contents of the allocated physical memory pages to external storage devices such as disks, and modify the page table to indicate that the page is swapped out, thereby releasing the physical memory pages in use. When being accessed or used by a process, it can be known that the page is on the external storage device through the mark, and then the page data is swapped from the storage device to the memory for normal use. The memory page swap mechanism can be used to expand the memory address space so that it can be used more effectively by user-mode processes. When the amount of physical memory is less than the total demand, the kernel is still allowed to run multiple applications with large memory requirements. As a supplement to physical memory, such a mechanism is still not as good as physical memory in actual performance. Each access of a process to a swapped-out page is several orders of magnitude slower than the access to a physical memory page. Therefore, in addition to the implementation of the swap function, performance needs should also be considered as much as possible.
[0003] In the open source monolithic Linux kernel, the swap mechanism is used to provide backup for non-mapped pages on disk. This swap needs to be transparent to the application, and there is no need to embed special swap-related instructions in the code. The swap mechanism uses the Present flag of each page table entry in the page table to determine whether a page is swapped out to the swap area based on the flag, and uses the remaining bits to store the swapped-out page identifier, which can be used to find the location of the swapped-out page on disk. When accessing the virtual address of the swapped-out page causes a page fault exception, the kernel's exception handler detects that the page is not in memory but in the swap area, and calls the corresponding function to swap the page in from disk.
[0004] The design of swap area in Linux is very complex. The implementation of swap area can use disk partition or files contained in large partition. At the same time, multiple swap areas can be enabled in a system, allowing system administrators to distribute swap space on several disks so that the hardware can operate these swap areas concurrently. This design also allows the size of swap space to be expanded without restarting the system while the system is running. In terms of swap area management, Linux divides the swap area into one or more swap sub-areas. Each sub-area corresponds to a group of physically adjacent page slots on the disk. The information and usage of the page slots in the area are managed and maintained through the descriptors of the sub-areas. This design can help the kernel better place the swapped pages in adjacent page slots when swapping out, thereby reducing the disk seek time when accessing the swap area. When using multiple swap areas, Linux designs a priority mechanism for the swap area. The swap area with faster device reading speed can obtain a higher priority. When obtaining free page slots, the system will obtain free pages from the swap area with the highest priority.
[0005] Linux provides new mechanisms and functions for swapping physical pages from memory to the swap area and from the swap area to memory. To swap a physical page out of memory, you first need to remove the reference to the page in the application page table. In early versions, since there is no way to quickly locate the page table entry pointing to the page, you need to loop through the process's page table to check whether the process has referenced the page. If it has, unmap it and fill in the swap identifier to facilitate the subsequent search for the corresponding physical page. During the traversal process, if it is found that all the mappings of a page have been unmapped, a physical page can be released. This design has very low release efficiency, so in earlier versions, Linux connects all the page table entries in the page frame through a linked list. However, this design also has the problem of too much overhead in link list maintenance. Therefore, starting from version 2.6, Linux introduced the "object-oriented reverse mapping" technology. For any reclaimable user-mode page, the kernel retains the reverse links of all linear area objects in the system where the page is located, and stores a pointer to the memory descriptor for each linear area descriptor, which in turn contains a pointer to the page global directory. The number of linear region descriptors is less than that of page descriptors, so the cost of maintaining reverse mapping information can be effectively reduced. This design allows the system to quickly retrieve all page table entries that reference the page when reclaiming the page. For the reverse mapping of anonymous pages, since the degree of sharing of anonymous pages is relatively low, the anonymous linear region where the page frame is located is stored in a bidirectional circular linked list. Multiple anonymous pages in a linear region will only use one reverse mapping linked list to act on all page frames in the region.
[0006] The swap page identifier of Linux can simply and uniquely identify a swap page. A swap page identifier mainly consists of three parts. The first bit is 0, which is used to let the system enter the page fault handling process in the page table entry. The second to eighth bits are the area code, which is used to identify which swap area the swap page belongs to. The remaining part is the page slot index, which is used to locate the page in the swap area. Linux provides multiple unified interfaces to support changing the content of the page table entry to the swap page identifier, obtaining the swap page identifier from the page table entry, and obtaining the swap area code and page slot index through the swap page identifier under different architectures.
[0007] In the macro kernel scenario, the main way to improve the performance of the memory page frame swap system is to optimize the strategy for selecting the physical memory page to be swapped out, and try to select pages with a lower probability of being accessed and used for swapping out, thereby reducing the number of swaps in and out. In the micro kernel scenario, there are more problems. If the swap system is placed entirely in the kernel, the micro kernel will be too bloated and not in line with the design concept of the micro kernel. Therefore, a more appropriate solution is to run the swap page management as a system service in user mode, communicate with the kernel through kernel notifications, system calls, etc., and cooperate to complete the swapping in and out of the swap page. Unlike the macro kernel, which can obtain physical page data and call IO functions in kernel mode, in the micro kernel environment, the system service needs to register the required communication methods with the kernel in advance, including the buffer for transmitting data, the virtual address segment prepared to reduce the number of copies, etc. When the system needs to swap in or out pages, the system service is awakened through the agreed notification method to obtain data from the buffer and process it.
[0008] The separation of the kernel and system services will make performance issues more significant. In a microkernel, disk reading and writing usually need to be performed by file systems and driver services running in user mode, and memory management is also a system service running in user mode in some microkernel systems, and in the kernel in others. The interaction between these systems needs to be implemented through inter-process communication and system interrupts, which involves frequent context switching and has a considerable performance overhead. Therefore, in addition to the page selection strategy, the design of the memory page swap mechanism needs to be optimized in a targeted manner to find a balance between function and performance to adapt to the microkernel environment.
[0009] Since the page swap mechanism involves memory management, file system, and disk reading and writing, it will have a high performance overhead in the microkernel system. In many current microkernel systems or lightweight kernel systems, such as seL4 and Huawei LiteOS, there is no design and support for the page swap mechanism. Among them, there is no swap mechanism in the LiteOS source code. Through the abstraction of memory pool and memory block provided by it, the problem of insufficient memory can be solved by modifying the system to use a part of the external storage as a memory pool during system initialization. However, such initialization requires users to modify the kernel code by themselves, and has great restrictions on the use environment, and is not universal. In the past, microkernels were mainly used to handle simple tasks, and rarely run applications with large memory requirements. However, with the development of the Internet of Things and artificial intelligence technologies, more and more embedded devices and terminals equipped with microkernel systems have strong hardware computing capabilities, and they also need to run programs such as machine learning and deep learning algorithms, which have high requirements for memory, and microkernels are also required to provide more powerful and general functions for such scenarios.
[0010] Patent document CN102402622A (application number: CN201110447991.4) discloses a memory page management scheduling method for an embedded memory database. The memory space of the memory database is divided into multiple blocks in units of pages, and the size of each page is exactly the same as the size of the database disk file page. Each disk file page is mapped to the memory page, including the following steps: Step 1: The system obtains the offset of the database file to be read and written, and maps the offset to the page number. Step 2: Detect whether there is a hash conflict, if so, perform the corresponding hash conflict resolution process; if not, proceed to the next step; Step 3: Check whether there is a page in the memory that can load data, if not, select a memory page that can exchange data from the memory; if so, proceed to the next step; Step 5: Write the new data content to the memory page. However, this patent does not adopt a design based on the user-mode swap page management system service, and cannot reduce the amount of code introduced into the kernel. Summary of the invention
[0011] In view of the defects in the prior art, the object of the present invention is to provide a memory page swap method and system for a microkernel operating system.
[0012] The memory page swapping method for a microkernel operating system provided by the present invention comprises:
[0013] Step 1: Run a swap page management system service in the user state of the microkernel operating system to exchange memory page data;
[0014] Step 2: Provide unified data encapsulation and transmission for communication between user-mode system services through the exchange page management system service;
[0015] Step 3: Generate a swap page identifier through the swap page management service, and register the virtual address area used to transfer data, the data buffer, and the thread that handles the page fault with the kernel;
[0016] Step 4: When swapping out a page, apply for a virtual address of the page size through the swap page management system service. The kernel modifies the swap page management system service page table to directly map the physical page to be swapped out, and directly reads and transmits the page data in the user state.
[0017] Step 5: When swapping in a page, the swap page management system service reads the specified page data content from the swap area into the data buffer according to the swap page identifier obtained from the page table when the page fault is triggered, and notifies the kernel to use the data for subsequent page fault processing;
[0018] Step 6: Encapsulate the various interfaces of the swap page management service to implement memory page swapping in different swap areas.
[0019] Preferably, the memory page swap system service is run in the user state of the microkernel operating system, which is responsible for initializing the swap area on the external storage device to prepare for storing memory page data. It is necessary to prepare a buffer and channel for data transmission with the kernel and a thread to assist in processing page faults in the user state. It is also necessary to interact with the file system and call the file system interface to read and write the swap area on the storage device.
[0020] The Kernel Swap module runs in the kernel and is responsible for overall control. It monitors memory usage when swapping out. When available memory is low, it interacts with the memory management module in the kernel to obtain memory pages that can be swapped to the swap area, and notifies the memory page swap system service to prepare free pages in the swap area to store memory page data. It obtains the swap page identifier provided by the swap page, records the identifier and the corresponding memory page in the swap page cache module, and maps the memory page data to the virtual address registered by the memory page swap system service. The memory page swap system service accesses the data of this page in user mode and writes it to the external storage device to complete the process. Then, the process that uses the page is found through reverse mapping, and the page table entry of the page is modified from the page table of each process, and the corresponding record is deleted from the swap page cache module, and the swap page page table entry is filled into the page table entry, which is used to trigger a page fault when the swap page is accessed subsequently and enter the swap page page fault error processing function; when swapping in, first search in the swap page cache module according to the swap page page table entry to see if it has been swapped in. If there is a swap-in record, the swapped-in address is directly used for page fault error processing, otherwise the user-state memory page swap system service is notified to transfer the content of the corresponding swap page to the Kernel Swap module. At the same time, the Kernel Swap module applies to the memory management module for a new page, writes the swap page content to the new page, fills the new page physical address into the page table entry in the page table, and determines whether to clear the corresponding record from the swap page cache module according to the number of shared processes recorded in the swap page cache module;
[0021] The swap page cache module is used to record the physical pages being swapped in and out. When a swap in and out request is triggered, the record in the swap page cache module is first checked. If it is already in the record, the information in the record is directly used as the result.
[0022] Preferably, a process in the system has an independent virtual address space, and mapping is performed when accessing addresses to read and write memory data, specifically, the physical page corresponding to the address to be accessed is found through the virtual address maintained in the page table, and one physical page can be mapped to multiple virtual addresses;
[0023] When swapping memory pages, first select the physical page, and then implement the reverse mapping from the physical page index to the virtual address by maintaining the index from the physical page to the physical memory object and the index from the physical memory object to one or more virtual address spaces in the microkernel. Then modify all virtual addresses previously mapped to the physical page.
[0024] Preferably, a page descriptor of a physical memory page to be swapped out is selected through an arbitrary page selection algorithm configured by the kernel, a pointer is maintained in the page descriptor to point to the physical memory object to which the page belongs, and during reverse mapping, the physical memory object to which the page belongs is found through the pointer stored in the page descriptor;
[0025] A linked list is maintained in the physical memory object to record the virtual address area to which the object is mapped, and the virtual address space to which it belongs is recorded in the virtual address area, so that one or more virtual address spaces where a physical memory object is located can be found.
[0026] Preferably, the virtual address space and the process page table are uniquely corresponding, so the process to which the virtual address space belongs can be obtained and the entry of the page table of the process can be found;
[0027] Get the starting position of the virtual address according to the virtual memory area, and get the offset index of the address of the physical page of the memory from the page descriptor, calculate the virtual address of the physical page of the memory, and find the corresponding page table entry in the page table;
[0028] Modify the corresponding page table entry according to the swap mechanism, fill the swap page identifier into the page table entry, and complete a reverse mapping dereference.
[0029] The memory page swapping system for a microkernel operating system provided by the present invention includes:
[0030] Module M1: runs a swap page management system service in the user state of the microkernel operating system to exchange memory page data;
[0031] Module M2: Provides unified data encapsulation and transmission for communication between user-mode system services through the exchange page management system service;
[0032] Module M3: Generates a swap page identifier through the swap page management service, and registers the virtual address area used for data transmission, the data buffer, and the thread for handling page faults with the kernel;
[0033] Module M4: When swapping out a page, a virtual address of the page size is requested through the swap page management system service. The kernel modifies the swap page management system service page table to directly map the physical page to be swapped out, and directly reads and transmits the page data in the user state.
[0034] Module M5: When swapping in a page, the swap page management system service reads the specified page data content from the swap area into the data buffer according to the swap page identifier obtained from the page table when the page fault is triggered, and notifies the kernel to use the data for subsequent page fault processing;
[0035] Module M6: Encapsulates various interfaces of the swap page management service and uses different swap areas to implement memory page swap.
[0036] Preferably, the memory page swap system service is run in the user state of the microkernel operating system, which is responsible for initializing the swap area on the external storage device to prepare for storing memory page data. It is necessary to prepare a buffer and channel for data transmission with the kernel and a thread to assist in processing page faults in the user state. It is also necessary to interact with the file system and call the file system interface to read and write the swap area on the storage device.
[0037] The Kernel Swap module runs in the kernel and is responsible for overall control. It monitors memory usage when swapping out. When available memory is low, it interacts with the memory management module in the kernel to obtain memory pages that can be swapped to the swap area, and notifies the memory page swap system service to prepare free pages in the swap area to store memory page data. It obtains the swap page identifier provided by the swap page, records the identifier and the corresponding memory page in the swap page cache module, and maps the memory page data to the virtual address registered by the memory page swap system service. The memory page swap system service accesses the data of this page in user mode and writes it to the external storage device to complete the process. Then, the process that uses the page is found through reverse mapping, and the page table entry of the page is modified from the page table of each process, and the corresponding record is deleted from the swap page cache module, and the swap page page table entry is filled into the page table entry, which is used to trigger a page fault when the swap page is accessed subsequently and enter the swap page page fault error processing function; when swapping in, first search in the swap page cache module according to the swap page page table entry to see if it has been swapped in. If there is a swap-in record, the swapped-in address is directly used for page fault error processing, otherwise the user-state memory page swap system service is notified to transfer the content of the corresponding swap page to the Kernel Swap module. At the same time, the Kernel Swap module applies to the memory management module for a new page, writes the swap page content to the new page, fills the new page physical address into the page table entry in the page table, and determines whether to clear the corresponding record from the swap page cache module according to the number of shared processes recorded in the swap page cache module;
[0038] The swap page cache module is used to record the physical pages being swapped in and out. When a swap in and out request is triggered, the record in the swap page cache module is first checked. If it is already in the record, the information in the record is directly used as the result.
[0039] Preferably, a process in the system has an independent virtual address space, and mapping is performed when accessing addresses to read and write memory data, specifically, the physical page corresponding to the address to be accessed is found through the virtual address maintained in the page table, and one physical page can be mapped to multiple virtual addresses;
[0040] When swapping memory pages, first select the physical page, and then implement the reverse mapping from the physical page index to the virtual address by maintaining the index from the physical page to the physical memory object and the index from the physical memory object to one or more virtual address spaces in the microkernel. Then modify all virtual addresses previously mapped to the physical page.
[0041] Preferably, a page descriptor of a physical memory page to be swapped out is selected through an arbitrary page selection algorithm configured by the kernel, a pointer is maintained in the page descriptor to point to the physical memory object to which the page belongs, and during reverse mapping, the physical memory object to which the page belongs is found through the pointer stored in the page descriptor;
[0042] A linked list is maintained in the physical memory object to record the virtual address area to which the object is mapped, and the virtual address space to which it belongs is recorded in the virtual address area, so that one or more virtual address spaces where a physical memory object is located can be found.
[0043] Preferably, the virtual address space and the process page table are uniquely corresponding, so the process to which the virtual address space belongs can be obtained and the entry of the page table of the process can be found;
[0044] Get the starting position of the virtual address according to the virtual memory area, and get the offset index of the address of the physical page of the memory from the page descriptor, calculate the virtual address of the physical page of the memory, and find the corresponding page table entry in the page table;
[0045] Modify the corresponding page table entry according to the swap mechanism, fill the swap page identifier into the page table entry, and complete a reverse mapping dereference.
[0046] Compared with the prior art, the present invention has the following beneficial effects:
[0047] (1) The present invention realizes the memory page swap function for the first time for a microkernel architecture that provides paging management, and adopts a design based on user-mode swap page management system services, thereby reducing the amount of code introduced into the kernel and supporting the microkernel operating system to run software with large memory requirements even when hardware resources are limited;
[0048] (2) The present invention utilizes page table mapping to effectively reduce the number of memory copies during the swap process under the microkernel architecture, thereby improving the operating efficiency of the swap system;
[0049] (3) The present invention implements and verifies the memory page swapping of the microkernel operating system based on the ChCore microkernel, and by encapsulating the interface, it can be migrated and deployed to other microkernel operating systems that provide the necessary interfaces and functions. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] Other features, objects and advantages of the present invention will become more apparent from the detailed description of non-limiting embodiments made with reference to the following drawings:
[0051] Figure 1 This is a schematic diagram of the memory page swap mechanism;
[0052] Figure 2 Reverse mapping flow chart implemented for a microkernel designed for object-based resource abstraction;
[0053] Figure 3 Interface encapsulation and initialization flow chart for swap area management and reading and writing for memory page swap system service;
[0054] Figure 4 Specific initialization flow chart for memory page swap system service;
[0055] Figure 5 The flowchart of the memory page swapping method for microkernel operating system. DETAILED DESCRIPTION
[0056] The present invention is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but are not intended to limit the present invention in any form. It should be noted that, for those of ordinary skill in the art, several changes and improvements can also be made without departing from the concept of the present invention. These all belong to the protection scope of the present invention.
[0057] Embodiment 1:
[0058] The present invention provides a memory page swap method for a microkernel operating system, comprising:
[0059] Step 1: Run a swap page management system service in the user state of the microkernel operating system to exchange memory page data;
[0060] Step 2: Provide unified data encapsulation and transmission for communication between user-mode system services through the exchange page management system service;
[0061] Step 3: Generate a swap page identifier through the swap page management service, and register the virtual address area used to transfer data, the data buffer, and the thread that handles the page fault with the kernel;
[0062] Step 4: When swapping out a page, apply for a virtual address of the page size through the swap page management system service. The kernel modifies the swap page management system service page table to directly map the physical page to be swapped out, and directly reads and transmits the page data in the user state.
[0063] Step 5: When swapping in a page, the swap page management system service reads the specified page data content from the swap area into the data buffer according to the swap page identifier obtained from the page table when the page fault is triggered, and notifies the kernel to use the data for subsequent page fault processing;
[0064] Step 6: Encapsulate the various interfaces of the swap page management service to implement memory page swapping in different swap areas.
[0065] The memory page swap system service runs in the user state of the microkernel operating system, which is responsible for initializing the swap area on the external storage device to prepare for storing memory page data. It needs to prepare buffers and channels for data transmission with the kernel and threads to assist in handling page faults in the user state. It also needs to interact with the file system and call the file system interface to read and write the swap area on the storage device.
[0066] The Kernel Swap module runs in the kernel and is responsible for overall control. It monitors memory usage when swapping out. When available memory is low, it interacts with the memory management module in the kernel to obtain memory pages that can be swapped to the swap area, and notifies the memory page swap system service to prepare free pages in the swap area to store memory page data. It obtains the swap page identifier provided by the swap page, records the identifier and the corresponding memory page in the swap page cache module, and maps the memory page data to the virtual address registered by the memory page swap system service. The memory page swap system service accesses the data of this page in user mode and writes it to the external storage device to complete the process. Then, the process that uses the page is found through reverse mapping, and the page table entry of the page is modified from the page table of each process, and the corresponding record is deleted from the swap page cache module, and the swap page page table entry is filled into the page table entry, which is used to trigger a page fault when the swap page is accessed subsequently and enter the swap page page fault error processing function; when swapping in, first search in the swap page cache module according to the swap page page table entry to see if it has been swapped in. If there is a swap-in record, the swapped-in address is directly used for page fault error processing, otherwise the user-state memory page swap system service is notified to transfer the content of the corresponding swap page to the Kernel Swap module. At the same time, the Kernel Swap module applies to the memory management module for a new page, writes the swap page content to the new page, fills the new page physical address into the page table entry in the page table, and determines whether to clear the corresponding record from the swap page cache module according to the number of shared processes recorded in the swap page cache module;
[0067] The swap page cache module is used to record the physical pages being swapped in and out. When a swap in and out request is triggered, the record in the swap page cache module is first checked. If it is already in the record, the information in the record is directly used as the result.
[0068] In the system, processes have independent virtual address spaces, which are mapped when accessing addresses to read and write memory data. Specifically, the physical page corresponding to the address to be accessed is found through the virtual address maintained in the page table. One physical page can be mapped by multiple virtual addresses.
[0069] When swapping memory pages, first select the physical page, and then implement the reverse mapping from the physical page index to the virtual address by maintaining the index from the physical page to the physical memory object and the index from the physical memory object to one or more virtual address spaces in the microkernel. Then modify all virtual addresses previously mapped to the physical page.
[0070] The page descriptor of the physical memory page to be swapped out is selected through the arbitrary page selection algorithm configured by the kernel. A pointer is maintained in the page descriptor to point to the physical memory object to which the page belongs. During reverse mapping, the physical memory object is found through the pointer saved in the page descriptor.
[0071] A linked list is maintained in the physical memory object to record the virtual address area to which the object is mapped, and the virtual address space to which it belongs is recorded in the virtual address area, so that one or more virtual address spaces where a physical memory object is located can be found.
[0072] The virtual address space and the process page table are uniquely corresponding, so the process to which the virtual address space belongs can be obtained and the entry of the page table of the process can be found;
[0073] Get the starting position of the virtual address according to the virtual memory area, and get the offset index of the address of the physical page of the memory from the page descriptor, calculate the virtual address of the physical page of the memory, and find the corresponding page table entry in the page table;
[0074] Modify the corresponding page table entry according to the swap mechanism, fill the swap page identifier into the page table entry, and complete a reverse mapping dereference.
[0075] Embodiment 2:
[0076] The present invention also provides a memory page swap system for a microkernel operating system. The memory page swap system for a microkernel operating system can be implemented by executing the process steps of the memory page swap method for a microkernel operating system, that is, those skilled in the art can understand the memory page swap method for a microkernel operating system as a preferred implementation of the memory page swap system for a microkernel operating system.
[0077] The memory page swapping system for microkernel operating systems includes:
[0078] A swap page management system service is run in the user state of the microkernel operating system to implement the memory page data exchange function, reduce the code in the kernel, and comply with the microkernel design principles.
[0079] The swap page management system service provides unified data encapsulation and transmission for communication between the kernel and other user-mode system services such as storage device read-write drivers and file systems.
[0080] The swap page management service is responsible for generating swap page identifiers and registering with the kernel the virtual address area used to transfer data, the data buffer, and the thread that handles page faults.
[0081] When swapping out a page, the swap page management system service applies for a virtual address of the page size. The kernel modifies the swap page management system service page table to directly map the physical page that needs to be swapped out. The page data can be directly read and transmitted in user mode, reducing the performance overhead caused by memory copying.
[0082] When swapping in a page, the swap page management system service reads the specified page data content from the swap area into the data buffer based on the swap page identifier obtained from the page table when a page fault is triggered, and notifies the kernel to use the data for subsequent page fault processing.
[0083] The various interfaces of the swap page management service are fully encapsulated, allowing developers to design and use different swap area implementation strategies. It is also conducive to adapting to other kernels that provide related capabilities and has high scalability.
[0084] Specifically, in a microkernel operating system where the kernel manages memory allocation, user-mode system services and programs usually cannot obtain information about memory usage and memory page allocation. In such an environment, it is more efficient to place the three parts of determining whether to swap memory pages, selecting memory pages to swap out, and handling page faults generated by swap pages in the kernel, while processes such as swap area management and swap area data reading and writing can be handled by user-mode system services.
[0085] The page switching mechanism as a whole can be divided into the cooperation of multiple modules, such as Figure 1 As shown in the figure, first of all, the memory page swap system service (Swapmgr) in user mode is responsible for initializing the swap area on external storage devices such as disks to prepare for storing memory page data; it needs to prepare buffers and channels for data transmission with the kernel and threads to assist in handling page faults in user mode. Swapmgr also needs to interact with the file system and call the file system interface to read and write the swap area on the storage device.
[0086] Secondly, the Kernel Swap module in the kernel is responsible for overall control. When swapping out, it needs to monitor memory usage, interact with the kernel memory management module (Kernel MM) when available memory is low, obtain memory pages that can be swapped to the swap area, and notify Swapmgr to prepare free pages in the swap area to store memory page data, obtain the swap page identifier provided by the swap page, record the identifier and the corresponding memory page in the swap page cache module (Swap Cache), and map the memory page data to the virtual address registered by Swapmgr. Swapmgr can access the data of this page in user mode and write it to the external storage device. After completion, it finds the process that uses the page through reverse mapping, modifies the page table entry (PTE) of the page from the page table (Page Table) of each process, deletes the corresponding record from the Swap Cache, and fills the swap page page table entry (swap_entry) into the page table entry, so that the program can trigger a page fault when accessing the swap page in the future and enter the swap page page fault handling function. When swapping in, since the page table entry has been modified before, the process of processing the swap page fault error in the Kernel Swap module can be correctly entered. First, according to swap_entry, it is searched in the Swap Cache to see if it has been swapped in. If there is a swap-in record, the swapped-in address can be directly used for page fault processing. Otherwise, the user-mode Swapmgr is notified to transfer the content of the corresponding swap page to the Kernel Swap module. At the same time, the KernelSwap module applies to the Kernel MM module to obtain a new page, writes the swap page content to the new page, and fills the new page physical address into the page table entry in the page table. In addition, it is also necessary to determine whether the corresponding record can be cleared from the Swap Cache based on the number of shared processes recorded in the Swap Cache.
[0087] The Swap Cache module does not actually cache the data of the entire physical page, but only records the physical page being swapped in and out. When a swap request is triggered, it will first check the record in the Swap Cache. If it is already in the record, the information in the record can be directly used as the result. This can effectively avoid the problem of repeated swapping in, repeated swapping out, and being accessed during the swapping out process.
[0088] As long as the kernel provides the corresponding abstraction and interface, the memory page exchange system service in the present invention can be accessed. The algorithm for selecting pages in the kernel can be selected by the user. In the present invention, the microkernel based on the object-based resource abstraction mode realizes the reverse mapping from the page descriptor to the mapping address by maintaining the connection between the page descriptor, the physical memory object, the virtual address area and the virtual address space.
[0089] like Figure 2, is the reverse mapping process implemented by the microkernel designed for object-based resource abstraction in the present invention, and the specific steps are as follows:
[0090] In the system, a process has an independent virtual address space. When accessing an address to read or write memory data, the physical page corresponding to the address to be accessed is found through the page table. The method of finding a physical page from a virtual address maintained in the page table is called mapping. A physical page may be mapped by multiple virtual addresses. When swapping memory pages, the physical page is selected first, and then the virtual address previously mapped to the physical page needs to be modified because the physical page will be swapped out. In other words, a method is needed to find all virtual addresses mapped to the physical page from the physical page. This method is called reverse mapping. The working principle of reverse mapping is to achieve reverse mapping from physical page index to virtual address by maintaining the index from physical page to physical memory object and the index from physical memory object to one or more virtual address spaces in the microkernel.
[0091] Select the page descriptor of the physical memory page to be swapped out through any page selection algorithm configured by the kernel;
[0092] Since a physical page usually belongs to only one physical memory object in an object-based microkernel, a pointer can be maintained in the page descriptor, pointing to the physical memory object to which the page belongs, so that during reverse mapping, the physical memory object can be found through the pointer stored in the page descriptor;
[0093] In a microkernel that provides virtual address space capabilities, physical memory objects are mapped to a virtual memory area in the process address space before they can be accessed and modified by user-mode processes. The virtual address space of a process contains multiple virtual memory areas. Considering that the same segment of physical memory may be mapped by multiple processes, a linked list can be maintained in the physical memory object to record which virtual address areas the object is mapped to, and the virtual address area will record the virtual address space to which it belongs, so that one or more virtual address spaces can be found from a physical memory object;
[0094] The virtual address space and the process page table are uniquely corresponding, so you can get the process to which the virtual address space belongs, and then find the entry of the page table of the process;
[0095] By obtaining the starting position of the virtual address according to the virtual memory area and the offset index of the address of the physical page of the memory from the page descriptor, the virtual address of the physical page of the memory can be calculated and the corresponding page table entry can be found in the page table;
[0096] According to the swap mechanism, the corresponding page table entry is modified and the swap page identifier is filled into the page table entry, completing a reverse mapping dereference.
[0097] This reverse mapping process makes full use of the object-based resource abstraction design in the microkernel. According to the reference relationship between objects, it can maintain the indexes from physical pages to physical memory objects, physical objects to virtual address segments, virtual address segments to virtual address spaces, and virtual address spaces to processes and page tables with less overhead, without the need to introduce more new modules and new structures to implement reverse mapping lookups as in the macro kernel, which reflects the advantages brought by the microkernel's perfect resource abstraction.
[0098] like Figure 3 , is the interface encapsulation and initialization logic of the memory page swap system service for swap area management and reading and writing. The interface is mainly divided into getting swap page descriptor (get_swap_page), writing swap page (write_swap_page) and reading swap page (read_swap_page). The memory page swap system service provides these interfaces when initializing the swap area, and calls the corresponding swap area operation interface according to the swap area used in subsequent operation. The swap area can be managed in a similar way to the memory partner system, or in a common cluster management mode, which can be selected and adapted by the developer. The interface for reading and writing swap pages can have multiple specific implementation methods, and can use files, block devices, compressed memory and other methods to store swap pages.
[0099] like Figure 4, is the specific initialization process of the memory page swap system service, which is mainly divided into three parts: initializing the swap area, initializing the swap-out thread, and initializing the swap-in thread. The initialization of the swap area mainly involves configuring the swap area size and providing the specific implementation of the swap area function mentioned in the previous part. The steps of initializing the swap-out thread include applying for the virtual address of the swapoutbuffer, creating the swapout routine thread, creating the swapout task ring buffer, and registering the swapout thread with the kernel and waiting for the kernel to notify the swapout task. Here, the swapout buffer only applies for a continuous virtual address of the page size, and does not need to allocate real memory, because when the kernel issues a swap-out task, it will map the page to be swapped out to the virtual address provided by swapmgr when registering with the kernel. When swapmgr receives the task notification issued by the kernel, it can directly access this virtual address to obtain the page data without performing additional memory copying, which can reduce performance overhead. The steps of initializing the swap-in thread include applying for the swapin buffer, creating the swapin routine thread, registering the swapin thread with the kernel as the user-mode swap page fault processing thread, and waiting for the kernel to be awakened and processed when a swap page fault occurs. Here, the swapin buffer needs to be allocated as a continuous page-sized memory to store the data read from the swap area in the buffer. The kernel can access the read data through the virtual address provided when the thread is registered, and then put it into a new page for subsequent page fault processing.
[0100] like Figure 5 , which is a flow chart of a memory page swap method for a microkernel operating system, including:
[0101] Step 1: The swap page management system service initializes the swap area and registers with the kernel;
[0102] Step 2: The system runs normally;
[0103] Step 3: The kernel detects that the system has insufficient available memory;
[0104] Step 4: The kernel triggers memory page swapping;
[0105] Step 5: The kernel calculates the amount of memory that needs to be swapped out;
[0106] Step 6: The kernel notifies the user-mode swap page management system service (swapmgr);
[0107] Step 7: The swap page management system service prepares a swap page in the swap area for storing swapped-out data;
[0108] Step 8: The swap page management system service informs the kernel of the generated swap page table entry (swap_entry);
[0109] Step 9: The kernel selects the appropriate anonymous physical page to start swapping out based on the LRU linked list or other methods;
[0110] Step 10: The kernel records the physical page and the corresponding swap_entry information into the swap cache;
[0111] Step 11: The kernel finds and releases the reference of the page table to the physical page according to the reverse mapping, and fills in swap_entry as the new page table entry;
[0112] Step 12: The kernel maps the physical page to swapmgr and notifies swapmgr to write to the swap area;
[0113] Step 13: swapmgr writes the physical page to the swap page corresponding to swap_entry and notifies the kernel;
[0114] Step 14: The kernel releases the corresponding physical page according to the swap_entry provided by swapmgr;
[0115] Step 15: Available memory increases and the system runs normally.
[0116] Those skilled in the art know that, in addition to implementing the system, device and its various modules provided by the present invention in a purely computer-readable program code, it is entirely possible to implement the same program in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers and embedded microcontrollers by logically programming the method steps. Therefore, the system, device and its various modules provided by the present invention can be considered as a hardware component, and the modules included therein for implementing various programs can also be considered as structures within the hardware component; the modules for implementing various functions can also be considered as both software programs for implementing the method and structures within the hardware component.
[0117] The above describes the specific embodiments of the present invention. It should be understood that the present invention is not limited to the above specific embodiments, and those skilled in the art can make various changes or modifications within the scope of the claims, which does not affect the essence of the present invention. In the absence of conflict, the embodiments of the present application and the features in the embodiments can be combined with each other arbitrarily.
Claims
1. A memory page swapping method for a microkernel operating system, It is characterized in that include: Step 1: Run a swap page management system service in the user state of the microkernel operating system to exchange memory page data; Step 2: Provide unified data encapsulation and transmission for communication between user-mode system services through the exchange page management system service; Step 3: Generate a swap page identifier through the swap page management service, and register the virtual address area used to transfer data, the data buffer, and the thread that handles the page fault with the kernel; Step 4: When swapping out a page, apply for a virtual address of the page size through the swap page management system service. The kernel modifies the swap page management system service page table to directly map the physical page to be swapped out, and directly reads and transmits the page data in the user state. Step 5: When swapping in a page, the swap page management system service reads the specified page data content from the swap area into the data buffer according to the swap page identifier obtained from the page table when the page fault is triggered, and notifies the kernel to use the data for subsequent page fault processing; Step 6: Encapsulate the various interfaces of the swap page management service to implement memory page swapping in different swap areas.
2. The memory page swapping method for a microkernel operating system according to claim 1, It is characterized in that The memory page swap system service runs in the user state of the microkernel operating system, which is responsible for initializing the swap area on the external storage device to prepare for storing memory page data. It needs to prepare buffers and channels for data transmission with the kernel and threads to assist in handling page faults in the user state. It also needs to interact with the file system and call the file system interface to read and write the swap area on the storage device. The Kernel Swap module runs in the kernel and is responsible for overall control. It monitors memory usage when swapping out. When available memory is low, it interacts with the memory management module in the kernel to obtain memory pages that can be swapped to the swap area, and notifies the memory page swap system service to prepare free pages in the swap area to store memory page data. It obtains the swap page identifier provided by the swap page, records the identifier and the corresponding memory page in the swap page cache module, and maps the memory page data to the virtual address registered by the memory page swap system service. The memory page swap system service accesses the data of this page in user mode and writes it to the external storage device to complete the process. Then, the process that uses the page is found through reverse mapping, and the page table entry of the page is modified from the page table of each process, and the corresponding record is deleted from the swap page cache module, and the swap page page table entry is filled into the page table entry, which is used to trigger a page fault when the swap page is accessed subsequently and enter the swap page page fault error processing function; when swapping in, first search in the swap page cache module according to the swap page page table entry to see if it has been swapped in. If there is a swap-in record, the swapped-in address is directly used for page fault error processing, otherwise the user-state memory page swap system service is notified to transfer the content of the corresponding swap page to the Kernel Swap module. At the same time, the Kernel Swap module applies to the memory management module for a new page, writes the swap page content to the new page, fills the new page physical address into the page table entry in the page table, and determines whether to clear the corresponding record from the swap page cache module according to the number of shared processes recorded in the swap page cache module; The swap page cache module is used to record the physical pages being swapped in and out. When a swap in and out request is triggered, the record in the swap page cache module is first checked. If it is already in the record, the information in the record is directly used as the result.
3. The memory page swapping method for a microkernel operating system according to claim 1, It is characterized in that In the system, processes have independent virtual address spaces, which are mapped when accessing addresses to read and write memory data. Specifically, the physical page corresponding to the address to be accessed is found through the virtual address maintained in the page table. One physical page can be mapped by multiple virtual addresses. When swapping memory pages, first select the physical page, and then implement the reverse mapping from the physical page index to the virtual address by maintaining the index from the physical page to the physical memory object and the index from the physical memory object to one or more virtual address spaces in the microkernel. Then modify all virtual addresses previously mapped to the physical page.
4. The memory page swapping method for a microkernel operating system according to claim 1, It is characterized in that The page descriptor of the physical memory page to be swapped out is selected through the arbitrary page selection algorithm configured by the kernel. A pointer is maintained in the page descriptor to point to the physical memory object to which the page belongs. During reverse mapping, the physical memory object is found through the pointer saved in the page descriptor. A linked list is maintained in the physical memory object to record the virtual address area to which the object is mapped, and the virtual address space to which it belongs is recorded in the virtual address area, so that one or more virtual address spaces where a physical memory object is located can be found.
5. The memory page swapping method for a microkernel operating system according to claim 1, It is characterized in that The virtual address space and the process page table are uniquely corresponding, so the process to which the virtual address space belongs can be obtained and the entry of the page table of the process can be found; Get the starting position of the virtual address according to the virtual memory area, and get the offset index of the address of the physical page of the memory from the page descriptor, calculate the virtual address of the physical page of the memory, and find the corresponding page table entry in the page table; Modify the corresponding page table entry according to the swap mechanism, fill the swap page identifier into the page table entry, and complete a reverse mapping dereference.
6. A memory page swapping system for microkernel operating systems, It is characterized in that include: Module M1: runs a swap page management system service in the user state of the microkernel operating system to exchange memory page data; Module M2: Provides unified data encapsulation and transmission for communication between user-mode system services through the exchange page management system service; Module M3: Generates a swap page identifier through the swap page management service, and registers the virtual address area used for data transmission, the data buffer, and the thread for handling page faults with the kernel; Module M4: When swapping out a page, a virtual address of the page size is requested through the swap page management system service. The kernel modifies the swap page management system service page table to directly map the physical page to be swapped out, and directly reads and transmits the page data in the user state. Module M5: When swapping in a page, the swap page management system service reads the specified page data content from the swap area into the data buffer according to the swap page identifier obtained from the page table when the page fault is triggered, and notifies the kernel to use the data for subsequent page fault processing; Module M6: Encapsulates various interfaces of the swap page management service and uses different swap areas to implement memory page swap.
7. The memory page swapping system for a microkernel operating system according to claim 6, It is characterized in that The memory page swap system service runs in the user state of the microkernel operating system, which is responsible for initializing the swap area on the external storage device to prepare for storing memory page data. It needs to prepare buffers and channels for data transmission with the kernel and threads to assist in handling page faults in the user state. It also needs to interact with the file system and call the file system interface to read and write the swap area on the storage device. The Kernel Swap module runs in the kernel and is responsible for overall control. It monitors memory usage when swapping out. When available memory is low, it interacts with the memory management module in the kernel to obtain memory pages that can be swapped to the swap area, and notifies the memory page swap system service to prepare free pages in the swap area to store memory page data. It obtains the swap page identifier provided by the swap page, records the identifier and the corresponding memory page in the swap page cache module, and maps the memory page data to the virtual address registered by the memory page swap system service. The memory page swap system service accesses the data of this page in user mode and writes it to the external storage device to complete the process. Then, the process that uses the page is found through reverse mapping, and the page table entry of the page is modified from the page table of each process, and the corresponding record is deleted from the swap page cache module, and the swap page page table entry is filled into the page table entry, which is used to trigger a page fault when the swap page is accessed subsequently and enter the swap page page fault error processing function; when swapping in, first search in the swap page cache module according to the swap page page table entry to see if it has been swapped in. If there is a swap-in record, the swapped-in address is directly used for page fault error processing, otherwise the user-state memory page swap system service is notified to transfer the content of the corresponding swap page to the Kernel Swap module. At the same time, the Kernel Swap module applies to the memory management module for a new page, writes the swap page content to the new page, fills the new page physical address into the page table entry in the page table, and determines whether to clear the corresponding record from the swap page cache module according to the number of shared processes recorded in the swap page cache module; The swap page cache module is used to record the physical pages being swapped in and out. When a swap in and out request is triggered, the record in the swap page cache module is first checked. If it is already in the record, the information in the record is directly used as the result.
8. The memory page swapping system for a microkernel operating system according to claim 6, It is characterized in that In the system, processes have independent virtual address spaces, which are mapped when accessing addresses to read and write memory data. Specifically, the physical page corresponding to the address to be accessed is found through the virtual address maintained in the page table. One physical page can be mapped by multiple virtual addresses. When swapping memory pages, first select the physical page, and then implement the reverse mapping from the physical page index to the virtual address by maintaining the index from the physical page to the physical memory object and the index from the physical memory object to one or more virtual address spaces in the microkernel. Then modify all virtual addresses previously mapped to the physical page.
9. The memory page swapping system for a microkernel operating system according to claim 6, It is characterized in that The page descriptor of the physical memory page to be swapped out is selected through the arbitrary page selection algorithm configured by the kernel. A pointer is maintained in the page descriptor to point to the physical memory object to which the page belongs. During reverse mapping, the physical memory object is found through the pointer saved in the page descriptor. A linked list is maintained in the physical memory object to record the virtual address area to which the object is mapped, and the virtual address space to which it belongs is recorded in the virtual address area, so that one or more virtual address spaces where a physical memory object is located can be found.
10. The memory page swapping system for a microkernel operating system according to claim 6, It is characterized in that The virtual address space and the process page table are uniquely corresponding, so the process to which the virtual address space belongs can be obtained and the entry of the page table of the process can be found; Get the starting position of the virtual address according to the virtual memory area, and get the offset index of the address of the physical page of the memory from the page descriptor, calculate the virtual address of the physical page of the memory, and find the corresponding page table entry in the page table; Modify the corresponding page table entry according to the swap mechanism, fill the swap page identifier into the page table entry, and complete a reverse mapping dereference.
Citation Information
Cited By
Active page changing method and system based on regional reuse pattern recognition
CN121233498A
PreemptRT-based memory real-time enhancement method
CN121614277A
Preempt_rt-based memory real-time enhancement method
CN121614277B
Application-noninductive memory management method and memory management system
CN122132327A
