9618ddefba
* Addition of basic structure
* Reworked categories
* More causal integration additions
* Causal implementation
* Update examples
* delete virtual_speedup files
* Update perfetto submodule to v31.0
* Update dyninst submodule
* Update timemory submodule
* ElfUtils build for libdw
* OMNITRACE_LIKELY and OMNITRACE_UNLIKELY
* Update common lib join
* Examples updates for causal profiling
* config updates with causal options
- OMNITRACE_CAUSAL_FIXED_LINE
- OMNITRACE_CAUSAL_FIXED_SPEEDUP
- OMNITRACE_CAUSAL_FILE
- OMNITRACE_CAUSAL_BINARY_SCOPE
- OMNITRACE_CAUSAL_SOURCE_SCOPE
- version info in banner
- support increments in parse_numeric_range
- fix occasional deadlock in first call to get_config
* PTL general task group
* Always include PID in debug/verbose messages
* Add blocking/unblocking gotchas to runtime init bundle
* CausalState
* thread_data updates
- generic component_bundle_cache
* Improve handling of causal in category_region
* components updates
- backtrace_causal component
- backtrace::get_data member func
- decrease ignore_depth in backtrace::sample(int)
- handle "omnitrace_main" in backtrace::filter_and_patch(...)
- tweak internal thread state scope for pthread_mutex_gotcha wrappers
* simplify tracing get_instrumentation_bundles usage
* sampling updates
- include backtrace_causal component
- disable backtrace_metrics if using causal and not using perfetto
- disable backtrace and backtrace_timestamp when using causal
- post_process_causal
* causal updates
- more checks in blocking_gotcha and unblocking_gotcha start/stop
- miscellaneous overhaul of data
- experiment update
* Remove virtual speedup
* libomnitrace code_object
* causal-profiling test
* libomnitrace library.cpp updates
- handle causal profiling
- fini_bundle
* Disable causal profiling by default
* Updated causal code and example
- example: three execution variants: cpu + rng, cpu, rng
- example: three instrumentation variants: none, omni, coz
- fix blocking gotcha credit
- rework perform_experiment_impl
- get_eligible_address_ranges
- compute_eligible_lines
- support fixed lines/speedups/functions
- update selected_entry to support function mode
- fix causal::delay
- experiment updates
* omnitrace_progress / omnitrace_user_progress
- with accompanying omnitrace_annotated_progress / omnitrace_user_annotated_progress
* Update timemory submodule
* CausalMode
- mode indicated whether causal predictions source be at line-level or function-level
* code_object, config, runtime, sampling, thread_data
- code_object: address_range
- code_object: basic::line_info serialize(), name(), hash()
- config updates
- two signals for causal sampling
- thread_data init fixes
* pthread updates
- pthread_create_gotcha processes delays
- pthread_mutex_gotcha does not wrap pthread_join in causal mode
* backtrace_causal update
- dynamic delay period stats
* main wrapper uses basename of argv[0]
* update elfio submodule
* perf support (currently unused)
* Fix experiment JSON serialization
- static_vector.hpp (unused)
* causal executable + config options updates
- omnitrace-causal exe simplifies running multiple causal configs
- changed the causal config option names
* Support both throughput and latency points
* process-causal-json.py script
- will be used later for testing
* stable_vector
* Rework thread_data
* Improve omnitrace-causal exe
- better verbosity handling
- correct diagnosis of status for child process
- execvpe when only one iteration (debugging)
* Update timemory submodule
* exe --version
- omnitrace, omnitrace-avail, and omnitrace-sample all support --version on command-line
* OMNITRACE_INTERNAL_API + OMNITRACE_{LIKELY,UNLIKELY}
* omnitrace-causal cmake format
* omnitrace config update
- OMNITRACE_CAUSAL_FILE_CLOBBER
* custom exception
- wraps STL exception and gets stacktrace during construction
* exit_gotcha supports _Exit
* use global construct_on_init + max threads
- add some safety when exceeding max # of threads
* update code_object binary filter
- exclude dyninst and tbbmalloc library
* containers: c_array, static_vector, stable_vector
- moved utility::c_array to container::c_array
- created static_vector: std::vector bound to std::array
- created stable_vector: vector with stable references
* grow thread_data when new thread created
* causal updates
- data: improve compute_eligible_lines to ignore lambdas
- data: use new thread_data
- delay: use new thread_data
- experiment: properly support latency points
- experiment: support file clobber
- experiment: ensure non-zero experiment time
- progress_point: use new thread_data
- backtrace_causal: use new thread_data
* Update causal-profiling tests
* fix omnitrace-causal backslash escaping
* process-causal-json script
* restructure causal implementation
- update verbose messages for omnitrace-causal diagnose_status
- migrated causal implementation in sampling.cpp to causal/sampling.cpp
- OMNITRACE_USE_CAUSAL does not require OMNITRACE_USE_SAMPLING
- added Mode::Causal
- causal sampling uses same signals as regular sampling
- moved tracing::thread_init to implementation file
- combined tracing::thread_init and tracing::thread_init_sampling
- added causal/components folder
- pthread_create_gotcha::wrapper_config
- omnitrace_preload checks OMNITRACE_USE_CAUSAL
- updates mode accordingly
* update timemory submodule
* update timemory submodule
* causal example updates
- causal for lulesh
* perf code + utility - helpers
- relocated causal perf code
- placement new when generating unique ptr trait for potentially allocating during sampling
- additions to utility header
- removed previously added helpers.hpp
* update timemory submodule
* Default env variables for omnitrace-causal
- activate OMNITRACE_USE_KOKKOSP, etc.
* update stable_vector and static_vector
- static vector can use atomic for size tracking for thread-safe situations
* update causal example header
- CAUSAL_PROGRESS_NAMED
- use CAUSAL_ prefix for some macros
* Tweak lulesh example
- use CAUSAL_PROGRESS instead of CAUSAL_BEGIN and CAUSAL_END
* omnitrace-sample support for causal mode
- set OMNITRACE_USE_SAMPLING to off when OMNITRACE_MODE=causal
* refactor and cleanup code_object
- scope filter
- fixes to address_range
* overhaul causal data + causal config options
- full support for function and line mode
- support static vector of instruction pointers
- improve line info mapping resolution
- remove thread-locality from miscellanous functions where unnecessary
- causal options for {binary,source,function,fileline} exclusion
* causal experiment, sampling, and backtrace updates
- is_selected + unwind address array
- experiment warning about progress points
- increased buffer size for backtrace_casual sampler
- backtrace_causal only stores IP addresses instead of full unwind info
* category_region updates
- minor refactor
- local_category_region::mark
* Update causal tests
* Bump version to 1.8.0
* omnitrace-causal args + CLOBBER -> RESET
- renamed OMNITRACE_CAUSAL_FILE_CLOBBER to OMNITRACE_CAUSAL_FILE_RESET
- updated omnitrace-causal exe to support recently added configuration options
- other miscellaneous tweaks to data.cpp, experiment.cpp, and sampling.cpp
* Refactor causal and code_object
- code_object.hpp and code_object.cpp moved into binary folder
- causal components namespaced into omnitrace::causal::component
- moved sample_data out of backtrace_causal and into own file
- renamed backtrace_causal to causal::component::backtrace
* preload omnitrace_init + OMNITRACE_DEBUG_MARK
- env OMNITRACE_DEBUG_MARK
- fix omnitrace_init call when LD_PRELOAD-ing omnitrace
* Fix fileline support + line-info output names + experiment log
- line-info log files are prefixed with experiment name
- don't print experiment duration when E2E
- account for fileline scope in analysis
* KokkosP: OMNITRACE_KOKKOSP_NAME_LENGTH_MAX
- config option to limit the name of kokkos tool callbacks
- remove [kokkos] from KokkosP names
* Update causal example
- minor tweaks to decrease probability of overlapping regions in binary
* omnitrace-causal update
- prefix N / Ntot in environment printout
* Miscellaneous updates
- causal::finish_experimenting()
- OMNITRACE_CAUSAL_RANDOM_SEED
- KokkosP causal updates
- exclude some callbacks, make some callbacks unique, etc.
- address_range::operator+=(address_range)
- combine contiguous ranges in binary/analysis.cpp when file, func, line is same and address range is contiguous
- bfd_line_info reads inline info
- wait for perform_experiment_impl to complete
- causal::delay updates
- delay::process checks if experiment is active
- uses threading::get_id()
- experiment scales duration up for larger speedup experiments
- line info samples includes excluded lines
- sampler uses CLOCK_REALTIME
- blocking_gotcha updates
- is no longer fully static
- adds audit routine which sets the postblock value to zero if try/timed routine fails
- category::host was added to causal_throughput_categories_t
- pthread_create_gotcha sets new threads local parent delay
- was using internal value, now uses sequent value
* Causal improvements to KokkosP
* Updates to experiment time scaling
- use stats instead of just max
* binary/link_map.{hpp,cpp}
* update process-causal-json.py
* Folded fileline scope into source scope
* Update documentation
- Add documentation for causal profiling
- Replace 'Omnitrace' with 'OmniTrace' everywhere
* Update causal-helpers.cmake + omnitrace-testing.cmake
- split tests/CMakeLists.txt partially into omnitrace-testing.cmake
* omnitrace/causal.h
- OMNITRACE_CAUSAL_PROGRESS
- OMNITRACE_CAUSAL_PROGRESS_NAMED
- OMNITRACE_CAUSAL_BEGIN
- OMNITRACE_CAUSAL_END
* selected_entry + remove default filters for lambdas and operator()
- selected entry stores range and binary load address
* update process-causal-json.py
* format examples/lulesh/CMakeLists.txt
* causal-helpers find_package(Threads)
* OMNITRACE_KOKKOSP_KERNEL_LOGGER
- was OMNITRACE_KOKKOS_KERNEL_LOGGER
* quiet find of coz-profiler
* Fix rocm_smi exception handling
* Update timemory submodule (binutils)
- fix binutls compile error on some systems
- bump binutils to v2.40
* Fix miscellaneous tests
* OMNITRACE_KOKKOSP_PREFIX
* revert rocm_smi handling
* ElfUtils updates
- default to download version 0.188
- add -Wno-error=null-dereference due to GCC 12 compiler error
* Update causal example
* Remove OMNITRACE_VERBOSE from global workflow envs
* Reliable causal test
* disable compilation of causal perf files
* Remove set_current_selection with unwind stack
* update timemory submodule
* fix for segfault on bionic
- locking in TLS dtor was causing segfault
* remove experiment::is_selected(unwind_stack_t)
* update default init of selected_entry
* Fix for when IP is not offset by load address
* Update CMakeLists.txt
* Miscellaneous updates
- OMNITRACE_WARNING_OR_CI_THROW
- OMNITRACE_REQUIRE
- OMNITRACE_PREFER
- fixed issues with no ASLR
- added load address variable and ipaddr() func to basic/bfd line info
- removed get_basic() from dwarf_line_info
- TIMEMORY_PREFER -> OMNITRACE_PREFER
- removed previously added binary_address and range variables from selected_entry
* Removed superfluous CausalState
* Additional causal tests (lulesh + kokkos)
* filter, prefer, analysis ASLR handling
- removed default filter on cold functions
- fixed OMNITRACE_PREFER
- fixed analysis ASLR handling
* Tweak line-info output
* Removed some superfluous code
- causal/delay
- causal/selected_entry
* Exclude main.cold in function mode
* Update validate-perfetto-proto.py
- account for occasional http errors
* Add sampling test disabling tmp files
* argparser for process-causal-json
- support validation
- support filtering
* Avoid pthread_{lock,unlock} in sampling offload
- use homemade atomic_mutex/atomic_lock since contention will be low and using pthread tools might trigger our wrappers
* Rename process-causal-json.py
- validate-causal-json.py
* rework omnitrace_add_causal_test
- capable of performing validation
- added validation tests
* Fix kokkosp_begin_deep_copy + causal
* Tweak address range in bfd_line_info::read_pc
* Tweak analysis and data IP handling
- look for gaps
* Disable scaling experiment time by speedup
* Revert change in max threads during CI
* binary updates
- significant overhaul of binary analysis implementation
- removed "basic_line_info" and "bfd_line_info" in lieu of "symbol" class
- symbol class has basic BFD info + vector of inlines + vector of dwarf info
* Updated causal to use new binary analysis
- Fix symbol.cpp includes
* Updated formatting target
- include *.cmake files
* Updated causal tests
- causal tests should be stable now
* Update timemory and dyninst submodules
- TPLs are stripped + built w/o debug info
* Increase tolerance for causal validation speedups
- higher speedups have more variance (increased to +/- 5 from 3)
* Support causal output for MPI
- i.e. tag with MPI rank
* omnitrace-causal launcher argument
* improve experiment sampling output
* causal data updates
- call compute lines once
- fixed filtered cached binary info
- debugging info when experiment fails to start
* Tweaked causal validation tests
* dwarf_entry ranges
* CI updates
- increase max threads to 64
* Tweak causal E2E validation tests
- more threads
- shorter thread runtime
- more iterations
* Fix shadowed variable
* fix symbol read_bfd last PC calculation
* fix maybe-uninitialized warning
* omnitrace-causal launcher update
- only inject "omnitrace-causal --" once
- throw error if no matches found
* Update causal profiling docs for launcher
* fix address range boundaries
1202 строки
41 KiB
C++
1202 строки
41 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/sampling.hpp"
|
|
#include "library/common.hpp"
|
|
#include "library/components/backtrace.hpp"
|
|
#include "library/components/backtrace_metrics.hpp"
|
|
#include "library/components/backtrace_timestamp.hpp"
|
|
#include "library/components/fwd.hpp"
|
|
#include "library/config.hpp"
|
|
#include "library/debug.hpp"
|
|
#include "library/locking.hpp"
|
|
#include "library/ptl.hpp"
|
|
#include "library/runtime.hpp"
|
|
#include "library/state.hpp"
|
|
#include "library/thread_data.hpp"
|
|
#include "library/thread_info.hpp"
|
|
#include "library/tracing.hpp"
|
|
#include "library/tracing/annotation.hpp"
|
|
#include "library/utility.hpp"
|
|
|
|
#include <timemory/backends/papi.hpp>
|
|
#include <timemory/backends/threading.hpp>
|
|
#include <timemory/components/data_tracker/components.hpp>
|
|
#include <timemory/components/macros.hpp>
|
|
#include <timemory/components/papi/extern.hpp>
|
|
#include <timemory/components/papi/papi_array.hpp>
|
|
#include <timemory/components/papi/papi_vector.hpp>
|
|
#include <timemory/components/timing/backends.hpp>
|
|
#include <timemory/components/trip_count/extern.hpp>
|
|
#include <timemory/macros.hpp>
|
|
#include <timemory/math.hpp>
|
|
#include <timemory/mpl.hpp>
|
|
#include <timemory/mpl/quirks.hpp>
|
|
#include <timemory/mpl/type_traits.hpp>
|
|
#include <timemory/operations.hpp>
|
|
#include <timemory/sampling/allocator.hpp>
|
|
#include <timemory/sampling/sampler.hpp>
|
|
#include <timemory/storage.hpp>
|
|
#include <timemory/units.hpp>
|
|
#include <timemory/utility/backtrace.hpp>
|
|
#include <timemory/utility/demangle.hpp>
|
|
#include <timemory/utility/procfs/maps.hpp>
|
|
#include <timemory/utility/types.hpp>
|
|
#include <timemory/variadic.hpp>
|
|
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <condition_variable>
|
|
#include <csignal>
|
|
#include <cstring>
|
|
#include <ctime>
|
|
#include <initializer_list>
|
|
#include <mutex>
|
|
#include <regex>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <type_traits>
|
|
|
|
#include <pthread.h>
|
|
#include <signal.h>
|
|
|
|
namespace tim
|
|
{
|
|
namespace math
|
|
{
|
|
template <typename Tp, typename Up>
|
|
TIMEMORY_INLINE Tp
|
|
plus(Tp&& _lhs, const Up& _rhs)
|
|
{
|
|
Tp _v = _lhs;
|
|
plus(_v, _rhs);
|
|
return _v;
|
|
}
|
|
} // namespace math
|
|
} // namespace tim
|
|
namespace omnitrace
|
|
{
|
|
namespace sampling
|
|
{
|
|
using ::tim::sampling::dynamic;
|
|
using tim::sampling::timer;
|
|
|
|
using hw_counters = typename component::backtrace_metrics::hw_counters;
|
|
using signal_type_instances = thread_data<std::set<int>, category::sampling>;
|
|
using sampler_running_instances = thread_data<bool, category::sampling>;
|
|
using bundle_t =
|
|
tim::lightweight_tuple<component::backtrace_timestamp, component::backtrace,
|
|
component::backtrace_metrics>;
|
|
using sampler_t = tim::sampling::sampler<bundle_t, dynamic>;
|
|
using sampler_instances = thread_data<sampler_t, category::sampling>;
|
|
using sampler_init_instances = thread_data<bundle_t, category::sampling>;
|
|
} // namespace sampling
|
|
} // namespace omnitrace
|
|
|
|
OMNITRACE_DEFINE_CONCRETE_TRAIT(prevent_reentry, sampling::sampler_t, std::true_type)
|
|
|
|
OMNITRACE_DEFINE_CONCRETE_TRAIT(provide_backtrace, sampling::sampler_t, std::false_type)
|
|
|
|
OMNITRACE_DEFINE_CONCRETE_TRAIT(buffer_size, sampling::sampler_t,
|
|
TIMEMORY_ESC(std::integral_constant<size_t, 1024>))
|
|
|
|
namespace omnitrace
|
|
{
|
|
namespace sampling
|
|
{
|
|
namespace
|
|
{
|
|
using sampler_allocator_t = typename sampler_t::allocator_t;
|
|
|
|
auto&
|
|
get_sampler_allocators()
|
|
{
|
|
static auto _v = std::vector<std::shared_ptr<sampler_allocator_t>>{};
|
|
return _v;
|
|
}
|
|
|
|
std::set<int>
|
|
configure(bool _setup, int64_t _tid = threading::get_id());
|
|
|
|
void
|
|
configure_sampler_allocator(std::shared_ptr<sampler_allocator_t>& _v)
|
|
{
|
|
if(_v) return;
|
|
|
|
OMNITRACE_SCOPED_SAMPLING_ON_CHILD_THREADS(false);
|
|
OMNITRACE_SCOPED_THREAD_STATE(ThreadState::Internal);
|
|
|
|
_v = std::make_shared<sampler_allocator_t>();
|
|
_v->reserve(config::get_sampling_allocator_size());
|
|
}
|
|
|
|
void
|
|
configure_sampler_allocators()
|
|
{
|
|
auto& _allocators = get_sampler_allocators();
|
|
if(_allocators.empty())
|
|
{
|
|
// avoid lock until necessary
|
|
auto_lock_t _alloc_lk{ type_mutex<decltype(_allocators)>() };
|
|
if(_allocators.empty())
|
|
{
|
|
_allocators.resize(std::ceil(config::get_num_threads_hint() /
|
|
config::get_sampling_allocator_size()));
|
|
for(auto& itr : _allocators)
|
|
configure_sampler_allocator(itr);
|
|
}
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<sampler_allocator_t>
|
|
get_sampler_allocator()
|
|
{
|
|
configure_sampler_allocators();
|
|
|
|
auto& _allocators = get_sampler_allocators();
|
|
|
|
OMNITRACE_SCOPED_THREAD_STATE(ThreadState::Internal);
|
|
|
|
auto_lock_t _lk{ type_mutex<sampler_allocator_t>() };
|
|
|
|
for(auto& itr : _allocators)
|
|
{
|
|
if(!itr) configure_sampler_allocator(itr);
|
|
if(itr->size() < config::get_sampling_allocator_size()) return itr;
|
|
}
|
|
|
|
auto& _v = _allocators.emplace_back();
|
|
configure_sampler_allocator(_v);
|
|
return _v;
|
|
}
|
|
|
|
template <typename... Args>
|
|
void
|
|
thread_sigmask(Args... _args)
|
|
{
|
|
auto _err = pthread_sigmask(_args...);
|
|
if(_err != 0)
|
|
{
|
|
errno = _err;
|
|
perror("pthread_sigmask");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
template <typename Tp>
|
|
sigset_t
|
|
get_signal_set(Tp&& _v)
|
|
{
|
|
sigset_t _sigset;
|
|
sigemptyset(&_sigset);
|
|
for(auto itr : _v)
|
|
sigaddset(&_sigset, itr);
|
|
return _sigset;
|
|
}
|
|
|
|
template <typename Tp>
|
|
std::string
|
|
get_signal_names(Tp&& _v)
|
|
{
|
|
std::string _sig_names{};
|
|
for(auto&& itr : _v)
|
|
_sig_names += std::get<0>(tim::signals::signal_settings::get_info(
|
|
static_cast<tim::signals::sys_signal>(itr))) +
|
|
" ";
|
|
return (_sig_names.empty()) ? _sig_names
|
|
: _sig_names.substr(0, _sig_names.length() - 1);
|
|
}
|
|
|
|
unique_ptr_t<sampler_t>&
|
|
get_sampler(int64_t _tid = threading::get_id())
|
|
{
|
|
static auto& _v = sampler_instances::instances();
|
|
return _v.at(_tid);
|
|
}
|
|
|
|
unique_ptr_t<bundle_t>&
|
|
get_sampler_init(int64_t _tid = threading::get_id())
|
|
{
|
|
static auto& _v = sampler_init_instances::instances();
|
|
if(!_v.at(_tid)) _v.at(_tid) = unique_ptr_t<bundle_t>{ new bundle_t{} };
|
|
return _v.at(_tid);
|
|
}
|
|
|
|
unique_ptr_t<bool>&
|
|
get_sampler_running(int64_t _tid)
|
|
{
|
|
static auto& _v = sampler_running_instances::instances(construct_on_init{}, false);
|
|
return _v.at(_tid);
|
|
}
|
|
|
|
auto&
|
|
get_duration_disabled()
|
|
{
|
|
static auto _v = std::atomic<bool>{ false };
|
|
return _v;
|
|
}
|
|
|
|
auto&
|
|
get_is_duration_thread()
|
|
{
|
|
static thread_local auto _v = false;
|
|
return _v;
|
|
}
|
|
|
|
auto&
|
|
get_duration_cv()
|
|
{
|
|
static auto _v = std::condition_variable{};
|
|
return _v;
|
|
}
|
|
|
|
auto&
|
|
get_duration_mutex()
|
|
{
|
|
static auto _v = std::mutex{};
|
|
return _v;
|
|
}
|
|
|
|
auto&
|
|
get_duration_thread()
|
|
{
|
|
static auto _v = std::unique_ptr<std::thread>{};
|
|
return _v;
|
|
}
|
|
|
|
auto
|
|
notify_duration_thread()
|
|
{
|
|
if(get_duration_thread() && !get_is_duration_thread())
|
|
{
|
|
std::unique_lock<std::mutex> _lk{ get_duration_mutex(), std::defer_lock };
|
|
if(!_lk.owns_lock()) _lk.lock();
|
|
get_duration_cv().notify_all();
|
|
}
|
|
}
|
|
|
|
void
|
|
stop_duration_thread()
|
|
{
|
|
if(get_duration_thread() && !get_is_duration_thread())
|
|
{
|
|
notify_duration_thread();
|
|
get_duration_thread()->join();
|
|
get_duration_thread().reset();
|
|
}
|
|
}
|
|
|
|
void
|
|
start_duration_thread()
|
|
{
|
|
static std::mutex _start_mutex{};
|
|
std::unique_lock<std::mutex> _start_lk{ _start_mutex, std::defer_lock };
|
|
if(!_start_lk.owns_lock()) _start_lk.lock();
|
|
|
|
if(!get_duration_thread() && config::get_sampling_duration() > 0.0)
|
|
{
|
|
// we may need to protect against recursion bc of pthread wrapper
|
|
static bool _protect = false;
|
|
if(_protect) return;
|
|
_protect = true;
|
|
auto _now = std::chrono::steady_clock::now();
|
|
auto _end = _now + std::chrono::nanoseconds{ static_cast<uint64_t>(
|
|
config::get_sampling_duration() * units::sec) };
|
|
auto _func = [_end]() {
|
|
thread_info::init(true);
|
|
threading::set_thread_name("omni.samp.dur");
|
|
get_is_duration_thread() = true;
|
|
bool _wait = true;
|
|
while(_wait)
|
|
{
|
|
_wait = false;
|
|
std::unique_lock<std::mutex> _lk{ get_duration_mutex(), std::defer_lock };
|
|
if(!_lk.owns_lock()) _lk.lock();
|
|
get_duration_cv().wait_until(_lk, _end);
|
|
auto _premature = (std::chrono::steady_clock::now() < _end);
|
|
auto _finalized = (get_state() == State::Finalized);
|
|
if(_premature && !_finalized)
|
|
{
|
|
// protect against spurious wakeups
|
|
OMNITRACE_VERBOSE(
|
|
2, "%sSpurious wakeup of sampling duration thread...\n",
|
|
tim::log::color::warning());
|
|
_wait = true;
|
|
}
|
|
else if(_finalized)
|
|
{
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
get_duration_disabled().store(true);
|
|
OMNITRACE_VERBOSE(1,
|
|
"Sampling duration of %f seconds has elapsed. "
|
|
"Shutting down sampling...\n",
|
|
config::get_sampling_duration());
|
|
configure(false, 0);
|
|
}
|
|
}
|
|
};
|
|
|
|
OMNITRACE_VERBOSE(1, "Sampling will be disabled after %f seconds...\n",
|
|
config::get_sampling_duration());
|
|
|
|
OMNITRACE_SCOPED_SAMPLING_ON_CHILD_THREADS(false);
|
|
get_duration_thread() = std::make_unique<std::thread>(_func);
|
|
_protect = false;
|
|
}
|
|
}
|
|
|
|
auto&
|
|
get_offload_file()
|
|
{
|
|
static auto _v = config::get_tmp_file("sampling");
|
|
return _v;
|
|
}
|
|
|
|
locking::atomic_mutex&
|
|
get_offload_mutex()
|
|
{
|
|
static auto _v = locking::atomic_mutex{};
|
|
return _v;
|
|
}
|
|
|
|
using sampler_bundle_t = typename sampler_t::bundle_type;
|
|
using sampler_buffer_t = tim::data_storage::ring_buffer<sampler_bundle_t>;
|
|
|
|
void
|
|
offload_buffer(int64_t _seq, sampler_buffer_t&& _buf)
|
|
{
|
|
OMNITRACE_REQUIRE(get_use_tmp_files())
|
|
<< "Error! sampling allocator tries to offload buffer of samples but "
|
|
"omnitrace was configured to not use temporary files\n";
|
|
|
|
// use homemade atomic_mutex/atomic_lock since contention will be low
|
|
// and using pthread_lock might trigger our wrappers
|
|
auto _lk = locking::atomic_lock{ get_offload_mutex() };
|
|
auto& _file = get_offload_file();
|
|
|
|
OMNITRACE_REQUIRE(_file)
|
|
<< "Error! sampling allocator tried to offload buffer of samples but the "
|
|
"offload file does not exist\n";
|
|
|
|
OMNITRACE_VERBOSE_F(3, "Saving sampling buffer for thread %li...\n", _seq);
|
|
auto& _fs = _file->stream;
|
|
|
|
OMNITRACE_REQUIRE(_fs.good())
|
|
<< "Error! temporary file for offloading buffer is in an invalid state\n";
|
|
|
|
_fs.write(reinterpret_cast<char*>(&_seq), sizeof(_seq));
|
|
auto _data = std::move(_buf);
|
|
_data.save(_fs);
|
|
_data.destroy();
|
|
_buf.destroy();
|
|
}
|
|
|
|
auto
|
|
load_offload_buffer()
|
|
{
|
|
auto _data = std::map<int64_t, std::vector<sampler_buffer_t>>{};
|
|
if(!get_use_tmp_files()) return _data;
|
|
|
|
// use homemade atomic_mutex/atomic_lock since contention will be low
|
|
// and using pthread_lock might trigger our wrappers
|
|
auto _lk = locking::atomic_lock{ get_offload_mutex() };
|
|
auto& _file = get_offload_file();
|
|
if(!_file)
|
|
{
|
|
OMNITRACE_WARNING_F(
|
|
0, "[sampling] returning no data because the offload file no longer exists");
|
|
return _data;
|
|
}
|
|
|
|
auto& _fs = _file->stream;
|
|
|
|
_fs.close();
|
|
_file->open(std::ios::binary | std::ios::in);
|
|
|
|
if(!_fs)
|
|
{
|
|
OMNITRACE_WARNING_F(0, "[sampling] %s failed to open", _file->filename.c_str());
|
|
}
|
|
|
|
while(!_fs.eof())
|
|
{
|
|
int64_t _seq = 0;
|
|
_fs.read(reinterpret_cast<char*>(&_seq), sizeof(_seq));
|
|
if(_fs.eof()) break;
|
|
sampler_buffer_t _buffer{};
|
|
_buffer.load(_fs);
|
|
OMNITRACE_VERBOSE_F(2, "Loading %zu samples for thread %li...\n", _buffer.count(),
|
|
_seq);
|
|
_data[_seq].emplace_back(std::move(_buffer));
|
|
}
|
|
_file.reset();
|
|
|
|
return _data;
|
|
}
|
|
|
|
std::set<int>
|
|
configure(bool _setup, int64_t _tid)
|
|
{
|
|
const auto& _info = thread_info::get(_tid, SequentTID);
|
|
auto& _sampler = sampling::get_sampler(_tid);
|
|
auto& _running = get_sampler_running(_tid);
|
|
bool _is_running = (!_running) ? false : *_running;
|
|
auto& _signal_types = sampling::get_signal_types(_tid);
|
|
|
|
OMNITRACE_CONDITIONAL_THROW(get_use_causal(),
|
|
"Internal error! configuring sampling not permitted when "
|
|
"causal profiling is enabled");
|
|
|
|
OMNITRACE_SCOPED_SAMPLING_ON_CHILD_THREADS(false);
|
|
|
|
auto&& _cpu_tids = get_sampling_cpu_tids();
|
|
auto&& _real_tids = get_sampling_real_tids();
|
|
|
|
auto _erase_tid_signal = [_tid, &_signal_types](auto& _tids, int _signum) {
|
|
if(!_tids.empty())
|
|
{
|
|
if(_tids.count(_tid) == 0)
|
|
{
|
|
OMNITRACE_VERBOSE(3, "Disabling SIG%i from thread %li\n", _signum, _tid);
|
|
_signal_types->erase(_signum);
|
|
}
|
|
}
|
|
};
|
|
|
|
_erase_tid_signal(_cpu_tids, get_cputime_signal());
|
|
_erase_tid_signal(_real_tids, get_realtime_signal());
|
|
|
|
if(_setup && !_sampler && !_is_running && !_signal_types->empty())
|
|
{
|
|
if(get_duration_disabled()) return std::set<int>{};
|
|
|
|
// if this thread has an offset ID, that means it was created internally
|
|
// and is probably here bc it called a function which was instrumented.
|
|
// thus we should not start a sampler for it
|
|
if(_tid > 0 && _info && _info->is_offset) return std::set<int>{};
|
|
// if the thread state is disabled or completed, return
|
|
if(_info && _info->index_data->sequent_value == _tid &&
|
|
get_thread_state() == ThreadState::Disabled)
|
|
return std::set<int>{};
|
|
|
|
(void) get_debug_sampling(); // make sure query in sampler does not allocate
|
|
assert(_tid == threading::get_id());
|
|
|
|
if(trait::runtime_enabled<backtrace_metrics>::get())
|
|
backtrace_metrics::configure(_setup, _tid);
|
|
|
|
// NOTE: signals need to be unblocked by calling function
|
|
sampling::block_signals(*_signal_types);
|
|
|
|
auto _verbose = std::min<int>(get_verbose() - 2, 2);
|
|
if(get_debug_sampling()) _verbose = 2;
|
|
|
|
OMNITRACE_DEBUG("Requesting allocator for sampler on thread %lu...\n", _tid);
|
|
auto _alloc = get_sampler_allocator();
|
|
|
|
OMNITRACE_DEBUG("Configuring sampler for thread %lu...\n", _tid);
|
|
sampling::sampler_instances::construct(construct_on_thread{ _tid }, _alloc,
|
|
"omnitrace", _tid, _verbose);
|
|
|
|
_sampler->set_flags(SA_RESTART);
|
|
_sampler->set_verbose(_verbose);
|
|
|
|
if(_signal_types->count(get_realtime_signal()) > 0)
|
|
{
|
|
_sampler->configure(timer{ get_realtime_signal(), CLOCK_REALTIME,
|
|
SIGEV_THREAD_ID, get_sampling_real_freq(),
|
|
get_sampling_real_delay(), _tid,
|
|
threading::get_sys_tid() });
|
|
}
|
|
|
|
if(_signal_types->count(get_cputime_signal()) > 0)
|
|
{
|
|
_sampler->configure(timer{ get_cputime_signal(), CLOCK_THREAD_CPUTIME_ID,
|
|
SIGEV_THREAD_ID, get_sampling_cpu_freq(),
|
|
get_sampling_cpu_delay(), _tid,
|
|
threading::get_sys_tid() });
|
|
}
|
|
|
|
if(get_use_tmp_files())
|
|
{
|
|
auto _file = get_offload_file();
|
|
if(_file && *_file) _sampler->set_offload(&offload_buffer);
|
|
}
|
|
|
|
static_assert(tim::trait::buffer_size<sampling::sampler_t>::value > 0,
|
|
"Error! Zero buffer size");
|
|
|
|
OMNITRACE_CONDITIONAL_THROW(
|
|
_sampler->get_buffer_size() !=
|
|
tim::trait::buffer_size<sampling::sampler_t>::value,
|
|
"dynamic sampler has a buffer size different from static trait: %zu instead "
|
|
"of %zu",
|
|
_sampler->get_buffer_size(),
|
|
tim::trait::buffer_size<sampling::sampler_t>::value);
|
|
|
|
OMNITRACE_CONDITIONAL_THROW(
|
|
_sampler->get_buffer_size() <= 0,
|
|
"dynamic sampler requires a positive buffer size: %zu",
|
|
_sampler->get_buffer_size());
|
|
|
|
for(auto itr : *_signal_types)
|
|
{
|
|
const char* _type = (itr == get_realtime_signal()) ? "wall" : "CPU";
|
|
const auto* _timer = _sampler->get_timer(itr);
|
|
if(_timer)
|
|
{
|
|
OMNITRACE_VERBOSE(
|
|
2,
|
|
"[SIG%i] Sampler for thread %lu will be triggered %.1fx per "
|
|
"second of %s-time (every %.3e milliseconds)...\n",
|
|
itr, _tid, _timer->get_frequency(units::sec), _type,
|
|
_timer->get_period(units::msec));
|
|
}
|
|
}
|
|
|
|
*_running = true;
|
|
sampling::get_sampler_init(_tid)->sample();
|
|
start_duration_thread();
|
|
_sampler->start();
|
|
}
|
|
else if(!_setup && _sampler && _is_running)
|
|
{
|
|
OMNITRACE_DEBUG("Destroying sampler for thread %lu...\n", _tid);
|
|
*_running = false;
|
|
|
|
if(_tid == threading::get_id() && !_signal_types->empty())
|
|
{
|
|
sampling::block_signals(*_signal_types);
|
|
}
|
|
|
|
notify_duration_thread();
|
|
|
|
if(_tid == 0)
|
|
{
|
|
block_samples();
|
|
|
|
// this propagates to all threads
|
|
_sampler->ignore(*_signal_types);
|
|
|
|
// wait for the samples to finish
|
|
auto _freq =
|
|
std::max<double>(get_sampling_cpu_freq(), get_sampling_real_freq());
|
|
auto _period = (1.0 / _freq) * units::sec;
|
|
_period = std::min<double>(_period, 1.0e9); // max of 1 second
|
|
std::this_thread::sleep_for(
|
|
std::chrono::nanoseconds{ static_cast<int64_t>(_period) });
|
|
|
|
for(int64_t i = 1; i < OMNITRACE_MAX_THREADS; ++i)
|
|
{
|
|
if(sampling::get_sampler(i)) sampling::get_sampler(i)->stop();
|
|
}
|
|
|
|
for(int64_t i = 1; i < OMNITRACE_MAX_THREADS; ++i)
|
|
{
|
|
if(sampling::get_sampler(i))
|
|
{
|
|
sampling::get_sampler(i)->reset();
|
|
*get_sampler_running(i) = false;
|
|
}
|
|
}
|
|
|
|
stop_duration_thread();
|
|
}
|
|
|
|
_sampler->stop();
|
|
|
|
if(trait::runtime_enabled<backtrace_metrics>::get())
|
|
backtrace_metrics::configure(_setup, _tid);
|
|
|
|
OMNITRACE_DEBUG("Sampler destroyed for thread %lu\n", _tid);
|
|
}
|
|
|
|
return (_signal_types) ? *_signal_types : std::set<int>{};
|
|
}
|
|
|
|
void
|
|
post_process_perfetto(int64_t _tid, const bundle_t* _init,
|
|
const std::vector<bundle_t*>& _data);
|
|
void
|
|
post_process_timemory(int64_t _tid, const bundle_t* _init,
|
|
const std::vector<bundle_t*>& _data);
|
|
} // namespace
|
|
|
|
unique_ptr_t<std::set<int>>&
|
|
get_signal_types(int64_t _tid)
|
|
{
|
|
static auto& _v = signal_type_instances::instances();
|
|
signal_type_instances::construct(construct_on_thread{ _tid },
|
|
omnitrace::get_sampling_signals(_tid));
|
|
return _v.at(_tid);
|
|
}
|
|
|
|
std::set<int>
|
|
setup()
|
|
{
|
|
if(!get_use_sampling()) return std::set<int>{};
|
|
return configure(true);
|
|
}
|
|
|
|
std::set<int>
|
|
shutdown()
|
|
{
|
|
auto _v = configure(false);
|
|
if(utility::get_thread_index() == 0) stop_duration_thread();
|
|
return _v;
|
|
}
|
|
|
|
void
|
|
block_samples()
|
|
{
|
|
trait::runtime_enabled<sampler_t>::set(false);
|
|
}
|
|
|
|
void
|
|
unblock_samples()
|
|
{
|
|
trait::runtime_enabled<sampler_t>::set(true);
|
|
}
|
|
|
|
void
|
|
block_signals(std::set<int> _signals)
|
|
{
|
|
if(_signals.empty()) _signals = *get_signal_types(threading::get_id());
|
|
if(_signals.empty())
|
|
{
|
|
OMNITRACE_PRINT("No signals to block...\n");
|
|
return;
|
|
}
|
|
|
|
OMNITRACE_DEBUG("Blocking signals [%s] on thread #%lu...\n",
|
|
get_signal_names(_signals).c_str(), threading::get_id());
|
|
|
|
sigset_t _v = get_signal_set(_signals);
|
|
thread_sigmask(SIG_BLOCK, &_v, nullptr);
|
|
}
|
|
|
|
void
|
|
unblock_signals(std::set<int> _signals)
|
|
{
|
|
if(_signals.empty()) _signals = *get_signal_types(threading::get_id());
|
|
if(_signals.empty())
|
|
{
|
|
OMNITRACE_PRINT("No signals to unblock...\n");
|
|
return;
|
|
}
|
|
|
|
OMNITRACE_DEBUG("Unblocking signals [%s] on thread #%lu...\n",
|
|
get_signal_names(_signals).c_str(), threading::get_id());
|
|
|
|
sigset_t _v = get_signal_set(_signals);
|
|
thread_sigmask(SIG_UNBLOCK, &_v, nullptr);
|
|
}
|
|
|
|
void
|
|
post_process()
|
|
{
|
|
OMNITRACE_SCOPED_THREAD_STATE(ThreadState::Internal);
|
|
|
|
size_t _total_data = 0;
|
|
size_t _total_threads = 0;
|
|
auto _external_samples = std::atomic<size_t>{ 0 };
|
|
auto _internal_samples = std::atomic<size_t>{ 0 };
|
|
|
|
OMNITRACE_VERBOSE(2 || get_debug_sampling(), "Stopping sampling components...\n");
|
|
omnitrace::component::backtrace::stop();
|
|
for(size_t i = 0; i < max_supported_threads; ++i)
|
|
{
|
|
backtrace_metrics::configure(false, i);
|
|
auto& _sampler = get_sampler(i);
|
|
if(_sampler)
|
|
{
|
|
_sampler->stop();
|
|
_sampler->set_offload(nullptr);
|
|
}
|
|
}
|
|
|
|
auto _loaded_data = load_offload_buffer();
|
|
for(size_t i = 0; i < max_supported_threads; ++i)
|
|
{
|
|
auto& _sampler = get_sampler(i);
|
|
|
|
if(!_sampler)
|
|
{
|
|
// this should be relatively common
|
|
OMNITRACE_CONDITIONAL_PRINT(
|
|
get_debug() && get_verbose() >= 2,
|
|
"Post-processing sampling entries for thread %lu skipped (no sampler)\n",
|
|
i);
|
|
continue;
|
|
}
|
|
|
|
auto* _init = get_sampler_init(i).get();
|
|
|
|
if(!_init)
|
|
{
|
|
// this is not common
|
|
OMNITRACE_PRINT("Post-processing sampling entries for thread %lu skipped "
|
|
"(not initialized)\n",
|
|
i);
|
|
continue;
|
|
}
|
|
|
|
const auto& _thread_info = thread_info::get(i, SequentTID);
|
|
|
|
OMNITRACE_VERBOSE(3 || get_debug_sampling(),
|
|
"Getting sampler data for thread %lu...\n", i);
|
|
|
|
auto _raw_data = _sampler->get_data();
|
|
for(auto litr : _loaded_data[i])
|
|
{
|
|
while(!litr.is_empty())
|
|
{
|
|
auto _v = sampler_bundle_t{};
|
|
litr.read(&_v);
|
|
_raw_data.emplace_back(std::move(_v));
|
|
}
|
|
litr.destroy();
|
|
}
|
|
|
|
OMNITRACE_VERBOSE(2 || get_debug_sampling(),
|
|
"Sampler data for thread %lu has %zu initial entries...\n", i,
|
|
_raw_data.size());
|
|
|
|
OMNITRACE_CI_THROW(
|
|
_sampler->get_sample_count() != _raw_data.size(),
|
|
"Error! sampler recorded %zu samples but %zu samples were returned\n",
|
|
_sampler->get_sample_count(), _raw_data.size());
|
|
// single sample that is useless (backtrace to unblocking signals)
|
|
if(_raw_data.size() == 1 && _raw_data.front().size() <= 1) _raw_data.clear();
|
|
|
|
std::vector<sampling::bundle_t*> _data{};
|
|
for(auto& itr : _raw_data)
|
|
{
|
|
auto* _bt = itr.get<backtrace>();
|
|
auto* _ts = itr.get<backtrace_timestamp>();
|
|
if(_thread_info && _bt && !_bt->empty() && _ts &&
|
|
_thread_info->is_valid_time(_ts->get_timestamp()))
|
|
{
|
|
_data.emplace_back(&itr);
|
|
}
|
|
}
|
|
|
|
_total_data += _data.size();
|
|
_total_threads += (!_data.empty()) ? 1 : 0;
|
|
|
|
if(!_data.empty())
|
|
{
|
|
OMNITRACE_VERBOSE(2 || get_debug_sampling(),
|
|
"Sampler data for thread %lu has %zu valid entries...\n", i,
|
|
_data.size());
|
|
|
|
if(get_use_perfetto()) post_process_perfetto(i, _init, _data);
|
|
if(get_use_timemory()) post_process_timemory(i, _init, _data);
|
|
}
|
|
else
|
|
{
|
|
OMNITRACE_VERBOSE(2 || get_debug_sampling(),
|
|
"Sampler data for thread %lu has zero valid entries out of "
|
|
"%zu... (skipped)\n",
|
|
i, _raw_data.size());
|
|
}
|
|
}
|
|
|
|
OMNITRACE_VERBOSE(3 || get_debug_sampling(),
|
|
"Destroying samplers and allocators...\n");
|
|
|
|
for(size_t i = 0; i < max_supported_threads; ++i)
|
|
get_sampler(i).reset();
|
|
|
|
for(auto& itr : get_sampler_allocators())
|
|
{
|
|
if(itr) itr.reset();
|
|
}
|
|
|
|
if(get_offload_file())
|
|
{
|
|
get_offload_file()->remove();
|
|
get_offload_file().reset();
|
|
}
|
|
|
|
OMNITRACE_VERBOSE(1 || get_debug_sampling(),
|
|
"Collected %zu samples from %zu threads... %zu samples out of %zu "
|
|
"were taken while within instrumented routines\n",
|
|
_total_data, _total_threads, _internal_samples.load(),
|
|
(_internal_samples + _external_samples));
|
|
}
|
|
|
|
namespace
|
|
{
|
|
void
|
|
post_process_perfetto(int64_t _tid, const bundle_t* _init,
|
|
const std::vector<bundle_t*>& _data)
|
|
{
|
|
if(trait::runtime_enabled<backtrace_metrics>::get())
|
|
{
|
|
OMNITRACE_VERBOSE(3 || get_debug_sampling(),
|
|
"[%li] Post-processing metrics for perfetto...\n", _tid);
|
|
backtrace_metrics::init_perfetto(_tid);
|
|
for(const auto& itr : _data)
|
|
{
|
|
const auto* _bt_metrics = itr->get<backtrace_metrics>();
|
|
const auto* _bt_time = itr->get<backtrace_timestamp>();
|
|
if(!_bt_metrics || !_bt_time) continue;
|
|
if(_bt_time->get_tid() != _tid) continue;
|
|
_bt_metrics->post_process_perfetto(_tid, _bt_time->get_timestamp());
|
|
}
|
|
|
|
backtrace_metrics::fini_perfetto(_tid);
|
|
}
|
|
|
|
OMNITRACE_VERBOSE(3 || get_debug_sampling(),
|
|
"[%li] Post-processing backtraces for perfetto...\n", _tid);
|
|
|
|
const auto& _thread_info = thread_info::get(_tid, SequentTID);
|
|
OMNITRACE_CI_THROW(!_thread_info, "No valid thread info for tid=%li\n", _tid);
|
|
|
|
if(!_thread_info) return;
|
|
|
|
uint64_t _beg_ns = _thread_info->get_start();
|
|
uint64_t _end_ns = _thread_info->get_stop();
|
|
uint64_t _last_ts =
|
|
std::max<uint64_t>(_init->get<backtrace_timestamp>()->get_timestamp(), _beg_ns);
|
|
|
|
auto _track = tracing::get_perfetto_track(
|
|
category::sampling{},
|
|
[](auto _seq_id, auto _sys_id) {
|
|
return TIMEMORY_JOIN(" ", "Thread", _seq_id, "(S)", _sys_id);
|
|
},
|
|
_thread_info->index_data->sequent_value, _thread_info->index_data->system_value);
|
|
|
|
tracing::push_perfetto_track(category::sampling{}, "samples [omnitrace]", _track,
|
|
_beg_ns, [&](perfetto::EventContext ctx) {
|
|
if(config::get_perfetto_annotations())
|
|
{
|
|
tracing::add_perfetto_annotation(ctx, "begin_ns",
|
|
_beg_ns);
|
|
}
|
|
});
|
|
|
|
auto _as_hex = [](auto _v) { return JOIN("", "0x", std::hex, _v); };
|
|
|
|
auto _labels = backtrace_metrics::get_hw_counter_labels(_tid);
|
|
for(const auto& itr : _data)
|
|
{
|
|
const auto* _bt_ts = itr->get<backtrace_timestamp>();
|
|
const auto* _bt_cs = itr->get<backtrace>();
|
|
const auto* _bt_mt = itr->get<backtrace_metrics>();
|
|
|
|
if(!_bt_ts || !_bt_cs || !_bt_mt) continue;
|
|
if(_bt_ts->get_tid() != _tid) continue;
|
|
|
|
static std::set<std::string> _static_strings{};
|
|
const auto* _last = _init;
|
|
auto _patched_data = backtrace::filter_and_patch(_bt_cs->get());
|
|
size_t _ncount = 0;
|
|
for(const auto& iitr : _patched_data)
|
|
{
|
|
uint64_t _beg = _last_ts;
|
|
uint64_t _end = _bt_ts->get_timestamp();
|
|
if(!_thread_info->is_valid_lifetime({ _beg, _end })) continue;
|
|
|
|
auto _ncur = _ncount++;
|
|
// the begin/end + HW counters will be same for entire call-stack so only
|
|
// annotate
|
|
// the top and the bottom functons to keep the data consumption low
|
|
bool _include_common = (_ncur == 0 || _ncur + 1 == _patched_data.size());
|
|
|
|
// Only annotate HW counters when:
|
|
// 1. when we can compute a difference from the last sample
|
|
// 2. when the number of HW counters b/t this sample and last are the same
|
|
bool _include_hw =
|
|
_include_common && (_last != nullptr) &&
|
|
_bt_mt->get_hw_counters().size() ==
|
|
_last->get<backtrace_metrics>()->get_hw_counters().size();
|
|
|
|
// annotations common to both modes
|
|
auto _common_annotate = [&](::perfetto::EventContext& ctx, bool _is_last) {
|
|
if(_include_common && _is_last)
|
|
{
|
|
tracing::add_perfetto_annotation(ctx, "begin_ns", _beg);
|
|
tracing::add_perfetto_annotation(ctx, "end_ns", _end);
|
|
}
|
|
if(_include_hw && _is_last)
|
|
{
|
|
// current values when read
|
|
auto _hw_cnt_vals = _bt_mt->get_hw_counters();
|
|
// compute difference from last sample to provide the HW counters for
|
|
// this sample
|
|
tim::math::minus(_hw_cnt_vals,
|
|
_last->get<backtrace_metrics>()->get_hw_counters());
|
|
for(size_t i = 0; i < _labels.size(); ++i)
|
|
tracing::add_perfetto_annotation(ctx, _labels.at(i),
|
|
_hw_cnt_vals.at(i));
|
|
}
|
|
};
|
|
|
|
if(get_sampling_include_inlines() && iitr.lineinfo)
|
|
{
|
|
auto _lines = iitr.lineinfo.lines;
|
|
std::reverse(_lines.begin(), _lines.end());
|
|
size_t _n = 0;
|
|
for(const auto& litr : _lines)
|
|
{
|
|
const auto* _name =
|
|
_static_strings.emplace(demangle(litr.name)).first->c_str();
|
|
auto _info = JOIN(':', litr.location, litr.line);
|
|
tracing::push_perfetto_track(
|
|
category::sampling{}, _name, _track, _beg,
|
|
[&](perfetto::EventContext ctx) {
|
|
if(config::get_perfetto_annotations())
|
|
{
|
|
_common_annotate(ctx, (_n == 0 && _ncur == 0) ||
|
|
(_n + 1 == _lines.size()));
|
|
tracing::add_perfetto_annotation(ctx, "file",
|
|
iitr.location);
|
|
tracing::add_perfetto_annotation(ctx, "lineinfo", _info);
|
|
tracing::add_perfetto_annotation(ctx, "inlined",
|
|
(_n++ > 0));
|
|
}
|
|
});
|
|
tracing::pop_perfetto_track(category::sampling{}, _name, _track,
|
|
_end);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
const auto* _name = _static_strings.emplace(iitr.name).first->c_str();
|
|
tracing::push_perfetto_track(
|
|
category::sampling{}, _name, _track, _beg,
|
|
[&](perfetto::EventContext ctx) {
|
|
if(config::get_perfetto_annotations())
|
|
{
|
|
_common_annotate(ctx, true);
|
|
tracing::add_perfetto_annotation(ctx, "file", iitr.location);
|
|
tracing::add_perfetto_annotation(ctx, "pc",
|
|
_as_hex(iitr.address));
|
|
tracing::add_perfetto_annotation(ctx, "line_address",
|
|
_as_hex(iitr.line_address));
|
|
if(iitr.lineinfo)
|
|
{
|
|
auto _lines = iitr.lineinfo.lines;
|
|
std::reverse(_lines.begin(), _lines.end());
|
|
size_t _n = 0;
|
|
for(const auto& litr : _lines)
|
|
{
|
|
auto _label = JOIN('-', "lineinfo", _n++);
|
|
tracing::add_perfetto_annotation(
|
|
ctx, _label.c_str(),
|
|
JOIN('@', demangle(litr.name),
|
|
JOIN(':', litr.location, litr.line)));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
tracing::pop_perfetto_track(category::sampling{}, _name, _track, _end);
|
|
}
|
|
}
|
|
_last_ts = _bt_ts->get_timestamp();
|
|
_last = itr;
|
|
}
|
|
|
|
tracing::pop_perfetto_track(category::sampling{}, "samples [omnitrace]", _track,
|
|
_end_ns, [&](perfetto::EventContext ctx) {
|
|
if(config::get_perfetto_annotations())
|
|
{
|
|
tracing::add_perfetto_annotation(ctx, "end_ns",
|
|
_end_ns);
|
|
}
|
|
});
|
|
}
|
|
|
|
void
|
|
post_process_timemory(int64_t _tid, const bundle_t* _init,
|
|
const std::vector<bundle_t*>& _data)
|
|
{
|
|
auto _depth_sum = std::map<int64_t, std::map<int64_t, int64_t>>{};
|
|
|
|
OMNITRACE_VERBOSE(3 || get_debug_sampling(),
|
|
"[%li] Post-processing data for timemory...\n", _tid);
|
|
|
|
const auto* _last = _init;
|
|
for(const auto& itr : _data)
|
|
{
|
|
using bundle_t = tim::lightweight_tuple<comp::trip_count, sampling_wall_clock,
|
|
sampling_cpu_clock, hw_counters>;
|
|
|
|
auto* _bt_data = itr->get<backtrace>();
|
|
auto* _bt_time = itr->get<backtrace_timestamp>();
|
|
auto* _bt_metrics = itr->get<backtrace_metrics>();
|
|
|
|
if(!_bt_data || !_bt_time || !_bt_metrics) continue;
|
|
|
|
double _elapsed_wc = (_bt_time->get_timestamp() -
|
|
_last->get<backtrace_timestamp>()->get_timestamp());
|
|
double _elapsed_cc = (_bt_metrics->get_cpu_timestamp() -
|
|
_last->get<backtrace_metrics>()->get_cpu_timestamp());
|
|
|
|
std::vector<bundle_t> _tc{};
|
|
_tc.reserve(_bt_data->size());
|
|
|
|
// generate the instances of the tuple of components and start them
|
|
for(const auto& iitr : backtrace::filter_and_patch(_bt_data->get()))
|
|
{
|
|
_tc.emplace_back(tim::string_view_t{ iitr.name });
|
|
_tc.back().push(_bt_time->get_tid());
|
|
_tc.back().start();
|
|
}
|
|
|
|
// stop the instances and update the values as needed
|
|
for(size_t i = 0; i < _tc.size(); ++i)
|
|
{
|
|
auto& iitr = _tc.at(_tc.size() - i - 1);
|
|
size_t _depth = 0;
|
|
_depth_sum[_bt_time->get_tid()][_depth] += 1;
|
|
iitr.stop();
|
|
if constexpr(tim::trait::is_available<sampling_wall_clock>::value)
|
|
{
|
|
auto* _sc = iitr.get<sampling_wall_clock>();
|
|
if(_sc)
|
|
{
|
|
auto _value = _elapsed_wc / sampling_wall_clock::get_unit();
|
|
_sc->set_value(_value);
|
|
_sc->set_accum(_value);
|
|
}
|
|
}
|
|
if constexpr(tim::trait::is_available<sampling_cpu_clock>::value)
|
|
{
|
|
auto* _cc = iitr.get<sampling_cpu_clock>();
|
|
if(_cc)
|
|
{
|
|
_cc->set_value(_elapsed_cc / sampling_cpu_clock::get_unit());
|
|
_cc->set_accum(_elapsed_cc / sampling_cpu_clock::get_unit());
|
|
}
|
|
}
|
|
if constexpr(tim::trait::is_available<hw_counters>::value)
|
|
{
|
|
auto _hw_cnt_vals = _bt_metrics->get_hw_counters();
|
|
if(_last && _bt_metrics->get_hw_counters().size() ==
|
|
_last->get<backtrace_metrics>()->get_hw_counters().size())
|
|
{
|
|
for(size_t k = 0; k < _bt_metrics->get_hw_counters().size(); ++k)
|
|
{
|
|
if(_last->get<backtrace_metrics>()->get_hw_counters()[k] >
|
|
_hw_cnt_vals[k])
|
|
_hw_cnt_vals[k] -=
|
|
_last->get<backtrace_metrics>()->get_hw_counters()[k];
|
|
}
|
|
}
|
|
auto* _hw_counter = iitr.get<hw_counters>();
|
|
if(_hw_counter)
|
|
{
|
|
_hw_counter->set_value(_hw_cnt_vals);
|
|
_hw_counter->set_accum(_hw_cnt_vals);
|
|
}
|
|
}
|
|
iitr.pop();
|
|
}
|
|
_last = itr;
|
|
}
|
|
|
|
for(auto&& itr : _data)
|
|
{
|
|
using bundle_t =
|
|
tim::lightweight_tuple<sampling_percent, quirk::config<quirk::tree_scope>>;
|
|
|
|
auto* _bt_data = itr->get<backtrace>();
|
|
auto* _bt_time = itr->get<backtrace_timestamp>();
|
|
|
|
if(!_bt_time || !_bt_data) continue;
|
|
if(_depth_sum.find(_bt_time->get_tid()) == _depth_sum.end()) continue;
|
|
|
|
std::vector<bundle_t> _tc{};
|
|
_tc.reserve(_bt_data->size());
|
|
|
|
// generate the instances of the tuple of components and start them
|
|
for(const auto& iitr : backtrace::filter_and_patch(_bt_data->get()))
|
|
{
|
|
_tc.emplace_back(tim::string_view_t{ iitr.name });
|
|
_tc.back().push(_bt_time->get_tid());
|
|
_tc.back().start();
|
|
}
|
|
|
|
// stop the instances and update the values as needed
|
|
for(size_t i = 0; i < _tc.size(); ++i)
|
|
{
|
|
auto& iitr = _tc.at(_tc.size() - i - 1);
|
|
size_t _depth = 0;
|
|
double _value = (1.0 / _depth_sum[_bt_time->get_tid()][_depth]) * 100.0;
|
|
iitr.store(std::plus<double>{}, _value);
|
|
iitr.stop();
|
|
iitr.pop();
|
|
}
|
|
}
|
|
}
|
|
|
|
struct sampling_initialization
|
|
{
|
|
static void preinit()
|
|
{
|
|
sampling_wall_clock::label() = "sampling_wall_clock";
|
|
sampling_wall_clock::description() = "Wall clock time (via sampling)";
|
|
|
|
sampling_cpu_clock::label() = "sampling_cpu_clock";
|
|
sampling_cpu_clock::description() = "CPU clock time (via sampling)";
|
|
|
|
sampling_percent::label() = "sampling_percent";
|
|
sampling_percent::description() = "Percentage of samples";
|
|
|
|
sampling_gpu_busy::label() = "sampling_gpu_busy_percent";
|
|
sampling_gpu_busy::description() = "Utilization of GPU(s)";
|
|
sampling_gpu_busy::set_precision(0);
|
|
sampling_gpu_busy::set_format_flags(sampling_gpu_busy::get_format_flags() &
|
|
std::ios_base::showpoint);
|
|
|
|
sampling_gpu_memory::label() = "sampling_gpu_memory_usage";
|
|
sampling_gpu_memory::description() = "Memory usage of GPU(s)";
|
|
|
|
sampling_gpu_power::label() = "sampling_gpu_power";
|
|
sampling_gpu_power::description() = "Power usage of GPU(s)";
|
|
sampling_gpu_power::unit() = units::watt;
|
|
sampling_gpu_power::display_unit() = "watts";
|
|
sampling_gpu_power::set_precision(2);
|
|
sampling_gpu_power::set_format_flags(sampling_gpu_power::get_format_flags());
|
|
|
|
sampling_gpu_temp::label() = "sampling_gpu_temperature";
|
|
sampling_gpu_temp::description() = "Temperature of GPU(s)";
|
|
sampling_gpu_temp::unit() = 1;
|
|
sampling_gpu_temp::display_unit() = "degC";
|
|
sampling_gpu_temp::set_precision(1);
|
|
sampling_gpu_temp::set_format_flags(sampling_gpu_temp::get_format_flags());
|
|
}
|
|
};
|
|
} // namespace
|
|
} // namespace sampling
|
|
} // namespace omnitrace
|
|
|
|
TIMEMORY_INVOKE_PREINIT(omnitrace::sampling::sampling_initialization)
|