Commit 50403ea0 authored by Dominik Charousset's avatar Dominik Charousset

Give CAF threads recognizable names for debugging

parent 0f333bb3
......@@ -98,6 +98,7 @@ set(LIBCAF_CORE_SRCS
src/sec.cpp
src/sequencer.cpp
src/serializer.cpp
src/set_thread_name.cpp
src/shared_spinlock.cpp
src/simple_actor_clock.cpp
src/skip.cpp
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
namespace caf {
namespace detail {
/// Sets the name thread shown by the OS. Not supported on all plattforms
/// (no-op on Windows).
void set_thread_name(const char* name);
} // namespace detail
} // namespace caf
......@@ -25,10 +25,10 @@
#include <memory>
#include <condition_variable>
#include "caf/scheduler/worker.hpp"
#include "caf/scheduler/abstract_coordinator.hpp"
#include "caf/detail/set_thread_name.hpp"
#include "caf/detail/thread_safe_actor_clock.hpp"
#include "caf/scheduler/abstract_coordinator.hpp"
#include "caf/scheduler/worker.hpp"
namespace caf {
namespace scheduler {
......@@ -75,7 +75,11 @@ protected:
// Launch an additional background thread for dispatching timeouts and
// delayed messages.
timer_ = std::thread{[&] {
CAF_SET_LOGGER_SYS(&system());
detail::set_thread_name("caf.clock");
system().thread_started();
clock_.run_dispatch_loop();
system().thread_terminates();
}};
// Run remaining startup code.
super::start();
......
......@@ -20,11 +20,11 @@
#include <cstddef>
#include "caf/detail/double_ended_queue.hpp"
#include "caf/detail/set_thread_name.hpp"
#include "caf/execution_unit.hpp"
#include "caf/logger.hpp"
#include "caf/resumable.hpp"
#include "caf/execution_unit.hpp"
#include "caf/detail/double_ended_queue.hpp"
namespace caf {
namespace scheduler {
......@@ -54,6 +54,8 @@ public:
CAF_ASSERT(this_thread_.get_id() == std::thread::id{});
auto this_worker = this;
this_thread_ = std::thread{[this_worker] {
CAF_SET_LOGGER_SYS(&this_worker->system());
detail::set_thread_name("caf.multiplexer");
this_worker->system().thread_started();
this_worker->run();
this_worker->system().thread_terminates();
......
......@@ -16,17 +16,17 @@
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include <utility>
#include "caf/blocking_actor.hpp"
#include "caf/logger.hpp"
#include "caf/actor_system.hpp"
#include "caf/actor_registry.hpp"
#include <utility>
#include "caf/detail/sync_request_bouncer.hpp"
#include "caf/detail/invoke_result_visitor.hpp"
#include "caf/actor_registry.hpp"
#include "caf/actor_system.hpp"
#include "caf/detail/default_invoke_result_visitor.hpp"
#include "caf/detail/invoke_result_visitor.hpp"
#include "caf/detail/set_thread_name.hpp"
#include "caf/detail/sync_request_bouncer.hpp"
#include "caf/logger.hpp"
namespace caf {
......@@ -92,6 +92,7 @@ void blocking_actor::launch(execution_unit*, bool, bool hide) {
home_system().inc_detached_threads();
std::thread([](strong_actor_ptr ptr) {
// actor lives in its own thread
detail::set_thread_name("caf.actor");
ptr->home_system->thread_started();
auto this_ptr = ptr->get();
CAF_ASSERT(dynamic_cast<blocking_actor*>(this_ptr) != nullptr);
......
......@@ -34,6 +34,7 @@
#include "caf/defaults.hpp"
#include "caf/detail/get_process_id.hpp"
#include "caf/detail/pretty_type_name.hpp"
#include "caf/detail/set_thread_name.hpp"
#include "caf/intrusive/task_result.hpp"
#include "caf/local_actor.hpp"
#include "caf/locks.hpp"
......@@ -555,6 +556,7 @@ void logger::start() {
log_first_line();
else
thread_ = std::thread{[this] {
detail::set_thread_name("caf.logger");
this->system_.thread_started();
this->run();
this->system_.thread_terminates();
......
......@@ -19,6 +19,7 @@
#include "caf/detail/private_thread.hpp"
#include "caf/config.hpp"
#include "caf/detail/set_thread_name.hpp"
#include "caf/logger.hpp"
#include "caf/scheduled_actor.hpp"
......@@ -87,6 +88,7 @@ void private_thread::shutdown() {
}
void private_thread::exec(private_thread* this_ptr) {
detail::set_thread_name("caf.actor");
this_ptr->system_.thread_started();
this_ptr->run();
// make sure to not destroy the private thread object before the
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/detail/set_thread_name.hpp"
#include "caf/config.hpp"
#ifndef CAF_WINDOWS
#include <pthread.h>
#endif // CAF_WINDOWS
#if defined(CAF_LINUX)
#include <sys/prctl.h>
#elif defined(CAF_BSD)
#include <pthread_np.h>
#endif // defined(...)
#include <thread>
#include <type_traits>
namespace caf {
namespace detail {
void set_thread_name(const char* name) {
CAF_IGNORE_UNUSED(name);
#ifdef CAF_WINDOWS
// nop
#else // CAF_WINDOWS
static_assert(std::is_same<std::thread::native_handle_type, pthread_t>::value,
"std::thread not based on pthread_t");
#if defined(CAF_MACOS)
pthread_setname_np(name);
#elif defined(CAF_LINUX)
prctl(PR_SET_NAME, name, 0, 0, 0);
#elif defined(CAF_BSD)
pthread_set_name_np(pthread_self(), name);
#endif // defined(...)
#endif // CAF_WINDOWS
}
} // namespace detail
} // namespace caf
......@@ -118,7 +118,8 @@ CAF_TEST_FIXTURE_SCOPE(counting_hook, fixture<counting_thread_hook>)
CAF_TEST(counting_system_without_actor) {
assumed_init_calls = 1;
assumed_thread_count = get_or(cfg, "scheduler.max-threads",
defaults::scheduler::max_threads);
defaults::scheduler::max_threads)
+ 1; // caf.clock thread
auto& sched = sys.scheduler();
if (sched.detaches_utility_actors())
assumed_thread_count += sched.num_utility_actors();
......@@ -128,7 +129,7 @@ CAF_TEST(counting_system_with_actor) {
assumed_init_calls = 1;
assumed_thread_count = get_or(cfg, "scheduler.max-threads",
defaults::scheduler::max_threads)
+ 1;
+ 2; // caf.clock thread plus detached actor
auto& sched = sys.scheduler();
if (sched.detaches_utility_actors())
assumed_thread_count += sched.num_utility_actors();
......
......@@ -55,6 +55,7 @@
#include "caf/detail/ripemd_160.hpp"
#include "caf/detail/safe_equal.hpp"
#include "caf/detail/get_root_uuid.hpp"
#include "caf/detail/set_thread_name.hpp"
#include "caf/detail/get_mac_addresses.hpp"
#ifdef CAF_WINDOWS
......@@ -323,6 +324,7 @@ void middleman::start() {
std::condition_variable cv;
thread_ = std::thread{[&,this] {
CAF_SET_LOGGER_SYS(&system());
detail::set_thread_name("caf.multiplexer");
system().thread_started();
CAF_LOG_TRACE("");
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment