1 /***
2 * Copyright 2009 Martin Vysny.
3 *
4 * This file is part of WebVM.
5 *
6 * WebVM is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * WebVM is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with WebVM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package sk.baka.webvm;
20
21 import java.io.File;
22 import java.lang.management.MemoryUsage;
23 import java.net.URL;
24 import java.util.List;
25 import org.apache.wicket.markup.html.WebPage;
26 import org.apache.wicket.markup.html.basic.Label;
27 import sk.baka.tools.UrlUtils;
28 import sk.baka.webvm.analyzer.HistorySample;
29 import sk.baka.webvm.analyzer.HistorySampler;
30 import sk.baka.webvm.misc.BluffGraph;
31 import sk.baka.webvm.misc.DivGraph;
32 import sk.baka.webvm.misc.GraphStyle;
33
34 /***
35 * A superclass of all webvm pages.
36 * @author Martin Vysny
37 */
38 public class WebVMPage extends WebPage {
39
40 private static final long serialVersionUID = 1L;
41 /***
42 * Each page is wrapped in this border.
43 */
44 protected final AppBorder border;
45
46 /***
47 * Creates new webvm page.
48 */
49 public WebVMPage() {
50 super();
51 border = new AppBorder("appBorder");
52 add(border);
53 }
54
55 /***
56 * Draws details for given memory usage object line.
57 * @param history the history to draw
58 * @param wid chain result with this wicket id
59 * @param index the memory usage index to the {@link HistorySample#memUsage} array.
60 */
61 public final void drawMemoryUsageGraph(final List<HistorySample> history, final String wid, final int index) {
62 if (history.size() == 0) {
63 border.add(new Label(wid, ""));
64 return;
65 }
66 final GraphStyle gs = Graphs.newDefaultStyle();
67 gs.colors = new String[]{Graphs.COLOR_BLUE, Graphs.COLOR_BROWN};
68 long maxMem = history.get(0).memPoolUsage[index].getMax();
69 if (maxMem == -1) {
70 maxMem = 0;
71 for (final HistorySample hs : history) {
72 final MemoryUsage usage = hs.memPoolUsage[index];
73 if (maxMem < usage.getCommitted()) {
74 maxMem = usage.getCommitted();
75 }
76 }
77 maxMem = maxMem * 5 / 4;
78 }
79 final BluffGraph dg = new BluffGraph((int) maxMem, gs);
80 for (final HistorySample hs : history) {
81 final MemoryUsage usage = hs.memPoolUsage[index];
82 dg.add(new int[]{(int) usage.getUsed(), (int) usage.getCommitted()});
83 }
84 dg.fillWithZero(HistorySampler.HISTORY_VMSTAT.getHistoryLength(), false);
85 unescaped(wid, dg.draw());
86 }
87
88 /***
89 * Shows given string unescaped.
90 * @param wid the wicket component
91 * @param value the value to show
92 */
93 public final void unescaped(final String wid, final String value) {
94 final Label l = new Label(wid, value);
95 l.setEscapeModelStrings(false);
96 border.add(l);
97 }
98
99 /***
100 * Draws a memory usage status for given memory usage object
101 * @param usage the memory usage object, must be in megabytes as int arithmetics is used.
102 * @param wid the wicket component
103 * @param width the width of the bar in pixels.
104 */
105 public final void drawMemoryStatus(final MemoryUsage usage, final String wid, final int width) {
106 final String bar = DivGraph.drawMemoryStatus(usage, width);
107 unescaped(wid, bar);
108 }
109
110 public static File toFile(final URL url) {
111 final String file = UrlUtils.toLocalFile(url.toString());
112 return file == null ? null : new File(file);
113 }
114 }