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/lib/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ buildconfig_header("chip_buildconfig") {
"CHIP_CONFIG_TLV_VALIDATE_CHAR_STRING_ON_READ=${chip_tlv_validate_char_string_on_read}",
"CHIP_CONFIG_COMMAND_SENDER_BUILTIN_SUPPORT_FOR_BATCHED_COMMANDS=${chip_enable_sending_batch_commands}",
"CHIP_CONFIG_TEST_GOOGLETEST=${chip_build_tests_googletest}",
"CHIP_CONFIG_MRP_ANALYTICS_ENABLED=${chip_enable_mrp_analytics}",
]

visibility = [ ":chip_config_header" ]
Expand Down
14 changes: 14 additions & 0 deletions src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,20 @@ extern const char CHIP_NON_PRODUCTION_MARKER[];
#define CHIP_CONFIG_TEST_GOOGLETEST 0
#endif // CHIP_CONFIG_TEST_GOOGLETEST

/**
* @def CHIP_CONFIG_MRP_ANALYTICS_ENABLED
*
* @brief
* Enables code for collecting and sending analytic related events for MRP
*
* The purpose of this macro is to prevent compiling code related to MRP analytics
* for devices that are not interested interested to save on flash.
*/

#ifndef CHIP_CONFIG_MRP_ANALYTICS_ENABLED
#define CHIP_CONFIG_MRP_ANALYTICS_ENABLED 0
#endif // CHIP_CONFIG_MRP_ANALYTICS_ENABLED

/**
* @}
*/
2 changes: 2 additions & 0 deletions src/lib/core/core.gni
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ declare_args() {
chip_enable_sending_batch_commands =
current_os == "linux" || current_os == "mac" || current_os == "ios" ||
current_os == "android"

chip_enable_mrp_analytics = current_os == "linux" || current_os == "android"
}

if (chip_target_style == "") {
Expand Down
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
49 changes: 49 additions & 0 deletions src/messaging/ReliableMessageMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ void ReliableMessageMgr::TicklessDebugDumpRetransTable(const char * log)
#endif
}

#if CHIP_CONFIG_MRP_ANALYTICS_ENABLED
void ReliableMessageMgr::NotifyMessageSendAnalytics(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);
}
#endif // CHIP_CONFIG_MRP_ANALYTICS_ENABLED

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

#if CHIP_CONFIG_MRP_ANALYTICS_ENABLED
NotifyMessageSendAnalytics(*entry, session, ReliableMessageAnalyticsDelegate::EventType::kFailed);
#endif // CHIP_CONFIG_MRP_ANALYTICS_ENABLED

// 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 +322,11 @@ bool ReliableMessageMgr::CheckAndRemRetransTable(ReliableMessageContext * rc, ui
mRetransTable.ForEachActiveObject([&](auto * entry) {
if (entry->ec->GetReliableMessageContext() == rc && entry->retainedBuf.GetMessageCounter() == ackMessageCounter)
{
#if CHIP_CONFIG_MRP_ANALYTICS_ENABLED
auto session = entry->ec->GetSessionHandle();
NotifyMessageSendAnalytics(*entry, session, ReliableMessageAnalyticsDelegate::EventType::kAcknowledged);
#endif // CHIP_CONFIG_MRP_ANALYTICS_ENABLED

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

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

#if CHIP_CONFIG_MRP_ANALYTICS_ENABLED
void ReliableMessageMgr::RegisterAnalyticsDelegate(ReliableMessageAnalyticsDelegate * analyticsDelegate)
{
mAnalyticsDelegate = analyticsDelegate;
}
#endif // CHIP_CONFIG_MRP_ANALYTICS_ENABLED

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

#if CHIP_CONFIG_MRP_ANALYTICS_ENABLED
// 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;
NotifyMessageSendAnalytics(entry, sessionHandle, eventType);
#endif // CHIP_CONFIG_MRP_ANALYTICS_ENABLED
}

#if CHIP_CONFIG_TEST
Expand Down
20 changes: 19 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,15 @@ class ReliableMessageMgr
*/
void RegisterSessionUpdateDelegate(SessionUpdateDelegate * sessionUpdateDelegate);

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

/**
* 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 +255,18 @@ class ReliableMessageMgr

void TicklessDebugDumpRetransTable(const char * log);

#if CHIP_CONFIG_MRP_ANALYTICS_ENABLED
void NotifyMessageSendAnalytics(const RetransTableEntry & entry, const SessionHandle & sessionHandle,
const ReliableMessageAnalyticsDelegate::EventType & eventType);
#endif // CHIP_CONFIG_MRP_ANALYTICS_ENABLED

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

SessionUpdateDelegate * mSessionUpdateDelegate = nullptr;
SessionUpdateDelegate * mSessionUpdateDelegate = nullptr;
#if CHIP_CONFIG_MRP_ANALYTICS_ENABLED
ReliableMessageAnalyticsDelegate * mAnalyticsDelegate = nullptr;
#endif // CHIP_CONFIG_MRP_ANALYTICS_ENABLED

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