1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "calibration/sample_rate_estimator/sample_rate_estimator.h"
18 
19 #include <string.h>
20 
21 #include "common/math/macros.h"
22 #include "util/nano_assert.h"
23 
24 // Helper function used to reset the sampling rate estimator accumulator.
sampleRateEstimatorResetAccumulator(struct SampleRateEstimator * sample_rate_estimator)25 static void sampleRateEstimatorResetAccumulator(
26     struct SampleRateEstimator* sample_rate_estimator) {
27   sample_rate_estimator->last_timestamp_nanos = 0.0f;
28   sample_rate_estimator->interval_accumulator_nanos = 0.0f;
29   sample_rate_estimator->num_intervals_collected = 0;
30 }
31 
sampleRateEstimatorInit(struct SampleRateEstimator * sample_rate_estimator,size_t num_intervals_to_collect,float max_interval_sec)32 void sampleRateEstimatorInit(struct SampleRateEstimator* sample_rate_estimator,
33                              size_t num_intervals_to_collect,
34                              float max_interval_sec) {
35   ASSERT_NOT_NULL(sample_rate_estimator);
36   memset(sample_rate_estimator, 0, sizeof(struct SampleRateEstimator));
37   sample_rate_estimator->mean_sampling_rate_estimate_hz =
38       SAMPLE_RATE_ESTIMATOR_INVALID_SAMPLE_RATE_HZ;
39   sample_rate_estimator->num_intervals_to_collect = num_intervals_to_collect;
40   sample_rate_estimator->max_interval_nanos =
41       max_interval_sec * SEC_TO_NANOS(1);
42 }
43 
sampleRateEstimatorGetHz(struct SampleRateEstimator * sample_rate_estimator)44 float sampleRateEstimatorGetHz(
45     struct SampleRateEstimator* sample_rate_estimator) {
46   sample_rate_estimator->new_sampling_rate_estimate_ready = false;
47   return sample_rate_estimator->mean_sampling_rate_estimate_hz;
48 }
49 
sampleRateEstimatorUpdate(struct SampleRateEstimator * sample_rate_estimator,uint64_t timestamp_nanos)50 void sampleRateEstimatorUpdate(
51     struct SampleRateEstimator* sample_rate_estimator,
52     uint64_t timestamp_nanos) {
53   // Resets the current interval capture and returns if:
54   //   1. A bad timestamp was received (i.e., time not monotonic).
55   //   2. 'last_timestamp_nanos' is zero. NOTE: 'last_timestamp_nanos' = 0
56   //      indicates that the first complete time interval has not been captured
57   //      yet (so, set it and return).
58   if (timestamp_nanos <= sample_rate_estimator->last_timestamp_nanos ||
59       sample_rate_estimator->last_timestamp_nanos == 0) {
60     sample_rate_estimator->last_timestamp_nanos = timestamp_nanos;
61     return;
62   }
63 
64   // Computes the current sampling interval. This conversion will be very fast
65   // for intervals less than ~4.3 seconds (i.e., 2^32 nano-seconds).
66   const float next_interval_nanos = floatFromUint64(
67       timestamp_nanos - sample_rate_estimator->last_timestamp_nanos);
68 
69   // Helps prevent corruption of the estimator when there are gaps in the input
70   // sampling intervals greater than 'max_interval_nanos' (i.e., intermittant
71   // periods where there are no input timestamps).
72   if (next_interval_nanos >= sample_rate_estimator->max_interval_nanos) {
73     // Resets the estimator and returns.
74     sampleRateEstimatorResetAccumulator(sample_rate_estimator);
75     return;
76   }
77 
78   // Accumulates the next sampling interval.
79   sample_rate_estimator->interval_accumulator_nanos += next_interval_nanos;
80   sample_rate_estimator->last_timestamp_nanos = timestamp_nanos;
81   sample_rate_estimator->num_intervals_collected++;
82 
83   // If the number of collected time intervals exceed the target number, then
84   // this computes a new sample rate estimate.
85   if (sample_rate_estimator->num_intervals_collected >
86       sample_rate_estimator->num_intervals_to_collect) {
87     sample_rate_estimator->mean_sampling_rate_estimate_hz =
88         sample_rate_estimator->num_intervals_collected *
89         (SEC_TO_NANOS(1) / sample_rate_estimator->interval_accumulator_nanos);
90 
91     // Sets the polling flag to indicate that a new estimate is ready.
92     sample_rate_estimator->new_sampling_rate_estimate_ready = true;
93 
94     // Resets the estimator variables.
95     sampleRateEstimatorResetAccumulator(sample_rate_estimator);
96   }
97 }
98