808ea7dfa7
## 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
318 lines
10 KiB
C++
318 lines
10 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.
|
|
|
|
#include "library/components/pthread_mutex_gotcha.hpp"
|
|
#include "library/components/category_region.hpp"
|
|
#include "library/components/pthread_gotcha.hpp"
|
|
#include "library/config.hpp"
|
|
#include "library/critical_trace.hpp"
|
|
#include "library/debug.hpp"
|
|
#include "library/runtime.hpp"
|
|
#include "library/sampling.hpp"
|
|
#include "library/utility.hpp"
|
|
|
|
#include <timemory/backends/threading.hpp>
|
|
#include <timemory/utility/signals.hpp>
|
|
#include <timemory/utility/types.hpp>
|
|
|
|
#include <cstdint>
|
|
#include <pthread.h>
|
|
#include <stdexcept>
|
|
|
|
namespace omnitrace
|
|
{
|
|
namespace component
|
|
{
|
|
using Device = critical_trace::Device;
|
|
using Phase = critical_trace::Phase;
|
|
|
|
pthread_mutex_gotcha::hash_array_t&
|
|
pthread_mutex_gotcha::get_hashes()
|
|
{
|
|
// theoretically, this private function will NEVER be called until it
|
|
// is called by a gotcha wrapper, which means the tool ids should be
|
|
// fully populated. If that fails to be the case for some reason,
|
|
// we could see weird results.
|
|
static auto _v = []() {
|
|
const auto& _data = pthread_mutex_gotcha_t::get_gotcha_data();
|
|
auto _init = hash_array_t{};
|
|
auto _skip = std::set<size_t>{};
|
|
if(!config::get_trace_thread_locks())
|
|
{
|
|
for(size_t i = 0; i < 3; ++i)
|
|
_skip.emplace(i);
|
|
}
|
|
if(!config::get_trace_thread_rwlocks())
|
|
{
|
|
for(size_t i = 3; i < 8; ++i)
|
|
_skip.emplace(i);
|
|
}
|
|
if(!config::get_trace_thread_spin_locks())
|
|
{
|
|
for(size_t i = 9; i < 12; ++i)
|
|
_skip.emplace(i);
|
|
}
|
|
for(size_t i = 0; i < gotcha_capacity; ++i)
|
|
{
|
|
auto&& _id = _data.at(i).tool_id;
|
|
if(!_id.empty())
|
|
_init.at(i) = critical_trace::add_hash_id(_id.c_str());
|
|
else
|
|
{
|
|
if(_skip.count(i) > 0) continue;
|
|
OMNITRACE_VERBOSE(
|
|
1,
|
|
"WARNING!!! pthread_mutex_gotcha tool id at index %zu was empty!\n",
|
|
i);
|
|
}
|
|
OMNITRACE_CI_FAIL(
|
|
_id.empty() || _init.at(i) == 0,
|
|
"pthread_mutex_gotcha tool id at index %zu has no hash value\n", i);
|
|
}
|
|
return _init;
|
|
}();
|
|
return _v;
|
|
}
|
|
|
|
void
|
|
pthread_mutex_gotcha::configure()
|
|
{
|
|
pthread_mutex_gotcha_t::get_initializer() = []() {
|
|
if(config::get_trace_thread_locks())
|
|
{
|
|
validate();
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<0, int, pthread_mutex_t*>{ "pthread_mutex_lock" });
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<1, int, pthread_mutex_t*>{ "pthread_mutex_unlock" });
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<2, int, pthread_mutex_t*>{ "pthread_mutex_trylock" });
|
|
}
|
|
|
|
if(config::get_trace_thread_rwlocks())
|
|
{
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<3, int, pthread_rwlock_t*>{
|
|
"pthread_rwlock_rdlock" });
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<4, int, pthread_rwlock_t*>{
|
|
"pthread_rwlock_wrlock" });
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<5, int, pthread_rwlock_t*>{
|
|
"pthread_rwlock_tryrdlock" });
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<6, int, pthread_rwlock_t*>{
|
|
"pthread_rwlock_trywrlock" });
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<7, int, pthread_rwlock_t*>{
|
|
"pthread_rwlock_unlock" });
|
|
}
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<8, int, pthread_barrier_t*>{ "pthread_barrier_wait" });
|
|
|
|
if(config::get_trace_thread_spin_locks())
|
|
{
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<9, int, pthread_spinlock_t*>{ "pthread_spin_lock" });
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<10, int, pthread_spinlock_t*>{
|
|
"pthread_spin_trylock" });
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<11, int, pthread_spinlock_t*>{
|
|
"pthread_spin_unlock" });
|
|
}
|
|
|
|
pthread_mutex_gotcha_t::configure(
|
|
comp::gotcha_config<12, int, pthread_t, void**>{ "pthread_join" });
|
|
};
|
|
}
|
|
|
|
void
|
|
pthread_mutex_gotcha::shutdown()
|
|
{
|
|
pthread_mutex_gotcha_t::disable();
|
|
}
|
|
|
|
void
|
|
pthread_mutex_gotcha::validate()
|
|
{
|
|
if(config::get_trace_thread_locks() && config::get_use_perfetto())
|
|
{
|
|
OMNITRACE_PRINT_F("\n");
|
|
OMNITRACE_PRINT_F("\n");
|
|
OMNITRACE_PRINT_F("\n");
|
|
OMNITRACE_PRINT_F(
|
|
"The overhead of all the mutex locking internally by perfetto is\n")
|
|
OMNITRACE_PRINT_F(
|
|
"so significant that all timing data is rendered meaningless.\n");
|
|
OMNITRACE_PRINT_F(
|
|
"However, mutex locking is effectively non-existant in timemory.\n");
|
|
OMNITRACE_PRINT_F("If you want to trace the mutex locking:\n")
|
|
OMNITRACE_PRINT_F(" OMNITRACE_USE_TIMEMORY=ON\n");
|
|
OMNITRACE_PRINT_F(" OMNITRACE_USE_PERFETTO=OFF\n");
|
|
OMNITRACE_PRINT_F("\n");
|
|
OMNITRACE_PRINT_F("\n");
|
|
OMNITRACE_PRINT_F("\n");
|
|
OMNITRACE_FAIL_F("OMNITRACE_USE_PERFETTO and OMNITRACE_TRACE_THREAD_LOCKS cannot "
|
|
"both be enabled.\n");
|
|
}
|
|
}
|
|
|
|
pthread_mutex_gotcha::pthread_mutex_gotcha(const gotcha_data_t& _data)
|
|
: m_data{ &_data }
|
|
{}
|
|
|
|
template <typename... Args>
|
|
auto
|
|
pthread_mutex_gotcha::operator()(uintptr_t&& _id, int (*_callee)(Args...),
|
|
Args... _args) const
|
|
{
|
|
using bundle_t = category_region<category::pthread>;
|
|
|
|
if(is_disabled())
|
|
{
|
|
if(!_callee)
|
|
{
|
|
if(m_data)
|
|
{
|
|
OMNITRACE_PRINT("Warning! nullptr to %s\n", m_data->tool_id.c_str());
|
|
}
|
|
return EINVAL;
|
|
}
|
|
return (*_callee)(_args...);
|
|
}
|
|
|
|
struct local_dtor
|
|
{
|
|
explicit local_dtor(bool& _v)
|
|
: _protect{ _v }
|
|
{}
|
|
~local_dtor() { _protect = false; }
|
|
bool& _protect;
|
|
} _dtor{ m_protect = true };
|
|
|
|
uint64_t _cid = 0;
|
|
uint64_t _parent_cid = 0;
|
|
uint32_t _depth = 0;
|
|
int64_t _ts = 0;
|
|
|
|
OMNITRACE_SCOPED_THREAD_STATE(ThreadState::Internal);
|
|
|
|
if(_id < std::numeric_limits<uintptr_t>::max() && get_use_critical_trace())
|
|
{
|
|
std::tie(_cid, _parent_cid, _depth) = create_cpu_cid_entry();
|
|
_ts = comp::wall_clock::record();
|
|
}
|
|
|
|
bundle_t::audit(std::string_view{ m_data->tool_id }, audit::incoming{}, _args...);
|
|
auto _ret = (*_callee)(_args...);
|
|
bundle_t::audit(std::string_view{ m_data->tool_id }, audit::outgoing{}, _ret);
|
|
|
|
if(_id < std::numeric_limits<uintptr_t>::max() && get_use_critical_trace())
|
|
{
|
|
add_critical_trace<Device::CPU, Phase::DELTA>(
|
|
threading::get_id(), _cid, 0, _parent_cid, _ts, comp::wall_clock::record(), 0,
|
|
_id, get_hashes().at(m_data->index), _depth);
|
|
}
|
|
|
|
tim::consume_parameters(_id, _cid, _parent_cid, _depth, _ts);
|
|
return _ret;
|
|
}
|
|
|
|
int
|
|
pthread_mutex_gotcha::operator()(int (*_callee)(pthread_mutex_t*),
|
|
pthread_mutex_t* _mutex) const
|
|
{
|
|
if(m_protect) return (*_callee)(_mutex);
|
|
return (*this)(reinterpret_cast<uintptr_t>(_mutex), _callee, _mutex);
|
|
}
|
|
|
|
int
|
|
pthread_mutex_gotcha::operator()(int (*_callee)(pthread_spinlock_t*),
|
|
pthread_spinlock_t* _lock) const
|
|
{
|
|
if(m_protect) return (*_callee)(_lock);
|
|
return (*this)(reinterpret_cast<uintptr_t>(_lock), _callee, _lock);
|
|
}
|
|
|
|
int
|
|
pthread_mutex_gotcha::operator()(int (*_callee)(pthread_rwlock_t*),
|
|
pthread_rwlock_t* _lock) const
|
|
{
|
|
if(m_protect) return (*_callee)(_lock);
|
|
return (*this)(reinterpret_cast<uintptr_t>(_lock), _callee, _lock);
|
|
}
|
|
|
|
int
|
|
pthread_mutex_gotcha::operator()(int (*_callee)(pthread_barrier_t*),
|
|
pthread_barrier_t* _barrier) const
|
|
{
|
|
if(m_protect) return (*_callee)(_barrier);
|
|
return (*this)(reinterpret_cast<uintptr_t>(_barrier), _callee, _barrier);
|
|
}
|
|
|
|
int
|
|
pthread_mutex_gotcha::operator()(int (*_callee)(pthread_t, void**), pthread_t _thr,
|
|
void** _tinfo) const
|
|
{
|
|
if(m_protect) return (*_callee)(_thr, _tinfo);
|
|
return (*this)(static_cast<uintptr_t>(threading::get_id()), _callee, _thr, _tinfo);
|
|
}
|
|
|
|
bool
|
|
pthread_mutex_gotcha::is_disabled()
|
|
{
|
|
return (get_state() != ::omnitrace::State::Active ||
|
|
get_thread_state() != ThreadState::Enabled ||
|
|
(get_use_sampling() && !pthread_gotcha::sampling_enabled_on_child_threads()));
|
|
}
|
|
} // namespace component
|
|
} // namespace omnitrace
|
|
|
|
namespace tim
|
|
{
|
|
namespace policy
|
|
{
|
|
template <size_t N>
|
|
pthread_mutex_gotcha&
|
|
static_data<pthread_mutex_gotcha, pthread_mutex_gotcha_t>::operator()(
|
|
std::integral_constant<size_t, N>, const component::gotcha_data& _data) const
|
|
{
|
|
using thread_data_t =
|
|
omnitrace::thread_data<pthread_mutex_gotcha, std::integral_constant<size_t, N>>;
|
|
static thread_local auto& _v =
|
|
thread_data_t::instance(omnitrace::construct_on_init{}, _data);
|
|
return *_v;
|
|
}
|
|
} // namespace policy
|
|
} // namespace tim
|