summaryrefslogtreecommitdiff
path: root/docs/users_guide/profiling.rst
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2016-01-09 04:38:16 +0100
committerBen Gamari <ben@smart-cactus.org>2016-01-09 04:38:54 +0100
commita6c3289d0aa0c520656e918dfc9f152548d940a4 (patch)
tree1c5aa8a05bec7dc626ce1b9c27163e93665db95d /docs/users_guide/profiling.rst
parent1cdf12c4f435262b93cb0173f9872f3f0f0da60a (diff)
downloadhaskell-a6c3289d0aa0c520656e918dfc9f152548d940a4.tar.gz
users_guide: Use semantic directive/role for command line options
And GHCi commands. This makes cross-referencing much easier. Also normalize markup a bit and add some missing flags.
Diffstat (limited to 'docs/users_guide/profiling.rst')
-rw-r--r--docs/users_guide/profiling.rst393
1 files changed, 183 insertions, 210 deletions
diff --git a/docs/users_guide/profiling.rst b/docs/users_guide/profiling.rst
index 34525d12b8..a163173e37 100644
--- a/docs/users_guide/profiling.rst
+++ b/docs/users_guide/profiling.rst
@@ -14,19 +14,19 @@ so much memory?".
Profiling a program is a three-step process:
-1. Re-compile your program for profiling with the ``-prof`` option, and
+1. Re-compile your program for profiling with the :ghc-flag:`-prof` option, and
probably one of the options for adding automatic annotations:
- ``-fprof-auto`` is the most common [1]_. ``-fprof-auto``
+ :ghc-flag:`-fprof-auto` is the most common [1]_.
- If you are using external packages with ``cabal``, you may need to
+ If you are using external packages with :command:`cabal`, you may need to
reinstall these packages with profiling support; typically this is
done with ``cabal install -p package --reinstall``.
2. Having compiled the program for profiling, you now need to run it to
generate the profile. For example, a simple time profile can be
- generated by running the program with ``+RTS -p``, which generates a file
- named ``⟨prog⟩.prof`` where ⟨prog⟩ is the name of your program (without the
- ``.exe`` extension, if you are on Windows).
+ generated by running the program with ``+RTS -p`` (see :rts-flag:`-p`), which
+ generates a file named :file:`{prog}.prof` where ⟨prog⟩ is the name of your
+ program (without the ``.exe`` extension, if you are on Windows).
There are many different kinds of profile that can be generated,
selected by different RTS options. We will be describing the various
@@ -50,27 +50,25 @@ Furthermore, GHC will remember the stack of enclosing cost centres for
any given expression at run-time and generate a call-tree of cost
attributions.
-Let's take a look at an example:
-
-::
+Let's take a look at an example: ::
main = print (fib 30)
fib n = if n < 2 then 1 else fib (n-1) + fib (n-2)
Compile and run this program as follows:
-::
+.. code-block:: none
$ ghc -prof -fprof-auto -rtsopts Main.hs
$ ./Main +RTS -p
121393
$
-When a GHC-compiled program is run with the ``-p`` RTS option, it
-generates a file called ``prog.prof``. In this case, the file will
-contain something like this:
+When a GHC-compiled program is run with the :rts-flag:`-p` RTS option, it
+generates a file called :file:`prog.prof`. In this case, the file will contain
+something like this:
-::
+.. code-block:: none
Wed Oct 12 16:14 2011 Time and Allocation Profiling Report (Final)
@@ -119,9 +117,7 @@ code covered by this cost centre stack alone, and “inherited”, which
includes the costs incurred by all the children of this node.
The usefulness of cost-centre stacks is better demonstrated by modifying
-the example slightly:
-
-::
+the example slightly: ::
main = print (f 30 + g 30)
where
@@ -133,7 +129,7 @@ the example slightly:
Compile and run this program as before, and take a look at the new
profiling results:
-::
+.. code-block:: none
COST CENTRE MODULE no. entries %time %alloc %time %alloc
@@ -172,7 +168,7 @@ The actual meaning of the various columns in the output is:
overheads) of the program made by this call and all of its
sub-calls.
-In addition you can use the ``-P`` RTS option ``-P`` to get the
+In addition you can use the :rts-flag:`-P` RTS option to get the
following additional information:
``ticks``
@@ -203,9 +199,7 @@ to the compiler, it automatically inserts a cost centre annotation
around every binding not marked INLINE in your program, but you are
entirely free to add cost centre annotations yourself.
-The syntax of a cost centre annotation is
-
-::
+The syntax of a cost centre annotation is ::
{-# SCC "name" #-} <expression>
@@ -214,15 +208,11 @@ your cost centre as it appears in the profiling output, and
``<expression>`` is any Haskell expression. An ``SCC`` annotation
extends as far to the right as possible when parsing. (SCC stands for
"Set Cost Centre"). The double quotes can be omitted if ``name`` is a
-Haskell identifier, for example:
-
-::
+Haskell identifier, for example: ::
{-# SCC my_function #-} <expression>
-Here is an example of a program with a couple of SCCs:
-
-::
+Here is an example of a program with a couple of SCCs: ::
main :: IO ()
main = do let xs = [1..1000000]
@@ -234,7 +224,7 @@ Here is an example of a program with a couple of SCCs:
which gives this profile when run:
-::
+.. code-block:: none
COST CENTRE MODULE no. entries %time %alloc %time %alloc
@@ -283,11 +273,11 @@ name for a top-level thunk is a CAF ("Constant Applicative Form"). GHC
assigns every CAF in a module a stack consisting of the single cost
centre ``M.CAF``, where ``M`` is the name of the module. It is also
possible to give each CAF a different stack, using the option
-``-fprof-cafs``. This is especially useful when
-compiling with ``-ffull-laziness`` (as is default with ``-O`` and
-higher), as constants in function bodies will be lifted to the top-level
+:ghc-flag:`-fprof-cafs`. This is especially useful when
+compiling with :ghc-flag:`-ffull-laziness` (as is default with :ghc-flag:`-O`
+and higher), as constants in function bodies will be lifted to the top-level
and become CAFs. You will probably need to consult the Core
-(``-ddump-simpl``) in order to determine what these CAFs correspond to.
+(:ghc-flag:`-ddump-simpl`) in order to determine what these CAFs correspond to.
.. index::
single: -fprof-cafs
@@ -301,41 +291,35 @@ Compiler options for profiling
single: profiling; options
single: options; for profiling
-``-prof``
- .. index::
- single: -prof
+.. ghc-flag:: -prof
To make use of the profiling system *all* modules must be compiled
- and linked with the ``-prof`` option. Any ``SCC`` annotations you've
+ and linked with the :ghc-flag:`-prof` option. Any ``SCC`` annotations you've
put in your source will spring to life.
- Without a ``-prof`` option, your ``SCC``\ s are ignored; so you can
+ Without a :ghc-flag:`-prof` option, your ``SCC``\ s are ignored; so you can
compile ``SCC``-laden code without changing it.
There are a few other profiling-related compilation options. Use them
-*in addition to* ``-prof``. These do not have to be used consistently
+*in addition to* :ghc-flag:`-prof`. These do not have to be used consistently
for all modules in a program.
-``-fprof-auto``
- .. index::
- single: -fprof-auto
+.. ghc-flag:: -fprof-auto
*All* bindings not marked INLINE, whether exported or not, top level
or nested, will be given automatic ``SCC`` annotations. Functions
marked INLINE must be given a cost centre manually.
-``-fprof-auto-top``
+.. ghc-flag:: -fprof-auto-top
+
.. index::
- single: -fprof-auto-top
single: cost centres; automatically inserting
GHC will automatically add ``SCC`` annotations for all top-level
bindings not marked INLINE. If you want a cost centre on an INLINE
function, you have to add it manually.
-``-fprof-auto-exported``
- .. index::
- single: -fprof-auto-top
+.. ghc-flag:: -fprof-auto-exported
.. index::
single: cost centres; automatically inserting
@@ -344,38 +328,34 @@ for all modules in a program.
functions not marked INLINE. If you want a cost centre on an INLINE
function, you have to add it manually.
-``-fprof-auto-calls``
+.. ghc-flag:: -fprof-auto-calls
+
.. index::
single: -fprof-auto-calls
Adds an automatic ``SCC`` annotation to all *call sites*. This is
particularly useful when using profiling for the purposes of
generating stack traces; see the function ``traceStack`` in the
- module ``Debug.Trace``, or the ``-xc`` RTS flag
+ module ``Debug.Trace``, or the :rts-flag:`-xc` RTS flag
(:ref:`rts-options-debugging`) for more details.
-``-fprof-cafs``
- .. index::
- single: -fprof-cafs
+.. ghc-flag:: -fprof-cafs
The costs of all CAFs in a module are usually attributed to one
- “big” CAF cost-centre. With this option, all CAFs get their own
+ "big" CAF cost-centre. With this option, all CAFs get their own
cost-centre. An “if all else fails” option…
-``-fno-prof-auto``
- .. index::
- single: -no-fprof-auto
+.. ghc-flag:: -fno-prof-auto
- Disables any previous ``-fprof-auto``, ``-fprof-auto-top``, or
- ``-fprof-auto-exported`` options.
+ Disables any previous :ghc-flag:`-fprof-auto`, :ghc-flag:`-fprof-auto-top`, or
+ :ghc-flag:`-fprof-auto-exported` options.
-``-fno-prof-cafs``
- .. index::
- single: -fno-prof-cafs
+.. ghc-flag:: -fno-prof-cafs
- Disables any previous ``-fprof-cafs`` option.
+ Disables any previous :ghc-flag:`-fprof-cafs` option.
+
+.. ghc-flag:: -fno-prof-count-entries
-``-fno-prof-count-entries``
.. index::
single: -fno-prof-count-entries
@@ -397,33 +377,29 @@ To generate a time and allocation profile, give one of the following RTS
options to the compiled program when you run it (RTS options should be
enclosed between ``+RTS ... -RTS`` as usual):
-``-p``, ``-P``, ``-pa``
+.. rts-flag:: -p
+ -P
+ -pa
+
.. index::
- single: -p; RTS option
- single: -P; RTS option
- single: -pa; RTS option
single: time profile
- The ``-p`` option produces a standard *time profile* report. It is
- written into the file ``program.prof``.
+ The :rts-flag:`-p` option produces a standard *time profile* report. It is
+ written into the file :file:`program.prof`.
- The ``-P`` option produces a more detailed report containing the
+ The :rts-flag:`-P` option produces a more detailed report containing the
actual time and allocation data as well. (Not used much.)
- The ``-pa`` option produces the most detailed report containing all
+ The :rts-flag:`-pa` option produces the most detailed report containing all
cost centres in addition to the actual time and allocation data.
-``-Vsecs``
- .. index::
- single: -V; RTS option
+.. rts-flag:: -V <secs>
Sets the interval that the RTS clock ticks at, which is also the
sampling interval of the time and allocation profile. The default is
0.02 seconds.
-``-xc``
- .. index::
- single: -xc; RTS option
+.. rts-flag:: -xc
This option causes the runtime to print out the current cost-centre
stack whenever an exception is raised. This can be particularly
@@ -448,7 +424,7 @@ To generate a heap profile from your program:
1. Compile the program for profiling (:ref:`prof-compiler-options`).
2. Run it with one of the heap profiling options described below (eg.
- ``-h`` for a basic producer profile). This generates the file
+ :rts-flag:`-h` for a basic producer profile). This generates the file
``prog.hp``.
3. Run ``hp2ps`` to produce a Postscript file, ``prog.ps``. The
@@ -476,46 +452,35 @@ All the different profile types yield a graph of live heap against time,
but they differ in how the live heap is broken down into bands. The
following RTS options select which break-down to use:
-``-hc``
- .. index::
- single: -hc; RTS option
+.. rts-flag:: -hc
+ -h
- (can be shortened to ``-h``). Breaks down the graph by the
+ (can be shortened to :rts-flag:`-h`). Breaks down the graph by the
cost-centre stack which produced the data.
-``-hm``
- .. index::
- single: -hm; RTS option
+.. rts-flag:: -hm
Break down the live heap by the module containing the code which
produced the data.
-``-hd``
- .. index::
- single: -hd; RTS option
+.. rts-flag:: -hd
Breaks down the graph by closure description. For actual data, the
description is just the constructor name, for other closures it is a
compiler-generated string identifying the closure.
-``-hy``
- .. index::
- single: -hy; RTS option
+.. rts-flag:: -hy
Breaks down the graph by type. For closures which have function type
or unknown/polymorphic type, the string will represent an
approximation to the actual type.
-``-hr``
- .. index::
- single: -hr; RTS option
+.. rts-flag:: -hr
Break down the graph by retainer set. Retainer profiling is
described in more detail below (:ref:`retainer-prof`).
-``-hb``
- .. index::
- single: -hb; RTS option
+.. rts-flag:: -hb
Break down the graph by biography. Biographical profiling is
described in more detail below (:ref:`biography-prof`).
@@ -526,50 +491,48 @@ type but only for data produced by a certain module, or a profile by
retainer for a certain type of data. Restrictions are specified as
follows:
-``-hc ⟨name⟩``
- .. index::
- single: -hc; RTS option
+.. comment
+
+ The flags below are marked with ``:noindex:`` to avoid duplicate
+ ID warnings from Sphinx.
+
+.. rts-flag:: -hc <name>
+ :noindex:
Restrict the profile to closures produced by cost-centre stacks with
one of the specified cost centres at the top.
-``-hC ⟨name⟩``
- .. index::
- single: -hC; RTS option
+.. rts-flag:: -hC <name>
+ :noindex:
Restrict the profile to closures produced by cost-centre stacks with
one of the specified cost centres anywhere in the stack.
-``-hm ⟨module⟩``
- .. index::
- single: -hm; RTS option
+.. rts-flag:: -hm <module>
+ :noindex:
Restrict the profile to closures produced by the specified modules.
-``-hd ⟨desc⟩``
- .. index::
- single: -hd; RTS option
+.. rts-flag:: -hd <desc>
+ :noindex:
Restrict the profile to closures with the specified description
strings.
-``-hy ⟨type⟩``
- .. index::
- single: -hy; RTS option
+.. rts-flag:: -hy <type>
+ :noindex:
Restrict the profile to closures with the specified types.
-``-hr ⟨cc⟩``
- .. index::
- single: -hr; RTS option
+.. rts-flag:: -hr <cc>
+ :noindex:
Restrict the profile to closures with retainer sets containing
cost-centre stacks with one of the specified cost centres at the
top.
-``-hb ⟨bio⟩``
- .. index::
- single: -hb; RTS option
+.. rts-flag:: -hb <bio>
+ :noindex:
Restrict the profile to closures with one of the specified
biographies, where ⟨bio⟩ is one of ``lag``, ``drag``, ``void``, or
@@ -578,20 +541,18 @@ follows:
For example, the following options will generate a retainer profile
restricted to ``Branch`` and ``Leaf`` constructors:
-::
+.. code-block:: none
prog +RTS -hr -hdBranch,Leaf
-There can only be one "break-down" option (eg. ``-hr`` in the example
+There can only be one "break-down" option (eg. :rts-flag:`-hr` in the example
above), but there is no limit on the number of further restrictions that
may be applied. All the options may be combined, with one exception: GHC
-doesn't currently support mixing the ``-hr`` and ``-hb`` options.
+doesn't currently support mixing the :rts-flag:`-hr` and :rts-flag:`-hb` options.
There are three more options which relate to heap profiling:
-``-isecs``
- .. index::
- single: -i
+.. rts-flag:: -i <secs>
Set the profiling (sampling) interval to ⟨secs⟩ seconds (the default
is 0.1 second). Fractions are allowed: for example ``-i0.2`` will
@@ -599,25 +560,21 @@ There are three more options which relate to heap profiling:
profiles are always sampled with the frequency of the RTS clock. See
:ref:`prof-time-options` for changing that.
-``-xt``
- .. index::
- single: -xt; RTS option
+.. rts-flag:: -xt
Include the memory occupied by threads in a heap profile. Each
thread takes up a small area for its thread state in addition to the
space allocated for its stack (stacks normally start small and then
grow as necessary).
- This includes the main thread, so using ``-xt`` is a good way to see
+ This includes the main thread, so using :rts-flag:`-xt` is a good way to see
how much stack space the program is using.
Memory occupied by threads and their stacks is labelled as “TSO” and
“STACK” respectively when displaying the profile by closure
description or type description.
-``-Lnum``
- .. index::
- single: -L; RTS option
+.. rts-flag:: -L <num>
Sets the maximum length of a cost-centre stack name in a heap
profile. Defaults to 25.
@@ -636,9 +593,9 @@ retainer:
In particular, constructors are *not* retainers.
-An object B retains object A if (i) B is a retainer object and (ii)
-object A can be reached by recursively following pointers starting from
-object B, but not meeting any other retainer objects on the way. Each
+An object ``B`` retains object ``A`` if (i) ``B`` is a retainer object and (ii)
+object ``A`` can be reached by recursively following pointers starting from
+object ``B``, but not meeting any other retainer objects on the way. Each
live object is retained by one or more retainer objects, collectively
called its retainer set, or its retainer set, or its retainers.
@@ -654,9 +611,10 @@ to discover the full retainer set for each object, which can be quite
slow. So we set a limit on the maximum size of a retainer set, where all
retainer sets larger than the maximum retainer set size are replaced by
the special set ``MANY``. The maximum set size defaults to 8 and can be
-altered with the ``-R`` RTS option:
+altered with the :rts-flag:`-R` RTS option:
+
+.. rts-flag:: -R <size>
-``-R ⟨size⟩``
Restrict the number of elements in a retainer set to ⟨size⟩ (default
8).
@@ -674,19 +632,19 @@ using ``seq`` or strictness annotations on data constructor fields.
Often a particular data structure is being retained by a chain of
unevaluated closures, only the nearest of which will be reported by
-retainer profiling - for example A retains B, B retains C, and C retains
-a large structure. There might be a large number of Bs but only a single
-A, so A is really the one we're interested in eliminating. However,
-retainer profiling will in this case report B as the retainer of the
-large structure. To move further up the chain of retainers, we can ask
-for another retainer profile but this time restrict the profile to B
-objects, so we get a profile of the retainers of B:
+retainer profiling - for example ``A`` retains ``B``, ``B`` retains ``C``, and
+``C`` retains a large structure. There might be a large number of ``B``\s but
+only a single ``A``, so ``A`` is really the one we're interested in eliminating.
+However, retainer profiling will in this case report ``B`` as the retainer of
+the large structure. To move further up the chain of retainers, we can ask for
+another retainer profile but this time restrict the profile to ``B`` objects, so
+we get a profile of the retainers of ``B``:
-::
+.. code-block:: none
prog +RTS -hr -hcB
-This trick isn't foolproof, because there might be other B closures in
+This trick isn't foolproof, because there might be other ``B`` closures in
the heap which aren't the retainers we are interested in, but we've
found this to be a useful technique in most cases.
@@ -720,14 +678,14 @@ states by a different criteria, by restricting a profile by biography.
For example, to show the portion of the heap in the drag or void state
by producer:
-::
+.. code-block:: none
prog +RTS -hc -hbdrag,void
Once you know the producer or the type of the heap in the drag or void
states, the next step is usually to find the retainer(s):
-::
+.. code-block:: none
prog +RTS -hr -hccc...
@@ -757,16 +715,16 @@ reasons for this:
The factor depends on the kind of garbage collection algorithm in
use: a major GC in the standard generation copying collector will
usually require 3L bytes of memory, where L is the amount of live
- data. This is because by default (see the ``+RTS -F`` option) we
+ data. This is because by default (see the RTS :rts-flag:`-F` option) we
allow the old generation to grow to twice its size (2L) before
collecting it, and we require additionally L bytes to copy the live
- data into. When using compacting collection (see the ``+RTS -c``
+ data into. When using compacting collection (see the :rts-flag:`-c`
option), this is reduced to 2L, and can further be reduced by
- tweaking the ``-F`` option. Also add the size of the allocation area
+ tweaking the :rts-flag:`-F` option. Also add the size of the allocation area
(currently a fixed 512Kb).
- The stack isn't counted in the heap profile by default. See the
- ``+RTS -xt`` option.
+ RTS :rts-flag:`-xt` option.
- The program text itself, the C stack, any non-heap data (e.g. data
allocated by foreign libraries, and data allocated by the RTS), and
@@ -785,23 +743,26 @@ reasons for this:
Usage:
-::
+.. code-block:: none
hp2ps [flags] [<file>[.hp]]
-The program ``hp2ps`` program converts a heap profile as produced
+The program :command:`hp2ps` program converts a heap profile as produced
by the ``-h<break-down>`` runtime option into a PostScript graph of the
-heap profile. By convention, the file to be processed by ``hp2ps`` has a
-``.hp`` extension. The PostScript output is written to ``<file>@.ps``.
+heap profile. By convention, the file to be processed by :command:`hp2ps` has a
+``.hp`` extension. The PostScript output is written to :file:`{file}@.ps`.
If ``<file>`` is omitted entirely, then the program behaves as a filter.
-``hp2ps`` is distributed in ``ghc/utils/hp2ps`` in a GHC source
+:command:`hp2ps` is distributed in :file:`ghc/utils/hp2ps` in a GHC source
distribution. It was originally developed by Dave Wakeling as part of
the HBC/LML heap profiler.
The flags are:
-``-d``
+.. program:: hp2ps
+
+.. option:: -d
+
In order to make graphs more readable, ``hp2ps`` sorts the shaded
bands for each identifier. The default sort ordering is for the
bands with the largest area to be stacked on top of the smaller
@@ -809,14 +770,16 @@ The flags are:
series of values with the largest standard deviations) to be stacked
on top of smoother ones.
-``-b``
+.. option:: -b
+
Normally, ``hp2ps`` puts the title of the graph in a small box at
the top of the page. However, if the JOB string is too long to fit
in a small box (more than 35 characters), then ``hp2ps`` will choose
to use a big box instead. The ``-b`` option forces ``hp2ps`` to use
a big box.
-``-e<float>[in|mm|pt]``
+.. option:: -e<float>[in|mm|pt]
+
Generate encapsulated PostScript suitable for inclusion in LaTeX
documents. Usually, the PostScript graph is drawn in landscape mode
in an area 9 inches wide by 6 inches high, and ``hp2ps`` arranges
@@ -829,19 +792,22 @@ The flags are:
(EPS) convention, and it can be included in a LaTeX document using
Rokicki's dvi-to-PostScript converter ``dvips``.
-``-g``
+.. option:: -g
+
Create output suitable for the ``gs`` PostScript previewer (or
similar). In this case the graph is printed in portrait mode without
scaling. The output is unsuitable for a laser printer.
-``-l``
+.. option:: -l
+
Normally a profile is limited to 20 bands with additional
identifiers being grouped into an ``OTHER`` band. The ``-l`` flag
removes this 20 band and limit, producing as many bands as
necessary. No key is produced as it won't fit!. It is useful for
creation time profiles with many bands.
-``-m<int>``
+.. option:: -m<int>
+
Normally a profile is limited to 20 bands with additional
identifiers being grouped into an ``OTHER`` band. The ``-m`` flag
specifies an alternative band limit (the maximum is 20).
@@ -850,7 +816,8 @@ The flags are:
necessary are produced. However no key is produced as it won't fit!
It is useful for displaying creation time profiles with many bands.
-``-p``
+.. option:: -p
+
Use previous parameters. By default, the PostScript graph is
automatically scaled both horizontally and vertically so that it
fills the page. However, when preparing a series of graphs for use
@@ -860,10 +827,12 @@ The flags are:
previous run of ``hp2ps`` on ``file``. These are extracted from
``file@.aux``.
-``-s``
+.. option:: -s
+
Use a small box for the title.
-``-t<float>``
+.. option:: -t<float>
+
Normally trace elements which sum to a total of less than 1% of the
profile are removed from the profile. The ``-t`` option allows this
percentage to be modified (maximum 5%).
@@ -871,13 +840,16 @@ The flags are:
``-t0`` requests no trace elements to be removed from the profile,
ensuring that all the data will be displayed.
-``-c``
+.. option:: -c
+
Generate colour output.
-``-y``
+.. option:: -y
+
Ignore marks.
-``-?``
+.. option:: -?
+
Print out usage information.
.. _manipulating-hp:
@@ -891,7 +863,7 @@ The ``FOO.hp`` file produced when you ask for the heap profile of a
program ``FOO`` is a text file with a particularly simple structure.
Here's a representative example, with much of the actual data omitted:
-::
+.. code-block:: none
JOB "FOO -hC"
DATE "Thu Dec 26 18:17 2002"
@@ -928,13 +900,13 @@ Viewing the heap profile of a running program
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The ``.hp`` file is generated incrementally as your program runs. In
-principle, running ``hp2ps`` on the incomplete file should produce a
+principle, running :command:`hp2ps` on the incomplete file should produce a
snapshot of your program's heap usage. However, the last sample in the
-file may be incomplete, causing ``hp2ps`` to fail. If you are using a
+file may be incomplete, causing :command:`hp2ps` to fail. If you are using a
machine with UNIX utilities installed, it's not too hard to work around
this problem (though the resulting command line looks rather Byzantine):
-::
+.. code-block:: sh
head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
| hp2ps > FOO.ps
@@ -942,20 +914,20 @@ this problem (though the resulting command line looks rather Byzantine):
The command ``fgrep -n END_SAMPLE FOO.hp`` finds the end of every
complete sample in ``FOO.hp``, and labels each sample with its ending
line number. We then select the line number of the last complete sample
-using ``tail`` and ``cut``. This is used as a parameter to ``head``; the
-result is as if we deleted the final incomplete sample from ``FOO.hp``.
+using :command:`tail` and :command:`cut`. This is used as a parameter to :command:`head`; the
+result is as if we deleted the final incomplete sample from :file:`FOO.hp`.
This results in a properly-formatted .hp file which we feed directly to
-``hp2ps``.
+:command:`hp2ps`.
Viewing a heap profile in real time
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-The ``gv`` and ``ghostview`` programs have a "watch file" option can be
-used to view an up-to-date heap profile of your program as it runs.
+The :command:`gv` and :command:`ghostview` programs have a "watch file" option
+can be used to view an up-to-date heap profile of your program as it runs.
Simply generate an incremental heap profile as described in the previous
-section. Run ``gv`` on your profile:
+section. Run :command:`gv` on your profile:
-::
+.. code-block:: sh
gv -watch -orientation=seascape FOO.ps
@@ -965,7 +937,7 @@ the view will update automatically.
This can all be encapsulated in a little script:
-::
+.. code-block:: sh
#!/bin/sh
head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
@@ -977,13 +949,13 @@ This can all be encapsulated in a little script:
| hp2ps > FOO.ps
done
-Occasionally ``gv`` will choke as it tries to read an incomplete copy of
-``FOO.ps`` (because ``hp2ps`` is still running as an update occurs). A
+Occasionally :command:`gv` will choke as it tries to read an incomplete copy of
+:file:`FOO.ps` (because :command:`hp2ps` is still running as an update occurs). A
slightly more complicated script works around this problem, by using the
fact that sending a SIGHUP to gv will cause it to re-read its input
file:
-::
+.. code-block:: sh
#!/bin/sh
head -`fgrep -n END_SAMPLE FOO.hp | tail -1 | cut -d : -f 1` FOO.hp \
@@ -1002,9 +974,9 @@ file:
Profiling Parallel and Concurrent Programs
------------------------------------------
-Combining ``-threaded`` and ``-prof`` is perfectly fine, and indeed it
-is possible to profile a program running on multiple processors with the
-``+RTS -N`` option. [3]_
+Combining :ghc-flag:`-threaded` and :ghc-flag:`-prof` is perfectly fine, and
+indeed it is possible to profile a program running on multiple processors with
+the RTS :rts-flag:`-N` option. [3]_
Some caveats apply, however. In the current implementation, a profiled
program is likely to scale much less well than the unprofiled program,
@@ -1014,7 +986,7 @@ allocation statistics collected by the profiled program are stored in
shared memory but *not* locked (for speed), which means that these
figures might be inaccurate for parallel programs.
-We strongly recommend that you use ``-fno-prof-count-entries`` when
+We strongly recommend that you use :ghc-flag:`-fno-prof-count-entries` when
compiling a program to be profiled on multiple cores, because the entry
counts are also stored in shared memory, and continuously updating them
on multiple cores is extremely slow.
@@ -1064,11 +1036,9 @@ one.
A small example: Reciprocation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-For an example we have a program, called ``Recip.hs``, which computes
+For an example we have a program, called :file:`Recip.hs`, which computes
exact decimal representations of reciprocals, with recurring parts
-indicated in brackets.
-
-::
+indicated in brackets. ::
reciprocal :: Int -> (String, Int)
reciprocal n | n > 1 = ('0' : '.' : digits, recur)
@@ -1101,9 +1071,9 @@ indicated in brackets.
putStrLn (showRecip number)
main
-HPC instrumentation is enabled with the -fhpc flag:
+HPC instrumentation is enabled with the :ghc-flag:`-fhpc` flag:
-::
+.. code-block:: sh
$ ghc -fhpc Recip.hs
@@ -1113,14 +1083,14 @@ don't need to worry about these files: they contain information needed
by the ``hpc`` tool to generate the coverage data for compiled modules
after the program is run.
-::
+.. code-block:: sh
$ ./Recip
1/3
= 0.(3)
Running the program generates a file with the ``.tix`` suffix, in this
-case ``Recip.tix``, which contains the coverage data for this run of the
+case :file:`Recip.tix`, which contains the coverage data for this run of the
program. The program may be run multiple times (e.g. with different test
data), and the coverage data from the separate runs is accumulated in
the ``.tix`` file. To reset the coverage data and start again, just
@@ -1128,7 +1098,7 @@ remove the ``.tix`` file.
Having run the program, we can generate a textual summary of coverage:
-::
+.. code-block:: none
$ hpc report Recip
80% expressions used (81/101)
@@ -1144,27 +1114,30 @@ Having run the program, we can generate a textual summary of coverage:
We can also generate a marked-up version of the source.
-::
+.. code-block:: none
$ hpc markup Recip
writing Recip.hs.html
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``.
+:file:`hpc_index.html`, :file:`hpc_index_alt.html`, :file:`hpc_index_exp.html`,
+:file:`hpc_index_fun.html`.
Options for instrumenting code for coverage
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-``-fhpc``
+.. program:: hpc
+
+.. ghc-flag:: -fhpc
+
Enable code coverage for the current module or modules being
compiled.
Modules compiled with this option can be freely mixed with modules
compiled without it; indeed, most libraries will typically be
- compiled without ``-fhpc``. When the program is run, coverage data
+ compiled without :ghc-flag:`-fhpc`. When the program is run, coverage data
will only be generated for those modules that were compiled with
- ``-fhpc``, and the ``hpc`` tool will only show information about
+ :ghc-flag:`-fhpc`, and the :command:`hpc` tool will only show information about
those modules.
The hpc toolkit
@@ -1172,7 +1145,7 @@ The hpc toolkit
The hpc command has several sub-commands:
-::
+.. code-block:: none
$ hpc
Usage: hpc COMMAND ...
@@ -1213,7 +1186,7 @@ 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.
-::
+.. code-block:: none
$ hpc help report
Usage: hpc report [OPTION] .. <TIX_FILE> [<MODULE> [<MODULE> ..]]
@@ -1237,7 +1210,7 @@ hpc markup
``hpc markup`` marks up source files into colored html.
-::
+.. code-block:: none
$ hpc help markup
Usage: hpc markup [OPTION] .. <TIX_FILE> [<MODULE> [<MODULE> ..]]
@@ -1263,7 +1236,7 @@ hpc sum
``.tix`` file. ``hpc sum`` does not change the original ``.tix`` file;
it generates a new ``.tix`` file.
-::
+.. code-block:: none
$ hpc help sum
Usage: hpc sum [OPTION] .. <TIX_FILE> [<TIX_FILE> [<TIX_FILE> ..]]
@@ -1284,7 +1257,7 @@ 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.
-::
+.. code-block:: none
$ hpc help combine
Usage: hpc combine [OPTION] .. <TIX_FILE> <TIX_FILE>
@@ -1305,7 +1278,7 @@ hpc map
hpc map inverts or zeros a ``.tix`` file. hpc map does not change the
original ``.tix`` file; it generates a new ``.tix`` file.
-::
+.. code-block:: none
$ hpc help map
Usage: hpc map [OPTION] .. <TIX_FILE>
@@ -1327,7 +1300,7 @@ 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.
-::
+.. code-block:: none
% hpc help overlay
Usage: hpc overlay [OPTION] .. <OVERLAY_FILE> [<OVERLAY_FILE> [...]]
@@ -1379,7 +1352,7 @@ Take a look at its
which includeds a link to the ticky-ticky profiling page.
.. [1]
- ``-fprof-auto`` was known as ``-auto-all``\ ``-auto-all`` prior to
+ :ghc-flag:`-fprof-auto` was known as ``-auto-all`` prior to
GHC 7.4.1.
.. [2]