Skip to content

Commit

Permalink
Make subscription latency estimation in MTRDevice a bit less noisy. (p…
Browse files Browse the repository at this point in the history
…roject-chip#33717)

Averages out the new latency with (lower weighted) previous ones.
  • Loading branch information
bzbarsky-apple authored Jun 5, 2024
1 parent f91b256 commit 2005be9
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ - (BOOL)isEqual:(id)object
// happen more often than once every 10 minutes.
#define MTRDEVICE_MIN_RESUBSCRIBE_DUE_TO_READ_INTERVAL_SECONDS (10 * 60)

// Weight of new data in determining subscription latencies. To avoid random
// outliers causing too much noise in the value, treat an existing value (if
// any) as having 2/3 weight and the new value as having 1/3 weight. These
// weights are subject to change, if it's determined that different ones give
// better behavior.
#define MTRDEVICE_SUBSCRIPTION_LATENCY_NEW_VALUE_WEIGHT (1.0 / 3.0)

@interface MTRDevice ()
@property (nonatomic, readonly) os_unfair_lock lock; // protects the caches and device state
// protects against concurrent time updates by guarding timeUpdateScheduled flag which manages time updates scheduling,
Expand Down Expand Up @@ -1014,7 +1021,12 @@ - (void)_handleSubscriptionEstablished
// We want time interval from initialSubscribeStart to now, not the other
// way around.
NSTimeInterval subscriptionLatency = -[initialSubscribeStart timeIntervalSinceNow];
_estimatedSubscriptionLatency = @(subscriptionLatency);
if (_estimatedSubscriptionLatency == nil) {
_estimatedSubscriptionLatency = @(subscriptionLatency);
} else {
NSTimeInterval newSubscriptionLatencyEstimate = MTRDEVICE_SUBSCRIPTION_LATENCY_NEW_VALUE_WEIGHT * subscriptionLatency + (1 - MTRDEVICE_SUBSCRIPTION_LATENCY_NEW_VALUE_WEIGHT) * _estimatedSubscriptionLatency.doubleValue;
_estimatedSubscriptionLatency = @(newSubscriptionLatencyEstimate);
}
[self _storePersistedDeviceData];
}

Expand Down

0 comments on commit 2005be9

Please sign in to comment.