* 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
Этот коммит содержится в:
Jonathan R. Madsen
2022-04-05 00:24:34 -05:00
коммит произвёл GitHub
родитель 945f541965
Коммит afa3edebab
42 изменённых файлов: 2442 добавлений и 239 удалений
+129
Просмотреть файл
@@ -0,0 +1,129 @@
# ########################################################################################
#
# omnitrace (Python)
#
# ########################################################################################
# if set, will screw up loading library
unset(CMAKE_DEBUG_POSTFIX)
set(CMAKE_CXX_CLANG_TIDY)
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
set(CMAKE_INSTALL_DEFAULT_COMPONENT_NAME python)
# ########################################################################################
function(OMNITRACE_CONFIGURE_PYTARGET _TARGET)
add_library(omnitrace::${_TARGET} ALIAS ${_TARGET})
target_link_libraries(${_TARGET} PRIVATE libpyomnitrace-interface)
set(_SUBDIR ${ARGN})
if(_SUBDIR)
set(_SUBDIR "/${_SUBDIR}")
endif()
set_target_properties(
${_TARGET}
PROPERTIES PREFIX ""
SUFFIX "${PYTHON_MODULE_EXTENSION}"
LIBRARY_OUTPUT_DIRECTORY
${PROJECT_BINARY_DIR}/python/omnitrace${_SUBDIR}
ARCHIVE_OUTPUT_DIRECTORY
${PROJECT_BINARY_DIR}/python/omnitrace${_SUBDIR}
RUNTIME_OUTPUT_DIRECTORY
${PROJECT_BINARY_DIR}/python/omnitrace${_SUBDIR}
PDB_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/python/omnitrace${_SUBDIR}
INSTALL_RPATH_USE_LINK_PATH ON
${EXTRA_PROPERTIES})
set(_PYLIB ${CMAKE_INSTALL_PYTHONDIR}/omnitrace${_SUBDIR})
if(NOT IS_ABSOLUTE "${_PYLIB}")
set(_PYLIB ${CMAKE_INSTALL_PREFIX}/${_PYLIB})
endif()
if(SKBUILD)
set(LIB_RELPATH ../../../..)
else()
file(RELATIVE_PATH LIB_RELPATH "${_PYLIB}"
"${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
endif()
set_target_properties(
${_TARGET}
PROPERTIES
INSTALL_RPATH
"\$ORIGIN:\$ORIGIN/${LIB_RELPATH}:\$ORIGIN/../../../..:${CMAKE_INSTALL_RPATH}"
)
install(
TARGETS ${_TARGET}
DESTINATION ${CMAKE_INSTALL_PYTHONDIR}/omnitrace${_SUBDIR}
OPTIONAL)
if(NOT "${_TARGET}" STREQUAL "libpyomnitrace")
add_dependencies(libpyomnitrace ${_TARGET})
endif()
endfunction()
# ########################################################################################
add_library(omnitrace-python-compile-options INTERFACE)
add_library(omnitrace::omnitrace-python-compile-options ALIAS
omnitrace-python-compile-options)
add_cxx_flag_if_avail("-frtti" omnitrace-python-compile-options)
add_cxx_flag_if_avail("-Wno-unused-value" omnitrace-python-compile-options)
add_cxx_flag_if_avail("-Wno-range-loop-analysis" omnitrace-python-compile-options)
add_cxx_flag_if_avail("-ftls-model=global-dynamic" omnitrace-python-compile-options)
add_cxx_flag_if_avail("-Wno-deprecated-declarations" omnitrace-python-compile-options)
add_cxx_flag_if_avail("-Wno-unused-but-set-parameter" omnitrace-python-compile-options)
file(GLOB pyheaders ${CMAKE_CURRENT_LIST_DIR}/libpyomnitrace*.hpp)
set(pysources ${CMAKE_CURRENT_LIST_DIR}/libpyomnitrace.cpp)
set(pybind_libs pybind11::module)
add_library(libpyomnitrace-interface INTERFACE)
target_link_libraries(
libpyomnitrace-interface
INTERFACE pybind11::module
timemory::timemory-headers
omnitrace::omnitrace-headers
omnitrace::omnitrace-compile-options
omnitrace::omnitrace-lto
omnitrace::omnitrace-dl-library
omnitrace::omnitrace-python
omnitrace::omnitrace-python-compile-options)
target_compile_definitions(libpyomnitrace-interface INTERFACE OMNITRACE_PYBIND11_SOURCE)
add_library(libpyomnitrace MODULE ${pysources} ${pyheaders})
omnitrace_configure_pytarget(libpyomnitrace)
add_subdirectory(omnitrace)
if(PYTHON_EXECUTABLE)
configure_file(${CMAKE_CURRENT_LIST_DIR}/setup.py.in
${PROJECT_BINARY_DIR}/python/setup.py @ONLY)
configure_file(${CMAKE_CURRENT_LIST_DIR}/setup.cfg.in
${PROJECT_BINARY_DIR}/python/setup.cfg @ONLY)
configure_file(${CMAKE_CURRENT_LIST_DIR}/pyproject.toml
${PROJECT_BINARY_DIR}/python/pyproject.toml COPYONLY)
execute_process(
COMMAND ${PYTHON_EXECUTABLE} setup.py dist_info
OUTPUT_VARIABLE _OUT
RESULT_VARIABLE _RET
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/python)
set(_DIST_DIR
${PROJECT_BINARY_DIR}/python/${PROJECT_NAME}-${PROJECT_VERSION}.dist-info)
if(NOT EXISTS ${_DIST_DIR})
set(_DIST_DIR ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.dist-info)
endif()
if(EXISTS ${_DIST_DIR} AND IS_DIRECTORY ${_DIST_DIR})
configure_file(${PROJECT_SOURCE_DIR}/LICENSE ${_DIST_DIR}/LICENSE.txt COPYONLY)
install(
DIRECTORY ${_DIST_DIR}
DESTINATION ${CMAKE_INSTALL_PYTHONDIR}
OPTIONAL)
endif()
endif()
+449
Просмотреть файл
@@ -0,0 +1,449 @@
# Python configuration
#
# include guard
include_guard(DIRECTORY)
# Stops lookup as soon as a version satisfying version constraints is found.
set(Python3_FIND_STRATEGY
"LOCATION"
CACHE STRING
"Stops lookup as soon as a version satisfying version constraints is found")
# virtual environment is used before any other standard paths to look-up for the
# interpreter
set(Python3_FIND_VIRTUALENV
"FIRST"
CACHE STRING "Virtual environment is used before any other standard paths")
set_property(CACHE Python3_FIND_VIRTUALENV PROPERTY STRINGS "FIRST;LAST;NEVER")
if(APPLE)
set(Python3_FIND_FRAMEWORK
"LAST"
CACHE STRING
"Order of preference between Apple-style and unix-style package components")
set_property(CACHE Python3_FIND_FRAMEWORK PROPERTY STRINGS "FIRST;LAST;NEVER")
endif()
# PyPy does not support embedding the interpreter
set(Python3_FIND_IMPLEMENTATIONS
"CPython"
CACHE STRING "Different implementations which will be searched.")
set_property(CACHE Python3_FIND_IMPLEMENTATIONS PROPERTY STRINGS
"CPython;IronPython;PyPy")
# variable is a 3-tuple specifying, in order, pydebug (d), pymalloc (m) and unicode (u)
# set(Python3_FIND_ABI "OFF" "OFF" "OFF" CACHE STRING "variable is a 3-tuple specifying
# pydebug (d), pymalloc (m) and unicode (u)")
# Create CMake cache entries for the above artifact specification variables so that users
# can edit them interactively. This disables support for multiple version/component
# requirements.
set(Python3_ARTIFACTS_INTERACTIVE
ON
CACHE BOOL "Create CMake cache entries so that users can edit them interactively")
# if("${Python3_USE_STATIC_LIBS}" STREQUAL "ANY") set(Python3_USE_STATIC_LIBS "OFF" CACHE
# STRING "If ON, only static libs; if OFF, only shared libs; if ANY, shared then static")
# set_property(CACHE Python3_USE_STATIC_LIBS PROPERTY STRINGS "ON;OFF;ANY") else()
# unset(Python3_USE_STATIC_LIBS) endif()
foreach(_VAR FIND_STRATEGY FIND_VIRTUALENV FIND_FRAMEWORK FIND_IMPLEMENTATIONS
ARTIFACTS_INTERACTIVE)
if(DEFINED Python3_${_VAR})
set(Python_${_VAR}
"${Python3_${_VAR}}"
CACHE STRING "Set via Python3_${_VAR} setting (omnitrace)")
mark_as_advanced(Python_${_VAR})
mark_as_advanced(Python3_${_VAR})
endif()
endforeach()
# display version
omnitrace_add_feature(OMNITRACE_PYTHON_VERSION "Python version for omnitrace" DOC)
# search hint
if(PYTHON_ROOT_DIR AND NOT Python3_ROOT_DIR)
set(Python3_ROOT_DIR ${PYTHON_ROOT_DIR})
endif()
# legacy specification of interpreter
if(PYTHON_EXECUTABLE AND NOT Python3_EXECUTABLE)
set(Python3_EXECUTABLE
"${PYTHON_EXECUTABLE}"
CACHE FILEPATH "Path to Python3 interpreter")
endif()
# default python types to search for
set(Python_ADDITIONAL_VERSIONS
"3.9;3.8;3.7;3.6"
CACHE STRING "Python versions supported by omnitrace")
# override types to search for
if(OMNITRACE_PYTHON_VERSION)
set(Python_ADDITIONAL_VERSIONS
${OMNITRACE_PYTHON_VERSION}
CACHE STRING "Python versions supported by omnitrace" FORCE)
elseif(PYBIND11_PYTHON_VERSION)
set(Python_ADDITIONAL_VERSIONS
${PYBIND11_PYTHON_VERSION}
CACHE STRING "Python versions supported by omnitrace")
endif()
# unset the version strings
if(_PYVERSION_LAST AND (OMNITRACE_PYTHON_VERSION VERSION_LESS _PYVERSION_LAST
OR OMNITRACE_PYTHON_VERSION VERSION_GREATER _PYVERSION_LAST))
unset(OMNITRACE_PYTHON_VERSION CACHE)
unset(PYBIND11_PYTHON_VERSION CACHE)
unset(CMAKE_INSTALL_PYTHONDIR CACHE)
endif()
# if OMNITRACE_PYTHON_VERSION specified, set to desired python version
set(_PYVERSION ${OMNITRACE_PYTHON_VERSION})
# if OMNITRACE_PYTHON_VERSION is not set but PYBIND11_PYTHON_VERSION is
if("${_PYVERSION}" STREQUAL "" AND PYBIND11_PYTHON_VERSION)
set(_PYVERSION ${PYBIND11_PYTHON_VERSION})
endif()
# basically just used to get Python3_SITEARCH for installation
find_package(Python3 ${_PYVERSION} MODULE ${OMNITRACE_FIND_REQUIREMENT}
COMPONENTS Interpreter Development)
# executable
set(PYTHON_EXECUTABLE
"${Python3_EXECUTABLE}"
CACHE FILEPATH "Set via Python3_EXECUTABLE (omnitrace)" FORCE)
# includes
if(Python3_INCLUDE_DIR AND NOT Python3_INCLUDE_DIRS)
set(Python3_INCLUDE_DIRS ${Python3_INCLUDE_DIR})
endif()
if(Python3_INCLUDE_DIRS)
set(PYTHON_INCLUDE_DIR
"${Python3_INCLUDE_DIRS}"
CACHE PATH "Set via Python3_INCLUDE_DIR (omnitrace)" FORCE)
set(PYTHON_INCLUDE_DIRS
"${Python3_INCLUDE_DIRS}"
CACHE PATH "Set via Python3_INCLUDE_DIRS (omnitrace)" FORCE)
endif()
# libraries
set(PYTHON_LIBRARY_DEBUG
"${Python3_LIBRARY_DEBUG}"
CACHE FILEPATH "Set via Python3_LIBRARY_DEBUG (omnitrace)" FORCE)
set(PYTHON_LIBRARY_RELEASE
"${Python3_LIBRARY_RELEASE}"
CACHE FILEPATH "Set via Python3_LIBRARY_DEBUG (omnitrace)" FORCE)
if(Python3_LIBRARY_RELEASE)
set(PYTHON_LIBRARY
"${Python3_LIBRARY_RELEASE}"
CACHE FILEPATH "Set via Python3_LIBRARY (omnitrace)" FORCE)
set(PYTHON_LIBRARIES
"${Python3_LIBRARY_RELEASE}"
CACHE FILEPATH "Set via Python3_LIBRARIES (omnitrace)" FORCE)
else(Python3_LIBRARY_DEBUG)
set(PYTHON_LIBRARY
"${Python3_LIBRARY_DEBUG}"
CACHE FILEPATH "Set via Python3_LIBRARY (omnitrace)" FORCE)
set(PYTHON_LIBRARIES
"${Python3_LIBRARY_DEBUG}"
CACHE FILEPATH "Set via Python3_LIBRARIES (omnitrace)" FORCE)
endif()
set(PYTHON_LIBRARY_DIRS
"${Python3_LIBRARY_DIRS}"
CACHE PATH "Set via Python3_LIBRARY_DIRS (omnitrace)" FORCE)
set(PYTHON_LINK_OPTIONS
"${Python3_LINK_OPTIONS}"
CACHE STRING "Set via Python3_LINK_OPTIONS (omnitrace)" FORCE)
# module
set(PYTHON_MODULE_EXTENSION
"${Python3_MODULE_EXTENSION}"
CACHE STRING "Set via Python3_MODULE_EXTENSION (omnitrace)" FORCE)
set(PYTHON_MODULE_PREFIX
"${Python3_MODULE_PREFIX}"
CACHE STRING "Set via Python3_MODULE_PREFIX (omnitrace)" FORCE)
# version
set(PYTHON_VERSION
"${Python3_VERSION}"
CACHE STRING "Set via Python3_VERSION (omnitrace)" FORCE)
set(PYTHON_VERSION_MAJOR
"${Python3_VERSION_MAJOR}"
CACHE STRING "Set via Python3_VERSION_MAJOR (omnitrace)" FORCE)
set(PYTHON_VERSION_MINOR
"${Python3_VERSION_MINOR}"
CACHE STRING "Set via Python3_VERSION_MINOR (omnitrace)" FORCE)
# find_package
set(PythonInterp_FOUND ${Python3_Interpreter_FOUND})
set(PythonLibs_FOUND ${Python3_Development_FOUND})
# set OMNITRACE_PYTHON_VERSION if we have the python version
if(PYTHON_VERSION_STRING)
set(OMNITRACE_PYTHON_VERSION
"${PYTHON_VERSION_STRING}"
CACHE STRING "Python version for omnitrace")
endif()
# if either not found, disable
if(NOT Python3_FOUND)
set(OMNITRACE_USE_PYTHON OFF)
set(OMNITRACE_BUILD_PYTHON OFF)
omnitrace_inform_empty_interface(omnitrace-python "Python embedded interpreter")
omnitrace_inform_empty_interface(omnitrace-plotting "Python plotting from C++")
else()
set(OMNITRACE_PYTHON_VERSION
"${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}"
CACHE STRING "Python version for omnitrace")
omnitrace_add_feature(PYTHON_EXECUTABLE "Python executable")
endif()
# C++ standard
if(NOT MSVC)
if(NOT "${PYBIND11_CPP_STANDARD}" STREQUAL "-std=c++${CMAKE_CXX_STANDARD}")
set(PYBIND11_CPP_STANDARD
-std=c++${CMAKE_CXX_STANDARD}
CACHE STRING "PyBind11 CXX standard" FORCE)
endif()
else()
if(NOT "${PYBIND11_CPP_STANDARD}" STREQUAL "/std:c++${CMAKE_CXX_STANDARD}")
set(PYBIND11_CPP_STANDARD
/std:c++${CMAKE_CXX_STANDARD}
CACHE STRING "PyBind11 CXX standard" FORCE)
endif()
endif()
option(PYBIND11_INSTALL "Enable Pybind11 installation" OFF)
if(OMNITRACE_BUILD_PYTHON AND NOT TARGET pybind11)
# checkout PyBind11 if not checked out
omnitrace_checkout_git_submodule(
RECURSIVE
RELATIVE_PATH external/pybind11
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/external/timemory
REPO_URL https://github.com/jrmadsen/pybind11.git
REPO_BRANCH master)
# add PyBind11 to project omnitrace_save_variables(IPO VARIABLES
# CMAKE_INTERPROCEDURAL_OPTIMIZATION)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)
add_subdirectory(${PROJECT_SOURCE_DIR}/external/timemory/external/pybind11)
# omnitrace_restore_variables(IPO VARIABLES CMAKE_INTERPROCEDURAL_OPTIMIZATION)
endif()
if(NOT PYBIND11_PYTHON_VERSION)
unset(PYBIND11_PYTHON_VERSION CACHE)
execute_process(
COMMAND
${PYTHON_EXECUTABLE} -c
"import sys; print('{}.{}'.format(sys.version_info[0], sys.version_info[1]))"
OUTPUT_VARIABLE PYTHON_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
set(PYBIND11_PYTHON_VERSION
"${PYTHON_VERSION}"
CACHE STRING "Python version")
endif()
omnitrace_add_feature(PYBIND11_CPP_STANDARD "PyBind11 C++ standard")
omnitrace_add_feature(PYBIND11_PYTHON_VERSION "PyBind11 Python version")
if(NOT "${OMNITRACE_PYTHON_VERSION}" MATCHES "${PYBIND11_PYTHON_VERSION}*")
message(STATUS "OMNITRACE_PYTHON_VERSION is set to ${OMNITRACE_PYTHON_VERSION}")
message(STATUS "PYBIND11_PYTHON_VERSION is set to ${PYBIND11_PYTHON_VERSION}")
message(
FATAL_ERROR "Mismatched 'OMNITRACE_PYTHON_VERSION' and 'PYBIND11_PYTHON_VERSION'")
endif()
execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c
"import time ; print('{} {}'.format(time.ctime(), time.tzname[0]))"
OUTPUT_VARIABLE OMNITRACE_INSTALL_DATE
OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
string(REPLACE " " " " OMNITRACE_INSTALL_DATE "${OMNITRACE_INSTALL_DATE}")
if(SKBUILD)
set(OMNITRACE_INSTALL_PYTHON
"prefix"
CACHE STRING "SKBUILD forced python install type" FORCE)
else()
set(OMNITRACE_INSTALL_PYTHON
"lib"
CACHE STRING "Installation type for python (prefix, lib, or global)")
endif()
omnitrace_add_feature(OMNITRACE_INSTALL_PYTHON
"Installation type for python (prefix, lib, or global)")
if(SKBUILD OR "${OMNITRACE_INSTALL_PYTHON}" STREQUAL "prefix")
set(CMAKE_INSTALL_PYTHONDIR
${CMAKE_INSTALL_PREFIX}
CACHE PATH "Installation directory for python")
elseif(SPACK_BUILD OR "${OMNITRACE_INSTALL_PYTHON}" STREQUAL "lib")
set(CMAKE_INSTALL_PYTHONDIR
lib/python${PYBIND11_PYTHON_VERSION}/site-packages
CACHE PATH "Installation directory for python")
else()
string(REPLACE "\\" "/" Python3_SITEARCH "${Python3_SITEARCH}")
set(CMAKE_INSTALL_PYTHONDIR ${Python3_SITEARCH})
omnitrace_add_feature(Python3_SITEARCH
"site-packages directory of python installation")
set(_REMOVE OFF)
# make the directory if it doesn't exist
if(NOT EXISTS ${Python3_SITEARCH}/omnitrace)
set(_REMOVE ON)
execute_process(
COMMAND ${CMAKE_COMMAND} -E make_directory ${Python3_SITEARCH}/omnitrace
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
ERROR_QUIET)
endif()
# figure out if we can install to Python3_SITEARCH
execute_process(
COMMAND ${CMAKE_COMMAND} -E touch ${Python3_SITEARCH}/omnitrace/__init__.py
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
ERROR_VARIABLE ERR_MSG
RESULT_VARIABLE ERR_CODE)
# remove the directory if we created it
if(_REMOVE)
execute_process(
COMMAND ${CMAKE_COMMAND} -E remove_directory ${Python3_SITEARCH}/omnitrace
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
ERROR_QUIET)
endif()
# check the error code of the touch command
if(ERR_CODE)
if("${OMNITRACE_INSTALL_PYTHON}" STREQUAL "global")
message(
FATAL_ERROR
"omnitrace could not install python files to ${Python3_SITEARCH} (not writable):\n${ERR_MSG}"
)
endif()
# get the python directory name, e.g. 'python3.6' from
# '/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6'
get_filename_component(PYDIR "${Python3_STDLIB}" NAME)
omnitrace_add_feature(Python3_STDLIB
"standard-library directory of python installation")
# Should not be CMAKE_INSTALL_LIBDIR! Python won't look in a lib64 folder
set(CMAKE_INSTALL_PYTHONDIR lib/${PYDIR}/site-packages)
endif()
endif()
if(OMNITRACE_BUILD_PYTHON OR pybind11_FOUND)
set(_PYBIND11_INCLUDE_DIRS)
foreach(_TARG pybind11 pybind11::pybind11 pybind11::module)
if(TARGET ${_TARG})
get_target_property(_INCLUDE_DIR ${_TARG} INTERFACE_INCLUDE_DIRECTORIES)
if(_INCLUDE_DIR)
list(APPEND _PYBIND11_INCLUDE_DIRS ${_INCLUDE_DIR})
endif()
endif()
endforeach()
if(_PYBIND11_INCLUDE_DIRS)
list(REMOVE_DUPLICATES _PYBIND11_INCLUDE_DIRS)
endif()
omnitrace_target_compile_definitions(omnitrace-python INTERFACE OMNITRACE_USE_PYTHON)
if(NOT APPLE)
target_link_libraries(omnitrace-python INTERFACE ${PYTHON_LIBRARIES})
endif()
if(PYTHON_INCLUDE_DIRS)
target_include_directories(omnitrace-python SYSTEM
INTERFACE ${PYTHON_INCLUDE_DIRS})
endif()
if(PYBIND11_INCLUDE_DIRS)
target_include_directories(omnitrace-python SYSTEM
INTERFACE ${PYBIND11_INCLUDE_DIRS})
endif()
if(PYBIND11_INCLUDE_DIR)
target_include_directories(omnitrace-python SYSTEM
INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>)
endif()
if(_PYBIND11_INCLUDE_DIRS)
target_include_directories(omnitrace-python SYSTEM
INTERFACE $<BUILD_INTERFACE:${_PYBIND11_INCLUDE_DIRS}>)
endif()
endif()
if(APPLE)
if(CMAKE_VERSION VERSION_LESS 3.18)
target_link_libraries(omnitrace-python INTERFACE "-undefined dynamic_lookup")
else()
target_link_libraries(
omnitrace-python
INTERFACE "$<$<LINK_LANGUAGE:CXX>:-undefined dynamic_lookup>")
endif()
endif()
if(WIN32)
# Windows produces:
#
# CMake Warning (dev) at tests/test-python-install-import.cmake:3 (SET): Syntax error
# in cmake code at
# C:/projects/omnitrace/build-omnitrace/tests/test-python-install-import.cmake:3 when
# parsing string C:\Python36-x64\Lib\site-packages Invalid escape sequence \P
string(REPLACE "\\" "/" INSTALL_PYTHONDIR "${CMAKE_INSTALL_PYTHONDIR}")
else()
set(INSTALL_PYTHONDIR "${CMAKE_INSTALL_PYTHONDIR}")
endif()
configure_file(
${PROJECT_SOURCE_DIR}/external/timemory/cmake/Templates/test-python-install-import.cmake.in
${PROJECT_BINARY_DIR}/tests/test-python-install-import.cmake
@ONLY)
unset(INSTALL_PYTHONDIR)
omnitrace_add_feature(CMAKE_INSTALL_PYTHONDIR
"Installation prefix of the python bindings")
set(_PYVERSION_LAST
"${OMNITRACE_PYTHON_VERSION}"
CACHE INTERNAL "Last version" FORCE)
find_package(PythonInterp ${OMNITRACE_PYTHON_VERSION} EXACT REQUIRED)
find_package(PythonLibs ${OMNITRACE_PYTHON_VERSION} EXACT REQUIRED)
# find_package(PythonExtensions REQUIRED)
if("${PYTHON_MODULE_EXTENSION}" STREQUAL "")
execute_process(
COMMAND
"${Python3_EXECUTABLE}" "-c" "
from distutils import sysconfig as s;import sys;import struct;
print('.'.join(str(v) for v in sys.version_info));
print(sys.prefix);
print(s.get_python_inc(plat_specific=True));
print(s.get_python_lib(plat_specific=True));
print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'));
print(hasattr(sys, 'gettotalrefcount')+0);
print(struct.calcsize('@P'));
print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
print(s.get_config_var('LIBDIR') or '');
print(s.get_config_var('MULTIARCH') or '');
"
RESULT_VARIABLE _PYTHON_SUCCESS
OUTPUT_VARIABLE _PYTHON_VALUES
ERROR_VARIABLE _PYTHON_ERROR_VALUE)
if(_PYTHON_SUCCESS MATCHES 0)
# Convert the process output into a list
if(WIN32)
string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES})
endif()
string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
list(GET _PYTHON_VALUES 1 PYTHON_PREFIX)
list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG)
list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
list(GET _PYTHON_VALUES 8 PYTHON_LIBDIR)
list(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH)
else()
message(WARNING "${_PYTHON_ERROR_VALUE}")
endif()
if("${PYTHON_MODULE_EXTENSION}" STREQUAL "")
message(WARNING "Python module extension is empty!")
endif()
endif()
+447
Просмотреть файл
@@ -0,0 +1,447 @@
// 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.
#include "libpyomnitrace.hpp"
#include "dl.hpp"
#include <timemory/backends/process.hpp>
#include <timemory/backends/threading.hpp>
#include <timemory/mpl/apply.hpp>
#include <timemory/utility/macros.hpp>
#include <timemory/variadic/macros.hpp>
#include <cctype>
#include <cstdint>
#include <locale>
#include <regex>
#include <set>
#include <unordered_map>
namespace pyomnitrace
{
namespace pyprofile
{
py::module
generate(py::module& _pymod);
}
} // namespace pyomnitrace
PYBIND11_MODULE(libpyomnitrace, omni)
{
using namespace pyomnitrace;
py::doc("omnitrace profiler for python");
pyprofile::generate(omni);
omni.def(
"initialize",
[](const std::string& _v) {
omnitrace_set_mpi(false, false);
omnitrace_init("trace", false, _v.c_str());
},
"Initialize omnitrace");
omni.def(
"finalize", []() { omnitrace_finalize(); }, "Initialize omnitrace");
}
//======================================================================================//
//
namespace pyomnitrace
{
namespace pyprofile
{
//
using profiler_t = std::pair<std::function<void()>, std::function<void()>>;
using profiler_vec_t = std::vector<profiler_t>;
using profiler_label_map_t = std::unordered_map<std::string, profiler_vec_t>;
using profiler_index_map_t = std::unordered_map<uint32_t, profiler_label_map_t>;
using strset_t = std::unordered_set<std::string>;
//
struct config
{
bool is_running = false;
bool trace_c = false;
bool include_internal = false;
bool include_args = false;
bool include_line = false;
bool include_filename = false;
bool full_filepath = false;
int32_t ignore_stack_depth = 0;
int32_t base_stack_depth = -1;
std::string base_module_path = {};
strset_t include_functions = {};
strset_t include_filenames = {};
strset_t exclude_functions = { "^(FILE|FUNC|LINE)$",
"^get_fcode$",
"^_(_exit__|handle_fromlist|shutdown|get_sep)$",
"^is(function|class)$",
"^basename$",
"^<.*>$" };
strset_t exclude_filenames = {
"(__init__|__main__|functools|encoder|decoder|_pylab_helpers|threading).py$",
"^<.*>$"
};
profiler_index_map_t records = {};
int32_t verbose = 0;
};
//
inline config&
get_config()
{
static auto* _instance = new config{};
static thread_local auto* _tl_instance = []() {
static std::atomic<uint32_t> _count{ 0 };
auto _cnt = _count++;
if(_cnt == 0) return _instance;
auto* _tmp = new config{};
_tmp->is_running = _instance->is_running;
_tmp->trace_c = _instance->trace_c;
_tmp->include_internal = _instance->include_internal;
_tmp->include_args = _instance->include_args;
_tmp->include_line = _instance->include_line;
_tmp->include_filename = _instance->include_filename;
_tmp->full_filepath = _instance->full_filepath;
_tmp->base_module_path = _instance->base_module_path;
_tmp->include_functions = _instance->include_functions;
_tmp->include_filenames = _instance->include_filenames;
_tmp->exclude_functions = _instance->exclude_functions;
_tmp->exclude_filenames = _instance->exclude_filenames;
_tmp->verbose = _instance->verbose;
return _tmp;
}();
return *_tl_instance;
}
//
int32_t
get_depth(PyFrameObject* frame)
{
return (frame->f_back) ? (get_depth(frame->f_back) + 1) : 0;
}
//
void
profiler_function(py::object pframe, const char* swhat, py::object arg)
{
static thread_local auto& _config = get_config();
static thread_local auto _disable = false;
if(_disable) return;
_disable = true;
tim::scope::destructor _dtor{ []() { _disable= false; } };
(void) _dtor;
if(pframe.is_none() || pframe.ptr() == nullptr) return;
static auto _omnitrace_path = _config.base_module_path;
auto* frame = reinterpret_cast<PyFrameObject*>(pframe.ptr());
int what = (strcmp(swhat, "call") == 0) ? PyTrace_CALL
: (strcmp(swhat, "c_call") == 0) ? PyTrace_C_CALL
: (strcmp(swhat, "return") == 0) ? PyTrace_RETURN
: (strcmp(swhat, "c_return") == 0) ? PyTrace_C_RETURN
: -1;
// only support PyTrace_{CALL,C_CALL,RETURN,C_RETURN}
if(what < 0)
{
if(_config.verbose > 2)
TIMEMORY_PRINT_HERE("%s :: %s",
"Ignoring what != {CALL,C_CALL,RETURN,C_RETURN}", swhat);
return;
}
// if PyTrace_C_{CALL,RETURN} is not enabled
if(!_config.trace_c && (what == PyTrace_C_CALL || what == PyTrace_C_RETURN))
{
if(_config.verbose > 2)
TIMEMORY_PRINT_HERE("%s :: %s", "Ignoring C call/return", swhat);
return;
}
// get the function name
auto _get_funcname = [&]() -> std::string {
return py::cast<std::string>(frame->f_code->co_name);
};
// get the filename
auto _get_filename = [&]() -> std::string {
return py::cast<std::string>(frame->f_code->co_filename);
};
// get the basename of the filename
auto _get_basename = [&](const std::string& _fullpath) {
if(_fullpath.find('/') != std::string::npos)
return _fullpath.substr(_fullpath.find_last_of('/') + 1);
return _fullpath;
};
// get the arguments
auto _get_args = [&]() {
auto inspect = py::module::import("inspect");
try
{
return py::cast<std::string>(
inspect.attr("formatargvalues")(*inspect.attr("getargvalues")(pframe)));
} catch(py::error_already_set& _exc)
{
TIMEMORY_CONDITIONAL_PRINT_HERE(_config.verbose > 1, "Error! %s",
_exc.what());
if(!_exc.matches(PyExc_AttributeError)) throw;
}
return std::string{};
};
// get the final label
auto _get_label = [&](auto& _func, auto& _filename, auto& _fullpath) {
auto _bracket = _config.include_filename;
if(_bracket) _func.insert(0, "[");
// append the arguments
if(_config.include_args) _func.append(_get_args());
if(_bracket) _func.append("]");
// append the filename
if(_config.include_filename)
{
if(_config.full_filepath)
_func.append(TIMEMORY_JOIN("", '[', std::move(_fullpath)));
else
_func.append(TIMEMORY_JOIN("", '[', std::move(_filename)));
}
// append the line number
if(_config.include_line && _config.include_filename)
_func.append(TIMEMORY_JOIN("", ':', frame->f_lineno, ']'));
else if(_config.include_line)
_func.append(TIMEMORY_JOIN("", ':', frame->f_lineno));
else if(_config.include_filename)
_func += "]";
return _func;
};
auto _find_matching = [](const strset_t& _expr, const std::string& _name) {
const auto _rconstants =
std::regex_constants::egrep | std::regex_constants::optimize;
for(const auto& itr : _expr)
{
if(std::regex_search(_name, std::regex(itr, _rconstants))) return true;
}
return false;
};
auto& _only_funcs = _config.include_functions;
auto& _skip_funcs = _config.exclude_functions;
auto _func = _get_funcname();
if(!_only_funcs.empty() && !_find_matching(_only_funcs, _func))
{
if(_config.verbose > 1)
TIMEMORY_PRINT_HERE("Skipping non-included function: %s", _func.c_str());
return;
}
if(_find_matching(_skip_funcs, _func))
{
if(_config.verbose > 1)
TIMEMORY_PRINT_HERE("Skipping designated function: '%s'", _func.c_str());
return;
}
auto& _only_files = _config.include_filenames;
auto& _skip_files = _config.exclude_filenames;
auto _full = _get_filename();
auto _file = _get_basename(_full);
if(!_config.include_internal &&
strncmp(_full.c_str(), _omnitrace_path.c_str(), _omnitrace_path.length()) == 0)
{
if(_config.verbose > 2)
TIMEMORY_PRINT_HERE("Skipping internal function: %s", _func.c_str());
return;
}
if(!_only_files.empty() && !_find_matching(_only_files, _full))
{
if(_config.verbose > 2)
TIMEMORY_PRINT_HERE("Skipping non-included file: %s", _full.c_str());
return;
}
if(_find_matching(_skip_files, _full))
{
if(_config.verbose > 2)
TIMEMORY_PRINT_HERE("Skipping non-included file: %s", _full.c_str());
return;
}
TIMEMORY_CONDITIONAL_PRINT_HERE(_config.verbose > 3, "%8s | %s%s | %s | %s", swhat,
_func.c_str(), _get_args().c_str(), _file.c_str(),
_full.c_str());
auto _label = _get_label(_func, _file, _full);
if(_label.empty()) return;
static thread_local strset_t _labels{};
const auto& _label_ref = *_labels.emplace(_label).first;
// get the depth of the frame
// auto _fdepth = get_depth(frame);
static thread_local int32_t _depth_tracker = 0;
auto _fdepth = _depth_tracker;
switch(what)
{
case PyTrace_CALL:
case PyTrace_C_CALL: _fdepth = _depth_tracker++; break;
case PyTrace_RETURN:
case PyTrace_C_RETURN: _fdepth = --_depth_tracker; break;
}
// start function
auto _profiler_call = [&]() {
auto& _entry = _config.records[_fdepth][_label];
_entry.emplace_back(
[&_label_ref]() { omnitrace_push_region(_label_ref.c_str()); },
[&_label_ref]() { omnitrace_pop_region(_label_ref.c_str()); });
_entry.back().first();
};
// stop function
auto _profiler_return = [&]() {
auto fitr = _config.records.find(_fdepth);
if(fitr == _config.records.end()) return;
auto litr = fitr->second.find(_label);
if(litr == fitr->second.end()) return;
if(litr->second.empty()) return;
litr->second.back().second();
litr->second.pop_back();
};
// process what
switch(what)
{
case PyTrace_CALL:
case PyTrace_C_CALL: _profiler_call(); break;
case PyTrace_RETURN:
case PyTrace_C_RETURN: _profiler_return(); break;
default: break;
}
// don't do anything with arg
tim::consume_parameters(arg);
}
//
py::module
generate(py::module& _pymod)
{
py::module _prof = _pymod.def_submodule("profiler", "Profiling functions");
auto _init = []() {
try
{
auto _file =
py::module::import("omnitrace").attr("__file__").cast<std::string>();
if(_file.find('/') != std::string::npos)
_file = _file.substr(0, _file.find_last_of('/'));
get_config().base_module_path = _file;
} catch(py::cast_error& e)
{
std::cerr << "[profiler_init]> " << e.what() << std::endl;
}
if(get_config().is_running) return;
get_config().records.clear();
get_config().base_stack_depth = -1;
get_config().is_running = true;
};
auto _fini = []() {
if(!get_config().is_running) return;
get_config().is_running = false;
get_config().base_stack_depth = -1;
get_config().records.clear();
};
_prof.def("profiler_function", &profiler_function, "Profiling function");
_prof.def("profiler_init", _init, "Initialize the profiler");
_prof.def("profiler_finalize", _fini, "Finalize the profiler");
py::class_<config> _pyconfig(_prof, "config", "Profiler configuration");
#define CONFIGURATION_PROPERTY(NAME, TYPE, DOC, ...) \
_pyconfig.def_property_static( \
NAME, [](py::object&&) { return __VA_ARGS__; }, \
[](py::object&&, TYPE val) { __VA_ARGS__ = val; }, DOC);
CONFIGURATION_PROPERTY("_is_running", bool, "Profiler is currently running",
get_config().is_running)
CONFIGURATION_PROPERTY("trace_c", bool, "Enable tracing C functions",
get_config().trace_c)
CONFIGURATION_PROPERTY("include_internal", bool, "Include functions within timemory",
get_config().include_internal)
CONFIGURATION_PROPERTY("include_args", bool, "Encode the function arguments",
get_config().include_args)
CONFIGURATION_PROPERTY("include_line", bool, "Encode the function line number",
get_config().include_line)
CONFIGURATION_PROPERTY("include_filename", bool,
"Encode the function filename (see also: full_filepath)",
get_config().include_filename)
CONFIGURATION_PROPERTY("full_filepath", bool,
"Display the full filepath (instead of file basename)",
get_config().full_filepath)
CONFIGURATION_PROPERTY("verbosity", int32_t, "Verbosity of the logging",
get_config().verbose)
static auto _get_strset = [](const strset_t& _targ) {
auto _out = py::list{};
for(auto itr : _targ)
_out.append(itr);
return _out;
};
static auto _set_strset = [](const py::list& _inp, strset_t& _targ) {
for(const auto& itr : _inp)
_targ.insert(itr.cast<std::string>());
};
#define CONFIGURATION_PROPERTY_LAMBDA(NAME, DOC, GET, SET) \
_pyconfig.def_property_static(NAME, GET, SET, DOC);
#define CONFIGURATION_STRSET(NAME, DOC, ...) \
{ \
auto GET = [](py::object&&) { return _get_strset(__VA_ARGS__); }; \
auto SET = [](py::object&&, const py::list& val) { \
_set_strset(val, __VA_ARGS__); \
}; \
CONFIGURATION_PROPERTY_LAMBDA(NAME, DOC, GET, SET) \
}
CONFIGURATION_STRSET("only_functions", "Function regexes to collect exclusively",
get_config().include_functions)
CONFIGURATION_STRSET("only_filenames", "Filename regexes to collect exclusively",
get_config().include_filenames)
CONFIGURATION_STRSET("skip_functions", "Function regexes to filter out of collection",
get_config().exclude_functions)
CONFIGURATION_STRSET("skip_filenames", "Filename regexes to filter out of collection",
get_config().exclude_filenames)
return _prof;
}
} // namespace pyprofile
} // namespace pyomnitrace
//
//======================================================================================//
+56
Просмотреть файл
@@ -0,0 +1,56 @@
// 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.
#pragma once
#include <pybind11/cast.h>
#include <pybind11/embed.h>
#include <pybind11/eval.h>
#include <pybind11/functional.h>
#include <pybind11/iostream.h>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>
#include <pybind11/stl.h>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <future>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <ostream>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
namespace pyomnitrace
{
namespace py = pybind11;
using namespace std::placeholders; // for _1, _2, _3...
using namespace py::literals;
} // namespace pyomnitrace
+51
Просмотреть файл
@@ -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
Просмотреть файл
@@ -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
Просмотреть файл
@@ -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
Просмотреть файл
@@ -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)
+12
Просмотреть файл
@@ -0,0 +1,12 @@
[build-system]
requires = [
"setuptools >= 40.0.4",
"setuptools_scm >= 2.0.0",
"wheel >= 0.29.0",
]
build-backend = 'setuptools.build_meta'
[tool.black]
line-length = 80
target-version = ['py38']
include = '\.py'
+36
Просмотреть файл
@@ -0,0 +1,36 @@
[metadata]
name = @PROJECT_NAME@
url = @PROJECT_HOMEPAGE_URL@
download_url = @PROJECT_HOMEPAGE_URL@.git
maintainer = AMD Research
license = MIT
description = @PROJECT_DESCRIPTION@
keywords =
performance
profiling
sampling
hardware counters
timing
memory
gpu
hip
rocm
classifiers =
Development Status :: 4 - Beta
Environment :: GPU
Intended Audience :: Developers
Intended Audience :: Science/Research
Natural Language :: English
License :: OSI Approved :: MIT License
Operating System :: POSIX :: Linux
Operating System :: Unix
Programming Language :: C++
Programming Language :: Python :: 3
Programming Language :: Python :: @OMNITRACE_PYTHON_VERSION@
Topic :: Software Development :: Libraries :: Python Modules
Topic :: Utilities
[options]
packages = @PROJECT_NAME@
zip_safe = false
include_package_data = false
+11
Просмотреть файл
@@ -0,0 +1,11 @@
from setuptools import setup
setup(
name="@PROJECT_NAME@",
version="@PROJECT_VERSION@",
description="@PROJECT_DESCRIPTION@",
author="AMD Research",
url="@PROJECT_HOMEPAGE_URL@",
packages=["@PROJECT_NAME@"],
python_requires="=@OMNITRACE_PYTHON_VERSION@",
)