Linux-based memory recovery method and device for avoiding page swap thrashing
Patent Information
- Application Number
- CN202211610899.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-12
- Publication Date
- 2026-11-03
- Estimated Expiration
- 2042-12-12
AI Technical Summary
In existing Linux operating systems, page thrashing is prone to occur during process switching in the emergency memory reclamation mechanism, affecting system stability, especially for kernel processes with high real-time requirements.
A memory reclamation method based on reinforcement learning is adopted. The activity of memory pages is adjusted by using the LMRL reinforcement learning model to predict and prioritize the retention of memory pages of important processes, thereby reducing page thrashing.
It effectively reduces page thrashing during process switching, improving system stability and real-time performance, especially ensuring the operation of critical processes.
Smart Images

Figure CN116821002B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer application technology, and in particular to a memory reclamation method and apparatus based on Linux that avoids page swapping thrashing. Background Technology
[0002] In an operating system, every process needs memory as a basic resource for operation. With the development of technology, the memory requirements of most processes are increasing, and due to limitations in computer physical resources, memory resource shortages are becoming more frequent. Therefore, to minimize the risk of abnormal memory allocation by the operating system, which could ultimately lead to system crashes, most operating systems are equipped with memory reclamation mechanisms. Taking Linux as an example, in its kernel, Linux uses a doubly linked list to record active and inactive pages. The basic principle for classifying active and inactive pages is that the more frequently used pages are, the more necessary they are; therefore, the most recently used pages are generally in the active page list.
[0003] The first method involves reclaiming most of the memory pages occupied by unnecessary processes when the system enters hibernation. The second method involves the system automatically scanning the memory list in the background at regular intervals, reclaiming inactive memory pages. The third method triggers an emergency memory reclamation mechanism when memory is scarce. Of these three methods, the first and second are relatively smooth and have less impact on the system. However, in the third method, regardless of whether the system is memory-consistent or memory-inconsistent, the memory reclamation process generally triggers a forced memory reclamation mechanism. Taking the Linux operating system as an example, the forced memory reclamation process consists of two steps: direct reclaim and background reclaim. The execution order is as follows: when the remaining memory reaches a certain threshold, the system issues a memory level warning, wakes up the kwswap process, reclaims inactive pages, and frequently scans the active page list, evicting some active pages from the inactive page list for reclamation. If memory becomes even scarcer and the remaining memory continues to decrease, reaching another threshold, the reclamation mechanism switches to direct claim, directly killing some unnecessary kernel processes and reclaiming the memory pages they occupy.
[0004] Traditional methods often have the following problems: In the emergency memory reclamation mechanism, the mechanism ensures that the process gets the basic memory requirement, but because a large number of active pages related to kernel processes are forced to be evicted, many of which are pages that the process needs but are not active enough at the moment, page thrashing is very likely to occur during process switching, which can damage some kernel processes with high real-time requirements and thus affect system stability. Summary of the Invention
[0005] The purpose of this invention is to at least address one of the shortcomings of the prior art by providing a Linux-based memory reclamation method and apparatus that avoids page swapping thrashing.
[0006] To achieve the above objectives, the present invention adopts the following technical solution.
[0007] Specifically, this paper proposes a memory reclamation method based on Linux to avoid page swapping thrashing. In the current Linux system, it consists of two stages, in which...
[0008] Phase 1:
[0009] Step 110: Preset the number and names of kernel processes, initialize them, and start a timer. The timer is used to record the runtime.
[0010] Step 120: After each first time interval T1, calculate the runtime of the current process and update it in the process list;
[0011] Step 130: Before the second stage is triggered, every second duration T2, the 15 processes with the longest runtime in the process chain are defined as frequently used processes. A memory page statistics array is created for each frequently used process. The frequently used page statistics array records n memory pages. Each item in the array table is the score of each memory page, which is used to measure the importance of the memory page to the process. The higher the score, the more important it is.
[0012] Step 140: At a preset time interval, scan the memory active page list and inactive page list corresponding to the kernel structure of all frequently used processes. In the memory frequently used page statistics array, after each scan, the value corresponding to the active page is increased by 3, and the value of the inactive page is increased by 1.
[0013] Step 150: Within one running cycle, determine whether the system has entered the emergency memory reclamation mechanism. If so, proceed to the second stage; otherwise, return to step 130, re-select the top 15 processes with the longest running time, and re-establish their corresponding memory frequently used page statistics array.
[0014] Phase Two:
[0015] Step 210: Determine and initialize the frequently used memory page statistics array to be used as initialization data.
[0016] Step 220: Input the memory page statistics array associated with each process into the LMRL reinforcement learning model and train it according to the preset training rules to obtain the training results. The LMRL reinforcement learning model is constructed based on the Q-learning algorithm model in reinforcement learning.
[0017] Step 230: Perform emergency memory reclamation based on the training results, and exit the second stage and return to step 130 when the preset conditions are met.
[0018] Furthermore, specifically, step 110 includes the following:
[0019] Five kernel processes are preset as commonly used kernel processes. The names of these processes are recorded as strings in the main body of the program as necessary information for subsequent system initialization. The names and number of commonly used kernel processes can be changed and adjusted.
[0020] Furthermore, specifically, step 120 includes the following:
[0021] Every 10 seconds, the timer will trigger a system interrupt. The interrupt handler will obtain the total running time of all processes through the kernel's process time interface. If the obtained process is already in the frequently used process list, the total running time of the corresponding process will be updated. If the process name is not in the frequently used process list, the process name will be added to the frequently used process list, and its running time will be updated.
[0022] Furthermore, specifically, the commonly used process linked list follows these rules:
[0023] The number of processes that can be held in the frequently used process list is limited to 50. Once the limit is reached, no new processes are allowed to be added. In addition, when the number of processes that can be held in the frequently used process list reaches 30, a new timer is started. The new timer will trigger a system interrupt every 5 seconds. The handler will obtain the runtime of the processes in the frequently used process list at the current moment, store it as a snapshot, and compare the current snapshot with the snapshot 5 seconds ago. The handler will select the 3 processes with the smallest runtime increase and delete these 3 processes from the frequently used process list.
[0024] Furthermore, specifically, when updating the runtime of the process, the following rules are followed:
[0025] When a process is a kernel process, the kernel provides an interface to obtain the total time since system startup for this type of process. The time obtained for this type of process can be directly updated to the corresponding entry in the frequently used process list. When a process is a child process derived from the original kernel process, the startup time of this type of process is calculated from the process creation time. If this type of process stores the previous runtime q in the frequently used process list, and the current runtime is p, if the previous runtime q is less than the current runtime p, it is determined that the process was terminated and then restarted, and the runtime t is updated to t+p. If the previous runtime q is not less than the current runtime p, the previous runtime t is updated to the original value plus the difference between the two, that is, the runtime t is updated to t+(p-q).
[0026] Furthermore, specifically, step 210 includes,
[0027] Retrieve the user-specified array of frequently used memory pages. If it exists, use the user-specified array of frequently used memory pages as the initialization data. If it does not exist, use the newly created array of frequently used memory pages as the initialization data.
[0028] Furthermore, specifically, the preset training rules in step 220 include the following:
[0029] The common memory pages associated with each process are used as the initial state S1, and the sum of the total scores of all memory pages is T. The optimization objective of reinforcement learning is to maximize T.
[0030] In each round of optimization, the system will randomly adjust 10 items in the memory frequently used page statistics array. The adjustment method is to randomly add or subtract any integer in [5, 10]. The adjustment action is An. Each state Sn will execute action An to obtain a total score Tn. If the total score Tn corresponding to state Sn is greater than the total score Tn-1 corresponding to state Sn-1, it means that the optimization direction is correct. The total score Tn is taken as the highest score T and T is updated. If the total score Tn is less than the total score Tn-1, T is not updated. Then, action Tn+1 is executed in this direction to obtain state Sn+1. The way to change from state Sn-1 to state Sn is as follows: select the top 20 items with the highest single score in the memory frequently used page statistics array under the condition of state Sn-1, increase their weight in the frequently used process list corresponding to the process, so as to ensure that the memory frequently used pages corresponding to these 20 items will not be eliminated. After 500 milliseconds, the updated memory frequently used page statistics array is taken as state Sn.
[0031] Adjust and optimize the direction with T as the target;
[0032] The training phase consists of 1000 training loops. After multiple rounds of repeated training, the algorithm learns how to adjust the weights of different pages to minimize the possibility of page breaks. In other words, by adjusting the weights to make T higher, page thrashing is less likely to occur.
[0033] Furthermore, specifically, the way to increase the weight in the frequently used process list corresponding to the process is to move the pages in the inactive list that are in the memory pages corresponding to the first 20 items to the active list, leave the pages in the active list unprocessed, and move the pages that are neither in the active list nor in the inactive list to the inactive list.
[0034] Furthermore, after entering the emergency memory reclamation mechanism, this system will capture the page fault information of these processes by the operating system, obtain the specific page corresponding to the page fault, and reduce the score of the specific page in the memory frequently used page statistics array by 5 points.
[0035] This invention also proposes a Linux-based memory reclamation device to avoid page swapping thrashing, comprising:
[0036] The first initialization module is used to preset the number and names of kernel processes, perform initialization, and start a timer. The timer is used to record the runtime.
[0037] The runtime statistics module is used to count the runtime of the current process every time the first duration T1 has elapsed, and update it in the process list.
[0038] The module for establishing a memory frequently used page statistics array is used to define the 15 processes with the longest runtime in the statistical process chain as frequently used processes after each second duration T2 before the second stage is triggered. A memory frequently used page statistics array is established for each frequently used process. The frequently used page statistics array records n memory pages. Each item in the array table is the score of each memory page, which is used to measure the importance of the memory page to the process. The higher the score, the more important it is.
[0039] The memory frequently used page statistics array adjustment module is used to scan the memory active page list and inactive page list corresponding to the kernel structure of all frequently used processes at preset time intervals. In the memory frequently used page statistics array, the value of the active page is increased by 3 after each scan, and the value of the inactive page is increased by 1.
[0040] The judgment module is used to determine whether the system has entered the emergency memory reclamation mechanism within a running cycle. If so, it enters the second stage; otherwise, it returns to the memory frequently used page statistics array creation module to run, re-select the top 15 processes with the longest running time, and re-create their corresponding memory frequently used page statistics arrays.
[0041] The second initialization module is used to determine and initialize the frequently used memory page statistics array, which will be used as initialization data.
[0042] The training module is used to input the statistical array of commonly used memory pages associated with each process into the LMRL reinforcement learning model and train it according to the preset training rules to obtain the training results. The LMRL reinforcement learning model is built based on the Q-learning algorithm model in reinforcement learning.
[0043] The emergency memory reclamation module is used to perform emergency memory reclamation based on the training results, and when the preset conditions are met, it exits the second stage and returns to the memory frequently used page statistics array creation module to run.
[0044] The beneficial effects of this invention are as follows:
[0045] This invention targets several kernel processes that have the longest usage time. The system initializes independent process page dependency models for each of these processes to assess their dependence on specific memory pages. The system adjusts these models in real-time to minimize the number of page faults in both normal and memory-constrained scenarios. This invention also starts simultaneously after operating system initialization; when the kernel triggers kwswap, the system adjusts page priorities for each process based on the dependency of different pages. This ensures that the pages necessary for the operation of these critical processes remain in the active page list during process switching after kwswap triggering. Attached Figure Description
[0046] The above and other features of this disclosure will become more apparent from the detailed description of the embodiments illustrated in conjunction with the accompanying drawings. In the accompanying drawings, the same reference numerals denote the same or similar elements. Obviously, the drawings described below are merely some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained from these drawings without any creative effort. In the drawings:
[0047] Figure 1 The diagram shows the flowchart of the first stage of a Linux-based memory reclamation method to avoid page swapping thrashing, according to the present invention.
[0048] Figure 2 The diagram shows the second stage of a Linux-based memory reclamation method for avoiding page swapping thrashing, as described in this invention. Detailed Implementation
[0049] The following will provide a clear and complete description of the concept, specific structure, and technical effects of the present invention in conjunction with embodiments and accompanying drawings, so as to fully understand the purpose, solution, and effects of the present invention. It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other. The same reference numerals used throughout the accompanying drawings indicate the same or similar parts.
[0050] Example 1: This invention proposes a memory reclamation method based on Linux to avoid page swapping thrashing. In the current Linux system, it consists of two stages, wherein...
[0051] Reference Figure 1 Phase 1:
[0052] Step 110: Preset the number and names of kernel processes, initialize them, and start a timer. The timer is used to record the runtime.
[0053] Step 120: After each first time interval T1, calculate the runtime of the current process and update it in the process list;
[0054] Step 130: Before the second stage is triggered, every second duration T2, the 15 processes with the longest runtime in the process chain are defined as frequently used processes. A memory page statistics array is created for each frequently used process. The frequently used page statistics array records n memory pages. Each item in the array table is the score of each memory page, which is used to measure the importance of the memory page to the process. The higher the score, the more important it is.
[0055] Step 140: At preset time intervals, scan the memory active page list and inactive page list corresponding to the kernel structure of all frequently used processes. In the memory frequently used page statistics array, after each scan, the value corresponding to the active page is incremented by 3, and the value of the inactive page is incremented by 1. The memory active page list and inactive page list are key data structures relied upon by the Linux operating system for memory reclamation. From the system's perspective, frequently accessed memory pages are placed in the active list, while infrequently used pages are placed in the inactive list. Pages in the two doubly linked lists can move between each other, and the operating system adjusts the list in which a page should be placed based on its activity level. Pages in the active list are active pages, and those in the inactive list are inactive pages.
[0056] Step 150: Within a running cycle, determine whether the system has entered the emergency memory reclamation mechanism (by adding a soft interrupt trigger in the Linux kernel memory management module, an interrupt will be triggered every time emergency memory reclamation is entered, and the system will capture the interrupt and enter the interrupt handling, which is the second stage mentioned later).
[0057] If yes, proceed to the second stage; otherwise, return to step 130, re-filter the top 15 processes with the longest runtime, and rebuild their corresponding memory page statistics array.
[0058] Reference Figure 2 Phase Two:
[0059] Step 210: Determine and initialize the frequently used memory page statistics array to be used as initialization data.
[0060] Step 220: Input the memory page statistics array associated with each process into the LMRL reinforcement learning model and train it according to the preset training rules to obtain the training results. The LMRL reinforcement learning model is constructed based on the Q-learning algorithm model in reinforcement learning.
[0061] Step 230: Perform emergency memory reclamation based on the training results, and exit the second stage and return to step 130 when the preset conditions are met.
[0062] In a preferred embodiment of the present invention, step 110 specifically includes the following:
[0063] Five kernel processes are preset as commonly used kernel processes. The names of these processes are recorded as strings in the main body of the program as necessary information for subsequent system initialization. The names and number of commonly used kernel processes can be changed and adjusted.
[0064] Furthermore, specifically, step 120 includes the following:
[0065] Every 10 seconds, the timer will trigger a system interrupt. The interrupt handler will obtain the total running time of all processes through the kernel's process time interface. If the obtained process is already in the frequently used process list, the total running time of the corresponding process will be updated. If the process name is not in the frequently used process list, the process name will be added to the frequently used process list, and its running time will be updated.
[0066] In a preferred embodiment of the present invention, the commonly used process linked list specifically follows the following rules.
[0067] The number of processes that can be held in the frequently used process list is limited to 50. Once the limit is reached, no new processes are allowed to be added. In addition, when the number of processes that can be held in the frequently used process list reaches 30, a new timer is started. The new timer will trigger a system interrupt every 5 seconds. The handler will obtain the runtime of the processes in the frequently used process list at the current moment, store it as a snapshot, and compare the current snapshot with the snapshot 5 seconds ago. The handler will select the 3 processes with the smallest runtime increase and delete these 3 processes from the frequently used process list.
[0068] In a preferred embodiment of the present invention, the following rules are followed during the runtime of the update process:
[0069] When a process is a kernel process, the kernel provides an interface to obtain the total time since system startup for this type of process. The time obtained for this type of process can be directly updated to the corresponding entry in the frequently used process list. When a process is a child process derived from the original kernel process, the startup time of this type of process is calculated from the process creation time. If this type of process stores the previous runtime q in the frequently used process list, and the current runtime is p, if the previous runtime q is less than the current runtime p, it is determined that the process was terminated and then restarted, and the runtime t is updated to t+p. If the previous runtime q is not less than the current runtime p, the previous runtime t is updated to the original value plus the difference between the two, that is, the runtime t is updated to t+(p-q).
[0070] In a preferred embodiment of the present invention, step 210 specifically includes:
[0071] Retrieve the user-specified array of frequently used memory pages. If it exists, use the user-specified array of frequently used memory pages as the initialization data. If it does not exist, use the newly created array of frequently used memory pages as the initialization data.
[0072] In a preferred embodiment of the present invention, the preset training rules in step 220 specifically include the following:
[0073] The common memory pages associated with each process are used as the initial state S1, and the sum of the total scores of all memory pages is T. The optimization objective of reinforcement learning is to maximize T.
[0074] In each round of optimization, the system will randomly adjust 10 items in the memory frequently used page statistics array. The adjustment method is to randomly add or subtract any integer in [5, 10]. The adjustment action is An. Each state Sn will execute action An to obtain a total score Tn. If the total score Tn corresponding to state Sn is greater than the total score Tn-1 corresponding to state Sn-1, it means that the optimization direction is correct. The total score Tn is taken as the highest score T and T is updated. If the total score Tn is less than the total score Tn-1, T is not updated. Then, action Tn+1 is executed in this direction to obtain state Sn+1. The way to change from state Sn-1 to state Sn is as follows: select the top 20 items with the highest single score in the memory frequently used page statistics array under the condition of state Sn-1, increase their weight in the frequently used process list corresponding to the process, so as to ensure that the memory frequently used pages corresponding to these 20 items will not be eliminated. After 500 milliseconds, the updated memory frequently used page statistics array is taken as state Sn.
[0075] Specifically, in the first round of optimization, the system randomly adjusts 10 items in the frequently used memory page statistics array by randomly adding or subtracting any integer from [5, 10]. This adjustment action is A1. After executing action A1 in state S1, the total score T1 is obtained. The top 20 items with the highest individual scores in the frequently used memory page statistics array are selected, and their weights in the frequently used process list corresponding to the process are increased to ensure that the frequently used memory pages corresponding to these 20 items are not eliminated. After 500 milliseconds, the updated frequently used memory page statistics array is set as state S2. If the total score T2 after executing action A1 in state S2 is higher than the total score T1 in state S1, it indicates that the optimization direction is correct. Based on this direction, action A1 is executed to obtain T3 in state S3. If T3 is less than T2, T2 is still used as the highest score T. If it is greater than T2, T is updated to T3. (This mainly involves 1. an existing state Sn and 2. executing action An.) 3. A loop obtains a new state S(n+1), where each Sn corresponds to a score Tn. The action refers to adjusting the score weights of pages during training, thereby adjusting the positions of some memory pages in the active or inactive linked lists. The state is the arrangement of memory pages in the linked list. In short, T records the highest score across all Ts. If the total score T2 under state S2 is not higher than the total score under state S1, then T is not updated.
[0076] Adjust and optimize the direction with T as the target;
[0077] The training phase consists of 1000 training loops. After multiple rounds of repeated training, the algorithm learns how to adjust the weights of different pages to minimize the possibility of page breaks. In other words, by adjusting the weights to make T higher, page thrashing is less likely to occur.
[0078] As a preferred embodiment of the present invention, specifically, the method of increasing the weight in the commonly used process list corresponding to the process is to move the pages in the inactive list that are in the memory pages corresponding to the first 20 items to the active list, and do not process the pages that are in the active list. If the page is neither in the active list nor in the inactive list, then move the page to the inactive list.
[0079] In a preferred embodiment of the present invention, after entering the emergency memory reclamation mechanism, the system will capture the page fault interrupt information of these processes by the operating system, obtain the specific page corresponding to the page fault interrupt, and reduce the score of the specific page in the memory frequently used page statistics array by 5 points.
[0080] During a single complete run, the method of this invention may experience all of the following:
[0081] Part One, System Initialization Phase: The overall process of Phase One is shown in the attached figure. Figure 1 As shown:
[0082] Before being compiled into the system kernel, this invention pre-defines five commonly used kernel processes for the Linux operating system. The names of these processes are recorded as strings in the program body, serving as necessary information for subsequent system initialization. The names and number of these pre-definement processes can be changed according to the specific requirements of the system.
[0083] The system initializes a linked list of commonly used kernel processes using the names of five preset, frequently used kernel processes as initialization information. This linked list records the names of the frequently used processes and their execution times. The initialization times for all preset, frequently used processes are recorded as follows:
[0084] The timer is started during initialization. The timer is one of the basic recording functions provided by the operating system; its recorded time is derived from the number of hardware clock cycles, which is then calculated using the formula: Elapsed Time = Number of Cycles / Clock Frequency. (This is essentially initialization.)
[0085] Every 10 seconds, the timer triggers a system interrupt. The interrupt handler uses the kernel's process time interface to obtain the total runtime of all processes. If the obtained process is already in the frequently used process list, the total runtime of the corresponding process is updated; if the process name is not in the frequently used process list, the process name is added to the list, and the runtime is updated.
[0086] As a preferred approach, the total length of the commonly used process list should not exceed 50 entries. Once the list reaches this limit, new process names are no longer added to the list. Simultaneously, when the process list reaches 30 entries, a new timer is started. This timer triggers a system interrupt every 5 seconds. The handler retrieves the time of the currently running processes and stores it as a snapshot. It then compares the current snapshot with the snapshot from 5 seconds ago, selecting the three processes with the smallest time increase and removing them from the list.
[0087] As a preferred approach, updating runtime can involve two scenarios. First, kernel processes have fixed process IDs. For these processes, the kernel provides an interface to retrieve the total runtime since system startup. The runtime obtained for these processes can be directly updated to the corresponding entry in the frequently used process list. Second, processes are child processes derived from the original kernel process. The startup time of these processes is calculated from their creation time. For these processes, the previous runtime q is stored in the linked list. If the previous runtime q is less than the current runtime p (i.e., p - q > 0), then the runtime t is updated to p. Otherwise, the runtime t is updated to the original value plus the difference between the two, i.e., t = t + (p - q). (This is equivalent to retrieving the runtime of the relevant process.)
[0088] Before the system triggers memory reclamation, a timer will trigger an interrupt every 10 minutes. The interrupt handler will count the 15 longest-running processes in the frequently used process list and create a memory frequently used page statistics array for each of the 15 processes.
[0089] At 1-second intervals, scan the memory active page list and inactive page list corresponding to the kernel structure of all frequently used processes. In the memory frequently used page statistics array, after each scan, the value corresponding to the active page is increased by 3, the value of the inactive page is increased by 1, and other items not involved remain unchanged.
[0090] If the operating system does not enter the emergency memory reclamation mechanism after 10 minutes, return to step 7, re-select the top 15 processes with the longest running time, and rebuild the memory frequently used page statistics array.
[0091] If the emergency memory reclamation mechanism is triggered within 10 minutes, the process will proceed to the stage of establishing a memory page correlation model.
[0092] Part Two, the stage of establishing and using the memory page affinity model, the overall process of Stage Two is as follows: Figure 2 As shown
[0093] There are two schemes during the initialization phase. Scheme 1: The user does not specify a common memory page statistics array. In this case, the common memory page statistics array from step 9 will be input as the initialization dataset, and the process will proceed to step 12. Scheme 2: The user can specify a specific common memory page statistics array as the initialization data. The subsequent processing steps for Scheme 2 are basically the same as those for Scheme 1.
[0094] For each process, a specific array of frequently used pages is input into the LMRL (Linux Memory Reclaim Reinforcement Learning) reinforcement learning model. This model is built upon the Q-learning algorithm in reinforcement learning.
[0095] As a preferred aspect, the main structure of the input data for this model is an array of n frequently used pages, where n is the total number of memory pages owned by the corresponding process. Each item in the array is a score for each page, used to measure the importance of that memory page to the process. A higher score indicates greater importance to the process, and a lower score indicates less importance to the process.
[0096] Upon entering the emergency memory reclamation mechanism, this system captures page fault information from the operating system for these processes and retrieves the specific page corresponding to the page fault. In the frequently used memory page statistics array, the score corresponding to that specific page is reduced by 5 points.
[0097] As a preferred approach, the LMRL algorithm is divided into a training phase and an application phase. During the training phase, taking Scheme 1 as an example, after inputting the statistical array of frequently used memory pages obtained from the system initialization phase into the LMRL, the model uses this input as its initial state S1. The sum of the total scores of all memory pages is T, and the optimization objective of reinforcement learning is to maximize T. In the first round of optimization, the system will speculatively adjust 10 of all numerical items by randomly adding or subtracting any integer from [5, 10]. This adjustment action is A1. After S1 executes the corresponding optimization action A1, it also needs to select the top 20 items with the highest individual scores based on the adjusted memory page correlation model and increase their weight in the page list corresponding to the process to ensure that these 20 pages are not evicted. After 500 milliseconds, the updated frequently used memory page statistics array is set as state S2. If the total score T2 in state S2 is higher than the total score in state S1, the optimization direction is correct. Based on this direction, action A3 is executed, state S3 is obtained, and T2 is recorded as the highest score T. Otherwise, T is not updated, and the optimization direction is adjusted with T as the target. The training loop count is 1000 times. After multiple rounds of repeated training, the algorithm learns how to adjust the weights of different pages to minimize the possibility of page breaks. That is, the adjustment method in the model that makes T higher is the optimization method that is less likely to cause page breakage.
[0098] As a preferred approach, the weighting is increased by moving pages from the 20 pages that exist in the inactive list to the active list; if a page does not exist in either the active or inactive list, then the page table is moved to the inactive list.
[0099] After LMRL training is completed, the top 20 pages with the highest scores in the scoring model process are marked as the pages with the highest process relevance. The corresponding active list of the model is adjusted every 1 second to ensure that these 20 pages always exist in the active list. Pages ranked from 20 to 50 are marked as the pages with the second highest process relevance. The corresponding inactive list of the model is adjusted every 10 seconds to ensure that the pages with the second highest relevance exist in the inactive list.
[0100] The phase of establishing and using the memory page associativity model will end in the following two situations: Situation 1: The operating system exits the emergency memory reclamation mechanism; Situation 2: The runtime after entering phase 2 reaches 30 minutes. In both cases, the system will return to step 7 of the system initialization phase and restart the process of acquiring the most frequently used processes within the last 10 minutes.
[0101] This invention also proposes a Linux-based memory reclamation device to avoid page swapping thrashing, comprising:
[0102] The first initialization module is used to preset the number and names of kernel processes, perform initialization, and start a timer. The timer is used to record the runtime.
[0103] The runtime statistics module is used to count the runtime of the current process every time the first duration T1 has elapsed, and update it in the process list.
[0104] The module for establishing a memory frequently used page statistics array is used to define the 15 processes with the longest runtime in the statistical process chain as frequently used processes after each second duration T2 before the second stage is triggered. A memory frequently used page statistics array is established for each frequently used process. The frequently used page statistics array records n memory pages. Each item in the array table is the score of each memory page, which is used to measure the importance of the memory page to the process. The higher the score, the more important it is.
[0105] The memory frequently used page statistics array adjustment module is used to scan the memory active page list and inactive page list corresponding to the kernel structure of all frequently used processes at preset time intervals. In the memory frequently used page statistics array, the value of the active page is increased by 3 after each scan, and the value of the inactive page is increased by 1.
[0106] The judgment module is used to determine whether the system has entered the emergency memory reclamation mechanism within a running cycle. If so, it enters the second stage; otherwise, it returns to the memory frequently used page statistics array creation module to run, re-select the top 15 processes with the longest running time, and re-create their corresponding memory frequently used page statistics arrays.
[0107] The second initialization module is used to determine and initialize the frequently used memory page statistics array, which will be used as initialization data.
[0108] The training module is used to input the statistical array of commonly used memory pages associated with each process into the LMRL reinforcement learning model and train it according to the preset training rules to obtain the training results. The LMRL reinforcement learning model is built based on the Q-learning algorithm model in reinforcement learning.
[0109] The emergency memory reclamation module is used to perform emergency memory reclamation based on the training results, and when the preset conditions are met, it exits the second stage and returns to the memory frequently used page statistics array creation module to run.
[0110] The modules described as separate components may or may not be physically separate. The components shown as modules may or may not be physical modules; that is, they may be located in one place or distributed across multiple network modules. Some or all of the modules can be selected to achieve the purpose of the solution in this embodiment, depending on actual needs.
[0111] Furthermore, the functional modules in the various embodiments of the present invention can be integrated into one processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The integrated modules described above can be implemented in hardware or as software functional modules.
[0112] If the integrated module is implemented as a software functional module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments of the present invention can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include: any entity or system capable of carrying the computer program code, recording media, USB flash drives, portable hard drives, magnetic disks, optical disks, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the content included in the computer-readable medium can be appropriately added or removed 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 electrical carrier signals and telecommunication signals.
[0113] Although the description of the invention has been quite detailed and particularly of several described embodiments, it is not intended to limit it to any of these details or embodiments or any particular embodiment, but should be considered as providing a broad possible interpretation of the claims by referring to the appended claims and taking into account the prior art, thereby effectively covering the intended scope of the invention. Furthermore, the invention has been described above with respect to embodiments foreseeable by the inventors in order to provide a useful description, and non-substantial modifications to the invention that have not yet been foreseen may still represent equivalent modifications.
[0114] The above description is merely a preferred embodiment of the present invention. The present invention is not limited to the above-described embodiments. Any embodiment that achieves the technical effects of the present invention using the same means should fall within the protection scope of the present invention. Within the protection scope of the present invention, various modifications and variations can be made to the technical solutions and / or implementation methods.
Claims
1. A Linux-based memory reclamation method to avoid page swapping thrashing, characterized in that, Including the following: In the current Linux system, it is divided into two stages, among which, Phase 1: Step 110: Preset the number and names of kernel processes, initialize them, and start a timer. The timer is used to record the runtime. Step 120: After each first time interval T1, calculate the runtime of the current process and update it in the process list; Step 130: Before the second stage is triggered, every second duration T2, the 15 processes with the longest runtime in the process chain are defined as frequently used processes. A memory page statistics array is created for each frequently used process. The frequently used page statistics array records n memory pages. Each item in the array table is the score of each memory page, which is used to measure the importance of the memory page to the process. The higher the score, the more important it is. Step 140: At a preset time interval, scan the memory active page list and inactive page list corresponding to the kernel structure of all frequently used processes. In the memory frequently used page statistics array, after each scan, the value corresponding to the active page is increased by 3, and the value of the inactive page is increased by 1. Step 150: Within one running cycle, determine whether the system has entered the emergency memory reclamation mechanism. If so, proceed to the second stage; otherwise, return to step 130, re-select the top 15 processes with the longest running time, and re-establish their corresponding memory frequently used page statistics array. Phase Two: Step 210: Determine and initialize the frequently used memory page statistics array to be used as initialization data. Step 220: Input the memory page statistics array associated with each process into the LMRL reinforcement learning model and train it according to the preset training rules to obtain the training results. The LMRL reinforcement learning model is constructed based on the Q-learning algorithm model in reinforcement learning. Step 230: Perform emergency memory reclamation based on the training results, and exit the second stage and return to step 130 when the preset conditions are met.
2. The memory reclamation method based on Linux to avoid page swapping thrashing as described in claim 1, characterized in that, Specifically, step 110 includes the following: Five kernel processes are preset as commonly used kernel processes. The names of these processes are recorded as strings in the main body of the program as necessary information for subsequent system initialization. The names and number of commonly used kernel processes can be changed and adjusted.
3. The memory reclamation method based on Linux to avoid page swapping thrashing as described in claim 1, characterized in that, Specifically, step 120 includes the following: Every 10 seconds, the timer will trigger a system interrupt. The interrupt handler will obtain the total running time of all processes through the kernel's process time interface. If the obtained process is already in the frequently used process list, the total running time of the corresponding process will be updated. If the process name is not in the frequently used process list, the process name will be added to the frequently used process list, and its running time will be updated.
4. A Linux-based memory reclamation method for avoiding page swapping thrashing, as described in claim 3, is characterized in that... Specifically, the commonly used process linked list follows these rules: The number of processes that can be held in the frequently used process list is limited to 50. Once the limit is reached, no new processes are allowed to be added. In addition, when the number of processes that can be held in the frequently used process list reaches 30, a new timer is started. The new timer will trigger a system interrupt every 5 seconds. The handler will obtain the runtime of the processes in the frequently used process list at the current moment, store it as a snapshot, and compare the current snapshot with the snapshot 5 seconds ago. The handler will select the 3 processes with the smallest runtime increase and delete these 3 processes from the frequently used process list.
5. A Linux-based memory reclamation method for avoiding page swapping thrashing, as described in claim 3, is characterized in that... Specifically, the following rules are followed when updating the runtime of the process: When a process is a kernel process, the kernel provides an interface to obtain the total time since system startup for this type of process. The time obtained for this type of process can be directly updated to the corresponding entry in the frequently used process list. When a process is a child process derived from the original kernel process, the startup time of this type of process is calculated from the process creation time. If this type of process stores the previous runtime q in the frequently used process list, and the current runtime is p, if the previous runtime q is less than the current runtime p, it is determined that the process was terminated and then restarted, and the runtime t is updated to t+p. If the previous runtime q is not less than the current runtime p, the previous runtime t is updated to the original value plus the difference between the two, that is, the runtime t is updated to t+(p-q).
6. A Linux-based memory reclamation method for avoiding page swapping thrashing, as described in claim 1, is characterized in that... Specifically, step 210 includes, Get the user-specified array of frequently used memory pages. If it exists, use the user-specified array of frequently used memory pages as the initialization data. If it does not exist, use the newly created array of frequently used memory pages as the initialization data.
7. A Linux-based memory reclamation method for avoiding page swapping thrashing, as described in claim 1, is characterized in that... Specifically, the preset training rules in step 220 include the following: The common memory pages associated with each process are used as the initial state S1, and the sum of the total scores of all memory pages is T. The optimization objective of reinforcement learning is to maximize T. In each round of optimization, the system will randomly adjust 10 items in the memory frequently used page statistics array. The adjustment method is to randomly add or subtract any integer in [5, 10]. The adjustment action is An. Each state Sn will execute action An to obtain a total score Tn. If the total score Tn corresponding to state Sn is greater than the total score Tn-1 corresponding to state Sn-1, it means that the optimization direction is correct. The total score Tn is taken as the highest score T and T is updated. If the total score Tn is less than the total score Tn-1, T is not updated. Then, action An+1 is executed in this direction to obtain state Sn+1. The way to change from state Sn-1 to state Sn is as follows: select the top 20 items with the highest single score in the memory frequently used page statistics array under the condition of state Sn-1, increase their weight in the frequently used process list corresponding to the process, so as to ensure that the memory frequently used pages corresponding to these 20 items will not be eliminated. After 500 milliseconds, the updated memory frequently used page statistics array is taken as state Sn. Adjust and optimize the direction with T as the target; The training phase consists of 1000 training loops. After multiple rounds of repeated training, the algorithm learns how to adjust the weights of different pages to minimize the possibility of page breaks. In other words, by adjusting the weights to make T higher, page thrashing is less likely to occur.
8. A Linux-based memory reclamation method for avoiding page swapping thrashing, as described in claim 7, is characterized in that... Specifically, the method to increase the weight in the frequently used process list corresponding to a process is to move the pages in the inactive list that are in the top 20 memory pages to the active list, leave the pages in the active list unprocessed, and move the pages that are neither in the active list nor in the inactive list to the inactive list.
9. A Linux-based memory reclamation method for avoiding page swapping thrashing, as described in claim 1, is characterized in that... After entering the emergency memory reclamation mechanism, this system will capture the page fault information of these processes by the operating system, obtain the specific page corresponding to the page fault, and reduce the score of the specific page in the memory frequently used page statistics array by 5 points.
10. A Linux-based memory reclamation device for avoiding page swapping thrashing, characterized in that, include: The first initialization module is used to preset the number and names of kernel processes, perform initialization, and start a timer. The timer is used to record the runtime. The runtime statistics module is used to count the runtime of the current process every time the first duration T1 has elapsed, and update it in the process list. The module for establishing a memory frequently used page statistics array is used to define the 15 processes with the longest runtime in the statistical process chain as frequently used processes after each second duration T2 before the second stage is triggered. A memory frequently used page statistics array is established for each frequently used process. The frequently used page statistics array records n memory pages. Each item in the array table is the score of each memory page, which is used to measure the importance of the memory page to the process. The higher the score, the more important it is. The memory frequently used page statistics array adjustment module is used to scan the memory active page list and inactive page list corresponding to the kernel structure of all frequently used processes at preset time intervals. In the memory frequently used page statistics array, the value of the active page is increased by 3 after each scan, and the value of the inactive page is increased by 1. The judgment module is used to determine whether the system has entered the emergency memory reclamation mechanism within a running cycle. If so, it enters the second stage; otherwise, it returns to the memory frequently used page statistics array creation module to run, re-select the top 15 processes with the longest running time, and re-create their corresponding memory frequently used page statistics arrays. The second initialization module is used to determine and initialize the frequently used memory page statistics array, which will be used as initialization data. The training module is used to input the statistical array of commonly used memory pages associated with each process into the LMRL reinforcement learning model and train it according to the preset training rules to obtain the training results. The LMRL reinforcement learning model is built based on the Q-learning algorithm model in reinforcement learning. The emergency memory reclamation module is used to perform emergency memory reclamation based on the training results, and when the preset conditions are met, it exits the second stage and returns to the memory frequently used page statistics array creation module to run.
Citation Information
Patent Citations
Shielding method of Linux page replacement mechanism
CN111782397A
Page type determination method, device, equipment and computer storage medium
CN111881398A
