| 1 | package net.minecraft.util.monitoring.jmx;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.lang.management.ManagementFactory;
|
| 5 | import java.util.Arrays;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.function.Function;
|
| 10 | import java.util.function.Supplier;
|
| 11 | import java.util.stream.Collectors;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import javax.management.Attribute;
|
| 14 | import javax.management.AttributeList;
|
| 15 | import javax.management.DynamicMBean;
|
| 16 | import javax.management.InstanceAlreadyExistsException;
|
| 17 | import javax.management.MBeanAttributeInfo;
|
| 18 | import javax.management.MBeanInfo;
|
| 19 | import javax.management.MBeanNotificationInfo;
|
| 20 | import javax.management.MBeanRegistrationException;
|
| 21 | import javax.management.MalformedObjectNameException;
|
| 22 | import javax.management.NotCompliantMBeanException;
|
| 23 | import javax.management.ObjectName;
|
| 24 | import net.minecraft.server.MinecraftServer;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 | import org.slf4j.Logger;
|
| 27 |
|
| 28 | public final class MinecraftServerStatistics implements DynamicMBean {
|
| 29 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 30 | private final MinecraftServer server;
|
| 31 | private final MBeanInfo mBeanInfo;
|
| 32 | private final Map<String, MinecraftServerStatistics.AttributeDescription> attributeDescriptionByName = Stream.of(
|
| 33 | new MinecraftServerStatistics.AttributeDescription("tickTimes", this::getTickTimes, "Historical tick times (ms)", long[].class),
|
| 34 | new MinecraftServerStatistics.AttributeDescription("averageTickTime", this::getAverageTickTime, "Current average tick time (ms)", long.class)
|
| 35 | )
|
| 36 | .collect(Collectors.toMap(attributeDescription -> attributeDescription.name, Function.identity()));
|
| 37 |
|
| 38 | private MinecraftServerStatistics(final MinecraftServer server) {
|
| 39 | this.server = server;
|
| 40 | MBeanAttributeInfo[] mBeanAttributeInfos = this.attributeDescriptionByName
|
| 41 | .values()
|
| 42 | .stream()
|
| 43 | .map(MinecraftServerStatistics.AttributeDescription::asMBeanAttributeInfo)
|
| 44 | .toArray(MBeanAttributeInfo[]::new);
|
| 45 | this.mBeanInfo = new MBeanInfo(
|
| 46 | MinecraftServerStatistics.class.getSimpleName(), "metrics for dedicated server", mBeanAttributeInfos, null, null, new MBeanNotificationInfo[0]
|
| 47 | );
|
| 48 | }
|
| 49 |
|
| 50 | public static void registerJmxMonitoring(final MinecraftServer server) {
|
| 51 | try {
|
| 52 | ManagementFactory.getPlatformMBeanServer().registerMBean(new MinecraftServerStatistics(server), new ObjectName("net.minecraft.server:type=Server"));
|
| 53 | } catch (MalformedObjectNameException | InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
|
| 54 | LOGGER.warn("Failed to initialise server as JMX bean", e);
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | private float getAverageTickTime() {
|
| 59 | return this.server.getCurrentSmoothedTickTime();
|
| 60 | }
|
| 61 |
|
| 62 | private long[] getTickTimes() {
|
| 63 | return this.server.getTickTimesNanos();
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public @Nullable Object getAttribute(final String attribute) {
|
| 68 | MinecraftServerStatistics.AttributeDescription attributeDescription = this.attributeDescriptionByName.get(attribute);
|
| 69 | return attributeDescription == null ? null : attributeDescription.getter.get();
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public void setAttribute(final Attribute attribute) {
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public AttributeList getAttributes(final String[] attributes) {
|
| 78 | List<Attribute> attributeList = Arrays.stream(attributes)
|
| 79 | .map(this.attributeDescriptionByName::get)
|
| 80 | .filter(Objects::nonNull)
|
| 81 | .map(attributeDescription -> new Attribute(attributeDescription.name, attributeDescription.getter.get()))
|
| 82 | .collect(Collectors.toList());
|
| 83 | return new AttributeList(attributeList);
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public AttributeList setAttributes(final AttributeList attributes) {
|
| 88 | return new AttributeList();
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public @Nullable Object invoke(final String actionName, final Object[] params, final String[] signature) {
|
| 93 | return null;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public MBeanInfo getMBeanInfo() {
|
| 98 | return this.mBeanInfo;
|
| 99 | }
|
| 100 |
|
| 101 | private static final class AttributeDescription {
|
| 102 | private final String name;
|
| 103 | private final Supplier<Object> getter;
|
| 104 | private final String description;
|
| 105 | private final Class<?> type;
|
| 106 |
|
| 107 | private AttributeDescription(final String name, final Supplier<Object> getter, final String description, final Class<?> type) {
|
| 108 | this.name = name;
|
| 109 | this.getter = getter;
|
| 110 | this.description = description;
|
| 111 | this.type = type;
|
| 112 | }
|
| 113 |
|
| 114 | private MBeanAttributeInfo asMBeanAttributeInfo() {
|
| 115 | return new MBeanAttributeInfo(this.name, this.type.getSimpleName(), this.description, true, false, false);
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 |
|