mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-26 20:06:28 -08:00
Telemetry fixes + Optimise the crap out of it
This commit is contained in:
1 parent
9a0cdb61cb
commit
f82f1eeddd
16 files changed
+806
-646
No files matched your search
@@ -16,7 +16,8 @@ data class WindowedDeviceStats(
|
||||
val packetLoss: Float?,
|
||||
)
|
||||
|
||||
private const val MAX_SAMPLE_RETENTION_MS = 2_000L
|
||||
private const val MAX_SAMPLE_RETENTION_MS = 5_000L
|
||||
private const val MIN_SAMPLES_FOR_LOSS = 20
|
||||
|
||||
fun trimSamples(samples: List<DevicePacketSample>, now: Long = System.currentTimeMillis()): List<DevicePacketSample> {
|
||||
val cutoff = now - MAX_SAMPLE_RETENTION_MS
|
||||
@@ -26,7 +27,7 @@ fun trimSamples(samples: List<DevicePacketSample>, now: Long = System.currentTim
|
||||
|
||||
fun computeWindowedStats(
|
||||
samples: List<DevicePacketSample>,
|
||||
windowMs: Long,
|
||||
windowMs: Long = 1000L,
|
||||
now: Long = System.currentTimeMillis(),
|
||||
): WindowedDeviceStats {
|
||||
if (samples.isEmpty()) {
|
||||
@@ -56,10 +57,17 @@ fun computeWindowedStats(
|
||||
rssiMax = latestRssi
|
||||
}
|
||||
|
||||
val samplesWithCounters = windowSamples.filter { it.packetsReceived != null && it.packetsLost != null }
|
||||
val packetLoss: Float? = if (samplesWithCounters.size >= 2) {
|
||||
val first = samplesWithCounters.first()
|
||||
val last = samplesWithCounters.last()
|
||||
var lossSamples = samples.filter { it.packetsReceived != null && it.packetsLost != null }
|
||||
val recentLossSamples = lossSamples.filter { it.time >= cutoff }
|
||||
if (recentLossSamples.size >= MIN_SAMPLES_FOR_LOSS) {
|
||||
lossSamples = recentLossSamples
|
||||
} else if (lossSamples.size > MIN_SAMPLES_FOR_LOSS) {
|
||||
lossSamples = lossSamples.takeLast(MIN_SAMPLES_FOR_LOSS)
|
||||
}
|
||||
|
||||
val packetLoss: Float? = if (lossSamples.size >= 2) {
|
||||
val first = lossSamples.first()
|
||||
val last = lossSamples.last()
|
||||
val dRec = last.packetsReceived!! - first.packetsReceived!!
|
||||
val dLost = last.packetsLost!! - first.packetsLost!!
|
||||
val total = dRec + dLost
|
||||
@@ -68,21 +76,6 @@ fun computeWindowedStats(
|
||||
} else {
|
||||
0f
|
||||
}
|
||||
} else if (samplesWithCounters.isNotEmpty()) {
|
||||
val current = samplesWithCounters.first()
|
||||
val prev = samples.lastOrNull { it.time < current.time && it.packetsReceived != null && it.packetsLost != null }
|
||||
if (prev != null) {
|
||||
val dRec = current.packetsReceived!! - prev.packetsReceived!!
|
||||
val dLost = current.packetsLost!! - prev.packetsLost!!
|
||||
val total = dRec + dLost
|
||||
if (total > 0) {
|
||||
(dLost.toFloat() / total.toFloat()).coerceIn(0f, 1f)
|
||||
} else {
|
||||
0f
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
@@ -266,20 +266,26 @@ class HIDSleepBehaviour : HIDReceiverBehaviour {
|
||||
class HIDStatusBehaviour : HIDReceiverBehaviour {
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.on<HIDStatus> { packet ->
|
||||
if (receiver.getTracker(packet.hidId) == null) return@on
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
DeviceActions.Update { copy(status = packet.status, signalStrength = packet.rssi) },
|
||||
)
|
||||
}.launchIn(receiver.context.scope)
|
||||
}
|
||||
}
|
||||
val device = receiver.getDevice(packet.hidId) ?: return@on
|
||||
val packetsReceived = packet.packetsReceived.toLong()
|
||||
val packetsLost = packet.packetsLost.toLong()
|
||||
|
||||
class HIDPacketLossBehaviour : HIDReceiverBehaviour {
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.on<HIDStatus> { packet ->
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
DeviceActions.PacketStats(packetsReceived = packet.packetsReceived.toLong(), packetsLost = packet.packetsLost.toLong()),
|
||||
)
|
||||
// Dispatched together so DeviceTelemetryBehaviour sees one atomic
|
||||
// state change per packet, not two separate samples microseconds apart.
|
||||
if (receiver.getTracker(packet.hidId) != null) {
|
||||
device.context.dispatch(
|
||||
DeviceActions.Update {
|
||||
copy(
|
||||
status = packet.status,
|
||||
signalStrength = packet.rssi,
|
||||
packetsReceived = packetsReceived,
|
||||
packetsLost = packetsLost,
|
||||
)
|
||||
},
|
||||
)
|
||||
} else {
|
||||
device.context.dispatch(DeviceActions.PacketStats(packetsReceived, packetsLost))
|
||||
}
|
||||
}.launchIn(receiver.context.scope)
|
||||
}
|
||||
}
|
||||
@@ -81,7 +81,6 @@ class HIDReceiver(
|
||||
HIDRotationBehaviour(),
|
||||
HIDBatteryBehaviour(),
|
||||
HIDStatusBehaviour(),
|
||||
HIDPacketLossBehaviour(),
|
||||
HIDSleepBehaviour(),
|
||||
HIDReceiverConfigBehaviour(settings, serialNumber),
|
||||
)
|
||||
|
||||
@@ -155,6 +155,7 @@ class SolarXRBridge(
|
||||
add(TrackingChecklistBehaviour(appContext.trackingChecklist, appContext.config.settings))
|
||||
add(AssignTrackerBehaviour(appContext.server))
|
||||
add(DongleSettingsBehaviour(appContext.server))
|
||||
add(TelemetryBehaviour(appContext.server))
|
||||
add(DriverHandshakeBehaviour(appContext))
|
||||
add(DriverOutgoingTrackersBehaviour(appContext))
|
||||
add(DriverIncomingTrackersBehaviour(appContext))
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
package dev.slimevr.solarxr
|
||||
|
||||
import dev.slimevr.VRServer
|
||||
import dev.slimevr.device.Device
|
||||
import dev.slimevr.device.DevicePacketSample
|
||||
import dev.slimevr.device.computeWindowedStats
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.cancelAndJoin
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import solarxr_protocol.datatypes.TrackerStatus
|
||||
import solarxr_protocol.rpc.StartTelemetryRequest
|
||||
import solarxr_protocol.rpc.StopTelemetryRequest
|
||||
import solarxr_protocol.rpc.TelemetryGapEvent
|
||||
import solarxr_protocol.rpc.TelemetryGapResponse
|
||||
import solarxr_protocol.rpc.TelemetrySample
|
||||
import solarxr_protocol.rpc.TelemetryUpdateResponse
|
||||
import kotlin.math.max
|
||||
|
||||
private fun Device.isDeviceOk(server: VRServer): Boolean {
|
||||
val deviceState = context.state.value
|
||||
if (deviceState.status == TrackerStatus.OK) return true
|
||||
val deviceTrackers = server.context.state.value.trackers.values
|
||||
.filter { it.context.state.value.deviceId == deviceState.id }
|
||||
return deviceTrackers.any { it.context.state.value.status == TrackerStatus.OK }
|
||||
}
|
||||
|
||||
private fun Device.getMeasuredTps(server: VRServer): Double {
|
||||
val deviceState = context.state.value
|
||||
val deviceTrackers = server.context.state.value.trackers.values
|
||||
.filter { it.context.state.value.deviceId == deviceState.id }
|
||||
return deviceTrackers
|
||||
.mapNotNull { it.context.state.value.tps.toDouble().takeIf { t -> t > 0.0 } }
|
||||
.maxOrNull() ?: 100.0
|
||||
}
|
||||
|
||||
class TelemetryBehaviour(private val server: VRServer) : SolarXRBridgeBehaviour {
|
||||
override fun observe(receiver: SolarXRBridge) {
|
||||
var activeJobs: List<Job> = emptyList()
|
||||
|
||||
receiver.rpcDispatcher.on<StartTelemetryRequest> { req ->
|
||||
activeJobs.forEach { it.cancelAndJoin() }
|
||||
val deviceIds = req.deviceIds ?: emptyList()
|
||||
if (deviceIds.isEmpty()) {
|
||||
activeJobs = emptyList()
|
||||
return@on
|
||||
}
|
||||
|
||||
val jobs = mutableListOf<Job>()
|
||||
|
||||
jobs.add(
|
||||
receiver.context.scope.launch {
|
||||
while (isActive) {
|
||||
val now = System.currentTimeMillis()
|
||||
val samples = mutableListOf<TelemetrySample>()
|
||||
|
||||
for (id in deviceIds) {
|
||||
val device = server.getDevice(id.toInt()) ?: continue
|
||||
val isOk = device.isDeviceOk(server)
|
||||
|
||||
val deviceSamples = device.context.state.value.samples
|
||||
val stats = computeWindowedStats(deviceSamples, windowMs = 1000L, now = now)
|
||||
val lastSample = deviceSamples.lastOrNull()
|
||||
|
||||
val isTotalLoss = !isOk || (stats.packetLoss != null && stats.packetLoss >= 1.0f)
|
||||
|
||||
samples.add(
|
||||
TelemetrySample(
|
||||
deviceId = id,
|
||||
time = now.toULong(),
|
||||
rssi = if (isTotalLoss) null else stats.rssiAvg?.toShort(),
|
||||
rssiMin = if (isTotalLoss) null else stats.rssiMin?.toShort(),
|
||||
rssiMax = if (isTotalLoss) null else stats.rssiMax?.toShort(),
|
||||
packetLossPct = if (!isOk) null else stats.packetLoss,
|
||||
packetsLost = lastSample?.packetsLost?.toInt(),
|
||||
packetsReceived = lastSample?.packetsReceived?.toInt(),
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
if (samples.isNotEmpty()) {
|
||||
receiver.sendRpc(TelemetryUpdateResponse(samples = samples))
|
||||
}
|
||||
|
||||
delay(100L)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
deviceIds.forEach { id ->
|
||||
val device = server.getDevice(id.toInt()) ?: return@forEach
|
||||
jobs.add(
|
||||
receiver.context.scope.launch {
|
||||
var prevSample: DevicePacketSample? = null
|
||||
device.context.state
|
||||
.map { it.samples.lastOrNull() }
|
||||
.distinctUntilChanged()
|
||||
.filterNotNull()
|
||||
.collect { cur ->
|
||||
val p = prevSample
|
||||
if (p != null && device.isDeviceOk(server)) {
|
||||
val durationMs = (cur.time - p.time).coerceAtLeast(0L)
|
||||
val hasPacketLoss = cur.packetsLost != null && p.packetsLost != null && cur.packetsLost > p.packetsLost
|
||||
|
||||
val deviceTps = device.getMeasuredTps(server)
|
||||
val expectedIntervalMs = 1000.0 / deviceTps.coerceIn(5.0, 200.0)
|
||||
val thresholdMs = max(3.0 * expectedIntervalMs, 30.0)
|
||||
|
||||
if (hasPacketLoss && durationMs.toDouble() >= thresholdMs) {
|
||||
val lostDiff = (cur.packetsLost!! - p.packetsLost!!).toUInt()
|
||||
receiver.sendRpc(
|
||||
TelemetryGapResponse(
|
||||
events = listOf(
|
||||
TelemetryGapEvent(
|
||||
deviceId = id,
|
||||
time = cur.time.toULong(),
|
||||
durationMs = durationMs.toUInt(),
|
||||
packetsLost = lostDiff,
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
prevSample = cur
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
activeJobs = jobs
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
receiver.rpcDispatcher.on<StopTelemetryRequest> {
|
||||
activeJobs.forEach { it.cancelAndJoin() }
|
||||
activeJobs = emptyList()
|
||||
}.launchIn(receiver.context.scope)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user