summaryrefslogtreecommitdiff
path: root/net/event_history.cc
blob: cd269f740883f36308c0378f8a385de50aa8e948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "shill/net/event_history.h"

#include <time.h>

#include <deque>

#include "shill/net/shill_time.h"

using std::deque;

namespace shill {

void EventHistory::RecordEvent() {
  RecordEventInternal(time_->GetNow());
}

void EventHistory::ExpireEventsBefore(int seconds_ago, ClockType clock_type) {
  ExpireEventsBeforeInternal(seconds_ago, time_->GetNow(), clock_type);
}

void EventHistory::RecordEventAndExpireEventsBefore(int seconds_ago,
                                                    ClockType clock_type) {
  Timestamp now = time_->GetNow();
  RecordEventInternal(now);
  ExpireEventsBeforeInternal(seconds_ago, now, clock_type);
}

std::vector<std::string> EventHistory::ExtractWallClockToStrings() const {
  std::vector<std::string> strings;
  for (deque<Timestamp>::const_iterator it = events_.begin();
       it != events_.end(); ++it) {
    strings.push_back(it->wall_clock);
  }
  return strings;
}

void EventHistory::RecordEventInternal(Timestamp now) {
  events_.push_back(now);
  while (!events_.empty() && max_events_specified_ &&
         (events_.size() > static_cast<size_t>(max_events_saved_))) {
    events_.pop_front();
  }
}

void EventHistory::ExpireEventsBeforeInternal(int seconds_ago, Timestamp now,
                                              ClockType clock_type) {
  struct timeval interval = (const struct timeval){seconds_ago};
  while (!events_.empty()) {
    struct timeval elapsed = {0, 0};
    switch (clock_type) {
      case kClockTypeBoottime:
        timersub(&now.boottime, &events_.front().boottime, &elapsed);
        break;
      case kClockTypeMonotonic:
        timersub(&now.monotonic, &events_.front().monotonic, &elapsed);
        break;
      default: {
        NOTIMPLEMENTED()
            << __func__ << ": "
            << "Invalid clock type specified - defaulting to boottime clock";
        timersub(&now.boottime, &events_.front().boottime, &elapsed);
      }
    }
    if (timercmp(&elapsed, &interval, < )) {
      break;
    }
    events_.pop_front();
  }
}

int EventHistory::CountEventsWithinInterval(int seconds_ago,
                                            ClockType clock_type) {
  int num_events_in_interval = 0;
  Timestamp now = time_->GetNow();
  struct timeval interval = (const struct timeval){seconds_ago};
  int i = 0;
  for (const auto& event : events_) {
    struct timeval elapsed = {0, 0};
    switch (clock_type) {
      case kClockTypeBoottime:
        timersub(&now.boottime, &event.boottime, &elapsed);
        break;
      case kClockTypeMonotonic:
        timersub(&now.monotonic, &event.monotonic, &elapsed);
        break;
      default: {
        NOTIMPLEMENTED()
            << __func__ << ": "
            << "Invalid clock type specified - defaulting to boottime clock";
        timersub(&now.boottime, &event.boottime, &elapsed);
      }
    }
    if (timercmp(&elapsed, &interval, <= )) {
      num_events_in_interval = events_.size() - i;
      break;
    }
    ++i;
  }
  return num_events_in_interval;
}

}  // namespace shill