Skip to content

Commit

Permalink
Initial framework for logging scalar data event
Browse files Browse the repository at this point in the history
  • Loading branch information
anush-apple committed Feb 13, 2024
1 parent c9fe07e commit ec63b96
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include <protocols/secure_channel/MessageCounterManager.h>
#include <setup_payload/QRCodeSetupPayloadParser.h>
#include <tracing/macros.h>
#include <tracing/scalar_event.h>

#if CONFIG_NETWORK_LAYER_BLE
#include <ble/BleLayer.h>
Expand Down Expand Up @@ -629,6 +630,7 @@ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, co
CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, RendezvousParameters & params)
{
MATTER_TRACE_SCOPE("EstablishPASEConnection", "DeviceCommissioner");

CHIP_ERROR err = CHIP_NO_ERROR;
CommissioneeDeviceProxy * device = nullptr;
CommissioneeDeviceProxy * current = nullptr;
Expand Down Expand Up @@ -746,7 +748,7 @@ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, Re
// exchange context right before calling Pair ensures that if allocation
// succeeds, PASESession has taken ownership.
exchangeCtxt = mSystemState->ExchangeMgr()->NewContext(session.Value(), &device->GetPairing());
VerifyOrExit(exchangeCtxt != nullptr, err = CHIP_ERROR_INTERNAL);
VerifyOrExit(exchangeCtxt != nullptr, MATTER_LOG_SCALER_EVENT(PASEConnectionFailed, err = CHIP_ERROR_INTERNAL));

err = device->GetPairing().Pair(*mSystemState->SessionMgr(), params.GetSetupPINCode(), GetLocalMRPConfig(), exchangeCtxt, this);
SuccessOrExit(err);
Expand All @@ -760,6 +762,7 @@ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, Re
}
}

MATTER_LOG_SCALER_EVENT(PASEConnectionEstablished, CHIP_NO_ERROR);
return err;
}

Expand Down
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/MTRFramework.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <dispatch/dispatch.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/logging/CHIPLogging.h>
#include <MTRMetricsCollector.h>

void MTRFrameworkInit()
{
Expand All @@ -34,5 +35,8 @@ void MTRFrameworkInit()
// Suppress CHIP logging until we actually need it for redirection
// (see MTRSetLogCallback()). Logging to os_log is always enabled.
chip::Logging::SetLogFilter(chip::Logging::kLogCategory_None);

// Setup metrics collection and tracing framework
InitializeMetricsCollection();
});
}
40 changes: 40 additions & 0 deletions src/darwin/Framework/CHIP/MTRMetricsCollector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
*
* Copyright (c) 2024 Project CHIP Authors
*
* 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.
*/

#import <Matter/MTRMetrics.h>

NS_ASSUME_NONNULL_BEGIN

void InitializeMetricsCollection();

/**
* A representation of metrics data for an operation.
*/
MTR_NEWLY_AVAILABLE
@interface MTRMetricsCollector : NSObject

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

/**
* Return the singleton MTRMetricsCollector to vend MTRMetric snapshots
*/
+ (instancetype)sharedInstance;

@end

NS_ASSUME_NONNULL_END
59 changes: 59 additions & 0 deletions src/darwin/Framework/CHIP/MTRMetricsCollector.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
*
* Copyright (c) 2024 Project CHIP Authors
*
* 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.
*/
#import "MTRMetricsCollector.h"
#include <MTRMetrics.h>
#import "MTRLogging_Internal.h"
#import <Matter/MTRDefines.h>
#include <platform/Darwin/Tracing.h>
#include <tracing/registry.h>

void InitializeMetricsCollection()
{
if ([MTRMetricsCollector sharedInstance])
{
MTR_LOG_INFO("Initialized metrics collection backend for Darwin");
}
}

@implementation MTRMetricsCollector {
NSMutableDictionary<NSString *, id> * _metricsData;
chip::Tracing::signposts::DarwinTracingBackend _tracingBackend;
}

+ (instancetype)sharedInstance
{
static MTRMetricsCollector * singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// initialize the singleton.
singleton = [[MTRMetricsCollector alloc] init];
if (singleton) {
chip::Tracing::Register(singleton->_tracingBackend);
}
});
return singleton;
}

- (instancetype)init
{
if (!(self = [super init])) {
return nil;
}
return self;
}

@end
8 changes: 8 additions & 0 deletions src/darwin/Framework/Matter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@
88EBF8CE27FABDD500686BC1 /* MTRDeviceAttestationDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 88EBF8CB27FABDD500686BC1 /* MTRDeviceAttestationDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; };
88EBF8CF27FABDD500686BC1 /* MTRDeviceAttestationDelegateBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 88EBF8CC27FABDD500686BC1 /* MTRDeviceAttestationDelegateBridge.mm */; };
88EBF8D027FABDD500686BC1 /* MTRDeviceAttestationDelegateBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 88EBF8CD27FABDD500686BC1 /* MTRDeviceAttestationDelegateBridge.h */; };
88FA798D2B7B257100CD4B6F /* MTRMetricsCollector.h in Headers */ = {isa = PBXBuildFile; fileRef = 88FA798B2B7B257100CD4B6F /* MTRMetricsCollector.h */; };
88FA798E2B7B257100CD4B6F /* MTRMetricsCollector.mm in Sources */ = {isa = PBXBuildFile; fileRef = 88FA798C2B7B257100CD4B6F /* MTRMetricsCollector.mm */; };
93B2CF9A2B56E45C00E4D187 /* MTRClusterNames.mm in Sources */ = {isa = PBXBuildFile; fileRef = 93B2CF992B56E45C00E4D187 /* MTRClusterNames.mm */; };
93E610AA2B626E290077F02A /* MTRClusterNames.h in Headers */ = {isa = PBXBuildFile; fileRef = 93E610A92B626E290077F02A /* MTRClusterNames.h */; settings = {ATTRIBUTES = (Public, ); }; };
991DC0842475F45400C13860 /* MTRDeviceController.h in Headers */ = {isa = PBXBuildFile; fileRef = 991DC0822475F45400C13860 /* MTRDeviceController.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -659,6 +661,8 @@
88EBF8CB27FABDD500686BC1 /* MTRDeviceAttestationDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRDeviceAttestationDelegate.h; sourceTree = "<group>"; };
88EBF8CC27FABDD500686BC1 /* MTRDeviceAttestationDelegateBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRDeviceAttestationDelegateBridge.mm; sourceTree = "<group>"; };
88EBF8CD27FABDD500686BC1 /* MTRDeviceAttestationDelegateBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRDeviceAttestationDelegateBridge.h; sourceTree = "<group>"; };
88FA798B2B7B257100CD4B6F /* MTRMetricsCollector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRMetricsCollector.h; sourceTree = "<group>"; };
88FA798C2B7B257100CD4B6F /* MTRMetricsCollector.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRMetricsCollector.mm; sourceTree = "<group>"; };
93B2CF992B56E45C00E4D187 /* MTRClusterNames.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRClusterNames.mm; sourceTree = "<group>"; };
93E610A92B626E290077F02A /* MTRClusterNames.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRClusterNames.h; sourceTree = "<group>"; };
991DC0822475F45400C13860 /* MTRDeviceController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRDeviceController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1158,6 +1162,8 @@
B202528F2459E34F00F97062 /* CHIP */ = {
isa = PBXGroup;
children = (
88FA798B2B7B257100CD4B6F /* MTRMetricsCollector.h */,
88FA798C2B7B257100CD4B6F /* MTRMetricsCollector.mm */,
88E6C9442B6334ED001A1FE0 /* MTRMetrics_Internal.h */,
88E6C9432B6334ED001A1FE0 /* MTRMetrics.h */,
88E6C9452B6334ED001A1FE0 /* MTRMetrics.mm */,
Expand Down Expand Up @@ -1514,6 +1520,7 @@
2C222AD0255C620600E446B9 /* MTRBaseDevice.h in Headers */,
7596A84F2877E6A9004DAE0E /* MTRCluster_Internal.h in Headers */,
991DC0842475F45400C13860 /* MTRDeviceController.h in Headers */,
88FA798D2B7B257100CD4B6F /* MTRMetricsCollector.h in Headers */,
88E6C9462B6334ED001A1FE0 /* MTRMetrics.h in Headers */,
AF1CB86E2874B03B00865A96 /* MTROTAProviderDelegate.h in Headers */,
51D0B1402B61B3A4006E3511 /* MTRServerCluster.h in Headers */,
Expand Down Expand Up @@ -1890,6 +1897,7 @@
5A6FEC9827B5C6AF00F25F42 /* MTRDeviceOverXPC.mm in Sources */,
514654492A72F9DF00904E61 /* MTRDemuxingStorage.mm in Sources */,
1E4D655229C30A8700BC3478 /* MTRCommissionableBrowser.mm in Sources */,
88FA798E2B7B257100CD4B6F /* MTRMetricsCollector.mm in Sources */,
3DA1A3562ABAB3B4004F0BB9 /* MTRAsyncWorkQueue.mm in Sources */,
51D0B1272B617246006E3511 /* MTRServerEndpoint.mm in Sources */,
3DECCB722934AFE200585AEC /* MTRLogging.mm in Sources */,
Expand Down
10 changes: 10 additions & 0 deletions src/platform/Darwin/Tracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <os/signpost.h>
#include <tracing/backend.h>

#define _MATTER_TRACE_DISABLE(...) \
do \
Expand Down Expand Up @@ -55,6 +56,15 @@ class Scoped
const char * mLabel;
const char * mGroup;
};

class DarwinTracingBackend : public ::chip::Tracing::Backend
{
public:
DarwinTracingBackend() = default;

void LogEvent(ScalarEvent & event) override;
};

} // namespace signposts
} // namespace Tracing
} // namespace chip
Expand Down
8 changes: 8 additions & 0 deletions src/platform/Darwin/Tracing.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/
#include <platform/Darwin/Tracing.h>
#include <tracing/scalar_event.h>

namespace chip {
namespace Tracing {
Expand All @@ -29,6 +30,13 @@ os_log_t GetMatterSignpostLogger()
});
return logger;
}

void DarwinTracingBackend::LogEvent(ScalarEvent & event)
{
ChipLogProgress(DeviceLayer, "Receive scalar event, type: %u, value: %u",
event.eventType, event.eventValue);
}

} // namespace signposts
} // namespace Tracing
} // namespace chip
Expand Down
1 change: 1 addition & 0 deletions src/tracing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static_library("tracing") {
"log_declares.h",
"registry.cpp",
"registry.h",
"scalar_event.h",
]

public_deps = [
Expand Down
1 change: 1 addition & 0 deletions src/tracing/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Backend : public ::chip::IntrusiveListNodeBase<>
virtual void LogNodeLookup(NodeLookupInfo &) { TraceInstant("Lookup", "DNSSD"); }
virtual void LogNodeDiscovered(NodeDiscoveredInfo &) { TraceInstant("Node Discovered", "DNSSD"); }
virtual void LogNodeDiscoveryFailed(NodeDiscoveryFailedInfo &) { TraceInstant("Discovery Failed", "DNSSD"); }
virtual void LogEvent(ScalarEvent &) {}
};

} // namespace Tracing
Expand Down
1 change: 1 addition & 0 deletions src/tracing/log_declares.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct MessageReceivedInfo;
struct NodeLookupInfo;
struct NodeDiscoveredInfo;
struct NodeDiscoveryFailedInfo;
struct ScalarEvent;

} // namespace Tracing
} // namespace chip
8 changes: 8 additions & 0 deletions src/tracing/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@
::chip::Tracing::Internal::LogNodeDiscoveryFailed(_trace_data); \
} while (false)

#define MATTER_LOG_SCALER_EVENT(eventType,eventValue) \
do \
{ \
::chip::Tracing::ScalarEvent _scalar_event(chip::Tracing::ScalarEvent::k##eventType, eventValue); \
::chip::Tracing::Internal::LogEvent(_scalar_event); \
} while (false)

#else // MATTER_TRACING_ENABLED

#define _MATTER_TRACE_DISABLE(...) \
Expand All @@ -95,5 +102,6 @@
#define MATTER_LOG_NODE_LOOKUP(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_LOG_NODE_DISCOVERED(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_LOG_NODE_DISCOVERY_FAILED(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)
#define MATTER_LOG_SCALER_EVENT(...) _MATTER_TRACE_DISABLE(__VA_ARGS__)

#endif // MATTER_TRACING_ENABLED
8 changes: 8 additions & 0 deletions src/tracing/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ void LogNodeDiscoveryFailed(::chip::Tracing::NodeDiscoveryFailedInfo & info)
}
}

void LogEvent(::chip::Tracing::ScalarEvent & event)
{
for (auto & backend : gTracingBackends)
{
backend.LogEvent(event);
}
}

} // namespace Internal

#endif // MATTTER_TRACING_ENABLED
Expand Down
1 change: 1 addition & 0 deletions src/tracing/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ void LogMessageReceived(::chip::Tracing::MessageReceivedInfo & info);
void LogNodeLookup(::chip::Tracing::NodeLookupInfo & info);
void LogNodeDiscovered(::chip::Tracing::NodeDiscoveredInfo & info);
void LogNodeDiscoveryFailed(::chip::Tracing::NodeDiscoveryFailedInfo & info);
void LogEvent(::chip::Tracing::ScalarEvent & event);

} // namespace Internal

Expand Down
61 changes: 61 additions & 0 deletions src/tracing/scalar_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2024 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.
*/
#pragma once

#include <lib/core/CHIPError.h>
#include <stdint.h>

namespace chip {
namespace Tracing {

struct ScalarEvent
{
static inline const uint32_t kEventDomainDiscovery = 1 << 16;
static inline const uint32_t kEventDomainPASE = 2 << 16;
static inline const uint32_t kEventDomainAttestation = 3 << 16;
static inline const uint32_t kEventDomainCASE = 4 << 16;

enum EventType {
kDiscoveryOverBLE = (kEventDomainDiscovery | 1),
kDiscoveryOnNetwork = (kEventDomainDiscovery | 2),
kPASEConnectionEstablished = (kEventDomainPASE | 1),
kPASEConnectionFailed = (kEventDomainPASE | 2),
kAttestationResult = (kEventDomainAttestation | 1),
kAttestationOverridden = (kEventDomainAttestation | 2),
kCASEConnectionEstablished = (kEventDomainCASE | 1),
kCASEConnectionFailed = (kEventDomainCASE | 2)
};

EventType eventType;
uint32_t eventValue;

ScalarEvent(EventType evtType, uint32_t evtValue = 0)
: eventType(evtType), eventValue(evtValue)
{}

ScalarEvent(EventType evtType, ChipError err)
: eventType(evtType), eventValue(err.AsInteger())
{}

static inline ScalarEvent make_event(EventType eventType, uint32_t eventValue = 0)
{
return ScalarEvent { eventType, eventValue };
}
};

} // namespace Tracing
} // namespace chip

0 comments on commit ec63b96

Please sign in to comment.