Skip to content

Commit d57d139

Browse files
committed
Properly track MSPT
1 parent b2d3d5f commit d57d139

File tree

6 files changed

+141
-60
lines changed

6 files changed

+141
-60
lines changed

platforms/sponge/build.gradle.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,30 @@
1616
*/
1717

1818
import org.spongepowered.gradle.plugin.config.PluginLoaders
19+
import org.spongepowered.gradle.vanilla.repository.MinecraftPlatform
1920
import org.spongepowered.plugin.metadata.model.PluginDependency
2021
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2122

2223
plugins {
2324
id("org.spongepowered.gradle.plugin") version("2.0.2")
25+
id("org.spongepowered.gradle.vanilla") version "0.2.1-SNAPSHOT"
2426
id("com.github.johnrengelman.shadow")
2527
}
2628

29+
minecraft {
30+
latestRelease()
31+
platform(MinecraftPlatform.SERVER)
32+
}
33+
34+
val mixinConfigsAttribute: String by extra { "unifiedmetrics.mixins.json" }
35+
tasks.withType<Jar> {
36+
manifest {
37+
attributes(
38+
mapOf("MixinConfigs" to mixinConfigsAttribute)
39+
)
40+
}
41+
}
42+
2743
sponge {
2844
apiVersion("12.0.0-SNAPSHOT")
2945
loader {
@@ -44,10 +60,12 @@ sponge {
4460

4561
dependencies {
4662
api(project(":unifiedmetrics-core"))
63+
compileOnly("org.spongepowered:mixin:0.8.6-SNAPSHOT")
4764
}
4865

4966
tasks {
5067
shadowJar {
68+
mergeServiceFiles()
5169
archiveClassifier.set("")
5270
relocate("retrofit2", "dev.cubxity.plugins.metrics.libs.retrofit2")
5371
relocate("com.charleskorn", "dev.cubxity.plugins.metrics.libs.com.charleskorn")
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* This file is part of UnifiedMetrics.
3+
*
4+
* UnifiedMetrics is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* UnifiedMetrics is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package dev.cubxity.plugins.metrics.sponge.mixins;
19+
20+
import dev.cubxity.plugins.metrics.sponge.events.TickEndEvent;
21+
import net.minecraft.server.MinecraftServer;
22+
import org.spongepowered.api.Sponge;
23+
import org.spongepowered.api.event.Cause;
24+
import org.spongepowered.api.event.EventContext;
25+
import org.spongepowered.asm.mixin.Mixin;
26+
import org.spongepowered.asm.mixin.Unique;
27+
import org.spongepowered.asm.mixin.injection.At;
28+
import org.spongepowered.asm.mixin.injection.Inject;
29+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
30+
31+
import static dev.cubxity.plugins.metrics.api.metric.collector.CollectorKt.NANOSECONDS_PER_MILLISECOND;
32+
import static dev.cubxity.plugins.metrics.api.metric.collector.CollectorKt.NANOSECONDS_PER_SECOND;
33+
34+
/**
35+
* Designed to emulate paper's tick event as closely as possible.
36+
*/
37+
@Mixin(MinecraftServer.class)
38+
public class MinecraftServerMixin {
39+
40+
@Unique
41+
private long unifiedmetrics$lastTick = 0;
42+
43+
@Inject(
44+
method = "runServer",
45+
at = @At(
46+
value = "INVOKE",
47+
target = "Lnet/minecraft/server/MinecraftServer;buildServerStatus()Lnet/minecraft/network/protocol/status/ServerStatus;"
48+
)
49+
)
50+
private void onRunServerBeforeLoop(CallbackInfo ci) {
51+
unifiedmetrics$lastTick = System.nanoTime() - ((long) NANOSECONDS_PER_SECOND / 20);
52+
}
53+
54+
@Inject(
55+
method = "tickServer",
56+
at = @At("HEAD")
57+
)
58+
private void onTickStart(CallbackInfo ci) {
59+
unifiedmetrics$lastTick = System.nanoTime();
60+
}
61+
62+
@Inject(
63+
method = "tickServer",
64+
at = @At(
65+
value = "CONSTANT",
66+
target = "Lnet/minecraft/util/profiling/ProfilerFiller;push(Ljava/lang/String;)V",
67+
args = "stringValue=tallying")
68+
)
69+
private void onTickEnd(CallbackInfo ci) {
70+
Sponge.eventManager().post(new TickEndEvent((double)(System.nanoTime() - unifiedmetrics$lastTick) / NANOSECONDS_PER_MILLISECOND, Cause.of(EventContext.empty(), Sponge.server())));
71+
}
72+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of UnifiedMetrics.
3+
*
4+
* UnifiedMetrics is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* UnifiedMetrics is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with UnifiedMetrics. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
package dev.cubxity.plugins.metrics.sponge.events
18+
19+
import org.spongepowered.api.event.Cause
20+
import org.spongepowered.api.event.impl.AbstractEvent
21+
22+
class TickEndEvent(val duration: Double, private val cause: Cause) : AbstractEvent() {
23+
24+
override fun cause(): Cause {
25+
return cause
26+
}
27+
}

platforms/sponge/src/main/kotlin/dev/cubxity/plugins/metrics/sponge/metric/tick/TickCollection.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ package dev.cubxity.plugins.metrics.sponge.metric.tick
2020
import dev.cubxity.plugins.metrics.api.metric.collector.Collector
2121
import dev.cubxity.plugins.metrics.api.metric.collector.CollectorCollection
2222
import dev.cubxity.plugins.metrics.api.metric.collector.Histogram
23+
import dev.cubxity.plugins.metrics.api.metric.collector.MILLISECONDS_PER_SECOND
2324
import dev.cubxity.plugins.metrics.api.metric.store.VolatileDoubleStore
2425
import dev.cubxity.plugins.metrics.api.metric.store.VolatileLongStore
2526
import dev.cubxity.plugins.metrics.common.metric.Metrics
2627
import dev.cubxity.plugins.metrics.sponge.bootstrap.UnifiedMetricsSpongeBootstrap
28+
import dev.cubxity.plugins.metrics.sponge.events.TickEndEvent
29+
import org.spongepowered.api.Sponge
30+
import org.spongepowered.api.event.Listener
2731

28-
class TickCollection(bootstrap: UnifiedMetricsSpongeBootstrap): CollectorCollection {
29-
30-
private val reporter = TickReporter(this, bootstrap)
32+
class TickCollection(private val bootstrap: UnifiedMetricsSpongeBootstrap) : CollectorCollection {
3133

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

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

40-
override val isAsync: Boolean = true
41-
4242
override fun initialize() {
43-
reporter.initialize()
43+
Sponge.eventManager().registerListeners(bootstrap.container, this)
4444
}
4545

4646
override fun dispose() {
47-
reporter.dispose()
47+
Sponge.eventManager().unregisterListeners(this)
48+
}
49+
50+
@Listener
51+
fun onTickEnd(event: TickEndEvent) {
52+
onTick(event.duration / MILLISECONDS_PER_SECOND)
4853
}
4954

5055
fun onTick(duration: Double) {

platforms/sponge/src/main/kotlin/dev/cubxity/plugins/metrics/sponge/metric/tick/TickReporter.kt

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"required": true,
3+
"minVersion": "0.8.2",
4+
"package": "dev.cubxity.plugins.metrics.sponge.mixins",
5+
"compatibilityLevel": "JAVA_21",
6+
"mixins": [
7+
"MinecraftServerMixin"
8+
],
9+
"injectors": {
10+
"defaultRequire": 1
11+
}
12+
}

0 commit comments

Comments
 (0)