Skip to content

Commit

Permalink
Expose the main thread ID in the reader
Browse files Browse the repository at this point in the history
To be able to filter allocations that happened only on the thread that
started the tracking, expose the main thread ID in the reader class.

Signed-off-by: Pablo Galindo <[email protected]>
  • Loading branch information
pablogsal committed Mar 7, 2024
1 parent b8dd3aa commit bdc8737
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/560.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expose the main thread id in the FileReader's metadata attribute.
1 change: 1 addition & 0 deletions src/memray/_memray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ cdef _create_metadata(header, peak_memory):
peak_memory=peak_memory,
command_line=header["command_line"],
pid=header["pid"],
main_thread_id=header["main_tid"],
python_allocator=allocator_id_to_name[header["python_allocator"]],
has_native_traces=header["native_traces"],
trace_python_allocators=header["trace_python_allocators"],
Expand Down
1 change: 1 addition & 0 deletions src/memray/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Metadata:
peak_memory: int
command_line: str
pid: int
main_thread_id: int
python_allocator: str
has_native_traces: bool
trace_python_allocators: bool
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import threading

import pytest

Expand Down Expand Up @@ -79,3 +80,28 @@ def test_read_pid(tmp_path):

# THEN
assert FileReader(output).metadata.pid == os.getpid()


def test_read_tid(tmp_path):
# GIVEN
output = tmp_path / "test.bin"
allocator = MemoryAllocator()

def func():
allocator.valloc(1024)

# WHEN
t = threading.Thread(target=func)
with Tracker(output):
func()
t.start()
t.join()

# THEN
reader = FileReader(output)
main_tid = reader.metadata.main_thread_id
all_allocations = reader.get_allocation_records()
all_tids = tuple(allocation.tid for allocation in all_allocations)
assert main_tid in set(all_tids)
# The main thread should be the first one in the list
assert main_tid == all_tids[0]
2 changes: 2 additions & 0 deletions tests/unit/test_stats_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def fake_stats():
has_native_traces=False,
trace_python_allocators=True,
file_format=FileFormat.ALL_ALLOCATIONS,
main_thread_id=0x1,
),
total_num_allocations=20,
total_memory_allocated=sum(mem_allocation_list),
Expand Down Expand Up @@ -438,6 +439,7 @@ def test_stats_output_json(fake_stats, tmp_path):
"has_native_traces": False,
"trace_python_allocators": True,
"file_format": 0,
"main_thread_id": 0x1,
},
}
actual = json.loads(output_file.read_text())
Expand Down

0 comments on commit bdc8737

Please sign in to comment.