Rename Omnitrace to ROCm Systems Profiler (#4)
The Omnitrace program is being renamed. Full name: "ROCm Systems Profiler" Package name: "rocprofiler-systems" Binary / Library names: "rocprof-sys-*" --------- Co-authored-by: Xuan Chen <xuchen@amd.com> Signed-off-by: David Galiffi <David.Galiffi@amd.com>
Cette révision appartient à :
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python@_VERSION@
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
__author__ = "AMD ROCm"
|
||||
__copyright__ = "Copyright 2024, Advanced Micro Devices, Inc."
|
||||
__license__ = "MIT"
|
||||
__version__ = "@PROJECT_VERSION@"
|
||||
__maintainer__ = "AMD ROCm"
|
||||
__status__ = "Development"
|
||||
|
||||
"""
|
||||
This submodule imports the timemory Python function profiler
|
||||
"""
|
||||
|
||||
try:
|
||||
import os
|
||||
|
||||
os.environ["ROCPROFSYS_PATH"] = os.path.abspath(
|
||||
os.path.join(os.path.dirname(__file__), "../../..")
|
||||
)
|
||||
|
||||
from .libpyrocprofsys import coverage
|
||||
from . import user
|
||||
from .profiler import Profiler, FakeProfiler
|
||||
from .libpyrocprofsys.profiler import (
|
||||
profiler_function,
|
||||
profiler_init,
|
||||
profiler_finalize,
|
||||
)
|
||||
from .libpyrocprofsys import initialize
|
||||
from .libpyrocprofsys import finalize
|
||||
from .libpyrocprofsys import is_initialized
|
||||
from .libpyrocprofsys import is_finalized
|
||||
from .libpyrocprofsys.profiler import config as Config
|
||||
|
||||
config = Config
|
||||
profile = Profiler
|
||||
noprofile = FakeProfiler
|
||||
|
||||
__all__ = [
|
||||
"initialize",
|
||||
"finalize",
|
||||
"is_initialized",
|
||||
"is_finalized",
|
||||
"Profiler",
|
||||
"Config",
|
||||
"FakeProfiler",
|
||||
"profiler_function",
|
||||
"profiler_init",
|
||||
"profiler_finalize",
|
||||
"config",
|
||||
"profile",
|
||||
"noprofile",
|
||||
"coverage",
|
||||
"user",
|
||||
]
|
||||
|
||||
import atexit
|
||||
|
||||
def _finalize_at_exit():
|
||||
if not is_finalized():
|
||||
finalize()
|
||||
|
||||
atexit.register(_finalize_at_exit)
|
||||
|
||||
except Exception as e:
|
||||
print("{}".format(e))
|
||||
@@ -0,0 +1,413 @@
|
||||
#!/usr/bin/env python@_VERSION@
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
__author__ = "AMD ROCm"
|
||||
__copyright__ = "Copyright 2024, Advanced Micro Devices, Inc."
|
||||
__license__ = "MIT"
|
||||
__version__ = "@PROJECT_VERSION@"
|
||||
__maintainer__ = "AMD ROCm"
|
||||
__status__ = "Development"
|
||||
|
||||
""" @file __main__.py
|
||||
Command line execution for profiler
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
import traceback
|
||||
|
||||
PY3 = sys.version_info[0] == 3
|
||||
_ROCPROFSYS_PYTHON_SCRIPT_FILE = None
|
||||
|
||||
# Python 3.x compatibility utils: execfile
|
||||
try:
|
||||
execfile
|
||||
except NameError:
|
||||
# Python 3.x doesn't have 'execfile' builtin
|
||||
import builtins
|
||||
|
||||
exec_ = getattr(builtins, "exec")
|
||||
|
||||
def execfile(filename, globals=None, locals=None):
|
||||
with open(filename, "rb") as f:
|
||||
exec_(compile(f.read(), filename, "exec"), globals, locals)
|
||||
|
||||
|
||||
def find_script(script_name):
|
||||
"""Find the script.
|
||||
|
||||
If the input is not a file, then $PATH will be searched.
|
||||
"""
|
||||
if os.path.isfile(script_name):
|
||||
return script_name
|
||||
path = os.getenv("PATH", os.defpath).split(os.pathsep)
|
||||
for dir in path:
|
||||
if dir == "":
|
||||
continue
|
||||
fn = os.path.join(dir, script_name)
|
||||
if os.path.isfile(fn):
|
||||
return fn
|
||||
|
||||
sys.stderr.write("Could not find script %s\n" % script_name)
|
||||
raise SystemExit(1)
|
||||
|
||||
|
||||
def parse_args(args=None):
|
||||
"""Parse the arguments"""
|
||||
|
||||
if args is None:
|
||||
args = sys.argv[1:]
|
||||
|
||||
from .libpyrocprofsys.profiler import config as _profiler_config
|
||||
|
||||
def str2bool(v):
|
||||
if isinstance(v, bool):
|
||||
return v
|
||||
if v.lower() in ("yes", "true", "t", "y", "1"):
|
||||
return True
|
||||
elif v.lower() in ("no", "false", "f", "n", "0"):
|
||||
return False
|
||||
else:
|
||||
raise argparse.ArgumentTypeError("Boolean value expected.")
|
||||
|
||||
_default_label = []
|
||||
if _profiler_config.include_args:
|
||||
_default_label.append("args")
|
||||
if _profiler_config.include_filename:
|
||||
_default_label.append("file")
|
||||
if _profiler_config.include_line:
|
||||
_default_label.append("line")
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
"rocprofsys",
|
||||
add_help=True,
|
||||
epilog="usage: {} -m rocprofsys <ROCPROFSYS_ARGS> -- <SCRIPT> <SCRIPT_ARGS>".format(
|
||||
os.path.basename(sys.executable)
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"-v",
|
||||
"--verbosity",
|
||||
type=int,
|
||||
default=_profiler_config.verbosity,
|
||||
help="Logging verbosity",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-b",
|
||||
"--builtin",
|
||||
action="store_true",
|
||||
help=(
|
||||
"Put 'profile' in the builtins. Use '@profile' to decorate a single function, "
|
||||
"or 'with profile:' to profile a single section of code."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c",
|
||||
"--config",
|
||||
default=None,
|
||||
type=str,
|
||||
metavar="FILE",
|
||||
help="ROCm Systems Profiler configuration file",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
"--setup",
|
||||
default=None,
|
||||
metavar="FILE",
|
||||
help="Code to execute before the code to profile",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-F",
|
||||
"--full-filepath",
|
||||
type=str2bool,
|
||||
nargs="?",
|
||||
metavar="BOOL",
|
||||
const=True,
|
||||
default=_profiler_config.full_filepath,
|
||||
help="Encode the full function filename (instead of basename)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--label",
|
||||
type=str,
|
||||
choices=("args", "file", "line"),
|
||||
nargs="*",
|
||||
default=_default_label,
|
||||
help="Encode the function arguments, filename, and/or line number into the profiling function label",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-I",
|
||||
"--function-include",
|
||||
type=str,
|
||||
nargs="+",
|
||||
metavar="FUNC",
|
||||
default=_profiler_config.include_functions,
|
||||
help="Include any entries with these function names",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-E",
|
||||
"--function-exclude",
|
||||
type=str,
|
||||
nargs="+",
|
||||
metavar="FUNC",
|
||||
default=_profiler_config.exclude_functions,
|
||||
help="Filter out any entries with these function names",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-R",
|
||||
"--function-restrict",
|
||||
type=str,
|
||||
nargs="+",
|
||||
metavar="FUNC",
|
||||
default=_profiler_config.restrict_functions,
|
||||
help="Select only entries with these function names",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-MI",
|
||||
"--module-include",
|
||||
type=str,
|
||||
nargs="+",
|
||||
metavar="FILE",
|
||||
default=_profiler_config.include_modules,
|
||||
help="Include any entries from these files",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-ME",
|
||||
"--module-exclude",
|
||||
type=str,
|
||||
nargs="+",
|
||||
metavar="FILE",
|
||||
default=_profiler_config.exclude_modules,
|
||||
help="Filter out any entries from these files",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-MR",
|
||||
"--module-restrict",
|
||||
type=str,
|
||||
nargs="+",
|
||||
metavar="FILE",
|
||||
default=_profiler_config.restrict_modules,
|
||||
help="Select only entries from these files",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--trace-c",
|
||||
type=str2bool,
|
||||
nargs="?",
|
||||
metavar="BOOL",
|
||||
const=True,
|
||||
default=_profiler_config.trace_c,
|
||||
help="Enable profiling C functions",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-a",
|
||||
"--annotate-trace",
|
||||
type=str2bool,
|
||||
nargs="?",
|
||||
metavar="BOOL",
|
||||
const=True,
|
||||
default=_profiler_config.annotate_trace,
|
||||
help="Enable perfetto debug annotations",
|
||||
)
|
||||
|
||||
return parser.parse_args(args)
|
||||
|
||||
|
||||
def get_value(env_var, default_value, dtype, arg=None):
|
||||
if arg is not None:
|
||||
return dtype(arg)
|
||||
else:
|
||||
val = os.environ.get(env_var)
|
||||
if val is None:
|
||||
os.environ[env_var] = "{}".format(default_value)
|
||||
return dtype(default_value)
|
||||
else:
|
||||
return dtype(val)
|
||||
|
||||
|
||||
def run(prof, cmd):
|
||||
if len(cmd) == 0:
|
||||
return
|
||||
|
||||
progname = cmd[0]
|
||||
sys.path.insert(0, os.path.dirname(progname))
|
||||
with open(progname, "rb") as fp:
|
||||
code = compile(fp.read(), progname, "exec")
|
||||
|
||||
import __main__
|
||||
|
||||
dict = __main__.__dict__
|
||||
print("code: {} {}".format(type(code).__name__, code))
|
||||
globs = {
|
||||
"__file__": progname,
|
||||
"__name__": "__main__",
|
||||
"__package__": None,
|
||||
"__cached__": None,
|
||||
**dict,
|
||||
}
|
||||
|
||||
prof.runctx(code, globs, None)
|
||||
|
||||
|
||||
def main(main_args=sys.argv):
|
||||
"""Main function"""
|
||||
|
||||
opts = None
|
||||
argv = None
|
||||
if "--" in main_args:
|
||||
_idx = main_args.index("--")
|
||||
_argv = main_args[(_idx + 1) :]
|
||||
opts = parse_args(main_args[1:_idx])
|
||||
argv = _argv
|
||||
else:
|
||||
if "-h" in main_args or "--help" in main_args:
|
||||
opts = parse_args()
|
||||
else:
|
||||
argv = main_args[1:]
|
||||
opts = parse_args([])
|
||||
if len(argv) == 0 or not os.path.isfile(argv[0]):
|
||||
raise RuntimeError(
|
||||
"Could not determine input script in '{}'. Use '--' before "
|
||||
"the script and its arguments to ensure correct parsing. \nE.g. "
|
||||
"python -m rocprofsys -- ./script.py".format(" ".join(argv))
|
||||
)
|
||||
|
||||
if len(argv) > 1:
|
||||
if argv[0] == "-m":
|
||||
argv = argv[1:]
|
||||
elif argv[0] == "-c":
|
||||
argv[0] = os.path.basename(sys.executable)
|
||||
else:
|
||||
while len(argv) > 1 and argv[0].startswith("-"):
|
||||
argv = argv[1:]
|
||||
if os.path.exists(argv[0]):
|
||||
break
|
||||
|
||||
if argv:
|
||||
os.environ["ROCPROFSYS_COMMAND_LINE"] = " ".join(argv)
|
||||
|
||||
if opts.config is not None:
|
||||
os.environ["ROCPROFSYS_CONFIG_FILE"] = ":".join(
|
||||
[os.environ.get("ROCPROFSYS_CONFIG_FILE", ""), opts.config]
|
||||
)
|
||||
|
||||
from .libpyrocprofsys import initialize
|
||||
|
||||
if os.path.isfile(argv[0]):
|
||||
argv[0] = os.path.realpath(argv[0])
|
||||
|
||||
initialize(argv)
|
||||
|
||||
from .libpyrocprofsys.profiler import config as _profiler_config
|
||||
|
||||
_profiler_config.trace_c = opts.trace_c
|
||||
_profiler_config.include_args = "args" in opts.label
|
||||
_profiler_config.include_line = "line" in opts.label
|
||||
_profiler_config.include_filename = "file" in opts.label
|
||||
_profiler_config.full_filepath = opts.full_filepath
|
||||
_profiler_config.include_functions = opts.function_include
|
||||
_profiler_config.include_modules = opts.module_include
|
||||
_profiler_config.exclude_functions = opts.function_exclude
|
||||
_profiler_config.exclude_modules = opts.module_exclude
|
||||
_profiler_config.restrict_functions = opts.function_restrict
|
||||
_profiler_config.restrict_modules = opts.module_restrict
|
||||
_profiler_config.annotate_trace = opts.annotate_trace
|
||||
_profiler_config.verbosity = opts.verbosity
|
||||
|
||||
print("[rocprofsys]> profiling: {}".format(argv))
|
||||
|
||||
main_args[:] = argv
|
||||
if opts.setup is not None:
|
||||
# Run some setup code outside of the profiler. This is good for large
|
||||
# imports.
|
||||
setup_file = find_script(opts.setup)
|
||||
__file__ = setup_file
|
||||
__name__ = "__main__"
|
||||
# Make sure the script's directory is on sys.path
|
||||
sys.path.insert(0, os.path.dirname(setup_file))
|
||||
ns = locals()
|
||||
execfile(setup_file, ns, ns)
|
||||
|
||||
from . import Profiler, FakeProfiler
|
||||
|
||||
script_file = find_script(main_args[0])
|
||||
__file__ = script_file
|
||||
__name__ = "__main__"
|
||||
# Make sure the script's directory is on sys.path
|
||||
sys.path.insert(0, os.path.dirname(script_file))
|
||||
|
||||
_ROCPROFSYS_PYTHON_SCRIPT_FILE = script_file
|
||||
os.environ["ROCPROFSYS_PYTHON_SCRIPT_FILE"] = script_file
|
||||
|
||||
prof = Profiler()
|
||||
fake = FakeProfiler()
|
||||
|
||||
if PY3:
|
||||
import builtins
|
||||
else:
|
||||
import __builtin__ as builtins
|
||||
|
||||
builtins.__dict__["profile"] = prof
|
||||
builtins.__dict__["noprofile"] = fake
|
||||
builtins.__dict__["trace"] = prof
|
||||
builtins.__dict__["notrace"] = fake
|
||||
|
||||
try:
|
||||
try:
|
||||
if not opts.builtin:
|
||||
prof.start()
|
||||
execfile_ = execfile
|
||||
ns = locals()
|
||||
if opts.builtin:
|
||||
execfile(script_file, ns, ns)
|
||||
else:
|
||||
prof.runctx("execfile_(%r, globals())" % (script_file,), ns, ns)
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
pass
|
||||
finally:
|
||||
if not opts.builtin:
|
||||
prof.stop()
|
||||
del prof
|
||||
del fake
|
||||
except Exception as e:
|
||||
exc_type, exc_value, exc_traceback = sys.exc_info()
|
||||
traceback.print_exception(exc_type, exc_value, exc_traceback, limit=10)
|
||||
print("Exception - {}".format(e))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = sys.argv
|
||||
if _ROCPROFSYS_PYTHON_SCRIPT_FILE is None:
|
||||
_ROCPROFSYS_PYTHON_SCRIPT_FILE = os.environ.get(
|
||||
"ROCPROFSYS_PYTHON_SCRIPT_FILE", None
|
||||
)
|
||||
|
||||
if "--" not in args and _ROCPROFSYS_PYTHON_SCRIPT_FILE is not None:
|
||||
args = [args[0]] + ["--", _ROCPROFSYS_PYTHON_SCRIPT_FILE] + args[1:]
|
||||
os.environ["ROCPROFSYS_USE_PID"] = "ON"
|
||||
|
||||
main(args)
|
||||
from .libpyrocprofsys import finalize
|
||||
|
||||
finalize()
|
||||
@@ -0,0 +1,136 @@
|
||||
#!/usr/bin/env python@_VERSION@
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
__author__ = "AMD ROCm"
|
||||
__copyright__ = "Copyright 2024, Advanced Micro Devices, Inc."
|
||||
__license__ = "MIT"
|
||||
__version__ = "@PROJECT_VERSION@"
|
||||
__maintainer__ = "AMD ROCm"
|
||||
__status__ = "Development"
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
from . import libpyrocprofsys
|
||||
from .libpyrocprofsys.profiler import profiler_init as _profiler_init
|
||||
from .libpyrocprofsys.profiler import profiler_finalize as _profiler_fini
|
||||
|
||||
|
||||
__all__ = ["exec_", "_file", "_get_argv", "_initialize", "_finalize"]
|
||||
|
||||
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
# exec (from https://bitbucket.org/gutworth/six/):
|
||||
if PY3:
|
||||
import builtins
|
||||
|
||||
exec_ = getattr(builtins, "exec")
|
||||
del builtins
|
||||
else:
|
||||
|
||||
def exec_(_code_, _globs_=None, _locs_=None):
|
||||
"""Execute code in a namespace."""
|
||||
if _globs_ is None:
|
||||
frame = sys._getframe(1)
|
||||
_globs_ = frame.f_globals
|
||||
if _locs_ is None:
|
||||
_locs_ = frame.f_locals
|
||||
del frame
|
||||
elif _locs_ is None:
|
||||
_locs_ = _globs_
|
||||
exec("""exec _code_ in _globs_, _locs_""")
|
||||
|
||||
|
||||
def _file(back=2, only_basename=True, use_dirname=False, noquotes=True):
|
||||
"""
|
||||
Returns the file name
|
||||
"""
|
||||
|
||||
from os.path import basename, dirname
|
||||
|
||||
def get_fcode(back):
|
||||
fname = "<module>"
|
||||
try:
|
||||
fname = sys._getframe(back).f_code.co_filename
|
||||
except Exception as e:
|
||||
print(e)
|
||||
fname = "<module>"
|
||||
return fname
|
||||
|
||||
result = None
|
||||
if only_basename is True:
|
||||
if use_dirname is True:
|
||||
result = "{}".format(
|
||||
join(
|
||||
basename(dirname(get_fcode(back))),
|
||||
basename(get_fcode(back)),
|
||||
)
|
||||
)
|
||||
else:
|
||||
result = "{}".format(basename(get_fcode(back)))
|
||||
else:
|
||||
result = "{}".format(get_fcode(back))
|
||||
|
||||
if noquotes is False:
|
||||
result = "'{}'".format(result)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def _get_argv(init_file, argv=None):
|
||||
if argv is None:
|
||||
argv = sys.argv[:]
|
||||
|
||||
if "--" in argv:
|
||||
_idx = argv.index("--")
|
||||
argv = sys.argv[(_idx + 1) :]
|
||||
|
||||
if len(argv) > 1:
|
||||
if argv[0] == "-m":
|
||||
argv = argv[1:]
|
||||
elif argv[0] == "-c":
|
||||
argv[0] = os.path.basename(sys.executable)
|
||||
else:
|
||||
while len(argv) > 1 and argv[0].startswith("-"):
|
||||
argv = argv[1:]
|
||||
if os.path.exists(argv[0]):
|
||||
break
|
||||
if len(argv) == 0:
|
||||
argv = [init_file]
|
||||
elif not os.path.exists(argv[0]):
|
||||
argv[0] = init_file
|
||||
|
||||
return argv
|
||||
|
||||
|
||||
def _initialize(_file):
|
||||
if not libpyrocprofsys.is_initialized():
|
||||
libpyrocprofsys.initialize(_get_argv(_file))
|
||||
|
||||
|
||||
def _finalize():
|
||||
if libpyrocprofsys.is_initialized() and not libpyrocprofsys.is_finalized():
|
||||
_profiler_fini()
|
||||
@@ -0,0 +1,278 @@
|
||||
#!/usr/bin/env python@_VERSION@
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
__author__ = "AMD ROCm"
|
||||
__copyright__ = "Copyright 2024, Advanced Micro Devices, Inc."
|
||||
__license__ = "MIT"
|
||||
__version__ = "@PROJECT_VERSION@"
|
||||
__maintainer__ = "AMD ROCm"
|
||||
__status__ = "Development"
|
||||
|
||||
import os
|
||||
import sys
|
||||
import threading
|
||||
from functools import wraps
|
||||
|
||||
from .common import exec_
|
||||
from .common import _initialize
|
||||
from .common import _file
|
||||
|
||||
from . import libpyrocprofsys
|
||||
from .libpyrocprofsys.profiler import (
|
||||
profiler_function as _profiler_function,
|
||||
)
|
||||
from .libpyrocprofsys.profiler import config as _profiler_config
|
||||
from .libpyrocprofsys.profiler import profiler_init as _profiler_init
|
||||
from .libpyrocprofsys.profiler import profiler_finalize as _profiler_fini
|
||||
from .libpyrocprofsys.profiler import profiler_pause as _profiler_pause
|
||||
from .libpyrocprofsys.profiler import profiler_resume as _profiler_resume
|
||||
|
||||
__all__ = [
|
||||
"profile",
|
||||
"noprofile",
|
||||
"config",
|
||||
"Profiler",
|
||||
"FakeProfiler",
|
||||
"Config",
|
||||
]
|
||||
|
||||
|
||||
config = _profiler_config
|
||||
Config = _profiler_config
|
||||
|
||||
|
||||
def _default_functor():
|
||||
return True
|
||||
|
||||
|
||||
class Profiler:
|
||||
"""Provides decorators and context-manager for the rocprof-sys profilers"""
|
||||
|
||||
global _default_functor
|
||||
|
||||
# static variable
|
||||
_conditional_functor = _default_functor
|
||||
|
||||
@staticmethod
|
||||
def condition(functor):
|
||||
"""Assign a function evaluating whether to enable the profiler"""
|
||||
Profiler._conditional_functor = functor
|
||||
|
||||
@staticmethod
|
||||
def is_enabled():
|
||||
"""Checks whether the profiler is enabled"""
|
||||
|
||||
try:
|
||||
return Profiler._conditional_functor()
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
""" """
|
||||
|
||||
self._original_function = (
|
||||
sys.getprofile() if sys.getprofile() != _profiler_function else None
|
||||
)
|
||||
self._unset = 0
|
||||
self._use = (
|
||||
not _profiler_config._is_running
|
||||
and Profiler.is_enabled() is True
|
||||
and not libpyrocprofsys.is_finalized()
|
||||
)
|
||||
self._file = _file()
|
||||
self.debug = kwargs["debug"] if "debug" in kwargs else False
|
||||
|
||||
def __del__(self):
|
||||
"""Make sure the profiler stops"""
|
||||
|
||||
self.stop()
|
||||
sys.setprofile(self._original_function)
|
||||
|
||||
def configure(self):
|
||||
"""Initialize, configure the bundle, store original profiler function"""
|
||||
|
||||
_initialize(self._file)
|
||||
|
||||
_profiler_init()
|
||||
|
||||
# store original
|
||||
if self.debug:
|
||||
sys.stderr.write("setting profile function...\n")
|
||||
if sys.getprofile() != _profiler_function:
|
||||
self._original_function = sys.getprofile()
|
||||
|
||||
if self.debug:
|
||||
sys.stderr.write("Tracer configured...\n")
|
||||
|
||||
def update(self):
|
||||
"""Updates whether the profiler is already running based on whether the tracer
|
||||
is not already running, is enabled, and the function is not already set
|
||||
"""
|
||||
|
||||
self._use = (
|
||||
not _profiler_config._is_running
|
||||
and Profiler.is_enabled() is True
|
||||
and sys.getprofile() == self._original_function
|
||||
and not libpyrocprofsys.is_finalized()
|
||||
)
|
||||
|
||||
def start(self):
|
||||
"""Start the profiler explicitly"""
|
||||
|
||||
self.update()
|
||||
if self._use:
|
||||
if self.debug:
|
||||
sys.stderr.write("Profiler starting...\n")
|
||||
self.configure()
|
||||
sys.setprofile(_profiler_function)
|
||||
threading.setprofile(_profiler_function)
|
||||
if self.debug:
|
||||
sys.stderr.write("Profiler started...\n")
|
||||
|
||||
self._unset = self._unset + 1
|
||||
return self._unset
|
||||
|
||||
def stop(self):
|
||||
"""Stop the profiler explicitly"""
|
||||
|
||||
self._unset = self._unset - 1
|
||||
if self._unset == 0:
|
||||
if self.debug:
|
||||
sys.stderr.write("Profiler stopping...\n")
|
||||
sys.setprofile(self._original_function)
|
||||
_profiler_fini()
|
||||
if self.debug:
|
||||
sys.stderr.write("Profiler stopped...\n")
|
||||
|
||||
return self._unset
|
||||
|
||||
def __call__(self, func):
|
||||
"""Decorator"""
|
||||
|
||||
@wraps(func)
|
||||
def function_wrapper(*args, **kwargs):
|
||||
# store whether this tracer started
|
||||
self.start()
|
||||
# execute the wrapped function
|
||||
result = func(*args, **kwargs)
|
||||
# unset the profiler if this wrapper set it
|
||||
self.stop()
|
||||
# return the result of the wrapped function
|
||||
return result
|
||||
|
||||
return function_wrapper
|
||||
|
||||
def __enter__(self, *args, **kwargs):
|
||||
"""Context manager start function"""
|
||||
|
||||
self.start()
|
||||
|
||||
def __exit__(self, exec_type, exec_value, exec_tb):
|
||||
"""Context manager stop function"""
|
||||
|
||||
self.stop()
|
||||
|
||||
if exec_type is not None and exec_value is not None and exec_tb is not None:
|
||||
import traceback
|
||||
|
||||
traceback.print_exception(exec_type, exec_value, exec_tb, limit=5)
|
||||
|
||||
def run(self, cmd):
|
||||
"""Execute and profile a command"""
|
||||
|
||||
import __main__
|
||||
|
||||
dict = __main__.__dict__
|
||||
if isinstance(cmd, str):
|
||||
return self.runctx(cmd, dict, dict)
|
||||
else:
|
||||
return self.runctx(" ".join(cmd), dict, dict)
|
||||
|
||||
def runctx(self, cmd, globals, locals):
|
||||
"""Profile a context"""
|
||||
|
||||
try:
|
||||
self.start()
|
||||
exec_(cmd, globals, locals)
|
||||
finally:
|
||||
self.stop()
|
||||
|
||||
return self
|
||||
|
||||
def runcall(self, func, *args, **kw):
|
||||
"""Profile a single function call"""
|
||||
|
||||
try:
|
||||
self.start()
|
||||
return func(*args, **kw)
|
||||
finally:
|
||||
self.stop()
|
||||
|
||||
|
||||
profile = Profiler
|
||||
|
||||
|
||||
class FakeProfiler:
|
||||
"""Provides decorators and context-manager for disabling the profiling"""
|
||||
|
||||
@staticmethod
|
||||
def condition(functor):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def is_enabled():
|
||||
return False
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def __call__(self, func):
|
||||
"""Decorator"""
|
||||
|
||||
@wraps(func)
|
||||
def function_wrapper(*args, **kwargs):
|
||||
_profiler_pause()
|
||||
ret = func(*args, **kwargs)
|
||||
_profiler_resume()
|
||||
return ret
|
||||
|
||||
return function_wrapper
|
||||
|
||||
def __enter__(self, *args, **kwargs):
|
||||
"""Context manager begin"""
|
||||
_profiler_pause()
|
||||
|
||||
def __exit__(self, exec_type, exec_value, exec_tb):
|
||||
"""Context manager end"""
|
||||
|
||||
_profiler_resume()
|
||||
import traceback
|
||||
|
||||
if exec_type is not None and exec_value is not None and exec_tb is not None:
|
||||
traceback.print_exception(exec_type, exec_value, exec_tb, limit=5)
|
||||
|
||||
|
||||
noprofile = FakeProfiler
|
||||
@@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env python@_VERSION@
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
__author__ = "AMD ROCm"
|
||||
__copyright__ = "Copyright 2024, Advanced Micro Devices, Inc."
|
||||
__license__ = "MIT"
|
||||
__version__ = "@PROJECT_VERSION@"
|
||||
__maintainer__ = "AMD ROCm"
|
||||
__status__ = "Development"
|
||||
|
||||
from functools import wraps
|
||||
|
||||
from . import libpyrocprofsys
|
||||
from .libpyrocprofsys import user as _libuser
|
||||
from .libpyrocprofsys.user import start_trace
|
||||
from .libpyrocprofsys.user import start_thread_trace
|
||||
from .libpyrocprofsys.user import stop_trace
|
||||
from .libpyrocprofsys.user import stop_thread_trace
|
||||
from .libpyrocprofsys.user import push_region
|
||||
from .libpyrocprofsys.user import pop_region
|
||||
|
||||
from .common import _initialize
|
||||
from .common import _file
|
||||
|
||||
|
||||
__all__ = [
|
||||
"region",
|
||||
"Region",
|
||||
"start_trace",
|
||||
"start_thread_trace",
|
||||
"stop_trace",
|
||||
"stop_thread_trace",
|
||||
"push_region",
|
||||
"pop_region",
|
||||
]
|
||||
|
||||
|
||||
class Region:
|
||||
"""Provides decorators and context-manager for the rocprof-sys user-defined regions"""
|
||||
|
||||
# static variable
|
||||
_counter = 0
|
||||
|
||||
def __init__(self, _label):
|
||||
"""Stores the label"""
|
||||
self._active = False
|
||||
self._label = _label
|
||||
self._count = 0
|
||||
self._file = _file() if Region._counter == 0 else None
|
||||
|
||||
def __del__(self):
|
||||
"""Stops"""
|
||||
self.stop()
|
||||
|
||||
def start(self):
|
||||
"""Start the region"""
|
||||
|
||||
if not self._active:
|
||||
self._active = True
|
||||
self._count = Region._counter
|
||||
if self._file is not None:
|
||||
_initialize(self._file)
|
||||
Region._counter += 1
|
||||
_libuser.push_region(self._label)
|
||||
|
||||
def stop(self):
|
||||
"""Stop the region"""
|
||||
|
||||
if self._active:
|
||||
Region._counter -= 1
|
||||
_count = Region._counter
|
||||
self._active = False
|
||||
if _count != self._count:
|
||||
raise RuntimeError(
|
||||
f"{self._label} was not popped in the order it was pushed. Current stack number: {_count}, expected stack number: {self._count}"
|
||||
)
|
||||
_libuser.pop_region(self._label)
|
||||
|
||||
def __call__(self, func):
|
||||
"""Decorator"""
|
||||
|
||||
@wraps(func)
|
||||
def function_wrapper(*args, **kwargs):
|
||||
# start the region
|
||||
self.start()
|
||||
# execute the wrapped function
|
||||
result = func(*args, **kwargs)
|
||||
# stop the region
|
||||
self.stop()
|
||||
return result
|
||||
|
||||
return function_wrapper
|
||||
|
||||
def __enter__(self, *args, **kwargs):
|
||||
"""Context manager start function"""
|
||||
|
||||
self.start()
|
||||
|
||||
def __exit__(self, exec_type, exec_value, exec_tb):
|
||||
"""Context manager stop function"""
|
||||
|
||||
self.stop()
|
||||
|
||||
if exec_type is not None and exec_value is not None and exec_tb is not None:
|
||||
import traceback
|
||||
|
||||
traceback.print_exception(exec_type, exec_value, exec_tb, limit=5)
|
||||
|
||||
def runcall(self, func, *args, **kwargs):
|
||||
"""Profile a single function call"""
|
||||
|
||||
try:
|
||||
self.start()
|
||||
return func(*args, **kwargs)
|
||||
finally:
|
||||
self.stop()
|
||||
|
||||
|
||||
region = Region
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur