Skip to content

Commit

Permalink
import aikar timings
Browse files Browse the repository at this point in the history
  • Loading branch information
JblusItsMe committed Aug 20, 2024
1 parent 53af1e2 commit 08dfba4
Show file tree
Hide file tree
Showing 9 changed files with 1,380 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/main/java/co/aikar/timings/FullServerTickTiming.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* This file is licensed under the MIT License (MIT).
*
* Copyright (c) 2014 Daniel Ennis <http://aikar.co>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package co.aikar.timings;

import static co.aikar.timings.TimingIdentifier.DEFAULT_GROUP;
import static co.aikar.timings.TimingsManager.*;

public class FullServerTickTiming extends Timing {
private static final TimingIdentifier IDENTIFIER = new TimingIdentifier(DEFAULT_GROUP.name, "Full Server Tick", null);
final TimingData minuteData;
double avgFreeMemory = -1D;
double avgUsedMemory = -1D;

FullServerTickTiming() {
super(IDENTIFIER);
this.minuteData = new TimingData(this.id);

TIMING_MAP.put(IDENTIFIER, this);
}

@Override
public Timing startTiming() {
if (TimingsManager.needsFullReset) {
TimingsManager.resetTimings();
} else if (TimingsManager.needsRecheckEnabled) {
TimingsManager.recheckEnabled();
}
super.startTiming();
return this;
}

@Override
public void stopTiming() {
super.stopTiming();
if (!this.enabled) {
return;
}

if (TimingsHistory.timedTicks % 20 == 0) {
final Runtime runtime = Runtime.getRuntime();
double usedMemory = runtime.totalMemory() - runtime.freeMemory();
double freeMemory = runtime.maxMemory() - usedMemory;

if (this.avgFreeMemory == -1) {
this.avgFreeMemory = freeMemory;
} else {
this.avgFreeMemory = (this.avgFreeMemory * (59 / 60D)) + (freeMemory * (1 / 60D));
}

if (this.avgUsedMemory == -1) {
this.avgUsedMemory = usedMemory;
} else {
this.avgUsedMemory = (this.avgUsedMemory * (59 / 60D)) + (usedMemory * (1 / 60D));
}
}

long start = System.nanoTime();
TimingsManager.tick();
long diff = System.nanoTime() - start;

CURRENT = Timings.timingsTickTimer;
Timings.timingsTickTimer.addDiff(diff);
//addDiff for timingsTickTimer incremented this, bring it back down to 1 per tick.
this.record.curTickCount--;
this.minuteData.curTickTotal = this.record.curTickTotal;
this.minuteData.curTickCount = 1;
boolean violated = isViolated();
this.minuteData.tick(violated);
Timings.timingsTickTimer.tick(violated);
tick(violated);

if (TimingsHistory.timedTicks % 1200 == 0) {
MINUTE_REPORTS.add(new TimingsHistory.MinuteReport());
TimingsHistory.resetTicks(false);
this.minuteData.reset();
}

if (TimingsHistory.timedTicks % Timings.getHistoryInterval() == 0) {
TimingsManager.HISTORY.add(new TimingsHistory());
TimingsManager.resetTimings();
}
}

boolean isViolated() {
return this.record.curTickTotal > 50000000;
}
}
169 changes: 169 additions & 0 deletions src/main/java/co/aikar/timings/Timing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* This file is licensed under the MIT License (MIT).
*
* Copyright (c) 2014 Daniel Ennis <http://aikar.co>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package co.aikar.timings;

import java.util.HashMap;
import java.util.Map;

public class Timing implements AutoCloseable {
private static int idPool = 1;
final int id = idPool++;

final String name;
private final boolean verbose;

final Map<Integer, TimingData> children = new HashMap<>();
private Timing parent;

private final Timing groupTiming;
final TimingData record;

private long start = 0;
private int timingDepth = 0;
private boolean added;
boolean timed;
boolean enabled;

Timing(TimingIdentifier id) {
if (id.name.startsWith("##")) {
this.verbose = true;
this.name = id.name.substring(3);
} else {
this.name = id.name;
this.verbose = false;
}

this.record = new TimingData(this.id);
this.groupTiming = id.groupTiming;

TimingIdentifier.getGroup(id.group).timings.add(this);
this.checkEnabled();
}

final void checkEnabled() {
this.enabled = Timings.isTimingsEnabled() && (!this.verbose || Timings.isVerboseEnabled());
}

void tick(boolean violated) {
if (this.timingDepth != 0 || this.record.curTickCount == 0) {
this.timingDepth = 0;
this.start = 0;
return;
}

this.record.tick(violated);
for (TimingData data : this.children.values()) {
data.tick(violated);
}
}

public Timing startTiming() {
if (!this.enabled) {
return this;
}

if (++this.timingDepth == 1) {
this.start = System.nanoTime();
this.parent = TimingsManager.CURRENT;
TimingsManager.CURRENT = this;
}

return this;
}

public void stopTiming() {
if (!this.enabled) {
return;
}

if (--this.timingDepth == 0 && this.start != 0) {
this.addDiff(System.nanoTime() - this.start);
this.start = 0;
}
}

public void abort() {
if (this.enabled && this.timingDepth > 0) {
this.start = 0;
}
}

void addDiff(long diff) {
if (TimingsManager.CURRENT == this) {
TimingsManager.CURRENT = this.parent;
if (this.parent != null) {
if (!this.parent.children.containsKey(this.id))
this.parent.children.put(this.id, new TimingData(this.id));
this.parent.children.get(this.id).add(diff);
}
}

this.record.add(diff);
if (!this.added) {
this.added = true;
this.timed = true;
TimingsManager.TIMINGS.add(this);
}

if (this.groupTiming != null) {
this.groupTiming.addDiff(diff);

if (!this.groupTiming.children.containsKey(this.id))
this.groupTiming.children.put(this.id, new TimingData(this.id));
this.groupTiming.children.get(this.id).add(diff);
}
}

void reset(boolean full) {
this.record.reset();
if (full) {
this.timed = false;
}
this.start = 0;
this.timingDepth = 0;
this.added = false;
this.children.clear();
this.checkEnabled();
}

@Override
public boolean equals(Object o) {
return (o instanceof Timing && this == o);
}

@Override
public int hashCode() {
return this.id;
}

//For try-with-resources
@Override
public void close() {
this.stopTiming();
}

boolean isSpecial() {
return this == Timings.fullServerTickTimer || this == Timings.timingsTickTimer;
}
}
90 changes: 90 additions & 0 deletions src/main/java/co/aikar/timings/TimingData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* This file is licensed under the MIT License (MIT).
*
* Copyright (c) 2014 Daniel Ennis <http://aikar.co>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package co.aikar.timings;

import com.fasterxml.jackson.databind.node.ArrayNode;
import org.cloudburstmc.server.timings.JsonUtil;

class TimingData {
private int id;
int count = 0;
private int lagCount = 0;
long totalTime = 0;
private long lagTotalTime = 0;

int curTickCount = 0;
int curTickTotal = 0;

TimingData(int id) {
this.id = id;
}

TimingData(TimingData data) {
this.id = data.id;
this.count = data.count;
this.lagCount = data.lagCount;
this.totalTime = data.totalTime;
this.lagTotalTime = data.lagTotalTime;
}

void add(long diff) {
++this.curTickCount;
this.curTickTotal += diff;
}

void tick(boolean violated) {
this.count += this.curTickCount;
this.totalTime += this.curTickTotal;

if (violated) {
this.lagCount += this.curTickCount;
this.lagTotalTime += this.curTickTotal;
}

this.curTickCount = 0;
this.curTickTotal = 0;
}

void reset() {
this.count = 0;
this.lagCount = 0;
this.totalTime = 0;
this.lagTotalTime = 0;
this.curTickCount = 0;
this.curTickTotal = 0;
}

protected TimingData clone() {
return new TimingData(this);
}

ArrayNode export() {
ArrayNode array = JsonUtil.toArray(this.id, this.count, this.totalTime);
if (this.lagCount > 0) {
array.add(this.lagCount);
array.add(this.lagTotalTime);
}
return array;
}
}
Loading

1 comment on commit 08dfba4

@AzaleeX
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have imported this but without a certain something

Please sign in to comment.