User regions in Python (#57)
* User regions in Python * User-region testing + common submodule - Updated examples/python/source.py to use user-regions - Python-level user submodule - Python-level common submodule - clean-up of profiler python code - extended source.py testing to include the user-regions
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
74fe68101b
Коммит
4dd144a32c
@@ -35,6 +35,10 @@ import sys
|
||||
import threading
|
||||
from functools import wraps
|
||||
|
||||
from .common import exec_
|
||||
from .common import _initialize
|
||||
from .common import _file
|
||||
|
||||
from . import libpyomnitrace
|
||||
from .libpyomnitrace.profiler import (
|
||||
profiler_function as _profiler_function,
|
||||
@@ -43,107 +47,17 @@ 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
|
||||
|
||||
|
||||
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 _default_functor():
|
||||
return True
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
#
|
||||
class Profiler:
|
||||
"""Provides decorators and context-manager for the omnitrace profilers"""
|
||||
|
||||
@@ -152,15 +66,11 @@ class Profiler:
|
||||
# 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"""
|
||||
@@ -171,8 +81,6 @@ class Profiler:
|
||||
pass
|
||||
return False
|
||||
|
||||
# ---------------------------------------------------------------------------------- #
|
||||
#
|
||||
def __init__(self, **kwargs):
|
||||
""" """
|
||||
|
||||
@@ -188,21 +96,16 @@ class Profiler:
|
||||
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"""
|
||||
|
||||
if not libpyomnitrace.is_initialized():
|
||||
libpyomnitrace.initialize(_get_argv(self._file))
|
||||
_initialize(self._file)
|
||||
|
||||
_profiler_init()
|
||||
|
||||
@@ -215,8 +118,6 @@ class Profiler:
|
||||
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
|
||||
@@ -229,8 +130,6 @@ class Profiler:
|
||||
and not libpyomnitrace.is_finalized()
|
||||
)
|
||||
|
||||
# ---------------------------------------------------------------------------------- #
|
||||
#
|
||||
def start(self):
|
||||
"""Start the profiler explicitly"""
|
||||
|
||||
@@ -247,8 +146,6 @@ class Profiler:
|
||||
self._unset = self._unset + 1
|
||||
return self._unset
|
||||
|
||||
# ---------------------------------------------------------------------------------- #
|
||||
#
|
||||
def stop(self):
|
||||
"""Stop the profiler explicitly"""
|
||||
|
||||
@@ -263,8 +160,6 @@ class Profiler:
|
||||
|
||||
return self._unset
|
||||
|
||||
# ---------------------------------------------------------------------------------- #
|
||||
#
|
||||
def __call__(self, func):
|
||||
"""Decorator"""
|
||||
|
||||
@@ -281,15 +176,11 @@ class Profiler:
|
||||
|
||||
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"""
|
||||
|
||||
@@ -300,8 +191,6 @@ class Profiler:
|
||||
|
||||
traceback.print_exception(exec_type, exec_value, exec_tb, limit=5)
|
||||
|
||||
# ---------------------------------------------------------------------------------- #
|
||||
#
|
||||
def run(self, cmd):
|
||||
"""Execute and profile a command"""
|
||||
|
||||
@@ -313,8 +202,6 @@ class Profiler:
|
||||
else:
|
||||
return self.runctx(" ".join(cmd), dict, dict)
|
||||
|
||||
# ---------------------------------------------------------------------------------- #
|
||||
#
|
||||
def runctx(self, cmd, globals, locals):
|
||||
"""Profile a context"""
|
||||
|
||||
@@ -326,8 +213,6 @@ class Profiler:
|
||||
|
||||
return self
|
||||
|
||||
# ---------------------------------------------------------------------------------- #
|
||||
#
|
||||
def runcall(self, func, *args, **kw):
|
||||
"""Profile a single function call"""
|
||||
|
||||
@@ -344,26 +229,18 @@ 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"""
|
||||
|
||||
@@ -373,14 +250,10 @@ class FakeProfiler:
|
||||
|
||||
return function_wrapper
|
||||
|
||||
# ---------------------------------------------------------------------------------- #
|
||||
#
|
||||
def __enter__(self, *args, **kwargs):
|
||||
"""Context manager begin"""
|
||||
pass
|
||||
|
||||
# ---------------------------------------------------------------------------------- #
|
||||
#
|
||||
def __exit__(self, exec_type, exec_value, exec_tb):
|
||||
"""Context manager end"""
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user