Python noprofile (#138)
* Fixed noprofile / FakeProfiler
- omnitrace.libpyomnitrace.profiler.profiler_pause()
- omnitrace.libpyomnitrace.profiler.profiler_resume()
* Python tests for noprofile
* Remove static imported module
[ROCm/rocprofiler-systems commit: 3f3ef7ddf9]
This commit is contained in:
committed by
GitHub
orang tua
4bef976f3f
melakukan
e40f11b1fc
@@ -187,6 +187,13 @@ strset_t default_exclude_functions = { "^<.*>$" };
|
||||
strset_t default_exclude_filenames = { "(encoder|decoder|threading).py$", "^<.*>$" };
|
||||
} // namespace
|
||||
//
|
||||
auto&
|
||||
get_paused()
|
||||
{
|
||||
static thread_local int64_t _v = 0;
|
||||
return _v;
|
||||
}
|
||||
//
|
||||
struct config
|
||||
{
|
||||
bool is_running = false;
|
||||
@@ -251,6 +258,8 @@ get_depth(PyFrameObject* frame)
|
||||
void
|
||||
profiler_function(py::object pframe, const char* swhat, py::object arg)
|
||||
{
|
||||
if(get_paused() > 0) return;
|
||||
|
||||
static thread_local auto& _config = get_config();
|
||||
static thread_local auto _disable = false;
|
||||
|
||||
@@ -504,9 +513,24 @@ generate(py::module& _pymod)
|
||||
get_config().records.clear();
|
||||
};
|
||||
|
||||
auto _sys = py::module::import("sys");
|
||||
auto _setprofile = _sys.attr("setprofile");
|
||||
|
||||
_prof.def("profiler_function", &profiler_function, "Profiling function");
|
||||
_prof.def("profiler_init", _init, "Initialize the profiler");
|
||||
_prof.def("profiler_finalize", _fini, "Finalize the profiler");
|
||||
_prof.def(
|
||||
"profiler_pause",
|
||||
[_setprofile]() {
|
||||
if(++get_paused() == 1) _setprofile(nullptr);
|
||||
},
|
||||
"Pause the profiler");
|
||||
_prof.def(
|
||||
"profiler_resume",
|
||||
[_setprofile]() {
|
||||
if(--get_paused() == 0) _setprofile(py::cpp_function{ profiler_function });
|
||||
},
|
||||
"Resume the profiler");
|
||||
|
||||
py::class_<config> _pyconfig(_prof, "config", "Profiler configuration");
|
||||
|
||||
|
||||
@@ -46,8 +46,17 @@ from .libpyomnitrace.profiler import (
|
||||
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
|
||||
from .libpyomnitrace.profiler import profiler_pause as _profiler_pause
|
||||
from .libpyomnitrace.profiler import profiler_resume as _profiler_resume
|
||||
|
||||
__all__ = ["profile", "config", "Profiler", "FakeProfiler", "Config"]
|
||||
__all__ = [
|
||||
"profile",
|
||||
"noprofile",
|
||||
"config",
|
||||
"Profiler",
|
||||
"FakeProfiler",
|
||||
"Config",
|
||||
]
|
||||
|
||||
|
||||
config = _profiler_config
|
||||
@@ -227,7 +236,7 @@ profile = Profiler
|
||||
|
||||
|
||||
class FakeProfiler:
|
||||
"""Provides dummy decorators and context-manager for the omnitrace profiler"""
|
||||
"""Provides decorators and context-manager for disabling the omnitrace profiler"""
|
||||
|
||||
@staticmethod
|
||||
def condition(functor):
|
||||
@@ -238,7 +247,6 @@ class FakeProfiler:
|
||||
return False
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
""" """
|
||||
pass
|
||||
|
||||
def __call__(self, func):
|
||||
@@ -246,18 +254,25 @@ class FakeProfiler:
|
||||
|
||||
@wraps(func)
|
||||
def function_wrapper(*args, **kwargs):
|
||||
return func(*args, **kwargs)
|
||||
_profiler_pause()
|
||||
ret = func(*args, **kwargs)
|
||||
_profiler_resume()
|
||||
return ret
|
||||
|
||||
return function_wrapper
|
||||
|
||||
def __enter__(self, *args, **kwargs):
|
||||
"""Context manager begin"""
|
||||
pass
|
||||
_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
|
||||
|
||||
@@ -7,6 +7,6 @@ requires = [
|
||||
build-backend = 'setuptools.build_meta'
|
||||
|
||||
[tool.black]
|
||||
line-length = 80
|
||||
line-length = 90
|
||||
target-version = ['py38']
|
||||
include = '\.py'
|
||||
|
||||
Reference in New Issue
Block a user