📄 mprof.tex
字号:
% Master File: mprof.tex% Document type: LaTeX\documentstyle[11pt,captions,doublespace,twoside]{article}\input{defs}\setstretch{1}\fullpagestyle\begin{document}\title{A Memory Allocation Profiler for C and Lisp Programs\footnotetext{This researchwas funded by DARPA contract number N00039-85-C-0269 as part of theSPUR research project.}}\author{Benjamin Zorn \\ Department of Computer Science \\ University of Colorado at Boulder \\ \\ Paul Hilfinger \\ Computer Science Division, Dept. of EECS \\ University of California at Berkeley \\ }\date{}\maketitle\begin{singlespace}\begin{abstract} This paper describes {\tt mprof}, a tool used to study the dynamicmemory allocation behavior of programs. {\tt Mprof} records theamount of memory that a function allocates, breaks down allocationinformation by type and size, and displays a program's dynamic callgraph so that functions indirectly responsible for memory allocationare easy to identify. {\tt Mprof} is a two-phase tool. The monitorphase is linked into executing programs and records information eachtime memory is allocated. The display phase reduces the datagenerated by the monitor and presents the information to a user inseveral tables. {\tt Mprof} has been implemented for C and KyotoCommon Lisp. Measurements of these implementations are presented.\end{abstract}\end{singlespace}\section{Introduction}\setlength{\parskip}{6pt plus 2pt}Dynamic memory allocation, hereafter referred to simply as memoryallocation, is an important part of many programs. By dynamicallocation, we mean the memory allocated from the heap. Unnecessaryallocation can decrease program locality and increase execution timefor the allocation itself and for possible memory reclamation. Ifreclamation is not performed, or if some objects are accidently notreclaimed (a ``memory leak''), programs can fail when they reach thememory size limit. Programmers often write their own versions ofmemory allocation routines to measure and reduce allocation overhead.It is estimated that Mesa programmers spent 40\% of their time solvingstorage-management related problems before automatic storagereclamation techniques were introduced in Cedar~\cite{rovner85:gc}.Even with automatic storage management, in which reclamation occurstransparently, memory allocation has a strong effect on theperformance of programs~\cite{moon84:gc}. Although memory allocationis important, few software tools exist to help programmers understandthe memory allocation behavior of their programs.{\tt Mprof} is a tool that allows programmers to identify where andwhy dynamic memory is allocated in a program. It records whichfunctions are directly responsible for memory allocation and alsorecords the dynamic call chain at an allocation to show whichfunctions were indirectly responsible for the allocation. The designof {\tt mprof} was inspired by the tool {\tt gprof}, a dynamicexecution profiler~\cite{graham83:gprof}. {\tt gprof} is a veryuseful tool for understanding the execution behavior of programs; {\ttmprof} extends the ideas of {\tt gprof} to give programmersinformation about the dynamic memory allocation behavior of theirprograms. {\tt Mprof} is a two-phase tool. A monitor phase is linkedinto an executing application and collects data as the applicationexecutes. A reduction and display phase takes the data collected bythe monitor and presents it to the user in concise tables.A profiling program such as {\tt mprof} should satisfy severalcriteria. First, a program monitor should not significantly alter thebehavior of the program being monitored. In particular, a monitorshould not impose so much overhead on the program being monitored thatlarge programs cannot be profiled. Second, a monitor should be easyto integrate into existing applications. To use {\tt mprof},programmers simply have to relink their applications with a specialversion of the system library. No source code modifications arerequired. Finally, a monitor should provide a programmer withinformation that he can understand and use to reduce the memoryallocation overhead of his programs. We will present an example thatillustrates such a use of {\tt mprof}.In Section \ref{using mprof}, we present a simple program anddescribe the use of {\tt mprof} with respect to the example. InSection \ref{implementing mprof} we discuss techniques for theeffective implementation of {\tt mprof} . Section \ref{measurements}presents some measurements of {\tt mprof}. Section \ref{related work}describes other memory profiling tools and previous work on which {\ttmprof} is based, while Section \ref{conclusions} contains ourconclusions.\section{Using {\tt mprof}}\label{using mprof}\subsection{A Producer/Consumer Example}To illustrate how {\tt mprof} helps a programmer understand the memoryallocation in his program, consider the C program in Figure~\ref{Cexample figure}. In this program, a simplified producer/consumersimulation, objects are randomly allocated by two producers and freedby the consumer. The function {\tt random\_flip}, which is not shown,randomly returns 1 or 0 with equal probability. The function {\ttconsume\_widget}, which is responsible for freeing the memoryallocated, contains a bug and does not free red widgets. If thesimulation ran for a long time, memory would eventually be exhausted,and the program would fail.\begin{figure}[htbp]\begin{singlespace}{\footnotesize\include{expr}}\end{singlespace}\caption{A Simple Producer/Consumer Simulation Program}\label{C example figure}\end{figure}In the example, {\tt make\_widget} is the only function that allocatesmemory directly. To fully understand the allocation behavior of theprogram, we must know which functions called {\tt make\_widget} andhence were indirectly responsible for memory allocation. {\tt Mprof}provides this information.To use {\tt mprof}, programmers link in special versions of the systemfunctions {\tt malloc} and {\tt free}, which are called each timememory is allocated and freed, respectively. The application is thenrun normally. The {\tt mprof} monitor function, linked in with {\ttmalloc}, gathers statistics as the program runs and writes thisinformation to a file when the application exits. The programmer thenruns a display program over the data file, and four tables areprinted: a list of memory leaks, an allocation bin table, a directallocation table, and a dynamic call graph. Each table presents theallocation behavior of the program from a different perspective. Therest of this section describes each of the tables for the C program inFigure~\ref{C example figure}.% Fields in the tables are described in detail in the appendix.\subsection{The Memory Leak Table}C programmers must explicitly free memory objects when they are doneusing them. Memory leaks arise when programmers accidently forget torelease memory. Because Lisp reclaims memory automatically, thememory leak table is not necessary in the Lisp version of {\tt mprof}.The memory leak table tells a programmer which functions allocated thememory associated with memory leaks. The table contains a list ofpartial call paths that resulted in memory that was allocated and notsubsequently freed. The paths are partial because complete pathinformation is not recorded; only the last five callers on the callstack are listed in the memory leak table. In our simple example,there is only one such path, and it tells us immediately what objectsare not freed. Figure~\ref{memory leak figure} contains the memoryleak table for our example.\begin{figure}[htbp]\begin{singlespace}{\include{extable0a}}\end{singlespace}\caption{Memory Leak Table for Producer/Consumer Example}\label{memory leak figure}\end{figure}In larger examples, more than one path through a particular functionis possible. We provide an option that distinguishes individual callsites within the same function in the memory leak table if such adistinction is needed.\subsection{The Allocation Bin Table}A major part of understanding the memory allocation behavior of aprogram is knowing what objects were allocated. In C, memoryallocation is done by object size; the type of object being allocatedis not known at allocation time. The allocation bin table providesinformation about what sizes of objects were allocated and whatprogram types correspond to the sizes listed. This knowledge helps aprogrammer recognize which data structures consume the most memory andallows him to concentrate any space optimizations on them.The allocation bin table breaks down object allocation by the size, inbytes, of allocated objects. Figure~\ref{allocation bin figure} showsthe allocation bin table for the program in Figure~\ref{C examplefigure}.\begin{figure}[htbp]\begin{singlespace}{\include{extable0}}\end{singlespace}\caption{Allocation Bin Table for Producer/Consumer Example}\label{allocation bin figure}\end{figure}The allocation bin table contains information about objects of eachbyte size from 0 to 1024 bytes and groups objects larger than 1024bytes into a single bin. For each byte size in which memory wasallocated, the allocation bin table shows the number of allocations ofthat size ({\tt allocs}), the total number of bytes allocated toobjects of that size ({\tt bytes}), the number of frees of objects ofthat size ({\tt frees}), the number of bytes not freed that wereallocated to objects of that size ({\tt kept}\footnote{The label {\ttkept} is used throughout the paper to refer to objects that were neverfreed.}), and user types whose size is the same as the bin size ({\tttypes}). From the example, we can see that 10,000 widgets wereallocated by the program, but only 4,981 of these were eventuallyfreed, resulting in 1,023,876 bytes of memory lost to the memory leak.The percentages show what fraction of all bins a particular bincontributed. This information is provided to allow a user to rapidlydetermine which bins are of interest (i.e., contribute a substantialpercentage). 99\% is the largest percentage possible because we choseto use a 2 character field width.\subsection{The Direct Allocation Table}Another facet of understanding memory allocation is knowing whichfunctions allocated memory and how much they allocated. In C, memoryallocation is performed explicitly by calling {\tt malloc}, and soprogrammers are often aware of the functions that allocate memory.Even in C, however, knowing how much memory was allocated can pointout functions that do unnecessary allocation and guide the programmerwhen he attempts to optimize the space consumption of his program. InLisp, memory allocation happens implicitly in many primitive routinessuch as {\tt mapcar}, {\tt *}, and {\tt intern}. The direct allocationtable can reveal unsuspected sources of allocation to Lispprogrammers.Figure~\ref{direct allocation figure} contains the direct allocationtable for our example. The direct allocation table corresponds to theflat profile generated by {\tt gprof}.\begin{figure}[htbp]\begin{singlespace}{\scriptsize\include{extable1}}\end{singlespace}\caption{Direct Allocation Table for Producer/Consumer Example}\label{direct allocation figure}\end{figure}The first line of the direct allocation table contains the totals forall functions allocating memory. In this example, only one function,{\tt make\_widget}, allocates memory. The direct allocation tableprints percent of total allocation that took place in each function({\tt \%~mem}), the number of bytes allocated by each function ({\ttbytes}), the number of bytes allocated by the function and never freed({\tt bytes kept}), and the number of calls made to the function thatresulted in allocation ({\tt calls}). The {\tt \%~mem(size)} fieldscontain a size breakdown\footnote{ Both the direct allocation tableand the dynamic call graph break down object allocation into fourcategories of object size: small (s), from 0--32 bytes; medium (m),from 33--256 bytes; large (l), from 257--2048 bytes; and extra large(x), larger than 2048 bytes. For Lisp, categorization is by type
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -