diff --git a/news/492.feature.rst b/news/492.feature.rst new file mode 100644 index 0000000000..6f41f62932 --- /dev/null +++ b/news/492.feature.rst @@ -0,0 +1 @@ +When generating a ``--leaks`` flamegraph, don't show a warning that the ``pymalloc`` allocator is in use if ``--trace-python-allocators`` was used when generating the capture file. diff --git a/src/memray/_memray.pyx b/src/memray/_memray.pyx index 4dafb2ae4a..f961cf699e 100644 --- a/src/memray/_memray.pyx +++ b/src/memray/_memray.pyx @@ -673,6 +673,7 @@ cdef class Tracker: command_line, native_traces, file_format, + trace_python_allocators, ) ) @@ -753,6 +754,7 @@ cdef _create_metadata(header, peak_memory): pid=header["pid"], python_allocator=allocator_id_to_name[header["python_allocator"]], has_native_traces=header["native_traces"], + trace_python_allocators=header["trace_python_allocators"], ) diff --git a/src/memray/_memray/record_reader.cpp b/src/memray/_memray/record_reader.cpp index cff2e73a3a..8eaa567987 100644 --- a/src/memray/_memray/record_reader.cpp +++ b/src/memray/_memray/record_reader.cpp @@ -87,7 +87,10 @@ RecordReader::readHeader(HeaderRecord& header) sizeof(header.skipped_frames_on_main_tid)) || !d_input->read( reinterpret_cast(&header.python_allocator), - sizeof(header.python_allocator))) + sizeof(header.python_allocator)) + || !d_input->read( + reinterpret_cast(&header.trace_python_allocators), + sizeof(header.trace_python_allocators))) { throw std::ios_base::failure("Failed to read input file header."); } @@ -936,7 +939,7 @@ RecordReader::dumpAllRecords() printf("HEADER magic=%.*s version=%d native_traces=%s file_format=%s" " n_allocations=%zd n_frames=%zd start_time=%lld end_time=%lld" " pid=%d main_tid=%lu skipped_frames_on_main_tid=%zd" - " command_line=%s python_allocator=%s\n", + " command_line=%s python_allocator=%s trace_python_allocators=%s\n", (int)sizeof(d_header.magic), d_header.magic, d_header.version, @@ -950,7 +953,8 @@ RecordReader::dumpAllRecords() d_header.main_tid, d_header.skipped_frames_on_main_tid, d_header.command_line.c_str(), - python_allocator.c_str()); + python_allocator.c_str(), + d_header.trace_python_allocators ? "true" : "false"); switch (d_header.file_format) { case FileFormat::ALL_ALLOCATIONS: diff --git a/src/memray/_memray/record_writer.cpp b/src/memray/_memray/record_writer.cpp index 14a247e732..8d979e0255 100644 --- a/src/memray/_memray/record_writer.cpp +++ b/src/memray/_memray/record_writer.cpp @@ -49,7 +49,8 @@ class StreamingRecordWriter : public RecordWriter explicit StreamingRecordWriter( std::unique_ptr sink, const std::string& command_line, - bool native_traces); + bool native_traces, + bool trace_python_allocators); StreamingRecordWriter(StreamingRecordWriter& other) = delete; StreamingRecordWriter(StreamingRecordWriter&& other) = delete; @@ -90,7 +91,8 @@ class AggregatingRecordWriter : public RecordWriter explicit AggregatingRecordWriter( std::unique_ptr sink, const std::string& command_line, - bool native_traces); + bool native_traces, + bool trace_python_allocators); AggregatingRecordWriter(StreamingRecordWriter& other) = delete; AggregatingRecordWriter(StreamingRecordWriter&& other) = delete; @@ -138,16 +140,22 @@ createRecordWriter( std::unique_ptr sink, const std::string& command_line, bool native_traces, - FileFormat file_format) + FileFormat file_format, + bool trace_python_allocators) { switch (file_format) { case FileFormat::ALL_ALLOCATIONS: - return std::make_unique(std::move(sink), command_line, native_traces); + return std::make_unique( + std::move(sink), + command_line, + native_traces, + trace_python_allocators); case FileFormat::AGGREGATED_ALLOCATIONS: return std::make_unique( std::move(sink), command_line, - native_traces); + native_traces, + trace_python_allocators); default: throw std::runtime_error("Invalid file format enumerator"); } @@ -156,7 +164,8 @@ createRecordWriter( StreamingRecordWriter::StreamingRecordWriter( std::unique_ptr sink, const std::string& command_line, - bool native_traces) + bool native_traces, + bool trace_python_allocators) : RecordWriter(std::move(sink)) , d_stats({0, 0, duration_cast(system_clock::now().time_since_epoch()).count()}) { @@ -170,7 +179,8 @@ StreamingRecordWriter::StreamingRecordWriter( ::getpid(), 0, 0, - getPythonAllocator()}; + getPythonAllocator(), + trace_python_allocators}; strncpy(d_header.magic, MAGIC, sizeof(d_header.magic)); } @@ -357,7 +367,7 @@ RecordWriter::writeHeaderCommon(const HeaderRecord& header) or !writeSimpleType(header.stats) or !writeString(header.command_line.c_str()) or !writeSimpleType(header.pid) or !writeSimpleType(header.main_tid) or !writeSimpleType(header.skipped_frames_on_main_tid) - or !writeSimpleType(header.python_allocator)) + or !writeSimpleType(header.python_allocator) or !writeSimpleType(header.trace_python_allocators)) { return false; } @@ -383,13 +393,15 @@ StreamingRecordWriter::cloneInChildProcess() return std::make_unique( std::move(new_sink), d_header.command_line, - d_header.native_traces); + d_header.native_traces, + d_header.trace_python_allocators); } AggregatingRecordWriter::AggregatingRecordWriter( std::unique_ptr sink, const std::string& command_line, - bool native_traces) + bool native_traces, + bool trace_python_allocators) : RecordWriter(std::move(sink)) { memcpy(d_header.magic, MAGIC, sizeof(d_header.magic)); @@ -399,6 +411,7 @@ AggregatingRecordWriter::AggregatingRecordWriter( d_header.command_line = command_line; d_header.pid = ::getpid(); d_header.python_allocator = getPythonAllocator(); + d_header.trace_python_allocators = trace_python_allocators; d_stats.start_time = duration_cast(system_clock::now().time_since_epoch()).count(); } @@ -512,7 +525,8 @@ AggregatingRecordWriter::cloneInChildProcess() return std::make_unique( std::move(new_sink), d_header.command_line, - d_header.native_traces); + d_header.native_traces, + d_header.trace_python_allocators); } bool diff --git a/src/memray/_memray/record_writer.h b/src/memray/_memray/record_writer.h index c64998d0b3..a9b756bfba 100644 --- a/src/memray/_memray/record_writer.h +++ b/src/memray/_memray/record_writer.h @@ -62,7 +62,8 @@ createRecordWriter( std::unique_ptr sink, const std::string& command_line, bool native_traces, - FileFormat file_format); + FileFormat file_format, + bool trace_python_allocators); template bool inline RecordWriter::writeSimpleType(const T& item) diff --git a/src/memray/_memray/record_writer.pxd b/src/memray/_memray/record_writer.pxd index 97a992f632..635fb705cb 100644 --- a/src/memray/_memray/record_writer.pxd +++ b/src/memray/_memray/record_writer.pxd @@ -13,4 +13,5 @@ cdef extern from "record_writer.h" namespace "memray::api": string command_line, bool native_trace, FileFormat file_format, + bool trace_python_allocators, ) except+ diff --git a/src/memray/_memray/records.h b/src/memray/_memray/records.h index 67a9b2952b..7f6373e12a 100644 --- a/src/memray/_memray/records.h +++ b/src/memray/_memray/records.h @@ -18,7 +18,7 @@ namespace memray::tracking_api { extern const char MAGIC[7]; // Value assigned in records.cpp -const int CURRENT_HEADER_VERSION = 10; +const int CURRENT_HEADER_VERSION = 11; using frame_id_t = size_t; using thread_id_t = unsigned long; @@ -118,6 +118,7 @@ struct HeaderRecord thread_id_t main_tid{}; size_t skipped_frames_on_main_tid{}; PythonAllocatorType python_allocator{}; + bool trace_python_allocators{}; }; struct MemoryRecord diff --git a/src/memray/_memray/records.pxd b/src/memray/_memray/records.pxd index 0f477bf80a..639c8ba66f 100644 --- a/src/memray/_memray/records.pxd +++ b/src/memray/_memray/records.pxd @@ -34,6 +34,7 @@ cdef extern from "records.h" namespace "memray::tracking_api": size_t main_tid size_t skipped_frames_on_main_tid int python_allocator + bool trace_python_allocators cdef cppclass Allocation: thread_id_t tid diff --git a/src/memray/_metadata.py b/src/memray/_metadata.py index 424e727d41..64cb2d7b55 100644 --- a/src/memray/_metadata.py +++ b/src/memray/_metadata.py @@ -13,3 +13,4 @@ class Metadata: pid: int python_allocator: str has_native_traces: bool + trace_python_allocators: bool diff --git a/src/memray/reporters/templates/base.html b/src/memray/reporters/templates/base.html index 949c305a6e..71b77ed8f3 100644 --- a/src/memray/reporters/templates/base.html +++ b/src/memray/reporters/templates/base.html @@ -55,16 +55,21 @@
- {% if show_memory_leaks and metadata.python_allocator == "pymalloc" %} + {% if show_memory_leaks and metadata.python_allocator == "pymalloc" and not metadata.trace_python_allocators %}