Skip to content

Commit

Permalink
Introduce replacement for getting and setting thread name: (XRPLF#4312)
Browse files Browse the repository at this point in the history
* In namespace ripple, introduces get_name function that takes a
  std::thread::native_handle_type and returns a std::string.
* In namespace ripple, introduces get_name function that takes a
  std::thread or std::jthread and returns a std::string.
* In namespace ripple::this_thread, introduces get_name function
  that takes no parameters and returns the name of the current
  thread as a std::string.
* In namespace ripple::this_thread, introduces set_name function
  that takes a std::string_view and sets the name of the current
  thread.
* Intended to replace the beast utilities setCurrentThreadName
  and getCurrentThreadName.
  • Loading branch information
HowardHinnant authored and ckeshava committed Sep 25, 2023
1 parent 3bd96b9 commit 6bc7962
Show file tree
Hide file tree
Showing 21 changed files with 205 additions and 204 deletions.
5 changes: 3 additions & 2 deletions Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ add_library(xrpl::libxrpl ALIAS libxrpl)
#]===============================]
target_sources (xrpl_core PRIVATE
src/ripple/beast/clock/basic_seconds_clock.cpp
src/ripple/beast/core/CurrentThreadName.cpp
src/ripple/beast/core/SemanticVersion.cpp
src/ripple/beast/hash/impl/xxhash.cpp
src/ripple/beast/insight/impl/Collector.cpp
Expand Down Expand Up @@ -56,6 +55,7 @@ target_sources (xrpl_core PRIVATE
src/ripple/basics/impl/Log.cpp
src/ripple/basics/impl/Number.cpp
src/ripple/basics/impl/StringUtilities.cpp
src/ripple/basics/impl/ThreadUtilities.cpp
#[===============================[
main sources:
subdir: json
Expand Down Expand Up @@ -200,6 +200,7 @@ install (
src/ripple/basics/TaggedCache.h
src/ripple/basics/tagged_integer.h
src/ripple/basics/ThreadSafetyAnalysis.h
src/ripple/basics/ThreadUtilities.h
src/ripple/basics/ToString.h
src/ripple/basics/UnorderedContainers.h
src/ripple/basics/UptimeClock.h
Expand Down Expand Up @@ -838,6 +839,7 @@ if (tests)
src/test/basics/Slice_test.cpp
src/test/basics/StringUtilities_test.cpp
src/test/basics/TaggedCache_test.cpp
src/test/basics/ThreadName_test.cpp
src/test/basics/XRPAmount_test.cpp
src/test/basics/base64_test.cpp
src/test/basics/base_uint_test.cpp
Expand All @@ -855,7 +857,6 @@ if (tests)
src/test/beast/LexicalCast_test.cpp
src/test/beast/SemanticVersion_test.cpp
src/test/beast/aged_associative_container_test.cpp
src/test/beast/beast_CurrentThreadName_test.cpp
src/test/beast/beast_Journal_test.cpp
src/test/beast/beast_PropertyStream_test.cpp
src/test/beast/beast_Zero_test.cpp
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/app/ledger/impl/LedgerCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <ripple/app/ledger/LedgerCleaner.h>
#include <ripple/app/ledger/LedgerMaster.h>
#include <ripple/app/misc/LoadFeeTrack.h>
#include <ripple/beast/core/CurrentThreadName.h>
#include <ripple/basics/ThreadUtilities.h>
#include <ripple/protocol/jss.h>

namespace ripple {
Expand Down Expand Up @@ -218,7 +218,7 @@ class LedgerCleanerImp : public LedgerCleaner
void
run()
{
beast::setCurrentThreadName("LedgerCleaner");
this_thread::set_name("LedgerCleaner");
JLOG(j_.debug()) << "Started";

while (true)
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/app/main/BasicApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//==============================================================================

#include <ripple/app/main/BasicApp.h>
#include <ripple/beast/core/CurrentThreadName.h>
#include <ripple/basics/ThreadUtilities.h>

BasicApp::BasicApp(std::size_t numberOfThreads)
{
Expand All @@ -28,7 +28,7 @@ BasicApp::BasicApp(std::size_t numberOfThreads)
while (numberOfThreads--)
{
threads_.emplace_back([this, numberOfThreads]() {
beast::setCurrentThreadName(
ripple::this_thread::set_name(
"io svc #" + std::to_string(numberOfThreads));
this->io_service_.run();
});
Expand Down
5 changes: 2 additions & 3 deletions src/ripple/app/main/GRPCServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <ripple/app/main/GRPCServer.h>
#include <ripple/app/reporting/P2pProxy.h>
#include <ripple/beast/core/CurrentThreadName.h>
#include <ripple/basics/ThreadUtilities.h>
#include <ripple/resource/Fees.h>

#include <ripple/beast/net/IPAddressConversion.h>
Expand Down Expand Up @@ -692,9 +692,8 @@ GRPCServer::start()
if (running_ = impl_.start(); running_)
{
thread_ = std::thread([this]() {
beast::setCurrentThreadName("rippled : GRPCServer");
// Start the event loop and begin handling requests
beast::setCurrentThreadName("rippled: grpc");
this_thread::set_name("rippled: grpc");
this->impl_.handleRpcs();
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/app/main/LoadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include <ripple/app/main/LoadManager.h>
#include <ripple/app/misc/LoadFeeTrack.h>
#include <ripple/app/misc/NetworkOPs.h>
#include <ripple/basics/ThreadUtilities.h>
#include <ripple/basics/UptimeClock.h>
#include <ripple/beast/core/CurrentThreadName.h>
#include <ripple/json/to_string.h>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -99,7 +99,7 @@ LoadManager::stop()
void
LoadManager::run()
{
beast::setCurrentThreadName("LoadManager");
this_thread::set_name("LoadManager");

using namespace std::chrono_literals;
using clock_type = std::chrono::steady_clock;
Expand Down
7 changes: 3 additions & 4 deletions src/ripple/app/main/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#include <ripple/app/rdb/Vacuum.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/basics/ThreadUtilities.h>
#include <ripple/basics/contract.h>
#include <ripple/beast/clock/basic_seconds_clock.h>
#include <ripple/beast/core/CurrentThreadName.h>
#include <ripple/core/Config.h>
#include <ripple/core/ConfigSections.h>
#include <ripple/core/TimeKeeper.h>
Expand Down Expand Up @@ -348,8 +348,7 @@ run(int argc, char** argv)
{
using namespace std;

beast::setCurrentThreadName(
"rippled: main " + BuildInfo::getVersionString());
this_thread::set_name("main " + BuildInfo::getVersionString());

po::variables_map vm;

Expand Down Expand Up @@ -777,7 +776,7 @@ run(int argc, char** argv)
}

// We have an RPC command to process:
beast::setCurrentThreadName("rippled: rpc");
this_thread::set_name("rippled: rpc");
return RPCCall::fromCommandLine(
*config, vm["parameters"].as<std::vector<std::string>>(), *logs);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/app/misc/SHAMapStoreImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <ripple/app/misc/NetworkOPs.h>
#include <ripple/app/rdb/State.h>
#include <ripple/app/rdb/backend/SQLiteDatabase.h>
#include <ripple/beast/core/CurrentThreadName.h>
#include <ripple/basics/ThreadUtilities.h>
#include <ripple/core/ConfigSections.h>
#include <ripple/core/Pg.h>
#include <ripple/nodestore/Scheduler.h>
Expand Down Expand Up @@ -286,7 +286,7 @@ SHAMapStoreImp::run()
"Reporting does not support online_delete. Remove "
"online_delete info from config");
}
beast::setCurrentThreadName("SHAMapStore");
this_thread::set_name("SHAMapStore");
LedgerIndex lastRotated = state_db_.getState().lastRotated;
netOPs_ = &app_.getOPs();
ledgerMaster_ = &app_.getLedgerMaster();
Expand Down
1 change: 0 additions & 1 deletion src/ripple/app/reporting/ETLSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include <ripple/app/reporting/ETLSource.h>
#include <ripple/app/reporting/ReportingETL.h>
#include <ripple/beast/core/CurrentThreadName.h>
#include <ripple/json/json_reader.h>
#include <ripple/json/json_writer.h>

Expand Down
11 changes: 5 additions & 6 deletions src/ripple/app/reporting/ReportingETL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

#include <ripple/app/rdb/backend/PostgresDatabase.h>
#include <ripple/app/reporting/ReportingETL.h>

#include <ripple/beast/core/CurrentThreadName.h>
#include <ripple/basics/ThreadUtilities.h>
#include <ripple/json/json_reader.h>
#include <ripple/json/json_writer.h>
#include <boost/asio/connect.hpp>
Expand Down Expand Up @@ -510,7 +509,7 @@ ReportingETL::runETLPipeline(uint32_t startSequence)
&startSequence,
&writeConflict,
&transformQueue]() {
beast::setCurrentThreadName("rippled: ReportingETL extract");
this_thread::set_name("ETL extract");
uint32_t currentSequence = startSequence;

// there are two stopping conditions here.
Expand Down Expand Up @@ -562,7 +561,7 @@ ReportingETL::runETLPipeline(uint32_t startSequence)
&writeConflict,
&loadQueue,
&transformQueue]() {
beast::setCurrentThreadName("rippled: ReportingETL transform");
this_thread::set_name("ETL transform");

assert(parent);
parent = std::make_shared<Ledger>(*parent, NetClock::time_point{});
Expand Down Expand Up @@ -601,7 +600,7 @@ ReportingETL::runETLPipeline(uint32_t startSequence)
&lastPublishedSequence,
&loadQueue,
&writeConflict]() {
beast::setCurrentThreadName("rippled: ReportingETL load");
this_thread::set_name("ETL load");
size_t totalTransactions = 0;
double totalTime = 0;
while (!writeConflict)
Expand Down Expand Up @@ -825,7 +824,7 @@ void
ReportingETL::doWork()
{
worker_ = std::thread([this]() {
beast::setCurrentThreadName("rippled: ReportingETL worker");
this_thread::set_name("ETL worker");
if (readOnly_)
monitorReadOnly();
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <[email protected]>
Portions of this file are from JUCE.
Copyright (c) 2013 - Raw Material Software Ltd.
Please visit http://www.juce.com
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2022 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand All @@ -21,31 +17,34 @@
*/
//==============================================================================

#ifndef BEAST_CORE_CURRENT_THREAD_NAME_H_INCLUDED
#define BEAST_CORE_CURRENT_THREAD_NAME_H_INCLUDED
#ifndef RIPPLE_BASICS_THREADUTILITIES_H_INCLUDED
#define RIPPLE_BASICS_THREADUTILITIES_H_INCLUDED

#include <string>
#include <string_view>
#include <thread>

namespace beast {
namespace ripple {

/** Changes the name of the caller thread.
Different OSes may place different length or content limits on this name.
*/
void
setCurrentThreadName(std::string_view newThreadName);
std::string
get_name(std::thread::native_handle_type t);

/** Returns the name of the caller thread.
template <class Thread>
inline auto
get_name(Thread& t) -> decltype(t.native_handle(), t.join(), std::string{})
{
return get_name(t.native_handle());
}

The name returned is the name as set by a call to setCurrentThreadName().
If the thread name is set by an external force, then that name change
will not be reported.
namespace this_thread {

If no name has ever been set, then the empty string is returned.
*/
std::string
getCurrentThreadName();
get_name();

void
set_name(std::string s);

} // namespace this_thread

} // namespace beast
} // namespace ripple

#endif
Loading

0 comments on commit 6bc7962

Please sign in to comment.