2
0

Python support (#37)

* Initial python support

* Add python testing

* Increase timeout for bin tests

* cmake-format

* Valid build types + testing + formatting + more

- Enforce valid build types
- Fix to numpy install
- Increase testing timeout
- Fix to cmake format glob
- Fix to backtrace verbose

* Disable stripping libraries by default

* omnitrace exe updates

- new '--print-instructions' option
- changed format of instructions in JSON
- remove no-save-fpr tests

* Default to strip libraries when release build
Este cometimento está contido em:
Jonathan R. Madsen
2022-04-05 00:24:34 -05:00
cometido por GitHub
ascendente 945f541965
cometimento afa3edebab
42 ficheiros modificados com 2442 adições e 239 eliminações
+51
Ver ficheiro
@@ -0,0 +1,51 @@
# ########################################################################################
#
# omnitrace (Python)
#
# ########################################################################################
file(GLOB_RECURSE PYTHON_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.py)
foreach(_IN ${PYTHON_FILES})
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}/python/omnitrace"
_OUT "${_IN}")
configure_file(${_IN} ${_OUT} @ONLY)
install(
FILES ${_OUT}
DESTINATION ${CMAKE_INSTALL_PYTHONDIR}/omnitrace
OPTIONAL)
endforeach()
# ----------------------------------------------------------------------------
# Console scripts
#
function(OMNITRACE_PYTHON_CONSOLE_SCRIPT SCRIPT_NAME SCRIPT_SUBMODULE)
configure_file(${PROJECT_SOURCE_DIR}/cmake/Templates/console-script.in
${PROJECT_BINARY_DIR}/bin/${SCRIPT_NAME} @ONLY)
if(CMAKE_INSTALL_PYTHONDIR)
install(
PROGRAMS ${PROJECT_BINARY_DIR}/bin/${SCRIPT_NAME}
DESTINATION ${CMAKE_INSTALL_BINDIR}
OPTIONAL)
endif()
if(OMNITRACE_BUILD_TESTING OR OMNITRACE_BUILD_PYTHON)
add_test(
NAME ${SCRIPT_NAME}-console-script-test
COMMAND ${PROJECT_BINARY_DIR}/bin/${SCRIPT_NAME} --help
WORKING_DIRECTORY ${PROJECT_BINARY_DIR})
set_tests_properties(
${SCRIPT_NAME}-console-script-test
PROPERTIES ENVIRONMENT
"PYTHONPATH=${PROJECT_BINARY_DIR}/python:$ENV{PYTHONPATH}")
endif()
endfunction()
if(NOT PYTHON_EXECUTABLE)
set(PYTHON_EXECUTABLE "python${OMNITRACE_PYTHON_VERSION}")
endif()
if(OMNITRACE_USE_PYTHON)
omnitrace_python_console_script("omnitrace-python" "omnitrace")
endif()
+67
Ver ficheiro
@@ -0,0 +1,67 @@
#!@PYTHON_EXECUTABLE@
# MIT License
#
# Copyright (c) 2022 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 Research"
__copyright__ = "Copyright 2022, Advanced Micro Devices, Inc."
__license__ = "MIT"
__version__ = "@PROJECT_VERSION@"
__maintainer__ = "AMD Research"
__status__ = "Development"
"""
This submodule imports the timemory Python function profiler
"""
try:
from .profiler import Profiler, FakeProfiler
from .libpyomnitrace.profiler import (
profiler_function,
profiler_init,
profiler_finalize,
)
from .libpyomnitrace import initialize
from .libpyomnitrace import finalize
from .libpyomnitrace.profiler import config as Config
config = Config
profile = Profiler
noprofile = FakeProfiler
__all__ = [
"initialize",
"finalize",
"Profiler",
"Config",
"FakeProfiler",
"profiler_function",
"profiler_init",
"profiler_finalize",
"config",
"profile",
"noprofile",
]
except Exception as e:
print("{}".format(e))
+362
Ver ficheiro
@@ -0,0 +1,362 @@
#!@PYTHON_EXECUTABLE@
# MIT License
#
# Copyright (c) 2022 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 Research"
__copyright__ = "Copyright 2022, Advanced Micro Devices, Inc."
__license__ = "MIT"
__version__ = "@PROJECT_VERSION@"
__maintainer__ = "AMD Research"
__status__ = "Development"
""" @file __main__.py
Command line execution for profiler
"""
import os
import sys
import argparse
import traceback
PY3 = sys.version_info[0] == 3
# 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 .libpyomnitrace.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.")
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument(
"-c",
"--config",
default=None,
type=str,
help="Omnitrace configuration file",
)
parser.add_argument(
"-b",
"--builtin",
action="store_true",
help="Put 'profile' in the builtins. Use 'profile.enable()' and "
"'profile.disable()' in your code to turn it on and off, or "
"'@profile' to decorate a single function, or 'with profile:' "
"to profile a single section of code.",
)
parser.add_argument(
"-s",
"--setup",
default=None,
help="Code to execute before the code to profile",
)
parser.add_argument(
"--trace-c",
type=str2bool,
nargs="?",
const=True,
default=_profiler_config.trace_c,
help="Enable profiling C functions",
)
parser.add_argument(
"-a",
"--include-args",
type=str2bool,
nargs="?",
const=True,
default=_profiler_config.include_args,
help="Encode the argument values",
)
parser.add_argument(
"-l",
"--include-line",
type=str2bool,
nargs="?",
const=True,
default=_profiler_config.include_line,
help="Encode the function line number",
)
parser.add_argument(
"-f",
"--include-file",
type=str2bool,
nargs="?",
const=True,
default=_profiler_config.include_filename,
help="Encode the function filename",
)
parser.add_argument(
"-F",
"--full-filepath",
type=str2bool,
nargs="?",
const=True,
default=_profiler_config.full_filepath,
help="Encode the full function filename (instead of basename)",
)
parser.add_argument(
"--skip-funcs",
type=str,
nargs="+",
default=_profiler_config.skip_functions,
help="Filter out any entries with these function names",
)
parser.add_argument(
"--skip-files",
type=str,
nargs="+",
default=_profiler_config.skip_filenames,
help="Filter out any entries from these files",
)
parser.add_argument(
"--only-funcs",
type=str,
nargs="+",
default=_profiler_config.only_functions,
help="Select only entries with these function names",
)
parser.add_argument(
"--only-files",
type=str,
nargs="+",
default=_profiler_config.only_filenames,
help="Select only entries from these files",
)
parser.add_argument(
"-v",
"--verbosity",
type=int,
default=_profiler_config.verbosity,
help="Logging verbosity",
)
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 function"""
opts = None
argv = None
if "--" in sys.argv:
_idx = sys.argv.index("--")
_argv = sys.argv[(_idx + 1) :]
opts = parse_args(sys.argv[1:_idx])
argv = _argv
else:
if "-h" in sys.argv or "--help" in sys.argv:
opts = parse_args()
else:
argv = sys.argv[1:]
opts = parse_args([])
if len(argv) == 0 or not os.path.isfile(argv[0]):
raise RuntimeError(
"Could not determine input script. Use '--' before "
"the script and its arguments to ensure correct parsing. \nE.g. "
"python -m omnitrace -- ./script.py"
)
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["OMNITRACE_COMMAND_LINE"] = " ".join(argv)
if opts.config is not None:
os.environ["OMNITRACE_CONFIG_FILE"] = ":".join(
[os.environ.get("OMNITRACE_CONFIG_FILE", ""), opts.config]
)
from .libpyomnitrace import initialize
if os.path.isfile(argv[0]):
argv[0] = os.path.realpath(argv[0])
initialize(argv[0])
from .libpyomnitrace.profiler import config as _profiler_config
_profiler_config.trace_c = opts.trace_c
_profiler_config.include_args = opts.include_args
_profiler_config.include_line = opts.include_line
_profiler_config.include_filename = opts.include_file
_profiler_config.full_filepath = opts.full_filepath
_profiler_config.skip_functions = opts.skip_funcs
_profiler_config.skip_filenames = opts.skip_files
_profiler_config.only_functions = opts.only_funcs
_profiler_config.only_filenames = opts.only_files
_profiler_config.verbosity = opts.verbosity
print("[omnitrace]> profiling: {}".format(argv))
sys.argv[:] = 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(sys.argv[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))
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__":
main()
from .libpyomnitrace import finalize
finalize()
+328
Ver ficheiro
@@ -0,0 +1,328 @@
#!@PYTHON_EXECUTABLE@
# MIT License
#
# Copyright (c) 2022 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 Research"
__copyright__ = "Copyright 2022, Advanced Micro Devices, Inc."
__license__ = "MIT"
__version__ = "@PROJECT_VERSION@"
__maintainer__ = "AMD Research"
__status__ = "Development"
import sys
import threading
from functools import wraps
from . import libpyomnitrace
from .libpyomnitrace.profiler import (
profiler_function as _profiler_function,
)
from .libpyomnitrace.profiler import config as _profiler_config
from .libpyomnitrace.profiler import profiler_init as _profiler_init
from .libpyomnitrace.profiler import profiler_finalize as _profiler_fini
__all__ = ["profile", "config", "Profiler", "FakeProfiler", "Config"]
#
def _default_functor():
return True
#
PY3 = sys.version_info[0] == 3
PY35 = PY3 and sys.version_info[1] >= 5
# 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_""")
config = _profiler_config
Config = _profiler_config
#
class Profiler:
"""Provides decorators and context-manager for the omnitrace 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
)
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"""
_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
)
# ---------------------------------------------------------------------------------- #
#
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 dummy decorators and context-manager for the omnitrace profiler"""
# ---------------------------------------------------------------------------------- #
#
@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):
return func(*args, **kwargs)
return function_wrapper
# ---------------------------------------------------------------------------------- #
#
def __enter__(self, *args, **kwargs):
"""Context manager begin"""
pass
# ---------------------------------------------------------------------------------- #
#
def __exit__(self, exec_type, exec_value, exec_tb):
"""Context manager end"""
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)