-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Introduce replacement for getting and setting thread name: #4312
Conversation
Also the linux and mac sections are almost identical. Maybe combine them? |
Great, thanks for the updates @HowardHinnant! I learned some things as well :-). |
@HowardHinnant does this change still make sense to apply? If so, could you suggest 2 potential reviewers? |
/** Returns the name of the caller thread. | ||
template <class Thread> | ||
inline auto | ||
get_name(Thread& t) -> decltype(t.native_handle(), t.join(), std::string{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is so cool! Am I understanding correctly that the intent is to make the template substitution fail if Thread
doesn't have native_handle()
or join()
members?
Reviewing your code is always fun as I learn modern best practices!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two issues we'll need to resolve:
-
On linux, threads do not start with an empty name (see: https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html). This isn't a killer, but it makes a test fail.
-
On linux (or at least my system) thread names are limited to 16 characters. We could just truncate the thread names, but it is unlikely anyone will notice their nice thread names were truncated until they look in a debugger - and fixing the thread names won't be a high priority if people are actively debugging other code.
I don't have a solution to offer. Thread names are usually known at compile time, so we could get "clever" and detect this at compile time, but honestly I'm not sure that kind of complexity is worth it for something like this.
At any rate, I'll let you think about solutions and I'll do so as well.
@HowardHinnant any thoughts on the issues mentioned above? |
f66ff10
to
d842eea
Compare
Addressed reviewer comments and rebased to develop. |
void | ||
set_name(std::string s) | ||
{ | ||
s.resize(15); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of silently truncating, what do you think about asserting the size instead? If someone renames or add a thread with too long of a name, right now it won't be caught until someone looks at it in a debugger (which can be a LONG time). Instead we could assert and it would be caught the first time rippled was run in debug mode.
Incidentally, it also save a string copy because we could pass by const&
again, but honestly I don't think we care; I'm much more worried about catching the long thread name.
* 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.
1. Remove test which assumes null default thread name 2. Limit names to 15 characters (16 including trailing null)
d842eea
to
a83a2a3
Compare
Addressed reviewer comments and rebased to develop. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
* 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.
note: libxrpl should be considered part of the "API" since this change potentially breaks dependents such as Clio |
nb: this was merged after 1.12 and may be included in the next release (expected to be 1.13 or 2.0) |
From now on, we'll need to be more careful about making changes that break libxrpl users. Test Plan
Notes
|
…RPLF#4312)" This reverts commit 36cb5f9.
…RPLF#4312)" This reverts commit 36cb5f9.
…RPLF#4312)" This reverts commit 36cb5f9.
…RPLF#4312)" This reverts commit 36cb5f9.
* 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.
* Revert "Remove CurrentThreadName.h from RippledCore.cmake (XRPLF#4697)" This reverts commit 3b5fcd5. * Revert "Introduce replacement for getting and setting thread name: (XRPLF#4312)" This reverts commit 36cb5f9.
* 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.
* Revert "Remove CurrentThreadName.h from RippledCore.cmake (XRPLF#4697)" This reverts commit 3b5fcd5. * Revert "Introduce replacement for getting and setting thread name: (XRPLF#4312)" This reverts commit 36cb5f9.
* 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.
* Revert "Remove CurrentThreadName.h from RippledCore.cmake (XRPLF#4697)" This reverts commit 3b5fcd5. * Revert "Introduce replacement for getting and setting thread name: (XRPLF#4312)" This reverts commit 36cb5f9.
High Level Overview of Change
std::thread::native_handle_type and returns a std::string.
std::thread or std::jthread and returns a std::string.
that takes no parameters and returns the name of the current
thread as a std::string.
that takes a std::string_view and sets the name of the current
thread.
and getCurrentThreadName.
The new API for getting and setting the thread name:
rippled/src/ripple/basics/ThreadUtilities.h
Lines 38 to 46 in a955057
Example:
The maximum length on the name is 15. There is an assert on that, and if asserts are turned off, the name is truncated to a max of 15.
Context of Change
Type of Change