📄 kernmalloc.t
字号:
.\" Copyright (c) 1988 The Regents of the University of California..\" All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\" notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\" notice, this list of conditions and the following disclaimer in the.\" documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\" must display the following acknowledgement:.\" This product includes software developed by the University of.\" California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\" may be used to endorse or promote products derived from this software.\" without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\" @(#)kernmalloc.t 5.1 (Berkeley) 4/16/91.\".\" reference a system routine name.de RN\fI\\$1\fP\^(\h'1m/24u')\\$2...\" reference a header name.de H.NH \\$1\\$2...\" begin figure.\" .FI "title".nr Fn 0 1.de FI.ds Lb Figure \\n+(Fn.ds Lt \\$1.KF.DS B.nf...\".\" end figure.de Fe.sp .5.\" cheat: original indent is stored in \n(OI by .DS B; restore it.\" then center legend after .DE rereads and centers the block.\\\\.in \\n(OI\\\\.ce\\\\*(Lb. \\\\*(Lt.sp .5.DE.KE.if \nd 'ls 2...EQdelim $$.EN.ds CH ".pn 295.sp.rs.ps -1.sp -1.fiReprinted from:\fIProceedings of the San Francisco USENIX Conference\fP,pp. 295-303, June 1988..ps.\".sp |\n(HMu.rm CM.nr PO 1.25i.TLDesign of a General Purpose Memory Allocator for the 4.3BSD UNIX\(dg Kernel.ds LF Summer USENIX '88.ds CF "%.ds RF San Francisco, June 20-24.EH 'Design of a General Purpose Memory ...''McKusick, Karels'.OH 'McKusick, Karels''Design of a General Purpose Memory ...'.FS\(dgUNIX is a registered trademark of AT&T in the US and other countries..FE.AUMarshall Kirk McKusick.AUMichael J. Karels.AIComputer Systems Research GroupComputer Science DivisionDepartment of Electrical Engineering and Computer ScienceUniversity of California, BerkeleyBerkeley, California 94720.ABThe 4.3BSD UNIX kernel uses many memory allocation mechanisms,each designed for the particular needs of the utilizing subsystem.This paper describes a general purpose dynamic memory allocatorthat can be used by all of the kernel subsystems.The design of this allocator takes advantage of known memory usagepatterns in the UNIX kernel and a hybrid strategy that is time-efficientfor small allocations and space-efficient for large allocations.This allocator replaces the multiple memory allocation interfaces with a single easy-to-program interface,results in more efficient use of global memory by eliminatingpartitioned and specialized memory pools,and is quick enough that no performance loss is observedrelative to the current implementations.The paper concludes with a discussion of our experience in usingthe new memory allocator,and directions for future work..AE.LP.H 1 "Kernel Memory Allocation in 4.3BSD.PPThe 4.3BSD kernel has at least ten different memory allocators.Some of them handle large blocks,some of them handle small chained data structures,and others include information to describe I/O operations.Often the allocations are for small pieces of memory that are onlyneeded for the duration of a single system call.In a user process such short-termmemory would be allocated on the run-time stack.Because the kernel has a limited run-time stack,it is not feasible to allocate even moderate blocks of memory on it.Consequently, such memory must be allocated through a more dynamic mechanism.For example,when the system must translate a pathname,it must allocate a one kilobye buffer to hold the name.Other blocks of memory must be more persistent than a single system calland really have to be allocated from dynamic memory.Examples include protocol control blocks that remain throughoutthe duration of the network connection..PPDemands for dynamic memory allocation in the kernel have increasedas more services have been added.Each time a new type of memory allocation has been required,a specialized memory allocation scheme has been written to handle it.Often the new memory allocation scheme has been built on topof an older allocator.For example, the block device subsystem provides a crude form ofmemory allocation through the allocation of empty buffers [Thompson78].The allocation is slow because of the implied semantics offinding the oldest buffer, pushing its contents to disk if they are dirty,and moving physical memory into or out of the buffer to create the requested size.To reduce the overhead, a ``new'' memory allocator was built in 4.3BSDfor name translation that allocates a pool of empty buffers.It keeps them on a free list so they canbe quickly allocated and freed [McKusick85]..PPThis memory allocation method has several drawbacks.First, the new allocator can only handle a limited range of sizes.Second, it depletes the buffer pool, as it steals memory intendedto buffer disk blocks to other purposes.Finally, it creates yet another interface ofwhich the programmer must be aware..PPA generalized memory allocator is needed to reduce the complexityof writing code inside the kernel.Rather than providing many semi-specialized ways of allocating memory,the kernel should provide a single general purpose allocator.With only a single interface, programmers do not need to figureout the most appropriate way to allocate memory.If a good general purpose allocator is available,it helps avoid the syndrome of creating yet another specialpurpose allocator..PPTo ease the task of understanding how to use it,the memory allocator should have an interface similar to the interfaceof the well-known memory allocator provided forapplications programmers through the C library routines.RN mallocand.RN free .Like the C library interface,the allocation routine should take a parameter specifying thesize of memory that is needed.The range of sizes for memory requests should not be constrained.The free routine should take a pointer to the storage being freed,and should not require additional information such as the sizeof the piece of memory being freed..H 1 "Criteria for a Kernel Memory Allocator.PPThe design specification for a kernel memory allocator is similar to,but not identical to,the design criteria for a user level memory allocator.The first criterion for a memory allocator is that it make good useof the physical memory.Good use of memory is measured by the amount of memory needed to holda set of allocations at any point in time.Percentage utilization is expressed as:.EQutilization~=~requested over required.ENHere, ``requested'' is the sum of the memory that has been requestedand not yet freed.``Required'' is the amount of memory that has beenallocated for the pool from which the requests are filled.An allocator requires more memory than requested because of fragmentationand a need to have a ready supply of free memory for future requests.A perfect memory allocator would have a utilization of 100%.In practice,having a 50% utilization is considered good [Korn85]..PPGood memory utilization in the kernel is more important thanin user processes.Because user processes run in virtual memory,unused parts of their address space can be paged out.Thus pages in the process address spacethat are part of the ``required'' pool that are notbeing ``requested'' need not tie up physical memory.Because the kernel is not paged,all pages in the ``required'' pool are held by the kernel andcannot be used for other purposes.To keep the kernel utilization percentage as high as possible,it is desirable to release unused memory in the ``required'' poolrather than to hold it as is typically done with user processes.Because the kernel can directly manipulate its own page maps,releasing unused memory is fast;a user process must do a system call to release memory..PPThe most important criterion for a memory allocator is that it be fast.Because memory allocation is done frequently,a slow memory allocator will degrade the system performance.Speed of allocation is more critical when executing in thekernel than in user code,because the kernel must allocate many data structure that userprocesses can allocate cheaply on their run-time stack.In addition, the kernel represents the platform on which all userprocesses run,and if it is slow, it will degrade the performance of every processthat is running..PPAnother problem with a slow memory allocator is that programmersof frequently-used kernel interfaces will feel that theycannot afford to use it as their primary memory allocator. Instead they will build their own memory allocator on top of theoriginal by maintaining their own pool of memory blocks.Multiple allocators reduce the efficiency with which memory is used.The kernel ends up with many different free lists of memoryinstead of a single free list from which all allocation can be drawn.For example,consider the case of two subsystems that need memory.If they have their own free lists,the amount of memory tied up in the two lists will be thesum of the greatest amount of memory that each ofthe two subsystems has ever used.If they share a free list,the amount of memory tied up in the free list may be as low as thegreatest amount of memory that either subsystem used.As the number of subsystems grows,the savings from having a single free list grow..H 1 "Existing User-level Implementations.PPThere are many different algorithms andimplementations of user-level memory allocators.A survey of those available on UNIX systems appeared in [Korn85].Nearly all of the memory allocators tested made good use of memory, though most of them were too slow for use in the kernel.The fastest memory allocator in the survey by nearly a factor of twowas the memory allocator provided on 4.2BSD originallywritten by Chris Kingsley at California Institute of Technology.Unfortunately,the 4.2BSD memory allocator also wasted twice as much memoryas its nearest competitor in the survey..PPThe 4.2BSD user-level memory allocator works by maintaining a set of liststhat are ordered by increasing powers of two.Each list contains a set of memory blocks of its corresponding size.To fulfill a memory request, the size of the request is rounded up to the next power of two.A piece of memory is then removed from the list correspondingto the specified power of two and returned to the requester.Thus, a request for a block of memory of size 53 returnsa block from the 64-sized list.A typical memory allocation requires a roundup calculationfollowed by a linked list removal.Only if the list is empty is a real memory allocation done.The free operation is also fast;the block of memory is put back onto the list from which it came.The correct list is identified by a size indicator storedimmediately preceding the memory block..H 1 "Considerations Unique to a Kernel Allocator.PPThere are several special conditions that arise when writing amemory allocator for the kernel that do not apply to a user processmemory allocator.First, the maximum memory allocation can be determined at the time that the machine is booted.This number is never more than the amount of physical memory on the machine,and is typically much less since a machine with all itsmemory dedicated to the operating system is uninteresting to use.Thus, the kernel can statically allocate a set of data structuresto manage its dynamically allocated memory.These data structures never need to beexpanded to accommodate memory requests;yet, if properly designed, they need not be large.For a user process, the maximum amount of memory that may be allocatedis a function of the maximum size of its virtual memory.Although it could allocate static data structures to manageits entire virtual memory,even if they were efficiently encoded they would potentially be huge.The other alternative is to allocate data structures as they are needed.However, that adds extra complications such as newfailure modes if it cannot allocate space for additionalstructures and additional mechanisms to link them all together..PPAnother special condition of the kernel memory allocator is that itcan control its own address space.Unlike user processes that can only grow and shrink their heap at one end,the kernel can keep an arena of kernel addresses and allocatepieces from that arena which it then populates with physical memory.The effect is much the same as a user process that has parts ofits address space paged out when they are not in use,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -