1c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 2c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Copyright (C) 2015 The Android Open Source Project 3c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 4c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Licensed under the Apache License, Version 2.0 (the "License"); 5c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// you may not use this file except in compliance with the License. 6c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// You may obtain a copy of the License at 7c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 8c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// http://www.apache.org/licenses/LICENSE-2.0 9c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 10c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Unless required by applicable law or agreed to in writing, software 11c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// distributed under the License is distributed on an "AS IS" BASIS, 12c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// See the License for the specific language governing permissions and 14c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// limitations under the License. 15c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// 1607dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 1707dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan#ifndef SHILL_NET_EVENT_HISTORY_H_ 1807dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan#define SHILL_NET_EVENT_HISTORY_H_ 1907dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 2007dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan#include <deque> 211da9419d10a1a617cc634e79772335fa08b3420fPeter Qiu#include <string> 221da9419d10a1a617cc634e79772335fa08b3420fPeter Qiu#include <vector> 2307dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 2407dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan#include <base/macros.h> 2507dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan#include <gtest/gtest_prod.h> // for FRIEND_TEST 2607dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 271da9419d10a1a617cc634e79772335fa08b3420fPeter Qiu#include "shill/net/shill_export.h" 2807dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan#include "shill/net/shill_time.h" 2907dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 3007dfabcb55d74e254f4722926a997959dc4c6393Samuel Tannamespace shill { 3107dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 32bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan// EventHistory is a list of timestamps tracking the occurrence of one or more 3307dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan// events. Events are ordered from earliest to latest. |max_events_saved| 3407dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan// can optionally be provided to limit the number of event timestamps saved 3507dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan// at any one time. 3607dfabcb55d74e254f4722926a997959dc4c6393Samuel Tanclass SHILL_EXPORT EventHistory { 3707dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan public: 38bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan enum ClockType { 39bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan kClockTypeBoottime = 0, 40bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan kClockTypeMonotonic = 1, 41bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan }; 42bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan 4307dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan EventHistory() : max_events_specified_(false), time_(Time::GetInstance()) {} 4407dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan explicit EventHistory(int max_events_saved) 4507dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan : max_events_specified_(true), 4607dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan max_events_saved_(max_events_saved), 4707dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan time_(Time::GetInstance()) {} 4807dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 4907dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan // Records the current event by adding the current time to the list. 5007dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan // If |event_limit_specificed_| and the size of |events_| is larger than 5107dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan // |max_events_saved_|, event timestamps are removed in FIFO order until the 5207dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan // size of |events_| is equal to |max_events_saved_|. 5307dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan void RecordEvent(); 5407dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 5507dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan // Start at the head of |events_| and remove all entries that occurred 56bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan // more than |seconds_ago| prior to the current time. |clock_type| determines 57bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan // what time of clock we use for time-related calculations. 58bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan void ExpireEventsBefore(int seconds_ago, ClockType clock_type); 5907dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 6007dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan // Records the current event by adding the current time to the list, and uses 6107dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan // this same timestamp to remove all entries that occurred more than 62bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan // |seconds_ago|. |clock_type| determines what time of clock we use for time- 63bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan // related calculations. 64bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan void RecordEventAndExpireEventsBefore(int seconds_ago, ClockType clock_type); 6507dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 6607dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan // Returns a vector of human-readable strings representing each timestamp in 6707dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan // |events_|. 681da9419d10a1a617cc634e79772335fa08b3420fPeter Qiu std::vector<std::string> ExtractWallClockToStrings() const; 6907dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 70bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan // Returns the number of timestamps in |events_| within the interval spanning 71bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan // now and the time |seconds_ago| before now (inclusive). |clock_type| 72bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan // determines what time of clock we use for time-related calculations. 73bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan int CountEventsWithinInterval(int seconds_ago, ClockType clock_type); 74bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan 7507dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan size_t Size() const { return events_.size(); } 7607dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan bool Empty() { return events_.empty(); } 7707dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan Timestamp Front() { return events_.front(); } 78ff59a1896ea250b4450b54db697692e0b1949528Samuel Tan void Clear() { events_.clear(); } 7907dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 8007dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan private: 8107dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan friend class EventHistoryTest; 8207dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan friend class ServiceTest; // RecordEventInternal, time_ 83dd19dce483cf54007db84bed6c2b523fbce04813Samuel Tan friend class WakeOnWiFiTest; // time_ 8407dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 8507dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan void RecordEventInternal(Timestamp now); 8607dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 8707dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan void ExpireEventsBeforeInternal(int seconds_ago, Timestamp now, 88bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan ClockType clock_type); 8907dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 9007dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan bool max_events_specified_; 9107dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan int max_events_saved_; 9207dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan std::deque<Timestamp> events_; 93e67a78539a05ea7fc68ed5ca18f6d1de333a3086Paul Stewart Time* time_; 9407dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 9507dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan DISALLOW_COPY_AND_ASSIGN(EventHistory); 9607dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan}; 9707dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 9807dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan} // namespace shill 9907dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan 10007dfabcb55d74e254f4722926a997959dc4c6393Samuel Tan#endif // SHILL_NET_EVENT_HISTORY_H_ 101