Memory processing method and device, electronic equipment, storage medium and program product
By setting a preset threshold for the number of times and appropriately storing the target page, the problem of excessive CPU load caused by reverse mapping in the Linux operating system was solved, improving memory reclamation efficiency and system stability.
Patent Information
- Application Number
- CN202410924446.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-10
- Publication Date
- 2026-01-13
AI Technical Summary
In the Linux operating system, the reverse mapping mechanism leads to excessive CPU load on physical pages that are frequently referenced by processes, affecting memory reclamation efficiency and system performance.
By determining the number of times the physical page to be processed is referenced and the duration of the reverse mapping, and setting a preset threshold, the reverse mapping step in the original kernel reclamation logic of the system is skipped, and the target page is directly stored in a suitable storage space.
This reduces the consumption of system resources by repeated reverse mapping of target pages, improves memory reclamation efficiency and system stability, and reduces CPU load.
Smart Images

Figure CN121326540A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the technical field of computer, and particularly relates to a memory processing method and device, electronic equipment, storage medium and program product. BACKGROUND
[0002] In computer operating systems, especially Linux, the purpose of reverse mapping is to find all virtual memory areas (VMAs) and page tables mapped to a page through the physical address of the page, which is usually applied to memory recycling and page migration.
[0003] In the direction of memory recycling, the mechanism of reverse mapping brings certain performance cost. For a physical page with a large number of references by a process, it is difficult to be reverse mapped, and a large amount of CPU time is consumed and a large amount of CPU load is occupied, thereby introducing performance problems such as process scheduling and temperature rise of the operating system. SUMMARY
[0004] To overcome the problems in the related art, the present disclosure provides a memory processing method and device, electronic equipment, storage medium and program product. Through the method provided by the present disclosure, the excessive consumption of system resources caused by repeated reverse mapping of a target page can be reduced.
[0005] According to a first aspect of an embodiment of the present disclosure, a memory processing method is provided, comprising:
[0006] determining a first number of times that a to-be-processed physical page is currently referenced by a process;
[0007] determining a to-be-processed physical page with a first number of times greater than a preset number threshold as a target page; wherein the preset number threshold is determined based on a number of times that a physical page reverse-mapped is referenced by a process and a time length of reverse mapping the physical page;
[0008] obtaining current state information of the memory, and storing the target page to a corresponding storage space based on the current state information.
[0009] According to a second aspect of an embodiment of the present disclosure, a memory processing device is provided, comprising:
[0010] a first determining module configured to determine a first number of times that a to-be-processed physical page is currently referenced by a process;
[0011] The second determining module is configured to determine the to-be-processed physical page with the first number of references greater than a preset number threshold as a target page, wherein the preset number threshold is determined based on the number of references of the physical page after reverse mapping by the process and the time length of reverse mapping of the physical page.
[0012] The storage module is configured to acquire current state information of the memory, and store the target page to a corresponding storage space based on the current state information.
[0013] According to a third aspect of the embodiments of the present disclosure, an electronic device is provided, comprising a processor, a memory for storing computer programs or instructions, wherein the processor executes the computer programs or instructions to implement the steps of the method in the first aspect.
[0014] According to a fourth aspect of the embodiments of the present disclosure, a non-transitory computer readable storage medium is provided, which stores computer programs or instructions, and when the computer programs or instructions in the storage medium are executed by a processor, the steps of the method in the first aspect are implemented.
[0015] According to a fifth aspect of the embodiments of the present disclosure, a computer program product is provided, comprising computer programs or instructions, and when the computer programs or instructions are executed by a processor, the steps of the method in the first aspect are implemented.
[0016] The technical solutions provided by the embodiments of the present disclosure can include the following beneficial effects:
[0017] In the memory processing method proposed by the embodiments of the present disclosure, the to-be-processed physical page with the first number of references higher than the preset number threshold is taken as the target page, and the current state information of the memory is used to directly determine the storage space corresponding to the target page. In this way, first, the embodiments of the present disclosure scientifically and flexibly determine the preset number threshold by considering the number of references of the page after reverse mapping in the historical time and the time length of reverse mapping, which helps to effectively filter out the target page with too many references and too long reverse mapping processing time; second, the embodiments of the present disclosure select the storage space for the target page, which can skip the reverse mapping step of the target page in the original kernel recycling logic of the system, reducing the excessive consumption of system resources caused by repeated reverse mapping of the target page; third, the embodiments of the present disclosure select the storage space by combining the current memory information, which reduces memory congestion and improves system running efficiency and stability compared with directly placing the target page in the storage space not preferentially recycled.
[0018] It should be understood that the foregoing general description and the following detailed description are only exemplary and explanatory, and cannot limit the present disclosure. BRIEF DESCRIPTION OF DRAWINGS
[0019] The accompanying drawings, which are incorporated in and form a part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure.
[0020] Figure 1 This is a flowchart illustrating a memory reclamation method proposed by related technologies.
[0021] Figure 2 This is a flowchart illustrating a memory processing method according to an exemplary embodiment.
[0022] Figure 3 This is a flowchart illustrating a method for determining a preset number of thresholds according to an exemplary embodiment.
[0023] Figure 4 This is a flowchart illustrating a memory reclamation method according to an exemplary embodiment.
[0024] Figure 5 This is a block diagram of a memory processing apparatus according to an exemplary embodiment.
[0025] Figure 6 This is a structural block diagram of an electronic device according to an exemplary embodiment. Detailed Implementation
[0026] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numerals in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this disclosure. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this disclosure as detailed in the appended claims.
[0027] In the field of electronics, the importance of memory reclamation is self-evident. As the applications and functions of electronic devices continue to expand, users' demands for performance and responsiveness are also increasing. However, electronic devices have limited memory resources. If memory cannot be reclaimed in a timely and effective manner, it can lead to slow system operation, sluggish application response, or even crashes. Therefore, implementing effective memory reclamation measures is crucial for improving system performance and enhancing user experience. By optimizing the memory reclamation process and strategies, the utilization rate of mobile phone memory can be maximized, ensuring stable system operation and providing users with a smooth and efficient user experience.
[0028] When the system is Linux, the memory reclamation mechanism includes at least periodic reclamation and direct page reclamation. For periodic reclamation, when system memory is scarce and cannot be allocated during low-water conditions, the background memory reclamation kernel thread (kswapd) is awakened to trigger the system's original kernel reclamation logic to asynchronously reclaim pages in memory. For direct memory reclamation, when an application for more free memory arrives, the direct memory reclamation mechanism is triggered to reclaim memory from user threads, ensuring normal memory usage.
[0029] When system memory is insufficient, both direct memory reclamation and periodic reclamation methods call the `shrink_lruvec` and `shrink_slab` operations. `shrink_lruvec` is used to reclaim file pages and anonymous pages within the physical pages, while `shrink_slab` is used to reclaim physical pages corresponding to various drivers. Regardless of whether it's direct or periodic reclamation, both methods ultimately call the `shrink_node` method to perform aging-up reclamation of physical pages for each memory zone.
[0030] See Figure 1 , Figure 1 This is a flowchart illustrating a memory reclamation method proposed in related technologies; [The last part, "targeting," appears to be an unrelated fragment and is left untranslated.] Figure 1 As shown, Linux sets up two types of storage lists: the active list (active_list) and the inactive list (inactive_list). The active_list is used to store physical pages that have just been accessed, while the inactive_list is used to store physical pages that have not been accessed for a long time. This way, whether it is a periodic reclamation method or a direct memory reclamation method, memory pages can be reclaimed directly in the inactive_list.
[0031] The kernel thread kswapd calls the system kernel's original garbage collection logic through shrink_active_list (scanning the active list) and shrink_inactive_list (scanning the inactive list), periodically moving pages that meet the conditions in active_list to inactive_list and updating active_list.
[0032] The processes `shrink_active_list` and `shrink_inactive_list` involve scanning and reversing the mapping of physical pages in `active_list` and `inactive_list`, respectively. They primarily utilize `page_referenced` to determine if a physical page has been recently referenced and `try_to_unmap` to unmap it. In `shrink_active_list`, if `page_referenced` determines that a physical page has been recently referenced by a process, the scanned physical page is added to `active_list`, and `active_list` is updated. If it is determined that it has not been recently referenced by a process, it undergoes a second-chance processing in `shrink_inactive_list`. Subsequently, `page_referenced` is processed again on the physical page; if it has been recently referenced, the relevant page is added to the `active` list, and `active_list` is updated; if it has not been referenced, the relevant page is reclaimed.
[0033] However, during page reclamation, since the purpose of reverse mapping is to find all the VMAs and page table entries mapped to a certain physical page, when a physical page is referenced and shared by hundreds of processes, the VMAs and page tables need to be traversed hundreds of times when reverse mapping this physical page. If such physical pages are repeatedly reverse mapped, it will put a lot of pressure on the CPU load, which may lead to performance problems such as untimely memory reclamation, untimely process scheduling, or increased device temperature.
[0034] To overcome the problems existing in related technologies, this disclosure proposes a memory processing method. Figure 2 This is a flowchart illustrating a memory processing method according to an exemplary embodiment; as shown below. Figure 2 As shown, the method mainly includes the following steps:
[0035] S201, Determine the first number of times the physical page to be processed is currently referenced by the process;
[0036] S201, the physical pages to be processed that have a first count greater than the preset count threshold are identified as target pages;
[0037] The preset threshold is determined based on the number of times the physical page after reverse mapping is referenced by the process and the duration of reverse mapping of the physical page.
[0038] S203: Obtain the current state information of the memory and store the target page to the corresponding storage space based on the current state information.
[0039] Here, the memory processing method proposed in this embodiment is applied to electronic devices, specifically to the system memory reclamation scenario of electronic devices. The memory reclamation scenario is applicable to fields including but not limited to smart terminal devices, cloud technology, artificial intelligence, vehicle systems, aerospace and other technical fields.
[0040] In S201, when the kernel thread kswapd of the electronic device calls the original recycling logic of the system kernel, it scans and processes the physical pages to be processed in different locations in the system. During the scanning process, it obtains the first number of times the physical page to be processed is currently referenced by the process.
[0041] It should be noted that the first count of a physical page to be processed is the cumulative number of times it has been referenced by a process since its creation. The fact that a physical page to be processed is referenced by a process means that the physical page to be processed is shared by the processes. The processes that reside in other memory pages have functions that call the physical page to be processed, and therefore can respond to user access commands and call the aforementioned physical page to be processed.
[0042] In this embodiment of the disclosure, the method for obtaining the first number of times a physical page to be processed is to create an access counter for each physical page to be processed in the system; when the physical page to be processed is referenced by different processes, the access counter will be incremented; therefore, when the physical page to be processed is scanned, the current accumulated number in the access counter can be obtained as the aforementioned first number.
[0043] In S202, this embodiment of the present disclosure compares the first number of times the physical page to be processed is scanned in S201 with a preset number threshold, and determines whether the physical page to be processed is the target page based on the comparison result.
[0044] Here, the preset threshold number is obtained through further data analysis and calculation of the number of times the physical page after reverse mapping is referenced by the process within a historical time period of the electronic device and the duration of reverse mapping of the physical page. The aforementioned historical time period can be a testing period before the electronic device is manufactured, a historical time period specified by the testing specialist of the electronic device, or any historical time period before the electronic device performs the step of scanning the physical page to be processed in S201. This embodiment of the disclosure does not impose any limitations on this.
[0045] It should be noted that the further data analysis and calculation of the number of times the physical page after reverse mapping is referenced by the process and the duration of reverse mapping of the physical page in this embodiment are mainly to find a critical number. This critical number indicates that if the number of times the physical page is referenced by the process is less than the critical number, then the load pressure generated by the reverse mapping process of this physical page on the system is small; if the number of times the physical page is referenced by the process is greater than the critical number, then the load pressure generated by the reverse mapping process of this physical page on the system is large, that is, it belongs to a shared hot page. In this way, when the kernel thread kswapd calls the original recycling logic of the system kernel, the preset number threshold can be compared with the number of times the physical page to be processed is referenced during the scanning process of each physical page to be processed to determine whether the physical page to be processed belongs to a shared hot page, so as to perform storage protection for shared hot pages in the subsequent S103 and skip one reverse mapping step.
[0046] In an exemplary embodiment of this disclosure, the method for determining the preset number threshold can be implemented as follows: obtaining the number of times and duration of access corresponding to all scanned physical pages within the aforementioned historical time period; according to the weight of the duration corresponding to each physical page; wherein, duration and weight are positively correlated; for each physical page, the number of accesses is weighted using the aforementioned weights to obtain the weighted number of accesses for each physical page; the weighted number of accesses for each physical page is sorted, and the median of each sorted number is determined; the median is determined as the aforementioned preset number threshold; thus, since duration can reflect the actual system resource occupancy time of physical pages, weighting the number of accesses by the weight corresponding to duration can scientifically and accurately identify shared hot pages with high resource consumption; and by determining the median, using the threshold value exceeding half as the preset number threshold can reduce the memory pressure caused by frequently skipping the reverse mapping process, which prevents the effective implementation of subsequent recycling processes, thus ensuring system stability.
[0047] Of course, the preset threshold can also be determined by performing other statistical processing on the number of visits and duration of all physical pages scanned within the aforementioned historical time period, such as summation or averaging. This disclosure does not impose further restrictions on this.
[0048] In S203, the system can collect current memory status information. This current status information includes, but is not limited to, total memory, used memory, available memory, and response speed. The current status information can reflect the current memory pressure and whether there is sufficient space. Taking response speed as an example, if the memory response speed after receiving a CPU instruction is lower than the system's preset speed, it means that the memory is currently being occupied by multiple processes, and the memory pressure is high and the memory is under pressure. In this case, it is necessary to reclaim memory pages as soon as possible to release memory space. If the response speed is higher than the system's preset speed, it means that the current memory pressure is not high, and the urgency of memory reclamation is lower.
[0049] Thus, this embodiment of the disclosure can select a suitable storage space for the target page based on the current state information. Here, the storage space is the physical or virtual space of the system memory. In the original reclamation logic of the system kernel, different storage spaces have different capacities or reclamation priorities. If the current state information indicates that the system memory is under pressure, the target page can be placed in a space with a small storage capacity or a high reclamation priority to reduce memory usage and reclaim it as soon as possible to further free up memory space. If the current state information indicates that the system memory is not under pressure, the target page can be placed in a space with a large storage capacity or a low reclamation priority to protect it and reduce the load pressure caused by reclamation of the target page.
[0050] It should be noted that by directly determining the storage space of the target page through the current state information, the reverse mapping process in the original kernel reclamation logic can be skipped during this scan. It is not necessary to use the reverse mapping result to determine whether the target page has been recently referenced by a process to decide how to handle it. This can reduce the resource consumption and load pressure caused by traversing multiple VMAs and pages during reverse mapping reclamation, and further improve the system's operating efficiency.
[0051] In the memory processing method proposed in this disclosure, the physical page to be processed that has been referenced by the process more than a preset threshold number of times is taken as the target page, and the storage space corresponding to the target page is directly determined using the current memory state information. Thus, firstly, by considering the number of references and the duration of reverse mapping of pages over a historical period, this disclosure scientifically and flexibly determines the preset threshold number, which helps to effectively filter out target pages with excessive reference counts and long reverse mapping processing times. Secondly, by selecting a storage space for the target page, this disclosure can skip the one-time reverse mapping step of the target page in the original kernel reclamation logic, reducing the excessive consumption of system resources caused by repeated reverse mapping of the target page. Thirdly, by combining current memory information to select a storage space, this disclosure reduces memory congestion and improves system operating efficiency and stability compared to directly placing the target page in a storage space that is not prioritized for reclamation.
[0052] In some embodiments, the above memory processing method further includes:
[0053] Determine the second number of times each physical page after reverse mapping is referenced by the process within a preset time period, and the first statistical value corresponding to each second number; wherein, the first statistical value is used to indicate the distribution status of the second number;
[0054] Based on the mapping duration of each physical page in reverse mapping within a preset time period, a second statistical value corresponding to each second number is determined; wherein, the second statistical value is used to indicate the distribution status of the mapping duration, and the mapping duration is related to the system resources occupied by the reverse-mapped physical pages;
[0055] Based on the first and second statistical values, a preset threshold number of times is determined from each second number.
[0056] The aforementioned preset time period is the historical time period mentioned above in this disclosure; specifically, it can be a testing period before the electronic device is manufactured, a historical time period specified by the testing specialist of the electronic device, or any historical time period before the electronic device performs the step of scanning the physical page to be processed in S201.
[0057] Here, taking any historical time period before the electronic device performs the scanning of the physical page to be processed in step S201 above as an example, in this embodiment of the present disclosure, a monitoring module can be set in the electronic device; the monitoring module will obtain in real time the number of times each scanned physical page is referenced by the process, and the mapping time experienced by it in the reverse mapping process; wherein the mapping time is related to the system resources occupied by the reverse-mapped physical page, specifically, the size of the mapping time is positively correlated with the amount of system resources occupied by the physical page in the reverse mapping process.
[0058] In this embodiment, the monitoring module has a clock unit and a storage unit. The clock unit periodically sends a clock signal to the storage unit, and the storage unit then begins to regularly store the second number of times each scanned physical page is referenced by the process, thereby obtaining a first statistical value. It also regularly stores the mapping duration of each scanned physical page, thereby obtaining a second statistical value. Furthermore, the clock unit sends a clock signal to the storage unit at preset intervals, instructing the storage unit to clear the current storage. Here, the preset time period can be any time period from when the storage unit starts storing to when it clears the storage.
[0059] Here, the storage unit has a data storage structure; for example, the data storage structure can be an array, a sequence, a two-dimensional table, a doubly linked list, or a singly linked list, etc., and this disclosure does not limit this. Taking an array as an example, two or more array members are allocated in the array for each second number; the array members corresponding to each second number respectively count the number of times it has appeared, and the mapping duration of the physical page corresponding to the number of times it has appeared.
[0060] In this embodiment of the disclosure, each second number corresponds to a first statistical value and a second statistical value within a preset time period; the first statistical value reflects a distribution of the second number within the preset time period, and the second statistical value reflects a distribution of the duration of the physical page corresponding to the second number.
[0061] The determination of the first statistical value corresponding to each second number can be, in this case, counting the number of times the second number appears within a preset time period, determining the proportion of the second number appearing among all the second numbers counted within the preset time period, or determining the proportion of the value corresponding to the second number exceeding the value of all the second numbers counted within the preset time period, etc. This disclosure embodiment does not impose further restrictions on this, as long as the first statistical value can reflect the distribution of the second numbers within the preset time period.
[0062] Correspondingly, determining the second statistical value for each second number can be a statistical value of the mapping duration of the physical page corresponding to the second number within a preset time period. Specifically, it can be an average value, a cumulative value, or a value obtained through other mathematical processing. Alternatively, it can be the proportion of the total mapping duration of the physical page corresponding to the second number within the preset time period to the total mapping duration of all physical pages, or the proportion of the total mapping duration of the physical page corresponding to the second number exceeding the total mapping duration of all physical pages corresponding to the second number within the preset time period, etc. This embodiment of the present disclosure does not impose further limitations on this, as long as the first statistical value can reflect the distribution of the second number within the preset time period.
[0063] In this way, by combining the first and second statistical values to determine the preset number of accesses threshold, it is possible to scientifically and accurately find physical pages with a large number of accesses and high resource consumption among all physical pages that have been reverse-mapped within a preset time period. This also helps to effectively filter out all physical pages with a large number of accesses and high resource consumption as target pages in the original kernel recycling logic of the system, thereby effectively protecting them.
[0064] In some embodiments, determining the first statistical value corresponding to each second number includes:
[0065] The number of repetitions of each second number obtained within a preset time period is determined, and a first statistical value corresponding to the second number is determined based on the number of repetitions of the second number; wherein, the number of repetitions is used to indicate the number of identical second numbers;
[0066] Based on the mapping duration of each physical page within a preset time period, the second statistical value corresponding to each second number is determined, including:
[0067] In the case of a second repetition, the mapping duration of each physical page corresponding to at least two second numbers of reverse mapping repetition is determined, and a second statistical value corresponding to the second number is determined based on the determined at least two mapping durations.
[0068] If the second number is not repeated, the second statistical value corresponding to the second number is determined based on the mapping duration of the physical page corresponding to the second number in the reverse mapping.
[0069] For example, by counting the number of times the second occurrence has occurred in each array member corresponding to the second number, and the mapping duration of the physical page corresponding to the number of times the second occurrence has occurred, this embodiment of the present disclosure can, during the scanning of physical pages within a preset time period, accumulate the number of times the second occurrence has occurred repeatedly in at least two array members corresponding to each second number, and accumulate the mapping duration of the reverse mapping of the physical page corresponding to the second number.
[0070] It should be noted that the repetition count is used to indicate the number of times the second number is the same. If the second number appears only once within the preset time period, the repetition count is 0.
[0071] In some examples, embodiments of this disclosure may use the number of repetitions of each second number as the first statistical value of each second number, that is, use the count reflected by the array member of each second number that counts the number of repetitions of that second number as the first statistical value; correspondingly, use the timestamp reflected by the array member of each second number that counts the mapping duration of the physical page corresponding to that second number as the second statistical value; where timestamp is the duration accumulation value.
[0072] Here, if the second number is repeated, that is, count is greater than or equal to 1, then timestamp is the sum of the mapping durations of the physical pages corresponding to at least two second numbers; if the second number is not repeated, that is, count is greater than or equal to 0, then timestamp is the mapping duration of the physical page corresponding to that second number.
[0073] In other examples, embodiments of this disclosure may use the number of repetitions corresponding to each second number and the associated second number as the first statistical value of each second number. That is, at least two array members of each second number include an array member that counts the number of repetitions of the second number and the associated second number. The first statistical value is formed by the counts reflected by the at least two array members. Correspondingly, the timestamp reflected by the array member that counts the mapping duration of the physical pages corresponding to each second number and the associated second number is used to form the second statistical value. Here, the process of forming the first and second statistical values of each second number may be to average, weight, or perform other data processing on the corresponding counts, and to average, weight, or perform other data processing on the timestamps, etc., and embodiments of this disclosure do not limit this.
[0074] In this embodiment of the disclosure, after determining the first statistical value and the second statistical value corresponding to all the second numbers, the second number that meets the hot page requirement can be filtered out from all the second numbers, and the filtered second number can be used as the preset number threshold.
[0075] For example, the first and second statistical values of each second number can be weighted; wherein the weights of the first and second statistical values are each within a first range, and the weighted result after weighting can fall into a second or third range; here, the third range is larger than the second range. In this embodiment of the present disclosure, the second numbers corresponding to the weighted results falling into the third range can be filtered out for further processing to determine a preset number threshold. The further processing can be taking the smallest number among these filtered second numbers as the preset number threshold, or filtering out some smaller numbers and averaging them to determine the preset number threshold.
[0076] Here, the weights of the first and second statistical values can be preset or flexibly determined based on the current memory status. If the current memory status is tight, the weight of the second statistical value can be increased.
[0077] In this embodiment of the disclosure, by utilizing the number of repetitions of each second number and the mapping duration of the corresponding physical page, the distribution of those physical pages that are used by the process the same number of times within a preset time period, as well as the resource consumption, can be effectively reflected. Thus, through the first statistical value and the second statistical value, a preset number threshold can be effectively filtered out from each second number.
[0078] In some embodiments, the method of determining a preset threshold number from each second number based on a first statistical value and a second statistical value, as described above in the embodiments of this disclosure, can be implemented as follows:
[0079] The target number is defined as at least one second number where the first statistical value is less than the first threshold and the second statistical value is greater than the second threshold.
[0080] Based on the target number of attempts, a preset threshold number of attempts is determined.
[0081] Here, for each second number, the first statistical value and the second statistical value can be compared respectively. After comparison, the second number that satisfies the condition that the first statistical value is less than the first threshold and the second statistical value is greater than the second threshold can be determined as the target number.
[0082] The first threshold can be preset or determined based on the number of repetitions corresponding to the second number of times all physical pages collected in the previous one or more preset time periods; the second threshold can also be preset or determined based on the mapping duration of all physical pages collected in the previous one or more preset time periods.
[0083] In this embodiment of the disclosure, taking the first statistical value as representing the number of repetitions and the second statistical value as representing the duration as an example, the first threshold can be the average number of repetitions of the second number of all physical pages collected in the first n preset time periods. The purpose is to classify those second numbers whose repetitions exceed the average number to find shared pages with high reference counts. The second threshold can be the average duration of the mapping duration of all physical pages collected in the first n preset time periods. The purpose is to classify those second numbers whose mapping duration exceeds the average duration to find shared hot pages with high reference counts and high load.
[0084] For example, taking the first statistical value of each second number as the sum of the number of repetitions of that second number, and the second statistical value as the sum of the mapping duration of the physical page corresponding to that second number, at least one second number among all the second numbers exceeding the preset time period, whose first statistical value is less than the first threshold and whose second statistical value is greater than the second threshold, can be determined as the target number.
[0085] In this embodiment of the disclosure, when determining at least one target number of times, the smallest number among the at least one target number of times can be used as a preset number of times threshold. Alternatively, the average number of times after averaging these target number of times can be used as the preset number of times threshold. Of course, other processing can be performed on the at least one target number of times to determine the preset number of times threshold. This disclosure does not limit this.
[0086] In practice, for electronic devices such as mobile phones and computers with multiple functions and applications, the predetermined number of times threshold is usually between 20 and 40; for example, the predetermined number of times threshold can be 30.
[0087] The embodiments of this disclosure determine a preset number of times threshold based on at least one second number where the first statistical value is less than the first threshold and the second statistical value is greater than the second threshold. This can improve the effectiveness and scientific nature of determining the preset number of times threshold, thereby improving the rationality of determining the shared hot page in this disclosure.
[0088] In some embodiments, the above memory processing method further includes:
[0089] Different second numbers are identified as candidate frequencies, and the candidate frequencies are sorted in a preset order to obtain an initial sequence; wherein, each candidate frequency corresponds to at least one physical page;
[0090] The first statistical value corresponding to the second number is determined based on the number of repetitions of the second number, including:
[0091] The candidate count at the first position in the initial sequence is determined as the number to be processed, and the candidate count in the initial sequence that is greater than the number to be processed is determined as the target candidate count;
[0092] The first statistical value of the number of times to be processed is determined based on the sum of the number of repetitions corresponding to the number of times to be processed and the number of repetitions corresponding to the number of times to be processed in the target candidate.
[0093] Determine the second statistical value corresponding to the second number, including:
[0094] The second statistical value of the number of pending processes is determined based on the mapping time of each physical page corresponding to the number of pending processes in reverse mapping and the sum of the mapping time of each physical page corresponding to the number of pending target candidate processes in reverse mapping.
[0095] At least one second number whose first statistical value is less than a first threshold and whose second statistical value is greater than a second threshold is determined as the target number, including:
[0096] If the first statistical value of the number of times to be processed is less than the first threshold and the second statistical value is greater than the second threshold, the number of times to be processed is determined as the target number.
[0097] Here, in the embodiments of this disclosure, each second count with different values is taken as a candidate count in each second count within a preset time period. Where the second count is not repeated, the candidate count can correspond to one physical page; where the second count is repeated, the candidate count can correspond to multiple physical pages.
[0098] In this embodiment of the disclosure, after determining the number of each candidate, the candidate numbers are first arranged in a preset order to form an initial sequence. The number of times to be processed at the first position in the initial sequence is determined by an improved binary search algorithm. Then, by analyzing whether the first statistical value of the number of times to be processed is less than the first threshold and whether the second statistical value is greater than the second threshold, it is determined whether the number of times to be processed can be used as the target number mentioned above.
[0099] In this embodiment of the disclosure, determining the first statistical value of the number of times to be processed can be implemented as follows: determining the candidate number of times in the initial sequence that is greater than the number of times to be processed as the target candidate number; determining the sum of the number of repetitions corresponding to the number of times to be processed and the number of repetitions corresponding to all target candidate numbers as the first statistical value corresponding to the number of times to be processed; correspondingly, determining the second statistical value of the number of times to be processed can be implemented as follows: determining the sum of the mapping duration of each physical page corresponding to the number of times to be processed and the mapping duration of each physical page corresponding to all target candidate numbers as the second statistical value corresponding to the number of times to be processed.
[0100] Here, if the first statistical value of the number of times to be processed is less than the first threshold and the second statistical value is greater than the second threshold, the number of times to be processed is determined as the target number; if the first statistical value of the number of times to be processed is greater than the first threshold or the second statistical value is less than the second threshold, the first position is re-determined in the initial sequence through the improved binary search algorithm, that is, the next number of times to be processed is re-determined.
[0101] It should be noted that in the improved binary search algorithm provided in this embodiment, after determining a target number of times, the subsequent search process is stopped, and the target number of times is directly used as a preset number threshold.
[0102] Here, the embodiments of this disclosure improve the scientific nature and convenience of searching for the target number in the initial sequence formed after sorting by using an improved binary search algorithm, which helps to find a suitable number of target searches.
[0103] In some embodiments, the initial sequence is an ascending sequence; the memory processing method described above further includes:
[0104] If the first statistical value of the number of times to be processed is less than the first threshold and the second statistical value is less than the second threshold, the candidate number of times located at the second position in the initial sequence is determined as the next number of times to be processed; wherein, the second position is located before the first position;
[0105] If the first statistical value of the number of times to be processed is greater than the first threshold, and the second statistical value is greater than the second threshold, the candidate number of times located at the third position in the initial sequence is determined as the next number of times to be processed; wherein, the third position is after the first position;
[0106] If the first statistical value of the number of times to be processed is greater than the first threshold and the second statistical value is less than the second threshold, the candidate number at the fourth position in the initial sequence is determined as the next number to be processed; wherein, the fourth position is after the first position.
[0107] In this embodiment of the disclosure, the initial sequence can be an ascending sequence obtained by sorting each candidate frequency in ascending order. After determining the ascending sequence, it can be halved, and the first position of the resulting sequence is taken as the first position. The sum of the number of repetitions corresponding to each candidate frequency in the latter sequence is the first statistical value corresponding to the frequency to be processed at the first position. Similarly, the mapping duration of the physical page corresponding to each candidate frequency in the latter sequence is the second statistical value corresponding to the frequency to be processed at the first position.
[0108] If the first statistical value of the number of times to be processed at the first position is greater than the first threshold, or the second statistical value is less than the second threshold, the first position is re-determined in the initial sequence through the improved binary search algorithm, that is, the next number of times to be processed is re-determined.
[0109] It should be noted that determining the next number of processing times can be understood as taking the newly determined second position as the updated first position, and taking the candidate number of the current second position as the number of processing times in the first position at the next moment. The process of determining the next number of processing times for the newly determined third and fourth positions is similar in the following text, and will not be described in detail in this embodiment.
[0110] In this embodiment of the disclosure, the first statistical value of the number of times to be processed is greater than the first threshold, or the second statistical value is less than the second threshold, includes: a first case where the first statistical value of the number of times to be processed is less than the first threshold and the second statistical value is less than the second threshold; a second case where the first statistical value of the number of times to be processed is greater than the first threshold and the second statistical value is greater than the second threshold; and a third case where the first statistical value of the number of times to be processed is greater than the first threshold and the second statistical value is less than the second threshold.
[0111] In the first case, the candidate number of the second position in the initial sequence that is located before the first position is determined as the next number to be processed;
[0112] In some examples, determining the second position can be done using a binary search algorithm, which halves the sequence before the first position and uses the center position of the halved sequence as the second position.
[0113] In other examples, determining the second position can also involve determining the first displacement distance and moving the first position forward by the first displacement distance to obtain the second position; wherein, the first displacement distance is used to indicate the number of candidate times contained between the current first position and the second position to be determined; here, the first displacement distance can be a pre-set fixed value, or it can be the product value of the initial sequence length and a preset coefficient.
[0114] In the second case, the present disclosure embodiment determines the candidate number of the third position after the first position in the initial sequence as the next number to be processed;
[0115] In some examples, determining the third position can be done using a binary search algorithm, which halves the sequence after the first position and uses the center of the halved sequence as the third position.
[0116] In other examples, determining the third position can also involve determining a second displacement distance and shifting the first position backward by that second displacement distance to obtain the third position. Here, the second displacement distance indicates the number of candidate positions between the current first position and the soon-to-be-determined third position; the second displacement distance can be a pre-set fixed value or a product of the initial sequence length and a preset coefficient.
[0117] In the third case, the present disclosure embodiment determines the candidate number of the fourth position after the first position in the initial sequence as the next number to be processed;
[0118] In some examples, determining the fourth position can be done using a binary search algorithm, which halves the sequence after the first position and uses the center of the halved sequence as the fourth position.
[0119] In other examples, determining the fourth position can also involve determining a third displacement distance and shifting the first position backward by that third displacement distance to obtain the fourth position. Here, the third displacement distance indicates the number of candidate positions between the current first position and the soon-to-be-determined fourth position; this third displacement distance can be a pre-set fixed value or a product of the initial sequence length and a preset coefficient.
[0120] It should be noted that the third position can be the same as the fourth position, or the third position can precede the fourth position.
[0121] Here, the embodiments of this disclosure use an improved binary search algorithm to find the number of times to be processed in the initial sequence formed after sorting by adopting an appropriate partitioning method, thereby improving the scientific nature and convenience of finding the target number of times and helping to find a suitable target number of times.
[0122] In some embodiments, the above memory processing method further includes:
[0123] Set the first timestamp when the reverse mapping process for the physical page begins, and set the second timestamp when the reverse mapping process for the physical page ends.
[0124] The mapping duration is determined based on the time difference between the first and second timestamps.
[0125] Here, in the logic code for scanning physical pages in system memory, this embodiment sets a timestamp function and uses the timestamp function to calculate the mapping duration for the reverse mapping of the currently scanned physical pages.
[0126] For example, two timestamp functions are set before and after the reverse mapping function (Page_referenced) in the logic code. When the reverse mapping function is called, that is, when the reverse mapping process of the physical page begins, the first timestamp is started. The time corresponding to the first timestamp is the start time of the reverse mapping process of the currently scanned physical page. Correspondingly, when the reverse mapping process of the physical page ends, the second timestamp is started. The time corresponding to the second timestamp is the end time of the reverse mapping process of the currently scanned physical page.
[0127] Thus, the time difference between the first and second timestamps can represent the time difference between the start and end times of the reverse mapping process for the currently scanned physical page, which is also the total time consumed by the system resources for the reverse mapping of the currently scanned physical page.
[0128] The embodiments disclosed herein employ a method of setting a first timestamp and a second timestamp, which can simply and effectively obtain the mapping duration of the reverse-mapped physical pages, and further reflect the system resource usage of different reverse-mapped physical pages through the mapping duration of different physical pages.
[0129] In some embodiments, storing the target page to the corresponding storage space based on the current state information includes:
[0130] If the available memory is greater than the preset memory threshold, store the target page in the first storage space;
[0131] If the currently available memory is less than or equal to a preset memory threshold, the target page will be stored in the second storage space.
[0132] The second storage space has a higher priority for reclamation than the first storage space. The storage space includes a linked list or nodes in the linked list.
[0133] Here, the current memory status information includes the currently available memory; the currently available memory can be determined using memory reading tools configured in the system. Taking a Linux system as an example, memory reading tools can be command-line tools, such as using the command "si_mem_available" to view the system's available memory; memory reading tools can also be performance monitoring tools, such as network monitoring tools like Zabbix or Cacti, which provide real-time memory usage graphs, allowing you to view real-time memory reading data and historical trends; memory reading tools can also be system monitoring tools, specifically using tools like SystemMonitor to directly view memory usage.
[0134] In this embodiment of the disclosure, the preset memory threshold is set according to the usage of the electronic device and is a critical value used to reflect the degree of system memory pressure; when the current available memory is greater than the preset memory threshold, it indicates that the system memory is in a state of pressure; when the current available memory is less than or equal to the preset memory threshold, it indicates that the system memory is in a state of non-pressure.
[0135] The preset memory threshold can be pre-set or dynamically adjusted. For example, when the electronic device has been used for a long time, the system response rate and battery life are relatively weak, so a large preset memory threshold can be set; conversely, when the electronic device has been used for a short time, a small preset memory threshold can be set.
[0136] In this embodiment of the present disclosure, a first storage space and a second storage space can be preset. Furthermore, when it is determined that the current available memory is less than or equal to a preset memory threshold, a request can be made to allocate the second storage space; when it is determined that the current available memory is greater than the preset memory threshold, a request can be made to allocate the first storage space.
[0137] In this embodiment, the first storage space and the second storage space can be either the system kernel space or the virtual memory space. In the original garbage collection logic of the system kernel, whether using periodic garbage collection or direct memory reclamation methods, physical pages stored in the second storage space will be prioritized for reclamation. At this time, physical pages located in the first storage space will be protected, awaiting the next scan. In this embodiment, the first storage space can be a linked list or a node in the linked list with a low reclamation priority; the second storage space can also be a linked list or any node in the linked list.
[0138] Thus, this embodiment of the present disclosure can determine the storage space based on the currently available memory. This not only skips the original reverse mapping process in the system kernel's original reclamation logic, reducing the pressure on the CPU from repeated reverse mapping, but also allows the target page to be placed in a high-priority second memory space for rapid reclamation when available memory is scarce, thereby further freeing up memory space. Conversely, when available memory is not scarce, the target page is placed in a low-priority second storage space for protection, reducing the load pressure caused by reclamation. In other words, by considering the currently available memory, this embodiment of the present disclosure reduces performance and stability issues caused by memory congestion compared to directly placing the target page in a low-priority reclamation space for protection.
[0139] In some embodiments, storing the target page in the first storage space includes:
[0140] If the target page is located at the first node in the first linked list, any second node in the first linked list that is located before the first node is determined as the first storage space, and the target page is moved from the first node to any second node.
[0141] If the target page is located in the second linked list, the first linked list is determined as the first storage space, and the target page is moved from the second linked list to the first linked list.
[0142] The first linked list proposed in this disclosure can be an active list; the second linked list can be an inactive list. The recycling priority of the first linked list is lower than that of the second linked list; the recycling priority of the head node in the second linked list is higher than that of the tail node.
[0143] In this embodiment of the disclosure, each zone in the electronic device system can be configured with at least one active_list and at least one inactive_list. In the original recycling logic of the system kernel, when scanning the physical pages at each system zone, shrink_active_list can be called to scan the physical pages in the first linked list (active_list) of that zone from beginning to end, and shrink_inactive_list can be called to scan the physical pages in the second linked list (inactive_list) of that zone from beginning to end.
[0144] When the target page scanned by shrink_active_list is located at the head of the linked list and the current available memory is greater than the preset memory threshold, the position is not adjusted; when the target page is scanned at the first node and the current available memory is greater than the preset memory threshold, the second node of the first linked list is used as the first storage space for the target page; and the target page is moved to the second node for storage.
[0145] It should be noted that, where the first node is located after a preset node in the first linked list, the second node is a node before the preset node. The preset node can be a node at 1 / 3 of the first linked list or an intermediate node; where the first node is located before the preset node, the second node can be any node before the first node; for example, in this embodiment of the disclosure, the second node can be directly set as the head node of the first linked list.
[0146] When the target page scanned by shrink_inactive_list is located in the second linked list and the current available memory is greater than the preset memory threshold, the first linked list is used as the first storage space; or any node in the first linked list is used as the first storage space of the target page; and the target page is moved to the second linked list for storage.
[0147] For example, if the target page is located in the second linked list, the target page is moved from the second linked list to the head node of the first linked list for storage.
[0148] Thus, the embodiments of this disclosure can determine a suitable storage location for the target page when scanning the physical pages in the first and second linked lists, and perform migration protection on the target page, thereby reducing the probability of the target page being reclaimed and effectively improving the system operating efficiency.
[0149] In some embodiments, storing the target page in the second storage space includes:
[0150] If the target page is located in the first linked list, the second linked list is determined as the second storage space, and the target page is moved from the first linked list to the second linked list;
[0151] The above memory processing methods also include:
[0152] If the target page is located in the second linked list, perform reverse mapping on the target page.
[0153] In conjunction with the above embodiments of this disclosure, when the shrink_active_list is called to scan and find a target page in the first linked list, and the currently available memory is less than or equal to a preset memory threshold, the second linked list is used as the second storage space, or any node in the second linked list is used as the second storage space for the target page. There is no need to perform reverse mapping on the target page; the target page is directly migrated to the second linked list for storage.
[0154] For example, if the target page is located in the first linked list, the target page is moved from the first linked list to the head node of the second linked list for storage.
[0155] When `shrink_inactive_list` scans and finds a target page in the second linked list, and the currently available memory is less than or equal to a preset memory threshold, this embodiment of the disclosure can directly perform reverse mapping on the target page and determine whether the target page needs to be reclaimed based on the mapping result. If so, it is immediately reclaimed or moved to the end of the second linked list; if not, the target page can be placed in any node of the first linked list, such as the head node.
[0156] Thus, in this embodiment of the present disclosure, when the target page is scanned in the first linked list, the reverse mapping step can be skipped, and the target page can be directly placed in the second linked list with a high recycling priority, thereby increasing the likelihood that the target page will be recycled, thereby quickly releasing memory space and reducing system memory pressure.
[0157] In some embodiments, the above memory processing method further includes:
[0158] If the first number of attempts is less than or equal to a preset number of attempts threshold, the physical page to be processed is reverse-mapped to obtain the mapping result;
[0159] Based on the mapping results, determine whether to reclaim the physical pages to be processed.
[0160] In conjunction with the above disclosure, the logic code that scans physical pages in system memory includes a reverse mapping function (Page_referenced). When Page_referenced is called, it performs reverse mapping on the currently scanned physical pages.
[0161] It should be noted that whether you call shrink_active_list to scan the physical pages in active_list or call shrink_inactive_list to scan the physical pages in inactive_list, as long as the first count of the scanned physical pages is less than or equal to the preset count threshold, the Page_referenced step in the original recycling logic of the system kernel can be started to obtain the mapping result of the reverse mapping processing.
[0162] Here, the mapping result of the reverse mapping is used to reflect whether the physical page to be processed in the current scan has been recently referenced by a process; being recently referenced can be expressed as whether it has been referenced and accessed by other processes within a specified time period before the current time.
[0163] If the currently scanned physical page to be processed is in the `active_list`, and the mapping result indicates that the currently scanned physical page to be processed has recently been referenced by a process, then the physical page to be processed is moved forward, for example, moved to the head of the `active_list`. If the mapping result indicates that the currently scanned physical page to be processed has not recently been referenced by a process, then the physical page to be processed is moved to the `inactive_list`, for example, moved to the head of the `active_list`. It should be noted that after being moved to the `inactive_list`, the physical page will be subject to a second check in the subsequent `shrink_inactive_list` to determine whether it needs to be reclaimed.
[0164] If the currently scanned physical page to be processed is in the inactive_list, and if the mapping result reflects that the currently scanned physical page to be processed has recently been referenced by a process, then the physical page to be processed is moved to the active_list, especially to the head of the active_list. If the mapping result reflects that the currently scanned physical page to be processed has not recently been referenced by a process, then the physical page to be processed is recycled. The recycling process can be direct recycling or waiting for periodic recycling. Periodic recycling will recycle pages in the inactive_list from the tail to the head.
[0165] The embodiments disclosed herein can perform reverse mapping processing on physical pages to be processed that have a first count less than or equal to a preset count threshold, and then determine the processing method for the physical pages to be processed based on the mapping result. This can avoid disrupting the original reclamation logic of the system kernel and ensure the effectiveness of the memory reclamation mechanism.
[0166] The memory processing method proposed in this disclosure will be illustrated below with specific memory reclamation scenarios.
[0167] In Linux operating systems, shared pages frequently referenced by processes are less likely to be reclaimed than ordinary pages. This is because these pages are mapped and used by multiple processes, increasing the probability of them being referenced again and making them more difficult to reclaim. Secondly, during the reclamation process, the CPU load from the reverse mapping process—determining if a page is recently referenced—is higher than that from the perspective of ordinary pages. Therefore, from a memory reclamation perspective, the system spends a significant amount of CPU time performing reverse mapping on frequently referenced shared pages. If these pages are not ultimately reclaimed, it results in substantial resource waste and a negative impact on system memory. Furthermore, if protective measures are implemented by placing all these frequently referenced shared pages in the active list (active_list), memory congestion can easily occur, leading to a series of performance and stability issues.
[0168] To overcome the above problems, this disclosure proposes a memory processing method. See also Figure 3 and Figure 4 , Figure 3 This is a flowchart illustrating a method for determining a preset number of thresholds according to an exemplary embodiment; Figure 4 This is a flowchart illustrating a memory reclamation method according to an exemplary embodiment; the memory processing method proposed in this disclosure can be found in [reference needed]. Figure 3 and Figure 4 The steps described above.
[0169] Figure 3The steps of the method shown for determining the preset number threshold include:
[0170] S301, Enable monitoring module;
[0171] Here, the monitoring module will obtain in real time the second number of times each scanned physical page is referenced by the process, as well as the mapping time experienced by the reverse mapping process of the physical page. The mapping time reflects the CPU time occupied by the reverse mapping process.
[0172] S302, set the preset time period and start scanning the physical pages;
[0173] S303, determine the number of repetitions of each second number;
[0174] S304, determine the total mapping duration for each second number;
[0175] S305, fills the data storage structure of the monitoring module;
[0176] The monitoring module sets up a data storage structure, such as an array. This storage structure is configured using the following actual code:
[0177] struct p_mapcount_stat{;
[0178] unsigned long count;
[0179] ktime_t timestamp;
[0180] };
[0181] Here, count can represent the number of repetitions of each second number, and timestamp can represent the total mapping time of all physical pages corresponding to each second number during the reverse mapping process.
[0182] It should be noted that, in this embodiment of the present disclosure, the second number of times the scanned page is mapped by the process (mapcount) can be obtained before reverse mapping, and the count can be added to the data storage structure and incremented by one; and a first timestamp can be added; after the reverse mapping is completed, a second timestamp can be added; wherein, the reverse mapping deadline represented by the second timestamp minus the start time represented by the first timestamp can be used to obtain the mapping duration, that is, the CPU time occupied by the reverse mapping, and then the mapping duration is added to the data storage structure and the timestamp is accumulated.
[0183] S306, using each second number, determine the ascending sequence;
[0184] S307, determine whether the first statistical value is greater than the first threshold; S308, determine whether the second statistical value is less than the second threshold;
[0185] S309, Determine the preset number of times threshold.
[0186] An improved binary search algorithm is provided for S306 to S309.
[0187] In this embodiment, the different second numbers are first determined as candidate counts, and the candidate counts are sorted to obtain an ascending sequence. The ascending sequence is then halved to obtain the first candidate count of the next sequence, which is used as the count to be processed. The first and second statistical values of the count to be processed are obtained. The first statistical value is the sum of all counts in the next sequence, representing the count distribution in the interval from the count to the maximum candidate count (max_mapcount). Correspondingly, the second statistical value is the sum of all timestamps in the next sequence, representing the load distribution in the interval from the count to the maximum candidate count (max_mapcount).
[0188] Specifically, if the first statistical value of the number of times to be processed is less than the first threshold and the second statistical value is greater than the second threshold, then the number of times to be processed is used as the preset number threshold (page_mapcount threshold). That is, in the subsequent recycling mechanism, pages referenced by processes with a number higher than the page_mapcount threshold can be filtered as shared hot pages with high access and high resource consumption.
[0189] Here, if the first statistical value of the number of times to be processed is less than the first threshold and the second statistical value is less than the second threshold, then the current halved sequence is shifted to the left and expanded, and the first candidate number in the expanded sequence is taken as the number of times to be processed; if the first statistical value of the number of times to be processed is greater than the first threshold, then the current halved sequence is halved again, and the first candidate number in the halved sequence is taken as the number of times to be processed; based on the above logic, this continues until the page_mapcount threshold is found.
[0190] Figure 4 The steps of the memory processing method shown include:
[0191] S401, using the monitoring module, determines the preset number of thresholds;
[0192] Here, step S401 can be achieved through... Figure 3 The steps S301 to S309 shown are performed.
[0193] S402, scanning memory nodes;
[0194] Specifically, shrink_node is used to perform page scanning on each memory node in the system.
[0195] S403, scans each linked list of the memory node;
[0196] Specifically, shrink_list is used to perform page scanning on the linked list of each node in the system.
[0197] S404, Obtain the first linked list and scan the first linked list;
[0198] The first linked list is active_list. At this time, shrink_active_list is used to scan the active_list of each zone.
[0199] S405, determine whether the current available memory is greater than the preset memory threshold;
[0200] Here, in the shrink_active_list logic, it first checks whether the current system memory is higher than the preset memory threshold. If it is, it means that the current system status is good and executes S406. If not, it means that the current system memory is tight and executes S407.
[0201] S406, Determine whether the first count is greater than the preset count threshold;
[0202] Get the first number of times the currently scanned physical page to be processed is referenced by the process, and determine if the first number is greater than 1. Figure 3 The preset number of times threshold determined in the steps shown.
[0203] If S406 is true, it means that the current system status is good and the physical page to be scanned is a shared hot page. In this case, we tend to protect the shared page, so we place the page at the head of the active_list linked list, do not execute S408 below, and return to S404 to continue scanning the next page; if S406 is not true, it means that the physical page to be scanned is not a shared hot page, so we execute S408.
[0204] S407, Determine whether the first count is greater than the preset count threshold;
[0205] If S407 is true, it means that the current system memory is relatively tight and the physical page to be processed is a shared hot page. In this case, it tends to reclaim the page and, based on the kernel's native reclamation logic, executes S409 instead of S408, placing the page at the head of the inactive_list linked list and giving the page a second chance to determine whether to reclaim it. If S407 is not true, it means that the physical page to be processed is not a shared hot page, so S408 is executed.
[0206] It should be noted that, based on the comparison results of the current available memory and the preset memory threshold, as well as the comparison results of the first count and the preset number of counts threshold, this disclosure can determine whether the physical page to be processed is migrated to the active_list or the inactive_list; thereby reducing the CPU load caused by executing the reverse mapping in page_referenced once by not executing S408, and thus reducing the additional memory burden created for the system.
[0207] S408, Determine if the physical page to be processed has been recently referenced;
[0208] The process involves performing `page_referenced` on the physical page to be processed. Based on the result, it is determined whether the page has been recently referenced. If so, the process tends to protect the physical page to be processed, so the page is placed at the head of the `active_list` linked list, and S404 is returned to continue scanning the next page. If not, the process tends to reclaim the page, and based on the kernel's native reclamation logic, the page is placed at the head of the `inactive_list` linked list, giving the page a second chance to determine whether to reclaim it.
[0209] S409, Obtain the second linked list and scan the second linked list;
[0210] In this case, the second linked list is inactive_list. In this case, the present disclosure uses shrink_inactive_list to perform page scanning on the inactive_list of each zone.
[0211] S410, determine whether the current available memory is greater than the preset memory threshold;
[0212] Here, in the shrink_inactive_list logic, it first determines whether the current system memory is higher than the preset memory threshold. If it is, it means that the current system status is good. If it is, then step S411 is executed; if not, the current system memory is tight, and step S412 is executed.
[0213] S411, Determine whether the first count is greater than the preset count threshold;
[0214] Get the first number of times the currently scanned physical page in inactive_list has been referenced by the process, and check if the first number is greater than 1. Figure 3 The preset number of times threshold determined in the steps shown.
[0215] If S411 is true, it means that the current system status is good and the physical page to be scanned is a shared hot page. In this case, we tend to protect the shared page, so we place the page at the head of the active_list linked list, do not execute S412 below, and update the active_list in S404. If S411 is not true, it means that the physical page to be scanned is not a shared hot page, so we execute S412.
[0216] S412, Determine if the physical page to be processed has been recently referenced;
[0217] In this process, the physical page to be processed is processed by page_referenced. Based on the result, it is determined whether it has been recently referenced. If so, the physical page to be processed is protected. Therefore, the page is placed at the head of the active_list linked list and the active_list of S404 is updated. If not, the page is reclaimed and S413 is executed.
[0218] S413, Reclaim pending physical pages.
[0219] It should be noted that, Figure 4 The steps outlined here apply to both direct memory reclamation mechanisms and periodic reclamation mechanisms. Figure 4 The S401 shown is deployed in different processes / threads along with the subsequent steps.
[0220] The memory processing method proposed in this embodiment employs an improved binary search algorithm. Based on the first and second statistical values obtained by the monitoring module, it compares them with the first and second thresholds, respectively. From the different second times referenced by processes, it selects the most suitable preset threshold, providing a more scientific and effective threshold. This helps to effectively determine the shared hot page, i.e., the target page mentioned above, in practical applications.
[0221] Furthermore, in this embodiment of the present disclosure, after filtering out shared hot pages, the reverse mapping operation in the original kernel reclamation logic is skipped, reducing the burden on the system CPU caused by the reverse mapping operation traversing multiple VMAs and pages.
[0222] Meanwhile, based on the current memory state of the system, this embodiment determines whether to migrate the selected shared hot pages to the active list or the inactive list, and comprehensively judges whether to take a biased protection or recycling approach for pages that exceed a preset threshold number of times. Moreover, the method proposed in this embodiment is applicable to the native kernel recycling process, without making significant changes to the original kernel recycling logic, thus improving system stability.
[0223] Figure 5 This is a block diagram illustrating a memory processing apparatus according to an exemplary embodiment. Figure 5 As shown, a memory processing device 500 is provided, comprising:
[0224] The first determining module 501 is configured to determine the first number of times the physical page to be processed is currently referenced by the process.
[0225] The second determining module 502 is configured to determine the physical pages to be processed that have a first count greater than a preset number threshold as target pages; wherein, the preset number threshold is determined based on the number of times the physical page after reverse mapping is referenced by the process and the duration of reverse mapping of the physical page;
[0226] Storage module 503 is configured to obtain the current state information of memory and store the target page to the corresponding storage space based on the current state information.
[0227] In some embodiments, the memory processing device 500 further includes:
[0228] The third determining module is configured to determine the second number of times each physical page after reverse mapping is referenced by a process within a preset time period, and the first statistical value corresponding to each second number; wherein the first statistical value is used to indicate the distribution state of the second number; based on the mapping duration of each physical page after reverse mapping within the preset time period, the second statistical value corresponding to each second number is determined; wherein the second statistical value is used to indicate the distribution state of the mapping duration, and the mapping duration is associated with the system resources occupied by the reverse-mapped physical page; based on the first statistical value and the second statistical value, a preset number threshold is determined from each second number.
[0229] In some embodiments, the third determining module is further configured to determine the number of repetitions of each second number obtained within a preset time period, and determine a first statistical value corresponding to the second number based on the number of repetitions of the second number; wherein the number of repetitions is used to indicate the number of identical second numbers; in the case of a second number of repetitions, the mapping duration of each physical page corresponding to at least two repetitive second numbers is determined, and a second statistical value corresponding to the second number is determined based on the determined at least two mapping durations; in the case of a second number of non-repetitions, the second statistical value corresponding to the second number is determined based on the mapping duration of the physical page corresponding to the second number.
[0230] In some embodiments, the third determining module is further configured to determine at least one second number where the first statistical value is less than the first threshold and the second statistical value is greater than the second threshold as the target number; and determine a preset number threshold based on the target number.
[0231] In some embodiments, the third determining module is further configured to determine different second numbers as candidate counts, and sort each candidate count according to a preset order to obtain an initial sequence; wherein, one candidate count corresponds to at least one physical page; the candidate count located at the first position in the initial sequence is determined as the count to be processed, and the candidate counts in the initial sequence that are greater than the count to be processed are determined as target candidate counts; a first statistical value of the count to be processed is determined based on the sum of the number of repetitions corresponding to the count to be processed and the number of repetitions corresponding to the target candidate counts; a second statistical value of the count to be processed is determined based on the mapping duration of each physical page corresponding to the count to be processed in reverse mapping and the sum of the mapping duration of each physical page corresponding to the target candidate count in reverse mapping; and the count to be processed is determined as the target count when the first statistical value of the count to be processed is less than a first threshold and the second statistical value is greater than a second threshold.
[0232] In some embodiments, the initial sequence is an ascending sequence; the third determining module is further configured to: determine the candidate number at the second position in the initial sequence as the next number to be processed when the first statistical value of the number to be processed is less than the first threshold and the second statistical value is less than the second threshold; wherein the second position is before the first position; determine the candidate number at the third position in the initial sequence as the next number to be processed when the first statistical value of the number to be processed is greater than the first threshold and the second statistical value is greater than the second threshold; wherein the third position is after the first position; determine the candidate number at the fourth position in the initial sequence as the next number to be processed when the first statistical value of the number to be processed is greater than the first threshold and the second statistical value is less than the second threshold; wherein the fourth position is after the first position.
[0233] In some embodiments, the third determining module is further configured to set a first timestamp at the moment when the reverse mapping process of the physical page begins, and to set a second timestamp at the moment when the reverse mapping process of the physical page ends; and to determine the mapping duration based on the time difference between the first timestamp and the second timestamp.
[0234] In some embodiments, the storage module 503 is further configured to store the target page in a first storage space when the current available memory is greater than a preset memory threshold; and to store the target page in a second storage space when the current available memory is less than or equal to the preset memory threshold; wherein the recycling priority of the second storage space is higher than that of the first storage space, and the storage space includes a linked list or nodes in the linked list.
[0235] In some embodiments, the storage module 503 is further configured to, when the target page is located at the first node in the first linked list, determine any second node in the first linked list preceding the first node as the first storage space and migrate the target page from the first node to any second node; when the target page is located in the second linked list, determine the first linked list as the first storage space and migrate the target page from the second linked list to the first linked list.
[0236] In some embodiments, the storage module 503 is further configured to determine the second linked list as the second storage space when the target page is located in the first linked list, and to migrate the target page from the first linked list to the second linked list;
[0237] The memory processing device 500 also includes a mapping module configured to perform reverse mapping processing on the target page when the target page is located in a second linked list.
[0238] In some embodiments, the mapping module is further configured to perform reverse mapping on the physical page to be processed if the first number of times is less than or equal to a preset number of times threshold, and obtain a mapping result; based on the mapping result, determine whether to recycle the physical page to be processed.
[0239] Regarding the memory reclamation device in the above embodiments, the specific methods by which each module performs its operations have been described in detail in the embodiments related to the above memory reclamation method, and will not be elaborated here.
[0240] Figure 6 This is a structural block diagram of an electronic device according to an exemplary embodiment. For example, the electronic device 600 may be a mobile phone, computer, digital broadcasting terminal, messaging device, game console, tablet device, medical device, fitness equipment, personal digital assistant, etc., and the embodiments disclosed herein are not limited thereto.
[0241] Reference Figure 6 The electronic device 600 may include one or more of the following components: processing component 602, memory 604, power supply component 606, multimedia component 608, audio component 610, input / output (I / O) interface 612, sensor component 614, and communication component 616.
[0242] Processing component 602 typically controls the overall operation of electronic device 600, such as operations associated with at least one of display, telephone call, data communication, camera operation, and recording operation. Processing component 602 may include one or more processors 620 to execute instructions to perform all or part of the steps of the methods described above. Furthermore, processing component 602 may include one or more modules to facilitate interaction between processing component 602 and other components. For example, processing component 602 may include a multimedia module to facilitate interaction between multimedia component 608 and processing component 602.
[0243] Memory 604 is configured to store various types of data to support operation on electronic device 600. Examples of such data include at least one of the following: instructions for any application or method operating on electronic device 600, contact data, phonebook data, messages, pictures, and videos. Memory 604 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as Static Random Access Memory (SRAM), Electrically Erasable Programmable Read Only Memory (EEPROM), Erasable Programmable Read-Only Memory (EPROM), Programmable Read Only Memory (PROM), Read-Only Memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.
[0244] Power supply component 606 provides power to various components of electronic device 600. Power supply component 606 may include at least one of the following: a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to electronic device 600.
[0245] Multimedia component 608 includes a screen that provides an output interface between electronic device 600 and user. In some embodiments, the screen may include a Liquid Crystal Display (LCD) and a Touch Panel (TP). If the screen includes a Touch Panel, the screen may be implemented as a touchscreen to receive input signals from the user. The Touch Panel includes one or more touch sensors to sense touches, swipes, and gestures on the Touch Panel. The touch sensors may sense not only the boundaries of touch or swipe actions but also the duration and pressure associated with the touch or swipe operation. In some embodiments, multimedia component 608 includes a front-facing camera and / or a rear-facing camera. When electronic device 600 is in an operating mode, such as a shooting mode or video mode, the front-facing camera and / or rear-facing camera may receive external multimedia data. Each front-facing camera and rear-facing camera may be a fixed optical lens system or have focal length and optical zoom capabilities.
[0246] Audio component 610 is configured to output and / or input audio signals. For example, audio component 610 includes a microphone (MIC) configured to receive external audio signals when electronic device 600 is in an operating mode, such as call mode, recording mode, and voice recognition mode. The received audio signals may be further stored in memory 604 or transmitted via communication component 616. In some embodiments, audio component 610 also includes a speaker for outputting audio signals.
[0247] I / O interface 612 provides an interface between processing component 602 and peripheral interface modules, such as keyboards, click wheels, and buttons. These buttons may include, but are not limited to, home buttons, volume buttons, power buttons, and lock buttons.
[0248] Sensor assembly 614 includes one or more sensors for providing state assessments of various aspects of electronic device 600. For example, sensor assembly 614 may detect the on / off state of electronic device 600, the relative positioning of components such as the display and keypad of electronic device 600, changes in position of electronic device 600 or one of its components, the presence or absence of user contact with electronic device 600, orientation or acceleration / deceleration of electronic device 600, and temperature changes of electronic device 600. Sensor assembly 614 may include a proximity sensor configured to detect the presence of nearby objects without any physical contact. Sensor assembly 614 may also include an optical sensor, such as a complementary metal-oxide-semiconductor (CMOS) or charge-coupled device (CCD) image sensor, for use in imaging applications. In some embodiments, sensor assembly 614 may also include, but is not limited to, at least one of the following: an accelerometer, a gyroscope, a magnetometer, a pressure sensor, and a temperature sensor.
[0249] Communication component 616 is configured to facilitate wired or wireless communication between electronic device 600 and other devices. Electronic device 600 can access wireless networks based on communication standards, such as Wi-Fi, 4G, 5G, or combinations thereof. In one exemplary embodiment, communication component 616 receives broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel. In one exemplary embodiment, communication component 616 also includes a Near Field Communication (NFC) module to facilitate short-range communication. For example, the NFC module may be implemented based on Radio Frequency Identification (RFID), Infrared Data Association (IrDA), Ultra Wide Band (UWB), Bluetooth (BT), and other technologies.
[0250] In an exemplary embodiment, the electronic device 600 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field-programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components.
[0251] In an exemplary embodiment, a non-transitory computer-readable storage medium including instructions is also provided, such as a memory 604 including executable instructions or a computer program, which can be executed by a processor 620 of an electronic device 600 to perform the above-described method. For example, the non-transitory computer-readable storage medium may be a ROM, random access memory (RAM), a compact disc read-only memory (CD-ROM), magnetic tape, floppy disk, and optical data storage device, etc.
[0252] A non-transitory computer-readable storage medium, when the instructions in the storage medium are executed by the processor of a mobile terminal, enables the mobile terminal to perform any of the memory processing methods described in the embodiments of this disclosure.
[0253] This disclosure provides a computer program product comprising a computer program or executable instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer program or executable instructions from the computer-readable storage medium and executes the computer program or executable instructions, causing the computer device to perform any of the memory processing methods described above in this disclosure.
[0254] Other embodiments of this disclosure will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This disclosure is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the claims.
[0255] It should be understood that this disclosure is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this disclosure is limited only by the appended claims.
Claims
1. A memory processing method, characterized in that, include: Determine the first number of times the physical page to be processed is currently referenced by the process; Physical pages to be processed that have exceeded the first number of times are identified as target pages; wherein, the preset number of times is determined based on the number of times the physical page after reverse mapping is referenced by the process and the duration of reverse mapping of the physical page; Obtain the current state information of the memory, and store the target page to the corresponding storage space based on the current state information.
2. The method according to claim 1, characterized in that, The method further includes: Determine the second number of times each physical page after reverse mapping is referenced by the process within a preset time period, and the first statistical value corresponding to each second number of references; wherein, the first statistical value is used to indicate the distribution state of the second number of references. Based on the mapping duration of each physical page during the preset time period, a second statistical value corresponding to each second number is determined; wherein, the second statistical value is used to indicate the distribution state of the mapping duration, and the mapping duration is associated with the system resources occupied by the reverse mapping of the physical page; Based on the first statistical value and the second statistical value, the preset number threshold is determined from each of the second number of times.
3. The method according to claim 2, characterized in that, Determining the first statistical value corresponding to each of the second times includes: The number of repetitions of each of the second counts obtained within the preset time period is determined, and the first statistical value corresponding to the second count is determined based on the number of repetitions of the second count; wherein, the number of repetitions is used to indicate the number of identical second counts; The step of determining the second statistical value corresponding to each of the second counts based on the mapping duration of each physical page within the preset time period includes: In the case of the second number of repetitions, the mapping duration of each physical page corresponding to at least two second number of repetitions of reverse mapping is determined, and the second statistical value corresponding to the second number of repetitions is determined based on the determined at least two mapping durations. If the second number of times is not repeated, the second statistical value corresponding to the second number of times is determined based on the mapping duration of the physical page corresponding to the second number of times through reverse mapping.
4. The method according to claim 3, characterized in that, The step of determining the preset number threshold from each of the second number of times based on the first statistical value and the second statistical value includes: The first statistical value being less than the first threshold and the second statistical value being greater than the second threshold, at least one second number, is determined as the target number; Based on the target number of times, the preset number threshold is determined.
5. The method according to claim 4, characterized in that, The method further includes: Different second counts are determined as candidate counts, and each candidate count is sorted in a preset order to obtain an initial sequence; wherein, one candidate count corresponds to at least one physical page; Determining the first statistical value corresponding to the second number of repetitions based on the number of repetitions of the second number of times includes: The candidate count located at the first position in the initial sequence is determined as the number of times to be processed, and the candidate count in the initial sequence that is greater than the number of times to be processed is determined as the target candidate count; The first statistical value of the number of times to be processed is determined based on the sum of the number of repetitions corresponding to the number of times to be processed and the number of repetitions corresponding to the number of times to be processed in the target candidate. Determining the second statistical value corresponding to the second number includes: The second statistical value of the number of pending processes is determined based on the mapping time of each physical page corresponding to the number of pending processes in reverse mapping, and the sum of the mapping time of each physical page corresponding to the number of target candidate processes in reverse mapping. The step of determining at least one second number where the first statistical value is less than the first threshold and the second statistical value is greater than the second threshold as the target number includes: If the first statistical value of the number of times to be processed is less than the first threshold and the second statistical value is greater than the second threshold, the number of times to be processed is determined as the target number.
6. The method according to claim 5, characterized in that, The initial sequence is an ascending sequence; the method further includes: If the first statistical value of the number of times to be processed is less than the first threshold and the second statistical value is less than the second threshold, the candidate number located at the second position in the initial sequence is determined as the next number of times to be processed; wherein, the second position is located before the first position; If the first statistical value of the number of times to be processed is greater than the first threshold, and the second statistical value is greater than the second threshold, the candidate number located at the third position in the initial sequence is determined as the next number of times to be processed; wherein, the third position is located after the first position; If the first statistical value of the number of times to be processed is greater than the first threshold and the second statistical value is less than the second threshold, the candidate number located at the fourth position in the initial sequence is determined as the next number of times to be processed; wherein the fourth position is located after the first position.
7. The method according to any one of claims 2 to 6, characterized in that, The method further includes: A first timestamp is set when the reverse mapping process for the physical page begins, and a second timestamp is set when the reverse mapping process for the physical page ends. The mapping duration is determined based on the time difference between the first timestamp and the second timestamp.
8. The method according to any one of claims 1 to 6, characterized in that, The step of storing the target page to the corresponding storage space based on the current state information includes: If the available memory is greater than a preset memory threshold, the target page is stored in the first storage space; If the currently available memory is less than or equal to the preset memory threshold, the target page is stored in the second storage space; The second storage space has a higher priority for reclamation than the first storage space, and the storage space includes a linked list or nodes in a linked list.
9. The method according to claim 8, characterized in that, The step of storing the target page in the first storage space includes: If the target page is located at the first node in the first linked list, any second node in the first linked list that is located before the first node is determined as the first storage space, and the target page is migrated from the first node to the any second node. If the target page is located in the second linked list, the first linked list is determined as the first storage space, and the target page is migrated from the second linked list to the first linked list.
10. The method according to claim 8, characterized in that, The step of storing the target page to the second storage space includes: If the target page is located in the first linked list, the second linked list is determined as the second storage space, and the target page is moved from the first linked list to the second linked list; The method further includes: If the target page is located in the second linked list, the target page is reverse-mapped.
11. The method according to any one of claims 1 to 6, characterized in that, The method further includes: If the first number of times is less than or equal to the preset number of times threshold, the physical page to be processed is reverse mapped to obtain the mapping result; Based on the mapping results, it is determined whether to recycle the physical pages to be processed.
12. A memory processing device, characterized in that, include: The first determining module is configured to determine the first number of times the physical page to be processed is currently referenced by the process. The second determining module is configured to determine the physical pages to be processed that have a first number of times greater than a preset number threshold as target pages; wherein the preset number threshold is determined based on the number of times the physical page after reverse mapping is referenced by the process and the duration of reverse mapping of the physical page; The storage module is configured to acquire the current state information of the memory and store the target page to the corresponding storage space based on the current state information.
13. An electronic device, characterized in that, include: processor; Memory used to store computer programs or instructions; The processor executes the computer program or instructions to implement the steps of the method according to any one of claims 1 to 11.
14. A non-transitory computer-readable storage medium storing a computer program or instructions, characterized in that, When the computer program or instructions in the storage medium are executed by a processor, the steps of the method according to any one of claims 1 to 10 are implemented.
15. A computer program product, comprising a computer program or instructions, characterized in that, When the computer program or instructions are executed by a processor, they implement the steps of the method according to any one of claims 1 to 11.