Skip to content

Commit

Permalink
Added basic profiler implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed May 6, 2023
1 parent b58c882 commit 7d937f6
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 2 deletions.
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.level.block.Block;
Expand Down Expand Up @@ -158,6 +159,11 @@ public Object createMenuInventoryHolder(InventoryType inventoryType, InventoryHo
return menuCreator == null ? null : menuCreator.apply(defaultHolder, title);
}

@Override
public double getCurrentTps() {
return Bukkit.getTPS()[0];
}

private interface MenuCreator extends BiFunction<InventoryHolder, String, Container> {

}
Expand Down
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.level.block.Block;
Expand Down Expand Up @@ -160,6 +161,11 @@ public Object createMenuInventoryHolder(InventoryType inventoryType, InventoryHo
return menuCreator == null ? null : menuCreator.apply(defaultHolder, title);
}

@Override
public double getCurrentTps() {
return Bukkit.getTPS()[0];
}

private interface MenuCreator extends BiFunction<InventoryHolder, String, Container> {
}

Expand Down
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.level.block.Block;
Expand Down Expand Up @@ -160,6 +161,11 @@ public int getMaxWorldSize() {
return Bukkit.getMaxWorldSize();
}

@Override
public double getCurrentTps() {
return Bukkit.getTPS()[0];
}

private interface MenuCreator extends BiFunction<InventoryHolder, String, Container> {
}

Expand Down
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.level.block.Block;
Expand Down Expand Up @@ -160,6 +161,11 @@ public int getMaxWorldSize() {
return Bukkit.getMaxWorldSize();
}

@Override
public double getCurrentTps() {
return Bukkit.getTPS()[0];
}

private interface MenuCreator extends BiFunction<InventoryHolder, String, Container> {
}

Expand Down
Expand Up @@ -160,6 +160,11 @@ public int getMaxWorldSize() {
return Bukkit.getMaxWorldSize();
}

@Override
public double getCurrentTps() {
return Bukkit.getTPS()[0];
}

private interface MenuCreator extends BiFunction<InventoryHolder, String, Container> {
}

Expand Down
Expand Up @@ -160,6 +160,11 @@ public int getMaxWorldSize() {
return Bukkit.getMaxWorldSize();
}

@Override
public double getCurrentTps() {
return Bukkit.getTPS()[0];
}

private interface MenuCreator extends BiFunction<InventoryHolder, String, Container> {
}

Expand Down
Expand Up @@ -13,6 +13,8 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.commands.SummonCommand;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.level.block.Block;
Expand Down Expand Up @@ -160,6 +162,11 @@ public int getMaxWorldSize() {
return Bukkit.getMaxWorldSize();
}

@Override
public double getCurrentTps() {
return MinecraftServer.getServer().tps1.getAverage();
}

private interface MenuCreator extends BiFunction<InventoryHolder, String, Container> {
}

Expand Down
Expand Up @@ -118,6 +118,11 @@ public int getMaxWorldSize() {
return server.getPropertyManager().getInt("max-world-size", 29999984);
}

@Override
public double getCurrentTps() {
return Bukkit.getTPS()[0];
}

@Override
public Object createMenuInventoryHolder(InventoryType inventoryType, InventoryHolder defaultHolder, String title) {
return defaultHolder;
Expand Down
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.server.v1_16_R3.IChatBaseComponent;
import net.minecraft.server.v1_16_R3.IInventory;
import net.minecraft.server.v1_16_R3.IRegistry;
import net.minecraft.server.v1_16_R3.MinecraftServer;
import net.minecraft.server.v1_16_R3.World;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -146,6 +147,11 @@ public int getMaxWorldSize() {
return Bukkit.getMaxWorldSize();
}

@Override
public double getCurrentTps() {
return Bukkit.getTPS()[0];
}

@Override
public Object createMenuInventoryHolder(InventoryType inventoryType, InventoryHolder defaultHolder, String title) {
MenuCreator menuCreator = MENUS_HOLDER_CREATORS.get(inventoryType);
Expand Down
Expand Up @@ -120,4 +120,9 @@ public int getMaxWorldSize() {
return server.getPropertyManager().getInt("max-world-size", 29999984);
}

@Override
public double getCurrentTps() {
return MinecraftServer.getServer().recentTps[0];
}

}
Expand Up @@ -135,7 +135,8 @@ public enum Debug {
PERMISSION_LOOKUP,
REPLACE_PLAYER,

SHOW_STACKTRACE;
SHOW_STACKTRACE,
PROFILER;

private static String[] DEBUG_NAMES = null;

Expand Down
Expand Up @@ -56,6 +56,14 @@ public static void errorFromFile(Throwable error, String fileName, Object first,
error.printStackTrace();
}

public static void profile(String[] profiledDataLines) {
if (!isDebugged(Debug.PROFILER))
return;

for (String line : profiledDataLines)
logInternal(Level.INFO, line);
}

public static void debug(Debug debug, Object... params) {
if (isDebugged(debug)) {
String[] classAndMethod = getClassAndMethodNames();
Expand Down
@@ -0,0 +1,27 @@
package com.bgsoftware.superiorskyblock.core.profiler;

import com.bgsoftware.superiorskyblock.core.LazyReference;
import com.bgsoftware.superiorskyblock.core.formatting.Formatters;

public enum ProfileType {

CREATE_ISLAND,
DISBAND_ISLAND,
SCHEMATIC_PLACE;

private final LazyReference<String> prettyName = new LazyReference<String>() {
@Override
protected String create() {
return Formatters.CAPITALIZED_FORMATTER.format(ProfileType.this.name());
}
};

ProfileType() {

}

public String getPrettyName() {
return this.prettyName.get();
}

}
@@ -0,0 +1,98 @@
package com.bgsoftware.superiorskyblock.core.profiler;

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.core.logging.Debug;
import com.bgsoftware.superiorskyblock.core.logging.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class Profiler {

private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();

private Profiler() {

}

private static final long INVALID_PROFILE_ID = -1;

private static final Map<Long, ProfilerSession> profilerSessions = new ConcurrentHashMap<>();
private static final AtomicLong lastProfilerId = new AtomicLong(0);

public static long start(ProfileType profileType) {
return start(profileType, 1);
}

public static long start(ProfileType profileType, int stopCount) {
if (!Log.isDebugged(Debug.PROFILER))
return INVALID_PROFILE_ID;

ProfilerSession profilerSession = new ProfilerSession(lastProfilerId.incrementAndGet(), stopCount, profileType);
profilerSessions.put(profilerSession.id, profilerSession);
return profilerSession.id;
}

public static void end(long id) {
if (id == INVALID_PROFILE_ID)
return;

ProfilerSession profilerSession = profilerSessions.get(id);

if (profilerSession == null)
return;

if (profilerSession.end()) {
profilerSessions.remove(id);
Log.profile(profilerSession.dump());
}
}

private static class ProfilerSession {

private final long id;
private final AtomicInteger stopCount;
private final ProfileType profileType;
private final ProfiledData startData;
private ProfiledData endData;

ProfilerSession(long id, int stopCount, ProfileType profileType) {
this.id = id;
this.stopCount = new AtomicInteger(stopCount);
this.profileType = profileType;
this.startData = new ProfiledData();
}

boolean end() {
boolean ended = this.stopCount.decrementAndGet() <= 0;
if (ended)
this.endData = new ProfiledData();
return ended;
}

String[] dump() {
List<String> dump = new ArrayList<>();

dump.add("Profiler #" + this.id);
dump.add(" Type: " + this.profileType.getPrettyName());
dump.add(" Time elapsed: " + TimeUnit.NANOSECONDS.toMillis(this.endData.time - this.startData.time) + "ms");
dump.add(" TPS: " + this.startData.tps + " -> " + this.endData.tps);

return dump.toArray(new String[0]);
}

}

private static class ProfiledData {

private final long time = System.nanoTime();
private final double tps = plugin.getNMSAlgorithms().getCurrentTps();

}

}
Expand Up @@ -45,6 +45,8 @@
import com.bgsoftware.superiorskyblock.core.logging.Debug;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import com.bgsoftware.superiorskyblock.core.profiler.ProfileType;
import com.bgsoftware.superiorskyblock.core.profiler.Profiler;
import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor;
import com.bgsoftware.superiorskyblock.core.threads.Synchronized;
import com.bgsoftware.superiorskyblock.island.builder.IslandBuilderImpl;
Expand Down Expand Up @@ -1401,6 +1403,8 @@ public void setDescription(String description) {

@Override
public void disbandIsland() {
long profilerId = Profiler.start(ProfileType.DISBAND_ISLAND, 2);

forEachIslandMember(Collections.emptyList(), false, islandMember -> {
if (islandMember.equals(owner)) {
owner.setIsland(null);
Expand All @@ -1427,9 +1431,11 @@ public void disbandIsland() {

plugin.getMissions().getAllMissions().forEach(this::resetMission);

resetChunks(true);
resetChunks(true, () -> Profiler.end(profilerId));

plugin.getGrid().deleteIsland(this);

Profiler.end(profilerId);
}

@Override
Expand Down
Expand Up @@ -11,6 +11,8 @@
import com.bgsoftware.superiorskyblock.core.events.EventResult;
import com.bgsoftware.superiorskyblock.core.logging.Debug;
import com.bgsoftware.superiorskyblock.core.logging.Log;
import com.bgsoftware.superiorskyblock.core.profiler.ProfileType;
import com.bgsoftware.superiorskyblock.core.profiler.Profiler;
import com.bgsoftware.superiorskyblock.island.builder.IslandBuilderImpl;
import com.google.common.base.Preconditions;
import org.bukkit.Location;
Expand Down Expand Up @@ -71,6 +73,8 @@ public CompletableFuture<IslandCreationResult> createIsland(Island.Builder build
return CompletableFuture.completedFuture(new IslandCreationResult(IslandCreationResult.Status.NAME_OCCUPIED, null, null, false));
}

long profiler = Profiler.start(ProfileType.CREATE_ISLAND);

CompletableFuture<IslandCreationResult> completableFuture = new CompletableFuture<>();

Location islandLocation = plugin.getProviders().getWorldsProvider().getNextLocation(
Expand All @@ -95,11 +99,13 @@ public CompletableFuture<IslandCreationResult> createIsland(Island.Builder build
builder.owner.getUniqueId(), builder.uuid);
completableFuture.complete(new IslandCreationResult(IslandCreationResult.Status.SUCCESS, island, islandLocation, event.getResult()));
island.getDatabaseBridge().setDatabaseBridgeMode(DatabaseBridgeMode.SAVE_DATA);
Profiler.end(profiler);
}, error -> {
island.getDatabaseBridge().setDatabaseBridgeMode(DatabaseBridgeMode.SAVE_DATA);
plugin.getProviders().getWorldsProvider().finishIslandCreation(islandLocation,
builder.owner.getUniqueId(), builder.uuid);
completableFuture.completeExceptionally(error);
Profiler.end(profiler);
});
}

Expand Down
Expand Up @@ -47,4 +47,6 @@ public interface NMSAlgorithms {

int getMaxWorldSize();

double getCurrentTps();

}

0 comments on commit 7d937f6

Please sign in to comment.