Created a SystemProfiler for CPU and CPU parts of a frame

Created actual SystemProfiler instances for sampling CPU and GPU frame times
CPU and GPU frame times are now graphed with each system (adding them should give total frame time)
Added sample field to SystemProfiler to store value of last call to SystemProfiler#sample(long)
Removed CPU and GPU profiling from ProfilerSystem
This commit is contained in:
Collin Smith 2020-06-11 18:39:43 -07:00
parent fcd4fa607f
commit 2e9e3f2b1f
3 changed files with 33 additions and 16 deletions

View File

@ -5,6 +5,8 @@ import com.artemis.SystemInvocationStrategy;
import com.artemis.utils.Bag;
import com.artemis.utils.ImmutableBag;
import com.riiablo.Riiablo;
/**
* {@link SystemInvocationStrategy} that will create a profiler for all systems that don't already
* have one Can be used in addition to or instead of {@link com.artemis.annotations.Profile}
@ -20,11 +22,12 @@ public class ProfilerInvocationStrategy extends SystemInvocationStrategy {
private boolean initialized = false;
protected SystemProfiler frameProfiler;
protected SystemProfiler cpuProfiler;
protected SystemProfiler gpuProfiler;
protected SystemProfiler[] profilers;
@Override
protected void process() {
if (!initialized) {
initialize();
initialized = true;
@ -33,6 +36,21 @@ public class ProfilerInvocationStrategy extends SystemInvocationStrategy {
frameProfiler.start();
processProfileSystems(systems);
frameProfiler.stop();
cpuProfiler.sample = gpuProfiler.sample = 0;
for (int i = 0, s = systems.size(); s > i; i++) {
if (disabled.get(i)) continue;
SystemProfiler profiler = profilers[i];
if (profiler == null) continue;
SystemProfiler active = profiler.gpu ? gpuProfiler : cpuProfiler;
active.sample += profiler.sample;
}
cpuProfiler.sample(cpuProfiler.sample);
gpuProfiler.sample(gpuProfiler.sample);
Riiablo.metrics.cpu = cpuProfiler.getMovingAvg();
Riiablo.metrics.gpu = gpuProfiler.getMovingAvg();
}
private void processProfileSystems(Bag<BaseSystem> systems) {
@ -57,6 +75,8 @@ public class ProfilerInvocationStrategy extends SystemInvocationStrategy {
@Override
protected void initialize() {
createFrameProfiler();
createCpuProfiler();
createGpuProfiler();
createSystemProfilers();
}
@ -80,4 +100,14 @@ public class ProfilerInvocationStrategy extends SystemInvocationStrategy {
frameProfiler = SystemProfiler.create("Frame");
frameProfiler.setColor(1, 1, 1, 1);
}
private void createCpuProfiler() {
cpuProfiler = SystemProfiler.create("CPU");
cpuProfiler.setColor(0, 0, 1, 1);
}
private void createGpuProfiler() {
gpuProfiler = SystemProfiler.create("GPU");
gpuProfiler.setColor(0, 1, 0, 1);
}
}

View File

@ -11,8 +11,6 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.riiablo.Riiablo;
/**
* Example profiling system.
*
@ -55,23 +53,10 @@ public class ProfilerSystem extends BaseSystem {
@Override
protected void processSystem() {
Riiablo.metrics.cpu = 0;
Riiablo.metrics.gpu = 0;
if (!isEnabled() || !isConfigured()) {
return;
}
final SystemProfiler[] profilers = SystemProfiler.get().items;
for (int i = 0, s = SystemProfiler.size(); i < s; i++) {
SystemProfiler profiler = profilers[i];
if (profiler.system == null || !profiler.system.isEnabled()) continue;
if (profiler.gpu) {
Riiablo.metrics.gpu += profiler.getMovingAvg();
} else {
Riiablo.metrics.cpu += profiler.getMovingAvg();
}
}
checkActivationButton();
if (gui.isVisible()) {

View File

@ -155,6 +155,7 @@ public class SystemProfiler implements ArtemisProfiler {
protected int samples;
protected long total;
protected long sample;
protected Color color;
protected String name;
@ -243,6 +244,7 @@ public class SystemProfiler implements ArtemisProfiler {
* @param time in nanoseconds
*/
public void sample(long time) {
sample = time;
lastMaxCounter++;
if (time > max || lastMaxCounter > 2000) {
max = time;