1 /** 2 * Copyright (c) 2014, Timothy Stack 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, this 10 * list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or otherlist materials provided with the distribution. 14 * * Neither the name of Timothy Stack nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * @file log_accel.hh 30 */ 31 32 #ifndef log_accel_h 33 #define log_accel_h 34 35 #include <stdint.h> 36 37 #include <cmath> 38 39 #include "base/lnav_log.hh" 40 41 /** 42 * Helper class for figuring out changes in the log message rate. 43 */ 44 class log_accel { 45 46 public: 47 /*< The direction of the message rate: steady, accelerating, or decelerating */ 48 enum direction_t { 49 A_STEADY, 50 A_DECEL, 51 A_ACCEL, 52 }; 53 54 /** 55 * Add a time point that will be used to compute velocity and then 56 * acceleration. Points should be added in reverse order, from most 57 * recent to oldest. 58 * 59 * @param point The point in time. 60 * @return True if more points can be added. 61 */ 62 bool add_point(int64_t point); 63 64 /** 65 * Get the average acceleration based on the time points we've received. 66 * 67 * @return The average message acceleration. 68 */ 69 double get_avg_accel() const; 70 71 /** 72 * Compute the message rate direction. If the average acceleration is less 73 * than a certain threshold, then we consider the rate to be steady. 74 * Otherwise, the message rate is increasing or decreasing. 75 * 76 * @return The direction of the message rate. 77 */ 78 direction_t get_direction() const; 79 80 private: 81 /** 82 * The amount of historical data to include in the average acceleration 83 * computation. 84 */ 85 static const int HISTORY_SIZE = 8; 86 /** 87 * The minimum range of velocities seen. This value should limit false- 88 * positives for small millisecond level fluctuations. 89 */ 90 static const double MIN_RANGE; 91 static const double THRESHOLD; 92 93 int64_t la_last_point{0}; 94 bool la_last_point_set{false}; 95 int64_t la_min_velocity{INT64_MAX}; 96 int64_t la_max_velocity{INT64_MIN}; 97 int64_t la_velocity[HISTORY_SIZE]; 98 int la_velocity_size{0}; 99 }; 100 101 #endif 102