1 Preface
Dpdk manages the physical memory reserved by hugepage in a unified manner, and then provides interfaces for external use in the form of libraries. Shows the relationship between memory-related modules in dpdk.
Rte_eal is a unified organization manager (of course, rte_eal is not just for memory work)
Rte_malloc provides APIs for allocating and releasing memory externally. The allocated memory is the memory managed in rte_eal.
Rte_ring provides a lock-free queue, which uses the memory managed by rte_eal.
Rte_mempool uses the memory and rte_ring in rte_eal to provide the memory pool function.
Ret_mbuf
Next we will analyze how each module processes and uses the memory one by one (focusing on the process and not introducing the Code in detail ), the Memory managed by dpdk has a very useful feature-there are different process address spaces in the same physical area, and the virtual addresses are the same. The following describes in detail how dpdk implements this function.
First of all, hugepage is not described here. To understand dpdk memory management, you must know hugepage. If you do not know about it, just complete it yourself. There is a document in the Linux kernel that describes it in detail. Click here to view details.
2 rte_eal: Memory organization and management
This this is implemented in rrte_eal. Related functions include:
Rrte_ealinit-> eal_hugepage_info_init
Rte_eal_init-> rte_eal_memory_init
Rte_eal_init-> rte_eal_memzone_init
Rte_eal_init-> eal_check_mem_on_local_socket
2.1 eal_hugepage_info_init
First, the dpdk memory is reserved through hugepage. This function mainly gets information about the reserved memory by reading files in/sys/kernel/MM/hugepages, for more information, see hugepage.
Reserved memory is classified by page size (i386 architecture supports 4 K and 4 m (2 m in PAE mode) page sizes, IA64 architecture supports multiple page sizes 4 K, 8 K, 64 K, 256 k, 1 m, 4 m, 16 m, 256 m and ppc64 supports 4 K and 16 m .), there may be multiple classes. dpdk reads all the information and saves it using struct hugepage_info. Each class corresponds to a struct hugepage_info type object. All these objects are saved in the struct internal_config array. Dpdk has a global variable of internal_config, which records the information.
struct hugepage_info { size_t hugepage_sz; /**< size of a huge page */ const char *hugedir; /**< dir where hugetlbfs is mounted */ uint32_t num_pages[RTE_MAX_NUMA_NODES]; /**< number of hugepages of that size on each socket */ int lock_descriptor; /**< file descriptor for hugepage dir */};
struct internal_config {
... unsigned num_hugepage_sizes; /**< how many sizes on this system */ struct hugepage_info hugepage_info[MAX_HUGEPAGE_SIZES];};
struct internal_config internal_config;
To sum up, eal_hugepage_info_init obtains the reserved memory information in the system. There are several types of information (the same page size is one type) that includes the information of each type (page size, page number, and mount point) stored in the internal_config.hugepage_info array. The hugepage_info array is also sorted. The information of large pages is placed before the information of small pages.
All memory pages of each category are also divided into the socket on which the page is located (do not understand how to check NUMA related knowledge). In hugepage_info, the number of memory pages is counted based on the socket on which the page belongs, however, in this step (eal_hugepage_info_init), the number of pages on which each page is located cannot be distinguished. Therefore, the number of pages is not counted by socket, the number of pages in the memory is directly recorded in num_pages [0.
2.2 rte_eal_memory_init
intrte_eal_memory_init(void){ RTE_LOG(INFO, EAL, "Setting up memory...\n"); const int retval = rte_eal_process_type() == RTE_PROC_PRIMARY ? rte_eal_hugepage_init() : rte_eal_hugepage_attach(); if (retval < 0) return -1; if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0) return -1; return 0;}
Dpdk is divided into rte_proc_primary process and Worker Process in multi-process state. rte_proc_primary initializes the memory, rte_proc_secondary obtains the rte_proc_primary memory ing information, and creates a memory ing similar to rte_proc_pri. This is how dpdk multi-process memory is shared. The description is not described here. As the process expands, you will naturally understand it.
2.2.1 rte_eal_mem_init-> rte_eal_hugepage_init
Dpdk has a global variable.
Static struct rte_config
The variable member struct rte_mem_config * mem_config; stores all information about the memory managed by dpdk. Dpdk manages the memory in a unified manner, that is, it saves all the memory to mem_config: memseg.
struct rte_mem_config { volatile uint32_t magic; /**< Magic number - Sanity check. */ /* memory topology */ uint32_t nchannel; /**< Number of channels (0 if unknown). */ uint32_t nrank; /**< Number of ranks (0 if unknown). */ /** * current lock nest order * - qlock->mlock (ring/hash/lpm) * - mplock->qlock->mlock (mempool) * Notice: * *ALWAYS* obtain qlock first if having to obtain both qlock and mlock */ rte_rwlock_t mlock; /**< only used by memzone LIB for thread-safe. */ rte_rwlock_t qlock; /**< used for tailq operation for thread safe. */ rte_rwlock_t mplock; /**< only used by mempool LIB for thread-safe. */ uint32_t memzone_idx; /**< Index of memzone */ /* memory segments and zones */ struct rte_memseg memseg[RTE_MAX_MEMSEG]; /**< Physmem descriptors. */ struct rte_memzone memzone[RTE_MAX_MEMZONE]; /**< Memzone descriptors. */ /* Runtime Physmem descriptors. */ struct rte_memseg free_memseg[RTE_MAX_MEMSEG]; struct rte_tailq_head tailq_head[RTE_MAX_TAILQ]; /**< Tailqs for objects */ /* Heaps of Malloc per socket */ struct malloc_heap malloc_heaps[RTE_MAX_NUMA_NODES];} __attribute__((__packed__));
Detailed process:
The function first obtains the total number of memory pages according to struct hugepage_info [max_hugepage_sizes]; (information obtained in eal_hugepage_info_init. Then, I applied for the memory for struct hugepage_file * tmp_hp, and allocated the number of hugepage_files as to the number of memory pages.
struct hugepage_file { void *orig_va; /**< virtual addr of first mmap() */ void *final_va; /**< virtual addr of 2nd mmap() */ uint64_t physaddr; /**< physical addr */ size_t size; /**< the page size */ int socket_id; /**< NUMA socket ID */ int file_id; /**< the ‘%d‘ in HUGEFILE_FMT */ int memseg_id; /**< the memory segment to which page belongs */#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS int repeated; /**< number of times the page size is repeated */#endif char filepath[MAX_HUGEPAGE_PATH]; /**< path to backing file on filesystem */};
Then, the system processes each type of memory (memory of the same page size is counted as one type,
For (each class ){
For (every page of this category ){
Each page corresponds to a hugepage_file, file_id, size, and filepath initialization. Here, file_id is the page serial number, which is used for filepath to create an independent file name for each file corresponding to the page (it will not be repeated with other pages ).
Create a file <filepath>. This file is used because the hugepage memory must be used in this way (create a file first, then use MMAP to obtain the memory virtual address.
Run MMAP to obtain the virtual address of the page and assign it to hugepage_file: orig_va.
} (Map_all_hugepages ())
All the physical memory reserved by hugepage is mapped and can be accessed according to hugepage_file: orig_va.
For (every page of this category ){
Obtain the physical address and assign it to hugepage_file: physaddr.
The method for obtaining the physical address is provided by the Linux kernel. If you are not familiar with it, click here to complete it.
} (Find_physaddrs ())
For (every page of this category ){
Obtain the socket of each page and assign it to hugepage_file: socket_id.
The method is also provided by the Linux kernel.
} (Find_numasocket ())
Hugepage_file [] of such pages are sorted by PAGE physical addresses.
For (every page of this category ){
Re-Execute MMAP and assign the obtained virtual address to hugepage_file: final_va.
} (Map_all_hugepages ())
This re-MMAP, unlike the first one, will apply for a virtual address space that is sufficient to map all the memory pages of this class, then, all pages of this class are consecutively mapped to the virtual address space.
If such a large virtual address space cannot be found, it will still map one page at a time, and let the kernel decide the virtual address space.
The second ing explains the second mapping tries to map continguous physical blocks in contiguous virtual blocks.
Unmap the first ing
}
Continue
Update internal_config.hugepage_info []. num_pages []. We know that when bugepage_info is initialized in eal_hugepage_info_init, we do not know which socket each page belongs to. Therefore, we directly use num_pages [0] to record all pages, however, in the previous step, we already know the socket to which each page belongs. Therefore, num_pages is updated here to accurately record the number of pages on which socket.
I haven't looked at the unmap pages that we won't need (Looks at used_hp). I will study it later.
Then, the shared memory is created, and hugepage_file is copied to the shared memory. After rte_proc_secondary is started, the information can be read in the shared memory, and a ing consistent with rte_proc_primary is created.
Finally, use mem_config-> memseg [] to manage all the memory.
A struct rte_memsg object that stores a block of memory with the same socket, the same page size, the physical address, and the virtual address. Therefore, the entire memory is composed of multiple memseg instances.
Summary: rte_eal_hugepage_init obtains all the physical memory reserved by hugepge, maps it to the virtual address space in a unified manner, and saves it to memseg. Now, we can theoretically obtain the virtual address through memseg to use the memory. However, dpdk provides a uniform way to use it. It can be used by no one.
2.2.2 rte_eal_mem_init-> rte_eal_hugepage_attach
/*
* This creates the memory mappings in the secondary process to match that
* The server process. It goes through each memory segment in the dpdk Runtime
* Configuration and finds the hugepages which form that segment, mapping them
* In order to form a contiguous block in the virtual memory space
*/
3 rte_malloc
4 rte_ring
5 rte_mempool
Original link http://www.cnblogs.com/jintianfree/p/4018043.html
Dpdk Memory Management (1)