Skip to content

Commit 32f166d

Browse files
authored
Transport threads and _hot_threads (#90482)
Add special handling for reported CPU time of transport threads in the hot threads report. Transport threads always run, but their other time is typically idle instead of actually runnable. Closes #90334
1 parent bc49392 commit 32f166d

File tree

4 files changed

+145
-30
lines changed

4 files changed

+145
-30
lines changed

docs/changelog/90482.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 90482
2+
summary: Transport threads and `_hot_threads`
3+
area: Infra/Core
4+
type: enhancement
5+
issues:
6+
- 90334

server/src/main/java/org/elasticsearch/monitor/jvm/HotThreads.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.common.unit.ByteSizeValue;
1616
import org.elasticsearch.common.util.Maps;
1717
import org.elasticsearch.core.TimeValue;
18+
import org.elasticsearch.transport.Transports;
1819

1920
import java.lang.management.ManagementFactory;
2021
import java.lang.management.ThreadInfo;
@@ -313,12 +314,15 @@ String innerDetect(ThreadMXBean threadBean, SunThreadInfo sunThreadInfo, long cu
313314
case CPU -> {
314315
double percentCpu = getTimeSharePercentage(topThread.getCpuTime());
315316
double percentOther = getTimeSharePercentage(topThread.getOtherTime());
317+
double percentTotal = (Transports.isTransportThread(threadName)) ? percentCpu : percentOther + percentCpu;
318+
String otherLabel = (Transports.isTransportThread(threadName)) ? "idle" : "other";
316319
sb.append(
317320
String.format(
318321
Locale.ROOT,
319-
"%n%4.1f%% [cpu=%1.1f%%, other=%1.1f%%] (%s out of %s) %s usage by thread '%s'%n",
320-
percentOther + percentCpu,
322+
"%n%4.1f%% [cpu=%1.1f%%, %s=%1.1f%%] (%s out of %s) %s usage by thread '%s'%n",
323+
percentTotal,
321324
percentCpu,
325+
otherLabel,
322326
percentOther,
323327
TimeValue.timeValueNanos(topThread.getCpuTime() + topThread.getOtherTime()),
324328
interval,
@@ -412,8 +416,8 @@ static int similarity(ThreadInfo threadInfo, ThreadInfo threadInfo0) {
412416

413417
static class ThreadTimeAccumulator {
414418
private final long threadId;
419+
private final String threadName;
415420
private final TimeValue interval;
416-
417421
private long cpuTime;
418422
private long blockedTime;
419423
private long waitedTime;
@@ -425,6 +429,7 @@ static class ThreadTimeAccumulator {
425429
this.cpuTime = cpuTime;
426430
this.allocatedBytes = allocatedBytes;
427431
this.threadId = info.getThreadId();
432+
this.threadName = info.getThreadName();
428433
this.interval = interval;
429434
}
430435

@@ -457,6 +462,8 @@ public long getRunnableTime() {
457462
// not running, or it has been asleep forever.
458463
if (getCpuTime() == 0) {
459464
return 0;
465+
} else if (Transports.isTransportThread(threadName)) {
466+
return getCpuTime();
460467
}
461468
return Math.max(interval.nanos() - getWaitedTime() - getBlockedTime(), 0);
462469
}
@@ -468,7 +475,7 @@ public long getOtherTime() {
468475
return 0;
469476
}
470477

471-
return Math.max(getRunnableTime() - getCpuTime(), 0);
478+
return Math.max(interval.nanos() - getWaitedTime() - getBlockedTime() - getCpuTime(), 0);
472479
}
473480

474481
public long getBlockedTime() {

server/src/main/java/org/elasticsearch/transport/Transports.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,16 @@ public enum Transports {
3737
* networking threads.
3838
*/
3939
public static boolean isTransportThread(Thread t) {
40-
final String threadName = t.getName();
40+
return isTransportThread(t.getName());
41+
}
42+
43+
/**
44+
* Utility method to detect whether a thread is a network thread. Typically
45+
* used in assertions to make sure that we do not call blocking code from
46+
* networking threads.
47+
* @param threadName the name of the thread
48+
*/
49+
public static boolean isTransportThread(String threadName) {
4150
for (String s : TRANSPORT_THREAD_NAMES) {
4251
if (threadName.contains(s)) {
4352
return true;

0 commit comments

Comments
 (0)