Example python program for using trace-cruncher to collect performance statistics of a given process. Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@xxxxxxxxx> --- examples/perf_sampling.py | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 examples/perf_sampling.py diff --git a/examples/perf_sampling.py b/examples/perf_sampling.py new file mode 100755 index 0000000..6219bff --- /dev/null +++ b/examples/perf_sampling.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +""" +SPDX-License-Identifier: CC-BY-4.0 + +Copyright 2022 VMware Inc, Tzvetomir Stoyanov (VMware) <tz.stoyanov@xxxxxxxxx> +""" + +import sys +import time +import shutil + +import tracecruncher.perfpy as perf + +def SortKey(sample): + return sample.time() + +if __name__ == "__main__": + if len(sys.argv) < 2: + print('Usage: ', sys.argv[0], ' [PROCESS]') + sys.exit(1) + + # Create perf sample object for the given process + if sys.argv[1].isdigit(): + p = perf.sampler_instance(pid=int(sys.argv[1]), freq=99) + else: + file = shutil.which(sys.argv[1]) + if file is None: + print('Cannot find ', sys.argv[1], ' in the system') + sys.exit(1) + p = perf.sampler_instance(command=file, argv=sys.argv[1:], freq=99) + + print('Start collecting performance data, press ctrl+c to stop') + # Start collecting performance traces + p.enable(wait=True) + + # Get collected samples + samples = p.get_samples() + # Sort the list based on the timestamp + samples.sort(key=SortKey) + time = 0 + ip_count = 0 + for s in samples: + # Print PID, TID, time and trace depth of each sample + if time == 0: + time = s.time() + print("{0} {1} ({2}), +{3}:".format(s.ip(), s.tid(), s.tid_comm(), s.time() - time)) + ips = s.stack() + ip_count += len(ips) + for ip in reversed(ips): + # Print stack trace of the sample + print("\t{0}".format(ip)) + print("\nCollected {0} samples, {1} ip traces".format(len(samples), ip_count)) \ No newline at end of file -- 2.35.1