aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xscripts/matplotlib_histograms.py36
-rwxr-xr-xsrc/chattyparser.py17
2 files changed, 34 insertions, 19 deletions
diff --git a/scripts/matplotlib_histograms.py b/scripts/matplotlib_histograms.py
index 4d3af45..7fae59a 100755
--- a/scripts/matplotlib_histograms.py
+++ b/scripts/matplotlib_histograms.py
@@ -11,37 +11,44 @@ import sys
import matplotlib.pyplot as plt
+currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
+parentdir = os.path.dirname(currentdir)
+sys.path.insert(0, parentdir)
+
+import src.chattyparser
def main():
parser = argparse.ArgumentParser(description="Plot histograms using a malt or chattymalloc output file")
parser.add_argument("input_file", help="path to malt or chattymalloc output file", type=str)
parser.add_argument("-e", "--export", help="export to csv", action="store_true")
+ parser.add_argument("-n", "--no-ascii", help="don't output a ascii histogram", action="store_true")
+ parser.add_argument("-i", "--interactive", help="open interactive matplotlib histogram plots",
+ action="store_true")
args = parser.parse_args()
- fname, fext = os.path.splitext(args.input_file)
- fname = os.path.basename(fname)
+ fpath, fext = os.path.splitext(args.input_file)
+ fname = os.path.basename(fpath)
# chattymalloc
if fname.startswith("chatty") and fext == ".txt":
- # import chattyparser
- currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
- parentdir = os.path.dirname(currentdir)
- sys.path.insert(0, parentdir)
-
- chattyparser = importlib.import_module("src.chattyparser")
-
- hist, _, _ = chattyparser.parse(args.input_file, coll_size=False)
+ hist, calls, _ = src.chattyparser.parse(args.input_file, coll_size=False)
# malt
else:
with open(args.input_file, "r") as json_file:
- hist = json.load(json_file)["memStats"]["sizeMap"]
+ malt_res = json.load(json_file)
+
+ hist = malt_res["memStats"]["sizeMap"]
+ calls = {}
+ for thread in malt_res["threads"]:
+ for func, data in thread["stats"].items():
+ calls[func] = calls.get(func, 0) + data["count"]
if args.export:
- with open(f"{fname}.csv", "w") as csv_file:
+ with open(f"{fpath}.csv", "w") as csv_file:
print("Size", "Amount", file=csv_file)
for size, amount in hist.items():
print(size, amount, file=csv_file)
- else:
+ if args.interactive:
sizes = []
sizes_smaller_4K = []
sizes_smaller_32K = []
@@ -74,5 +81,8 @@ def main():
plt.title("All sizes")
plt.show()
+ if not args.no_ascii:
+ src.chattyparser.plot_hist_ascii(f"{fpath}.hist.txt", hist, calls)
+
if __name__ == "__main__":
main()
diff --git a/src/chattyparser.py b/src/chattyparser.py
index 4e4acae..87a5c05 100755
--- a/src/chattyparser.py
+++ b/src/chattyparser.py
@@ -177,17 +177,21 @@ def plot_profile(total_sizes, trace_path, plot_path, sizes):
def plot_hist_ascii(path, hist, calls):
"""Plot an ascii histogram"""
+ # make sure all sizes are stored as ints
+ for nonint_key in [key for key in hist.keys() if type(key) is not int]:
+ hist[int(nonint_key)] = hist[nonint_key]
+ del hist[nonint_key]
+
bins = {}
for size in sorted(hist):
- size_class = int(size / 16)
+ size_class = size / 16
bins[size_class] = bins.get(size_class, 0) + hist[size]
- total = sum(calls.values()) - calls["free"]
with open(path, "w") as hist_file:
- print("Total function calls:", total, file=hist_file)
- for func in TRACE_REGEX:
- print(func, calls[func], file=hist_file)
+ print("Total function calls:", sum(calls.values()), file=hist_file)
+ for func, func_calls in calls.items():
+ print(func, func_calls, file=hist_file)
print(file=hist_file)
@@ -201,6 +205,7 @@ def plot_hist_ascii(path, hist, calls):
print(file=hist_file)
print("Histogram of sizes:", file=hist_file)
+ total = sum(hist.values())
sbins = sorted(bins)
binmaxlength = str(len(str(sbins[-1])) + 1)
amountmaxlength = str(len(str(sorted(bins.values())[-1])))
@@ -220,7 +225,7 @@ if __name__ == "__main__":
print("License GPLv3: GNU GPL version 3 <http://gnu.org/licenses/gpl.html>")
exit(0)
- if len(sys.argv) != 2:
+ if len(sys.argv) != 2 or sys.argv[1] in ["-h", "--help"]:
print("chattyparser: parse chattymalloc output and",
"create size histogram and memory profile", file=sys.stderr)
print(f"Usage: {sys.argv[0]} chattymalloc-file", file=sys.stderr)