-
Notifications
You must be signed in to change notification settings - Fork 397
/
setup.py
353 lines (305 loc) · 10.7 KB
/
setup.py
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
import distutils.command.build
import distutils.log
import os
import pathlib
import subprocess
import sys
import tempfile
from sys import platform
from sys import version_info
import pkgconfig
from Cython.Build import cythonize
from setuptools import Extension
from setuptools import find_packages
from setuptools import setup
from setuptools.command.build_ext import build_ext as build_ext_orig
IS_MAC = sys.platform == "darwin"
IS_LINUX = "linux" in sys.platform
LIBBACKTRACE_LOCATION = (
pathlib.Path(__file__).parent / "src" / "vendor" / "libbacktrace"
).resolve()
LIBBACKTRACE_INCLUDEDIRS = LIBBACKTRACE_LOCATION / "install" / "include"
LIBBACKTRACE_LIBDIR = LIBBACKTRACE_LOCATION / "install" / "lib"
ASSETS_LOCATION = (
pathlib.Path(__file__).parent
/ "src"
/ "memray"
/ "reporters"
/ "templates"
/ "assets"
)
class BuildMemray(build_ext_orig):
def run(self):
self.build_js_files()
self.build_libbacktrace()
super().run()
def announce_and_run(self, command, **kwargs):
self.announce(
"executing command: `{}`".format(" ".join(command)),
level=distutils.log.INFO,
)
subprocess.run(command, check=True, **kwargs)
def build_libbacktrace(self):
archive_location = LIBBACKTRACE_LIBDIR / "libbacktrace.a"
if archive_location.exists():
return
if not LIBBACKTRACE_LOCATION.exists():
self.announce_and_run(
[f"{LIBBACKTRACE_LOCATION.parent / 'regenerate_libbacktrace.sh'}"],
cwd=LIBBACKTRACE_LOCATION.parent,
)
configure_cmd = [
f"{LIBBACKTRACE_LOCATION}/configure",
"--with-pic",
"--prefix",
f"{LIBBACKTRACE_LOCATION}/install",
"--includedir",
f"{LIBBACKTRACE_LOCATION}/install/include/libbacktrace",
]
libbacktrace_target = os.getenv("MEMRAY_LIBBACKTRACE_TARGET")
if libbacktrace_target is not None:
configure_cmd.extend(["--host", libbacktrace_target])
with tempfile.TemporaryDirectory() as tmpdirname:
self.announce_and_run(
configure_cmd,
cwd=tmpdirname,
)
self.announce_and_run(["make", "-j"], cwd=tmpdirname)
self.announce_and_run(["make", "install"], cwd=tmpdirname)
def build_js_files(self):
if any(ASSETS_LOCATION.glob("*.js")):
return
self.announce_and_run(["npm", "install"])
self.announce_and_run(["npm", "run-script", "build"])
install_requires = [
"jinja2 >= 2.9",
"typing_extensions; python_version < '3.8.0'",
"rich >= 11.2.0",
"textual >= 0.41.0",
]
docs_requires = [
"IPython",
"bump2version",
"sphinx",
"furo",
"sphinx-argparse",
"towncrier",
]
lint_requires = [
"black",
"flake8",
"isort",
"mypy",
"check-manifest",
]
test_requires = [
"Cython",
"greenlet; python_version < '3.13'",
"pytest",
"pytest-cov",
"ipython",
"setuptools; python_version >= '3.12'",
"pytest-textual-snapshot",
"textual >= 0.43, != 0.65.2, != 0.66",
"packaging",
]
benchmark_requires = [
"asv",
]
TEST_BUILD = False
if "--test-build" in sys.argv:
TEST_BUILD = True
sys.argv.remove("--test-build")
if os.getenv("CYTHON_TEST_MACROS", None) is not None:
TEST_BUILD = True
MINIMIZE_INLINING = os.getenv("MEMRAY_MINIMIZE_INLINING", "") != ""
COMPILER_DIRECTIVES = {
"language_level": 3,
"embedsignature": True,
"boundscheck": False,
"wraparound": False,
"cdivision": True,
"profile": False,
"linetrace": False,
"c_string_type": "unicode",
"c_string_encoding": "utf8",
}
EXTRA_COMPILE_ARGS = []
EXTRA_LINK_ARGS = []
UNDEF_MACROS = []
if MINIMIZE_INLINING:
EXTRA_COMPILE_ARGS.append("-Og")
else:
EXTRA_COMPILE_ARGS.append("-flto")
EXTRA_LINK_ARGS.append("-flto")
# For Python 3.9+, hide all of our symbols except the module init function. For
# Python 3.8 and earlier this isn't as easy, because PyMODINIT_FUNC doesn't
# include __attribute__((visibility ("default"))), and Cython doesn't give us
# a way to add the attribute. So, skip this optimization on 3.8 and earlier.
if sys.version_info[:2] >= (3, 9):
EXTRA_COMPILE_ARGS.append("-fvisibility=hidden")
if TEST_BUILD:
COMPILER_DIRECTIVES = {
"language_level": 3,
"boundscheck": True,
"embedsignature": True,
"wraparound": True,
"cdivision": False,
"profile": False,
"linetrace": False,
"overflowcheck": True,
"infer_types": True,
"c_string_type": "unicode",
"c_string_encoding": "utf8",
}
EXTRA_COMPILE_ARGS = []
UNDEF_MACROS = ["NDEBUG"]
if IS_LINUX:
EXTRA_COMPILE_ARGS.extend(["-D_GLIBCXX_DEBUG", "-D_LIBCPP_DEBUG"])
DEFINE_MACROS = []
# Ensure that we have a 64-bit off_t in all translation units.
DEFINE_MACROS.append(("_FILE_OFFSET_BITS", "64"))
# memray uses thread local storage (TLS) variables. As memray is compiled
# into a Python extension, is a shared object. TLS variables in shared objects
# use the most conservative and slow TLS model available by default:
# global-dynamic. This TLS model generates function calls (__tls_get_addr) to
# obtain the address of the TLS storage block, which is quite slow. To
# circuvent the slowdown, memray uses by default a less restrictive model:
# initial-exec. This model is very fast but uses the limited TLS storage of the
# executable. This means that is possible that dlopen will refuse to load the
# shared object of the extension if there is not enough space. glibc reserves
# 1152 bytes for oportunustic usage for shared libraries with initial-exec, so
# this model will not present problems as long as the application uses glibc. In
# case these assumptions are wrong, memray can revert to use the most
# conservative model by setting the NO_MEMRAY_FAST_TLS environment variable.
MEMRAY_FAST_TLS = True
if os.getenv("NO_MEMRAY_FAST_TLS", None) is not None:
MEMRAY_FAST_TLS = False
if MEMRAY_FAST_TLS:
DEFINE_MACROS.append(("USE_MEMRAY_TLS_MODEL", "1"))
BINARY_FORMATS = {"darwin": "macho", "linux": "elf"}
BINARY_FORMAT = BINARY_FORMATS.get(sys.platform, "elf")
library_flags = {"libraries": ["lz4"]}
if IS_LINUX:
library_flags["libraries"].append("unwind")
library_flags["libraries"].append("debuginfod")
try:
library_flags = pkgconfig.parse(
" ".join(f"lib{libname}" for libname in library_flags["libraries"])
)
except EnvironmentError as e:
print("pkg-config not found.", e)
print("Falling back to static flags.")
except pkgconfig.PackageNotFoundError as e:
print("Package Not Found", e)
print("Falling back to static flags.")
MEMRAY_EXTENSION = Extension(
name="memray._memray",
sources=[
"src/memray/_memray.pyx",
"src/memray/_memray/compat.cpp",
"src/memray/_memray/hooks.cpp",
"src/memray/_memray/tracking_api.cpp",
f"src/memray/_memray/{BINARY_FORMAT}_shenanigans.cpp",
"src/memray/_memray/logging.cpp",
"src/memray/_memray/python_helpers.cpp",
"src/memray/_memray/source.cpp",
"src/memray/_memray/sink.cpp",
"src/memray/_memray/records.cpp",
"src/memray/_memray/record_reader.cpp",
"src/memray/_memray/record_writer.cpp",
"src/memray/_memray/snapshot.cpp",
"src/memray/_memray/socket_reader_thread.cpp",
"src/memray/_memray/native_resolver.cpp",
],
language="c++",
extra_compile_args=["-std=c++17", "-Wall", *EXTRA_COMPILE_ARGS],
extra_objects=[str(LIBBACKTRACE_LIBDIR / "libbacktrace.a")],
extra_link_args=["-std=c++17", *EXTRA_LINK_ARGS],
define_macros=DEFINE_MACROS,
undef_macros=UNDEF_MACROS,
**library_flags,
)
MEMRAY_EXTENSION.include_dirs[:0] = ["src", str(LIBBACKTRACE_INCLUDEDIRS)]
MEMRAY_EXTENSION.libraries.append("dl")
MEMRAY_TEST_EXTENSION = Extension(
name="memray._test_utils",
sources=[
"src/memray/_memray_test_utils.pyx",
],
language="c++",
extra_compile_args=["-std=c++17", "-Wall", *EXTRA_COMPILE_ARGS],
extra_link_args=["-std=c++17", *EXTRA_LINK_ARGS],
define_macros=DEFINE_MACROS,
undef_macros=UNDEF_MACROS,
)
MEMRAY_INJECT_EXTENSION = Extension(
name="memray._inject",
sources=[
"src/memray/_memray/inject.cpp",
],
language="c++",
extra_compile_args=["-std=c++17", "-Wall", *EXTRA_COMPILE_ARGS],
extra_link_args=["-std=c++17", *EXTRA_LINK_ARGS],
define_macros=DEFINE_MACROS,
undef_macros=UNDEF_MACROS,
py_limited_api=True,
)
if not (IS_LINUX or IS_MAC):
raise RuntimeError(f"memray does not support this platform ({platform})")
about = {}
with open("src/memray/_version.py") as fp:
exec(fp.read(), about)
HERE = pathlib.Path(__file__).parent.resolve()
LONG_DESCRIPTION = (HERE / "README.md").read_text(encoding="utf-8")
setup(
name="memray",
version=about["__version__"],
python_requires=">=3.7.0",
description="A memory profiler for Python applications",
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
url="https://github.com/bloomberg/memray",
author="Pablo Galindo Salgado",
classifiers=[
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Software Development :: Debuggers",
],
license="Apache 2.0",
package_dir={"": "src"},
packages=find_packages(where="src", exclude="memray/_memray/"),
ext_modules=cythonize(
[MEMRAY_EXTENSION, MEMRAY_TEST_EXTENSION, MEMRAY_INJECT_EXTENSION],
include_path=["src/memray"],
compiler_directives=COMPILER_DIRECTIVES,
),
include_package_data=True,
install_requires=install_requires,
extras_require={
"test": test_requires,
"docs": docs_requires,
"lint": lint_requires,
"benchmark": benchmark_requires,
"dev": test_requires + lint_requires + docs_requires + benchmark_requires,
},
entry_points={
"console_scripts": [
f"memray{version_info.major}.{version_info.minor}=memray.__main__:main",
"memray=memray.__main__:main",
],
},
cmdclass={
"build_ext": BuildMemray,
},
)