Show server CPU and heap use in the studio status bar

The bridge sends a usage event every second and when a studio connects: CPU with 100 per busy thread, like top, and the Java heap in use. The status bar shows both beside the cursor and turns each warning-coloured at 80%.

v3: e1e158364 11e4ba0b1 268e72b9c 275b5965b
This commit is contained in:
piexel committed 2026-11-10 11:31:27 +01:00
1 parent d28b9d13cc
commit 723764a782
10 files changed
+243 -8

No files matched your search

@@ -0,0 +1,51 @@
/*
* Iris is a World Generator for Minecraft Servers
* Copyright (c) 2026 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.core.studio.bridge;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
/**
* What the server asks of its machine: CPU counted like {@code top}, where 100 is one thread kept busy, and the Java
* heap in use.
*
* @param cpuPercent the process's CPU use with 100 per busy thread, or null when the JVM cannot tell
* @param threads the threads the server's CPU has, so the studio can show the most {@code cpuPercent} can reach
* @param heapUsedBytes the Java heap in use
* @param heapMaxBytes the most the Java heap can grow to
*/
record ServerUsage(Double cpuPercent, int threads, long heapUsedBytes, long heapMaxBytes) {
private static final double PERCENT_PER_THREAD = 100D;
static ServerUsage sample() {
Runtime runtime = Runtime.getRuntime();
OperatingSystemMXBean system = ManagementFactory.getOperatingSystemMXBean();
return of(processCpuLoad(system), runtime.availableProcessors(), runtime.totalMemory() - runtime.freeMemory(), runtime.maxMemory());
}
/** @param processCpuLoad the share of every core the process uses, or below zero when unknown */
static ServerUsage of(double processCpuLoad, int threads, long heapUsedBytes, long heapMaxBytes) {
Double cpuPercent = processCpuLoad < 0D ? null : processCpuLoad * threads * PERCENT_PER_THREAD;
return new ServerUsage(cpuPercent, threads, heapUsedBytes, heapMaxBytes);
}
private static double processCpuLoad(OperatingSystemMXBean system) {
return system instanceof com.sun.management.OperatingSystemMXBean hotspot ? hotspot.getProcessCpuLoad() : -1D;
}
}
@@ -32,22 +32,26 @@ import java.util.concurrent.atomic.AtomicBoolean;
/**
* Server-sent events to every connected studio: {@code players} when a player moves, {@code world} when the world's
* engine changes (a hotload), and a comment now and then so a dead connection is noticed. A studio that stops
* engine changes (a hotload), {@code usage} with the server's CPU and heap every second, and a comment now and then so
* a dead connection is noticed. A studio that stops
* reading is dropped.
*/
final class StudioEvents {
private static final byte[] KEEP_ALIVE = ": alive\n\n".getBytes(StandardCharsets.UTF_8);
static final long POLL_INTERVAL_MILLIS = 250L;
private static final long KEEP_ALIVE_INTERVAL_MILLIS = 10_000L;
private static final long USAGE_INTERVAL_MILLIS = 1_000L;
private final StudioWorld world;
private final Gson gson;
private final List<OutputStream> listeners = new CopyOnWriteArrayList<>();
private final AtomicBoolean listenerJoined = new AtomicBoolean();
private final AtomicBoolean usageWanted = new AtomicBoolean();
private final AtomicBoolean hotloaded = new AtomicBoolean();
private List<GuiMarker> lastPlayers;
private Engine lastEngine;
private long lastSentMillis;
private long lastUsageMillis;
StudioEvents(StudioWorld world, Gson gson) {
this.world = world;
@@ -57,6 +61,7 @@ final class StudioEvents {
void listen(OutputStream stream) {
listeners.add(stream);
listenerJoined.set(true);
usageWanted.set(true);
}
/** The pack was hotloaded; the engine may be the same, its streams are not. */
@@ -80,7 +85,12 @@ final class StudioEvents {
lastPlayers = players;
send("players", players);
}
if (System.currentTimeMillis() - lastSentMillis > KEEP_ALIVE_INTERVAL_MILLIS) {
long now = System.currentTimeMillis();
if (usageWanted.getAndSet(false) || now - lastUsageMillis >= USAGE_INTERVAL_MILLIS) {
lastUsageMillis = now;
send("usage", ServerUsage.sample());
}
if (now - lastSentMillis > KEEP_ALIVE_INTERVAL_MILLIS) {
sendToAll(KEEP_ALIVE);
}
}
@@ -0,0 +1,38 @@
package art.arcane.iris.core.studio.bridge;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class ServerUsageTest {
@Test
public void cpuCountsAHundredPercentPerBusyThread() {
ServerUsage usage = ServerUsage.of(0.25D, 8, 1L, 2L);
assertEquals(200D, usage.cpuPercent(), 1e-9D);
assertEquals(8, usage.threads());
}
@Test
public void everyThreadBusyReachesAHundredPerThread() {
assertEquals(1600D, ServerUsage.of(1D, 16, 1L, 2L).cpuPercent(), 1e-9D);
}
@Test
public void anUnknownCpuLoadIsLeftOut() {
assertNull(ServerUsage.of(-1D, 8, 1L, 2L).cpuPercent());
}
@Test
public void aSampleHoldsTheHeapInUseWithinItsMaximum() {
ServerUsage usage = ServerUsage.sample();
assertNotNull(usage);
assertTrue(usage.threads() >= 1);
assertTrue(usage.heapUsedBytes() > 0L);
assertTrue(usage.heapUsedBytes() <= usage.heapMaxBytes());
}
}
@@ -211,6 +211,24 @@ public class StudioBridgeTest {
}
}
@Test
public void aNewListenerHearsTheServersUsageRightAway() throws Exception {
HttpResponse<InputStream> events = http.send(authorized("events"), HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader lines = new BufferedReader(new InputStreamReader(events.body(), StandardCharsets.UTF_8))) {
String line;
do {
line = lines.readLine();
} while (line != null && !line.equals("event: usage"));
assertEquals("event: usage", line);
JsonObject usage = JsonParser.parseString(lines.readLine().substring("data: ".length())).getAsJsonObject();
assertTrue(usage.get("threads").getAsInt() >= 1);
assertTrue(usage.get("heapUsedBytes").getAsLong() > 0L);
assertTrue(usage.get("heapMaxBytes").getAsLong() >= usage.get("heapUsedBytes").getAsLong());
assertTrue(usage.has("cpuPercent"));
}
}
private HttpResponse<String> get(String endpoint) throws Exception {
return http.send(authorized(endpoint), HttpResponse.BodyHandlers.ofString());
}