Skip to content

Commit 02f2de3

Browse files
Speedup OsStats initialization (#118141) (#118209)
Similar to other OS/FS type stats we can optimize here. Found this as a slowdown when profiling tests in a loop during test fixing. This helps node startup and maybe more importantly test performance. No need to initialize the stats eagerly when we can just get them as we load them the first time.
1 parent 38fcaba commit 02f2de3

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

server/src/main/java/org/elasticsearch/monitor/os/OsService.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public class OsService implements ReportingService<OsInfo> {
2525

2626
private static final Logger logger = LogManager.getLogger(OsService.class);
2727

28-
private final OsProbe probe;
2928
private final OsInfo info;
3029
private final SingleObjectCache<OsStats> osStatsCache;
3130

@@ -37,10 +36,9 @@ public class OsService implements ReportingService<OsInfo> {
3736
);
3837

3938
public OsService(Settings settings) throws IOException {
40-
this.probe = OsProbe.getInstance();
4139
TimeValue refreshInterval = REFRESH_INTERVAL_SETTING.get(settings);
42-
this.info = probe.osInfo(refreshInterval.millis(), EsExecutors.nodeProcessors(settings));
43-
this.osStatsCache = new OsStatsCache(refreshInterval, probe.osStats());
40+
this.info = OsProbe.getInstance().osInfo(refreshInterval.millis(), EsExecutors.nodeProcessors(settings));
41+
this.osStatsCache = new OsStatsCache(refreshInterval);
4442
logger.debug("using refresh_interval [{}]", refreshInterval);
4543
}
4644

@@ -53,14 +51,28 @@ public OsStats stats() {
5351
return osStatsCache.getOrRefresh();
5452
}
5553

56-
private class OsStatsCache extends SingleObjectCache<OsStats> {
57-
OsStatsCache(TimeValue interval, OsStats initValue) {
58-
super(interval, initValue);
54+
private static class OsStatsCache extends SingleObjectCache<OsStats> {
55+
56+
private static final OsStats MISSING = new OsStats(
57+
0L,
58+
new OsStats.Cpu((short) 0, new double[0]),
59+
new OsStats.Mem(0, 0, 0),
60+
new OsStats.Swap(0, 0),
61+
null
62+
);
63+
64+
OsStatsCache(TimeValue interval) {
65+
super(interval, MISSING);
5966
}
6067

6168
@Override
6269
protected OsStats refresh() {
63-
return probe.osStats();
70+
return OsProbe.getInstance().osStats();
71+
}
72+
73+
@Override
74+
protected boolean needsRefresh() {
75+
return getNoRefresh() == MISSING || super.needsRefresh();
6476
}
6577
}
6678
}

0 commit comments

Comments
 (0)