Skip to content

Commit

Permalink
stats: Improve JSON output
Browse files Browse the repository at this point in the history
- For the histogram, dump objects rather than lists, in order to help
  explain what the values represent.
- For the count of allocations by allocator, dump an object mapping
  allocator name to count.

Signed-off-by: Matt Wozniski <mwozniski@bloomberg.net>
  • Loading branch information
godlygeek authored and pablogsal committed May 22, 2023
1 parent f96b6b9 commit 27ee005
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
28 changes: 22 additions & 6 deletions src/memray/reporters/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import Counter
from dataclasses import asdict
from pathlib import Path
from typing import Any
from typing import Dict
from typing import Iterator
from typing import List
Expand Down Expand Up @@ -37,6 +38,19 @@ def get_histogram_databins(data: Dict[int, int], bins: int) -> List[Tuple[int, i
return [(steps[b], dist[b]) for b in range(bins)]


def describe_histogram_databins(
databins: List[Tuple[int, int]]
) -> List[Dict[str, int]]:
ret: List[Dict[str, int]] = []
start = 0
for i, (end, count) in enumerate(databins):
# The max size for the last bucket is inclusive, not exclusive
adjustment = 1 if i != len(databins) - 1 else 0
ret.append(dict(min_bytes=start, max_bytes=end - adjustment, count=count))
start = end
return ret


def draw_histogram(
data: Dict[int, int], bins: int, *, hist_scale_factor: int = 25
) -> str:
Expand Down Expand Up @@ -149,23 +163,25 @@ def _render_to_terminal(self, histogram_params: Dict[str, int]) -> None:
print(f"\t- {self._format_location(location)} -> {count}")

def _render_to_json(self, histogram_params: Dict[str, int], out_path: Path) -> None:
alloc_size_hist = get_histogram_databins(
self._stats.allocation_count_by_size, bins=histogram_params["num_bins"]
alloc_size_hist = describe_histogram_databins(
get_histogram_databins(
self._stats.allocation_count_by_size, bins=histogram_params["num_bins"]
)
)

metadata = asdict(self._stats.metadata)
for name, val in metadata.items():
if isinstance(val, datetime.datetime):
metadata[name] = str(val)

data = dict(
data: Dict[str, Any] = dict(
total_num_allocations=self._stats.total_num_allocations,
total_bytes_allocated=self._stats.total_memory_allocated,
allocation_size_histogram=alloc_size_hist,
allocator_type_distribution=[
(allocation_type, count)
allocator_type_distribution={
allocation_type: count
for allocation_type, count in self._get_allocator_type_distribution()
],
},
top_allocations_by_size=[
{"location": self._format_location(location), "size": size}
for location, size in self._get_top_allocations_by_size()
Expand Down
32 changes: 16 additions & 16 deletions tests/unit/test_stats_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,23 +398,23 @@ def test_stats_output_json(fake_stats, tmp_path):
"total_num_allocations": 20,
"total_bytes_allocated": 3341500,
"allocation_size_histogram": [
(4739, 1),
(8986, 0),
(17036, 3),
(32299, 0),
(61237, 1),
(116099, 1),
(220113, 6),
(417312, 0),
(791181, 0),
(1500000, 2),
],
"allocator_type_distribution": [
("MALLOC", 1013),
("REALLOC", 797),
("CALLOC", 152),
("MMAP", 4),
{"min_bytes": 0, "max_bytes": 4738, "count": 1},
{"min_bytes": 4739, "max_bytes": 8985, "count": 0},
{"min_bytes": 8986, "max_bytes": 17035, "count": 3},
{"min_bytes": 17036, "max_bytes": 32298, "count": 0},
{"min_bytes": 32299, "max_bytes": 61236, "count": 1},
{"min_bytes": 61237, "max_bytes": 116098, "count": 1},
{"min_bytes": 116099, "max_bytes": 220112, "count": 6},
{"min_bytes": 220113, "max_bytes": 417311, "count": 0},
{"min_bytes": 417312, "max_bytes": 791180, "count": 0},
{"min_bytes": 791181, "max_bytes": 1500000, "count": 2},
],
"allocator_type_distribution": {
"MALLOC": 1013,
"REALLOC": 797,
"CALLOC": 152,
"MMAP": 4,
},
"top_allocations_by_size": [
{"location": "fake_func:fake.py:5", "size": 5242880},
{"location": "fake_func2:fake.py:10", "size": 3072},
Expand Down

0 comments on commit 27ee005

Please sign in to comment.