Files
rocm-systems/source/lib/omnitrace/library/causal/components/backtrace.cpp
T
Jonathan R. Madsen 7c73d98125 Causal profiling fixes (#241)
- corrections in the calculations for latency and throughput points in `validate-causal-json.py`
- `omnitrace-causal` LD_PRELOAD libpthread
  - ensures omnitrace is always wrapping libpthread.so pthread symbols
- minimal experiment delay
  - always sleep 10 milliseconds before starting experiments
  - ensures ~10 samples are taken to determine the sampling rate
- fixes issue with deadlocks on condition variables
- overhaul of `causal::component::blocking_gotcha` and `causal::component::unblocking_gotcha` components
  - these components enforce the processing/crediting of delays before/after a thread is suspended
  - these components wrap functions `pthread_cond_wait`, `pthread_cond_signal`, `pthread_mutex_lock`, etc.
- Fully implemented correct handling of processing/crediting delays based on return values and arguments
  - E.g. skip crediting delay if `pthread_mutex_trylock` fail acquiring lock
  - E.g. `kill`, `sigwait`, etc. check to make sure they are only applied if the PID matches its PID
 
## Condition Variable Deadlock Fix

In parallel applications using condition variables, it was found that the causal profiling was virtually guaranteed to deadlock. Although it was difficult to prove, evidence suggested that this was due to the work that was being done while taking a sample was causing notification to the condition variable to be lost. This was alleviated by the following updates:
 
- Separate out the part of `causal::backtrace::sample(int)` which calculates the sampling rate into small `sample_rate` component
  - This component is essentially "always on"  during sampling
  - Added bundle of components invoked by `causal_sampler_t` during sampling
- Added two function calls to support disabling and re-enabling calls to `causal::backtrace::sample(int)` on a per-thread basis 
  - `causal::sampling::block_backtrace_samples()`
  - `causal::sampling::unblock_backtrace_samples()`
  - These two function now surround the wrappee functions of `blocking_gotcha` and `unblocking_gotcha`

**This solution was experimentally validated with a Geant4 application which uses a tasking model which makes _numerous_ calls to wait on a condition variables** (it was this application which exposed the bug)

* Fix validate-causal-json.py

- corrections in the calculations for latency and throughput points

* Update timemory submodule

- support for thread-local trait::runtime_enabled

* omnitrace-causal: LD_PRELOAD pthread library

- ensures omnitrace is always wrapping libpthread.so pthread symbols

* initial experiment delay

- always sleep 10 milliseconds before starting experiments
- ensures ~10 samples are taken to determine the sampling rate

* sample_rate component + block_backtrace_samples

- separate out the part of backtrace::sample which calculates the sampling rate into small sample_rate component
- add sample_rate component to causal_bundle_t used by causal_sampler_t
- causal::sampling::block_backtrace_samples() disables backtrace samples from being taken on a thread
- causal::sampling::unblock_backtrace_samples() enables backtrace samples from being taken on a thread
- above two function surround calls to function wrapped by blocking_gotcha and unblocking_gotcha
  - the work happening in backtrace::sample when within these calls
    produced deadlocks for condition variables (notifications to
    condition variables were lost)

* blocking/unblocking gotcha updates

- overhaul of blocking_gotcha and unblocking_gotcha
  - added fast_gotcha trait: replace function calls instead of wrapping
- when wrappees are called, backtrace samples are suppressed (thread-local)
- properly handle kill, sigwait, sigwaitinfo, sigtimedwait
- properly handle all instances of applying postblock based on return value

* Fix calculation of OMNITRACE_MAX_THREADS

* removed unnecessary checks in causal::delay

* Updated timemory with internal compiler error fix
2023-02-09 09:47:48 -06:00

207 lines
6.5 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/causal/components/backtrace.hpp"
#include "core/concepts.hpp"
#include "core/config.hpp"
#include "core/debug.hpp"
#include "core/state.hpp"
#include "core/utility.hpp"
#include "library/causal/data.hpp"
#include "library/causal/delay.hpp"
#include "library/causal/experiment.hpp"
#include "library/runtime.hpp"
#include "library/thread_data.hpp"
#include "library/thread_info.hpp"
#include "library/tracing.hpp"
#include <timemory/components/timing/backends.hpp>
#include <timemory/components/timing/wall_clock.hpp>
#include <timemory/mpl/concepts.hpp>
#include <timemory/mpl/type_traits.hpp>
#include <timemory/mpl/types.hpp>
#include <timemory/process/threading.hpp>
#include <timemory/units.hpp>
#include <timemory/utility/backtrace.hpp>
#include <atomic>
#include <ctime>
#include <type_traits>
namespace omnitrace
{
namespace causal
{
namespace component
{
namespace
{
using ::tim::backtrace::get_unw_signal_frame_stack_raw;
auto&
get_delay_statistics()
{
using thread_data_t =
thread_data<identity<tim::statistics<int64_t>>, category::sampling>;
static_assert(
use_placement_new_when_generating_unique_ptr<thread_data_t>::value,
"delay statistics thread data should use placement new to allocate unique_ptr");
static auto& _v = thread_data_t::instance(construct_on_init{});
return _v;
}
} // namespace
void
backtrace::start()
{
// do not delete these lines. The thread data needs to be allocated
// before it is called in sampler or else a deadlock will occur when
// the sample interrupts a malloc call
(void) get_delay_statistics();
}
void
backtrace::stop()
{}
void
sample_rate::sample(int _sig)
{
if(_sig != get_realtime_signal()) return;
// update the last sample for backtrace signal(s) even when in use
static thread_local int64_t _last_sample = 0;
auto _this_sample = tracing::now();
auto& _period_stat = get_delay_statistics()->at(threading::get_id());
if(_last_sample > 0) _period_stat += (_this_sample - _last_sample);
_last_sample = _this_sample;
}
void
backtrace::sample(int _sig)
{
constexpr size_t depth = ::omnitrace::causal::unwind_depth;
constexpr int64_t ignore_depth = ::omnitrace::causal::unwind_offset;
// update the last sample for backtrace signal(s) even when in use
static thread_local size_t _protect_flag = 0;
// sampling_guard _guard{};
if((_protect_flag & 1) == 1 ||
OMNITRACE_UNLIKELY(!trait::runtime_enabled<causal::component::backtrace>::get()))
{
return;
}
++_protect_flag;
m_index = causal::experiment::get_index();
m_stack = get_unw_signal_frame_stack_raw<depth, ignore_depth>();
// the batch handler timer delivers a signal according to the thread CPU
// clock, ensuring that setting the current selection and processing the
// delays only happens when the thread is active
if(_sig == get_cputime_signal())
{
if(!causal::experiment::is_active())
causal::set_current_selection(m_stack);
else
causal::delay::process();
}
else if(_sig == get_realtime_signal())
{
static thread_local auto _tid = threading::get_id();
auto& _period_stat = get_delay_statistics()->at(_tid);
if(causal::experiment::is_active() && causal::experiment::is_selected(m_stack))
{
m_selected = true;
causal::experiment::add_selected();
// compute the delay time based on the rate of taking samples,
// unless we have taken less than 10, in which case, we just
// use the pre-computed value.
auto _delay =
(_period_stat.get_count() < 10)
? causal::experiment::get_delay()
: (_period_stat.get_mean() * causal::experiment::get_delay_scaling());
causal::delay::get_local() += _delay;
}
}
else
{
OMNITRACE_THROW("unhandled signal %i\n", _sig);
}
++_protect_flag;
}
template <typename Tp>
Tp
backtrace::get_period(uint64_t _units)
{
using cast_type = std::conditional_t<std::is_floating_point<Tp>::value, Tp, double>;
double _realtime_freq =
(get_use_sampling_realtime()) ? get_sampling_real_freq() : 0.0;
double _cputime_freq = (get_use_sampling_cputime()) ? get_sampling_cpu_freq() : 0.0;
auto _freq = std::max<double>(_realtime_freq, _cputime_freq);
double _period = 1.0 / _freq;
int64_t _period_nsec = static_cast<int64_t>(_period * units::sec) % units::sec;
return static_cast<Tp>(_period_nsec) / static_cast<cast_type>(_units);
}
tim::statistics<int64_t>
backtrace::get_period_stats()
{
auto _data = tim::statistics<int64_t>{};
if(!get_delay_statistics()) return _data;
for(auto itr : *get_delay_statistics())
{
if(itr.get_count() > 1) _data += itr;
}
return _data;
}
void
backtrace::reset_period_stats()
{
for(auto& itr : *get_delay_statistics())
{
itr.reset();
}
}
} // namespace component
} // namespace causal
} // namespace omnitrace
#define INSTANTIATE_BT_CAUSAL_PERIOD(TYPE) \
template TYPE omnitrace::causal::component::backtrace::get_period<TYPE>(uint64_t);
INSTANTIATE_BT_CAUSAL_PERIOD(float)
INSTANTIATE_BT_CAUSAL_PERIOD(double)
INSTANTIATE_BT_CAUSAL_PERIOD(int64_t)
INSTANTIATE_BT_CAUSAL_PERIOD(uint64_t)