Skip to content
Permalink
Browse files

Introduce Profiler interface

  • Loading branch information...
skalarproduktraum committed Nov 29, 2019
1 parent 3a1fd4c commit 097ba70aea721ccda38a04e089a43f352c8ed39e
@@ -12,7 +12,8 @@ import graphics.scenery.net.NodePublisher
import graphics.scenery.net.NodeSubscriber
import graphics.scenery.repl.REPL
import graphics.scenery.utils.LazyLogger
import graphics.scenery.utils.Remotery
import graphics.scenery.utils.Profiler
import graphics.scenery.utils.RemoteryProfiler
import graphics.scenery.utils.Renderdoc
import graphics.scenery.utils.SceneryPanel
import graphics.scenery.utils.Statistics
@@ -22,7 +23,6 @@ import org.scijava.ui.behaviour.ClickBehaviour
import java.lang.Boolean.parseBoolean
import java.lang.management.ManagementFactory
import java.nio.file.Paths
import java.rmi.Remote
import java.util.*
import java.util.concurrent.CountDownLatch
import kotlin.concurrent.thread
@@ -145,7 +145,7 @@ open class SceneryBase @JvmOverloads constructor(var applicationName: String,
running = true

if(parseBoolean(System.getProperty("scenery.Profiler", "false"))) {
hub.add(Remotery(hub))
hub.add(RemoteryProfiler(hub))
}

val headless = parseBoolean(System.getProperty("scenery.Headless", "false"))
@@ -240,7 +240,7 @@ open class SceneryBase @JvmOverloads constructor(var applicationName: String,
val frameTimes = ArrayDeque<Float>(16)
val frameTimeKeepCount = 16

val profiler = hub.get<Remotery>()
val profiler = hub.get<Profiler>()

while (!shouldClose || gracePeriod > 0) {
runtime = (System.nanoTime() - startTime) / 1000000f
@@ -339,7 +339,7 @@ open class SceneryBase @JvmOverloads constructor(var applicationName: String,
renderer?.close()
renderdoc?.close()

hub.get<Remotery>()?.close()
hub.get<Profiler>()?.close()
}

/**
@@ -1863,7 +1863,7 @@ open class VulkanRenderer(hub: Hub,
* This function renders the scene
*/
override fun render() = runBlocking {
val profiler = hub?.get<Remotery>()
val profiler = hub?.get<Profiler>()
// profiler?.begin("Renderer.Housekeeping")
val swapchainChanged = pollEvents()
@@ -0,0 +1,43 @@
package graphics.scenery.utils

import graphics.scenery.Hubable

/**
* Interface for profilers.
*
* @author Ulrik Guenther <hello@ulrik.is>
*/
interface Profiler: Hubable {
/**
* Profiler sample type.
*/
enum class SampleType {
/** Default behaviour, samples will not be merged. */
Default,
/** Search parent for samples of the same name and merge the timing instead of adding a new sample. */
Aggregate,
/** Merge sample with the parent's if it is the same sample. */
Recursive
}

/**
* Begins a new sample with [name]. Default type is [SampleType.Default].
* Calls to [begin] and [end] behave like brackets an can be nested.
*/
fun begin(name: String = "", type: SampleType = SampleType.Default)

/**
* Ends the current sample. Must have a corresponding [begin].
*/
fun end()

/**
* Sets the current thread name to [name].
*/
fun setThreadName(name: String)

/**
* Closes the profiler instance and cleans up.
*/
fun close()
}
@@ -3,9 +3,6 @@ package graphics.scenery.utils
import graphics.scenery.Hub
import graphics.scenery.Hubable
import org.lwjgl.PointerBuffer
import org.lwjgl.util.remotery.Remotery.RMTSF_Aggregate
import org.lwjgl.util.remotery.Remotery.RMTSF_None
import org.lwjgl.util.remotery.Remotery.RMTSF_Recursive
import org.lwjgl.util.remotery.Remotery.rmt_BeginCPUSample
import org.lwjgl.util.remotery.Remotery.rmt_CreateGlobalInstance
import org.lwjgl.util.remotery.Remotery.rmt_DestroyGlobalInstance
@@ -21,22 +18,10 @@ import org.lwjgl.util.remotery.Remotery.rmt_SetCurrentThreadName
*
* @author Ulrik Guenther <hello@ulrik.is>
*/
class Remotery(override var hub : Hub?) : Hubable, AutoCloseable {
class RemoteryProfiler(override var hub : Hub?) : Hubable, Profiler, AutoCloseable {
private var instance = PointerBuffer.allocateDirect(1)
private val logger by LazyLogger()

/**
* Remotery sample type.
*/
enum class SampleType(val t: Int) {
/** Default behaviour, samples will not be merged. */
Default(RMTSF_None),
/** Search parent for samples of the same name and merge the timing instead of adding a new sample. */
Aggregate(RMTSF_Aggregate),
/** Merge sample with the parent's if it is the same sample. */
Recursive(RMTSF_Recursive)
}

init {
val error = rmt_CreateGlobalInstance(instance)
if(error == 0) {
@@ -49,22 +34,22 @@ class Remotery(override var hub : Hub?) : Hubable, AutoCloseable {
/**
* Sets the current thread name to [name].
*/
fun setThreadName(name: String) {
override fun setThreadName(name: String) {
rmt_SetCurrentThreadName(name)
}

/**
* Begins a new sample with [name]. Default type is [SampleType.Default].
* Calls to [begin] and [end] behave like brackets an can be nested.
*/
fun begin(name: String = "", type: SampleType = SampleType.Default) {
override fun begin(name: String, type: Profiler.SampleType) {
rmt_BeginCPUSample(name, type.ordinal, null)
}

/**
* Ends the current sample. Must have a corresponding [begin].
*/
fun end() {
override fun end() {
rmt_EndCPUSample()
}

@@ -2,26 +2,27 @@ package graphics.scenery.tests.unit.utils

import graphics.scenery.Hub
import graphics.scenery.numerics.Random
import graphics.scenery.utils.Remotery
import graphics.scenery.utils.Profiler
import graphics.scenery.utils.RemoteryProfiler
import org.junit.Test
import kotlin.test.assertTrue

/**
* Unit tests for [Remotery] integration.
* Unit tests for [RemoteryProfiler] integration.
*
* @author Ulrik Guenther <hello@ulrik.is>
*/
class RemoteryTests {
class RemoteryProfilerTests {
/**
* Tests standard remotery functionality.
*/
@Test
fun testRemotery() {
val hub = Hub()
val profiler = Remotery(hub)
val profiler = RemoteryProfiler(hub)
hub.add(profiler)

assertTrue { hub.get<Remotery>() == profiler }
assertTrue { hub.get<Profiler>() == profiler }

profiler.setThreadName("MainThread")

0 comments on commit 097ba70

Please sign in to comment.
You can’t perform that action at this time.