Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.server.notification; |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * Exponentially weighted moving average estimator for event rate. |
| 22 | * |
| 23 | * {@hide} |
| 24 | */ |
| 25 | public class RateEstimator { |
| 26 | private static final double RATE_ALPHA = 0.8; |
| 27 | private static final double MINIMUM_DT = 0.0005; |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 28 | private Long mLastEventTime; |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 29 | private double mInterarrivalTime; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 30 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 31 | public RateEstimator() { |
| 32 | // assume something generous if we have no information |
| 33 | mInterarrivalTime = 1000.0; |
| 34 | } |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 35 | |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 36 | /** Update the estimate to account for an event that just happened. */ |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 37 | public float update(long now) { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 38 | float rate; |
| 39 | if (mLastEventTime == null) { |
| 40 | // No last event time, rate is zero. |
| 41 | rate = 0f; |
| 42 | } else { |
| 43 | // Calculate the new inter-arrival time based on last event time. |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 44 | mInterarrivalTime = getInterarrivalEstimate(now); |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 45 | rate = (float) (1.0 / mInterarrivalTime); |
| 46 | } |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 47 | mLastEventTime = now; |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 48 | return rate; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** @return the estimated rate if there were a new event right now. */ |
| 52 | public float getRate(long now) { |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 53 | if (mLastEventTime == null) { |
| 54 | return 0f; |
| 55 | } |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 56 | return (float) (1.0 / getInterarrivalEstimate(now)); |
| 57 | } |
| 58 | |
| 59 | /** @return the average inter-arrival time if there were a new event right now. */ |
| 60 | private double getInterarrivalEstimate(long now) { |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 61 | double dt = ((double) (now - mLastEventTime)) / 1000.0; |
| 62 | dt = Math.max(dt, MINIMUM_DT); |
Tony Mak | fd30332 | 2016-05-24 18:57:50 +0100 | [diff] [blame] | 63 | // a*iat_old + (1-a)*(t_now-t_last) |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 64 | return (RATE_ALPHA * mInterarrivalTime + (1.0 - RATE_ALPHA) * dt); |
| 65 | } |
| 66 | } |