001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.persist;
028:
029: import java.io.IOException;
030: import java.io.PrintWriter;
031: import java.text.SimpleDateFormat;
032: import java.util.Arrays;
033: import java.util.Date;
034:
035: import javax.servlet.http.HttpServlet;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: import org.cougaar.core.plugin.ServiceUserPlugin;
040: import org.cougaar.core.service.EventService;
041: import org.cougaar.core.service.PersistenceMetricsService;
042: import org.cougaar.core.service.ServletService;
043:
044: /**
045: * This component is a {@link javax.servlet.Servlet} that displays
046: * the persistence snapshots available for the agent, and allows
047: * the user to request a Full Persistence snapshot.
048: */
049: public class PersistenceMetricsServlet extends ServiceUserPlugin {
050: private static final String PERSIST_NOW = "PersistNow";
051:
052: private static final Class[] requiredServices = {
053: ServletService.class, PersistenceMetricsService.class,
054: EventService.class };
055:
056: private static SimpleDateFormat dateFormat;
057: static {
058: dateFormat = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss");
059: }
060:
061: private ServletService servletService = null;
062: private PersistenceMetricsService metricsService = null;
063: private EventService eventService = null;
064: private String agentName;
065:
066: public PersistenceMetricsServlet() {
067: super (requiredServices);
068: }
069:
070: protected boolean haveServices() {
071: if (servletService != null)
072: return true;
073: if (acquireServices()) {
074: eventService = (EventService) getServiceBroker()
075: .getService(this , EventService.class, null);
076: servletService = (ServletService) getServiceBroker()
077: .getService(this , ServletService.class, null);
078: metricsService = (PersistenceMetricsService) getServiceBroker()
079: .getService(this , PersistenceMetricsService.class,
080: null);
081: try {
082: servletService.register("/persistenceMetrics",
083: new MyServlet());
084: } catch (Exception e) {
085: logger.error(
086: "Failed to register completionControl servlet",
087: e);
088: }
089: return true;
090: }
091: return false;
092: }
093:
094: public void setupSubscriptions() {
095: agentName = getAgentIdentifier().toString();
096:
097: // haveServices will acquire the services if not done yet. Returns (ignored) boolean
098: haveServices();
099: }
100:
101: public void execute() {
102: // haveServices will acquire the services if not done yet. Returns (ignored) boolean
103: haveServices();
104: }
105:
106: private static String getMedia(
107: PersistenceMetricsService.Metric metric) {
108: StringBuffer buf = new StringBuffer();
109: buf.append(metric.getPersistencePluginName()).append("(");
110: for (int i = 0, n = metric.getPersistencePluginParamCount(); i < n; i++) {
111: if (i > 0)
112: buf.append(",");
113: buf.append(metric.getPersistencePluginParam(i));
114: }
115: buf.append(")");
116: return buf.toString();
117: }
118:
119: private static Long getTime(PersistenceMetricsService.Metric metric) {
120: return new Long(metric.getStartTime());
121: }
122:
123: private static Long getElapsed(
124: PersistenceMetricsService.Metric metric) {
125: return new Long(metric.getEndTime() - metric.getStartTime());
126: }
127:
128: private static Long getCpu(PersistenceMetricsService.Metric metric) {
129: return new Long(metric.getCpuTime());
130: }
131:
132: private static Long getSize(PersistenceMetricsService.Metric metric) {
133: return new Long(metric.getSize());
134: }
135:
136: private static String getType(
137: PersistenceMetricsService.Metric metric) {
138: return metric.isFull() ? "Full" : "Delta";
139: }
140:
141: private static String getName(
142: PersistenceMetricsService.Metric metric) {
143: return metric.getName();
144: }
145:
146: private static class SortItem implements Comparable {
147: int ix;
148: Comparable key;
149:
150: public SortItem(int ix) {
151: this (ix, new Integer(ix));
152: }
153:
154: public SortItem(int ix, long l) {
155: this (ix, new Long(l));
156: }
157:
158: public SortItem(int ix, Comparable key) {
159: this .ix = ix;
160: this .key = key;
161: }
162:
163: public int compareTo(Object o) {
164: SortItem that = (SortItem) o;
165: return key.compareTo(that.key);
166: }
167: }
168:
169: private class MyServlet extends HttpServlet {
170: protected void doGet(HttpServletRequest request,
171: HttpServletResponse response) throws IOException {
172: doPostOrGet(request, response, false);
173: }
174:
175: protected void doPost(HttpServletRequest request,
176: HttpServletResponse response) throws IOException {
177: doPostOrGet(request, response, true);
178: }
179:
180: private String getSortParams(boolean currentReverse,
181: String currentSort, String newSort) {
182: String rev = "";
183: if (newSort.equals(currentSort) && !currentReverse)
184: rev = "&rev=true";
185: return "sort=" + newSort + rev;
186: }
187:
188: protected void doPostOrGet(HttpServletRequest request,
189: HttpServletResponse response, boolean doUpdate)
190: throws IOException {
191: PrintWriter out = response.getWriter();
192: out.println("<html>");
193: out.println(" <head>");
194: out.println(" <title>Persistence Metrics For " + agentName
195: + "</title>");
196: out.println(" </head>");
197: out.println(" <body>");
198: String submit = request.getParameter("submit");
199: if (PERSIST_NOW.equals(submit)) {
200: try {
201: blackboard.persistNow();
202:
203: // Send event allowing automated users to move on
204: if (eventService != null) {
205: eventService.event("Did Full Persist.");
206: }
207: } catch (PersistenceNotEnabledException pnee) {
208: out.println(pnee);
209: }
210: }
211: String sort = request.getParameter("sort");
212: boolean rev = "true".equals(request.getParameter("rev"));
213: out.println(" <h1>Persistence Metrics For " + agentName
214: + "</h1>");
215: out.println(" <form method=\"GET\">");
216: out
217: .println(" <input type=\"submit\" name=\"submit\" value=\""
218: + PERSIST_NOW + "\">");
219: out.println(" </form>");
220: out.println(" <table border=1>");
221: out.println(" <tr>");
222: out.println(" <td><A href=\"?"
223: + getSortParams(rev, sort, "time")
224: + "\">Time</a></td>");
225: out.println(" <td><A href=\"?"
226: + getSortParams(rev, sort, "media")
227: + "\">Media</a></td>");
228: out.println(" <td><A href=\"?"
229: + getSortParams(rev, sort, "type")
230: + "\">Type</a></td>");
231: out
232: .println(" <td><A href=\"?"
233: + getSortParams(rev, sort, "id")
234: + "\">Id</a></td>");
235: out.println(" <td><A href=\"?"
236: + getSortParams(rev, sort, "elapsed")
237: + "\">Elapsed (ms)</a></td>");
238: out.println(" <td><A href=\"?"
239: + getSortParams(rev, sort, "cpu")
240: + "\">CPU (ms)</a></td>");
241: out.println(" <td><A href=\"?"
242: + getSortParams(rev, sort, "size")
243: + "\">Bytes</a></td>");
244: out.println(" </tr>");
245: PersistenceMetricsService.Metric[] metrics = metricsService
246: .getAll(PersistenceMetricsService.ALL);
247: response.setContentType("text/html");
248: SortItem[] items = new SortItem[metrics.length];
249: if ("time".equals(sort)) {
250: for (int i = 0; i < metrics.length; i++) {
251: items[i] = new SortItem(i, getTime(metrics[i]));
252: }
253: } else if ("media".equals(sort)) {
254: for (int i = 0; i < metrics.length; i++) {
255: items[i] = new SortItem(i, getMedia(metrics[i]));
256: }
257: } else if ("type".equals(sort)) {
258: for (int i = 0; i < metrics.length; i++) {
259: items[i] = new SortItem(i, getType(metrics[i]));
260: }
261: } else if ("id".equals(sort)) {
262: for (int i = 0; i < metrics.length; i++) {
263: items[i] = new SortItem(i, getName(metrics[i]));
264: }
265: } else if ("elapsed".equals(sort)) {
266: for (int i = 0; i < metrics.length; i++) {
267: items[i] = new SortItem(i, getElapsed(metrics[i]));
268: }
269: } else if ("cpu".equals(sort)) {
270: for (int i = 0; i < metrics.length; i++) {
271: items[i] = new SortItem(i, getCpu(metrics[i]));
272: }
273: } else if ("size".equals(sort)) {
274: for (int i = 0; i < metrics.length; i++) {
275: items[i] = new SortItem(i, getSize(metrics[i]));
276: }
277: } else {
278: for (int i = 0; i < metrics.length; i++) {
279: items[i] = new SortItem(i);
280: }
281: }
282: Arrays.sort(items);
283: for (int i = 0; i < items.length; i++) {
284: SortItem item = items[rev ? items.length - 1 - i : i];
285: printMetric(out, metrics[item.ix], null);
286: }
287: out.println("<tr></tr>");
288: printMetric(
289: out,
290: metricsService
291: .getAverage(PersistenceMetricsService.FULL),
292: "Average("
293: + metricsService
294: .getCount(PersistenceMetricsService.FULL)
295: + ") Full");
296: printMetric(
297: out,
298: metricsService
299: .getAverage(PersistenceMetricsService.DELTA),
300: "Average"
301: + metricsService
302: .getCount(PersistenceMetricsService.DELTA)
303: + ") Delta");
304: printMetric(
305: out,
306: metricsService
307: .getAverage(PersistenceMetricsService.ALL),
308: "Average"
309: + metricsService
310: .getCount(PersistenceMetricsService.ALL)
311: + ") All");
312: out.println(" </table>");
313: out.println(" </body>");
314: out.println("</html>");
315: }
316: }
317:
318: private void printMetric(PrintWriter out,
319: PersistenceMetricsService.Metric metric, String kind) {
320: out.println(" <tr>");
321: if (kind != null) {
322: out.println(" <td><p align=\"right\">"
323: + dateFormat.format(new Date()) + "</p></td>");
324: out.println(" <td colspan=3><p align=\"left\" >" + kind
325: + "</p></td>");
326: } else {
327: out.println(" <td><p align=\"right\">"
328: + dateFormat
329: .format(new Date(metric.getStartTime()))
330: + "</p></td>");
331: out.println(" <td><p align=\"left\" >"
332: + getMedia(metric) + "</p></td>");
333: out.println(" <td><p align=\"left\" >" + getType(metric)
334: + "</p></td>");
335: out.println(" <td><p align=\"left\" >" + getName(metric)
336: + "</p></td>");
337: }
338: out.println(" <td><p align=\"right\">" + getElapsed(metric)
339: + "</p></td>");
340: out.println(" <td><p align=\"right\">" + getCpu(metric)
341: + "</p></td>");
342: out.println(" <td><p align=\"right\">" + getSize(metric)
343: + "</p></td>");
344: out.println(" </tr>");
345: }
346: }
|