You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cassandra.apache.org by Evan Jones via dev <de...@cassandra.apache.org> on 2025/12/10 21:26:44 UTC

CPU Profiler reports ~2% of time spent in Thread.setName

I happened to look at profiles of Cassandra in Datadog's development
environment, and I noticed a number of our Cassandra clusters report using
something like 1-4% of CPU in the JDK Thread.setName function. This seems
like a misconfiguration in our environment, or a minor Cassandra
performance bug: 2% of CPU for some debugging information seems expensive.
We must be calling this function too often from SEPWorker.run()? If anyone
has suggestions for how we might be able to reduce this, I would be happy
to attempt to turn it into a patch.

Additional Details:

The attached image shows the Datadog profiler's flamegraph [1]. This shows
Cassandra 5.0.6 running with OpenJDK 17 spending 2.34% of the CPU samples
in Thread.setName, which is aggregated from 1 day of usage.

The text version of the call stack is below.

Thread.setName function is called by SEPWorker.run():
https://github.com/apache/cassandra/blob/trunk/src/java/org/apache/cassandra/concurrent/SEPWorker.java#L132

It appears this tries to only set the name when it changes states, but I
guess for our applications it might be changing states fairly quickly?

If anyone has any suggestions for how we might resolve this, I would love
to help test and/or submit a fix. Thanks!

Evan Jones


Profiler call stack:

Thread::call_run()
javaThread::thread_main_inner()
thread_entry(javaThread*)
JavaCalls::call_virtual(javaValue*, Handle, Klass*, Symbol*, JavaThread*)
JavaCalls::call_helper(javaValue*, methodHandle const&, JavaCallArguments*,
JavaThread*)
StubRoutines (1) {call_stub_return_address}
Thread.run()
FastThreadLocalRunnable.run()
SEPWorker.run()
Thread.setName(String)
Thread.setNativeName(String)
JVM_SetNativeThreadName(libjvm.so)

[1] https://github.com/DataDog/dd-otel-host-profiler

[image: Screenshot 2025-12-10 at 16.18.36.png]

Re: CPU Profiler reports ~2% of time spent in Thread.setName

Posted by Dmitry Konstantinov <ne...@gmail.com>.
ticket for the change: https://issues.apache.org/jira/browse/CASSANDRA-21089


On Fri, 19 Dec 2025 at 00:55, Abe Ratnofsky <ab...@aber.io> wrote:

> My understanding as well is that async-profiler collects samples with the
> thread ID, then does the lookup of thread ID to name when the JFR output is
> produced, so there could be misleading profile results if a thread is
> renamed between execution sample and output generation. I haven’t noticed a
> case of this before, but it does seem like an existing issue.
>
> So I’m supportive of defaulting thread renaming in SEPExecutor to off, and
> we can always support a toggle via JMX hot-prop so names can be available
> during profile collection.



-- 
Dmitry Konstantinov

Re: CPU Profiler reports ~2% of time spent in Thread.setName

Posted by Abe Ratnofsky <ab...@aber.io>.
My understanding as well is that async-profiler collects samples with the thread ID, then does the lookup of thread ID to name when the JFR output is produced, so there could be misleading profile results if a thread is renamed between execution sample and output generation. I haven’t noticed a case of this before, but it does seem like an existing issue.

So I’m supportive of defaulting thread renaming in SEPExecutor to off, and we can always support a toggle via JMX hot-prop so names can be available during profile collection.

Re: CPU Profiler reports ~2% of time spent in Thread.setName

Posted by Dmitry Konstantinov <ne...@gmail.com>.
Here there is a patch with both changes:
https://github.com/apache/cassandra/compare/trunk...netudima:cassandra:sep_thread_name-trunk?expand=1


>> but thread names in logs + JFR are very helpful and I wouldn’t want to
lose those.
I have not had a time to validate it but I suspect that profilers do not
capture thread names for every trace/sample to avoid performance overheads
and only use thread ids, names are captured only on creation or
periodically and then linked (I would do it in this way). If it is true
then having a changing name for a thread can be not helpful but even
misleading during a profile analysis...
Regarding logs, do we have an example when a SEP thread name gave some new
information? (we have an id in the thread name even without this renaming
feature to be able to differentiate them, a phase such as
Read/Write/Response/NativeTransport usually is clear from the log message
itself..)


On Thu, 18 Dec 2025 at 19:32, C. Scott Andreas <sc...@paradoxica.net> wrote:

> I'd support defaulting to off as well - this is a surprising find for me.
>
> On Dec 18, 2025, at 11:18 AM, Evan Jones via dev <de...@cassandra.apache.org>
> wrote:
>
>
> I finally got the time to set up a reproducible test. I put a local
> variable cache for the thread name in SEPWorker.run, and it only has about
> ~50% hit rate. It does reduce the cost, but not by as much as I would like.
> This still seems very expensive for something that is looked at extremely
> rarely. I'm happy to submit a patch if someone thinks we should merge it.
> It is a only a few lines of code, but in my opinion this doesn't really
> solve the problem completely.
>
> Personally: I would advocate to defaulting this feature to off, since 1-4%
> of CPU across all Cassandra clusters globally seems like a pretty big
> waste. I'm also happy to submit a patch for that if it would be welcomed,
> but I also won't spend too much time advocating for that change.
>
> Any other ideas for how we might be able to not call Thread.setName for
> every single task but still keep some of the benefit?
>
> Evan
>
>
>
>
>
>
>
>
> On Mon, Dec 15, 2025 at 12:52 PM Evan Jones <ev...@datadoghq.com>
> wrote:
>
>> Hmm I spent a bit of time and can't get the symbols for libc to match. I
>> think for the version of the JVM we are using (17), the
>> JVM calls pthread_setname_np [1], which then calls glibc's prctl() [2],
>> which then makes the system call, so I *think* those are the stacks we are
>> seeing. Newer JVMs just call prctl() directly which might be slightly more
>> efficient.
>>
>> For what it is worth: I got some of Datadog's JVM experts interested in
>> this, and we have a tentative JVM patch that makes setName slightly faster
>> (about ~25% in a microbenchmark) [3]. I am also going to investigate your
>> idea of renaming on change. I suspect that will make it substantially
>> better, but I need to find some time to put together a somewhat controlled
>> test to try to "measure" the improvement.
>>
>> Thanks everyone for the responses!
>>
>> Evan Jones
>>
>>
>> [1]
>> https://github.com/openjdk/jdk17u-dev/blob/master/src/hotspot/os/linux/os_linux.cpp#L4892
>> [2]
>> https://github.com/bminor/glibc/blob/f56382e67e73c0b309c7e39c6b122b04b29b6808/nptl/pthread_setname.c#L43C12-L43C19
>> [3] https://github.com/openjdk/jdk/pull/28773
>>
>>
>>
>> On Wed, Dec 10, 2025 at 6:45 PM Abe Ratnofsky <ab...@aber.io> wrote:
>>
>>> I’ve seen slightly different issues with Thread.setNativeName in
>>> profiles, but thread names in logs + JFR are very helpful and I wouldn’t
>>> want to lose those. AFAIK, the JDK doesn't currently offer a way to set a
>>> java.lang.Thread name without updating the platform thread name.
>>>
>>> Evan - are you able to use a version of libc with debug symbols, so the
>>> “Unknown Function” calls in your profile can be resolved? I’m assuming
>>> they’re the copying + re-encoding steps here[1].
>>>
>>> [1]:
>>> https://github.com/openjdk/jdk/blob/master/src/hotspot/os/linux/os_linux.cpp#L4823-L4827
>>>
>>> We could potentially improve this by interning the current thread name
>>> in SEPWorker, and only renaming when we need to change. Then we’d have a
>>> reference comparison instead of a syscall, at least some of the time.
>>>
>>
>

-- 
Dmitry Konstantinov

Re: CPU Profiler reports ~2% of time spent in Thread.setName

Posted by "C. Scott Andreas" <sc...@paradoxica.net>.
I'd support defaulting to off as well - this is a surprising find for me. On Dec 18, 2025, at 11:18 AM, Evan Jones via dev <de...@cassandra.apache.org> wrote: I finally got the time to set up a reproducible test. I put a local variable cache for the thread name in SEPWorker.run, and it only has about ~50% hit rate. It does reduce the cost, but not by as much as I would like. This still seems very expensive for something that is looked at extremely rarely. I'm happy to submit a patch if someone thinks we should merge it. It is a only a few lines of code, but in my opinion this doesn't really solve the problem completely. Personally: I would advocate to defaulting this feature to off, since 1-4% of CPU across all Cassandra clusters globally seems like a pretty big waste. I'm also happy to submit a patch for that if it would be welcomed, but I also won't spend too much time advocating for that change. Any other ideas for how we might be able to not call Thread.setName for every single task but still keep some of the benefit? Evan On Mon, Dec 15, 2025 at 12:52 PM Evan Jones < evan.jones@datadoghq.com > wrote: Hmm I spent a bit of time and can't get the symbols for libc to match. I think for the version of the JVM we are using (17), the JVM calls pthread_setname_np [1], which then calls glibc's prctl() [2], which then makes the system call, so I *think* those are the stacks we are seeing. Newer JVMs just call prctl() directly which might be slightly more efficient. For what it is worth: I got some of Datadog's JVM experts interested in this, and we have a tentative JVM patch that makes setName slightly faster (about ~25% in a microbenchmark) [3]. I am also going to investigate your idea of renaming on change. I suspect that will make it substantially better, but I need to find some time to put together a somewhat controlled test to try to "measure" the improvement. Thanks everyone for the responses! Evan Jones [1] https://github.com/openjdk/jdk17u-dev/blob/master/src/hotspot/os/linux/os_linux.cpp#L4892 [2] https://github.com/bminor/glibc/blob/f56382e67e73c0b309c7e39c6b122b04b29b6808/nptl/pthread_setname.c#L43C12-L43C19 [3] https://github.com/openjdk/jdk/pull/28773 On Wed, Dec 10, 2025 at 6:45 PM Abe Ratnofsky < abe@aber.io > wrote: I’ve seen slightly different issues with Thread.setNativeName in profiles, but thread names in logs + JFR are very helpful and I wouldn’t want to lose those. AFAIK, the JDK doesn't currently offer a way to set a java.lang.Thread name without updating the platform thread name. Evan - are you able to use a version of libc with debug symbols, so the “Unknown Function” calls in your profile can be resolved? I’m assuming they’re the copying + re-encoding steps here[1]. [1]: https://github.com/openjdk/jdk/blob/master/src/hotspot/os/linux/os_linux.cpp#L4823-L4827 We could potentially improve this by interning the current thread name in SEPWorker, and only renaming when we need to change. Then we’d have a reference comparison instead of a syscall, at least some of the time.

Re: CPU Profiler reports ~2% of time spent in Thread.setName

Posted by Evan Jones via dev <de...@cassandra.apache.org>.
I finally got the time to set up a reproducible test. I put a local
variable cache for the thread name in SEPWorker.run, and it only has about
~50% hit rate. It does reduce the cost, but not by as much as I would like.
This still seems very expensive for something that is looked at extremely
rarely. I'm happy to submit a patch if someone thinks we should merge it.
It is a only a few lines of code, but in my opinion this doesn't really
solve the problem completely.

Personally: I would advocate to defaulting this feature to off, since 1-4%
of CPU across all Cassandra clusters globally seems like a pretty big
waste. I'm also happy to submit a patch for that if it would be welcomed,
but I also won't spend too much time advocating for that change.

Any other ideas for how we might be able to not call Thread.setName for
every single task but still keep some of the benefit?

Evan








On Mon, Dec 15, 2025 at 12:52 PM Evan Jones <ev...@datadoghq.com>
wrote:

> Hmm I spent a bit of time and can't get the symbols for libc to match. I
> think for the version of the JVM we are using (17), the
> JVM calls pthread_setname_np [1], which then calls glibc's prctl() [2],
> which then makes the system call, so I *think* those are the stacks we are
> seeing. Newer JVMs just call prctl() directly which might be slightly more
> efficient.
>
> For what it is worth: I got some of Datadog's JVM experts interested in
> this, and we have a tentative JVM patch that makes setName slightly faster
> (about ~25% in a microbenchmark) [3]. I am also going to investigate your
> idea of renaming on change. I suspect that will make it substantially
> better, but I need to find some time to put together a somewhat controlled
> test to try to "measure" the improvement.
>
> Thanks everyone for the responses!
>
> Evan Jones
>
>
> [1]
> https://github.com/openjdk/jdk17u-dev/blob/master/src/hotspot/os/linux/os_linux.cpp#L4892
> [2]
> https://github.com/bminor/glibc/blob/f56382e67e73c0b309c7e39c6b122b04b29b6808/nptl/pthread_setname.c#L43C12-L43C19
> [3] https://github.com/openjdk/jdk/pull/28773
>
>
>
> On Wed, Dec 10, 2025 at 6:45 PM Abe Ratnofsky <ab...@aber.io> wrote:
>
>> I’ve seen slightly different issues with Thread.setNativeName in
>> profiles, but thread names in logs + JFR are very helpful and I wouldn’t
>> want to lose those. AFAIK, the JDK doesn't currently offer a way to set a
>> java.lang.Thread name without updating the platform thread name.
>>
>> Evan - are you able to use a version of libc with debug symbols, so the
>> “Unknown Function” calls in your profile can be resolved? I’m assuming
>> they’re the copying + re-encoding steps here[1].
>>
>> [1]:
>> https://github.com/openjdk/jdk/blob/master/src/hotspot/os/linux/os_linux.cpp#L4823-L4827
>>
>> We could potentially improve this by interning the current thread name in
>> SEPWorker, and only renaming when we need to change. Then we’d have a
>> reference comparison instead of a syscall, at least some of the time.
>>
>

Re: CPU Profiler reports ~2% of time spent in Thread.setName

Posted by Evan Jones via dev <de...@cassandra.apache.org>.
Hmm I spent a bit of time and can't get the symbols for libc to match. I
think for the version of the JVM we are using (17), the
JVM calls pthread_setname_np [1], which then calls glibc's prctl() [2],
which then makes the system call, so I *think* those are the stacks we are
seeing. Newer JVMs just call prctl() directly which might be slightly more
efficient.

For what it is worth: I got some of Datadog's JVM experts interested in
this, and we have a tentative JVM patch that makes setName slightly faster
(about ~25% in a microbenchmark) [3]. I am also going to investigate your
idea of renaming on change. I suspect that will make it substantially
better, but I need to find some time to put together a somewhat controlled
test to try to "measure" the improvement.

Thanks everyone for the responses!

Evan Jones


[1]
https://github.com/openjdk/jdk17u-dev/blob/master/src/hotspot/os/linux/os_linux.cpp#L4892
[2]
https://github.com/bminor/glibc/blob/f56382e67e73c0b309c7e39c6b122b04b29b6808/nptl/pthread_setname.c#L43C12-L43C19
[3] https://github.com/openjdk/jdk/pull/28773



On Wed, Dec 10, 2025 at 6:45 PM Abe Ratnofsky <ab...@aber.io> wrote:

> I’ve seen slightly different issues with Thread.setNativeName in profiles,
> but thread names in logs + JFR are very helpful and I wouldn’t want to lose
> those. AFAIK, the JDK doesn't currently offer a way to set a
> java.lang.Thread name without updating the platform thread name.
>
> Evan - are you able to use a version of libc with debug symbols, so the
> “Unknown Function” calls in your profile can be resolved? I’m assuming
> they’re the copying + re-encoding steps here[1].
>
> [1]:
> https://github.com/openjdk/jdk/blob/master/src/hotspot/os/linux/os_linux.cpp#L4823-L4827
>
> We could potentially improve this by interning the current thread name in
> SEPWorker, and only renaming when we need to change. Then we’d have a
> reference comparison instead of a syscall, at least some of the time.
>

Re: CPU Profiler reports ~2% of time spent in Thread.setName

Posted by Abe Ratnofsky <ab...@aber.io>.
I’ve seen slightly different issues with Thread.setNativeName in profiles, but thread names in logs + JFR are very helpful and I wouldn’t want to lose those. AFAIK, the JDK doesn't currently offer a way to set a java.lang.Thread name without updating the platform thread name.

Evan - are you able to use a version of libc with debug symbols, so the “Unknown Function” calls in your profile can be resolved? I’m assuming they’re the copying + re-encoding steps here[1].

[1]: https://github.com/openjdk/jdk/blob/master/src/hotspot/os/linux/os_linux.cpp#L4823-L4827

We could potentially improve this by interning the current thread name in SEPWorker, and only renaming when we need to change. Then we’d have a reference comparison instead of a syscall, at least some of the time.

Re: CPU Profiler reports ~2% of time spent in Thread.setName

Posted by Dmitry Konstantinov <ne...@gmail.com>.
Just a thread name usually gives a too small amount of information to be
able to identify a leaking issue, based on my experience. Reference leaks
have a stackstace where we can clearly see what kind of logic it is related
to, without it - it is very hard to find the cause.
In the case of thread dumps - we also can say where we are in the logic
based on a stack trace.
In the case of logging for SEP threads I do not remember any cases when a
thread name helped me. It can be helpful for flushing or repair threads
(which ones are not SEP) where we have many messages and we may want to
correlate them but for request processing threads the amount of logs is
really small to get even one per request.

In any case, if other people think that the current value is more
beneficial I would not insist too much on changing the default, taking in
account the ability to change it.

On Wed, 10 Dec 2025 at 22:40, Josh McKenzie <jm...@apache.org> wrote:

> I’ve considered submitting a ticket to change the default for this option.
>
> Co-locating this kind of debug information with log level and tracing
> reference leaks seems like it could be a nice gesture to our users. And
> whatever else we may have rustling around (undocumented -D flags, other
> things in the .yaml, etc)
>
>
> On Wed, Dec 10, 2025, at 5:08 PM, Dmitry Konstantinov wrote:
>
> I’ve considered submitting a ticket to change the default for this option.
>
>
>

-- 
Dmitry Konstantinov

Re: CPU Profiler reports ~2% of time spent in Thread.setName

Posted by Josh McKenzie <jm...@apache.org>.
> I’ve considered submitting a ticket to change the default for this option.
> 
Co-locating this kind of debug information with log level and tracing reference leaks seems like it could be a nice gesture to our users. And whatever else we may have rustling around (undocumented -D flags, other things in the .yaml, etc)


On Wed, Dec 10, 2025, at 5:08 PM, Dmitry Konstantinov wrote:
> I’ve considered submitting a ticket to change the default for this option.
> 
> 

Re: CPU Profiler reports ~2% of time spent in Thread.setName

Posted by Dmitry Konstantinov <ne...@gmail.com>.
Hi Evan,

You can disable this logic by setting the following Java system property in
conf/jvm-server.options (this is what I usually do in my environments):

-Dcassandra.set_sep_thread_name=false

The SEP logic shares a thread pool to process operations from different
queues. It renames threads to reflect the type of operation a thread is
currently processing. While the renaming can be useful for reading thread
dumps or ttop output, in my experience it’s not so needed.

I’ve considered submitting a ticket to change the default for this option.

Regards,
Dmitry


On Wed, 10 Dec 2025 at 21:28, Evan Jones via dev <de...@cassandra.apache.org>
wrote:

> I happened to look at profiles of Cassandra in Datadog's development
> environment, and I noticed a number of our Cassandra clusters report using
> something like 1-4% of CPU in the JDK Thread.setName function. This seems
> like a misconfiguration in our environment, or a minor Cassandra
> performance bug: 2% of CPU for some debugging information seems expensive.
> We must be calling this function too often from SEPWorker.run()? If anyone
> has suggestions for how we might be able to reduce this, I would be happy
> to attempt to turn it into a patch.
>
> Additional Details:
>
> The attached image shows the Datadog profiler's flamegraph [1]. This shows
> Cassandra 5.0.6 running with OpenJDK 17 spending 2.34% of the CPU samples
> in Thread.setName, which is aggregated from 1 day of usage.
>
> The text version of the call stack is below.
>
> Thread.setName function is called by SEPWorker.run():
> https://github.com/apache/cassandra/blob/trunk/src/java/org/apache/cassandra/concurrent/SEPWorker.java#L132
>
> It appears this tries to only set the name when it changes states, but I
> guess for our applications it might be changing states fairly quickly?
>
> If anyone has any suggestions for how we might resolve this, I would love
> to help test and/or submit a fix. Thanks!
>
> Evan Jones
>
>
> Profiler call stack:
>
> Thread::call_run()
> javaThread::thread_main_inner()
> thread_entry(javaThread*)
> JavaCalls::call_virtual(javaValue*, Handle, Klass*, Symbol*, JavaThread*)
> JavaCalls::call_helper(javaValue*, methodHandle const&,
> JavaCallArguments*, JavaThread*)
> StubRoutines (1) {call_stub_return_address}
> Thread.run()
> FastThreadLocalRunnable.run()
> SEPWorker.run()
> Thread.setName(String)
> Thread.setNativeName(String)
> JVM_SetNativeThreadName(libjvm.so)
>
> [1] https://github.com/DataDog/dd-otel-host-profiler
>
> [image: Screenshot 2025-12-10 at 16.18.36.png]
>


-- 
Dmitry Konstantinov