Skip to content

Commit de20f4e

Browse files
committed
Put thread name formatting in a free function
Introduce a new `memray.reporters.common` module for utilities needed by multiple reporters. Replace the `AllocationRecord.pretty_thread_name` property with a `format_thread_name` function in that new module. Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
1 parent c19e280 commit de20f4e

File tree

8 files changed

+24
-28
lines changed

8 files changed

+24
-28
lines changed

src/memray/_memray.pyi

-4
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ class AllocationRecord:
4747
def native_segment_generation(self) -> int: ...
4848
@property
4949
def thread_name(self) -> str: ...
50-
@property
51-
def pretty_thread_name(self) -> str: ...
5250
def hybrid_stack_trace(
5351
self,
5452
max_stacks: Optional[int] = None,
@@ -94,8 +92,6 @@ class TemporalAllocationRecord:
9492
def native_segment_generation(self) -> int: ...
9593
@property
9694
def thread_name(self) -> str: ...
97-
@property
98-
def pretty_thread_name(self) -> str: ...
9995
def hybrid_stack_trace(
10096
self,
10197
max_stacks: Optional[int] = None,

src/memray/_memray.pyx

-14
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,6 @@ cdef class AllocationRecord:
309309
assert self._reader.get() != NULL, "Cannot get thread name without reader."
310310
return self._reader.get().getThreadName(self.tid)
311311

312-
@property
313-
def pretty_thread_name(self):
314-
if self.tid == -1:
315-
return "merged thread"
316-
name = self.thread_name
317-
thread_id = hex(self.tid)
318-
return f"{thread_id} ({name})" if name else f"{thread_id}"
319-
320312
def stack_trace(self, max_stacks=None):
321313
cache_key = ("python", max_stacks)
322314
if cache_key not in self._stack_trace_cache:
@@ -450,12 +442,6 @@ cdef class TemporalAllocationRecord:
450442
assert self._reader.get() != NULL, "Cannot get thread name without reader."
451443
return self._reader.get().getThreadName(self.tid)
452444

453-
@property
454-
def pretty_thread_name(self):
455-
name = self.thread_name
456-
thread_id = hex(self.tid)
457-
return f"{thread_id} ({name})" if name else f"{thread_id}"
458-
459445
def stack_trace(self, max_stacks=None):
460446
cache_key = ("python", max_stacks)
461447
if cache_key not in self._stack_trace_cache:

src/memray/reporters/common.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import Union
2+
3+
from memray._memray import AllocationRecord
4+
from memray._memray import TemporalAllocationRecord
5+
6+
7+
def format_thread_name(
8+
record: Union[AllocationRecord, TemporalAllocationRecord]
9+
) -> str:
10+
if record.tid == -1:
11+
return "merged thread"
12+
name = record.thread_name
13+
thread_id = hex(record.tid)
14+
return f"{thread_id} ({name})" if name else f"{thread_id}"

src/memray/reporters/flamegraph.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from memray import Metadata
2727
from memray._memray import Interval
2828
from memray._memray import TemporalAllocationRecord
29+
from memray.reporters.common import format_thread_name
2930
from memray.reporters.frame_tools import StackFrame
3031
from memray.reporters.frame_tools import is_cpython_internal
3132
from memray.reporters.frame_tools import is_frame_from_import_system
@@ -263,21 +264,21 @@ def _from_any_snapshot(
263264

264265
unique_threads: Set[str] = set()
265266
for record in allocations:
266-
unique_threads.add(record.pretty_thread_name)
267+
unique_threads.add(format_thread_name(record))
267268

268269
record_data: RecordData
269270
if temporal:
270271
assert isinstance(record, TemporalAllocationRecord)
271272
record_data = {
272-
"thread_name": record.pretty_thread_name,
273+
"thread_name": format_thread_name(record),
273274
"intervals": record.intervals,
274275
"size": None,
275276
"n_allocations": None,
276277
}
277278
else:
278279
assert not isinstance(record, TemporalAllocationRecord)
279280
record_data = {
280-
"thread_name": record.pretty_thread_name,
281+
"thread_name": format_thread_name(record),
281282
"intervals": None,
282283
"size": record.size,
283284
"n_allocations": record.n_allocations,

src/memray/reporters/table.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from memray import AllocatorType
1010
from memray import MemorySnapshot
1111
from memray import Metadata
12+
from memray.reporters.common import format_thread_name
1213
from memray.reporters.templates import render_report
1314

1415

@@ -47,7 +48,7 @@ def from_snapshot(
4748
allocator = AllocatorType(record.allocator)
4849
result.append(
4950
{
50-
"tid": record.pretty_thread_name,
51+
"tid": format_thread_name(record),
5152
"size": record.size,
5253
"allocator": allocator.name.lower(),
5354
"n_allocations": record.n_allocations,

src/memray/reporters/transform.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from memray import AllocatorType
1212
from memray import MemorySnapshot
1313
from memray import Metadata
14+
from memray.reporters.common import format_thread_name
1415

1516
Location = Tuple[str, str]
1617

@@ -117,7 +118,7 @@ def render_as_csv(
117118
record.n_allocations,
118119
record.size,
119120
record.tid,
120-
record.pretty_thread_name,
121+
format_thread_name(record),
121122
"|".join(f"{func};{mod};{line}" for func, mod, line in stack_trace),
122123
]
123124
)

src/memray/reporters/tree.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
from memray import AllocationRecord
3838
from memray._memray import size_fmt
39+
from memray.reporters.common import format_thread_name
3940
from memray.reporters.frame_tools import is_cpython_internal
4041
from memray.reporters.frame_tools import is_frame_from_import_system
4142
from memray.reporters.frame_tools import is_frame_interesting
@@ -461,7 +462,7 @@ def from_snapshot(
461462
current_frame = current_frame.children[stack_frame]
462463
current_frame.value += size
463464
current_frame.n_allocations += record.n_allocations
464-
current_frame.thread_id = record.pretty_thread_name
465+
current_frame.thread_id = format_thread_name(record)
465466

466467
if index > MAX_STACKS:
467468
break

tests/utils.py

-4
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ class MockAllocationRecord:
6666
_hybrid_stack: Optional[List[Tuple[str, str, int]]] = None
6767
thread_name: str = ""
6868

69-
@property
70-
def pretty_thread_name(self):
71-
return str(hex(self.tid)) if self.tid != -1 else "merged thread"
72-
7369
@staticmethod
7470
def __get_stack_trace(stack, max_stacks):
7571
if max_stacks == 0:

0 commit comments

Comments
 (0)