Method and device for creating executable program code segment read-only file mapping memory copy
By creating a memory copy of the code segment on its local NUMA node for each process under a multi-NUMA architecture, the high memory access delay problem caused by access across NUMA nodes is solved, and the program performance is significantly improved.
Patent Information
- Application Number
- CN202510143132.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-10
- Publication Date
- 2025-05-23
AI Technical Summary
Under the multi-NUMA architecture, in the prior art, the read-only file mapping memory of executable program code segments is accessed across NUMA nodes, resulting in high memory access delay and reducing application performance.
In a NUMA architecture system, when a process executes on a NUMA node, it uses read and write methods to map the code segments, and iterates through all pages, read and write back bytes to trigger page-missing processing, completes the allocation of the code segment memory copy, and ensures that each process has an independent code segment memory copy on its local NUMA node through system call locking and control permissions.
It effectively avoids accessing code segment memory across NUMA nodes, significantly reduces memory fetch latency, improves program execution efficiency and performance, and ensures that each process accesses code segments with the highest memory fetch performance.
Smart Images

Figure CN120029691A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of program loading and memory management, and in particular to a method and device for creating a read-only file-mapped memory copy of an executable program code segment. Background Art
[0002] In modern computer operating systems, the operation of applications requires the operating system and the loader to load the application and its dynamic dependency libraries into memory. The operating system kernel is responsible for loading the executable program and the dynamic loader into memory. The dynamic loader then parses the dynamic library dependencies of the executable program and loads them into memory, and finally passes control to the main program of the application. This process is usually achieved by mapping the application on disk to the memory address space of the process, and allocating physical memory by the page fault handling function when it is first accessed.
[0003] Non-Uniform Memory Access (NUMA) is a computer memory design for multiprocessing where the memory access time depends on the location of the memory relative to the processor. In the NUMA architecture, each processor node has local memory, and the processor can access its own local memory faster, while non-local memory (the local memory of another processor or the memory shared between processors) is accessed relatively slowly. The I / O bus is often associated with NUMA. For example, in a system with two NUMA nodes, each NUMA node has local memory and CPU, and the process of NUMA0 can access the memory of NUMA1. In the absence of mandatory configuration of memory allocation policy, the operating system kernel usually allocates local memory to application processes to optimize memory access performance.
[0004] Under the NUMA architecture, the operating system loads executable programs and the dynamic loader loads dynamic libraries as follows: When an application is executed on NUMA0, the kernel and dynamic loader are responsible for loading the data segment and code segment into memory, and the operating system will give priority to allocating NUMA0 local memory; the mapping mode of the data segment is read-write, while the mapping mode of the code segment is read-only. Similarly, when another application is executed on NUMA1, the kernel and dynamic loader are responsible for loading the data segment and code segment into memory, where the mapping mode of the data segment is read-write, and memory will be allocated on the local memory of NUMA1; the mapping mode of the code segment is read-only, and the operating system will perform memory optimization for the file mapped in this read-only mode. When a read page fault interrupt occurs, the operating system will point the virtual address of the process to the physical address of the code segment of NUMA0, so that there is only one copy of the code segment in the physical memory.
[0005] However, the existing technical solutions have obvious disadvantages in multi-NUMA situations. Executable files and code segments such as dynamic libraries have read-only file mapping memory accesses across NUMA memory, which will lead to higher memory access latency, thereby reducing application performance, and this performance reduction is positively correlated with cross-NUMA memory access latency.
[0006] Therefore, how to solve the high latency problem of memory access across NUMA nodes in existing solutions and improve the performance of programs under multi-NUMA architectures by optimizing the way of mapping memory copies of read-only files of executable program code segments to meet the needs of modern multi-processing computer systems for high-performance memory access has become a technical problem that needs to be solved urgently. Summary of the invention
[0007] In view of this, in order to overcome the deficiencies of the prior art, the present application aims to provide a method and device for creating a read-only file-mapped memory copy of an executable program code segment.
[0008] According to a first aspect of the present application, a method for creating a read-only file-mapped memory copy of an executable program code segment is provided, the method comprising the following steps: In a NUMA architecture system, when a process is executed on a NUMA node, the code segment is mapped using read-write mode; Traverse all pages mapped by this code segment, read a byte from each page and write it back, triggering the page fault handling process to complete the allocation of the memory copy of the code segment of the process; The memory copy of the code segment is locked and permission controlled through system calls.
[0009] Optionally, in the method for creating a read-only file-mapped memory copy of an executable program code segment of the present application, the allocation of the code segment memory copy is performed separately for each process, and each process has an independent code segment memory copy on its local NUMA node.
[0010] Optionally, in the method for creating a read-only file-mapped memory copy of an executable program code segment of the present application, the allocation process of the code segment memory copy is completed when the process starts, and during the running of the process, the process only accesses the code segment memory copy on its local NUMA node.
[0011] Optionally, in the method for creating a read-only file mapping memory copy of an executable program code segment of the present application, a dynamic loader environment variable is used to control whether to create a process-level memory copy for the dynamic library code segment read-only file mapping.
[0012] Optionally, in the method for creating a read-only file-mapped memory copy of the executable program code segment of the present application, when the value of the dynamic loader environment variable is 0, it means that a process-level memory copy is not created; when the value of the dynamic loader environment variable is 1, it means that a process-level memory copy is created.
[0013] Optionally, in the method for creating a read-only file-mapped memory copy of an executable program code segment of the present application, a copy-on-write method is used to create the code segment memory copy.
[0014] Optionally, in the method for creating a read-only file-mapped memory copy of an executable program code segment of the present application, the code segment memory copy is locked and permission controlled through a system call, including: locking the code segment memory copy through the mlock system call to prevent it from being swapped out.
[0015] Optionally, in the method for creating a read-only file-mapped memory copy of an executable program code segment of the present application, the code segment memory copy is locked and permission controlled through a system call, and also includes: deleting the write permission of the code segment memory copy through the mprotect system call to make it a read-only copy.
[0016] Optionally, in the method for creating a read-only file-mapped memory copy of an executable program code segment of the present application, when a new process is created and run, the process will have a new read-only copy of the code segment.
[0017] According to a second aspect of the present application, a computer device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method described in the first aspect of the present application is implemented.
[0018] The method and device for creating a read-only file-mapped memory copy of an executable program code segment of the present application have the following beneficial technical effects: 1. Significantly improve program execution performance: By creating a NUMA local physical memory copy for the executable program code segment, it is possible to effectively avoid accessing the code segment memory across NUMA nodes. Under the traditional NUMA architecture, memory access across NUMA nodes will result in higher memory access latency, which significantly reduces the program's running performance. This application creates a physical memory copy of the code segment locally on the NUMA node where each process is located, allowing the process to directly access the code segment copy in the local memory, thereby significantly reducing memory access latency and significantly improving program execution efficiency and performance.
[0019] 2. Propose an innovative process-level code segment copy solution: Different from the existing technology in which there is only one code segment copy in the physical memory, this application creates a separate code segment copy for each process on its local NUMA node. This design can not only effectively solve the high memory access latency problem caused by cross-NUMA access to code segment memory in the existing technology, but also fundamentally solve the high latency problem of cross-NUMA node memory access, ensuring that each process can access the code segment with the highest memory access performance, thereby improving the overall performance of the program.
[0020] 3. Flexible implementation and dynamic control mechanism: This application maps the code segment in a read-write manner and uses the copy-on-write (COW) technology to implement a read-only physical memory copy of the code segment at the process level, which not only ensures the memory security of the memory allocation process, but also avoids major changes to the existing system architecture and operating mechanism, and has good compatibility and feasibility. In addition, the present invention also introduces a dynamic loader environment variable to flexibly control whether to create a process-level memory copy for the dynamic library code segment. This dynamic control mechanism allows users to flexibly choose whether to enable the memory copy function according to actual needs and system resource conditions, further improving the flexibility and applicability of the system.
[0021] 4. Universality and wide applicability: The solution of this application is not only applicable to executable program code segments, but also has universality and can be applied to all read-only file mappings. By creating a physical memory copy for read-only file mappings, this application can effectively reduce memory access latency, thereby improving program performance. This universality allows this application to be widely used in various scenarios that require optimization of memory access performance, not only limited to code segments, but also extended to other types of read-only file mappings, providing a wider application prospect for system performance optimization. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0023] Figure 1 A flowchart of a method for creating a read-only file-mapped memory copy of an executable program code segment according to an embodiment of the present application; Figure 2 An example diagram of an execution of a method for creating a read-only file-mapped memory copy of an executable program code segment according to an embodiment of the present application; Figure 3 A schematic diagram of the structure of the device provided in this application. DETAILED DESCRIPTION
[0024] The embodiments of the present application are described in detail below with reference to the accompanying drawings.
[0025] It should be noted that the following embodiments and features in the embodiments may be combined with each other in the absence of conflict; and, based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in the field without making any creative work are within the scope of protection of the present disclosure.
[0026] It should be noted that various aspects of the embodiments within the scope of the appended claims are described below. It should be apparent that the aspects described herein may be embodied in a wide variety of forms, and any specific structure and / or function described herein is merely illustrative. Based on the present disclosure, it should be understood by those skilled in the art that an aspect described herein may be implemented independently of any other aspect, and two or more of these aspects may be combined in various ways. For example, any number of aspects described herein may be used to implement the device and / or practice the method. In addition, other structures and / or functionalities other than one or more of the aspects described herein may be used to implement this device and / or practice this method.
[0027] Figure 1 FIG. 1 is a flowchart of a method for creating a read-only file mapping memory copy of an executable program code segment according to an embodiment of the present application. Figure 1 As shown, the method for creating a read-only file mapping memory copy of the executable program code segment includes the following steps: Step S101: In a NUMA architecture system, when a process is executed on a certain NUMA node, a code segment is mapped using a read-write method.
[0028] In this embodiment, the allocation of code segment memory copies is performed for each process separately, and each process has an independent code segment memory copy on its local NUMA node. The allocation process of code segment memory copies is completed when the process starts, and during the process running, the process only accesses the code segment memory copy on its local NUMA node.
[0029] Step S102: traverse all pages mapped by the code segment, read a byte from each page and write the byte back, trigger the page fault processing flow, and complete the allocation of the memory copy of the code segment of the process.
[0030] The dynamic loader environment variable is used to control whether to create a process-level memory copy for the dynamic library code segment read-only file mapping. When the value of the dynamic loader environment variable is 0, it means that a process-level memory copy is not created. When the value of the dynamic loader environment variable is 1, it means that a process-level memory copy is created. It should be noted that this embodiment uses a write-time copy method to create a code segment memory copy.
[0031] Step S103: Locking and controlling the permissions of the code segment memory copy through a system call.
[0032] The mlock system call is used to lock the memory copy of the code segment to prevent it from being swapped out. The mprotect system call is used to delete the write permission of the memory copy of the code segment to make it a read-only copy. It should be noted that in this embodiment, when a new process is created and run, the process will have a new read-only copy of the code segment.
[0033] The following further describes in detail the method for creating a read-only file-mapped memory copy of an executable program code segment in this embodiment in a specific scenario. Figure 2 The present invention is an example diagram of the execution of a method for creating a read-only file-mapped memory copy of an executable program code segment according to an embodiment of the present application.
[0034] like Figure 2 As shown, in this scenario, the creation of a read-only file-mapped memory copy of the executable program code segment is performed as follows: Process 1 is executed on NUMA0, and the data segment is mapped in read-write mode; the code segment is mapped in read-only mode; the operating system kernel allocates physical memory for the mapping on NUMA0; process x is executed on NUMAx, and the data segment is mapped in read-write mode; the code segment is mapped in read-write mode, and then all pages of this mapping are traversed, a byte is read from each page, and the byte is written back, so that this mapping completes the page fault processing flow and completes the allocation of the memory copy of the code segment of the process; then, the mlock system call is used to prevent this block of memory from being swapped out. Finally, in order to prevent the application process from tampering with the code segment, the mprotect system call is used to delete the write permission of the memory copy of the code segment. In this embodiment, the mlock system call is used to lock the memory pages to prevent these pages from being swapped to the disk. The mprotect system call is used to change the protection attributes of the memory area, which allows the program to dynamically modify the access rights of the allocated memory area, such as setting it to read-only, read-write, inaccessible, etc.
[0035] In this embodiment, a copy is created using a copy-on-write method. During the mapping and page fault processing, the memory is safe because the process is controlled by the kernel or dynamic loader at this time, and the content of the memory copy will not be accessed by the application itself. When a new process is run again, the process will have a new read-only copy of the code segment.
[0036] The process-level code segment memory copy is flexible. You can add the dynamic loader environment variable LD_DUP_RO_FILE_MEM to control whether to create a process-level memory copy for the dynamic library code segment read-only file mapping. The dynamic loader environment variable is LD_DUP_RO_FILE_MEM. When its value is 0, it means that the process-level memory copy is not created. When its value is 1, it means that the process-level memory copy is created.
[0037] The method for creating a read-only file-mapped memory copy of an executable program code segment in this embodiment has the following beneficial technical effects: 1. Significantly improve program execution performance: By creating a NUMA local physical memory copy for the executable program code segment, it is possible to effectively avoid accessing the code segment memory across NUMA nodes. Under the traditional NUMA architecture, memory access across NUMA nodes will result in higher memory access latency, which significantly reduces the program's running performance. This application creates a physical memory copy of the code segment locally on the NUMA node where each process is located, allowing the process to directly access the code segment copy in the local memory, thereby significantly reducing memory access latency and significantly improving program execution efficiency and performance.
[0038] 2. Propose an innovative process-level code segment copy solution: Different from the existing technology in which there is only one code segment copy in the physical memory, this application creates a separate code segment copy for each process on its local NUMA node. This design can not only effectively solve the high memory access latency problem caused by cross-NUMA access to code segment memory in the existing technology, but also fundamentally solve the high latency problem of cross-NUMA node memory access, ensuring that each process can access the code segment with the highest memory access performance, thereby improving the overall performance of the program.
[0039] 3. Flexible implementation and dynamic control mechanism: This application maps the code segment in a read-write manner and uses the copy-on-write (COW) technology to implement a read-only physical memory copy of the code segment at the process level, which not only ensures the memory security of the memory allocation process, but also avoids major changes to the existing system architecture and operating mechanism, and has good compatibility and feasibility. In addition, the present invention also introduces a dynamic loader environment variable to flexibly control whether to create a process-level memory copy for the dynamic library code segment. This dynamic control mechanism allows users to flexibly choose whether to enable the memory copy function according to actual needs and system resource conditions, further improving the flexibility and applicability of the system.
[0040] 4. Universality and wide applicability: The solution of this application is not only applicable to executable program code segments, but also has universality and can be applied to all read-only file mappings. By creating a physical memory copy for read-only file mappings, this application can effectively reduce memory access latency, thereby improving program performance. This universality allows this application to be widely used in various scenarios that require optimization of memory access performance, not only limited to code segments, but also extended to other types of read-only file mappings, providing a wider application prospect for system performance optimization.
[0041] like Figure 3 As shown, the present application also provides a device, including a processor 210, a communication interface 220, a memory 230 for storing a processor executable computer program, and a communication bus 240. The processor 210, the communication interface 220, and the memory 230 communicate with each other through the communication bus 240. The processor 210 implements the above-mentioned method for creating a read-only file mapped memory copy of an executable program code segment by running an executable computer program.
[0042] Among them, the computer program in the memory 230 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.
[0043] The system embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, i.e., they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected based on actual needs to achieve the purpose of the solution of this embodiment. Those of ordinary skill in the art may understand and implement it without creative effort.
[0044] Through the description of the above implementation modes, those skilled in the art can clearly understand that each implementation mode can be implemented by means of software plus a necessary general hardware platform, or of course by hardware. Based on such an understanding, the above technical solution can essentially or in other words be embodied in the form of a software product that contributes to the prior art. The computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods of each embodiment or some parts of the embodiment.
[0045] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed in the present application should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.
Claims
1. A method for creating a read-only file-mapped memory copy of an executable program code segment, characterized in that: The following steps are involved: In a NUMA architecture system, when a process is executed on a NUMA node, the code segment is mapped using read-write mode; Traverse all pages mapped by this code segment, read a byte from each page and write it back, triggering the page fault handling process to complete the allocation of the memory copy of the code segment of the process; The memory copy of the code segment is locked and permission controlled through system calls.
2. The method for creating a read-only file-mapped memory copy of an executable program code segment according to claim 1, characterized in that: The allocation of code segment memory copies is performed separately for each process, and each process has an independent code segment memory copy on its local NUMA node.
3. The method for creating a read-only file-mapped memory copy of an executable program code segment according to claim 1, characterized in that: The allocation process of the code segment memory copy is completed when the process starts, and during the process running, the process only accesses the code segment memory copy on its local NUMA node.
4. The method for creating a read-only file-mapped memory copy of an executable program code segment according to claim 1, characterized in that: Whether to create a process-level memory copy of the dynamic library code segment read-only file mapping is controlled by the dynamic loader environment variable.
5. The method for creating a read-only file-mapped memory copy of an executable program code segment according to claim 4, characterized in that: When the value of the dynamic loader environment variable is 0, it means that no process-level memory copy is created. When the value of the dynamic loader environment variable is 1, it means that a process-level memory copy is created.
6. The method for creating a read-only file-mapped memory copy of an executable program code segment according to claim 1, characterized in that: A copy of the code segment memory is created using a copy-on-write method.
7. The method for creating a read-only file-mapped memory copy of an executable program code segment according to claim 1, locking and controlling the permissions of the code segment memory copy through a system call, comprising: The mlock system call is used to lock the memory copy of the code segment to prevent it from being swapped out.
8. The method for creating a read-only file-mapped memory copy of an executable program code segment according to claim 1, characterized in that: The memory copy of the code segment is locked and permission controlled through a system call, and also includes: deleting the write permission of the memory copy of the code segment through an mprotect system call to make it a read-only copy.
9. The method for creating a read-only file-mapped memory copy of an executable program code segment according to claim 1, characterized in that: When a new process is created and run, it will have a read-only copy of the new code segment.
10. A computer device, characterized in that: The computer device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements the steps of the method according to any one of claims 1 to 9 when executing the program.
Citation Information
Cited By
NUMA system page cache copy management method and system
CN122132334A
Numa system page cache copy management method and system
CN122132334B