File: memory.rst

package info (click to toggle)
apache-arrow 23.0.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 76,220 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,365; makefile: 669; javascript: 125; xml: 41
file content (443 lines) | stat: -rw-r--r-- 19,031 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements.  See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership.  The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License.  You may obtain a copy of the License at

..   http://www.apache.org/licenses/LICENSE-2.0

.. Unless required by applicable law or agreed to in writing,
.. software distributed under the License is distributed on an
.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
.. KIND, either express or implied.  See the License for the
.. specific language governing permissions and limitations
.. under the License.

.. default-domain:: cpp
.. highlight:: cpp

.. _cpp_memory_management:

=================
Memory Management
=================

.. seealso::
   :doc:`Memory management API reference <api/memory>`

Buffers
=======

To avoid passing around raw data pointers with varying and non-obvious
lifetime rules, Arrow provides a generic abstraction called :class:`arrow::Buffer`.
A Buffer encapsulates a pointer and data size, and generally also ties its
lifetime to that of an underlying provider (in other words, a Buffer should
*always* point to valid memory till its destruction).  Buffers are untyped:
they simply denote a physical memory area regardless of its intended meaning
or interpretation.

Buffers may be allocated by Arrow itself , or by third-party routines.
For example, it is possible to pass the data of a Python bytestring as a Arrow
buffer, keeping the Python object alive as necessary.

In addition, buffers come in various flavours: mutable or not, resizable or
not.  Generally, you will hold a mutable buffer when building up a piece
of data, then it will be frozen as an immutable container such as an
:doc:`array <arrays>`.

.. note::
   Some buffers may point to non-CPU memory, such as GPU-backed memory
   provided by a CUDA context.  If you're writing a GPU-aware application,
   you will need to be careful not to interpret a GPU memory pointer as
   a CPU-reachable pointer, or vice-versa.

Accessing Buffer Memory
-----------------------

Buffers provide fast access to the underlying memory using the
:func:`~arrow::Buffer::size` and :func:`~arrow::Buffer::data` accessors
(or :func:`~arrow::Buffer::mutable_data` for writable access to a mutable
buffer).

Slicing
-------

It is possible to make zero-copy slices of buffers, to obtain a buffer
referring to some contiguous subset of the underlying data.  This is done
by calling the :func:`arrow::SliceBuffer` and :func:`arrow::SliceMutableBuffer`
functions.

Allocating a Buffer
-------------------

You can allocate a buffer yourself by calling one of the
:func:`arrow::AllocateBuffer` or :func:`arrow::AllocateResizableBuffer`
overloads::

   arrow::Result<std::unique_ptr<Buffer>> maybe_buffer = arrow::AllocateBuffer(4096);
   if (!maybe_buffer.ok()) {
      // ... handle allocation error
   }

   std::shared_ptr<arrow::Buffer> buffer = *std::move(maybe_buffer);
   uint8_t* buffer_data = buffer->mutable_data();
   memcpy(buffer_data, "hello world", 11);

Allocating a buffer this way ensures it is 64-bytes aligned and padded
as recommended by the :doc:`Arrow memory specification <../format/Layout>`.

Building a Buffer
-----------------

You can also allocate *and* build a Buffer incrementally, using the
:class:`arrow::BufferBuilder` API::

   BufferBuilder builder;
   builder.Resize(11);  // reserve enough space for 11 bytes
   builder.Append("hello ", 6);
   builder.Append("world", 5);

   auto maybe_buffer = builder.Finish();
   if (!maybe_buffer.ok()) {
      // ... handle buffer allocation error
   }
   std::shared_ptr<arrow::Buffer> buffer = *maybe_buffer;

If a Buffer is meant to contain values of a given fixed-width type (for
example the 32-bit offsets of a List array), it can be more convenient to
use the template :class:`arrow::TypedBufferBuilder` API::

   TypedBufferBuilder<int32_t> builder;
   builder.Reserve(2);  // reserve enough space for two int32_t values
   builder.Append(0x12345678);
   builder.Append(-0x765643210);

   auto maybe_buffer = builder.Finish();
   if (!maybe_buffer.ok()) {
      // ... handle buffer allocation error
   }
   std::shared_ptr<arrow::Buffer> buffer = *maybe_buffer;

.. _cpp_memory_pool:

Memory Pools
============

When allocating a Buffer using the Arrow C++ API, the buffer's underlying
memory is allocated by a :class:`arrow::MemoryPool` instance.  Usually this
will be the process-wide *default memory pool*, but many Arrow APIs allow
you to pass another MemoryPool instance for their internal allocations.

Memory pools are used for large long-lived data such as array buffers.
Other data, such as small C++ objects and temporary workspaces, usually
goes through the regular C++ allocators.

Default Memory Pool
-------------------

The default memory pool depends on how Arrow C++ was compiled:

- if enabled at compile time, a `mimalloc <https://github.com/microsoft/mimalloc>`_
  heap;
- otherwise, if enabled at compile time, a `jemalloc <http://jemalloc.net/>`_ heap;
- otherwise, the C library ``malloc`` heap.

Overriding the Default Memory Pool
----------------------------------

One can override the above selection algorithm by setting the
:envvar:`ARROW_DEFAULT_MEMORY_POOL` environment variable.

STL Integration
---------------

If you wish to use a Arrow memory pool to allocate the data of STL containers,
you can do so using the :class:`arrow::stl::allocator` wrapper.

Conversely, you can also use a STL allocator to allocate Arrow memory,
using the :class:`arrow::stl::STLMemoryPool` class.  However, this may be less
performant, as STL allocators don't provide a resizing operation.

Devices
=======

Many Arrow applications only access host (CPU) memory.  However, in some cases
it is desirable to handle on-device memory (such as on-board memory on a GPU)
as well as host memory.

Arrow represents the CPU and other devices using the
:class:`arrow::Device` abstraction.  The associated class :class:`arrow::MemoryManager`
specifies how to allocate on a given device.  Each device has a default memory manager, but
additional instances may be constructed (for example, wrapping a custom
:class:`arrow::MemoryPool` the CPU).
:class:`arrow::MemoryManager` instances which specify how to allocate
memory on a given device (for example, using a particular
:class:`arrow::MemoryPool` on the CPU).

Device-Agnostic Programming
---------------------------

If you receive a Buffer from third-party code, you can query whether it is
CPU-readable by calling its :func:`~arrow::Buffer::is_cpu` method.

You can also view the Buffer on a given device, in a generic way, by calling
:func:`arrow::Buffer::View` or :func:`arrow::Buffer::ViewOrCopy`.  This will
be a no-operation if the source and destination devices are identical.
Otherwise, a device-dependent mechanism will attempt to construct a memory
address for the destination device that gives access to the buffer contents.
Actual device-to-device transfer may happen lazily, when reading the buffer
contents.

Similarly, if you want to do I/O on a buffer without assuming a CPU-readable
buffer, you can call :func:`arrow::Buffer::GetReader` and
:func:`arrow::Buffer::GetWriter`.

For example, to get an on-CPU view or copy of an arbitrary buffer, you can
simply do::

   std::shared_ptr<arrow::Buffer> arbitrary_buffer = ... ;
   std::shared_ptr<arrow::Buffer> cpu_buffer = arrow::Buffer::ViewOrCopy(
      arbitrary_buffer, arrow::default_cpu_memory_manager());


Memory Profiling
================

On Linux, detailed profiles of memory allocations can be generated using
``perf record``, without any need to modify the binaries. These profiles can
show the traceback in addition to allocation size. This does require debug
symbols, from either a debug build or a release with debug symbols build.

.. note::
   If you are profiling Arrow's tests on another platform, you can run the
   following Docker container using Archery to access a Linux environment:

   .. code-block:: shell

      archery docker run ubuntu-cpp bash
      # Inside the Docker container...
      /arrow/ci/scripts/cpp_build.sh /arrow /build
      cd build/cpp/debug
      ./arrow-array-test # Run a test
      apt-get update
      apt-get install -y linux-tools-generic
      alias perf=/usr/lib/linux-tools/<version-path>/perf


To track allocations, create probe points on each of the allocator methods used.
Collecting ``$params`` allows us to record the size of the allocations
requested, while collecting ``$retval`` allows us to record the address of
recorded allocations, so we can correlate them with the call to free/de-allocate.

.. tab-set::

   .. tab-item:: jemalloc

      .. code-block:: shell

         perf probe -x libarrow.so je_arrow_mallocx '$params'
         perf probe -x libarrow.so je_arrow_mallocx%return '$retval'
         perf probe -x libarrow.so je_arrow_rallocx '$params'
         perf probe -x libarrow.so je_arrow_rallocx%return '$retval'
         perf probe -x libarrow.so je_arrow_dallocx '$params'
         PROBE_ARGS="-e probe_libarrow:je_arrow_mallocx \
            -e probe_libarrow:je_arrow_mallocx__return \
            -e probe_libarrow:je_arrow_rallocx \
            -e probe_libarrow:je_arrow_rallocx__return \
            -e probe_libarrow:je_arrow_dallocx"

   .. tab-item:: mimalloc

      .. code-block:: shell

         perf probe -x libarrow.so mi_malloc_aligned '$params'
         perf probe -x libarrow.so mi_malloc_aligned%return '$retval'
         perf probe -x libarrow.so mi_realloc_aligned '$params'
         perf probe -x libarrow.so mi_realloc_aligned%return '$retval'
         perf probe -x libarrow.so mi_free '$params'
         PROBE_ARGS="-e probe_libarrow:mi_malloc_aligned \
            -e probe_libarrow:mi_malloc_aligned__return \
            -e probe_libarrow:mi_realloc_aligned \
            -e probe_libarrow:mi_realloc_aligned__return \
            -e probe_libarrow:mi_free"

Once probes have been set, you can record calls with associated tracebacks using
``perf record``. In this example, we are running the StructArray unit tests in
Arrow:

.. code-block:: shell

   perf record -g --call-graph dwarf \
     $PROBE_ARGS \
     ./arrow-array-test --gtest_filter=StructArray*

If you want to profile a running process, you can run ``perf record -p <PID>``
and it will record until you interrupt with CTRL+C. Alternatively, you can do
``perf record -P <PID> sleep 10`` to record for 10 seconds.

The resulting data can be processed with standard tools to work with perf or
``perf script`` can be used to pipe a text format of the data to custom scripts.
The following script parses ``perf script`` output and prints the output in
new lines delimited JSON for easier processing.

.. code-block:: python
   :caption: process_perf_events.py

   import sys
   import re
   import json

   # Example non-traceback line
   # arrow-array-tes 14344 [003]  7501.073802: probe_libarrow:je_arrow_mallocx: (7fbcd20bb640) size=0x80 flags=6

   current = {}
   current_traceback = ''

   def new_row():
       global current_traceback
       current['traceback'] = current_traceback
       print(json.dumps(current))
       current_traceback = ''

   for line in sys.stdin:
       if line == '\n':
           continue
       elif line[0] == '\t':
           # traceback line
           current_traceback += line.strip("\t")
       else:
           line = line.rstrip('\n')
           if not len(current) == 0:
               new_row()
           parts = re.sub(' +', ' ', line).split(' ')

           parts.reverse()
           parts.pop() # file
           parts.pop() # "14344"
           parts.pop() # "[003]"

           current['time'] = float(parts.pop().rstrip(":"))
           current['event'] = parts.pop().rstrip(":")

           parts.pop() # (7fbcd20bddf0)
           if parts[-1] == "<-":
               parts.pop()
               parts.pop()

           params = {}

           for pair in parts:
               key, value = pair.split("=")
               params[key] = value

           current['params'] = params


Here's an example invocation of that script, with a preview of output data:

.. code-block:: console

   $ perf script | python3 /arrow/process_perf_events.py > processed_events.jsonl
   $ head processed_events.jsonl | cut -c -120
   {"time": 14814.954378, "event": "probe_libarrow:je_arrow_mallocx", "params": {"flags": "6", "size": "0x80"}, "traceback"
   {"time": 14814.95443, "event": "probe_libarrow:je_arrow_mallocx__return", "params": {"arg1": "0x7f4a97e09000"}, "traceba
   {"time": 14814.95448, "event": "probe_libarrow:je_arrow_mallocx", "params": {"flags": "6", "size": "0x40"}, "traceback":
   {"time": 14814.954486, "event": "probe_libarrow:je_arrow_mallocx__return", "params": {"arg1": "0x7f4a97e0a000"}, "traceb
   {"time": 14814.954502, "event": "probe_libarrow:je_arrow_rallocx", "params": {"flags": "6", "size": "0x40", "ptr": "0x7f
   {"time": 14814.954507, "event": "probe_libarrow:je_arrow_rallocx__return", "params": {"arg1": "0x7f4a97e0a040"}, "traceb
   {"time": 14814.954796, "event": "probe_libarrow:je_arrow_mallocx", "params": {"flags": "6", "size": "0x40"}, "traceback"
   {"time": 14814.954805, "event": "probe_libarrow:je_arrow_mallocx__return", "params": {"arg1": "0x7f4a97e0a080"}, "traceb
   {"time": 14814.954817, "event": "probe_libarrow:je_arrow_mallocx", "params": {"flags": "6", "size": "0x40"}, "traceback"
   {"time": 14814.95482, "event": "probe_libarrow:je_arrow_mallocx__return", "params": {"arg1": "0x7f4a97e0a0c0"}, "traceba


From there one can answer a number of questions. For example, the following
script will find which allocations were never freed, and print the associated
tracebacks along with the count of dangling allocations:

.. code-block:: python
   :caption: count_tracebacks.py

   '''Find tracebacks of allocations with no corresponding free'''
   import sys
   import json
   from collections import defaultdict

   allocated = dict()

   for line in sys.stdin:
       line = line.rstrip('\n')
       data = json.loads(line)

       if data['event'] == "probe_libarrow:je_arrow_mallocx__return":
           address = data['params']['arg1']
           allocated[address] = data['traceback']
       elif data['event'] == "probe_libarrow:je_arrow_rallocx":
           address = data['params']['ptr']
           del allocated[address]
       elif data['event'] == "probe_libarrow:je_arrow_rallocx__return":
           address = data['params']['arg1']
           allocated[address] = data['traceback']
       elif data['event'] == "probe_libarrow:je_arrow_dallocx":
           address = data['params']['ptr']
           if address in allocated:
               del allocated[address]
       elif data['event'] == "probe_libarrow:mi_malloc_aligned__return":
           address = data['params']['arg1']
           allocated[address] = data['traceback']
       elif data['event'] == "probe_libarrow:mi_realloc_aligned":
           address = data['params']['p']
           del allocated[address]
       elif data['event'] == "probe_libarrow:mi_realloc_aligned__return":
           address = data['params']['arg1']
           allocated[address] = data['traceback']
       elif data['event'] == "probe_libarrow:mi_free":
           address = data['params']['p']
           if address in allocated:
               del allocated[address]

   traceback_counts = defaultdict(int)

   for traceback in allocated.values():
       traceback_counts[traceback] += 1

   for traceback, count in sorted(traceback_counts.items(), key=lambda x: -x[1]):
       print("Num of dangling allocations:", count)
       print(traceback)


The script can be invoked like so:

.. code-block:: console

   $ cat processed_events.jsonl | python3 /arrow/count_tracebacks.py
   Num of dangling allocations: 1
    7fc945e5cfd2 arrow::(anonymous namespace)::JemallocAllocator::ReallocateAligned+0x13b (/build/cpp/debug/libarrow.so.700.0.0)
    7fc945e5fe4f arrow::BaseMemoryPoolImpl<arrow::(anonymous namespace)::JemallocAllocator>::Reallocate+0x93 (/build/cpp/debug/libarrow.so.700.0.0)
    7fc945e618f7 arrow::PoolBuffer::Resize+0xed (/build/cpp/debug/libarrow.so.700.0.0)
    55a38b163859 arrow::BufferBuilder::Resize+0x12d (/build/cpp/debug/arrow-array-test)
    55a38b163bbe arrow::BufferBuilder::Finish+0x48 (/build/cpp/debug/arrow-array-test)
    55a38b163e3a arrow::BufferBuilder::Finish+0x50 (/build/cpp/debug/arrow-array-test)
    55a38b163f90 arrow::BufferBuilder::FinishWithLength+0x4e (/build/cpp/debug/arrow-array-test)
    55a38b2c8fa7 arrow::TypedBufferBuilder<int, void>::FinishWithLength+0x4f (/build/cpp/debug/arrow-array-test)
    55a38b2bcce7 arrow::NumericBuilder<arrow::Int32Type>::FinishInternal+0x107 (/build/cpp/debug/arrow-array-test)
    7fc945c065ae arrow::ArrayBuilder::Finish+0x5a (/build/cpp/debug/libarrow.so.700.0.0)
    7fc94736ed41 arrow::ipc::internal::json::(anonymous namespace)::Converter::Finish+0x123 (/build/cpp/debug/libarrow.so.700.0.0)
    7fc94737426e arrow::ipc::internal::json::ArrayFromJSON+0x299 (/build/cpp/debug/libarrow.so.700.0.0)
    7fc948e98858 arrow::ArrayFromJSON+0x64 (/build/cpp/debug/libarrow_testing.so.700.0.0)
    55a38b6773f3 arrow::StructArray_FlattenOfSlice_Test::TestBody+0x79 (/build/cpp/debug/arrow-array-test)
    7fc944689633 testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>+0x68 (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)
    7fc94468132a testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>+0x5d (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)
    7fc9446555eb testing::Test::Run+0xf1 (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)
    7fc94465602d testing::TestInfo::Run+0x13f (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)
    7fc944656947 testing::TestSuite::Run+0x14b (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)
    7fc9446663f5 testing::internal::UnitTestImpl::RunAllTests+0x433 (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)
    7fc94468ab61 testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>+0x68 (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)
    7fc944682568 testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>+0x5d (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)
    7fc944664b0c testing::UnitTest::Run+0xcc (/build/cpp/googletest_ep-prefix/lib/libgtestd.so.1.11.0)
    7fc9446d0299 RUN_ALL_TESTS+0x14 (/build/cpp/googletest_ep-prefix/lib/libgtest_maind.so.1.11.0)
    7fc9446d021b main+0x42 (/build/cpp/googletest_ep-prefix/lib/libgtest_maind.so.1.11.0)
    7fc9441e70b2 __libc_start_main+0xf2 (/usr/lib/x86_64-linux-gnu/libc-2.31.so)
    55a38b10a50d _start+0x2d (/build/cpp/debug/arrow-array-test)