From: Nicholas Nethercote Date: Fri, 7 Aug 2009 00:18:25 +0000 (+0000) Subject: Thoroughly overhauled the Cachegrind manual chapter, mostly by putting X-Git-Tag: svn/VALGRIND_3_5_0~120 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=88a2f8bed1643068f057d311f4324cc5eb854f2a;p=thirdparty%2Fvalgrind.git Thoroughly overhauled the Cachegrind manual chapter, mostly by putting things in a more sensible order. Also tweaked the Massif chapter a bit more. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10730 --- diff --git a/cachegrind/cg_annotate.in b/cachegrind/cg_annotate.in index 31e95069b2..83f348310c 100644 --- a/cachegrind/cg_annotate.in +++ b/cachegrind/cg_annotate.in @@ -146,7 +146,7 @@ usage: cg_annotate [options] output-file [source-files] options for the user, with defaults in [ ], are: -h --help show this message - -v --version show version + --version show version --show=A,B,C only show figures for events A,B,C [all] --sort=A,B,C sort columns by events A,B,C [event column order] --threshold=<0--100> percentage of counts (of primary sort event) we @@ -179,7 +179,7 @@ sub process_cmd_line() if ($arg =~ /^-/) { # --version - if ($arg =~ /^-v$|^--version$/) { + if ($arg =~ /^--version$/) { die("cg_annotate-$version\n"); # --show=A,B,C diff --git a/cachegrind/docs/cg-manual.xml b/cachegrind/docs/cg-manual.xml index fa3ca342ed..90a8d43b22 100644 --- a/cachegrind/docs/cg-manual.xml +++ b/cachegrind/docs/cg-manual.xml @@ -15,30 +15,56 @@ Valgrind command line. Overview Cachegrind simulates how your program interacts with a machine's cache -hierarchy and (optionally) branch predictor. It gathers the following -statistics: +hierarchy and (optionally) branch predictor. It simulates a machine with +independent first level instruction and data caches (I1 and D1), backed by a +unified second level cache (L2). This configuration is used by almost all +modern machines. + + +It gathers the following statistics (abbreviations used for each statistic +is given in parentheses): - L1 instruction cache reads and read misses; + I cache reads (Ir, + which equals the number of instructions executed), + I1 cache read misses (I1mr) and + L2 cache instruction read misses (I1mr). + - L1 data cache reads and read misses, writes and write - misses; + D cache reads (Dr, which + equals the number of memory reads), + D1 cache read misses (D1mr), and + L2 cache data read misses (D2mr). + - L2 unified cache reads and read misses, writes and - writes misses. + D cache writes (Dw, which equals + the number of memory writes), + D1 cache write misses (D1mw), and + L2 cache data write misses (D2mw). + - Conditional branches and mispredicted conditional branches. + Conditional branches executed (Bc) and + conditional branches mispredicted (Bcm). + - Indirect branches and mispredicted indirect branches. An - indirect branch is a jump or call to a destination only known at - run time. + Indirect branches executed (Bi) and + indirect branches mispredicted (Bim). + +Note that D1 total accesses is given by +D1mr + +D1mw, and that L2 total +accesses is given by I2mr + +D2mr + +D2mw. + + These statistics are presented for the entire program and for each function in the program. You can also annotate each line of source code in the program with the counts that were caused directly by it. @@ -54,244 +80,35 @@ to make it faster. instruction executed, you can find out how many instructions are executed per line, which can be useful for traditional profiling. -Branch profiling is not enabled by default. To use it, you must -additionally specify -on the command line. + + - -Basics + +Using Cachegrind, cg_annotate and cg_merge First off, as for normal Valgrind use, you probably want to compile with debugging info (the flag). But by contrast with -normal Valgrind use, you probably do want to turn +normal Valgrind use, you probably do want to turn optimisation on, since you should profile your program as it will be normally run. -The two steps are: - - - Run your program with valgrind - --tool=cachegrind in front of the normal - command line invocation. When the program finishes, - Cachegrind will print summary cache statistics. It also - collects line-by-line information in a file - cachegrind.out.<pid>, where - <pid> is the program's process - ID. - - Branch prediction statistics are not collected by default. - To do so, add the flag - . - - - This step should be done every time you want to collect - information about a new program, a changed program, or about - the same program with different input. - - - - Generate a function-by-function summary, and possibly - annotate source files, using the supplied - cg_annotate program. Source - files to annotate can be specified manually, or manually on - the command line, or "interesting" source files can be - annotated automatically with the - option. You can - annotate C/C++ files or assembly language files equally - easily. - - This step can be performed as many times as you like - for each Step 2. You may want to do multiple annotations - showing different information each time. - - - - -As an optional intermediate step, you can use the supplied -cg_merge program to sum together the -outputs of multiple Cachegrind runs, into a single file which you then -use as the input for cg_annotate. - -These steps are described in detail in the following -sections. - - - - - -Cache simulation specifics - -Cachegrind simulates a machine with independent -first level instruction and data caches (I1 and D1), backed by a -unified second level cache (L2). This configuration is used by almost -all modern machines. Some old Cyrix CPUs had a unified I and D L1 -cache, but they are ancient history now. - -Specific characteristics of the simulation are as -follows: - - - - - Write-allocate: when a write miss occurs, the block - written to is brought into the D1 cache. Most modern caches - have this property. - - - - Bit-selection hash function: the set of line(s) in the cache - to which a memory block maps is chosen by the middle bits - M--(M+N-1) of the byte address, where: - - - line size = 2^M bytes - - - (cache size / line size / associativity) = 2^N bytes - - - - - - Inclusive L2 cache: the L2 cache typically replicates all - the entries of the L1 caches, because fetching into L1 involves - fetching into L2 first (this does not guarantee strict inclusiveness, - as lines evicted from L2 still could reside in L1). This is - standard on Pentium chips, but AMD Opterons, Athlons and Durons - use an exclusive L2 cache that only holds - blocks evicted from L1. Ditto most modern VIA CPUs. - - - - -The cache configuration simulated (cache size, -associativity and line size) is determined automagically using -the x86 CPUID instruction. If you have an machine that (a) -doesn't support the CPUID instruction, or (b) supports it in an -early incarnation that doesn't give any cache information, then -Cachegrind will fall back to using a default configuration (that -of a model 3/4 Athlon). Cachegrind will tell you if this -happens. You can manually specify one, two or all three levels -(I1/D1/L2) of the cache from the command line using the -, - and - options. -For cache parameters to be valid for simulation, the number -of sets (with associativity being the number of cache lines in -each set) has to be a power of two. - -On PowerPC platforms -Cachegrind cannot automatically -determine the cache configuration, so you will -need to specify it with the -, - and - options. - - -Other noteworthy behaviour: - - - - References that straddle two cache lines are treated as - follows: - - - If both blocks hit --> counted as one hit - - - If one block hits, the other misses --> counted - as one miss. - - - If both blocks miss --> counted as one miss (not - two) - - - - - - Instructions that modify a memory location - (eg. inc and - dec) are counted as doing - just a read, ie. a single data reference. This may seem - strange, but since the write can never cause a miss (the read - guarantees the block is in the cache) it's not very - interesting. - - Thus it measures not the number of times the data cache - is accessed, but the number of times a data cache miss could - occur. - - - - -If you are interested in simulating a cache with different -properties, it is not particularly hard to write your own cache -simulator, or to modify the existing ones in -cg_sim.c. We'd be -interested to hear from anyone who does. - - - - - -Branch simulation specifics - -Cachegrind simulates branch predictors intended to be -typical of mainstream desktop/server processors of around 2004. - -Conditional branches are predicted using an array of 16384 2-bit -saturating counters. The array index used for a branch instruction is -computed partly from the low-order bits of the branch instruction's -address and partly using the taken/not-taken behaviour of the last few -conditional branches. As a result the predictions for any specific -branch depend both on its own history and the behaviour of previous -branches. This is a standard technique for improving prediction -accuracy. - -For indirect branches (that is, jumps to unknown destinations) -Cachegrind uses a simple branch target address predictor. Targets are -predicted using an array of 512 entries indexed by the low order 9 -bits of the branch instruction's address. Each branch is predicted to -jump to the same address it did last time. Any other behaviour causes -a mispredict. - -More recent processors have better branch predictors, in -particular better indirect branch predictors. Cachegrind's predictor -design is deliberately conservative so as to be representative of the -large installed base of processors which pre-date widespread -deployment of more sophisticated indirect branch predictors. In -particular, late model Pentium 4s (Prescott), Pentium M, Core and Core -2 have more sophisticated indirect branch predictors than modelled by -Cachegrind. - -Cachegrind does not simulate a return stack predictor. It -assumes that processors perfectly predict function return addresses, -an assumption which is probably close to being true. - -See Hennessy and Patterson's classic text "Computer -Architecture: A Quantitative Approach", 4th edition (2007), Section -2.3 (pages 80-89) for background on modern branch predictors. - - - - - - +Then, you need to run Cachegrind itself to gather the profiling +information, and then run cg_annotate to get a detailed presentation of that +information. As an optional intermediate step, you can use cg_merge to sum +together the outputs of multiple Cachegrind runs, into a single file which +you then use as the input for cg_annotate. - -Profiling programs + +Running Cachegrind -To gather cache profiling information about the program -ls -l, invoke Cachegrind like -this: - - +To run Cachegrind on a program prog, run: + The program will execute (slowly). Upon completion, summary statistics that look like this will be printed: @@ -299,13 +116,13 @@ summary statistics that look like this will be printed: Combined instruction and data figures for the L2 cache -follow that. - +follow that. Note that the L2 miss rate is computed relative to the total +number of memory accesses, not the number of L1 misses. I.e. it is +(I2mr + D2mr + D2mw) / (Ir + Dr + Dw) +not +(I2mr + D2mr + D2mw) / (I1mr + D1mr + D1mw) + +Branch prediction statistics are not collected by default. +To do so, add the flag . - -Output file + -As well as printing summary information, Cachegrind also -writes line-by-line cache profiling information to a user-specified -file. By default this file is named -cachegrind.out.<pid>. This file -is human-readable, but is intended to be interpreted by the accompanying -program cg_annotate, described in the next section. -Things to note about the -cachegrind.out.<pid> -file: + +Output File - - - It is written every time Cachegrind is run, and will - overwrite any existing - cachegrind.out.<pid> - in the current directory (but that won't happen very often - because it takes some time for process ids to be - recycled). - - - To use an output file name other than the default - cachegrind.out, - use the - switch. - - - It can be big: ls -l - generates a file of about 350KB. Browsing a few files and - web pages with a Konqueror built with full debugging - information generates a file of around 15 MB. - - +As well as printing summary information, Cachegrind also writes +more detailed profiling information to a file. By default this file is named +cachegrind.out.<pid> (where +<pid> is the program's process ID), but its name +can be changed with the option. This +file is human-readable, but is intended to be interpreted by the +accompanying program cg_annotate, described in the next section. The default .<pid> suffix on the output file name serves two purposes. Firstly, it means you @@ -374,164 +174,50 @@ Secondly, and more importantly, it allows correct profiling with the option of programs that spawn child processes. +The output file can be big, many megabytes for large applications +built with full debugging information. + + + +Running cg_annotate - -Cachegrind options +Before using cg_annotate, +it is worth widening your window to be at least 120-characters +wide if possible, as the output lines can be quite long. - -Using command line options, you can -manually specify the I1/D1/L2 cache -configuration to simulate. For each cache, you can specify the -size, associativity and line size. The size and line size -are measured in bytes. The three items -must be comma-separated, but with no spaces, eg: - valgrind --tool=cachegrind --I1=65535,2,64 +To get a function-by-function summary, run: -You can specify one, two or three of the I1/D1/L2 caches. Any level not -manually specified will be simulated using the configuration found in -the normal way (via the CPUID instruction for automagic cache -configuration, or failing that, via defaults). +cg_annotate <filename> -Cache-simulation specific options are: +on a Cachegrind output file. - + - - - - - - Specify the size, associativity and line size of the level 1 - instruction cache. - - - - - - - - Specify the size, associativity and line size of the level 1 - data cache. - - + +The Output Preamble - - - - - - Specify the size, associativity and line size of the level 2 - cache. - - +The first part of the output looks like this: - - - - - - Write the profile data to - file rather than to the default - output file, - cachegrind.out.<pid>. The - and format specifiers - can be used to embed the process ID and/or the contents of an - environment variable in the name, as is the case for the core - option . - - - + - - - - - - Enables or disables collection of cache access and miss - counts. - - - - - - - - Enables or disables collection of branch instruction and - misprediction counts. By default this is disabled as it - slows Cachegrind down by approximately 25%. Note that you - cannot specify - and - together, as that would leave Cachegrind with no - information to collect. - - - - - - - - - - - -Annotating C/C++ programs - -Before using cg_annotate, -it is worth widening your window to be at least 120-characters -wide if possible, as the output lines can be quite long. - -To get a function-by-function summary, run cg_annotate -<filename> on a Cachegrind output file. - -The output looks like this: - - - - -First up is a summary of the annotation options: +This is a summary of the annotation options: @@ -547,68 +233,10 @@ Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw file:function - Events recorded: event abbreviations are: + Events recorded: which events were recorded. - - Ir: I cache reads - (ie. instructions executed) - - - I1mr: I1 cache read - misses - - - I2mr: L2 cache - instruction read misses - - - Dr: D cache reads - (ie. memory reads) - - - D1mr: D1 cache read - misses - - - D2mr: L2 cache data - read misses - - - Dw: D cache writes - (ie. memory writes) - - - D1mw: D1 cache write - misses - - - D2mw: L2 cache data - write misses - - - Bc: Conditional branches - executed - - - Bcm: Conditional branches - mispredicted - - - Bi: Indirect branches - executed - - - Bim: Conditional branches - mispredicted - - Note that D1 total accesses is given by - D1mr + - D1mw, and that L2 total - accesses is given by I2mr + - D2mr + - D2mw. @@ -628,7 +256,7 @@ Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw file:function option. Note that this dictates the order the functions appear. - It is not the order in which the columns + It is not the order in which the columns appear; that is dictated by the "events shown" line (and can be changed with the option). @@ -660,40 +288,80 @@ Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw file:function + + + + +The Global and Function-level Counts + Then follows summary statistics for the whole -program. These are similar to the summary provided when running -valgrind --tool=cachegrind. +program: -Then follows function-by-function statistics. Each function + + + +These are similar to the summary provided when Cachegrind finishes running. + + +Then comes function-by-function statistics: + + + +Each function is identified by a file_name:function_name pair. If a column contains only a dot it means the function never performs -that event (eg. the third row shows that +that event (e.g. the third row shows that strcmp() contains no instructions that write to memory). The name ??? is used if the the file name and/or function name could not be determined from debugging information. If most of the entries have the form ???:??? the program probably -wasn't compiled with . If any -code was invalidated (either due to self-modifying code or -unloading of shared objects) its counts are aggregated into a -single cost centre written as -(discarded):(discarded). +wasn't compiled with . It is worth noting that functions will come both from -the profiled program (eg. concord.c) -and from libraries (eg. getc.c) - -There are two ways to annotate source files -- by choosing -them manually, or with the - option. To do it -manually, just specify the filenames as additional arguments to -cg_annotate. For example, the -output from running cg_annotate <filename> -concord.c for our example produces the same output as above -followed by an annotated version of concord.c, a -section of which looks like: +the profiled program (e.g. concord.c) +and from libraries (e.g. getc.c) + + + + + +Line-by-line Counts + +There are two ways to annotate source files -- by specifying them +manually as arguments to cg_annotate, or with the + option. For example, the output from running +cg_annotate <filename> concord.c for our example +produces the same output as above followed by an annotated version of +concord.c, a section of which looks like: -------------------------------------------------------------------------------- Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw -[snip] - . . . . . . . . . void init_hash_table(char *file_name, Word_Node *table[]) 3 1 1 . . . 1 0 0 { . . . . . . . . . FILE *file_ptr; @@ -759,8 +425,7 @@ part of a file the shown code comes from, eg: controlled by the option. -To get automatic annotation, run -cg_annotate <filename> --auto=yes. +To get automatic annotation, use the option. cg_annotate will automatically annotate every source file it can find that is mentioned in the function-by-function summary. Therefore, the files chosen for auto-annotation are affected by @@ -782,7 +447,7 @@ The following files chosen for auto-annotation could not be found: This is quite common for library files, since libraries are usually compiled with debugging information, but the source files are often not present on a system. If a file is chosen for -annotation both manually and automatically, it +annotation both manually and automatically, it is marked as User-annotated source. Use the / option to tell Valgrind where @@ -790,15 +455,15 @@ to look for source files if the filenames found from the debugging information aren't specific enough. Beware that cg_annotate can take some time to digest large -cachegrind.out.<pid> files, +cachegrind.out.<pid> files, e.g. 30 seconds or more. Also beware that auto-annotation can produce a lot of output if your program is large! - -Annotating assembly code programs + +Annotating Assembly Code Programs Valgrind can annotate assembly code programs too, or annotate the assembly code generated for your C program. Sometimes this is @@ -828,120 +493,8 @@ cg_annotate. - - - - -cg_annotate options - - - - - - - - - Show the help message. - - - - - - - - - Show the version number. - - - - - - - - - Specifies the events upon which the sorting of the - function-by-function entries will be based. Useful if you - want to concentrate on eg. I cache misses - (), or D cache misses - (), or L2 misses - (). - - - - - - - - - Specifies which events to show (and the column - order). Default is to use all present in the - cachegrind.out.<pid> file (and - use the order in the file). - - - - - - - - - Sets the threshold for the function-by-function - summary. Functions are shown that account for more than X% - of the primary sort event. If auto-annotating, also affects - which files are annotated. - - Note: thresholds can be set for more than one of the - events by appending any events for the - option with a colon - and a number (no spaces, though). E.g. if you want to see - the functions that cover 99% of L2 read misses and 99% of L2 - write misses, use this option: - - - - - - - - - - When enabled, automatically annotates every file that - is mentioned in the function-by-function summary that can be - found. Also gives a list of those that couldn't be found. - - - - - - - - - Print N lines of context before and after each - annotated line. Avoids printing large sections of source - files that were not executed. Use a large number - (eg. 10,000) to show all source lines. - - - - - -