Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the main thread ID in the reader #560

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading