26
26
#include < lib/support/CodeUtils.h>
27
27
#include < lib/support/SafeInt.h>
28
28
29
+ // Log error each time sync storage takes longer than this threshold
30
+ #define SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS (2.0 )
31
+
29
32
// FIXME: Are these good key strings? https://github.com/project-chip/connectedhomeip/issues/28973
30
33
static NSString * sResumptionNodeListKey = @" caseResumptionNodeList" ;
31
34
static NSString * sLastLocallyUsedNOCKey = @" lastLocallyUsedControllerNOC" ;
@@ -134,6 +137,7 @@ - (nullable instancetype)initWithController:(MTRDeviceController *)controller
134
137
135
138
__block id resumptionNodeList;
136
139
__block NSArray <NSNumber *> * nodesWithAttributeInfo;
140
+ NSDate * startTime = [NSDate now ];
137
141
dispatch_sync (_storageDelegateQueue, ^{
138
142
@autoreleasepool {
139
143
// NOTE: controller, not our weak ref, since we know it's still
@@ -146,6 +150,10 @@ - (nullable instancetype)initWithController:(MTRDeviceController *)controller
146
150
nodesWithAttributeInfo = [self _fetchNodeIndex ];
147
151
}
148
152
});
153
+ NSTimeInterval syncDuration = -[startTime timeIntervalSinceNow ];
154
+ if (syncDuration > SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS) {
155
+ MTR_LOG_ERROR (" MTRDeviceControllerDataStore init took %0.6lf seconds to read from storage" , syncDuration);
156
+ }
149
157
if (resumptionNodeList != nil ) {
150
158
if (![resumptionNodeList isKindOfClass: [NSArray class ]]) {
151
159
MTR_LOG_ERROR (" List of CASE resumption node IDs is not an array" );
@@ -177,11 +185,16 @@ - (void)fetchAttributeDataForAllDevices:(MTRDeviceControllerDataStoreClusterData
177
185
MTRDeviceController * controller = _controller;
178
186
VerifyOrReturn (controller != nil ); // No way to call delegate without controller.
179
187
188
+ NSDate * startTime = [NSDate now ];
180
189
dispatch_sync (_storageDelegateQueue, ^{
181
190
if ([self ->_storageDelegate respondsToSelector: @selector (valuesForController:securityLevel:sharingType: )]) {
182
191
dataStoreSecureLocalValues = [self ->_storageDelegate valuesForController: controller securityLevel: MTRStorageSecurityLevelSecure sharingType: MTRStorageSharingTypeNotShared];
183
192
}
184
193
});
194
+ NSTimeInterval syncDuration = -[startTime timeIntervalSinceNow ];
195
+ if (syncDuration > SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS) {
196
+ MTR_LOG_ERROR (" MTRDeviceControllerDataStore fetchAttributeDataForAllDevices took %0.6lf seconds to read from storage" , syncDuration);
197
+ }
185
198
186
199
if (dataStoreSecureLocalValues.count ) {
187
200
clusterDataHandler ([self _getClusterDataFromSecureLocalValues: dataStoreSecureLocalValues]);
@@ -204,6 +217,7 @@ - (void)storeResumptionInfo:(MTRCASESessionResumptionInfo *)resumptionInfo
204
217
VerifyOrReturn (controller != nil ); // No way to call delegate without controller.
205
218
206
219
auto * oldInfo = [self findResumptionInfoByNodeID: resumptionInfo.nodeID];
220
+ NSDate * startTime = [NSDate now ];
207
221
dispatch_sync (_storageDelegateQueue, ^{
208
222
if (oldInfo != nil ) {
209
223
// Remove old resumption id key. No need to do that for the
@@ -241,6 +255,10 @@ - (void)storeResumptionInfo:(MTRCASESessionResumptionInfo *)resumptionInfo
241
255
securityLevel: MTRStorageSecurityLevelSecure
242
256
sharingType: MTRStorageSharingTypeNotShared];
243
257
});
258
+ NSTimeInterval syncDuration = -[startTime timeIntervalSinceNow ];
259
+ if (syncDuration > SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS) {
260
+ MTR_LOG_ERROR (" MTRDeviceControllerDataStore storeResumptionInfo took %0.6lf seconds to store to storage" , syncDuration);
261
+ }
244
262
}
245
263
246
264
- (void )clearAllResumptionInfo
@@ -274,6 +292,7 @@ - (void)_clearResumptionInfoForNodeID:(NSNumber *)nodeID controller:(MTRDeviceCo
274
292
{
275
293
auto * oldInfo = [self findResumptionInfoByNodeID: nodeID];
276
294
if (oldInfo != nil ) {
295
+ NSDate * startTime = [NSDate now ];
277
296
dispatch_sync (_storageDelegateQueue, ^{
278
297
[_storageDelegate controller: controller
279
298
removeValueForKey: ResumptionByResumptionIDKey (oldInfo.resumptionID)
@@ -284,6 +303,10 @@ - (void)_clearResumptionInfoForNodeID:(NSNumber *)nodeID controller:(MTRDeviceCo
284
303
securityLevel: MTRStorageSecurityLevelSecure
285
304
sharingType: MTRStorageSharingTypeNotShared];
286
305
});
306
+ NSTimeInterval syncDuration = -[startTime timeIntervalSinceNow ];
307
+ if (syncDuration > SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS) {
308
+ MTR_LOG_ERROR (" MTRDeviceControllerDataStore _clearResumptionInfoForNodeID took %0.6lf seconds to remove from storage" , syncDuration);
309
+ }
287
310
}
288
311
}
289
312
@@ -293,13 +316,18 @@ - (CHIP_ERROR)storeLastLocallyUsedNOC:(MTRCertificateTLVBytes)noc
293
316
VerifyOrReturnError (controller != nil , CHIP_ERROR_PERSISTED_STORAGE_FAILED); // No way to call delegate without controller.
294
317
295
318
__block BOOL ok;
319
+ NSDate * startTime = [NSDate now ];
296
320
dispatch_sync (_storageDelegateQueue, ^{
297
321
ok = [_storageDelegate controller: controller
298
322
storeValue: noc
299
323
forKey: sLastLocallyUsedNOCKey
300
324
securityLevel: MTRStorageSecurityLevelSecure
301
325
sharingType: MTRStorageSharingTypeNotShared];
302
326
});
327
+ NSTimeInterval syncDuration = -[startTime timeIntervalSinceNow ];
328
+ if (syncDuration > SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS) {
329
+ MTR_LOG_ERROR (" MTRDeviceControllerDataStore storeLastLocallyUsedNOC took %0.6lf seconds to store to storage" , syncDuration);
330
+ }
303
331
return ok ? CHIP_NO_ERROR : CHIP_ERROR_PERSISTED_STORAGE_FAILED;
304
332
}
305
333
@@ -309,6 +337,7 @@ - (MTRCertificateTLVBytes _Nullable)fetchLastLocallyUsedNOC
309
337
VerifyOrReturnValue (controller != nil , nil ); // No way to call delegate without controller.
310
338
311
339
__block id data;
340
+ NSDate * startTime = [NSDate now ];
312
341
dispatch_sync (_storageDelegateQueue, ^{
313
342
@autoreleasepool {
314
343
data = [_storageDelegate controller: controller
@@ -317,6 +346,10 @@ - (MTRCertificateTLVBytes _Nullable)fetchLastLocallyUsedNOC
317
346
sharingType: MTRStorageSharingTypeNotShared];
318
347
}
319
348
});
349
+ NSTimeInterval syncDuration = -[startTime timeIntervalSinceNow ];
350
+ if (syncDuration > SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS) {
351
+ MTR_LOG_ERROR (" MTRDeviceControllerDataStore fetchLastLocallyUsedNOC took %0.6lf seconds to read from storage" , syncDuration);
352
+ }
320
353
321
354
if (data == nil ) {
322
355
return nil ;
@@ -340,6 +373,7 @@ - (nullable MTRCASESessionResumptionInfo *)_findResumptionInfoWithKey:(nullable
340
373
}
341
374
342
375
__block id resumptionInfo;
376
+ NSDate * startTime = [NSDate now ];
343
377
dispatch_sync (_storageDelegateQueue, ^{
344
378
@autoreleasepool {
345
379
resumptionInfo = [_storageDelegate controller: controller
@@ -348,6 +382,10 @@ - (nullable MTRCASESessionResumptionInfo *)_findResumptionInfoWithKey:(nullable
348
382
sharingType: MTRStorageSharingTypeNotShared];
349
383
}
350
384
});
385
+ NSTimeInterval syncDuration = -[startTime timeIntervalSinceNow ];
386
+ if (syncDuration > SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS) {
387
+ MTR_LOG_ERROR (" MTRDeviceControllerDataStore _findResumptionInfoWithKey took %0.6lf seconds to read from storage" , syncDuration);
388
+ }
351
389
352
390
if (resumptionInfo == nil ) {
353
391
return nil ;
@@ -866,6 +904,7 @@ - (void)clearAllStoredClusterData
866
904
}
867
905
868
906
__block NSMutableDictionary <MTRClusterPath *, MTRDeviceClusterData *> * clusterDataToReturn = nil ;
907
+ NSDate * startTime = [NSDate now ];
869
908
dispatch_sync (_storageDelegateQueue, ^{
870
909
std::lock_guard lock (self->_nodeArrayLock );
871
910
@@ -924,6 +963,10 @@ - (void)clearAllStoredClusterData
924
963
}
925
964
}
926
965
});
966
+ NSTimeInterval syncDuration = -[startTime timeIntervalSinceNow ];
967
+ if (syncDuration > SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS) {
968
+ MTR_LOG_ERROR (" MTRDeviceControllerDataStore getStoredClusterDataForNodeID took %0.6lf seconds to read from storage" , syncDuration);
969
+ }
927
970
928
971
return clusterDataToReturn;
929
972
}
@@ -934,9 +977,14 @@ - (nullable MTRDeviceClusterData *)getStoredClusterDataForNodeID:(NSNumber *)nod
934
977
// when the consumer knows that we're supposed to have data for this cluster
935
978
// path.
936
979
__block MTRDeviceClusterData * clusterDataToReturn = nil ;
980
+ NSDate * startTime = [NSDate now ];
937
981
dispatch_sync (_storageDelegateQueue, ^{
938
982
clusterDataToReturn = [self _fetchClusterDataForNodeID: nodeID endpointID: endpointID clusterID: clusterID];
939
983
});
984
+ NSTimeInterval syncDuration = -[startTime timeIntervalSinceNow ];
985
+ if (syncDuration > SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS) {
986
+ MTR_LOG_ERROR (" MTRDeviceControllerDataStore getStoredClusterDataForNodeID took %0.6lf seconds to read from storage" , syncDuration);
987
+ }
940
988
return clusterDataToReturn;
941
989
}
942
990
@@ -1173,6 +1221,7 @@ - (NSString *)_deviceDataKeyForNodeID:(NSNumber *)nodeID
1173
1221
- (nullable NSDictionary <NSString *, id> *)getStoredDeviceDataForNodeID : (NSNumber *)nodeID
1174
1222
{
1175
1223
__block NSDictionary <NSString *, id > * deviceData = nil ;
1224
+ NSDate * startTime = [NSDate now ];
1176
1225
dispatch_sync (_storageDelegateQueue, ^{
1177
1226
MTRDeviceController * controller = self->_controller ;
1178
1227
VerifyOrReturn (controller != nil ); // No way to call delegate without controller.
@@ -1204,6 +1253,10 @@ - (NSString *)_deviceDataKeyForNodeID:(NSNumber *)nodeID
1204
1253
// to do that.
1205
1254
deviceData = dictionary;
1206
1255
});
1256
+ NSTimeInterval syncDuration = -[startTime timeIntervalSinceNow ];
1257
+ if (syncDuration > SYNC_OPERATION_DURATION_LOG_THRESHOLD_SECONDS) {
1258
+ MTR_LOG_ERROR (" MTRDeviceControllerDataStore getStoredDeviceDataForNodeID took %0.6lf seconds to read from storage" , syncDuration);
1259
+ }
1207
1260
return deviceData;
1208
1261
}
1209
1262
0 commit comments