Skip to content

Commit

Permalink
feat: add tracy profiler integration
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminAmos authored and soloturn committed Jul 14, 2024
1 parent 855b2be commit a22a9cc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
12 changes: 11 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ dependencies {
// Natives for JNBullet
natives(group = "org.terasology.jnbullet", name = "JNBullet", version = "1.0.4", ext = "zip")

// Natives for Tracy bindings (embedded in JAR)
natives("io.github.benjaminamos.TracyJavaBindings:TracyJavaBindings:1.0.0-SNAPSHOT")
}

tasks.register<Copy>("extractWindowsNatives") {
Expand Down Expand Up @@ -135,14 +137,22 @@ tasks.register<Copy>("extractNativeBulletNatives") {
into("$dirNatives")
}

tasks.register<Copy>("extractTracyJNINatives") {
description = "Extracts the Tracy JNI natives from the module jar"
from(configurations["natives"].filter { it.name.contains("TracyJavaBindings") }.map { zipTree(it) })
into(dirNatives)
exclude("io/**", "META-INF/**")
}

tasks.register("extractNatives") {
description = "Extracts all the native lwjgl libraries from the downloaded zip"
dependsOn(
"extractWindowsNatives",
"extractLinuxNatives",
"extractMacOSXNatives",
"extractJNLuaNatives",
"extractNativeBulletNatives"
"extractNativeBulletNatives",
"extractTracyJNINatives"
)
// specifying the outputs directory lets gradle have an up-to-date check, and automatic clean task
outputs.dir("$dirNatives")
Expand Down
2 changes: 2 additions & 0 deletions engine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ dependencies {
implementation("org.terasology.crashreporter:cr-terasology:5.0.0")

api(project(":subsystems:TypeHandlerLibrary"))

implementation("io.github.benjaminamos.TracyJavaBindings:TracyJavaBindings:1.0.0-SNAPSHOT")
}

protobuf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,10 @@ public synchronized void runMain() {
*/
@SuppressWarnings("checkstyle:EmptyBlock")
private void mainLoop() {
PerformanceMonitor.startActivity("Other");
// MAIN GAME LOOP
while (tick()) {
/* do nothing */
}
PerformanceMonitor.endActivity();
}

/**
Expand All @@ -482,6 +480,7 @@ private void mainLoop() {
* @return true if the loop requesting a tick should continue running
*/
public boolean tick() {
PerformanceMonitor.startActivity("Tick");
if (shutdownRequested) {
return false;
}
Expand Down Expand Up @@ -524,8 +523,8 @@ public boolean tick() {
}
assetTypeManager.disposedUnusedAssets();

PerformanceMonitor.endActivity();
PerformanceMonitor.rollCycle();
PerformanceMonitor.startActivity("Other");
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine.monitoring.impl;

import io.github.benjaminamos.tracy.Tracy;
import com.google.common.collect.Lists;
import com.google.common.collect.Queues;
import gnu.trove.map.TObjectDoubleMap;
Expand Down Expand Up @@ -78,6 +79,8 @@ public PerformanceMonitorImpl() {

timer = (EngineTime) CoreRegistry.get(Time.class);
mainThread = Thread.currentThread();

Tracy.startupProfiler();
}

@Override
Expand All @@ -101,6 +104,10 @@ public void rollCycle() {

currentExecutionData = new TObjectLongHashMap<>();
currentAllocationData = new TObjectLongHashMap<>();

activityStack.clear();

Tracy.markFrame();
}

@Override
Expand All @@ -111,6 +118,11 @@ public Activity startActivity(String activityName) {

ActivityInfo newActivity = new ActivityInfo(activityName).initialize();

StackWalker.StackFrame caller = java.security.AccessController.doPrivileged((java.security.PrivilegedAction<StackWalker.StackFrame>) () ->
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).walk(s -> s.skip(4).findFirst()).get());
long sourceLocation = Tracy.allocSourceLocation(caller.getLineNumber(), caller.getFileName(), caller.getClassName() + "#" + caller.getMethodName(), activityName, 0);
newActivity.zoneContext = Tracy.zoneBegin(sourceLocation, 1);

if (!activityStack.isEmpty()) {
ActivityInfo currentActivity = activityStack.peek();
currentActivity.ownTime += newActivity.startTime - ((currentActivity.resumeTime > 0)
Expand All @@ -133,6 +145,8 @@ public void endActivity() {

ActivityInfo oldActivity = activityStack.pop();

Tracy.zoneEnd(oldActivity.zoneContext);

long endTime = timer.getRealTimeInMs();
long totalTime = (oldActivity.resumeTime > 0)
? oldActivity.ownTime + endTime - oldActivity.resumeTime
Expand All @@ -152,6 +166,7 @@ public void endActivity() {
}
}


@Override
public TObjectDoubleMap<String> getRunningMean() {
TObjectDoubleMap<String> activityToMeanMap = new TObjectDoubleHashMap<>();
Expand Down Expand Up @@ -186,6 +201,8 @@ private class ActivityInfo {
public long ownTime;
public long startMem;
public long ownMem;
public StackWalker.StackFrame caller;
public Tracy.ZoneContext zoneContext;

ActivityInfo(String activityName) {
this.name = activityName;
Expand Down

0 comments on commit a22a9cc

Please sign in to comment.