| 1 | package net.minecraft.util.profiling.jfr;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.net.SocketAddress;
|
| 5 | import java.nio.file.Path;
|
| 6 | import jdk.jfr.FlightRecorder;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.network.ConnectionProtocol;
|
| 9 | import net.minecraft.network.protocol.PacketType;
|
| 10 | import net.minecraft.resources.ResourceKey;
|
| 11 | import net.minecraft.util.profiling.jfr.callback.ProfiledDuration;
|
| 12 | import net.minecraft.world.level.ChunkPos;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.chunk.storage.RegionFileVersion;
|
| 15 | import net.minecraft.world.level.chunk.storage.RegionStorageInfo;
|
| 16 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 | import org.slf4j.Logger;
|
| 19 |
|
| 20 | public interface JvmProfiler {
|
| 21 | JvmProfiler INSTANCE = Runtime.class.getModule().getLayer().findModule("jdk.jfr").isPresent() && FlightRecorder.isAvailable()
|
| 22 | ? JfrProfiler.getInstance()
|
| 23 | : new JvmProfiler.NoOpProfiler();
|
| 24 |
|
| 25 | boolean start(Environment environment);
|
| 26 |
|
| 27 | Path stop();
|
| 28 |
|
| 29 | boolean isRunning();
|
| 30 |
|
| 31 | boolean isAvailable();
|
| 32 |
|
| 33 | void onServerTick(float averageTickTime);
|
| 34 |
|
| 35 | void onClientTick(int fps);
|
| 36 |
|
| 37 | void onPacketReceived(final ConnectionProtocol protocol, final PacketType<?> packetId, final SocketAddress remoteAddress, final int readableBytes);
|
| 38 |
|
| 39 | void onPacketSent(final ConnectionProtocol protocol, final PacketType<?> packetId, final SocketAddress remoteAddress, final int writtenBytes);
|
| 40 |
|
| 41 | void onRegionFileRead(RegionStorageInfo info, ChunkPos pos, RegionFileVersion version, int readBytes);
|
| 42 |
|
| 43 | void onRegionFileWrite(RegionStorageInfo info, ChunkPos pos, RegionFileVersion version, int writtenBytes);
|
| 44 |
|
| 45 | @Nullable ProfiledDuration onWorldLoadedStarted();
|
| 46 |
|
| 47 | @Nullable ProfiledDuration onChunkGenerate(ChunkPos pos, ResourceKey<Level> dimension, String name);
|
| 48 |
|
| 49 | @Nullable ProfiledDuration onStructureGenerate(ChunkPos sourceChunkPos, ResourceKey<Level> dimension, Holder<Structure> structure);
|
| 50 |
|
| 51 | class NoOpProfiler implements JvmProfiler {
|
| 52 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 53 | private static final ProfiledDuration NO_OP_COMMIT = ignored -> {};
|
| 54 |
|
| 55 | @Override
|
| 56 | public boolean start(final Environment environment) {
|
| 57 | LOGGER.warn("Attempted to start Flight Recorder, but it's not supported on this JVM");
|
| 58 | return false;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public Path stop() {
|
| 63 | throw new IllegalStateException("Attempted to stop Flight Recorder, but it's not supported on this JVM");
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public boolean isRunning() {
|
| 68 | return false;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public boolean isAvailable() {
|
| 73 | return false;
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void onPacketReceived(final ConnectionProtocol protocol, final PacketType<?> packetId, final SocketAddress remoteAddress, final int readableBytes) {
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public void onPacketSent(final ConnectionProtocol protocol, final PacketType<?> packetId, final SocketAddress remoteAddress, final int writtenBytes) {
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void onRegionFileRead(final RegionStorageInfo info, final ChunkPos pos, final RegionFileVersion version, final int readBytes) {
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public void onRegionFileWrite(final RegionStorageInfo info, final ChunkPos pos, final RegionFileVersion version, final int writtenBytes) {
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public void onServerTick(final float averageTickTime) {
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public void onClientTick(final int fps) {
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public ProfiledDuration onWorldLoadedStarted() {
|
| 102 | return NO_OP_COMMIT;
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public @Nullable ProfiledDuration onChunkGenerate(final ChunkPos pos, final ResourceKey<Level> dimension, final String name) {
|
| 107 | return null;
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public ProfiledDuration onStructureGenerate(final ChunkPos sourceChunkPos, final ResourceKey<Level> dimension, final Holder<Structure> structure) {
|
| 112 | return NO_OP_COMMIT;
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|