-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasure_heap.py
executable file
·69 lines (53 loc) · 1.97 KB
/
measure_heap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
import subprocess
import re
import argparse
# Function to format memory size
def format_memory_size(size_bytes):
units = ["B", "KB", "MB", "GB", "TB"]
unit_index = 0
while size_bytes >= 1024 and unit_index < len(units) - 1:
size_bytes /= 1024
unit_index += 1
return f"{size_bytes:.2f} {units[unit_index]}"
def run_program_with_valgrind(program_path, command_args):
# Run the program with valgrind, redirect stderr to DEVNULL, and capture stdout
valgrind_command = [
"valgrind",
"--tool=massif",
"--pages-as-heap=yes",
"--massif-out-file=/dev/stdout",
program_path,
] + command_args
process = subprocess.Popen(
valgrind_command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
)
stdout, _ = process.communicate()
# Convert the captured output to a string
output_str = stdout.decode("utf-8")
# Extract the 'mem_heap_B' lines from the output
mem_heap_lines = [
line.strip() for line in output_str.split("\n") if "mem_heap_B" in line
]
# Extract the values of 'mem_heap_B' using regular expressions
mem_heap_values = [
int(re.search(r"mem_heap_B=(\d+)", line).group(1)) for line in mem_heap_lines
]
# Sort the values in ascending order and get the maximum
max_mem_heap = max(mem_heap_values)
max_mem_heap_formatted = format_memory_size(max_mem_heap)
print(f"The maximum heap size is: {max_mem_heap_formatted}")
def main():
parser = argparse.ArgumentParser(
description="Run a program with Valgrind and measure memory usage."
)
parser.add_argument("program_path", help="Path to the program to run with Valgrind")
parser.add_argument(
"command_args",
nargs="*",
help="Additional command-line arguments for the program",
)
args = parser.parse_args()
run_program_with_valgrind(args.program_path, args.command_args)
if __name__ == "__main__":
main()