File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\queue\Timeline.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, InfraDNA, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model.queue;
25
26 import java.util.Map;
27 import java.util.SortedMap;
28 import java.util.TreeMap;
29
30 import static java.lang.Math.*;
31
32 /**
33 * Represents a mutable q(t), a discrete value that changes over the time.
34 *
35 * <p>
36 * Internally represented by a set of ranges and the value of q(t) in that range,
37 * as a map from "starting time of a range" to "value of q(t)".
38 */
39 final class Timeline {
40 // int[] is always length=1
41 private final TreeMap<Long, int[]> data = new TreeMap<Long, int[]>();
42
43 /**
44 * Obtains q(t) for the given t.
45 */
46 private int at(long t) {
47 SortedMap<Long, int[]> head = data.subMap(t,Long.MAX_VALUE);
48 if (head.isEmpty()) return 0;
49 return data.get(head.firstKey())[0];
50 }
51
52 /**
53 * Finds smallest t' > t where q(t')!=q(t)
54 *
55 * If there's no such t' this method returns null.
56 */
57 private Long next(long t) {
58 SortedMap<Long, int[]> x = data.tailMap(t + 1);
59 return x.isEmpty() ? null : x.firstKey();
60 }
61
62 /**
63 * Splits the range set at the given timestamp (if it hasn't been split yet)
64 */
65 private void splitAt(long t) {
66 if (data.containsKey(t)) return; // already split at this timestamp
67
68 SortedMap<Long, int[]> head = data.headMap(t);
69
70 int v = head.isEmpty() ? 0 : data.get(head.lastKey())[0];
71 data.put(t, new int[]{v});
72 }
73
74 /**
75 * increases q(t) by n for t in [start,end).
76 *
77 * @return peak value of q(t) in this range as a result of addition.
78 */
79 int insert(long start, long end, int n) {
80 splitAt(start);
81 splitAt(end);
82
83 int peak = 0;
84 for (Map.Entry<Long, int[]> e : data.tailMap(start).headMap(end).entrySet()) {
85 peak = max(peak, e.getValue()[0] += n);
86 }
87 return peak;
88 }
89
90 /**
91 * Finds a "valley" in this timeline that fits the given duration.
92 * <p>
93 * More formally, find smallest x that:
94 * <ul>
95 * <li>x >= start
96 * <li>q(t) <= n for all t \in [x,x+duration)
97 * </ul>
98 *
99 * @return null
100 * if no such x was found.
101 */
102 Long fit(long start, long duration, int n) {
103 OUTER:
104 while (true) {
105 long t = start;
106 // check if 'start' satisfies the two conditions by moving t across [start,start+duration)
107 while ((t-start)<duration) {
108 if (at(t)>n) {
109 // value too big. what's the next t that's worth trying?
110 Long nxt = next(t);
111 if (nxt==null) return null;
112 start = nxt;
113 continue OUTER;
114 } else {
115 Long nxt = next(t);
116 if (nxt==null) t = Long.MAX_VALUE;
117 else t = nxt;
118 }
119 }
120 // q(t) looks good at the entire [start,start+duration)
121 return start;
122 }
123 }
124 }
125