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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
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.diagnosis;
25
26 import hudson.util.TimeUnit2;
27 import hudson.util.ColorPalette;
28 import hudson.Extension;
29 import hudson.model.PeriodicWork;
30 import hudson.model.MultiStageTimeSeries;
31 import hudson.model.MultiStageTimeSeries.TrendChart;
32 import hudson.model.MultiStageTimeSeries.TimeScale;
33
34 import java.lang.management.MemoryPoolMXBean;
35 import java.lang.management.MemoryType;
36 import java.lang.management.MemoryUsage;
37 import java.lang.management.ManagementFactory;
38 import java.util.List;
39 import java.util.ArrayList;
40 import java.io.IOException;
41
42 import org.kohsuke.stapler.QueryParameter;
43
44 /**
45 * Monitors the memory usage of the system in OS specific way.
46 *
47 * @author Kohsuke Kawaguchi
48 */
49 @Extension
50 public final class MemoryUsageMonitor extends PeriodicWork {
51 /**
52 * A memory group is conceptually a set of memory pools.
53 */
54 public final class MemoryGroup {
55 private final List<MemoryPoolMXBean> pools = new ArrayList<MemoryPoolMXBean>();
56
57 /**
58 * Trend of the memory usage, after GCs.
59 * So this shows the accurate snapshot of the footprint of live objects.
60 */
61 public final MultiStageTimeSeries used = new MultiStageTimeSeries(Messages._MemoryUsageMonitor_USED(), ColorPalette.RED, 0,0);
62 /**
63 * Trend of the maximum memory size, after GCs.
64 */
65 public final MultiStageTimeSeries max = new MultiStageTimeSeries(Messages._MemoryUsageMonitor_TOTAL(), ColorPalette.BLUE, 0,0);
66
67 private MemoryGroup(List<MemoryPoolMXBean> pools, MemoryType type) {
68 for (MemoryPoolMXBean pool : pools) {
69 if (pool.getType() == type)
70 this.pools.add(pool);
71 }
72 }
73
74 private void update() {
75 long used = 0;
76 long max = 0;
77 // long cur = 0;
78 for (MemoryPoolMXBean pool : pools) {
79 MemoryUsage usage = pool.getCollectionUsage();
80 if(usage==null) continue; // not available
81 used += usage.getUsed();
82 max += usage.getMax();
83
84 // usage = pool.getUsage();
85 // if(usage==null) continue; // not available
86 // cur += usage.getUsed();
87 }
88
89 // B -> KB
90 used /= 1024;
91 max /= 1024;
92 // cur /= 1024;
93
94 this.used.update(used);
95 this.max.update(max);
96 //
97 // return String.format("%d/%d/%d (%d%%)",used,cur,max,used*100/max);
98 }
99
100 /**
101 * Generates the memory usage statistics graph.
102 */
103 public TrendChart doGraph(@QueryParameter String type) throws IOException {
104 return MultiStageTimeSeries.createTrendChart(TimeScale.parse(type),used,max);
105 }
106 }
107
108 public final MemoryGroup heap;
109 public final MemoryGroup nonHeap;
110
111 public MemoryUsageMonitor() {
112 List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
113 heap = new MemoryGroup(pools, MemoryType.HEAP);
114 nonHeap = new MemoryGroup(pools, MemoryType.NON_HEAP);
115 }
116
117 public long getRecurrencePeriod() {
118 return TimeUnit2.SECONDS.toMillis(10);
119 }
120
121 protected void doRun() {
122 heap.update();
123 nonHeap.update();
124 }
125 }
126