[client/idd] input: add transport diagnostics

This commit is contained in:
Geoffrey McRae
2026-08-09 01:55:45 +10:00
parent 5b90384123
commit 487920ada3
9 changed files with 634 additions and 34 deletions

View File

@@ -35,6 +35,10 @@ bool CInputPipeServer::Init()
DeInit();
m_state.store(0, std::memory_order_release);
m_performanceFrequency.QuadPart = 0;
if (!QueryPerformanceFrequency(&m_performanceFrequency))
m_performanceFrequency.QuadPart = 0;
m_lastStatistics = GetTickCount64();
m_stopEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
m_queueEvent = CreateEventW(nullptr, FALSE, FALSE, nullptr);
if (!m_stopEvent || !m_queueEvent)
@@ -98,6 +102,19 @@ void CInputPipeServer::DeInit()
m_absoluteValid = false;
m_relativeButtons = 0;
m_absoluteButtons = 0;
m_statEnqueued = 0;
m_statRelativeCoalesced = 0;
m_statAbsoluteCoalesced = 0;
m_statResyncs = 0;
m_statResyncDiscarded = 0;
m_statQueueHighWater = 0;
m_statSent = 0;
m_statStale = 0;
m_statWriteFailed = 0;
m_statSlowWrites = 0;
m_statWriteTicks = 0;
m_statMaxWriteTicks = 0;
}
bool CInputPipeServer::QueueLocked(
@@ -133,6 +150,7 @@ bool CInputPipeServer::QueueLocked(
{
tail.payload.mouseRelative.deltaX = static_cast<int32_t>(x);
tail.payload.mouseRelative.deltaY = static_cast<int32_t>(y);
++m_statRelativeCoalesced;
return true;
}
}
@@ -145,6 +163,7 @@ bool CInputPipeServer::QueueLocked(
{
tail.payload.mouseAbsolute.x = payload.mouseAbsolute.x;
tail.payload.mouseAbsolute.y = payload.mouseAbsolute.y;
++m_statAbsoluteCoalesced;
return true;
}
}
@@ -169,6 +188,9 @@ bool CInputPipeServer::QueueRawLocked(
m_queue[index].payload = payload;
m_queue[index].pureMotion = pureMotion;
++m_queueCount;
++m_statEnqueued;
if (m_queueCount > m_statQueueHighWater)
m_statQueueHighWater = m_queueCount;
SetEvent(m_queueEvent);
return true;
}
@@ -201,6 +223,8 @@ bool CInputPipeServer::QueueResetLocked()
void CInputPipeServer::ResyncLocked()
{
++m_statResyncs;
m_statResyncDiscarded += m_queueCount;
m_queueHead = 0;
m_queueCount = 0;
uint64_t state = m_state.load(std::memory_order_relaxed);
@@ -375,6 +399,9 @@ bool CInputPipeServer::Send(const QueueItem& item)
bool current;
bool sent = true;
bool timed = false;
LARGE_INTEGER start = {};
LARGE_INTEGER end = {};
{
CSRWSharedLock lock(&m_connectionLock);
const uint64_t state = m_state.load(std::memory_order_acquire);
@@ -382,10 +409,33 @@ bool CInputPipeServer::Send(const QueueItem& item)
if (current)
{
message.sequence = ++m_sequence;
timed = QueryPerformanceCounter(&start) != FALSE;
sent = m_endpoint.Send(&message, sizeof(message));
timed = timed && QueryPerformanceCounter(&end) != FALSE;
}
}
if (!current)
++m_statStale;
else
{
if (timed)
{
const int64_t ticks = end.QuadPart - start.QuadPart;
m_statWriteTicks += ticks;
if (ticks > m_statMaxWriteTicks)
m_statMaxWriteTicks = ticks;
if (m_performanceFrequency.QuadPart &&
ticks * 1000 >= m_performanceFrequency.QuadPart)
++m_statSlowWrites;
}
if (sent)
++m_statSent;
else
++m_statWriteFailed;
}
if (!sent)
{
Invalidate(item.state, true);
@@ -394,6 +444,78 @@ bool CInputPipeServer::Send(const QueueItem& item)
return current;
}
void CInputPipeServer::LogStatistics()
{
const ULONGLONG now = GetTickCount64();
if (now - m_lastStatistics < STATISTICS_INTERVAL_MS)
return;
uint64_t enqueued;
uint64_t relativeCoalesced;
uint64_t absoluteCoalesced;
uint64_t resyncs;
uint64_t resyncDiscarded;
size_t queueHighWater;
{
CSRWExclusiveLock lock(&m_queueLock);
enqueued = m_statEnqueued;
relativeCoalesced = m_statRelativeCoalesced;
absoluteCoalesced = m_statAbsoluteCoalesced;
resyncs = m_statResyncs;
resyncDiscarded = m_statResyncDiscarded;
queueHighWater = m_statQueueHighWater;
m_statEnqueued = 0;
m_statRelativeCoalesced = 0;
m_statAbsoluteCoalesced = 0;
m_statResyncs = 0;
m_statResyncDiscarded = 0;
m_statQueueHighWater = m_queueCount;
}
const uint64_t sent = m_statSent;
const uint64_t stale = m_statStale;
const uint64_t writeFailed = m_statWriteFailed;
const uint64_t slowWrites = m_statSlowWrites;
const int64_t writeTicks = m_statWriteTicks;
const int64_t maxWriteTicks = m_statMaxWriteTicks;
m_statSent = 0;
m_statStale = 0;
m_statWriteFailed = 0;
m_statSlowWrites = 0;
m_statWriteTicks = 0;
m_statMaxWriteTicks = 0;
m_lastStatistics = now;
if (!(enqueued || relativeCoalesced || absoluteCoalesced || resyncs ||
resyncDiscarded || sent || stale || writeFailed || slowWrites))
return;
const double writeMs = m_performanceFrequency.QuadPart ?
static_cast<double>(writeTicks) * 1000.0 /
m_performanceFrequency.QuadPart : 0.0;
const double maxWriteMs = m_performanceFrequency.QuadPart ?
static_cast<double>(maxWriteTicks) * 1000.0 /
m_performanceFrequency.QuadPart : 0.0;
DEBUG_TRACE("LGInput pipe: %llu queued, %llu sent, %llu stale, "
"%llu failed; %llu relative and %llu absolute coalesced, "
"%llu resyncs discarded %llu, peak %zu; %.3f ms writes, "
"%.3f ms max, %llu slow",
static_cast<unsigned long long>(enqueued),
static_cast<unsigned long long>(sent),
static_cast<unsigned long long>(stale),
static_cast<unsigned long long>(writeFailed),
static_cast<unsigned long long>(relativeCoalesced),
static_cast<unsigned long long>(absoluteCoalesced),
static_cast<unsigned long long>(resyncs),
static_cast<unsigned long long>(resyncDiscarded),
queueHighWater,
writeMs,
maxWriteMs,
static_cast<unsigned long long>(slowWrites));
}
void CInputPipeServer::Invalidate(uint64_t state, bool requireMatch)
{
CSRWExclusiveLock connectionLock(&m_connectionLock);
@@ -435,8 +557,12 @@ void CInputPipeServer::Thread()
QueueItem item = {};
while (Pop(item))
if (!Send(item))
{
const bool sent = Send(item);
if (!sent)
break;
}
LogStatistics();
}
Invalidate(0, false);

View File

@@ -33,6 +33,7 @@ class CInputPipeServer : public IInputSink, private IPipeEndpointHandler
private:
static constexpr size_t QUEUE_LENGTH = 128;
static constexpr size_t MOTION_COALESCE_THRESHOLD = QUEUE_LENGTH / 2;
static constexpr DWORD STATISTICS_INTERVAL_MS = 5000;
struct QueueItem
{
@@ -72,6 +73,22 @@ private:
uint32_t m_relativeButtons = 0;
uint32_t m_absoluteButtons = 0;
uint64_t m_statEnqueued = 0;
uint64_t m_statRelativeCoalesced = 0;
uint64_t m_statAbsoluteCoalesced = 0;
uint64_t m_statResyncs = 0;
uint64_t m_statResyncDiscarded = 0;
size_t m_statQueueHighWater = 0;
uint64_t m_statSent = 0;
uint64_t m_statStale = 0;
uint64_t m_statWriteFailed = 0;
uint64_t m_statSlowWrites = 0;
int64_t m_statWriteTicks = 0;
int64_t m_statMaxWriteTicks = 0;
ULONGLONG m_lastStatistics = 0;
LARGE_INTEGER m_performanceFrequency = {};
bool QueueLocked(LGInputPipeMessageType type,
const KVMFRInputPayload& payload, bool pureMotion);
bool QueueRawLocked(LGInputPipeMessageType type,
@@ -81,6 +98,7 @@ private:
bool Pop(QueueItem& item);
bool Send(const QueueItem& item);
void Invalidate(uint64_t state, bool requireMatch);
void LogStatistics();
void Thread();
static DWORD WINAPI ThreadProc(void * context);

View File

@@ -289,19 +289,31 @@ bool CLGMPInputTransport::IsOwner(
bool CLGMPInputTransport::Claim(
uint32_t sourceClientID, const KVMFRInputMessage& message)
{
if (message.sequence != 1 || !m_sink)
if (message.sequence != 1)
{
++m_statistics.sequenceErrors;
return false;
}
if (!m_sink)
{
++m_statistics.deliveryFailures;
return false;
}
const uint64_t sinkState = m_sink->GetState();
if (!(sinkState & 1) || sinkState != m_sinkState ||
!m_sink->Reset() || m_sink->GetState() != sinkState)
{
++m_statistics.deliveryFailures;
return false;
}
m_ownerClientID = sourceClientID;
m_ownerGeneration = message.generation;
m_ownerSequence = message.sequence;
RenewLease();
m_statusDirty = true;
++m_statistics.claims;
DEBUG_INFO("Input owner %u generation %u acquired",
m_ownerClientID, m_ownerGeneration);
return true;
@@ -328,6 +340,7 @@ void CLGMPInputTransport::ReleaseOwner(
m_ownerSequence = 0;
m_ownerDeadline = 0;
m_statusDirty = true;
++m_statistics.releases;
DEBUG_INFO("Input owner %u generation %u released (%s)",
clientID, generation, reason);
}
@@ -420,6 +433,7 @@ bool CLGMPInputTransport::ProcessMessage(
if (!message.generation || !message.sequence || message.reserved ||
!ValidatePayload(message))
{
++m_statistics.malformedMessage;
if (owner)
ReleaseOwner(true, "invalid input message");
return false;
@@ -430,10 +444,14 @@ bool CLGMPInputTransport::ProcessMessage(
if (m_ownerClientID)
{
if (!owner)
{
++m_statistics.nonOwner;
return true;
}
if (message.sequence == 1 && m_ownerSequence == 1)
return true;
++m_statistics.sequenceErrors;
ReleaseOwner(true, "sequence discontinuity");
return false;
}
@@ -441,18 +459,23 @@ bool CLGMPInputTransport::ProcessMessage(
}
if (!owner)
{
++m_statistics.nonOwner;
return true;
}
uint32_t expectedSequence = m_ownerSequence + 1;
if (!expectedSequence)
expectedSequence = 1;
if (message.sequence != expectedSequence)
{
++m_statistics.sequenceErrors;
ReleaseOwner(true, "sequence discontinuity");
return false;
}
bool accepted = false;
bool accepted = false;
bool inputReport = false;
switch (message.type)
{
case KVMFR_INPUT_MESSAGE_RELEASE:
@@ -468,6 +491,7 @@ bool CLGMPInputTransport::ProcessMessage(
break;
case KVMFR_INPUT_MESSAGE_MOUSE_RELATIVE:
inputReport = true;
accepted = m_sink && m_sink->SendMouseRelative(
message.payload.mouseRelative.deltaX,
message.payload.mouseRelative.deltaY,
@@ -476,6 +500,7 @@ bool CLGMPInputTransport::ProcessMessage(
break;
case KVMFR_INPUT_MESSAGE_MOUSE_ABSOLUTE:
inputReport = true;
accepted = m_sink && m_sink->SendMouseAbsolute(
message.payload.mouseAbsolute.x,
message.payload.mouseAbsolute.y,
@@ -484,6 +509,7 @@ bool CLGMPInputTransport::ProcessMessage(
break;
case KVMFR_INPUT_MESSAGE_KEYBOARD:
inputReport = true;
accepted = m_sink && m_sink->SendKeyboard(
message.payload.keyboard.modifiers,
message.payload.keyboard.keys);
@@ -495,6 +521,7 @@ bool CLGMPInputTransport::ProcessMessage(
if (!accepted)
{
++m_statistics.deliveryFailures;
ReleaseOwner(true, "input delivery failed");
return false;
}
@@ -509,13 +536,16 @@ bool CLGMPInputTransport::ProcessMessage(
m_ownerSequence = message.sequence;
RenewLease();
if (inputReport)
++m_statistics.reports;
return true;
}
bool CLGMPInputTransport::DrainMessages()
{
bool received = false;
for (unsigned count = 0; count < 256; ++count)
unsigned count = 0;
for (; count < 256; ++count)
{
uint8_t data[LGMP_MSGS_SIZE] = {};
size_t size = 0;
@@ -532,9 +562,11 @@ bool CLGMPInputTransport::DrainMessages()
}
received = true;
++m_statistics.messages;
if (size != sizeof(KVMFRInputMessage))
{
DEBUG_WARN("Ignoring invalid KVMFR input message size");
++m_statistics.malformedSize;
if (sourceClientID == m_ownerClientID)
ReleaseOwner(true, "invalid input message");
}
@@ -547,9 +579,50 @@ bool CLGMPInputTransport::DrainMessages()
lgmpHostAckData(m_queue);
}
if (count > m_statistics.maxDrain)
m_statistics.maxDrain = count;
if (count == 256)
++m_statistics.drainLimit;
return received;
}
void CLGMPInputTransport::LogStatistics(ULONGLONG now)
{
if (!m_statistics.lastLog)
{
m_statistics.lastLog = now;
return;
}
if (now - m_statistics.lastLog < LOG_INTERVAL_MS)
return;
const Statistics statistics = m_statistics;
m_statistics = {};
m_statistics.lastLog = now;
if (!statistics.messages && !statistics.claims &&
!statistics.releases && !statistics.deliveryFailures)
return;
const double elapsed =
static_cast<double>(now - statistics.lastLog) / 1000.0;
DEBUG_TRACE("LGMP input host: %.1f msg/s, %llu reports, "
"drain max %u, %llu limit; %llu bad size, %llu malformed, "
"%llu sequence, %llu non-owner, %llu delivery failures; "
"%llu claims, %llu releases",
statistics.messages / elapsed,
static_cast<unsigned long long>(statistics.reports),
statistics.maxDrain,
static_cast<unsigned long long>(statistics.drainLimit),
static_cast<unsigned long long>(statistics.malformedSize),
static_cast<unsigned long long>(statistics.malformedMessage),
static_cast<unsigned long long>(statistics.sequenceErrors),
static_cast<unsigned long long>(statistics.nonOwner),
static_cast<unsigned long long>(statistics.deliveryFailures),
static_cast<unsigned long long>(statistics.claims),
static_cast<unsigned long long>(statistics.releases));
}
DWORD CALLBACK CLGMPInputTransport::ThreadProc(void * context)
{
static_cast<CLGMPInputTransport *>(context)->Thread();
@@ -567,15 +640,20 @@ void CLGMPInputTransport::Thread()
GetLastError());
ULONGLONG activeUntil = 0;
m_statistics = {};
m_statistics.lastLog = GetTickCount64();
const HANDLE waitHandles[] = { m_stopEvent, m_pollTimer };
for (;;)
{
CheckOwner();
if (DrainMessages())
activeUntil = GetTickCount64() + ACTIVE_POLL_MS;
const bool received = DrainMessages();
PublishStatus();
const ULONGLONG now = GetTickCount64();
if (received)
activeUntil = now + ACTIVE_POLL_MS;
LogStatistics(now);
const bool active = GetTickCount64() < activeUntil;
const bool active = now < activeUntil;
if (!ArmPollTimer(m_pollTimer, active))
{
DEBUG_ERROR_HR(GetLastError(), "Failed to arm LGMP input timer");

View File

@@ -40,6 +40,23 @@ class CLGMPInputTransport final : public IInputTransport
private:
static constexpr ULONGLONG OWNER_LEASE_MS = 500;
static constexpr ULONGLONG ACTIVE_POLL_MS = 50;
static constexpr ULONGLONG LOG_INTERVAL_MS = 5000;
struct Statistics
{
ULONGLONG lastLog;
uint64_t messages;
uint64_t reports;
uint64_t drainLimit;
uint64_t malformedSize;
uint64_t malformedMessage;
uint64_t sequenceErrors;
uint64_t nonOwner;
uint64_t deliveryFailures;
uint64_t claims;
uint64_t releases;
unsigned maxDrain;
};
CLGMPHost& m_host;
@@ -52,19 +69,21 @@ private:
HANDLE m_pollTimer = nullptr;
HANDLE m_thread = nullptr;
uint32_t m_ownerClientID = 0;
uint32_t m_ownerGeneration = 0;
uint32_t m_ownerSequence = 0;
ULONGLONG m_ownerDeadline = 0;
uint64_t m_sinkState = 0;
uint32_t m_endpointGeneration = 0;
uint32_t m_statusSerial = 0;
bool m_statusDirty = false;
uint32_t m_ownerClientID = 0;
uint32_t m_ownerGeneration = 0;
uint32_t m_ownerSequence = 0;
ULONGLONG m_ownerDeadline = 0;
uint64_t m_sinkState = 0;
uint32_t m_endpointGeneration = 0;
uint32_t m_statusSerial = 0;
bool m_statusDirty = false;
Statistics m_statistics = {};
bool Initialize();
void DeInit();
void UpdateSinkState(uint64_t state);
void PublishStatus();
void LogStatistics(ULONGLONG now);
bool DrainMessages();
bool ProcessMessage(uint32_t sourceClientID,
const KVMFRInputMessage& message);