blob: 4f0093c0f775ce9183699d666eb4fc7917f6f590 [file] [log] [blame]
Jonathan Hart233a18a2015-03-02 17:24:58 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onlab.util;
17
18import java.util.ArrayList;
19import java.util.Collections;
20import java.util.List;
21import java.util.concurrent.Executors;
22import java.util.concurrent.ScheduledExecutorService;
23import java.util.concurrent.TimeUnit;
24import java.util.concurrent.atomic.AtomicLong;
25import java.util.stream.Collectors;
26
27import static com.google.common.base.Preconditions.checkArgument;
28
29/**
30 * Maintains a sliding window of value counts. The sliding window counter is
31 * initialized with a number of window slots. Calls to #incrementCount() will
32 * increment the value in the current window slot. Periodically the window
33 * slides and the oldest value count is dropped. Calls to #get() will get the
34 * total count for the last N window slots.
35 */
36public final class SlidingWindowCounter {
37
38 private volatile int headSlot;
39 private final int windowSlots;
40
41 private final List<AtomicLong> counters;
42
43 private final ScheduledExecutorService background;
44
45 private static final int SLIDE_WINDOW_PERIOD_SECONDS = 1;
46
47 /**
48 * Creates a new sliding window counter with the given total number of
49 * window slots.
50 *
51 * @param windowSlots total number of window slots
52 */
53 public SlidingWindowCounter(int windowSlots) {
54 checkArgument(windowSlots > 0, "Window size must be a positive integer");
55
56 this.windowSlots = windowSlots;
57 this.headSlot = 0;
58
59 // Initialize each item in the list to an AtomicLong of 0
60 this.counters = Collections.nCopies(windowSlots, 0)
Thomas Vachuskab0317c62015-04-08 23:58:58 -070061 .stream()
62 .map(AtomicLong::new)
63 .collect(Collectors.toCollection(ArrayList::new));
Jonathan Hart233a18a2015-03-02 17:24:58 -080064
65 background = Executors.newSingleThreadScheduledExecutor();
66 background.scheduleWithFixedDelay(this::advanceHead, 0,
Thomas Vachuskab0317c62015-04-08 23:58:58 -070067 SLIDE_WINDOW_PERIOD_SECONDS, TimeUnit.SECONDS);
Jonathan Hart233a18a2015-03-02 17:24:58 -080068 }
69
70 /**
71 * Releases resources used by the SlidingWindowCounter.
72 */
73 public void destroy() {
74 background.shutdownNow();
75 }
76
77 /**
78 * Increments the count of the current window slot by 1.
79 */
80 public void incrementCount() {
81 incrementCount(headSlot, 1);
82 }
83
84 /**
85 * Increments the count of the current window slot by the given value.
86 *
87 * @param value value to increment by
88 */
89 public void incrementCount(long value) {
90 incrementCount(headSlot, value);
91 }
92
93 private void incrementCount(int slot, long value) {
94 counters.get(slot).addAndGet(value);
95 }
96
97 /**
98 * Gets the total count for the last N window slots.
99 *
100 * @param slots number of slots to include in the count
101 * @return total count for last N slots
102 */
103 public long get(int slots) {
104 checkArgument(slots <= windowSlots,
105 "Requested window must be less than the total window slots");
106
107 long sum = 0;
108
109 for (int i = 0; i < slots; i++) {
110 int currentIndex = headSlot - i;
111 if (currentIndex < 0) {
112 currentIndex = counters.size() + currentIndex;
113 }
114 sum += counters.get(currentIndex).get();
115 }
116
117 return sum;
118 }
119
120 void advanceHead() {
121 counters.get(slotAfter(headSlot)).set(0);
122 headSlot = slotAfter(headSlot);
123 }
124
125 private int slotAfter(int slot) {
126 return (slot + 1) % windowSlots;
127 }
128
129}