Skip to content

Commit 5719e0f

Browse files
committed
Doesn't work due to rust-lang/rust#66265
1 parent 4a7efd8 commit 5719e0f

File tree

6 files changed

+33
-38
lines changed

6 files changed

+33
-38
lines changed

Makefile

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ MAKEFLAGS += --warn-undefined-variables
55
MAKEFLAGS += --no-builtin-rules
66

77
.PHONY: build
8-
build: filprofiler/_filpreload.so filprofiler/libpymemprofile_api.so build_ext
8+
build: filprofiler/libpymemprofile_api.so build_ext
99

1010
.PHONY: build_ext
1111
build_ext: filprofiler/libpymemprofile_api.so
1212
python3.8 setup.py build_ext --inplace
1313

14-
filprofiler/_filpreload.so: filprofiler/_filpreload.c
15-
gcc -std=c11 -D_FORTIFY_SOURCE=2 -fasynchronous-unwind-tables -fstack-clash-protection -fstack-protector -Werror=format-security -Werror=implicit-function-declaration -O2 -shared -ldl -g -fPIC -fvisibility=hidden -Wall -o $@ $<
16-
17-
filprofiler/libpymemprofile_api.so: Cargo.lock memapi/Cargo.toml memapi/src/*.rs
14+
filprofiler/libpymemprofile_api.so: Cargo.lock memapi/Cargo.toml memapi/src/*.rs memapi/src/*.c memapi/build.rs
1815
rm -f filprofiler/libymemprofile_api.so
1916
cargo build --release
2017
cp -f target/release/libpymemprofile_api.so filprofiler/

filprofiler/_script.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def stage_1():
2222
# Route all allocations from Python through malloc() directly:
2323
environ["PYTHONMALLOC"] = "malloc"
2424
# Library setup:
25-
environ["LD_PRELOAD"] = library_path("_filpreload")
26-
environ["FIL_API_LIBRARY"] = library_path("libpymemprofile_api")
25+
environ["LD_PRELOAD"] = library_path("libpymemprofile_api")
26+
2727
# Disable multi-threaded backends in various scientific computing libraries
2828
# (Zarr uses Blosc, NumPy uses BLAS):
2929
environ["BLOSC_NTHREADS"] = "1"

filprofiler/_tracer.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
from ._utils import library_path
1010

11-
# Load with RTLD_GLOBAL so _profiler.so has access to those symbols; explicit
12-
# linking may be possible but haven't done that yet, oh well.
13-
pymemprofile = CDLL(library_path("libpymemprofile_api"), mode=RTLD_GLOBAL)
14-
preload = CDLL(library_path("_filpreload"), mode=RTLD_GLOBAL)
11+
# Load with RTLD_GLOBAL so _profiler.so has access to those symbols; TODO
12+
# explicit linking may be possible but haven't done that yet.
13+
preload = CDLL(library_path("libpymemprofile_api"), mode=RTLD_GLOBAL)
1514
from . import _profiler
1615

1716

memapi/build.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
use cc;
2+
use std::env;
23

34
fn main() {
5+
let out_dir = env::var("OUT_DIR").unwrap();
6+
println!("cargo:rerun-if-changed=src/filpreload.c");
7+
println!("cargo:rustc-link-search=native={}", out_dir);
48
cc::Build::new()
59
.file("src/filpreload.c")
610
.warnings_into_errors(true)
711
.flag("-std=c11")
812
.flag("-Wall")
913
.flag("-Werror=format-security")
1014
.flag("-Werror=implicit-function-declaration")
15+
.static_flag(true)
16+
.shared_flag(false)
17+
.flag("-fvisibility=hidden")
18+
.cargo_metadata(false)
1119
.compile("filpreload");
12-
println!("cargo:rerun-if-changed=src/filpreload.c");
1320
}

memapi/src/filpreload.c

+10-26
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ static void *(*underlying_real_mmap)(void *addr, size_t length, int prot,
1111
int flags, int fd, off_t offset) = 0;
1212
static void (*underlying_real_free)(void *addr) = 0;
1313

14-
// The internal API we're notifying of allocations:
15-
static void (*add_allocation_hook)(size_t address, size_t length) = 0;
16-
static void (*free_allocation_hook)(size_t address) = 0;
17-
1814
// Note whether we've been initialized yet or not:
1915
static int initialized = 0;
2016

@@ -29,23 +25,6 @@ static void __attribute__((constructor)) constructor() {
2925
fprintf(stderr, "BUG: expected size of size_t and void* to be the same.\n");
3026
exit(1);
3127
}
32-
void *lib =
33-
dlopen(getenv("FIL_API_LIBRARY"), RTLD_NOW | RTLD_DEEPBIND | RTLD_GLOBAL);
34-
if (!lib) {
35-
fprintf(stderr, "Couldn't load libpymemprofile_api.so library: %s\n",
36-
dlerror());
37-
exit(1);
38-
}
39-
add_allocation_hook = dlsym(lib, "pymemprofile_add_allocation");
40-
if (!add_allocation_hook) {
41-
fprintf(stderr, "Couldn't load pymemprofile API function: %s\n", dlerror());
42-
exit(1);
43-
}
44-
free_allocation_hook = dlsym(lib, "pymemprofile_free_allocation");
45-
if (!free_allocation_hook) {
46-
fprintf(stderr, "Couldn't load pymemprofile API function: %s\n", dlerror());
47-
exit(1);
48-
}
4928
underlying_real_mmap = dlsym(RTLD_NEXT, "mmap");
5029
if (!underlying_real_mmap) {
5130
fprintf(stderr, "Couldn't load mmap(): %s\n", dlerror());
@@ -61,10 +40,14 @@ static void __attribute__((constructor)) constructor() {
6140

6241
extern void *__libc_malloc(size_t size);
6342
extern void *__libc_calloc(size_t nmemb, size_t size);
43+
44+
// The Rust API in lib.rs:
45+
extern void pymemprofile_add_allocation(size_t address, size_t length);
46+
extern void pymemprofile_free_allocation(size_t address);
6447
extern void pymemprofile_start_call(const char *filename, const char *funcname);
6548
extern void pymemprofile_finish_call();
6649
extern void pymemprofile_reset();
67-
extern void pymemprofile_dump_peak_to_flamegraph(const char* path);
50+
extern void pymemprofile_dump_peak_to_flamegraph(const char *path);
6851

6952
__attribute__((visibility("default"))) void
7053
fil_start_call(const char *filename, const char *funcname) {
@@ -91,7 +74,8 @@ __attribute__((visibility("default"))) void fil_reset() {
9174
}
9275
}
9376

94-
__attribute__((visibility("default"))) void fil_dump_peak_to_flamegraph(const char* path) {
77+
__attribute__((visibility("default"))) void
78+
fil_dump_peak_to_flamegraph(const char *path) {
9579
if (!will_i_be_reentrant) {
9680
will_i_be_reentrant = 1;
9781
pymemprofile_dump_peak_to_flamegraph(path);
@@ -104,7 +88,7 @@ __attribute__((visibility("default"))) void *malloc(size_t size) {
10488
void *result = __libc_malloc(size);
10589
if (!will_i_be_reentrant && initialized) {
10690
will_i_be_reentrant = 1;
107-
add_allocation_hook((size_t)result, size);
91+
pymemprofile_add_allocation((size_t)result, size);
10892
will_i_be_reentrant = 0;
10993
}
11094
return result;
@@ -115,7 +99,7 @@ __attribute__((visibility("default"))) void *calloc(size_t nmemb, size_t size) {
11599
size_t allocated = nmemb * size;
116100
if (!will_i_be_reentrant && initialized) {
117101
will_i_be_reentrant = 1;
118-
add_allocation_hook((size_t)result, allocated);
102+
pymemprofile_add_allocation((size_t)result, allocated);
119103
will_i_be_reentrant = 0;
120104
}
121105
return result;
@@ -129,7 +113,7 @@ __attribute__((visibility("default"))) void free(void *addr) {
129113
underlying_real_free(addr);
130114
if (!will_i_be_reentrant) {
131115
will_i_be_reentrant = 1;
132-
free_allocation_hook((size_t)addr);
116+
pymemprofile_free_allocation((size_t)addr);
133117
will_i_be_reentrant = 0;
134118
}
135119
}

memapi/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,13 @@ pub unsafe extern "C" fn pymemprofile_dump_peak_to_flamegraph(path: *const c_cha
5151
memorytracking::dump_peak_to_flamegraph(&path);
5252
}
5353

54+
// Re-export symbols from filpreload.c
55+
#[link(name = "filpreload", kind = "static")]
56+
extern "C" {
57+
pub fn malloc(size: libc::size_t) -> *mut libc::c_void;
58+
pub fn free(addr: *mut libc::c_void);
59+
pub fn fil_dump_peak_to_flamegraph(path: *const c_char);
60+
}
61+
5462
#[cfg(test)]
5563
mod tests {}

0 commit comments

Comments
 (0)