Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding MRP analytics delegate #37439

Merged
merged 15 commits into from
Feb 19, 2025
1 change: 1 addition & 0 deletions src/messaging/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static_library("messaging") {
"ExchangeMgr.cpp",
"ExchangeMgr.h",
"Flags.h",
"ReliableMessageAnalyticsDelegate.h",
"ReliableMessageContext.cpp",
"ReliableMessageContext.h",
"ReliableMessageMgr.cpp",
Expand Down
56 changes: 56 additions & 0 deletions src/messaging/ReliableMessageAnalyticsDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2025 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* This file defines interface for objects interested in MRP events for analytics
*/

#pragma once

#include <lib/core/DataModelTypes.h>
#include <lib/core/NodeId.h>

namespace chip {
namespace Messaging {

class ReliableMessageAnalyticsDelegate
{
public:
virtual ~ReliableMessageAnalyticsDelegate() = default;
enum class EventType
{
kUndefined,
kInitialSend,
kRetransmission,
kAcknowledged,
kFailed,
};

struct TransmitEvent
{
NodeId nodeId = kUndefinedNodeId;
FabricIndex fabricIndex = kUndefinedFabricIndex;
EventType eventType = EventType::kUndefined;
uint32_t messageCounter = 0;
};

virtual void OnTransmitEvent(const TransmitEvent & event) = 0;
};

} // namespace Messaging
} // namespace chip
39 changes: 39 additions & 0 deletions src/messaging/ReliableMessageMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ void ReliableMessageMgr::TicklessDebugDumpRetransTable(const char * log)
#endif
}

void ReliableMessageMgr::TransmitEventAnalyticNotification(const RetransTableEntry & entry, const SessionHandle & sessionHandle,
const ReliableMessageAnalyticsDelegate::EventType & eventType)
{
if (!mAnalyticsDelegate)
{
return;
}
uint32_t messageCounter = entry.retainedBuf.GetMessageCounter();
auto fabricIndex = sessionHandle->GetFabricIndex();
auto destination = kUndefinedNodeId;
if (sessionHandle->IsSecureSession())
{
destination = sessionHandle->AsSecureSession()->GetPeerNodeId();
}
ReliableMessageAnalyticsDelegate::TransmitEvent event = {
.nodeId = destination, .fabricIndex = fabricIndex, .eventType = eventType, .messageCounter = messageCounter
};

mAnalyticsDelegate->OnTransmitEvent(event);
}

void ReliableMessageMgr::ExecuteActions()
{
System::Clock::Timestamp now = System::SystemClock().GetMonotonicTimestamp();
Expand Down Expand Up @@ -155,6 +176,8 @@ void ReliableMessageMgr::ExecuteActions()
Transport::GetSessionTypeString(session), fabricIndex, ChipLogValueX64(destination),
CHIP_CONFIG_RMP_DEFAULT_MAX_RETRANS);

TransmitEventAnalyticNotification(*entry, session, ReliableMessageAnalyticsDelegate::EventType::kFailed);

// If the exchange is expecting a response, it will handle sending
// this notification once it detects that it has not gotten a
// response. Otherwise, we need to do it.
Expand Down Expand Up @@ -295,6 +318,9 @@ bool ReliableMessageMgr::CheckAndRemRetransTable(ReliableMessageContext * rc, ui
mRetransTable.ForEachActiveObject([&](auto * entry) {
if (entry->ec->GetReliableMessageContext() == rc && entry->retainedBuf.GetMessageCounter() == ackMessageCounter)
{
auto session = entry->ec->GetSessionHandle();
TransmitEventAnalyticNotification(*entry, session, ReliableMessageAnalyticsDelegate::EventType::kAcknowledged);

// Clear the entry from the retransmision table.
ClearRetransTable(*entry);

Expand Down Expand Up @@ -440,6 +466,11 @@ void ReliableMessageMgr::RegisterSessionUpdateDelegate(SessionUpdateDelegate * s
mSessionUpdateDelegate = sessionUpdateDelegate;
}

void ReliableMessageMgr::RegisterAnalyticsDelegate(ReliableMessageAnalyticsDelegate * analyticsDelegate)
{
mAnalyticsDelegate = analyticsDelegate;
}

CHIP_ERROR ReliableMessageMgr::MapSendError(CHIP_ERROR error, uint16_t exchangeId, bool isInitiator)
{
if (
Expand Down Expand Up @@ -520,6 +551,14 @@ void ReliableMessageMgr::CalculateNextRetransTime(RetransTableEntry & entry)
backoff.count(), peerIsActive ? "Active" : "Idle", config.mIdleRetransTimeout.count(),
config.mActiveRetransTimeout.count(), config.mActiveThresholdTime.count());
#endif // CHIP_PROGRESS_LOGGING

// For initial send the packet has already been submitted to transport layer successfully.
// On re-transmits we do not know if transport layer is unable to retransmit for some
// reason, so saying we have sent re-transmit here is a little presumptuous.
ReliableMessageAnalyticsDelegate::EventType eventType = entry.sendCount == 0
? ReliableMessageAnalyticsDelegate::EventType::kInitialSend
: ReliableMessageAnalyticsDelegate::EventType::kRetransmission;
TransmitEventAnalyticNotification(entry, sessionHandle, eventType);
}

#if CHIP_CONFIG_TEST
Expand Down
14 changes: 13 additions & 1 deletion src/messaging/ReliableMessageMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <lib/support/BitFlags.h>
#include <lib/support/Pool.h>
#include <messaging/ExchangeContext.h>
#include <messaging/ReliableMessageAnalyticsDelegate.h>
#include <messaging/ReliableMessageProtocolConfig.h>
#include <system/SystemLayer.h>
#include <system/SystemPacketBuffer.h>
Expand Down Expand Up @@ -185,6 +186,13 @@ class ReliableMessageMgr
*/
void RegisterSessionUpdateDelegate(SessionUpdateDelegate * sessionUpdateDelegate);

/**
* Registers a delegate interested in analytic information
*
* @param[in] analyticsDelegate - Pointer to delegate for reporting analytic
*/
void RegisterAnalyticsDelegate(ReliableMessageAnalyticsDelegate * analyticsDelegate);

/**
* Map a send error code to the error code we should actually use for
* success checks. This maps some error codes to CHIP_NO_ERROR as
Expand Down Expand Up @@ -245,10 +253,14 @@ class ReliableMessageMgr

void TicklessDebugDumpRetransTable(const char * log);

void TransmitEventAnalyticNotification(const RetransTableEntry & entry, const SessionHandle & sessionHandle,
const ReliableMessageAnalyticsDelegate::EventType & eventType);

// ReliableMessageProtocol Global tables for timer context
ObjectPool<RetransTableEntry, CHIP_CONFIG_RMP_RETRANS_TABLE_SIZE> mRetransTable;

SessionUpdateDelegate * mSessionUpdateDelegate = nullptr;
SessionUpdateDelegate * mSessionUpdateDelegate = nullptr;
ReliableMessageAnalyticsDelegate * mAnalyticsDelegate = nullptr;

static System::Clock::Timeout sAdditionalMRPBackoffTime;
};
Expand Down
Loading
Loading