Skip to content

Commit ef8c134

Browse files
committed
Merge branch 'callstack-of-ints' into 'master'
Callstack of ints See merge request pythonspeed/memory-profiler!3
2 parents 0b8b5ab + c524e08 commit ef8c134

File tree

9 files changed

+530
-41
lines changed

9 files changed

+530
-41
lines changed

Cargo.lock

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

filprofiler/_tracer.py

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ctypes import CDLL, RTLD_GLOBAL
55

66
from ._utils import library_path
7+
78
# Load with RTLD_GLOBAL so _profiler.so has access to those symbols; explicit
89
# linking may be possible but haven't done that yet, oh well.
910
pymemprofile = CDLL(library_path("libpymemprofile_api"), mode=RTLD_GLOBAL)
@@ -15,10 +16,18 @@ def start_tracing():
1516
preload.fil_reset()
1617
_profiler.start_tracing()
1718

19+
1820
def stop_tracing(svg_output_path: str):
1921
sys.settrace(None)
2022
path = svg_output_path.encode("utf-8")
2123
preload.fil_dump_peak_to_flamegraph(path)
24+
with open(path) as f:
25+
data = f.read().replace(
26+
"SUBTITLE-HERE",
27+
"""Made with the Fil memory profiler. <a href="https://pythonspeed.com/products/filmemoryprofiler/" style="text-decoration: underline;" target="_parent">Try it on your code!</a>""",
28+
)
29+
with open(path, "w") as f:
30+
f.write(data)
2231

2332

2433
def trace(code, globals_, svg_output_path: str):

memapi/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10+
im = "14.2.0"
1011
libc = "0.2"
1112
itertools = "0.8.2"
1213
lazy_static = "1.4.0"
@@ -16,9 +17,17 @@ rustc-hash = "1.0.1"
1617
version = "0.9.4"
1718
default-features = false
1819

20+
[dependencies.smallstr]
21+
version = "0.2.0"
22+
features = ["ffi"]
23+
1924
[dev-dependencies]
2025
proptest = "0.9.5"
2126

27+
[profile.release]
28+
# Needed for profiling, disable for real release:
29+
#debug = true
30+
2231
[lib]
2332
name = "pymemprofile_api"
2433
crate_type = ["cdylib"]

memapi/src/lib.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use smallstr::SmallString;
12
use std::ffi::CStr;
23
use std::os::raw::c_char;
4+
use std::str;
35

46
#[macro_use]
57
extern crate lazy_static;
@@ -23,16 +25,17 @@ pub unsafe extern "C" fn pymemprofile_start_call(
2325
file_name: *const c_char,
2426
func_name: *const c_char,
2527
) {
26-
let name = format!(
27-
"{}:{}",
28-
CStr::from_ptr(file_name)
29-
.to_str()
30-
.expect("Function name wasn't UTF-8"),
31-
CStr::from_ptr(func_name)
32-
.to_str()
33-
.expect("Function name wasn't UTF-8")
34-
);
35-
memorytracking::start_call(name);
28+
let function_name = SmallString::from_str(str::from_utf8_unchecked(
29+
CStr::from_ptr(func_name).to_bytes(),
30+
));
31+
let module_name = SmallString::from_str(str::from_utf8_unchecked(
32+
CStr::from_ptr(file_name).to_bytes(),
33+
));
34+
let call_site = memorytracking::CallSite {
35+
function_name,
36+
module_name,
37+
};
38+
memorytracking::start_call(call_site);
3639
}
3740

3841
#[no_mangle]

0 commit comments

Comments
 (0)