Skip to content

Commit

Permalink
Move clear_malloc_data into Profile and add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Oct 31, 2014
1 parent 9451b33 commit 68a09e0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 13 deletions.
3 changes: 3 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ const Ranges = Range
export Range1
const Range1 = UnitRange

export clear_malloc_data
@deprecate clear_malloc_data() Profile.clear_malloc_data()

This comment has been minimized.

Copy link
@ivarne

ivarne Oct 31, 2014

Member

Isn't the export clear_malloc_data redundant here? As far as I can see @deprecate will automatically add a export declaration.

This comment has been minimized.

Copy link
@timholy

timholy Oct 31, 2014

Author Member

OK, see ce8c625.


@deprecate set_rounding(r::RoundingMode) set_rounding(Float64,r)
@deprecate get_rounding() get_rounding(Float64)
@deprecate with_rounding(f::Function, r::RoundingMode) with_rounding(f::Function, Float64, r)
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,6 @@ export
gc_disable,
gc_enable,
precompile,
clear_malloc_data,

# misc
atexit,
Expand Down
7 changes: 7 additions & 0 deletions base/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ callers(funcname::ByteString; kwargs...) = callers(funcname, retrieve()...; kwar
callers(func::Function, bt::Vector{Uint}, lidict; kwargs...) = callers(string(func), bt, lidict; kwargs...)
callers(func::Function; kwargs...) = callers(string(func), retrieve()...; kwargs...)

##
## For --track-allocation
##
# Reset the malloc log. Used to avoid counting memory allocated during
# compilation.
clear_malloc_data() = ccall(:jl_clear_malloc_data, Void, ())


####
#### Internal interface
Expand Down
3 changes: 0 additions & 3 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ gc_time_ns() = ccall(:jl_gc_total_hrtime, Uint64, ())
# total number of bytes allocated so far
gc_bytes() = ccall(:jl_gc_total_bytes, Int64, ())

# reset the malloc log. Used to avoid counting memory allocated during compilation.
clear_malloc_data() = ccall(:jl_clear_malloc_data, Void, ())

function tic()
t0 = time_ns()
task_local_storage(:TIMERS, (t0, get(task_local_storage(), :TIMERS, ())))
Expand Down
2 changes: 1 addition & 1 deletion doc/manual/performance-tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ diagnose problems and improve the performance of your code:
suspect a type problem. You can also start julia with the
``--track-allocation=user`` option and examine the resulting
``*.mem`` files to see information about where those allocations
occur.
occur. See :ref:`stdlib-track-allocation`.

- The `TypeCheck <https://github.com/astrieanna/TypeCheck.jl>`_
package can help identify certain kinds of type problems. A more
Expand Down
8 changes: 0 additions & 8 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,6 @@ Getting Around

This function should only be used interactively.

.. function:: clear_malloc_data()

Clears any stored memory allocation data when running julia with
``--track-allocation``. Execute the command(s) you want to test
(to force JIT-compilation), then call ``clear_malloc_data()``.
Then execute your command(s) again, quit julia, and examine the
resulting ``*.mem`` files.

All Objects
-----------

Expand Down
44 changes: 44 additions & 0 deletions doc/stdlib/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,42 @@ as well as increase it; however, the overhead of profiling grows once
the delay becomes similar to the amount of time needed to take a
backtrace (~30 microseconds on the author's laptop).

.. _stdlib-track-allocation:

Direct analysis of memory allocation
====================================

One of the most common techniques to improve performance is to reduce
memory allocation. The total amount of allocation can be measured
with ``@time`` and ``@allocated``, and specific lines triggering
allocation can often be inferred from profiling via the cost of
garbage collection that these lines incur. However, sometimes it is
more efficient to directly measure the amount of memory allocated by
each line of code.

To measure allocation line-by-line, start julia with the
``--track-allocation=<setting>`` command-line option, for which you
can choose ``none`` (the default, do not measure allocation), ``user``
(measure memory allocation everywhere except julia's core code), or
``all`` (measure memory allocation at each line of julia code).
Allocation gets measured for each line of compiled code. When you quit
julia, the cumulative results are written to text files with ``.mem``
appended after the file name, residing in the same directory as the
source file. Each line lists the total number of bytes allocated.
The ``Coverage`` package contains some elementary analysis tools, for
example to sort the lines in order of number of bytes allocated.

In interpreting the results, there are a few important details. Under
the ``user`` setting, the first line of any function directly called
from the REPL will exhibit allocation due to events that happen in the
REPL code itself. More significantly, JIT-compilation also adds to
allocation counts, because much of julia's compiler is written in
Julia (and compilation usually requires memory allocation). The
recommended procedure it to force compilation by executing all the
commands you want to analyze, then call ``clear_malloc_data()`` to
reset all allocation counters. Finally, execute the desired commands
and quit julia to trigger the generation of the ``.mem`` files.

Function reference
------------------

Expand Down Expand Up @@ -366,3 +402,11 @@ Function reference
about the caller. One can optionally supply backtrace data
obtained from ``retrieve``; otherwise, the current internal profile
buffer is used.
.. function:: clear_malloc_data()

Clears any stored memory allocation data when running julia with
``--track-allocation``. Execute the command(s) you want to test
(to force JIT-compilation), then call ``clear_malloc_data()``.
Then execute your command(s) again, quit julia, and examine the
resulting ``*.mem`` files.

5 comments on commit 68a09e0

@timholy
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better?

@JeffBezanson
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Very nice.

@tkelman
Copy link
Contributor

@tkelman tkelman commented on 68a09e0 Jan 4, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--track-allocation works on release-0.3, right? should we backport the documentation part of this? @JuliaBackports

@timholy
Copy link
Member Author

@timholy timholy commented on 68a09e0 Jan 4, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

@tkelman
Copy link
Contributor

@tkelman tkelman commented on 68a09e0 Jan 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backported in c951564, thanks @staticfloat

Please sign in to comment.