This application is based upon and claims the benefit of priority from Japanese Patent Application No. 2012-068192, filed on Mar. 23, 2012; the entire contents of which are incorporated herein by reference.
Embodiments described herein relate generally to a memory management method and an information processing device.
A processor that includes a memory management unit (MMU) supporting only a single page size consumes a lot of translation look-aside buffer (TLB) entries for a bunch of memory regions involving consecutive addresses. As a result, a TLB miss occurs, and a performance of the processor is degraded. Accordingly, a recent MMU supports a plurality of page sizes.
In general, according to one embodiment, a memory management method implemented by a computer includes managing each block of a memory region included in the computer based on a buddy allocation algorithm. The method includes managing a correspondence relation between a virtual address and a physical address of one block using one entry of a page table. Each block has a size of a super page. The method includes allocating an empty first block to a process so that the number of empty blocks does not exceed the number of empty entries of a translation look-aside buffer (TLB).
Exemplary embodiments of a memory management method and an information processing device will be explained below in detail with reference to the accompanying drawings. The present invention is not limited to the following embodiments.
The memory 12 stores a kernel program 15 in advance. Further, the memory 12 includes a memory region 20 that may be allocated to a process. The kernel program 15 (hereinafter, simply referred to as a kernel 15) manages the core 10. The kernel 15 is executed by the core 10, and allocates a portion of the memory region 20 or the entire memory region 20 to a process executed in the core 10. Here, the process refers to the memory region 20 by using a virtual address. The kernel 15 registers a virtual address, paired with a physical address of the memory 12, of a region allocated to a process in a page table 16 when performing a memory allocation. Hereinafter, a registration of an entry in the page table 16 is simply referred to as mapping.
The MMU 11 is a unit that processes access from the core 10 to the memory 12. The MMU 11 includes a TLB 14 that caches a predetermined number of entries in the page table 16. When an entry related to a virtual address requested from the core 10 is cached in the TLB 14, the MMU 11 exchanges the virtual address for a physical address using the entry, and accesses the physical address acquired through the exchange. When an entry, related to a virtual address, required from the TLB 14 is not cached in the TLB 14, that is, when a TLB miss occurs, the MMU 11 searches for the entry with reference to the page table 16. In this way, since the TLB miss entails a process of referring to the page table 16, it is preferable that a TLB miss be reduced as possible. Here, all entries of the TLB 14 are switched concurrently with a switch of a process executed by the core 10.
Further, the kernel 15 manages the memory region 20. Here, a buddy system (buddy allocation algorithm) is employed as a memory management algorithm. According to the buddy system, all empty pages are managed as a block constructed by pages of which the number is consecutive powers of two. When consecutive pages are requested to be allocated from a process, the kernel 15 rounds up the number of pages to be allocated so that the number of requested pages is a power of two. Then, the kernel 15 searches for a block corresponding to the number of pages that is rounded up. When a block of a size to be allocated is found, the buddy system allocates all pages within the block to a user. When the block is not found, the kernel 15 finds a block of a relatively large size, and divides the block into two blocks of the same size. The two blocks of the same size generated as described in the foregoing are referred to as mutual buddy blocks. The kernel 15 selects one of the mutual buddy blocks, continues division until a block size becomes a size corresponding to the number of pages to be allocated, and allocates all pages included in the block to a process when a size of the block generated through the division matches the size corresponding to the number of pages to be allocated. When deallocating an allocated page, the kernel 15 combines empty buddy blocks, and merges the empty buddy blocks into a block of a double size. It is determined that two empty blocks are mutual buddy blocks when the following three conditions are satisfied.
(1) Two blocks have the same size.
(2) Two blocks are consecutive in a physical address space.
(3) A beginning address of a block formed by combining two blocks is aligned by a size of the block formed by combining two blocks.
Here, in the embodiment, the MMU 11, the TLB 14, and the page table 16 support a plurality of page sizes. In other words, each entry constituting the TLB 14 and the page table 16 indicates a region of a size which is a power of two times a page size. In this way, a block that is under control of the buddy system may be designated by a single entry rather than a plurality of entries for each page, and thus entry consumption of the TLB 14 may be reduced. As a result, a TLB miss may be decreased. Hereinafter, a region of a size greater than that of a base page (here, a region of a size which is a power of two times a page size) is referred to as a super page. Further, a region of a page size may be referred to as a base page. Hereinafter, it is presumed that a page includes not only a base page but also a super page. A beginning address of a page indicated by each entry is aligned by a size of the page.
Here, a technology compared with the embodiment of the invention (hereinafter, referred to as a technology according to a comparative example) is described.
However, according to the technology related to the comparative example, the kernel determines whether to reserve consecutive regions corresponding to a super page based on whether it is more likely to access most of reserved consecutive regions later. When it is determined that a reserve is not necessary, the kernel performs a mapping in a base page (or a small super page) as before.
In the embodiment, to prevent the TLB 14 from overflowing, the kernel 15 performs a reservation and a mergence of consecutive regions corresponding to a super page based on the number of empty entries of the TLB 14.
When the block of a size greater than or equal to the size to be allocated is present (Yes in S2), the kernel 15 determines whether a block of a size equal to the size to be allocated is present (S3). When the block of a size equal to the size to be allocated is absent (No in S3), the kernel 15 determines whether the total number of empty blocks is equal to the number of empty entries of the TLB 14 (S4). When the total number of empty blocks is not equal to the number of empty entries of the TLB 14 (No in S4), that is, when the total number of empty blocks is smaller than the number of empty entries of the TLB 14, the kernel 15 divides the smallest block among blocks of a size greater than the size to be allocated in accordance with the rule of the buddy system (S5). Then, the kernel 15 performs the determining process of S3 again.
When an empty block is divided, the total number of empty blocks exceeds the number of empty entries of the TLB 14. Thus, when the entire blocks are allocated to the same process thereafter, a TLB miss may occur. Therefore, when the total number of empty blocks is equal to the number of empty entries of the TLB 14 (Yes in S4), the kernel 15 sets the smallest block among blocks of a size greater than the size to be allocated to a reserved region (S6).
In this way, since the kernel 15 divides an empty block so that the number of empty blocks does not exceed the number of empty entries of the TLB 14, it is guaranteed that the number of entries of the TLB 14 corresponding to a memory region allocated to a process does not exceed the maximum number of entries of the TLB. Further, when the total number of empty blocks is less than the number of empty entries of the TLB 14, the kernel 15 divides an empty block.
Subsequently, the kernel 15 determines whether a set reserved region may be merged with an adjacent memory region that is being used (S7). The kernel 15 determines that two memory regions (a reserved region and an adjacent memory region that is being used) may be merged into a memory region when all of the three conditions below are satisfied, and determines that it is difficult to merge memory regions when at least one of the three conditions is not satisfied.
(4) Two memory regions are adjacent to each other in both of a virtual address space and a physical address space.
(5) Two memory regions have the same size.
(6) A virtual address and a physical address at a beginning of a super page after a mergence are concurrently aligned by a size of the super page after the mergence.
When two memory regions may be merged together (Yes in S7), the kernel 15 merges the two memory regions together to perform a remapping as a memory region of a super page (S8). Then, the kernel 15 allocates a memory region of a super page generated through the remapping to a user (S9), and ends the operation. When two memory regions may not be merged together (No in S7), the kernel 15 allocates a reserved region to a user in S9, and ends the operation. Here, when a process of S9 is performed after undergoing the No process of S7, the kernel 15 maps the reserved region.
When the kernel 15 reserves consecutive regions, available memory regions decrease by the amount of reservation, and thus a shortage of memory regions may occur at a stage. In this instance, the kernel 15 needs to collect a page frame that is not being used.
First, the kernel 15 determines whether a memory region that is not being used (accessed or mapped) is present within a reserved memory region (S21). When a memory region that is not being used is absent (No in S21), the kernel 15 ends the operation.
When a memory region that is not being used is present within the reserved region (Yes in S21), the kernel 15 determines whether there is room for the number of empty entries of the TLB 14 (S22). In particular, the kernel 15 determines whether the total number of empty blocks is a value greater than or equal to the number of empty entries of the TLB 14 when the reserved region is divided in accordance with the process of S23 to be described below. When there is room for the number of empty entries of the TLB 14 (Yes in S22), the kernel 15 divides the reserved region in accordance with a rule of a buddy system, collects a region, as an empty block, which is not being used in the reserved region, and remaps a memory region that is being used in the reserved region (S23). When a region that is not being used is absent within the reserved region (No in S21), or when there is no room for the number of empty blocks of the TLB 14 (No in S22), the kernel 15 ends the operation.
Here, the kernel 15 may perform the operation of
Here, in the description above, description has been made on the assumption that the information processing device 1 includes one core 10. However, in the embodiment of the invention, an information processing device including a plurality of cores 10 may be applied.
An information processing device 1 illustrated in
In this way, according to the embodiment of the invention, the kernel 15 allocates an empty block to a process so that the number of empty blocks does not exceed the number of empty entries of the TLB 14, and thus it is possible to prevent a TLB miss from occurring.
Further, when a process requests a memory region to be allocated, the kernel 15 divides an empty block so that the number of empty blocks does not exceed the number of empty entries of a TLB to generate an empty block to be allocated (S3 to S6), allocates the empty block to be allocated to the process, and registers an entry describing a correspondence relation between a virtual address and a physical address related to the allocated block in a page table (S9). Accordingly, it is guaranteed that the number of empty blocks does not exceed the number of empty entries of the TLB 14.
Furthermore, the kernel 15 rounds up a size of a memory region requested from a process to be a base page size times a power of two to calculate a first size (S1), determines an empty block of the first size to be an empty block to be allocated (S9) when the empty block of the first size is present (Yes in S3), and determines an empty block of a second size that is greater than the first size to be an empty block to be allocated (S6 and S9) when the empty block of the first size is absent (No in S3), and the total number of empty blocks is equal to the number of empty entries of the TLB (Yes in S4). Accordingly, it is guaranteed that the number of empty blocks does not exceed the number of empty entries of the TLB 14.
Moreover, when the empty block of the first size is absent (No in S3), and the total number of empty blocks is smaller than the number of empty entries of the TLB (No in S4), the kernel 15 divides an empty block of the smallest size (S5). Accordingly, it is guaranteed that the number of empty blocks does not exceed the number of empty entries of the TLB 14.
Further, when an empty block to be allocated may be merged with a block allocated to a process (Yes in S7), the kernel 15 merges the empty block to be allocated with the allocated block to arrange an entry of the TLB (S8). Accordingly, the number of empty blocks may be increased as possible.
Furthermore, when a block of the second size among blocks allocated to a process includes a memory region that is not being used by the process (Yes in S21), the kernel 15 divides a block of the second size so that the number of empty blocks does not exceed the number of empty entries of the TLB, and the memory region that is not being used by the process becomes an empty block, and updates the TLB (S23). Accordingly, it is possible to efficiently use a memory region while inhibiting an occurrence of a TLB miss.
While certain embodiments have been described, these embodiments have been presented by way of example only, and are not intended to limit the scope of the inventions. Indeed, the novel embodiments described herein may be embodied in a variety of other forms; furthermore, various omissions, substitutions and changes in the form of the embodiments described herein may be made without departing from the spirit of the inventions. The accompanying claims and their equivalents are intended to cover such forms or modifications as would fall within the scope and spirit of the inventions.
Number | Date | Country | Kind |
---|---|---|---|
2012-068192 | Mar 2012 | JP | national |