File: caliper_hatchet_plot.py

package info (click to toggle)
sundials 7.1.1%2Bdfsg1-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 83,788 kB
  • sloc: ansic: 264,746; f90: 120,987; cpp: 69,481; python: 5,134; sh: 2,483; makefile: 266; perl: 123
file content (76 lines) | stat: -rw-r--r-- 2,767 bytes parent folder | download
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
70
71
72
73
74
75
76
#!/usr/bin/env python3
# -----------------------------------------------------------------------------
# Programmer(s): Cody J. Balos @ LLNL
# -----------------------------------------------------------------------------
# SUNDIALS Copyright Start
# Copyright (c) 2002-2024, Lawrence Livermore National Security
# and Southern Methodist University.
# All rights reserved.
#
# See the top-level LICENSE and NOTICE files for details.
#
# SPDX-License-Identifier: BSD-3-Clause
# SUNDIALS Copyright End
# -----------------------------------------------------------------------------

"""
This script can be used to parse Caliper data if Caliper was run with the
hatchet-region-profile config. The expected format is json-split.
See http://software.llnl.gov/Caliper/BuiltinConfigurations.html and
https://hatchet.readthedocs.io/en/latest/analysis_examples.html#json-file.

The script requires numpy, pandas and hatchet:
  pip install hatchet
  pip install pandas
  pip install numpy

It can also use [Flamegraph](https://github.com/brendangregg/FlameGraph) to
generate a nice stack-trace-like plot, if Flamegraph is available in your path.
"""

import argparse
import hatchet as ht
import numpy as np
import pandas as pd
import subprocess
import tempfile

def run_flamegraph(txt_file, fg_args):
    """
    Call Flamegraph.
    """
    subprocess.run(["flamegraph.pl"] + fg_args + [txt_file])


def Flamegraph(gf, fg_args):
    with tempfile.NamedTemporaryFile() as folded_stack:
        folded_stack.write(str.encode(gf.to_flamegraph()))
        folded_stack.seek(0)
        run_flamegraph(folded_stack.name, fg_args)


parser = argparse.ArgumentParser(description='Utility for parsing/plotting Caliper data with Hatchet.')
subparsers = parser.add_subparsers(help='sub-command help', dest='command')

fg_parser = subparsers.add_parser('flamegraph')
fg_parser.add_argument('--fg-bin',
                       help='path to Flamegraph binary')
fg_parser.add_argument('json_file',
                       help='path to the Caliper json-split data file (generated when the hatchet-region-profile Caliper config is enabled)')
fg_parser.add_argument('fg_args', help='Flamegraph arguments', nargs=argparse.REMAINDER)

ht_parser = subparsers.add_parser('hatchet')
ht_parser.add_argument('json_file',
                       help='path to the Caliper json-split data file (generated when the hatchet-region-profile Caliper config is enabled)')

args = parser.parse_args()
# print(args)

# Use hatchet's ``from_caliper_json`` API with the resulting json-split.
# The result is stored into Hatchet's GraphFrame.
gf = ht.GraphFrame.from_caliper_json(args.json_file)

if args.command == 'flamegraph':
    Flamegraph(gf, args.fg_args)
elif args.command == 'hatchet':
    print(gf.tree())