forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTRMetricsCollector.mm
271 lines (231 loc) · 8.49 KB
/
MTRMetricsCollector.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
*
* 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"
#import "MTRLogging_Internal.h"
#import "MTRMetrics.h"
#import "MTRMetrics_Internal.h"
#import <MTRUnfairLock.h>
#import <os/lock.h>
#include <platform/Darwin/Tracing.h>
#include <system/SystemClock.h>
#include <tracing/metric_event.h>
#include <tracing/registry.h>
/*
* Set this to MTR_LOG_DEBUG(__VA_ARGS__) to enable logging noisy debug logging for metrics events processing
*/
#define MTR_METRICS_LOG_DEBUG(...)
using MetricEvent = chip::Tracing::MetricEvent;
@implementation MTRMetricData {
chip::System::Clock::Microseconds64 _timePoint;
MetricEvent::Type _type;
}
- (instancetype)init
{
// Default is to create data for instant event type.
// The key can be anything since it is not really used in this context.
MetricEvent event(MetricEvent::Type::kInstantEvent, "");
return [self initWithMetricEvent:event];
}
- (instancetype)initWithMetricEvent:(const MetricEvent &)event
{
if (!(self = [super init])) {
return nil;
}
_type = event.type();
using EventType = MetricEvent::Type;
switch (_type) {
// Capture timepoint for begin and end to calculate duration
case EventType::kBeginEvent:
case EventType::kEndEvent:
_timePoint = chip::System::SystemClock().GetMonotonicMicroseconds64();
break;
case EventType::kInstantEvent:
_timePoint = chip::System::Clock::Microseconds64(0);
break;
}
using ValueType = MetricEvent::Value::Type;
switch (event.ValueType()) {
case ValueType::kInt32:
_value = [NSNumber numberWithInteger:event.ValueInt32()];
break;
case ValueType::kUInt32:
_value = [NSNumber numberWithUnsignedInteger:event.ValueUInt32()];
break;
case ValueType::kChipErrorCode:
_errorCode = [NSNumber numberWithUnsignedInteger:event.ValueErrorCode()];
break;
case ValueType::kUndefined:
break;
}
MTR_METRICS_LOG_DEBUG("Initializing metric event data %s, type: %d, with time point %llu", event.key(), _type, _timePoint.count());
return self;
}
- (void)setDurationFromMetricData:(MTRMetricData *)fromData
{
auto duration = _timePoint - fromData->_timePoint;
_duration = [NSNumber numberWithDouble:double(duration.count()) / USEC_PER_SEC];
MTR_METRICS_LOG_DEBUG("Calculating duration for Matter metric with type %d, from type %d, (%llu - %llu) = %llu us (%llu s)",
_type, fromData->_type, _timePoint.count(), fromData->_timePoint.count(), duration.count(), [_duration unsignedLongLongValue]);
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<MTRMetricData: Type %d, Value = %@, Error Code = %@, Duration = %@ us>",
static_cast<int>(_type), self.value, self.errorCode, self.duration];
}
@end
@interface MTRMetricsCollector ()
- (void)registerTracingBackend;
- (void)unregisterTracingBackend;
@end
void StartupMetricsCollection()
{
if ([MTRMetricsCollector sharedInstance]) {
MTR_LOG("Initialized metrics collection backend for Darwin");
[[MTRMetricsCollector sharedInstance] registerTracingBackend];
}
}
void ShutdownMetricsCollection()
{
[[MTRMetricsCollector sharedInstance] unregisterTracingBackend];
}
@implementation MTRMetricsCollector {
os_unfair_lock _lock;
NSMutableDictionary<NSString *, MTRMetricData *> * _metricsDataCollection;
chip::Tracing::signposts::DarwinTracingBackend _tracingBackend;
BOOL _tracingBackendRegistered;
}
+ (instancetype)sharedInstance
{
static MTRMetricsCollector * singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Initialize the singleton and register the event handler
singleton = [[MTRMetricsCollector alloc] init];
if (singleton) {
singleton->_tracingBackend.SetMetricEventHandler(^(MetricEvent event) {
if (singleton) {
[singleton handleMetricEvent:event];
}
});
}
});
return singleton;
}
- (instancetype)init
{
if (!(self = [super init])) {
return nil;
}
_lock = OS_UNFAIR_LOCK_INIT;
_metricsDataCollection = [NSMutableDictionary dictionary];
_tracingBackendRegistered = FALSE;
return self;
}
- (void)registerTracingBackend
{
std::lock_guard lock(_lock);
// Register only once
if (!_tracingBackendRegistered) {
chip::Tracing::Register(_tracingBackend);
MTR_LOG("Registered tracing backend with the registry");
_tracingBackendRegistered = TRUE;
}
}
- (void)unregisterTracingBackend
{
std::lock_guard lock(_lock);
// Unregister only if registered before
if (_tracingBackendRegistered) {
chip::Tracing::Unregister(_tracingBackend);
MTR_LOG("Unregistered tracing backend with the registry");
_tracingBackendRegistered = FALSE;
}
}
static inline NSString * suffixNameForMetricType(MetricEvent::Type type)
{
switch (type) {
case MetricEvent::Type::kBeginEvent:
return @"_begin";
case MetricEvent::Type::kEndEvent:
return @"_end";
case MetricEvent::Type::kInstantEvent:
return @"_event";
}
}
static inline NSString * suffixNameForMetric(const MetricEvent & event)
{
return suffixNameForMetricType(event.type());
}
- (void)handleMetricEvent:(MetricEvent)event
{
std::lock_guard lock(_lock);
using ValueType = MetricEvent::Value::Type;
switch (event.ValueType()) {
case ValueType::kInt32:
MTR_METRICS_LOG_DEBUG("Received metric event, key: %s, type: %d, value: %d", event.key(), static_cast<int>(event.type()), event.ValueInt32());
break;
case ValueType::kUInt32:
MTR_METRICS_LOG_DEBUG("Received metric event, key: %s, type: %d, value: %u", event.key(), static_cast<int>(event.type()), event.ValueUInt32());
break;
case ValueType::kChipErrorCode:
MTR_METRICS_LOG_DEBUG("Received metric event, key: %s, type: %d, error value: %u", event.key(), static_cast<int>(event.type()), event.ValueErrorCode());
break;
case ValueType::kUndefined:
MTR_METRICS_LOG_DEBUG("Received metric event, key: %s, type: %d, value: nil", event.key(), static_cast<int>(event.type()));
break;
default:
MTR_METRICS_LOG_DEBUG("Received metric event, key: %s, type: %d, unknown value", event.key(), static_cast<int>(event.type()));
return;
}
// Create the new metric key based event type
auto metricsKey = [NSString stringWithFormat:@"%s%@", event.key(), suffixNameForMetric(event)];
MTRMetricData * data = [[MTRMetricData alloc] initWithMetricEvent:event];
// If End event, compute its duration using the Begin event
if (event.type() == MetricEvent::Type::kEndEvent) {
auto metricsBeginKey = [NSString stringWithFormat:@"%s%@", event.key(), suffixNameForMetricType(MetricEvent::Type::kBeginEvent)];
MTRMetricData * beginMetric = _metricsDataCollection[metricsBeginKey];
if (beginMetric) {
[data setDurationFromMetricData:beginMetric];
} else {
// Unbalanced end
MTR_LOG_ERROR("Unable to find Begin event corresponding to Metric Event: %s", event.key());
}
}
// Add to the collection only if it does not exist as yet.
if (![_metricsDataCollection valueForKey:metricsKey]) {
[_metricsDataCollection setValue:data forKey:metricsKey];
}
}
- (MTRMetrics *)metricSnapshot:(BOOL)resetCollection
{
std::lock_guard lock(_lock);
MTRMetrics * metrics = [[MTRMetrics alloc] initWithCapacity:[_metricsDataCollection count]];
for (NSString * key in _metricsDataCollection) {
[metrics setMetricData:_metricsDataCollection[key] forKey:key];
}
// Clear curent stats, if specified
if (resetCollection) {
[_metricsDataCollection removeAllObjects];
}
return metrics;
}
- (void)resetMetrics
{
std::lock_guard lock(_lock);
[_metricsDataCollection removeAllObjects];
}
@end