Dosyalar
rocm-systems/source/lib/common/setup.hpp
T
Jonathan R. Madsen 808ea7dfa7 Rework sampling and colorized logs (#140)
## Overview

This is a significant PR which has 3 very notable characteristics:

1. Omnitrace colorizes most of it's logging
2. Completely reworked the sampling 
  - Samples now record the current instruction pointers instead of strings
    - This _dramatically_ decreases the overhead of taking a sample
  - The collection of metrics during a sample are split out into another component, enabling that data collection to be disabled -- which decreases the sampling overhead even further
  - When both `OMNITRACE_SAMPLING_CPUTIME` and `OMNITRACE_SAMPLING_REALTIME` are ON:
    - `OMNITRACE_SAMPLING_CPUTIME_FREQ` and `OMNITRACE_SAMPLING_REALTIME_FREQ` can be used to individually control the sampling frequency 
  - `OMNITRACE_SAMPLING_CPUTIME_DELAY` and `OMNITRACE_SAMPLING_REALTIME_DELAY` can be used to individually control the delay time before starting
  - Now, omnitrace does not start a real-time sampler on the main thread unless `OMNITRACE_SAMPLING_REALTIME` is ON
    - In the future, an `OMNITRACE_SAMPLING_TIDS` (and real-time, cpu-time variants) configuration variable(s) will allow you to select which threads will be sampled
3. Files produced by `omnitrace` exe -- `available-instr.txt`, `instrumented-instr.txt`, etc. -- now no longer has `-instr` suffix and are placed in `instrumentation/` subfolder, i.e. `available-instr.txt` -> instrumentation/available.txt`
  - This helped de-clutter the output folder

Most of the other edits were reorganization (e.g. internal namespace changes), cleanup, and splitting up functionality.

## Bug Fixes

There is a bug fix with respect to the HSA callbacks which disabled sampling on child threads when an HSA API call was made

## Details

- created thread_info struct for mapping different thread IDs
- reorganized file structure significantly
- added categories.hpp, concepts.hpp
- moved around name trait definitions
- moved all omnitrace components into `omnitrace::component` namespace
  - there was a lot of inconsistency b/t using `tim::component` in some places and `omnitrace::component`
  - added macros like OMNITRACE_DECLARE_COMPONENT in lieu of TIMEMORY_DECLARE_COMPONENT
- OMNITRACE_CRITICAL_TRACE_NUM_THREADS -> OMNITRACE_THREAD_POOL_SIZE
- roctracer and critical_trace use same thread pool
- critical_trace functions do not lock anymore bc of thread-local TaskGroup
- added `component::local_category_region` to support using `component::category_region` without explicitly passing in name
- removed `component::omnitrace` (unused)
- migrated KokkosP and OMPT to use `component::local_category_region`
  - removed `component::user_region` as a result
- migrated omnitrace_{push,pop}_{trace,region}_hidden to use component::category_region
  - removed `component::functors` as a result
- migrated some ppdefs
- `api::omnitrace` -> `project::omnitrace`
- `api::(...)` -> `category::(...)`
- improved recording the execution time of threads
  - migrated this functionality out of pthread_create_gotcha and into thread_info
- moved mpi_gotcha, fork_gotcha, exit_gotcha, rcclp into omnitrace::component namespace
- split backtrace up into backtrace, backtrace_metrics, backtrace_timestamp components
- sampling.cpp handles setup and post-processing that was formerly in backtrace
- updated logging to use colors
- `OMNITRACE_COLORIZED_LOG` config variable
- updated docs on JSON output from timemory
- instrumentation info in instrumentation subfolder
- added testing for KokkosP entries
- added testing for ompt entries
- add_critical_trace function defined in critical_trace.hpp
- disable push_thread_state and pop_thread_state when thread state is Disabled or Completed
- add comp::page_rss to main bundle
- thread_data supports std::optional instead of std::unique_ptr
- thread_data supports tim::identity<T> to avoid unique_ptr or optional
- tracing::record_thread_start_time()
- tracing::push_timemory and tracing::pop_timemory are templated on CategoryT
- removed anonymous namespace from omnitrace::utility
- sampling backtrace stores instruction pointers instead of strings
- component::category_region updates
  - handle disabled thread state
  - handle finalized state
  - fewer debug messages
  - invoke thread_init()
  - invoke thread_init_sampling()
  - handle push/pop count based on category
  - push/pop count only modified when used
- component::cpu_freq
- components/ensure_storage.hpp
- reworked the pthread_create replacement function
- updated parallel-overhead example to report # of times locked
- OMNITRACE_MAX_UNWIND_DEPTH build option
- update timemory submodule
2022-08-31 01:24:31 -05:00

207 satır
7.7 KiB
C++

// 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 "common/defines.h"
#include "common/delimit.hpp"
#include "common/environment.hpp"
#include "common/join.hpp"
#include <cstdlib>
#include <cstring>
#include <string>
#include <string_view>
#include <sys/stat.h>
#include <unistd.h>
#if !defined(OMNITRACE_SETUP_LOG_NAME)
# if defined(OMNITRACE_COMMON_LIBRARY_NAME)
# define OMNITRACE_SETUP_LOG_NAME "[" OMNITRACE_COMMON_LIBRARY_NAME "]"
# else
# define OMNITRACE_SETUP_LOG_NAME
# endif
#endif
#if !defined(OMNITRACE_SETUP_LOG_START)
# if defined(OMNITRACE_COMMON_LIBRARY_LOG_START)
# define OMNITRACE_SETUP_LOG_START OMNITRACE_COMMON_LIBRARY_LOG_START
# elif defined(TIMEMORY_LOG_COLORS_AVAILABLE)
# define OMNITRACE_SETUP_LOG_START \
fprintf(stderr, "%s", ::tim::log::color::info());
# else
# define OMNITRACE_SETUP_LOG_START
# endif
#endif
#if !defined(OMNITRACE_SETUP_LOG_END)
# if defined(OMNITRACE_COMMON_LIBRARY_LOG_END)
# define OMNITRACE_SETUP_LOG_END OMNITRACE_COMMON_LIBRARY_LOG_END
# elif defined(TIMEMORY_LOG_COLORS_AVAILABLE)
# define OMNITRACE_SETUP_LOG_END fprintf(stderr, "%s", ::tim::log::color::end());
# else
# define OMNITRACE_SETUP_LOG_END
# endif
#endif
#define OMNITRACE_SETUP_LOG(CONDITION, ...) \
if(CONDITION) \
{ \
fflush(stderr); \
OMNITRACE_SETUP_LOG_START \
fprintf(stderr, "[omnitrace]" OMNITRACE_SETUP_LOG_NAME "[%i] ", getpid()); \
fprintf(stderr, __VA_ARGS__); \
OMNITRACE_SETUP_LOG_END \
fflush(stderr); \
}
namespace omnitrace
{
inline namespace common
{
namespace path
{
inline std::string
find_path(const std::string& _path, int _verbose, std::string _search_paths = {})
{
auto _exists = [](std::string_view _name) {
struct stat _buffer;
if(stat(_name.data(), &_buffer) == 0)
return (S_ISREG(_buffer.st_mode) != 0 || S_ISLNK(_buffer.st_mode) != 0);
return false;
};
if(_exists(_path) && !_path.empty() && _path.at(0) == '/') return _path;
auto _default_search_paths =
join(":", get_env("OMNITRACE_PATH", ""), get_env("PWD", ""), ".",
get_env("LD_LIBRARY_PATH", ""), get_env("LIBRARY_PATH", ""));
if(_search_paths.empty()) _search_paths = _default_search_paths;
auto _paths = delimit(_search_paths, ":");
if(_paths.empty())
{
_search_paths = _default_search_paths;
_paths = delimit(_search_paths, ":");
}
int _verbose_lvl = 2;
for(const auto& itr : _paths)
{
auto _f = join('/', itr, _path);
OMNITRACE_SETUP_LOG(_verbose >= _verbose_lvl, "searching for '%s' in '%s' ...\n",
_path.c_str(), itr.c_str());
if(_exists(_f)) return _f;
for(const auto* sitr : { "lib", "lib64", "../lib", "../lib64" })
{
_f = join('/', itr, sitr, _path);
OMNITRACE_SETUP_LOG(_verbose >= _verbose_lvl,
"searching for '%s' in '%s' ...\n", _path.c_str(),
common::join('/', itr, sitr).c_str());
if(_exists(_f)) return _f;
}
}
return _path;
}
inline std::string
dirname(const std::string& _fname)
{
if(_fname.find('/') != std::string::npos)
return _fname.substr(0, _fname.find_last_of('/'));
return std::string{};
}
} // namespace path
inline void
setup_environ(int _verbose, const std::string& _search_paths = {},
std::string _omnilib = "libomnitrace.so",
std::string _omnilib_dl = "libomnitrace-dl.so")
{
_omnilib = common::path::find_path(_omnilib, _verbose, _search_paths);
_omnilib_dl = common::path::find_path(_omnilib_dl, _verbose, _search_paths);
// This environment variable forces the ROCR-Runtime to use polling to wait
// for signals rather than interrupts. We set this variable to avoid issues with
// rocm/roctracer hanging when interrupted by the sampler
//
// see:
// https://github.com/ROCm-Developer-Tools/roctracer/issues/22#issuecomment-572814465
setenv("HSA_ENABLE_INTERRUPT", "0", 0);
#if defined(OMNITRACE_USE_ROCTRACER) && OMNITRACE_USE_ROCTRACER > 0
setenv("HSA_TOOLS_LIB", _omnilib.c_str(), 0);
#endif
#if defined(OMNITRACE_USE_ROCPROFILER) && OMNITRACE_USE_ROCPROFILER > 0
# if OMNITRACE_HIP_VERSION >= 50200
# define ROCPROFILER_METRICS_DIR "lib/rocprofiler"
# else
# define ROCPROFILER_METRICS_DIR "rocprofiler/lib"
# endif
setenv("HSA_TOOLS_LIB", _omnilib.c_str(), 0);
setenv("ROCP_TOOL_LIB", _omnilib.c_str(), 0);
setenv("ROCPROFILER_LOG", "1", 0);
setenv("ROCP_HSA_INTERCEPT", "1", 0);
setenv("HSA_TOOLS_REPORT_LOAD_FAILURE", "1", 0);
for(const auto* itr : { "OMNITRACE_ROCM_PATH", "ROCM_PATH" })
{
if(getenv(itr))
{
setenv("ROCP_METRICS",
common::join('/', getenv(itr), ROCPROFILER_METRICS_DIR, "metrics.xml")
.c_str(),
0);
setenv("OMNITRACE_ROCPROFILER_LIBRARY",
common::join('/', getenv(itr), "rocprofiler/lib/librocprofiler64.so")
.c_str(),
0);
break;
}
}
// default path
setenv("ROCP_METRICS",
common::join('/', OMNITRACE_DEFAULT_ROCM_PATH, ROCPROFILER_METRICS_DIR,
"metrics.xml")
.c_str(),
0);
#endif
#if defined(OMNITRACE_USE_OMPT) && OMNITRACE_USE_OMPT > 0
std::string _omni_omp_libs = _omnilib_dl;
const char* _omp_libs = getenv("OMP_TOOL_LIBRARIES");
if(_omp_libs != nullptr &&
std::string_view{ _omp_libs }.find(_omnilib_dl) == std::string::npos)
_omni_omp_libs = common::join(':', _omp_libs, _omnilib_dl);
OMNITRACE_SETUP_LOG(_verbose >= 1, "setting OMP_TOOL_LIBRARIES to '%s'\n",
_omni_omp_libs.c_str());
setenv("OMP_TOOL_LIBRARIES", _omni_omp_libs.c_str(), 1);
#endif
(void) _omnilib;
(void) _omnilib_dl;
}
} // namespace common
} // namespace omnitrace