DOC PREVIEW
UW CSE 303 - Profiling

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentDan GrossmanSpring 2007Lecture 19— Profiling (gprof); Linking and LibrariesDan Grossman CSE303 Spring 2007, Lecture 19 1'&$%Where are weAlready started playing with gprof; some more to learn about:• Effective performance tuning• Limitations of gprof-style profilingThen: Linking code in multiple files (the .o story and the Java storyand more)Friday: societal implications (electronic voting)Next week +: Finish linking, A taste of concurrency, A taste of C++Dan Grossman CSE303 Spring 2007, Lecture 19 2'&$%Using gprof• Compile with -pg on the right.– When you create the .o (for call counts)– When you create the executable (for time samples)• Run the program (creates (overwrites) gmon.out)• Run gprof (on gmon.out) to get human-readable results.• Read the results (takes a little getting used to).Dan Grossman CSE303 Spring 2007, Lecture 19 3'&$%Getting useful info• The information depends on your inputs! (Always know whatyou’re profiling)• Statistical sampling requires a reasonable number of samples– Probably want at very least a few thousand– Can run a program over and over and use gprof -s (learn onyour own; write a shell-script)• Make sure performance matters– Is 10% faster worth uglier or buggier code?– Do you have better things to do (documentation, testing, ...)?Dan Grossman CSE303 Spring 2007, Lecture 19 4'&$%Performance tuning• Never tune until you know the bottleneck (that’s what gprof isfor, but it does n’t tell you how to tune).• Rarely overtune to some inputs at the expense of others.• Always focus on the overall algorithm first.• Think doubly-hard about making non-modular changes.• Focus on low- leve l tricks only if you really need to (< 5 times inyour career?)• See if compiler flags (e.g., -O) are enough.Note: Performance tuning a library is harder because you want to dowell for “unknown programs and inputs”.Dan Grossman CSE303 Spring 2007, Lecture 19 5'&$%Our example• Different bottlenecks for large array-size and large max-number!!– If you knew max-number could never be more than 10, wouldyou optimize is_prime?• Optimal algorithm for is_prime is slower than forfind_largest, but we did not write the optimal algorithms!• After fixing time for find_largest, we s till had a stack overflow.• Changing the is_prime algorithm helped a lot.• Little things (e.g., reordering tests and loops) generally “lost inthe noise”.• Output affects wall-clock time.Note: For more rigorous comparisons, we should not randomly seedthe random-number generator.Dan Grossman CSE303 Spring 2007, Lecture 19 6'&$%Misleading Fact #1Cumulative times are based on call estimation. They can be really,really wrong, but usually aren’t.int g = 0;void c(int i) {if(i) return;for(; i < 100000000; ++i)++g;}void a() { c(0); }void b() { c(1); }int main(int argc,char**argv) { a(); b(); return 0; }Conclusion: You must understand what your profiler measures andwhat it presents to you. gprof doesn’t lie (if you read the manual)Dan Grossman CSE303 Spring 2007, Lecture 19 7'&$%Misleading Fact #2Sampling errors (for time samples) can be caused by too few samples,or by periodic samplingvoid a() { /* takes 0.09 s */ }void b() { /* takes 0.01 s */ }int main(int argc,char**argv) {for(; i < 10000; ++i) {a();b();}}This probably doesn’t happen muc h and be tter profilers can userandom intervals to avoid it.Related fact: Measurement code changes timing (an uncertaintyprinciple).Dan Grossman CSE303 Spring 2007, Lecture 19 8'&$%Poor man’s profilingThe time command is more useful because no measurement overhead,but less useful because you get only whole-program numbers.• real: roughly “wall-clock”• user: time spent running the code in the program• system: time the O/S spent doing things on behalf of the programNot precise for small numbe rsMisleading Fact #3: gprof does not measure system time?Effects on real time: Machine load, disk access, I/OEffects on system time: I/O to screen, file, or /dev/nullDan Grossman CSE303 Spring 2007, Lecture 19 9'&$%Compiler OptimizationCompilers must:• Trade “compile-time” for “code-quality”• Trade “amount of code” for “spe cialization of code”• Make guesses about how code will be used.You can affect the trade-off via “optimization flags” – definitely easierbut less predictable than modifying your code.gcc is not a great optimizer:• For our initial example, it made a big improvement.• No promises; it could slow your program down (unlikely, but Ihave seen it even for the primes program).Bottom line: Remember to “turn optimizations on” if it matters.Dan Grossman CSE303 Spring 2007, Lecture 19 10'&$%Intro to l inki ngLinking is just one example of “using stuff in other files”...In compiling and running code, one constantly needs other files andprograms that find them.Examples:• C preprocessor #include• C libraries (where is the code for printf and malloc)• Java source files (referring to other source code)• Java class files (referring to other compiled code)Usually you’re happy with programs “automatically finding what youneed” so the complicated rules can be hidden.Today we will demystify and make ge neralizations.Dan Grossman CSE303 Spring 2007, Lecture 19 11'&$%Common questions1. What you are looking for?2. When are you looking for it?3. Where are you looking?4. What problems do cycle s c ause?5. How do you change the answers?our old friends: files, function names, paths, environment variables,command-line flags, scripts, configuration files, ...Dan Grossman CSE303 Spring 2007, Lecture 19 12'&$%Previous examplecpp (invoked implicitly by gcc on files ending in .c).What: files named “foo” when encountering #include <foo> or#include "foo" (note .h is just a convention).When: When the preprocessor is run (making x.i from x.c).Where: “include path” current-directory, directories chosen w hen cppis installed (e.g., /usr/include), directories liste d in INCLUDE shellvariable, directories listed via -I flags, ...The rules on “what overrides what” exist, but tough to remember.Can look at result to see “what really happened”.Example: for nes ted #include, the original current-directory or theheader file’s current-directory?Example: Why shouldn’t you run cpp on 1 m achine and compile theresults on another?Dan Grossman CSE303 Spring 2007, Lecture 19 13'&$%javac is similarIf A.java defines class A


View Full Document

UW CSE 303 - Profiling

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Profiling
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Profiling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Profiling 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?