From 2f017a64d8aa6643723294b74dbbf90ae92e22f8 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 08:50:13 +0000 Subject: [PATCH] Debug: log communication timing of sensors and the Ethernet2UART gateway DEBUG_COMMUNICATION is turned on in deviceNodeEthernet2Serial.cpp and deviceNodeSensor.cpp only. It isn't defined project-wide, which would also enable the per-byte hex dumps of protocol.cpp and the serial transports. To turn it off, comment out the #define at the top of the two files. Gateway I/O thread (tag E2UART): - "IO: request #n (x B): NO reply in N ms": nothing within the reply window (E2UART_REPLY_TIMEOUT_MS). - "IO: request #n: reply x B, first byte N ms, done N ms": replies slower than 500 ms (E2UART_SLOW_REPLY_MS), or an incomplete packet. - "IO: late data x B, N ms after request #n": a reply that came after the window (now given to the parser of that request). - "IO stats 60 s [host:port]": requests, replies, first-byte avg/max, slow, without reply, late. Sensor (tag NODSEN): - "COMM: reply/NACK WM STG reply N ms": replies slower than 500 ms (LGS_SLOW_REPLY_MS), NACKs and commands that needed retries, with the time since the first attempt. - "COMM: no reply - WM STG attempt k/17 timed out after N ms": each attempt that timed out. - "COMM: command failed - ...": details next to "Communication Wait State TO!". - "COMM stats 60 s": replies, avg/max reply time, slow, timed-out attempts (with the wait per attempt), failed commands. The gateway's per-chunk "Send/Read x bytes" debug lines are replaced by these per-request lines. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/App/common/deviceNodeEthernet2Serial.cpp | 84 ++++++++++--- lib/App/common/deviceNodeSensor.cpp | 122 ++++++++++++++++++- lib/App/common/deviceNodeSensor.h | 7 ++ 3 files changed, 196 insertions(+), 17 deletions(-) diff --git a/lib/App/common/deviceNodeEthernet2Serial.cpp b/lib/App/common/deviceNodeEthernet2Serial.cpp index 43e45ee2..f1653d55 100644 --- a/lib/App/common/deviceNodeEthernet2Serial.cpp +++ b/lib/App/common/deviceNodeEthernet2Serial.cpp @@ -10,6 +10,12 @@ // #define DEBUG_EXTRA // #define DEBUG_SERIAL_COMMUNICATION +// Request / reply timing of the I/O thread: no reply, slow and late replies, one-minute summary +#define DEBUG_COMMUNICATION +#ifndef E2UART_SLOW_REPLY_MS + #define E2UART_SLOW_REPLY_MS 500 +#endif +#define E2UART_TIMING_REPORT_MS 60000 #define LOG(X) deviceNode::m_pLog->Log("E2UART", this, X) @@ -876,7 +882,23 @@ void deviceETH2UART::m_fThreadProc(void) #endif pthread_setname_np( pthread_self(), "E2S IO" ); SIO_TZ * tIdle = (SIO_TZ *) malloc(sizeof(SIO_TZ)); // receive buffer for data between requests +#ifdef DEBUG_COMMUNICATION + uint32_t tReqNo = 0, tStReq = 0, tStReply = 0, tStNoReply = 0, tStSlow = 0, tStLate = 0; + uint64_t tStSum = 0, tStMax = 0, tStStart = e2uNowMs(), tLastReqMs = 0; + char tDbg[256]; +#endif while (m_bThreadStatus < TH_ENDED){ +#ifdef DEBUG_COMMUNICATION + if (e2uNowMs() - tStStart >= E2UART_TIMING_REPORT_MS){ + snprintf(tDbg, sizeof(tDbg), "IO stats %d s [%.64s:%d]: %u requests, %u replies (first byte avg %llu ms, max %llu ms), %u slow (> %d ms), %u without reply in %d ms, %u late", + E2UART_TIMING_REPORT_MS / 1000, m_sUartData.path, m_sUartData.port, tStReq, tStReply, (unsigned long long) (tStReply ? tStSum / tStReply : 0), + (unsigned long long) tStMax, tStSlow, E2UART_SLOW_REPLY_MS, tStNoReply, E2UART_REPLY_TIMEOUT_MS, tStLate); + LOG(tDbg); + tStReq = tStReply = tStNoReply = tStSlow = tStLate = 0; + tStSum = tStMax = 0; + tStStart = e2uNowMs(); + }; +#endif SIO_TZ * tIOData = NULL; m_mBufferProtectorMutex.lock(); if (! m_qIO.empty()){ @@ -897,6 +919,12 @@ void deviceETH2UART::m_fThreadProc(void) m_bThreadStatus = TH_ENDED; LOG_ERROR("Connection Droped on Reception Data"); } else if (tIdle->inp_size > 0){ +#ifdef DEBUG_COMMUNICATION + tStLate++; + snprintf(tDbg, sizeof(tDbg), "IO: late data %d B, %llu ms after request #%u (reply window %d ms)", tIdle->inp_size, + (unsigned long long) (tLastReqMs ? e2uNowMs() - tLastReqMs : 0), tReqNo, E2UART_REPLY_TIMEOUT_MS); + LOG(tDbg); +#endif if (isRecipient(m_pLastParser)){ m_pLastParser->parse(tIdle->inp_buffer, tIdle->inp_size); } @@ -913,13 +941,15 @@ void deviceETH2UART::m_fThreadProc(void) }; //Sending Data to line +#ifdef DEBUG_COMMUNICATION + int tSentBytes = tIOData->out_size; + uint64_t tSendMs = e2uNowMs(); + tReqNo++; + tStReq++; + tLastReqMs = tSendMs; +#endif int index = 0; while ((index < tIOData->out_size) && (m_bThreadStatus < TH_ENDED)){ - #ifdef DEBUG_COMMUNICATION - char TT[128]; - sprintf(TT, "Send \t%d bytes for \t%p", ( tIOData->out_size - index ), tIOData->boundParser ); - LOG(TT); - #endif RetVal = sendBlock(tIOData->out_buffer + index, tIOData->out_size - index); if (RetVal > 0){ index += RetVal; @@ -946,6 +976,10 @@ void deviceETH2UART::m_fThreadProc(void) tIOData->out_size = 0; bool tDataOnline = false; uint64_t tDeadline = e2uNowMs() + E2UART_REPLY_TIMEOUT_MS; +#ifdef DEBUG_COMMUNICATION + uint64_t tFirstByteMs = 0; + int tReplyBytes = 0; +#endif do { RetVal = recptBlock( tIOData->inp_buffer, &tIOData->inp_size ); if( RetVal == -1 ){ @@ -954,6 +988,12 @@ void deviceETH2UART::m_fThreadProc(void) m_bThreadStatus = TH_ENDED; LOG_ERROR("Connection Droped on Reception Data"); } else if( tIOData->inp_size ){ +#ifdef DEBUG_COMMUNICATION + if (tFirstByteMs == 0){ + tFirstByteMs = e2uNowMs(); + }; + tReplyBytes += tIOData->inp_size; +#endif uint64_t tGapEnd = e2uNowMs() + E2UART_REPLY_GAP_MS; if (tGapEnd > tDeadline){ tDeadline = tGapEnd; @@ -961,11 +1001,6 @@ void deviceETH2UART::m_fThreadProc(void) if( tParser == NULL ){ tDataOnline = true; } else { - #ifdef DEBUG_COMMUNICATION - char tB[128]; - sprintf( tB, "Read \t%d bytes for \t%p", tIOData->inp_size, tParser ); - LOG( tB ); - #endif tDataOnline = tParser->parse( tIOData->inp_buffer, tIOData->inp_size ) || tDataOnline; }; } else { @@ -975,12 +1010,29 @@ void deviceETH2UART::m_fThreadProc(void) SLEEP(5); }; } while ((m_bThreadStatus < TH_ENDED) && (e2uNowMs() < tDeadline)); - #ifdef DEBUG_COMMUNICATION - if (! tDataOnline){ - LOG_ERROR("PACK TIMEOUT!"); - }; - LOG("Finishing Read Package"); - #endif +#ifdef DEBUG_COMMUNICATION + { + uint64_t tDoneMs = e2uNowMs(); + if (tFirstByteMs == 0){ + tStNoReply++; + snprintf(tDbg, sizeof(tDbg), "IO: request #%u (%d B): NO reply in %llu ms", tReqNo, tSentBytes, (unsigned long long) (tDoneMs - tSendMs)); + LOG(tDbg); + } else { + uint64_t tFirst = tFirstByteMs - tSendMs; + tStReply++; + tStSum += tFirst; + if (tFirst > tStMax){ + tStMax = tFirst; + }; + if (tFirst > E2UART_SLOW_REPLY_MS || ! tDataOnline){ + tStSlow++; + snprintf(tDbg, sizeof(tDbg), "IO: request #%u (%d B): reply %d B, first byte %llu ms, done %llu ms%s", tReqNo, tSentBytes, tReplyBytes, + (unsigned long long) tFirst, (unsigned long long) (tDoneMs - tSendMs), tDataOnline ? "" : " - INCOMPLETE packet"); + LOG(tDbg); + }; + }; + } +#endif // Free Package free(tIOData); }; diff --git a/lib/App/common/deviceNodeSensor.cpp b/lib/App/common/deviceNodeSensor.cpp index fa8acb01..db235c28 100644 --- a/lib/App/common/deviceNodeSensor.cpp +++ b/lib/App/common/deviceNodeSensor.cpp @@ -3,6 +3,22 @@ #include "../../components/object/objectSystemAnchor.h" #include "kernelPrototype.h" +// Communication timing: slow replies, timeouts and a one-minute summary (see txTimingReport) +#define DEBUG_COMMUNICATION +#ifndef LGS_SLOW_REPLY_MS + #define LGS_SLOW_REPLY_MS 500 // replies slower than this are logged one by one +#endif +#define LGS_TIMING_REPORT_MS 60000 + +#include + +static uint64_t nsNowMs(void) +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t) ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +} + #define LOG(X) deviceNode::m_pLog->Log("NODSEN", this, X) #define LOG_ERROR(X) deviceNode::m_pLog->Log("NODSEN", this, _LOG_ERROR, X) @@ -143,8 +159,13 @@ void deviceNodeSensor::transactionResetResult(void) void deviceNodeSensor::transactionSetResult(bool ACK) { m_mPPModeProtector.lock(); - m_ePPMODE = (ACK) ? STG_POOL : STG_PUSH; + bool tWasWaiting = (m_ePPMODE == STG_WAIT); + m_ePPMODE = (ACK) ? STG_POOL : STG_PUSH; m_mPPModeProtector.unlock(); + if (tWasWaiting) + { + txTimingDone(ACK); + }; } void deviceNodeSensor::transactionWaitResult(void) @@ -152,6 +173,74 @@ void deviceNodeSensor::transactionWaitResult(void) m_mPPModeProtector.lock(); m_ePPMODE = STG_WAIT; m_mPPModeProtector.unlock(); + m_nTxStartMs = nsNowMs(); + if (m_nTxFirstMs == 0) + { + m_nTxFirstMs = m_nTxStartMs; // a retry keeps the time of the first attempt + }; +} + +/* + A reply ended the wait of the running request: reply time from the request, and from the first + attempt when the command needed retries. +*/ +void deviceNodeSensor::txTimingDone(bool ok) +{ + if (m_nTxStartMs == 0) + { + return; + }; + uint64_t tNow = nsNowMs(); + uint64_t tReply = tNow - m_nTxStartMs; + uint64_t tTotal = (m_nTxFirstMs != 0) ? tNow - m_nTxFirstMs : tReply; + m_nTxCount++; + m_nTxSumMs += tReply; + if (tReply > m_nTxMaxMs) + { + m_nTxMaxMs = tReply; + }; +#ifdef DEBUG_COMMUNICATION + if (tReply > LGS_SLOW_REPLY_MS || tTotal > tReply || ! ok) + { + m_nTxSlow++; + char tB[192]; + snprintf(tB, sizeof(tB), "COMM: %s WM 0x%02X STG 0x%02X reply %llu ms%s (wait %u ms per attempt)", ok ? "reply" : "NACK", (unsigned) getWM(), (unsigned) getStage(), + (unsigned long long) tReply, (tTotal > tReply) ? ", command needed retries" : "", (unsigned) m_nTOCounterBase * 100); + LOG(tB); + if (tTotal > tReply) + { + snprintf(tB, sizeof(tB), "COMM: command done %llu ms after its first attempt", (unsigned long long) tTotal); + LOG(tB); + }; + }; +#endif + m_nTxStartMs = 0; + m_nTxFirstMs = 0; + txTimingReport(false); +} + +void deviceNodeSensor::txTimingReport(bool force) +{ +#ifdef DEBUG_COMMUNICATION + uint64_t tNow = nsNowMs(); + if (! force && tNow - m_nTxStatStartMs < LGS_TIMING_REPORT_MS) + { + return; + }; + char tB[256]; + snprintf(tB, sizeof(tB), "COMM stats %llu s: %u replies, avg %llu ms, max %llu ms, %u slow (> %d ms) | %u attempts timed out (%u ms each), %u commands failed", + (unsigned long long) ((tNow - m_nTxStatStartMs) / 1000), m_nTxCount, (unsigned long long) (m_nTxCount ? m_nTxSumMs / m_nTxCount : 0), + (unsigned long long) m_nTxMaxMs, m_nTxSlow, LGS_SLOW_REPLY_MS, m_nTxAttemptTO, (unsigned) m_nTOCounterBase * 100, m_nTxFailed); + LOG(tB); + m_nTxCount = m_nTxSlow = m_nTxAttemptTO = m_nTxFailed = 0; + m_nTxSumMs = m_nTxMaxMs = 0; + m_nTxStatStartMs = tNow; +#else + if (force) + { + force = false; + }; +#endif } STAGE_PP_CONDITION deviceNodeSensor::getPPStatus() @@ -170,6 +259,10 @@ deviceNodeSensor::deviceNodeSensor(uint32_t id) : m_nCommErrorsCounter = 0; m_nTOCounterBase = LGS_DEFAULT_TO_COUNTER; + m_nTxStartMs = m_nTxFirstMs = 0; + m_nTxCount = m_nTxSlow = m_nTxAttemptTO = m_nTxFailed = 0; + m_nTxSumMs = m_nTxMaxMs = 0; + m_nTxStatStartMs = nsNowMs(); m_ePPMODE = STG_INIT; m_bValidationSkipCounter = DAFAULT_VALIDATION_SKIP_COUNTER; m_nCommErrorsStatTO = COMMUNICATION_ERROR_STATISTIC_HARVEST_INTERVAL_IN_SECONDS; @@ -254,6 +347,10 @@ deviceNodeSensor::deviceNodeSensor(DBC_NODE_DATA * Data) : { m_nCommErrorsCounter = 0; m_nTOCounterBase = LGS_DEFAULT_TO_COUNTER; + m_nTxStartMs = m_nTxFirstMs = 0; + m_nTxCount = m_nTxSlow = m_nTxAttemptTO = m_nTxFailed = 0; + m_nTxSumMs = m_nTxMaxMs = 0; + m_nTxStatStartMs = nsNowMs(); m_ePPMODE = STG_INIT; m_bValidationSkipCounter = DAFAULT_VALIDATION_SKIP_COUNTER; m_nCommErrorsStatTO = COMMUNICATION_ERROR_STATISTIC_HARVEST_INTERVAL_IN_SECONDS; @@ -1189,6 +1286,15 @@ void deviceNodeSensor::waitStateFunction(LRF_WORK_STAGE boboStage) { #ifdef DEBUG_EXTRA LOG("ATTEMPT RECOVER"); +#endif + m_nTxAttemptTO++; +#ifdef DEBUG_COMMUNICATION + { + char tB[160]; + snprintf(tB, sizeof(tB), "COMM: no reply - WM 0x%02X STG 0x%02X attempt %d/%d timed out after %llu ms", (unsigned) getWM(), (unsigned) getStage(), + LGS_CMD_RETRY_COUNT - m_bOpRetryCnt + 1, LGS_CMD_RETRY_COUNT + 1, (unsigned long long) (m_nTxStartMs ? nsNowMs() - m_nTxStartMs : 0)); + LOG(tB); + } #endif m_bOpRetryCnt--; m_bTimeOutCounter = m_nTOCounterBase; @@ -1225,6 +1331,18 @@ void deviceNodeSensor::waitStateFunction(LRF_WORK_STAGE boboStage) else { LOG_ERROR("Communication Wait State TO!"); + m_nTxFailed++; +#ifdef DEBUG_COMMUNICATION + { + char tB[192]; + snprintf(tB, sizeof(tB), "COMM: command failed - WM 0x%02X STG 0x%02X, %d attempts of %u ms, %llu ms since the first attempt", (unsigned) getWM(), (unsigned) getStage(), + LGS_CMD_RETRY_COUNT + 1, (unsigned) m_nTOCounterBase * 100, (unsigned long long) (m_nTxFirstMs ? nsNowMs() - m_nTxFirstMs : 0)); + LOG(tB); + } + txTimingReport(true); +#endif + m_nTxStartMs = 0; + m_nTxFirstMs = 0; registerCommError(true); // setStage( boboStage ); setWM(LRF_WM_CONNECTION); @@ -1249,6 +1367,8 @@ bool deviceNodeSensor::initWModeFunction(uint8_t wm) setWM(wm); setStage(LRF_WS_INIT); transactionResetResult(); + m_nTxStartMs = 0; // a new work mode: no command running + m_nTxFirstMs = 0; m_bOpRetryCnt = LGS_CMD_RETRY_COUNT; return true; }; diff --git a/lib/App/common/deviceNodeSensor.h b/lib/App/common/deviceNodeSensor.h index 0dca9599..4f96ef47 100644 --- a/lib/App/common/deviceNodeSensor.h +++ b/lib/App/common/deviceNodeSensor.h @@ -182,6 +182,13 @@ class deviceNodeSensor : public deviceNode, public basicThread uint8_t m_bOpRetryCnt; uint16_t m_bTimeOutCounter; uint16_t m_nTOCounterBase; // wait per attempt (x 100 ms): LGS_DEFAULT_TO_COUNTER or LGS_NETWORK_TO_COUNTER + // Communication timing (logged with DEBUG_COMMUNICATION in deviceNodeSensor.cpp; always present: class layout) + uint64_t m_nTxStartMs; // transactionWaitResult() of the running request + uint64_t m_nTxFirstMs; // first attempt of the running command (retries included) + uint32_t m_nTxCount, m_nTxSlow, m_nTxAttemptTO, m_nTxFailed; + uint64_t m_nTxSumMs, m_nTxMaxMs, m_nTxStatStartMs; + void txTimingDone(bool ok); + void txTimingReport(bool force); E_MODES m_bActualDeviceWM; E_MODES m_bRequestedDeviceWM; uint32_t m_nChannelTO;