2022-01-26 23:25:00 -06:00
|
|
|
// MIT License
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2022 Advanced Micro Devices, Inc. All Rights Reserved.
|
2022-01-24 20:49:17 -06:00
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
2022-01-26 23:25:00 -06:00
|
|
|
// 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
|
2022-01-24 20:49:17 -06:00
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
2022-01-26 23:25:00 -06:00
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
// copies or substantial portions of the Software.
|
2022-01-24 20:49:17 -06:00
|
|
|
//
|
|
|
|
|
// 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
|
2022-01-26 23:25:00 -06:00
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
2022-01-24 20:49:17 -06:00
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
2022-01-26 23:25:00 -06:00
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
// SOFTWARE.
|
2022-01-24 20:49:17 -06:00
|
|
|
|
|
|
|
|
#include "library/components/pthread_gotcha.hpp"
|
2022-02-08 17:42:17 -06:00
|
|
|
#include "library/components/omnitrace.hpp"
|
2022-05-08 04:40:10 -05:00
|
|
|
#include "library/components/pthread_create_gotcha.hpp"
|
|
|
|
|
#include "library/components/pthread_mutex_gotcha.hpp"
|
2022-02-08 17:42:17 -06:00
|
|
|
#include "library/components/roctracer.hpp"
|
2022-01-24 20:49:17 -06:00
|
|
|
#include "library/config.hpp"
|
|
|
|
|
#include "library/debug.hpp"
|
2022-04-25 17:00:52 -05:00
|
|
|
#include "library/runtime.hpp"
|
2022-01-24 20:49:17 -06:00
|
|
|
#include "library/sampling.hpp"
|
2022-02-25 05:33:59 -06:00
|
|
|
#include "library/thread_data.hpp"
|
2022-05-08 04:40:10 -05:00
|
|
|
#include "library/utility.hpp"
|
2022-01-24 20:49:17 -06:00
|
|
|
|
2022-02-23 06:59:32 -06:00
|
|
|
#include <timemory/backends/threading.hpp>
|
2022-01-24 20:49:17 -06:00
|
|
|
#include <timemory/sampling/allocator.hpp>
|
|
|
|
|
#include <timemory/utility/types.hpp>
|
|
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
2022-05-08 04:40:10 -05:00
|
|
|
#include <array>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2022-01-24 20:49:17 -06:00
|
|
|
namespace omnitrace
|
|
|
|
|
{
|
2022-02-08 17:42:17 -06:00
|
|
|
namespace
|
|
|
|
|
{
|
2022-05-08 04:40:10 -05:00
|
|
|
using bundle_t = tim::lightweight_tuple<pthread_create_gotcha_t, pthread_mutex_gotcha_t>;
|
2022-02-25 05:33:59 -06:00
|
|
|
|
2022-05-08 04:40:10 -05:00
|
|
|
auto&
|
|
|
|
|
get_sampling_on_child_threads_history(int64_t _idx = utility::get_thread_index())
|
2022-02-25 05:33:59 -06:00
|
|
|
{
|
2022-05-08 04:40:10 -05:00
|
|
|
static auto _v = utility::get_filled_array<OMNITRACE_MAX_THREADS>(
|
|
|
|
|
[]() { return utility::get_reserved_vector<bool>(32); });
|
|
|
|
|
return _v.at(_idx);
|
2022-02-25 05:33:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto&
|
2022-05-08 04:40:10 -05:00
|
|
|
get_bundle()
|
2022-02-25 05:33:59 -06:00
|
|
|
{
|
2022-05-08 04:40:10 -05:00
|
|
|
static auto _v = std::unique_ptr<bundle_t>{};
|
|
|
|
|
if(!_v) _v = std::make_unique<bundle_t>("pthread_gotcha");
|
|
|
|
|
return _v;
|
2022-02-25 05:33:59 -06:00
|
|
|
}
|
2022-02-08 17:42:17 -06:00
|
|
|
} // namespace
|
|
|
|
|
|
2022-04-27 16:56:38 -05:00
|
|
|
//--------------------------------------------------------------------------------------//
|
|
|
|
|
|
2022-01-24 20:49:17 -06:00
|
|
|
void
|
|
|
|
|
pthread_gotcha::configure()
|
|
|
|
|
{
|
2022-05-08 04:40:10 -05:00
|
|
|
pthread_create_gotcha::configure();
|
|
|
|
|
pthread_mutex_gotcha::configure();
|
2022-01-24 20:49:17 -06:00
|
|
|
}
|
|
|
|
|
|
2022-02-08 17:42:17 -06:00
|
|
|
void
|
|
|
|
|
pthread_gotcha::shutdown()
|
|
|
|
|
{
|
2022-05-08 04:40:10 -05:00
|
|
|
pthread_create_gotcha::shutdown();
|
|
|
|
|
pthread_mutex_gotcha::shutdown();
|
2022-02-08 17:42:17 -06:00
|
|
|
}
|
|
|
|
|
|
2022-02-25 05:33:59 -06:00
|
|
|
bool
|
|
|
|
|
pthread_gotcha::sampling_enabled_on_child_threads()
|
|
|
|
|
{
|
|
|
|
|
return sampling_on_child_threads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
pthread_gotcha::push_enable_sampling_on_child_threads(bool _v)
|
|
|
|
|
{
|
|
|
|
|
auto& _hist = get_sampling_on_child_threads_history();
|
|
|
|
|
bool _last = sampling_on_child_threads();
|
|
|
|
|
_hist.emplace_back(_last);
|
|
|
|
|
sampling_on_child_threads() = _v;
|
|
|
|
|
return _last;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
pthread_gotcha::pop_enable_sampling_on_child_threads()
|
|
|
|
|
{
|
|
|
|
|
auto& _hist = get_sampling_on_child_threads_history();
|
|
|
|
|
if(!_hist.empty())
|
|
|
|
|
{
|
|
|
|
|
bool _restored = _hist.back();
|
|
|
|
|
_hist.pop_back();
|
|
|
|
|
sampling_on_child_threads() = _restored;
|
|
|
|
|
}
|
|
|
|
|
return sampling_on_child_threads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
pthread_gotcha::set_sampling_on_all_future_threads(bool _v)
|
|
|
|
|
{
|
|
|
|
|
for(size_t i = 0; i < max_supported_threads; ++i)
|
|
|
|
|
get_sampling_on_child_threads_history(i).emplace_back(_v);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 20:49:17 -06:00
|
|
|
bool&
|
2022-02-25 05:33:59 -06:00
|
|
|
pthread_gotcha::sampling_on_child_threads()
|
2022-01-24 20:49:17 -06:00
|
|
|
{
|
2022-02-25 05:33:59 -06:00
|
|
|
static thread_local bool _v = get_sampling_on_child_threads_history().empty()
|
|
|
|
|
? false
|
|
|
|
|
: get_sampling_on_child_threads_history().back();
|
2022-01-24 20:49:17 -06:00
|
|
|
return _v;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-08 04:40:10 -05:00
|
|
|
void
|
|
|
|
|
pthread_gotcha::start()
|
2022-01-24 20:49:17 -06:00
|
|
|
{
|
2022-05-08 04:40:10 -05:00
|
|
|
get_bundle()->start();
|
2022-01-24 20:49:17 -06:00
|
|
|
}
|
|
|
|
|
|
2022-05-08 04:40:10 -05:00
|
|
|
void
|
|
|
|
|
pthread_gotcha::stop()
|
|
|
|
|
{
|
|
|
|
|
get_bundle()->stop();
|
|
|
|
|
get_bundle().reset();
|
|
|
|
|
}
|
2022-01-24 20:49:17 -06:00
|
|
|
} // namespace omnitrace
|