New syntax for GADT-style record declarations, and associated refactoring
[ghc-hetmet.git] / docs / users_guide / profiling.xml
index 76e4bcf..adb7a33 100644 (file)
@@ -33,7 +33,9 @@
     <listitem>
       <para> Run your program with one of the profiling options, eg.
       <literal>+RTS -p -RTS</literal>.  This generates a file of
-      profiling information.</para>
+      profiling information.  Note that multi-processor execution
+      (e.g. <literal>+RTS -N2</literal>) is not supported while
+      profiling.</para>
       <indexterm><primary><option>-p</option></primary><secondary>RTS
       option</secondary></indexterm>
     </listitem>
@@ -261,7 +263,24 @@ MAIN                     MAIN             0    0.0   0.0    100.0 100.0
       in the profiling output, and
       <literal>&lt;expression&gt;</literal> is any Haskell
       expression.  An <literal>SCC</literal> annotation extends as
-      far to the right as possible when parsing.</para>
+      far to the right as possible when parsing. (SCC stands for "Set
+      Cost Centre").</para>
+
+      <para>Here is an example of a program with a couple of SCCs:</para>
+
+<programlisting>
+main :: IO ()
+main = do let xs = {-# SCC "X" #-} [1..1000000]
+          let ys = {-# SCC "Y" #-} [1..2000000]
+          print $ last xs
+          print $ last $ init xs
+          print $ last ys
+          print $ last $ init ys
+</programlisting>
+
+      <para>which gives this heap profile when run:</para>
+
+      <imagedata fileref="prof_scc"/>
 
     </sect2>
 
@@ -1255,31 +1274,30 @@ to re-read its input file:
     <indexterm><primary>hpc</primary></indexterm>
 
     <para>
-      Code coverage tools allow a programer to determine what parts of
+      Code coverage tools allow a programmer to determine what parts of
       their code have been actually executed, and which parts have
       never actually been invoked.  GHC has an option for generating
       instrumented code that records code coverage as part of the
       <ulink url="http://www.haskell.org/hpc">Haskell Program Coverage
-      </ulink>(HPC) toolkit.  HPC tools can be used to render the
-      outputed code coverage infomation into human understandable
-      format.  
-   </para>
+      </ulink>(HPC) toolkit, which is included with GHC. HPC tools can
+      be used to render the generated code coverage information into
+      human understandable format.  </para>
 
     <para>
-      HPC provides coverage information of two kinds: source coverage
-      and boolean-control coverage. Source coverage is the extent to
-      which every part of the program was used, measured at three
-      different levels: declarations (both top-level and local),
-      alternatives (among several equations or case branches) and
-      expressions (at every level).  Boolean coverage is the extent to
-      which each of the values True and False is obtained in every
-      syntactic boolean context (ie. guard, condition, qualifier).
-    </para>
+      Correctly instrumented code provides coverage information of two
+      kinds: source coverage and boolean-control coverage. Source
+      coverage is the extent to which every part of the program was
+      used, measured at three different levels: declarations (both
+      top-level and local), alternatives (among several equations or
+      case branches) and expressions (at every level).  Boolean
+      coverage is the extent to which each of the values True and
+      False is obtained in every syntactic boolean context (ie. guard,
+      condition, qualifier).  </para>
 
     <para>
-      HPC displays both kinds of information in two different ways:
-      textual reports with summary statistics (hpc-report) and sources
-      with color mark-up (hpc-markup).  For boolean coverage, there
+      HPC displays both kinds of information in two primary ways:
+      textual reports with summary statistics (hpc report) and sources
+      with color mark-up (hpc markup).  For boolean coverage, there
       are four possible outcomes for each guard, condition or
       qualifier: both True and False values occur; only True; only
       False; never evaluated. In hpc-markup output, highlighting with
@@ -1291,10 +1309,9 @@ to re-read its input file:
    <sect2><title>A small example: Reciprocation</title>
 
     <para>
-     For an example we have a program which computes exact decimal
+     For an example we have a program, called Recip.hs, which computes exact decimal
      representations of reciprocals, with recurring parts indicated in
-     brackets.  We first build an instrumented version using the
-     hpc-build script. Assuming the source file is Recip.hs.
+     brackets.
     </para>
 <programlisting>
 reciprocal :: Int -> (String, Int)
@@ -1329,23 +1346,23 @@ main = do
   main
 </programlisting>
 
-`    <para>The HPC intrumentation is enabled using the -fhpc flag.
+    <para>The HPC instrumentation is enabled using the -fhpc flag.
     </para>
 
 <screen>
 $ ghc -fhpc Recip.hs --make 
 </screen>
     <para>HPC index (.mix) files are placed placed in .hpc subdirectory. These can be considered like
-    the .hi files for HPC. They contain information about what parts of the haskell each modules.
+    the .hi files for HPC. 
    </para>
 <screen>
 $ ./Recip
 1/3
 = 0.(3)
 </screen>
-    <para>Now for a textual summary of coverage:</para>
+    <para>We can generate a textual summary of coverage:</para>
 <screen>
-$ hpc-report Recip
+$ hpc report Recip
  80% expressions used (81/101)
  12% boolean coverage (1/8)
       14% guards (1/7), 3 always True, 
@@ -1357,17 +1374,215 @@ $ hpc-report Recip
 100% local declarations used (9/9)
 100% top-level declarations used (5/5)
 </screen>
-    <para>Finally, we generate a marked-up version of the source.</para>
+    <para>We can also generate a marked-up version of the source.</para>
 <screen>
-$ hpc-markup Recip
+$ hpc markup Recip
 writing Recip.hs.html
 </screen>
-<figure>
-       <title>Recip.hs.html</title>
-       <graphic fileref="images/Recip.png"></graphic>
-    </figure>
+    <para>
+               This generates one file per Haskell module, and 4 index files,
+               hpc_index.html, hpc_index_alt.html, hpc_index_exp.html,
+               hpc_index_fun.html.
+       </para>
+     </sect2> 
+
+     <sect2><title>Options for instrumenting code for coverage</title>
+       <para>
+               Turning on code coverage is easy, use the -fhpc flag. 
+               Instrumented and non-instrumented can be freely mixed.
+               When compiling the Main module GHC automatically detects when there
+               is an hpc compiled file, and adds the correct initialization code.
+       </para>
+
+     </sect2>
+
+     <sect2><title>The hpc toolkit</title>
+
+      <para>
+      The hpc toolkit uses a cvs/svn/darcs-like interface, where a
+      single binary contains many function units.</para> 
+<screen>
+$ hpc 
+Usage: hpc COMMAND ...
+
+Commands:
+  help        Display help for hpc or a single command
+Reporting Coverage:
+  report      Output textual report about program coverage
+  markup      Markup Haskell source with program coverage
+Processing Coverage files:
+  sum         Sum multiple .tix files in a single .tix file
+  combine     Combine two .tix files in a single .tix file
+  map         Map a function over a single .tix file
+Coverage Overlays:
+  overlay     Generate a .tix file from an overlay file
+  draft       Generate draft overlay that provides 100% coverage
+Others:
+  show        Show .tix file in readable, verbose format
+  version     Display version for hpc
+</screen>
+
+     <para>In general, these options act on .tix file after an
+     instrumented binary has generated it, which hpc acting as a
+     conduit between the raw .tix file, and the more detailed reports
+     produced. 
+       </para>
+         
+       <para>
+               The hpc tool assumes you are in the top-level directory of
+               the location where you built your application, and the .tix
+               file is in the same top-level directory. You can use the
+               flag --srcdir to use hpc for any other directory, and use
+               --srcdir multiple times to analyse programs compiled from
+               difference locations, as is typical for packages.
+       </para>
+         
+       <para>
+       We now explain in more details the major modes of hpc.
+     </para>
+
+       <sect3><title>hpc report</title>
+               <para>hpc report gives a textual report of coverage. By default,
+                       all modules and packages are considered in generating report,
+                       unless include or exclude are used. The report is a summary
+                       unless the --per-module flag is used. The --xml-output option
+                       allows for tools to use hpc to glean coverage. 
+               </para> 
+<screen>
+$ hpc help report
+Usage: hpc report [OPTION] .. &lt;TIX_FILE&gt; [&lt;MODULE&gt; [&lt;MODULE&gt; ..]]
+
+Options:
+
+    --per-module                  show module level detail
+    --decl-list                   show unused decls
+    --exclude=[PACKAGE:][MODULE]  exclude MODULE and/or PACKAGE
+    --include=[PACKAGE:][MODULE]  include MODULE and/or PACKAGE
+    --srcdir=DIR                  path to source directory of .hs files
+                                  multi-use of srcdir possible
+    --hpcdir=DIR                  sub-directory that contains .mix files
+                                  default .hpc [rarely used]
+    --xml-output                  show output in XML
+</screen>
+       </sect3>
+       <sect3><title>hpc markup</title>
+               <para>hpc markup marks up source files into colored html.
+               </para>
+<screen>
+$ hpc help markup
+Usage: hpc markup [OPTION] .. &lt;TIX_FILE&gt; [&lt;MODULE&gt; [&lt;MODULE&gt; ..]]
+
+Options:
+
+    --exclude=[PACKAGE:][MODULE]  exclude MODULE and/or PACKAGE
+    --include=[PACKAGE:][MODULE]  include MODULE and/or PACKAGE
+    --srcdir=DIR                  path to source directory of .hs files
+                                  multi-use of srcdir possible
+    --hpcdir=DIR                  sub-directory that contains .mix files
+                                  default .hpc [rarely used]
+    --fun-entry-count             show top-level function entry counts
+    --highlight-covered           highlight covered code, rather that code gaps
+    --destdir=DIR                 path to write output to
+</screen>
+
+       </sect3>
+       <sect3><title>hpc sum</title>
+               <para>hpc sum adds together any number of .tix files into a single 
+               .tix file. hpc sum does not change the original .tix file; it generates a new .tix file. 
+               </para>
+<screen>
+$ hpc help sum
+Usage: hpc sum [OPTION] .. &lt;TIX_FILE&gt; [&lt;TIX_FILE&gt; [&lt;TIX_FILE&gt; ..]]
+Sum multiple .tix files in a single .tix file
 
-    </sect2> 
+Options:
+
+    --exclude=[PACKAGE:][MODULE]  exclude MODULE and/or PACKAGE
+    --include=[PACKAGE:][MODULE]  include MODULE and/or PACKAGE
+    --output=FILE                 output FILE
+    --union                       use the union of the module namespace (default is intersection)
+</screen>
+       </sect3>
+       <sect3><title>hpc combine</title>
+               <para>hpc combine is the swiss army knife of hpc. It can be 
+                used to take the difference between .tix files, to subtract one
+               .tix file from another, or to add two .tix files. hpc combine does not
+               change the original .tix file; it generates a new .tix file. 
+               </para>
+<screen>
+$ hpc help combine
+Usage: hpc combine [OPTION] .. &lt;TIX_FILE&gt; &lt;TIX_FILE&gt;
+Combine two .tix files in a single .tix file
+
+Options:
+
+    --exclude=[PACKAGE:][MODULE]  exclude MODULE and/or PACKAGE
+    --include=[PACKAGE:][MODULE]  include MODULE and/or PACKAGE
+    --output=FILE                 output FILE
+    --function=FUNCTION           combine .tix files with join function, default = ADD
+                                  FUNCTION = ADD | DIFF | SUB
+    --union                       use the union of the module namespace (default is intersection)
+</screen>
+       </sect3>
+       <sect3><title>hpc map</title>
+               <para>hpc map inverts or zeros a .tix file. hpc map does not
+               change the original .tix file; it generates a new .tix file. 
+               </para>
+<screen>
+$ hpc help map
+Usage: hpc map [OPTION] .. &lt;TIX_FILE&gt; 
+Map a function over a single .tix file
+
+Options:
+
+    --exclude=[PACKAGE:][MODULE]  exclude MODULE and/or PACKAGE
+    --include=[PACKAGE:][MODULE]  include MODULE and/or PACKAGE
+    --output=FILE                 output FILE
+    --function=FUNCTION           apply function to .tix files, default = ID
+                                  FUNCTION = ID | INV | ZERO
+    --union                       use the union of the module namespace (default is intersection)
+</screen>
+       </sect3>
+       <sect3><title>hpc overlay and hpc draft</title>
+               <para>
+                       Overlays are an experimental feature of HPC, a textual description
+                       of coverage. hpc draft is used to generate a draft overlay from a .tix file,
+                       and hpc overlay generates a .tix files from an overlay.
+               </para>
+<screen>
+% hpc help overlay
+Usage: hpc overlay [OPTION] .. &lt;OVERLAY_FILE&gt; [&lt;OVERLAY_FILE&gt; [...]]
+
+Options:
+
+    --srcdir=DIR   path to source directory of .hs files
+                   multi-use of srcdir possible
+    --hpcdir=DIR   sub-directory that contains .mix files
+                   default .hpc [rarely used]
+    --output=FILE  output FILE
+% hpc help draft  
+Usage: hpc draft [OPTION] .. &lt;TIX_FILE&gt;
+
+Options:
+
+    --exclude=[PACKAGE:][MODULE]  exclude MODULE and/or PACKAGE
+    --include=[PACKAGE:][MODULE]  include MODULE and/or PACKAGE
+    --srcdir=DIR                  path to source directory of .hs files
+                                  multi-use of srcdir possible
+    --hpcdir=DIR                  sub-directory that contains .mix files
+                                  default .hpc [rarely used]
+    --output=FILE                 output FILE
+</screen>
+      </sect3>
+     </sect2>
+     <sect2><title>Caveats and Shortcomings of Haskell Program Coverage</title>
+         <para>
+               HPC does not attempt to lock the .tix file, so multiple concurrently running
+               binaries in the same directory will exhibit a race condition. There is no way
+               to change the name of the .tix file generated, apart from renaming the binary.
+               HPC does not work with GHCi.
+         </para>
+    </sect2>
   </sect1>
 
   <sect1 id="ticky-ticky">