33LuaJIT memory profiler
44======================
55
6- Starting from version :doc: `2.7.1 </release/2.7.1 >`, Tarantool
6+ Since version :doc: `2.7.1 </release/2.7.1 >`, Tarantool
77has a built‑in module called ``misc.memprof `` that implements a LuaJIT memory
8- profiler (which we will just call *the profiler * in this section). The profiler provides
9- a memory allocation report that helps analyze Lua code and find the places
10- that put the most pressure on the Lua garbage collector (GC).
8+ profiler (further in this section we call it *the profiler * for short).
9+ The profiler provides a memory allocation report that helps analyze Lua code
10+ and find the places that put the most pressure on the Lua garbage collector (GC).
11+
12+ Inside this section:
1113
1214.. contents ::
1315 :local:
@@ -18,7 +20,7 @@ that put the most pressure on the Lua garbage collector (GC).
1820Working with the profiler
1921-------------------------
2022
21- Usage of the profiler involves two steps:
23+ The profiler usage involves two steps:
2224
23251. :ref: `Collecting <profiler_usage_get >` a binary profile of allocations,
2426 reallocations, and deallocations in memory related to Lua
@@ -28,13 +30,13 @@ Usage of the profiler involves two steps:
2830
2931.. _profiler_usage_get :
3032
31- Collecting binary profile
32- ~~~~~~~~~~~~~~~~~~~~~~~~~
33+ Collecting a binary profile
34+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
3335
3436To collect a binary profile for a particular part of the Lua code,
3537you need to place this part between two ``misc.memprof `` functions,
3638namely, ``misc.memprof.start() `` and ``misc.memprof.stop() ``, and then execute
37- the code under Tarantool.
39+ the code in Tarantool.
3840
3941Below is a chunk of Lua code named ``test.lua `` to illustrate this.
4042
@@ -75,6 +77,7 @@ for example if it is not possible to open a file for writing or if the profiler
7577``misc.memprof.start() `` returns ``nil `` as the first result,
7678an error-message string as the second result,
7779and a system-dependent error code number as the third result.
80+
7881If the operation succeeds, ``misc.memprof.start() `` returns ``true ``.
7982
8083The Lua code for stopping the profiler -- as in line 18 in the ``test.lua `` example above -- is:
@@ -89,13 +92,14 @@ or if there is a failure during reporting,
8992``misc.memprof.stop() `` returns ``nil `` as the first result,
9093an error-message string as the second result,
9194and a system-dependent error code number as the third result.
95+
9296If the operation succeeds, ``misc.memprof.stop() `` returns ``true ``.
9397
9498.. _profiler_usage_generate :
9599
96100To generate the file with memory profile in binary format
97101(in the :ref: `test.lua code example above <profiler_usage_example01 >`
98- the file name is ``memprof_new.bin ``), execute the code under Tarantool:
102+ the file name is ``memprof_new.bin ``), execute the code in Tarantool:
99103
100104.. code-block :: console
101105
@@ -127,8 +131,8 @@ a profiling report:
127131
128132.. _profiler_usage_parse :
129133
130- Parsing binary profile and generating profiling report
131- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134+ Parsing a binary profile and generating a profiling report
135+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132136
133137.. _profiler_usage_parse_command :
134138
@@ -143,8 +147,11 @@ via Tarantool by using the following command
143147
144148 where ``memprof_new.bin `` is the binary profile
145149:ref: `generated earlier <profiler_usage_generate >` by ``tarantool test.lua ``.
146- (Warning: there is a slight behavior change here, the ``tarantool -e ... ``
147- command was slightly different in Tarantool versions prior to Tarantool 2.8.1.)
150+
151+ .. note ::
152+
153+ There is a slight behavior change here: the ``tarantool -e ... `` command
154+ was slightly different in Tarantool versions prior to Tarantool 2.8.1.
148155
149156Tarantool generates a profiling report and displays it on the console before closing
150157the session:
@@ -187,7 +194,6 @@ the session:
187194 On macOS, a report will be different for the same chunk of code because
188195 Tarantool and LuaJIT are built with the GC64 mode enabled for macOS.
189196
190-
191197Let's examine the report structure. A report has four sections:
192198
193199* ALLOCATIONS
@@ -255,10 +261,10 @@ structures.
255261As for investigating the Lua code with the help of profiling reports,
256262it is always code-dependent and there can't be hundred per cent definite
257263recommendations in this regard. Nevertheless, you can see some of the things
258- in the :ref: `Profiling report analysis example <profiler_analysis >` later.
264+ in the :ref: `Profiling a report analysis example <profiler_analysis >` later.
259265
260266Also, below is the :ref: `FAQ <profiler_faq >` section with the questions that
261- most probably can arise while using profiler.
267+ most probably can arise while using the profiler.
262268
263269.. _profiler_faq :
264270
@@ -342,18 +348,18 @@ console for a running instance.
342348 log.warn("end of profile")
343349 end)
344350
345- where
351+ where:
346352
347- * ``FILENAME ``—the name of the binary file where profiling events are written
348- * ``TIME ``—duration of profiling, in seconds.
353+ * ``FILENAME ``—the name of the binary file where profiling events are written
354+ * ``TIME ``—duration of profiling, in seconds.
349355
350356Also, you can directly call ``misc.memprof.start() `` and ``misc.memprof.stop() ``
351357from a console.
352358
353359.. _profiler_analysis :
354360
355- Profiling report analysis example
356- ---------------------------------
361+ Profiling a report analysis example
362+ -----------------------------------
357363
358364In the example below, the following Lua code named ``format_concat.lua `` is
359365investigated with the help of the memory profiler reports.
@@ -395,7 +401,7 @@ investigated with the help of the memory profiler reports.
395401
396402 os.exit()
397403
398- When you run this code :ref: `under Tarantool <profiler_usage_generate >` and
404+ When you run this code :ref: `in Tarantool <profiler_usage_generate >` and
399405then :ref: `parse <profiler_usage_parse_command >` the binary memory profile
400406in /tmp/memprof_format_concat.bin,
401407you will get the following profiling report:
@@ -468,8 +474,8 @@ To profile only the ``concat()`` function, comment out line 23 (which is
468474 **Q **: But what will change if JIT compilation is enabled?
469475
470476**A **: In the :ref: `code <profiler_usage_example03 >`, comment out line 2 (which is
471- ``jit.off() ``) and run
472- the profiler . Now there are only 56 allocations in the report, and all other
477+ ``jit.off() ``) and run the profiler.
478+ Now there are only 56 allocations in the report, and all the other
473479allocations are JIT-related (see also the related
474480`dev issue <https://github.com/tarantool/tarantool/issues/5679 >`_):
475481
@@ -535,7 +541,7 @@ identifier is not yet compiled via LuaJIT. So, a trace can't be recorded and
535541the compiler doesn't perform the corresponding optimizations.
536542
537543If we change the ``format() `` function in lines 9-12 of the
538- :ref: `Profiling report analysis example <profiler_usage_example03 >`
544+ :ref: `Profiling a report analysis example <profiler_usage_example03 >`
539545in the following way
540546
541547.. code-block :: lua
@@ -591,4 +597,4 @@ for example
591597
592598 $ tarantool -e 'require("memprof")(arg)' - --leak-only memprof_new.bin
593599
594- When `--leak-only`` is used, only the HEAP SUMMARY section is displayed.
600+ When `` --leak-only `` is used, only the HEAP SUMMARY section is displayed.
0 commit comments