Files
TheRock/rocm-systems/projects/rocprofiler-systems/source/python/rocprofsys/user.py
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

122 lines
3.1 KiB
Python

#!/usr/bin/env python@_VERSION@
# Copyright (c) Advanced Micro Devices, Inc.
# SPDX-License-Identifier: MIT
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