1Make and GprofProf. David AugustCOS 2172Goals of Today’s Lecture• Overview of two important programming toolso Make for compiling and linking multi-file programso Gprof for profiling to identify slow parts of the code•Makeo Overview of compilation processo Motivation for using Makefileso Example Makefile, refined in five steps• Gprofo Timing, instrumenting, and profilingo GNU Performance Profiler (Gprof)o Running gprof and understanding the output3Example of a Three-File Program• Program divided into three fileso intmath.h: interface, included in intmath.c and testintmath.co intmath.c: implementation of math functionso testintmath.c: implementation of tests of the math functions• Creating the testintmath binary executableintmath.h intmath.ctestintmath.ctestintmathgcc –Wall –ansi –pedantic –o testintmath testintmath.c intmath.c4Many Steps, Under the Hood• Preprocessing (gcc –E intmath.c > intmath.i)o Removes preprocessor directiveso Produces intmath.i and testintmath.i• Compiling (gcc –S intmath.i)o Converts to assembly languageo Produces intmath.s and testintmath.s• Assembling (gcc –c intmath.s)o Converts to machine language with unresolved directiveso Produces the intmath.o and testintmath.o binaries•Linking(gcc –o testintmath testintmath.ointmath.o –lc)o Creates machine language exectutableo Produces the testintmath binary5Motivation for Makefiles• Typing at command-line gets tediouso Long command with compiler, flags, and file nameso Easy to make a mistake • Compiling everything from scratch is time-consumingo Repeating preprocessing, compiling, assembling, and linkingo Repeating these steps for every file, even if just one has changed• UNIX Makefile toolo Makefile: file containing information necessary to build a program– Lists the files as well as the dependencieso Recompile or relink only as necessary– When a dependent file has changed since command was run– E.g. if intmath.c changes, recompile intmath.c but not testintmath.co Simply type “make”, or “make –f <makefile_name>”6Main Ingredients of a Makefile• Group of lineso Target: the file you want to createo Dependencies: the files on which this file dependso Command: what to execute to create the file (after a TAB)• Examplestestintmath: testintmath.o intmath.ogcc –o testintmath testintmath.o intmath.ointmath.o: intmath.c intmath.hgcc -Wall -ansi -pedantic -c -o intmath.o intmath.c7Complete Makefile #1testintmath: testintmath.o intmath.ogcc –o testintmath testintmath.o intmath.otestintmath.o: testintmath.c intmath.hgcc -Wall -ansi -pedantic -c -o testintmath.o testintmath.cintmath.o: intmath.c intmath.hgcc -Wall -ansi -pedantic -c -o intmath.o intmath.c• Three groupso testintmath: link testintmath.o and intmath.oo testintmath.o: compile testintmath.c, which depends on intmath.ho intmath.o: compile intmath.c, which depends on intmath.h8Adding Non-File Targets• Adding useful shortcuts for the programmero “make all”: create the final binaryo “make clobber”: delete all temp files, core files, binaries, etc.o “make clean”: delete all binaries• Commands in the exampleo “rm –f”: remove files without querying the usero Files ending in ‘~’ and starting/ending in ‘#”’ are temporary fileso “core” is a file produced when a program “dumps core”all: testintmathclobber: cleanrm -f *~ \#*\# coreclean:rm -f testintmath *.o9Complete Makefile #2# Build rules for non-file targetsall: testintmathclobber: cleanrm -f *~ \#*\# coreclean:rm -f testintmath *.o# Build rules for file targetstestintmath: testintmath.o intmath.ogcc –o testintmath testintmath.o intmath.otestintmath.o: testintmath.c intmath.hgcc -Wall -ansi -pedantic -c -o testintmath.o testintmath.cintmath.o: intmath.c intmath.hgcc -Wall -ansi -pedantic -c -o intmath.o intmath.c10Useful Abbreviations• Abbreviationso Target file: $@o First item in the dependency list: $<•Exampletestintmath: testintmath.o intmath.ogcc –o testintmath testintmath.o intmath.otestintmath: testintmath.o intmath.ogcc –o $@ $< intmath.o11Complete Makefile #3# Build rules for non-file targetsall: testintmathclobber: cleanrm -f *~ \#*\# coreclean:rm -f testintmath *.o# Build rules for file targetstestintmath: testintmath.o intmath.ogcc –o $@ $< intmath.otestintmath.o: testintmath.c intmath.hgcc -Wall -ansi -pedantic -c -o $@ $<intmath.o: intmath.c intmath.hgcc -Wall -ansi -pedantic -c -o $@ $<12Useful Pattern Rules: Wildcard %• Can define a default behavioro Build rule: gcc -Wall -ansi -pedantic -c -o $@ $<o Applied when target ends in “.o” and dependency in “.c”• Can omit command clause in build rules (even some rules!)%.o: %.cgcc -Wall -ansi -pedantic -c -o $@ $<testintmath: testintmath.o intmath.ogcc –o $@ $< intmath.otestintmath.o: testintmath.c intmath.hintmath.o: intmath.c intmath.h13Macros for Compiling and Linking• Make it easy to change which compiler is usedo Macro: CC = gcco Usage: $(CC) -o $@ $< intmath.o• Make it easy to change the compiler flagso Macro: CFLAGS = -Wall -ansi –pedantico Usage: $(CC) $(CFLAGS) -c -o $@ $<CC = gcc# CC = gccmemstatCFLAGS = -Wall -ansi -pedantic# CFLAGS = -Wall -ansi -pedantic -g# CFLAGS = -Wall -ansi -pedantic -DNDEBUG# CFLAGS = -Wall -ansi -pedantic -DNDEBUG -O314Sequence of Makefiles (see Web)1. Initial Makefile with file targetstestintmath, testintmath.o, intmath.o2. Adding non-file targets all, clobber, and clean3. Adding abbreviations $@ and $<4. Adding pattern rules %.o: %.c5. Adding macrosCC and CFLAGS15References on Makefiles• Brief discussion in the King booko Section 15.4 (pp. 320-322)• GNU makeo http://www.gnu.org/software/make/manual/html_mono/make.html• Cautionary noteso Don’t forget to use a TAB character, rather than blankso Be careful with how you use the “rm –f” command16Timing, Instrumenting, Profiling• How slow is the code?o How long does it take for certain types of inputs?• Where is the code slow?o Which code is being executed most?• Why is the code running out of memory?o Where is the memory going?o Are there leaks?• Why is the code slow?o How imbalanced is my hash table or binary tree?ProgramOutputInput17Timing• Most shells provide tool to time program executiono E.g., bash “time” command• Breakdown of timeo Real: elapsed time between invocation and terminationo User: time spent executing the programo System: time spent within the OS on the program’s behalf• But, which parts
View Full Document