Linux system memory recovery control method and system based on eBPF
By adopting an eBPF-based method in the Linux operating system, the problem of version differences in the memory recycling control mechanism, the inability to independently recycle file pages and anonymous pages, and the inability to control the memory recycling scanning strategy is solved, and the cross-version unified memory recycling control and independent recycling of specific page types is realized, improving the system memory usage efficiency and stability.
Patent Information
- Application Number
- CN202411545453.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-01
- Publication Date
- 2025-03-04
- Estimated Expiration
- 2044-11-01
AI Technical Summary
In the existing Linux operating systems, there are problems with version differences in the memory recycling control mechanism, the inability to independently recycle file pages and anonymous pages, and the scanning strategy for memory recycling cannot be controlled.
Using an eBPF-based method, the recycling control parameters input by the user are obtained through the BPF user-state program and passed them to the BPF kernel-state program, triggering the kernel memory recovery program to realize memory recovery control of the memory control group. This method supports the independent recycling of file pages and anonymous pages based on the application's memory usage characteristics, and controls the scanning strategy of memory recycling based on the memory hot and cold characteristics.
It realizes unified memory recycling control across versions, supports independent recycling control for file pages and anonymous pages, and optimizes memory recycling strategies based on the application's memory characteristics, thereby improving system memory usage efficiency and stability.
Smart Images

Figure CN119046021B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of memory management of Linux systems, and in particular to a memory recovery control method and system for Linux systems based on eBPF. Background Art
[0002] For the Linux operating system, memory is a limited key system resource. All applications running on it need to apply for and use memory. In the design of the Linux operating system, it tends to satisfy the memory usage requests of processes as much as possible. In order to balance the memory requirements of various applications in the system, Linux uses a memory recycling mechanism to use memory efficiently. Memory recycling can prevent system memory exhaustion and system performance degradation; improve the efficiency of system cache usage by recycling various caches; and allow the system to run more applications by releasing unused memory. It can be seen that memory recycling is very critical to the operating system. eBPF (Extended Berkeley Packet Filter) is a virtual machine technology running in the Linux kernel. It allows users to write and execute custom programs in the kernel. At the same time, eBPF programs will be verified and restricted when loaded and run to ensure that they will not undermine the stability and security of the system. It has become a new way to interact with the kernel. The Linux operating system provides a control group (cgroup) mechanism to group applications and provides an interface through the cgroupfs virtual file system to limit the resources of applications. The memory control group (memory cgroup) is a cgroup restriction mechanism for memory resources. However, the current mechanism has the following problems: (1) During the development of cgroup, there are two versions, v1 and v2, and the memory recycling control interfaces of the two versions are not completely unified. (2) In the current memory cgroup, it is not possible to independently recycle file pages and anonymous pages. (3) In the current memory cgroup, it is not possible to control the scanning strategy for memory recycling. Summary of the invention
[0003] Technical problem to be solved by the present invention: In view of the above-mentioned problems in the prior art, a Linux system memory recovery control method and system based on eBPF are provided. The present invention aims to utilize the powerful interaction capability of eBPF and the kernel to realize memory recovery control of the Linux operating system, so as to avoid the differences between different versions of the control group, provide a general memory control framework and intuitive and easy-to-understand control parameters for recovery control, support independent recovery control of file pages and anonymous pages according to the memory usage characteristics of the application, and support control of the scanning strategy for memory recovery according to the hot and cold memory characteristics of the application.
[0004] In order to solve the above technical problems, the technical solution adopted by the present invention is:
[0005] A Linux system memory recycling control method based on eBPF comprises the following steps:
[0006] S1, obtain the recycling control parameters of different controls implemented by the user by combining different inputs through the BPF user state program, and pass the recycling control parameters to the BPF kernel state program;
[0007] S2, a BPF kernel-mode program loaded and run by the BPF module of the Linux operating system parses the recycling control parameters and triggers the kernel memory recycling program;
[0008] S3, implements memory recovery control of the memory control group through the triggered kernel memory recovery program or migrates the user process to the newly created memory control group to implement memory recovery control of the user process, wherein the memory recovery control includes independent recovery control of file pages and anonymous pages according to the memory usage characteristics of the application, and controls the scanning strategy during memory recovery according to the hot and cold memory characteristics of the application.
[0009] Optionally, the execution steps of the BPF user mode program in step S1 include:
[0010] S1.1, parse the user input and obtain the recycling control parameters. If the recycling control parameters are successfully obtained, jump to step S1.2, otherwise end and exit;
[0011] S1.2, construct the control parameter structure required by the BPF kernel state program according to the obtained recycling control parameters;
[0012] S1.3, open the BPF kernel state program, save the program structure pointer, if the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.4;
[0013] S1.4, load the BPF kernel-mode program into the kernel. If the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.5;
[0014] S1.5, obtain the file descriptor of the BPF kernel program. If the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.6;
[0015] S1.6, attach the BPF kernel-mode program to the kernel memory recovery program entry. If the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.7;
[0016] S1.7, construct the running context parameters of the BPF kernel state program in combination with the control parameter structure;
[0017] S1.8 triggers the execution of the BPF kernel-mode program through the obtained file descriptor and running context parameters.
[0018] Optionally, the recycling control parameters in step S1.1 include: the absolute path cg of the memory control group, the expected memory recycling amount bytes, the recycling balance strategy balance, the file page recycling ratio file_ratio, and the scanning control options may_swap and may_unmap, wherein the recycling balance strategy balance and the file page recycling ratio file_ratio are set according to the memory usage characteristics of the application, and the scanning control options may_swap and may_unmap are set according to the memory hot and cold characteristics of the application.
[0019] Optionally, the execution steps of the BPF kernel state program in step S2 include:
[0020] S2.1, specify the running trigger point of the BPF kernel program;
[0021] S2.2, parsing the BPF kernel-mode program running context parameters passed by the BPF user-mode program;
[0022] S2.3, triggers the kernel memory recycling program.
[0023] Optionally, in step S3, when memory recovery control of the memory control group is implemented through the triggered kernel memory recovery program or the user process is migrated to the newly created memory control group to implement memory recovery control of the user process, the scan control structure scan_control in the kernel of the Linux system contains three fields: a memory recovery indication field bpf_reclaim, used to indicate whether the memory recovery is initiated by eBPF; a balance strategy field bpf_balance, used to indicate the recovery balance strategy, and the balance strategy includes only recovering file pages SCAN_FILE, only recovering anonymous pages SCAN_ANON, equal proportion recovery SCAN_EQUAL and specified proportion recovery SCAN_FRAC; a recovery ratio field bpf_fileratio, used to indicate the recovery ratio of file pages, which is only valid when the recovery balance strategy is proportional recovery SCAN_FRAC.
[0024] Optionally, the kernel memory recovery program in step S3 is a kernel memory recovery program based on a BPF kernel function kfuncs. The kernel memory recovery program based on the BPF kernel function kfuncs is compatible with the memory recovery mechanism based on the memory control group in processing logic and can be directly called by the BPF kernel state program. The execution steps of the kernel memory recovery program based on the BPF kernel function kfuncs include:
[0025] S3.1, use BPF kernel function kfuncs to define kernel memory recovery function;
[0026] S3.2, annotate the kernel memory recovery function so that it can pass the verification of the BPF module;
[0027] S3.3, register the kernel memory recovery function so that the BPF module can recognize the function;
[0028] S3.4, parse the function parameters passed by the BPF kernel program, mainly including the target control group cgroup, the expected amount of reclaimed memory nr_to_reclaim, the scan control option reclaim_options, the reclaim balance strategy balance, and the file page reclaim ratio file_ratio. The above parameters are saved in the scan control structure scan_control;
[0029] S3.5, obtaining the target control group number cgroup id of the target control group cgroup to be recycled;
[0030] S3.6, trigger kernel memory recycling for the target control group cgroup;
[0031] S3.7, determine whether the reclaimed memory reaches the expected reclaimed memory amount nr_to_reclaim, if so, end and exit; otherwise return to step S3.6 to continue memory reclaim.
[0032] Optionally, the kernel memory recovery program in step S3 also includes actively controlling the input parameters constructed by the BPF user mode program according to the following steps by adding active control logic when calling the scan number acquisition function get_scan_count() to obtain the scan number of file pages and anonymous pages:
[0033] S3.1a, obtain the input parameters constructed by the BPF user-mode program, including the scan control structure scan_control, the current system scan balance strategy balance_old, the current system anonymous page scan ratio ap_old, and the current system file page scan ratio fp_old;
[0034] S3.2a, determine whether the value of the memory reclaim indication field bpf_reclaim is 1. If the reclaim is triggered by the BPF user mode program, the value of the memory reclaim indication field bpf_reclaim will be set to 1, which will cause the value of the memory reclaim indication field bpf_reclaim to be 1, then jump to step S3.3a; otherwise, end and exit;
[0035] S3.3a, setting the scan balance strategy balance_new after control adjustment to the value of the balance strategy field bpf_balance in the scan control structure scan_control;
[0036] S3.4a, determine whether the value of the balance strategy field bpf_balance in the scan control structure scan_control is the specified proportion recovery SCAN_FRAC, if yes, jump to step S3.5a; otherwise, end and exit;
[0037] S3.5a, calculate the denominator for proportional recovery according to the following formula:
[0038] denominator = ap_old + fp_old,
[0039] In the above formula, ap_old is the current system anonymous page scan ratio in the input parameter, and fp_old is the current system file page scan ratio in the input parameter;
[0040] S3.6a, read the recovery ratio field bpf_fileratio in the scan control structure scan_control, and calculate the file page scan ratio fp_new after control adjustment according to the following formula:
[0041] fp_new = sc->bpf_fileratio * denominator / 100,
[0042] In the above formula, sc->bpf_fileratio is the recovery ratio field bpf_fileratio in the scan control structure scan_control, and denominator is the denominator end of the proportional recovery;
[0043] S3.7a, calculate the anonymous page scan ratio ap_new after control adjustment according to the following formula:
[0044] ap_new = denominator - fp_new,
[0045] In the above formula, denominator is the denominator end of the proportional recovery;
[0046] S3.8a, outputting the controlled and adjusted scanning balance strategy balance_new, the controlled and adjusted anonymous page scanning ratio ap_new, and the controlled and adjusted file page scanning ratio fp_new to respectively control the file pages and anonymous pages in the memory.
[0047] In addition, the present invention also provides a Linux system memory recovery control system based on eBPF, including a microprocessor and a memory connected to each other, and the microprocessor is programmed or configured to execute the Linux system memory recovery control method based on eBPF.
[0048] In addition, the present invention also provides a computer-readable storage medium, which stores a computer program or instruction, and the computer program or instruction is programmed or configured to execute the eBPF-based Linux system memory recovery control method through a processor.
[0049] In addition, the present invention also provides a computer program product, including a computer program or instructions, which are programmed or configured to execute the eBPF-based Linux system memory recycling control method through a processor.
[0050] Compared with the prior art, the present invention mainly has the following advantages: the method of the present invention comprises obtaining recycling control parameters for realizing different controls by combining different inputs by the user through a BPF user state program and passing the parameters to a BPF kernel state program; parsing the recycling control parameters through the BPF kernel state program and triggering the kernel memory recycling program; and executing memory recycling control according to the recycling control parameters through the triggered kernel memory recycling program. The present invention utilizes the powerful interaction capability of eBPF and the kernel to realize memory recycling control of the Linux operating system to avoid the differences between different versions of the control group, provides a general memory control framework and intuitive and easy-to-understand control parameters for recycling control, supports independent recycling control of file pages and anonymous pages according to the memory usage characteristics of the application, and supports controlling the scanning strategy of memory recycling according to the hot and cold memory characteristics of the application. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] Figure 1 Schematic diagram of the basic flow of the method of the embodiment of the present invention.
[0052] Figure 2 Schematic diagram of program interaction relationship in an embodiment of the present invention.
[0053] Figure 3 The figure is a schematic diagram of the BPF user mode program processing flow in an embodiment of the present invention.
[0054] Figure 4 Schematic diagram of the BPF kernel program processing flow in an embodiment of the present invention.
[0055] Figure 5 It is the design of the scanning structure in the embodiment of the present invention.
[0056] Figure 6 The figure is a schematic diagram of the kernel memory recycling program processing flow in an embodiment of the present invention.
[0057] Figure 7 Schematic diagram of the scanning balance strategy and scanning ratio control flow in an embodiment of the present invention. DETAILED DESCRIPTION
[0058] like Figure 1 and Figure 2 As shown, the Linux system memory recovery control method based on eBPF in this embodiment includes the following steps:
[0059] S1, obtain the recycling control parameters of different controls implemented by the user by combining different inputs through the BPF user state program, and pass the recycling control parameters to the BPF kernel state program;
[0060] S2, a BPF kernel-mode program loaded and run by the BPF module of the Linux operating system parses the recycling control parameters and triggers the kernel memory recycling program;
[0061] S3, implements memory recovery control of the memory control group through the triggered kernel memory recovery program or migrates the user process to the newly created memory control group to implement memory recovery control of the user process, wherein the memory recovery control includes independent recovery control of file pages and anonymous pages according to the memory usage characteristics of the application, and controls the scanning strategy during memory recovery according to the hot and cold memory characteristics of the application.
[0062] The memory recovery control method of the Linux system based on eBPF in this embodiment includes three modules in implementation, namely, BPF user state program, BPF kernel state program and kernel memory recovery program. The modules cooperate with each other, and the interaction relationship is as shown in the attached figure. Figure 2 As shown in the figure. For BPF user-mode programs, it provides intuitive and easy-to-understand control parameters. Users can achieve different controls by combining different inputs. The implementation of BPF user-mode programs includes but is not limited to command-line-based implementation or UI implementation based on Linux graphical environment development. Figure 3 As shown, the execution steps of the BPF user mode program in step S1 of this embodiment include:
[0063] S1.1, parse the user input and obtain the recycling control parameters. If the recycling control parameters are successfully obtained, jump to step S1.2, otherwise end and exit;
[0064] S1.2, construct the control parameter structure required by the BPF kernel state program according to the obtained recycling control parameters;
[0065] S1.3, open the BPF kernel state program, save the program structure pointer, if the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.4;
[0066] S1.4, load the BPF kernel-mode program into the kernel. If the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.5;
[0067] S1.5, obtain the file descriptor of the BPF kernel program. If the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.6;
[0068] S1.6, attach the BPF kernel-mode program to the kernel memory recovery program entry. If the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.7;
[0069] S1.7, construct the running context parameters of the BPF kernel state program in combination with the control parameter structure;
[0070] S1.8 triggers the execution of the BPF kernel-mode program through the obtained file descriptor and running context parameters.
[0071] The recycling control parameters in step S1.1 of this embodiment include: the absolute path cg of the memory control group, the expected memory recycling amount bytes, the recycling balance strategy balance, the file page recycling ratio file_ratio, and the scanning control options may_swap and may_unmap, wherein the recycling balance strategy balance and the file page recycling ratio file_ratio are set according to the memory usage characteristics of the application, and the scanning control options may_swap and may_unmap are set according to the memory hot and cold characteristics of the application.
[0072] In this embodiment, the memory usage feature refers to whether the application mainly uses file pages or anonymous pages. The high usage of file pages indicates that it is mainly performing operations such as file IO; the high usage of anonymous pages indicates that it is mainly performing operations such as heap memory allocation of the application, which can be judged by observing the buff / cache ratio in the free -h output. The control parameters associated with it are: the recovery balance strategy balance and the recovery ratio file_ratio of the file page. The control method is: 1) If the use of file pages is dominant, the recovery balance strategy balance can be specified as SCAN_FILE, and only file pages are recovered during recovery; 2) If the use of anonymous pages is dominant, the recovery balance strategy balance can be specified as SCAN_ANON, and only anonymous pages are recovered during recovery; 3) If both exist and have a certain proportion, the recovery balance strategy balance can be specified as SCAN_FRAC, and the recovery ratio file_ratio of the file page can be used to recover both in proportion. 4) If the usage ratio of the two is close, the recovery balance strategy balance can be specified as SCAN_EQUAL, and the two can be recovered in equal proportion. In this embodiment, the hot and cold characteristics of memory mainly refer to whether the memory has been used recently. If it has been used recently, it is a hot page; if it has not been used recently, it is a cold page, which can be judged by observing Active and Inactive in cat / proc / meminfo. The control parameters associated with it are: scan control option reclaim_options. Currently, reclaim_options supports two control strategies may_swap and may_unmap, which are mainly aimed at the scenario where the memory usage characteristics are anonymous pages. It needs to be controlled with the recovery balance strategy balance. The control method is: 1) If the current anonymous page usage is dominant and there is a swap device in the system, the recovery balance strategy balance is set to SCAN_ANON, and may_swap is set to 1, which means that the anonymous pages (cold pages) that have not been used recently are swapped out to the swap device (swap space) during recycling in order to recycle their memory. 2) If anonymous pages are currently used in a dominant manner, the recycle balance policy balance is set to SCAN_ANON and may_unmap is set to 1, which means that anonymous pages (cold pages) that have been mapped but not recently used will be unmapped during recycle, and then their memory will be reclaimed. If the proportion of hot pages in the system is high (exceeding the preset threshold), it is not appropriate to enable the above two control parameters.
[0073] For BPF kernel-mode programs, it is a bridge between BPF user-mode programs and kernel memory recycling programs. It is loaded and run through the BPF module of the Linux operating system, such as Figure 4 As shown, the execution steps of the BPF kernel state program in step S2 of this embodiment include:
[0074] S2.1, specify the running trigger point of the BPF kernel program;
[0075] S2.2, parsing the BPF kernel-mode program running context parameters passed by the BPF user-mode program;
[0076] S2.3, triggers the kernel memory recycling program.
[0077] like Figure 5 As shown, in step S3 of this embodiment, when the memory recovery control of the memory control group is implemented by the triggered kernel memory recovery program or the user process is migrated to the newly created memory control group to implement the memory recovery control of the user process, the scan control structure scan_control in the kernel of the Linux system includes three fields: a memory recovery indication field bpf_reclaim, used to indicate whether the memory recovery is initiated by eBPF; a balance strategy field bpf_balance, used to indicate the recovery balance strategy, and the balance strategy includes only recovering file pages SCAN_FILE, only recovering anonymous pages SCAN_ANON, equal proportion recovery SCAN_EQUAL and specified proportion recovery SCAN_FRAC; a recovery ratio field bpf_fileratio, used to indicate the recovery ratio of file pages, which is only valid when the recovery balance strategy is proportional recovery SCAN_FRAC.
[0078] The implementation of the kernel memory recovery program includes but is not limited to the implementation based on kfuncs (BPF kernelFunctions) or the implementation based on bpf helpers. As an optional implementation, the kernel memory recovery program in step S3 of this embodiment is a kernel memory recovery program based on BPF kernel functions kfuncs (BPF kernel Functions). The kernel memory recovery program based on BPF kernel functions kfuncs is compatible with the memory recovery mechanism based on the memory control group in terms of processing logic and can be directly called by the BPF kernel state program, which greatly enhances the flexibility of the BPF program. Figure 6 As shown, the execution steps of the kernel memory recovery program based on the BPF kernel function kfuncs in this embodiment include:
[0079] S3.1, use BPF kernel function kfuncs to define kernel memory recovery function;
[0080] S3.2, annotate the kernel memory recovery function so that it can pass the verification of the BPF module;
[0081] S3.3, register the kernel memory recovery function so that the BPF module can recognize the function;
[0082] S3.4, parse the function parameters passed by the BPF kernel-state program, mainly including the target control group cgroup, the expected amount of reclaimed memory nr_to_reclaim, the scan control option reclaim_options, the reclaim balance strategy balance, and the file page reclaim ratio file_ratio. The above parameters are saved in the scan control structure scan_control and used for the control logic judgment in step S3; the conversion relationship between the reclaim control parameters in step S1.1 and the function parameters passed by the BPF kernel-state program in step S3.4 is shown in Table 1.
[0083] Table 1: Conversion relationship between recycling control parameters and function parameters
[0084]
[0085] Referring to Table 1, in the recycling control parameters in step S1.1: the absolute path cg of the memory control group is the path of a directory on the operating system. By parsing the absolute path cg of the memory control group, the target control group cgroup can be obtained; the expected memory recycling number bytes is the amount of recycled memory that can be understood by the user, and the unit is byte. By parsing the expected memory recycling number bytes, the expected recycled memory number nr_to_reclaim can be obtained, which is the unit (physical page number) used by the operating system when recycling; the scanning control parameters may_swap and may_unmap respectively represent whether the page can be swapped out to the swap partition during recycling and whether the mapped page can be unmapped. The scanning control option reclaim_options can be obtained by parsing and encoding into bits.
[0086] S3.5, obtaining the target control group number cgroup id of the target control group cgroup to be recycled;
[0087] S3.6, triggering kernel memory recycling for the target control group cgroup, this process will call the control logic in step S3;
[0088] S3.7, determine whether the reclaimed memory reaches the expected reclaimed memory amount nr_to_reclaim, if so, end and exit; otherwise return to step S3.6 to continue memory reclaim.
[0089] In order to control the file pages and anonymous pages in memory separately, such as Figure 7As shown, the kernel memory recovery program in step S3 of this embodiment also includes actively controlling the input parameters constructed by the BPF user mode program according to the following steps by adding active control logic when calling the scan number acquisition function get_scan_count() to obtain the scan number of file pages and anonymous pages:
[0090] S3.1a, obtain the input parameters constructed by the BPF user-mode program, including the scan control structure scan_control, the current system scan balance strategy balance_old, the current system anonymous page scan ratio ap_old, and the current system file page scan ratio fp_old;
[0091] S3.2a, determine whether the value of the memory reclaim indication field bpf_reclaim is 1. If the reclaim is triggered by the BPF user mode program, the value of the memory reclaim indication field bpf_reclaim will be set to 1, which will cause the value of the memory reclaim indication field bpf_reclaim to be 1, then jump to step S3.3a; otherwise, end and exit;
[0092] S3.3a, setting the scan balance strategy balance_new after control adjustment to the value of the balance strategy field bpf_balance in the scan control structure scan_control;
[0093] S3.4a, determine whether the value of the balance strategy field bpf_balance in the scan control structure scan_control is the specified proportion recovery SCAN_FRAC, if yes, jump to step S3.5a; otherwise, end and exit;
[0094] S3.5a, calculate the denominator for proportional recovery according to the following formula:
[0095] denominator = ap_old + fp_old,
[0096] In the above formula, ap_old is the current system anonymous page scan ratio in the input parameter, and fp_old is the current system file page scan ratio in the input parameter;
[0097] S3.6a, read the recovery ratio field bpf_fileratio in the scan control structure scan_control, and calculate the file page scan ratio fp_new after control adjustment according to the following formula:
[0098] fp_new = sc->bpf_fileratio * denominator / 100,
[0099] In the above formula, sc->bpf_fileratio is the recovery ratio field bpf_fileratio in the scan control structure scan_control, and denominator is the denominator end of the proportional recovery;
[0100] S3.7a, calculate the anonymous page scan ratio ap_new after control adjustment according to the following formula:
[0101] ap_new = denominator - fp_new,
[0102] In the above formula, denominator is the denominator end of the proportional recovery;
[0103] S3.8a, outputting the controlled and adjusted scanning balance strategy balance_new, the controlled and adjusted anonymous page scanning ratio ap_new, and the controlled and adjusted file page scanning ratio fp_new to respectively control the file pages and anonymous pages in the memory.
[0104] In summary, the memory recovery control method of the Linux system based on eBPF in this embodiment uses the powerful interaction ability of eBPF and the kernel to realize the memory recovery control of the Linux operating system, so as to avoid the differences between different versions of the control group, provide a general memory control framework and intuitive and easy-to-understand control parameters for recovery control, support independent recovery control of file pages and anonymous pages according to the memory usage characteristics of the application, and support control of the scanning strategy of memory recovery according to the memory hot and cold characteristics of the application. The memory recovery control method of the Linux system based on eBPF in this embodiment directly controls the kernel to recover memory through intuitive and easy-to-understand control parameters, avoids the interface differences between different versions of the control group cgroup, and provides a general memory control framework. The memory recovery control method of the Linux system based on eBPF in this embodiment supports independent recovery control of file pages and anonymous pages according to the memory usage characteristics of the application. The memory recovery control method of the Linux system based on eBPF in this embodiment supports control of the scanning strategy during memory recovery according to the memory hot and cold characteristics of the application.
[0105] In addition, this embodiment also provides a Linux system memory recycling control system based on eBPF, including a microprocessor and a memory connected to each other, and the microprocessor is programmed or configured to execute the Linux system memory recycling control method based on eBPF.
[0106] In addition, this embodiment also provides a computer-readable storage medium, which stores a computer program or instruction, and the computer program or instruction is programmed or configured to execute the eBPF-based Linux system memory recovery control method through a processor.
[0107] In addition, this embodiment also provides a computer program product, including a computer program or instructions, which are programmed or configured to execute the eBPF-based Linux system memory recycling control method through a processor.
[0108] Those skilled in the art should understand that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application can take the form of a computer program product implemented on one or more computer-readable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code. The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the process Figure 1 A process or multiple processes and / or boxes Figure 1 These computer program instructions can also be stored in a computer-readable memory that can guide a computer or other programmable data processing device to work in a specific way, so that the instructions stored in the computer-readable memory produce a product including an instruction device, which implements the functions specified in the process. Figure 1 A process or multiple processes and / or boxes Figure 1 These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to produce a computer-implemented process, so that the instructions executed on the computer or other programmable device provide for implementing the process in the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0109] The above is only a preferred embodiment of the present invention, and the protection scope of the present invention is not limited to the above embodiments. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, some improvements and modifications without departing from the principle of the present invention should also be regarded as the protection scope of the present invention.
Claims
1. A Linux system memory recovery control method based on eBPF, characterized in that: The steps include: S1, obtain the recycling control parameters of different controls implemented by the user by combining different inputs through the BPF user state program, and pass the recycling control parameters to the BPF kernel state program; S2, the BPF kernel program loaded and run by the BPF module of the Linux operating system parses the recycling control parameters and triggers the kernel memory recycling program; S3, implementing memory recycling control on the memory control group through the triggered kernel memory recycling program or migrating the user process to the newly created memory control group to implement memory recycling control on the user process, wherein the memory recycling control includes independent recycling control on file pages and anonymous pages according to the memory usage characteristics of the application, and controlling the scanning strategy during memory recycling according to the memory hot and cold characteristics of the application; The kernel memory recovery program is a kernel memory recovery program based on the BPF kernel function kfuncs. The kernel memory recovery program based on the BPF kernel function kfuncs is compatible with the memory recovery mechanism based on the memory control group in processing logic and can be directly called by the BPF kernel state program.
2. The eBPF-based Linux system memory recovery control method according to claim 1, characterized in that: The execution steps of the BPF user mode program in step S1 include: S1.1, parse the user input and obtain the recycling control parameters. If the recycling control parameters are successfully obtained, jump to step S1.2, otherwise end and exit; S1.2, construct the control parameter structure required by the BPF kernel state program according to the obtained recycling control parameters; S1.3, open the BPF kernel state program, save the program structure pointer, if the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.4; S1.4, load the BPF kernel-mode program into the kernel. If the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.5; S1.5, obtain the file descriptor of the BPF kernel program. If the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.6; S1.6, attach the BPF kernel-mode program to the kernel memory recovery program entry. If the execution fails, execute the specified failure processing, end and exit; otherwise, jump to step S1.7; S1.7, construct the running context parameters of the BPF kernel state program in combination with the control parameter structure; S1.8 triggers the execution of the BPF kernel-mode program through the obtained file descriptor and running context parameters.
3. The eBPF-based Linux system memory recovery control method according to claim 2, characterized in that: The recycling control parameters in step S1.1 include: the absolute path cg of the memory control group, the expected memory recycling amount bytes, the recycling balance strategy balance, the file page recycling ratio file_ratio, and the scanning control options may_swap and may_unmap. The recycling balance strategy balance and the file page recycling ratio file_ratio are set according to the memory usage characteristics of the application, and the scanning control options may_swap and may_unmap are set according to the memory hot and cold characteristics of the application.
4. The eBPF-based Linux system memory recovery control method according to claim 1, characterized in that: The execution steps of the BPF kernel state program in step S2 include: S2.1, specify the running trigger point of the BPF kernel program; S2.2, parsing the BPF kernel-mode program running context parameters passed by the BPF user-mode program; S2.3, triggers the kernel memory recycling program.
5. The method for controlling memory recovery in a Linux system based on eBPF according to claim 1, characterized in that: In step S3, when the memory recovery control of the memory control group is implemented through the triggered kernel memory recovery program or the user process is migrated to the newly created memory control group to implement memory recovery control of the user process, the scan control structure scan_control in the kernel of the Linux system contains three fields: a memory recovery indication field bpf_reclaim, used to indicate whether the memory recovery is initiated by eBPF; a balance strategy field bpf_balance, used to indicate the recovery balance strategy, and the balance strategy includes only recovering file pages SCAN_FILE, only recovering anonymous pages SCAN_ANON, equal proportion recovery SCAN_EQUAL and specified proportion recovery SCAN_FRAC; a recovery ratio field bpf_fileratio, used to indicate the recovery ratio of file pages, which is only valid when the recovery balance strategy is proportional recovery SCAN_FRAC.
6. The eBPF-based Linux system memory recycling control method according to claim 5, characterized in that: The execution steps of the kernel memory recovery program based on the BPF kernel function kfuncs include: S3.1, use BPF kernel function kfuncs to define kernel memory recovery function; S3.2, annotate the kernel memory recovery function so that it can pass the verification of the BPF module; S3.3, register the kernel memory recovery function so that the BPF module can recognize the function; S3.4, parse the function parameters passed by the BPF kernel program, mainly including the target control group cgroup, the expected amount of reclaimed memory nr_to_reclaim, the scan control option reclaim_options, the reclaim balance strategy balance, and the file page reclaim ratio file_ratio. The above parameters are saved in the scan control structure scan_control; S3.5, obtaining the target control group number cgroup id of the target control group cgroup to be recycled; S3.6, trigger kernel memory recycling for the target control group cgroup; S3.7, determine whether the reclaimed memory reaches the expected reclaimed memory amount nr_to_reclaim, if so, end and exit; otherwise return to step S3.6 to continue memory reclaim.
7. The eBPF-based Linux system memory recovery control method according to claim 5, characterized in that: The kernel memory recovery program in step S3 also includes actively controlling the input parameters constructed by the BPF user mode program by adding active control logic according to the following steps when calling the scan number acquisition function get_scan_count() to obtain the scan number of file pages and anonymous pages: S3.1a, obtain the input parameters constructed by the BPF user-mode program, including the scan control structure scan_control, the current system scan balance strategy balance_old, the current system anonymous page scan ratio ap_old, and the current system file page scan ratio fp_old; S3.2a, determine whether the value of the memory reclaim indication field bpf_reclaim is 1. If the reclaim is triggered by the BPF user mode program, the value of the memory reclaim indication field bpf_reclaim will be set to 1, which will cause the value of the memory reclaim indication field bpf_reclaim to be 1, then jump to step S3.3a; otherwise, end and exit; S3.3a, setting the scan balance strategy balance_new after control adjustment to the value of the balance strategy field bpf_balance in the scan control structure scan_control; S3.4a, determine whether the value of the balance strategy field bpf_balance in the scan control structure scan_control is the specified proportion recovery SCAN_FRAC, if yes, jump to step S3.5a; otherwise, end and exit; S3.5a, calculate the denominator for proportional recovery according to the following formula: denominator = ap_old + fp_old, In the above formula, ap_old is the current system anonymous page scan ratio in the input parameter, and fp_old is the current system file page scan ratio in the input parameter; S3.6a, read the recovery ratio field bpf_fileratio in the scan control structure scan_control, and calculate the file page scan ratio fp_new after control adjustment according to the following formula: fp_new = sc->bpf_fileratio * denominator / 100, In the above formula, sc->bpf_fileratio is the recovery ratio field bpf_fileratio in the scan control structure scan_control, and denominator is the denominator end of the proportional recovery; S3.7a, calculate the anonymous page scan ratio ap_new after control adjustment according to the following formula: ap_new = denominator - fp_new, In the above formula, denominator is the denominator end of the proportional recovery; S3.8a, outputting the controlled and adjusted scanning balance strategy balance_new, the controlled and adjusted anonymous page scanning ratio ap_new, and the controlled and adjusted file page scanning ratio fp_new to respectively control the file pages and anonymous pages in the memory.
8. A Linux system memory recycling control system based on eBPF, comprising a microprocessor and a memory connected to each other, characterized in that: The microprocessor is programmed or configured to execute the eBPF-based Linux system memory recycling control method described in any one of claims 1 to 7.
9. A computer-readable storage medium having a computer program or instruction stored therein, characterized in that: The computer program or instruction is programmed or configured to execute the eBPF-based Linux system memory recycling control method described in any one of claims 1 to 7 through a processor.
10. A computer program product comprising a computer program or instructions, characterized in that The computer program or instruction is programmed or configured to execute the eBPF-based Linux system memory recycling control method described in any one of claims 1 to 7 through a processor.
Citation Information
Patent Citations
Memory bookkeeping anomaly detection method and device, electronic equipment and storage medium
CN113835917A
Memory recovery method and device and terminal equipment
CN116048769A
Cited By
Progressive memory delay monitoring and diagnosis method for virtual memory subsystem
CN121029539A
Progressive memory latency monitoring and diagnostic method for virtual memory subsystems
CN121029539B