File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\queue\LoadPredictor.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 hudson.Extension;
27 import hudson.ExtensionList;
28 import hudson.ExtensionPoint;
29 import hudson.model.Computer;
30 import hudson.model.Executor;
31 import jenkins.model.Jenkins;
32
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36
37 /**
38 * Predicts future load to the system, to assist the scheduling decisions
39 *
40 * <p>
41 * When Hudson makes a scheduling decision, Hudson considers predicted future load
42 * &mdash; e.g., "We do currently have one available executor, but we know we need this for something else in 30 minutes,
43 * so we can't currently schedule a build that takes 1 hour."
44 *
45 * <p>
46 * This extension point plugs in such estimation of future load.
47 *
48 * @author Kohsuke Kawaguchi
49 */
50 public abstract class LoadPredictor implements ExtensionPoint {
51 /**
52 * Estimates load starting from the 'start' timestamp, up to the 'end' timestamp.
53 *
54 * @param start
55 * Where to start enumeration. Always bigger or equal to the current time of the execution.
56 * @param plan
57 * This is the execution plan for which we are making a load prediction. Never null. While
58 * this object is still being partially constructed when this method is called, some
59 * of its properties (like {@link MappingWorksheet#item} provide access to more contextual
60 * information.
61 * @since 1.380
62 */
63 public Iterable<FutureLoad> predict(MappingWorksheet plan, Computer computer, long start, long end) {
64 // maintain backward compatibility by calling the old signature.
65 return predict(computer,start,end);
66 }
67
68 /**
69 * Estimates load starting from the 'start' timestamp, up to the 'end' timestamp.
70 *
71 * @param start
72 * Where to start enumeration. Always bigger or equal to the current time of the execution.
73 * @deprecated as of 1.380
74 * Use {@link #predict(MappingWorksheet, Computer, long, long)}
75 */
76 public Iterable<FutureLoad> predict(Computer computer, long start, long end) {
77 return Collections.emptyList();
78 }
79
80 /**
81 * All the registered instances.
82 */
83 public static ExtensionList<LoadPredictor> all() {
84 return Jenkins.getInstance().getExtensionList(LoadPredictor.class);
85 }
86
87 /**
88 * Considers currently running tasks and their completion.
89 */
90 @Extension
91 public static class CurrentlyRunningTasks extends LoadPredictor {
92 @Override
93 public Iterable<FutureLoad> predict(MappingWorksheet plan, final Computer computer, long start, long eternity) {
94 long now = System.currentTimeMillis();
95 List<FutureLoad> fl = new ArrayList<FutureLoad>();
96 for (Executor e : computer.getExecutors()) {
97 if (e.isIdle()) continue;
98
99 long eta = e.getEstimatedRemainingTimeMillis();
100 long end = eta<0 ? eternity : now + eta; // when does this task end?
101 if (end < start) continue; // should be over by the 'start' time.
102 fl.add(new FutureLoad(start, end-start, 1));
103 }
104 return fl;
105 }
106 }
107 }
108