Skip to content

Commit

Permalink
Properly track MSPT
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed Jan 15, 2025
1 parent b2d3d5f commit d57d139
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 60 deletions.
18 changes: 18 additions & 0 deletions platforms/sponge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,30 @@
*/

import org.spongepowered.gradle.plugin.config.PluginLoaders
import org.spongepowered.gradle.vanilla.repository.MinecraftPlatform
import org.spongepowered.plugin.metadata.model.PluginDependency
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
id("org.spongepowered.gradle.plugin") version("2.0.2")
id("org.spongepowered.gradle.vanilla") version "0.2.1-SNAPSHOT"
id("com.github.johnrengelman.shadow")
}

minecraft {
latestRelease()
platform(MinecraftPlatform.SERVER)
}

val mixinConfigsAttribute: String by extra { "unifiedmetrics.mixins.json" }
tasks.withType<Jar> {
manifest {
attributes(
mapOf("MixinConfigs" to mixinConfigsAttribute)
)
}
}

sponge {
apiVersion("12.0.0-SNAPSHOT")
loader {
Expand All @@ -44,10 +60,12 @@ sponge {

dependencies {
api(project(":unifiedmetrics-core"))
compileOnly("org.spongepowered:mixin:0.8.6-SNAPSHOT")
}

tasks {
shadowJar {
mergeServiceFiles()
archiveClassifier.set("")
relocate("retrofit2", "dev.cubxity.plugins.metrics.libs.retrofit2")
relocate("com.charleskorn", "dev.cubxity.plugins.metrics.libs.com.charleskorn")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* This file is part of UnifiedMetrics.
*
* UnifiedMetrics is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* UnifiedMetrics 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.cubxity.plugins.metrics.sponge.mixins;

import dev.cubxity.plugins.metrics.sponge.events.TickEndEvent;
import net.minecraft.server.MinecraftServer;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.Cause;
import org.spongepowered.api.event.EventContext;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static dev.cubxity.plugins.metrics.api.metric.collector.CollectorKt.NANOSECONDS_PER_MILLISECOND;
import static dev.cubxity.plugins.metrics.api.metric.collector.CollectorKt.NANOSECONDS_PER_SECOND;

/**
* Designed to emulate paper's tick event as closely as possible.
*/
@Mixin(MinecraftServer.class)
public class MinecraftServerMixin {

@Unique
private long unifiedmetrics$lastTick = 0;

@Inject(
method = "runServer",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/MinecraftServer;buildServerStatus()Lnet/minecraft/network/protocol/status/ServerStatus;"
)
)
private void onRunServerBeforeLoop(CallbackInfo ci) {
unifiedmetrics$lastTick = System.nanoTime() - ((long) NANOSECONDS_PER_SECOND / 20);
}

@Inject(
method = "tickServer",
at = @At("HEAD")
)
private void onTickStart(CallbackInfo ci) {
unifiedmetrics$lastTick = System.nanoTime();
}

@Inject(
method = "tickServer",
at = @At(
value = "CONSTANT",
target = "Lnet/minecraft/util/profiling/ProfilerFiller;push(Ljava/lang/String;)V",
args = "stringValue=tallying")
)
private void onTickEnd(CallbackInfo ci) {
Sponge.eventManager().post(new TickEndEvent((double)(System.nanoTime() - unifiedmetrics$lastTick) / NANOSECONDS_PER_MILLISECOND, Cause.of(EventContext.empty(), Sponge.server())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of UnifiedMetrics.
*
* UnifiedMetrics is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* UnifiedMetrics 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
*/
package dev.cubxity.plugins.metrics.sponge.events

import org.spongepowered.api.event.Cause
import org.spongepowered.api.event.impl.AbstractEvent

class TickEndEvent(val duration: Double, private val cause: Cause) : AbstractEvent() {

override fun cause(): Cause {
return cause
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ package dev.cubxity.plugins.metrics.sponge.metric.tick
import dev.cubxity.plugins.metrics.api.metric.collector.Collector
import dev.cubxity.plugins.metrics.api.metric.collector.CollectorCollection
import dev.cubxity.plugins.metrics.api.metric.collector.Histogram
import dev.cubxity.plugins.metrics.api.metric.collector.MILLISECONDS_PER_SECOND
import dev.cubxity.plugins.metrics.api.metric.store.VolatileDoubleStore
import dev.cubxity.plugins.metrics.api.metric.store.VolatileLongStore
import dev.cubxity.plugins.metrics.common.metric.Metrics
import dev.cubxity.plugins.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap
import dev.cubxity.plugins.metrics.sponge.events.TickEndEvent
import org.spongepowered.api.Sponge
import org.spongepowered.api.event.Listener

class TickCollection(bootstrap: UnifiedMetricsSpongeBootstrap): CollectorCollection {

private val reporter = TickReporter(this, bootstrap)
class TickCollection(private val bootstrap: UnifiedMetricsSpongeBootstrap) : CollectorCollection {

private val tickDuration = Histogram(
Metrics.Server.TickDurationSeconds,
Expand All @@ -37,14 +39,17 @@ class TickCollection(bootstrap: UnifiedMetricsSpongeBootstrap): CollectorCollect

override val collectors: List<Collector> = listOf(tickDuration)

override val isAsync: Boolean = true

override fun initialize() {
reporter.initialize()
Sponge.eventManager().registerListeners(bootstrap.container, this)
}

override fun dispose() {
reporter.dispose()
Sponge.eventManager().unregisterListeners(this)
}

@Listener
fun onTickEnd(event: TickEndEvent) {
onTick(event.duration / MILLISECONDS_PER_SECOND)
}

fun onTick(duration: Double) {
Expand Down

This file was deleted.

12 changes: 12 additions & 0 deletions platforms/sponge/src/main/resources/unifiedmetrics.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"required": true,
"minVersion": "0.8.2",
"package": "dev.cubxity.plugins.metrics.sponge.mixins",
"compatibilityLevel": "JAVA_21",
"mixins": [
"MinecraftServerMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit d57d139

Please sign in to comment.