Skip to content

Commit d5c190a

Browse files
authored
Merge pull request #275 from plasma-umass/python-memory-module
Python memory module
2 parents 4193a47 + f21a3ab commit d5c190a

File tree

10 files changed

+385
-322
lines changed

10 files changed

+385
-322
lines changed

.github/workflows/release-pypi-upload.yml

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@ on:
1515
workflow_dispatch: # manual execution
1616

1717
jobs:
18-
pypi-upload:
18+
pick-devN:
19+
name: create .devN build date coordinated across all matrix jobs
20+
runs-on: ubuntu-latest
21+
steps:
22+
- run: TZ='America/New_York' date '+%Y%m%d%H%M' > devN.txt
23+
24+
- uses: actions/upload-artifact@v2
25+
with:
26+
name: devN
27+
path: devN.txt
28+
29+
build-and-upload:
30+
needs: pick-devN
1931
runs-on: ${{ matrix.os }}
2032
strategy:
2133
matrix:
@@ -37,57 +49,62 @@ jobs:
3749
TWINE_REPOSITORY: ${{ secrets.PYPI_REPOSITORY }}
3850

3951
steps:
40-
- name: Pick .devN name if a test build
41-
if: env.TWINE_REPOSITORY == 'testpypi'
42-
run: |
43-
echo "DEV_BUILD=$(date '+%Y%m%d%H%M')" >> $GITHUB_ENV # for setup.py
44-
45-
- uses: actions/checkout@v2
46-
47-
- name: Set up python (script version)
48-
if: matrix.container == ''
49-
uses: actions/setup-python@v2
50-
with:
51-
python-version: ${{ matrix.python_version }}
52-
53-
- name: Set up python (container version)
54-
if: matrix.container != ''
55-
run: |
56-
PYV=`echo "${{ matrix.python_version }}" | tr -d "."`; ls -d -1 /opt/python/cp$PYV*/bin | head -n 1 >> $GITHUB_PATH
57-
cat $GITHUB_PATH
58-
59-
- name: Install dependencies
60-
run: |
61-
pip3 install setuptools wheel twine find_libpython
62-
63-
- name: Work around arm64 support on MacOS
64-
# https://github.com/actions/virtual-environments/issues/2557
65-
if: matrix.os == 'macos-latest'
66-
run: sudo rm -Rf /Library/Developer/CommandLineTools/SDKs/*
67-
68-
- name: Build source dist
69-
if: matrix.upload_source
70-
run: make sdist
71-
72-
- name: Build binary dist
73-
run: make bdist
74-
75-
- name: Figure out google drive upload target
76-
if: env.GOOGLE_DRIVE_FOLDER != ''
77-
run: |
78-
echo "UPLOAD_TARGET=$(ls dist/scalene*.whl)" >> $GITHUB_ENV
79-
80-
- name: Upload to google drive
81-
if: env.GOOGLE_DRIVE_FOLDER != ''
82-
uses: Jodebu/upload-to-drive@master
83-
with:
84-
target: ${{ env.UPLOAD_TARGET }}
85-
credentials: ${{ secrets.GOOGLE_DRIVE_CREDENTIALS }}
86-
folder: ${{ env.GOOGLE_DRIVE_FOLDER }}
87-
88-
- name: Upload
89-
if: env.GOOGLE_DRIVE_FOLDER == ''
90-
env:
91-
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
92-
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
93-
run: twine upload dist/*
52+
- name: get coordinated .devN
53+
uses: actions/download-artifact@v2
54+
with:
55+
name: devN
56+
57+
- name: set up to use .devN name if a test build
58+
if: env.TWINE_REPOSITORY == 'testpypi'
59+
run: |
60+
echo "DEV_BUILD=$(cat devN.txt)" >> $GITHUB_ENV # for setup.py
61+
62+
- uses: actions/checkout@v2
63+
64+
- name: Set up python (script version)
65+
if: matrix.container == ''
66+
uses: actions/setup-python@v2
67+
with:
68+
python-version: ${{ matrix.python_version }}
69+
70+
- name: Set up python (container version)
71+
if: matrix.container != ''
72+
run: |
73+
PYV=`echo "${{ matrix.python_version }}" | tr -d "."`; ls -d -1 /opt/python/cp$PYV*/bin | head -n 1 >> $GITHUB_PATH
74+
cat $GITHUB_PATH
75+
76+
- name: Install dependencies
77+
run: |
78+
pip3 install setuptools wheel twine
79+
80+
- name: Work around arm64 support on MacOS
81+
# https://github.com/actions/virtual-environments/issues/2557
82+
if: matrix.os == 'macos-latest'
83+
run: sudo rm -Rf /Library/Developer/CommandLineTools/SDKs/*
84+
85+
- name: Build source dist
86+
if: matrix.upload_source
87+
run: make sdist
88+
89+
- name: Build binary dist
90+
run: make bdist
91+
92+
- name: Figure out google drive upload target
93+
if: env.GOOGLE_DRIVE_FOLDER != ''
94+
run: |
95+
echo "UPLOAD_TARGET=$(ls dist/scalene*.whl)" >> $GITHUB_ENV
96+
97+
- name: Upload to google drive
98+
if: env.GOOGLE_DRIVE_FOLDER != ''
99+
uses: Jodebu/upload-to-drive@master
100+
with:
101+
target: ${{ env.UPLOAD_TARGET }}
102+
credentials: ${{ secrets.GOOGLE_DRIVE_CREDENTIALS }}
103+
folder: ${{ env.GOOGLE_DRIVE_FOLDER }}
104+
105+
- name: Upload
106+
if: env.GOOGLE_DRIVE_FOLDER == ''
107+
env:
108+
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
109+
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
110+
run: twine upload dist/*

GNUmakefile

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ C_SOURCES = src/source/libscalene.cpp src/source/get_line_atomic.cpp src/include
99
CXXFLAGS = -std=c++17 -g -O3 -DNDEBUG -D_REENTRANT=1 -pipe -fno-builtin-malloc -fvisibility=hidden
1010
CXX = g++
1111

12-
PYTHON_INCLUDE := $(shell python3 -c "from sysconfig import get_paths as gp; print(gp()['include'])")
13-
PYTHON_LIBRARY := $(shell python3 -m find_libpython)
14-
1512
INCLUDES = -Isrc -Isrc/include
1613
INCLUDES := $(INCLUDES) -Ivendor/Heap-Layers -Ivendor/Heap-Layers/wrappers -Ivendor/Heap-Layers/utility
1714
INCLUDES := $(INCLUDES) -Ivendor/printf
18-
INCLUDES := $(INCLUDES) -I$(PYTHON_INCLUDE)
15+
INCLUDES := $(INCLUDES) $(shell python3-config --includes)
1916

2017
ifeq ($(shell uname -s),Darwin)
2118
LIBFILE := lib$(LIBNAME).dylib
@@ -26,7 +23,6 @@ ifeq ($(shell uname -s),Darwin)
2623
ARCH := -arch x86_64
2724
endif
2825
CXXFLAGS := $(CXXFLAGS) -flto -ftls-model=initial-exec -ftemplate-depth=1024 $(ARCH) -compatibility_version 1 -current_version 1 -dynamiclib
29-
RPATH_FLAGS := -Wl,-rpath $(shell dirname $(PYTHON_LIBRARY))
3026
SED_INPLACE = -i ''
3127

3228
else # non-Darwin
@@ -46,10 +42,10 @@ OUTDIR=scalene
4642
all: $(OUTDIR)/$(LIBFILE)
4743

4844
$(OUTDIR)/$(LIBFILE): vendor/Heap-Layers $(SRC) $(C_SOURCES) GNUmakefile
49-
$(CXX) $(CXXFLAGS) $(INCLUDES) $(SRC) -o $(OUTDIR)/$(LIBFILE) -ldl -lpthread $(RPATH_FLAGS) $(PYTHON_LIBRARY)
45+
$(CXX) $(CXXFLAGS) $(INCLUDES) $(SRC) -o $(OUTDIR)/$(LIBFILE) -ldl -lpthread
5046

5147
clean:
52-
rm -f $(OUTDIR)/$(LIBFILE) scalene/get_line_atomic*.so
48+
rm -f $(OUTDIR)/$(LIBFILE) scalene/*.so scalene/*.dylib
5349
rm -rf $(OUTDIR)/$(LIBFILE).dSYM
5450
rm -rf scalene.egg-info
5551
rm -rf build dist *egg-info
@@ -77,9 +73,9 @@ clang-format:
7773
black:
7874
-black -l 79 $(PYTHON_SOURCES)
7975

80-
#ifeq ($(shell uname -s),Darwin)
81-
# PYTHON_PLAT:=-p $(shell $(PYTHON) -c 'from pkg_resources import get_build_platform; p=get_build_platform(); print(p[:p.rindex("-")])')-universal2
82-
#endif
76+
ifeq ($(shell uname -s),Darwin)
77+
PYTHON_PLAT:=-p $(shell $(PYTHON) -c 'from pkg_resources import get_build_platform; p=get_build_platform(); print(p[:p.rindex("-")])')-universal2
78+
endif
8379

8480
PYTHON_API_VER:=$(shell $(PYTHON) -c 'from pip._vendor.packaging.tags import interpreter_name, interpreter_version; print(interpreter_name()+interpreter_version())')
8581

scalene/scalene_profiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,9 +1438,9 @@ def run_profiler(args: argparse.Namespace, left: List[str]) -> None:
14381438
Scalene.__program_path = program_path
14391439
# Grab local and global variables.
14401440
if not Scalene.__args.cpu_only:
1441-
import register_files_to_profile
1441+
from scalene import pywhere
14421442

1443-
register_files_to_profile.register_files_to_profile(
1443+
pywhere.register_files_to_profile(
14441444
list(Scalene.__files_to_profile.keys()),
14451445
Scalene.__program_path,
14461446
Scalene.__args.profile_all,

setup.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ def multiarch_args():
1919
"""Returns args requesting multi-architecture support, if applicable."""
2020
# On MacOS we build "universal2" packages, for both x86_64 and arm64/M1
2121
if sys.platform == 'darwin':
22-
return []
23-
# multiple architectures disabled for now, because of issues linking with the Python library.
24-
# return ['-arch', 'x86_64', '-arch', 'arm64']
22+
return ['-arch', 'x86_64', '-arch', 'arm64']
2523
return []
2624

2725
def extra_compile_args():
@@ -85,6 +83,14 @@ def build_libscalene(self):
8583
language="c++"
8684
)
8785

86+
pywhere = Extension('scalene.pywhere',
87+
include_dirs=['.', 'src', 'src/include'],
88+
sources = ['src/source/pywhere.cpp'],
89+
extra_compile_args=extra_compile_args(),
90+
extra_link_args=multiarch_args(),
91+
py_limited_api=False,
92+
language="c++")
93+
8894
# If we're testing packaging, build using a ".devN" suffix in the version number,
8995
# so that we can upload new files (as testpypi/pypi don't allow re-uploading files with
9096
# the same name as previously uploaded).
@@ -135,7 +141,7 @@ def build_libscalene(self):
135141
"wheel==0.36.2",
136142
"numpy"
137143
],
138-
ext_modules=[get_line_atomic],
144+
ext_modules=[get_line_atomic, pywhere],
139145
setup_requires=['setuptools_scm'],
140146
include_package_data=True,
141147
entry_points={"console_scripts": ["scalene = scalene.__main__:main"]},

src/include/memcpysampler.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#ifndef MEMCPYSAMPLER_HPP
33
#define MEMCPYSAMPLER_HPP
44

5-
#include <Python.h>
65
#include <fcntl.h>
76
#include <signal.h>
87
#include <stdint.h>
@@ -268,7 +267,7 @@ class MemcpySampler {
268267
void incrementMemoryOps(int n) {
269268
_memcpyOps += n;
270269
auto sampleMemop = _memcpySampler.sample(n);
271-
if (Py_IsInitialized() && unlikely(sampleMemop)) {
270+
if (unlikely(sampleMemop)) {
272271
writeCount();
273272
_memcpyTriggered++;
274273
_memcpyOps = 0;

src/include/py_env.hpp

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/include/pywhere.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef __PYWHERE_H
2+
#define __PYWHERE_H
3+
4+
#include <string>
5+
#include <atomic>
6+
7+
/**
8+
* Examines the current Python stack frame and let us know where in the code we are.
9+
*/
10+
extern "C" int whereInPython(std::string& filename, int& lineno, int& bytei);
11+
12+
13+
/**
14+
* Pointer to "whereInPython" for efficient linkage between pywhere and libscalene.
15+
*/
16+
extern "C" std::atomic<decltype(whereInPython)*> p_whereInPython;
17+
18+
#endif

0 commit comments

Comments
 (0)