Wolfram Research

Function Repository Resource:

TraceView (1.0.1) current version: 1.1.1 »

Source Notebook

Interactively explore execution traces

Contributed by: Lukas Lang

ResourceFunction["TraceView"][expr]

display the execution trace of expr in an interactively explorable view.

ResourceFunction["TraceView"][expr,form]

filters the trace to show only expressions matching form.

Details and Options

ResourceFunction["TraceView"][expr] displays an interactive view of the execution trace of the evaluation process of expr. Shown are the input and output expressions, their sub-steps, as well as the time taken by each step.
ResourceFunction["TraceView"] is based on TraceScan, and takes all the same options.
In addition, ResourceFunction["TraceView"] accepts the following options:
“TimesRelativeTo""Outer"the reference to use for the timing bars
"MaxDisplayDepth"4the maximum number of levels shown at once
"MaxChildDisplay"5the maximum number of sub-steps shown at once
"MaxExpressionPaneSize"AbsoluteCurrentValue[WindowSize]/{3,6}the maximum size of the panes containing the individual expressions (in printers points)
"DefaultExpressionSizeLimit"1000the maximum size of the expressions before parts are elided
"ShowAllSteps"Falsewhether to show trivial steps
The "TimesRelativeTo" setting can be set to the following:
"Outer"show times relative to the outermost visible parent step
"Total"show times relative to the total execution time
"Parent"show times relative to the direct parent step
The "TimesRelativeTo" setting controls the display of the time bar shown next to the execution time of each step, where the light-blue part of the bar indicates the total time taken by a step, and the dark-blue part the own-time taken by the step itself (excluding any sub-steps).
When navigating deeper into the call tree, outer levels will be omitted to keep the view responsive and reasonably sized. Closing sub-steps or clicking the outer edge of the view will reveal outer levels again.
The number of levels shown is controlled by the "MaxDisplayDepth" setting.
Similarly, the number of sub-steps displayed at once is limited by the "MaxChildDisplay" setting. When not all sub-steps are shown, the buttons at the bottom/top of the list can be used to reveal more sub-steps. When holding the  (Windows/Linux) or  (macOS) key, an entire page of new sub-steps is revealed. When holding the  key instead, the view jumps to the end/beginning of the list.
The value of the "DefaultExpressionSizeLimit" setting corresponds roughly to the ByteCount of the box expression shown before placeholders are inserted. The placeholders are clickable, so more of the expression can be viewed if required.
The input expressions are shown in what is effectively InputForm, while the output is shown in StandardForm. Both can be copied and pasted into a notebook and should be fully equivalent to the original expression. If some parts of the expression are elided, interpretation of the boxes only works in the kernel session the trace was generated in.
While the expressions copied from a ResourceFunction["TraceView"] output are equivalent to the original expression, symbol contexts may not be visible. To avoid this, the expression can be copied as input text via Copy As Input Text (in the Edit menu or context menu)
With the default setting "ShowAllSteps"False, trivial steps are automatically omitted from the trace to reduce clutter. Steps are considered trivial if they have no sub-steps, and their input and output are the same. The resulting trace may therefore be shorter than the corresponding output from TracePrint.
Setting "ShowAllSteps"True forces ResourceFunction["TraceView"] to include all steps.
Below the main trace display, a toolbar with the most common settings is displayed to allow on-the-fly changes. The accessible settings are "TimesRelativeTo", "MaxDisplayDepth" and "MaxChildDisplay".
The toolbar also contains a search bar, which allows the trace to be searched for a given pattern. Both pattern expressions and abbreviated string patterns (as those supported by Names) are supported.
ResourceFunction["TraceView"] is designed to efficiently handle and display large execution traces, with traces up to 1000000 steps being processed in well below a minute, and with no slowdown during the interactive exploration of the trace.
In order to maintain acceptable performance levels for large traces, the trace output is not stored in the notebook, and will not be available in future kernel sessions.
Although ResourceFunction["TraceView"] is optimized for low overhead, the timings shown might not be entirely accurate, especially for many fast steps. Limiting the number of profiled steps, for example using the form argument or the TraceDepth option, can provide more accurate results.

Examples

Basic Examples (3) 

View the execution trace for a simple expression:

In[1]:=
ResourceFunction["TraceView"][f[1, 2 + 3]]
Out[1]=

Investigate a recursive function:

In[2]:=
fib[0] = fib[1] = 1; fib[n_] := fib[n - 1] + fib[n - 2];
In[3]:=
ResourceFunction["TraceView"][fib[10]]
Out[3]=

Add memoization, and show only evaluations of the function itself:

In[4]:=
fib[n_] := (fib[n] = fib[n - 1] + fib[n - 2]);
In[5]:=
ResourceFunction["TraceView"][fib[10], fib]
Out[5]=

Investigate a slowdown by drilling down to the step with an unusually large own-time:

In[6]:=
func[x_] := (Pause@If[x == 3(*simulate a slow case*), 3, 0.2]; x^2)
ResourceFunction["TraceView"][Array[func, 7]]
Out[7]=

Increase the display limits to see the entire trace at once:

Out[8]=

Toggle the display of the time bars to be relative to the immediate parent step:

Out[9]=

Scope (2) 

Attempt to implement a function that walks through one list with step sizes given by another list:

In[10]:=
SeedRandom[3];
steps = RandomInteger[{-5, 5}, 9]
data = Range@10;
res = FoldList[data[[# + #2]] &, steps];
Out[11]=

Investigate the message by tracing the evaluation:

In[12]:=
ResourceFunction["TraceView"][FoldList[data[[# + #2]] &, steps]]
Out[12]=

Using the search field in the toolbar, navigate to the first occurrence of Message to investigate the issue:

Out[12]=

Trace built-in functions:

In[13]:=
ResourceFunction["TraceView"][Plot[x^2, {x, 0, 1}]]
Out[13]=

Options (16) 

TimesRelativeTo (3) 

With the default setting "TimesRelativeTo""Outer", time bars are shown relative to the time taken for the outermost visible step:

In[14]:=
ResourceFunction["TraceView"][
 Table[{Table[{Pause@0.1, Pause@0.1}, 3]}, 3]]
Out[14]=

Show times relative to the immediate parent step instead:

In[15]:=
ResourceFunction["TraceView"][
 Table[{Table[{Pause@0.1, Pause@0.1}, 3]}, 3], "TimesRelativeTo" -> "Parent"]
Out[15]=

Toggle the setting using the toolbar instead, and change it to show times relative to the total time taken:

In[16]:=
ResourceFunction["TraceView"][
 Table[{Table[{Pause@0.1, Pause@0.1}, 3]}, 3]]
Out[16]=

MaxDisplayDepth (3) 

With the default setting "MaxDisplayDepth"4, at most 4 levels of the call tree are shown at once:

In[17]:=
fac[n_] := fac[n - 1] n; fac[0] = 1;
ResourceFunction["TraceView"][fac[5]]
Out[18]=

Show 5 levels instead:

In[19]:=
fac[n_] := fac[n - 1] n; fac[0] = 1;
ResourceFunction["TraceView"][fac[5], "MaxDisplayDepth" -> 5]
Out[19]=

Change the setting using the toolbar instead:

In[20]:=
fac[n_] := fac[n - 1] n; fac[0] = 1;
ResourceFunction["TraceView"][fac[5]]
Out[20]=

MaxChildDisplay (3) 

With the default setting "MaxChildDisplay"5, at most 5 sub-steps are shown at once:

In[21]:=
ResourceFunction["TraceView"][Table[Table[i*j, {j, 10}], {i, 10}]]
Out[21]=

Reduce the number to 3 to make the trace view more compact:

In[22]:=
ResourceFunction["TraceView"][Table[Table[i*j, {j, 10}], {i, 10}], "MaxChildDisplay" -> 3]
Out[22]=

Change the setting using the toolbar instead:

In[23]:=
ResourceFunction["TraceView"][Table[Table[i*j, {j, 10}], {i, 10}]]
Out[23]=

MaxExpressionPaneSize (2) 

With the default setting "MaxExpressionPaneSize"AbsoluteCurrentValue[WindowSize]/{3,6}, the cells containing the expressions are at most one third of the horizontal window size and one sixth of the vertical size:

In[24]:=
ResourceFunction["TraceView"][Table[Range[i]!^4, {i, 10}]]
Out[24]=

Reduce the size further:

In[25]:=
ResourceFunction["TraceView"][Table[Range[i]!^4, {i, 10}], "MaxExpressionPaneSize" -> {200, 100}]
Out[25]=

DefaultExpressionSizeLimit (3) 

With the default setting "DefaultExpressionSizeLimit"1000, parts of the expressions are elided if their boxes take up more than around 1000 bytes:

In[26]:=
ResourceFunction["TraceView"][
 Table[Inactive[Plus] @@ Table[i j, {j, 50}], {i, 2}]]
Out[26]=

Show more of the expressions by default:

In[27]:=
ResourceFunction["TraceView"][
 Table[Inactive[Plus] @@ Table[i j, {j, 50}], {i, 2}], "DefaultExpressionSizeLimit" -> 2500]
Out[27]=

Reduce the initial size, but selectively dig in by clicking on the elision indicators:

In[28]:=
ResourceFunction["TraceView"][
 Table[Inactive[Plus] @@ Table[i j, {j, 50}], {i, 2}], "DefaultExpressionSizeLimit" -> 250]
Out[28]=

ShowAllSteps (2) 

With the default setting "ShowAllSteps"False, trivial steps are omitted:

In[29]:=
ResourceFunction["TraceView"][First[{1, 2, 3 + 4}]]
Out[29]=

Force all steps to be shown:

In[30]:=
ResourceFunction["TraceView"][Last[{1, 2, 3 + 4}], "ShowAllSteps" -> True]
Out[30]=

This output closely matches the output of TracePrint:

In[31]:=
TracePrint[Last[{1, 2, 3 + 4}]]
Out[31]=

Properties and Relations (1) 

TraceView is effectively a pretty-printed version of Trace:

In[32]:=
ResourceFunction["TraceView"][Table[i^2 + i, {i, 3}]]
Out[32]=
In[33]:=
Trace[Table[i^2 + i, {i, 3}]]
Out[33]=

Possible Issues (1) 

The search pattern must either be a valid abbreviated string pattern or a complete expression pattern:

In[34]:=
ResourceFunction["TraceView"][Table[i^2, {i, 3}]]
Out[34]=

Publisher

Lukas Lang

Version History

  • 1.1.1 – 21 August 2023
  • 1.1.0 – 13 February 2023
  • 1.0.1 – 25 January 2023
  • 1.0.0 – 17 January 2023

Source Metadata

Related Resources

Author Notes

Changes from version 1.0.0 to 1.0.1:

Fixed contexts not being properly elided in some output expressions
Fixed functions like Throw, Break, … completely breaking TraceView

License Information