forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.pxi
More file actions
153 lines (121 loc) · 4.61 KB
/
Copy pathmemory.pxi
File metadata and controls
153 lines (121 loc) · 4.61 KB
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
# 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.
# cython: profile=False
# distutils: language = c++
# cython: embedsignature = True
cdef class MemoryPool:
"""
Base class for memory allocation.
Besides tracking its number of allocated bytes, a memory pool also
takes care of the required 64-byte alignment for Arrow data.
"""
def __init__(self):
raise TypeError("Do not call {}'s constructor directly, "
"use pyarrow.*_memory_pool instead."
.format(self.__class__.__name__))
cdef void init(self, CMemoryPool* pool):
self.pool = pool
def bytes_allocated(self):
"""
Return the number of bytes that are currently allocated from this
memory pool.
"""
return self.pool.bytes_allocated()
def max_memory(self):
"""
Return the peak memory allocation in this memory pool.
This can be an approximate number in multi-threaded applications.
None is returned if the pool implementation doesn't know how to
compute this number.
"""
ret = self.pool.max_memory()
return ret if ret >= 0 else None
cdef CMemoryPool* maybe_unbox_memory_pool(MemoryPool memory_pool):
if memory_pool is None:
return c_get_memory_pool()
else:
return memory_pool.pool
cdef class LoggingMemoryPool(MemoryPool):
cdef:
unique_ptr[CLoggingMemoryPool] logging_pool
def __init__(self):
raise TypeError("Do not call {}'s constructor directly, "
"use pyarrow.logging_memory_pool instead."
.format(self.__class__.__name__))
cdef class ProxyMemoryPool(MemoryPool):
"""
Memory pool implementation that tracks the number of bytes and
maximum memory allocated through its direct calls, while redirecting
to another memory pool.
"""
cdef:
unique_ptr[CProxyMemoryPool] proxy_pool
def __init__(self):
raise TypeError("Do not call {}'s constructor directly, "
"use pyarrow.proxy_memory_pool instead."
.format(self.__class__.__name__))
def default_memory_pool():
"""
Return the process-global memory pool.
"""
cdef:
MemoryPool pool = MemoryPool.__new__(MemoryPool)
pool.init(c_get_memory_pool())
return pool
def proxy_memory_pool(MemoryPool parent):
"""
Create and return a MemoryPool instance that redirects to the
*parent*, but with separate allocation statistics.
"""
cdef ProxyMemoryPool out = ProxyMemoryPool.__new__(ProxyMemoryPool)
out.proxy_pool.reset(new CProxyMemoryPool(parent.pool))
out.init(out.proxy_pool.get())
return out
def logging_memory_pool(MemoryPool parent):
"""
Create and return a MemoryPool instance that redirects to the
*parent*, but also dumps allocation logs on stderr.
"""
cdef LoggingMemoryPool out = LoggingMemoryPool.__new__(
LoggingMemoryPool, parent)
out.logging_pool.reset(new CLoggingMemoryPool(parent.pool))
out.init(out.logging_pool.get())
return out
def set_memory_pool(MemoryPool pool):
c_set_default_memory_pool(pool.pool)
cdef MemoryPool _default_memory_pool = default_memory_pool()
cdef LoggingMemoryPool _logging_memory_pool = logging_memory_pool(
_default_memory_pool)
def log_memory_allocations(enable=True):
"""
Enable or disable memory allocator logging for debugging purposes
Parameters
----------
enable : boolean, default True
Pass False to disable logging
"""
if enable:
set_memory_pool(_logging_memory_pool)
else:
set_memory_pool(_default_memory_pool)
def total_allocated_bytes():
"""
Return the currently allocated bytes from the default memory pool.
Other memory pools may not be accounted for.
"""
cdef CMemoryPool* pool = c_get_memory_pool()
return pool.bytes_allocated()