Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove FC_USE_PTHREAD_NAME_NP & pthread_getname_np() usage #1317

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/thread_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace eosio { namespace chain {
if (offset != std::string::npos)
tn.erase(0, offset+2);
tn = tn.substr(0, tn.find('>')) + "-" + std::to_string( i );
fc::set_os_thread_name( tn );
fc::set_thread_name( tn );
if ( init )
init();
} FC_LOG_AND_RETHROW()
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/platform_timer_asio_fallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ platform_timer::platform_timer() {
std::promise<void> p;
auto f = p.get_future();
checktime_thread = std::thread([&p]() {
fc::set_os_thread_name("checktime");
fc::set_thread_name("checktime");
checktime_ios = std::make_unique<boost::asio::io_service>();
boost::asio::io_service::work work(*checktime_ios);
p.set_value();
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/platform_timer_kqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ platform_timer::platform_timer() {
FC_ASSERT(kevent64(kqueue_fd, &quit_event, 1, NULL, 0, KEVENT_FLAG_IMMEDIATE, NULL) == 0, "failed to create quit event");

kevent_thread = std::thread([]() {
fc::set_os_thread_name("checktime");
fc::set_thread_name("checktime");
while(true) {
struct kevent64_s anEvent;
int c = kevent64(kqueue_fd, NULL, 0, &anEvent, 1, 0, NULL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ code_cache_async::code_cache_async(const std::filesystem::path data_dir, const e
wait_on_compile_monitor_message();

_monitor_reply_thread = std::thread([this]() {
fc::set_os_thread_name("oc-monitor");
fc::set_thread_name("oc-monitor");
_ctx.run();
});
}
Expand Down
13 changes: 0 additions & 13 deletions libraries/libfc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,6 @@ file( GLOB_RECURSE fc_headers ${CMAKE_CURRENT_SOURCE_DIR} *.hpp *.h )

add_library(fc ${fc_sources} ${fc_headers})

function(detect_thread_name)
include(CheckSymbolExists)
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
list(APPEND CMAKE_REQUIRED_LIBRARIES "-pthread")
check_symbol_exists(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
if(HAVE_PTHREAD_SETNAME_NP)
set_source_files_properties(src/log/logger_config.cpp PROPERTIES COMPILE_DEFINITIONS FC_USE_PTHREAD_NAME_NP)
endif()
endfunction()
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
detect_thread_name()
endif()

# Yuck: newer CMake files from boost iostreams will effectively target_link_libraries(Boost::iostreams z;bz2;lzma;zstd)
# without first "finding" those libraries. This resolves to simple -lz -lbz2 etc: it'll look for those libraries in the linker's
# library search path. This is most problematic on macOS where something like libzstd isn't in the standard search path. Historically
Expand Down
1 change: 0 additions & 1 deletion libraries/libfc/include/fc/log/logger_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ namespace fc {
void configure_logging( const std::filesystem::path& log_config );
bool configure_logging( const logging_config& l );

void set_os_thread_name( const std::string& name );
void set_thread_name( const std::string& name );
const std::string& get_thread_name();
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/libfc/src/log/gelf_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace fc

my->thread = std::thread([this] {
try {
fc::set_os_thread_name("gelf");
fc::set_thread_name("gelf");
my->io_context.run();
} catch (std::exception& ex) {
fprintf(stderr, "GELF logger caught exception at %s:%d : %s\n", __FILE__, __LINE__, ex.what());
Expand Down
29 changes: 14 additions & 15 deletions libraries/libfc/src/log/logger_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <fc/reflect/variant.hpp>
#include <fc/exception/exception.hpp>

#define BOOST_DLL_USE_STD_FS
#include <boost/dll/runtime_symbol_info.hpp>

namespace fc {

log_config& log_config::get() {
Expand Down Expand Up @@ -133,26 +136,22 @@ namespace fc {
}

static thread_local std::string thread_name;
void set_os_thread_name( const std::string& name ) {
#ifdef FC_USE_PTHREAD_NAME_NP
pthread_setname_np( pthread_self(), name.c_str() );
#endif
}

void set_thread_name( const std::string& name ) {
thread_name = name;
#if defined(__linux__) || defined(__FreeBSD__)
pthread_setname_np( pthread_self(), name.c_str() );
#elif defined(__APPLE__)
pthread_setname_np( name.c_str() );
#endif
}
const std::string& get_thread_name() {
if( thread_name.empty() ) {
#ifdef FC_USE_PTHREAD_NAME_NP
char thr_name[64];
int rc = pthread_getname_np( pthread_self(), thr_name, 64 );
if( rc == 0 ) {
thread_name = thr_name;
if(thread_name.empty()) {
try {
thread_name = boost::dll::program_location().filename().generic_string();
} catch (...) {
thread_name = "unknown";
}
#else
static int thread_count = 0;
thread_name = std::string( "thread-" ) + std::to_string( thread_count++ );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On mac we used to always get the thread-n version here. Does this now provide the actual thread name on mac?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes; really for any platform the thread names should be logged identically now since get_thread_name() just pulls from the static thread_local std::string thread_name -- that is, no platform specific code is invoked for thread names for logging purposes.

But on macos & freebsd we've also gained the thread names being set at the platform level, and I've confirmed on macos they do show in the debugger.

#endif
}
return thread_name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class resource_monitor_plugin_impl {
}

monitor_thread = std::thread( [this] {
fc::set_os_thread_name( "resmon" ); // console_appender uses 9 chars for thread name reporting.
fc::set_thread_name( "resmon" ); // console_appender uses 9 chars for thread name reporting.
space_handler.space_monitor_loop();

ctx.run();
Expand Down
2 changes: 1 addition & 1 deletion plugins/trace_api_plugin/store_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ namespace eosio::trace_api {

void slice_directory::start_maintenance_thread(log_handler log) {
_maintenance_thread = std::thread([this, log=std::move(log)](){
fc::set_os_thread_name( "trace-mx" );
fc::set_thread_name( "trace-mx" );
uint32_t last_lib = 0;

while(true) {
Expand Down