001: // GcStatFrame.java
002: // $Id: GcStatFrame.java,v 1.12 2003/09/25 21:30:24 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.status;
007:
008: import org.w3c.www.http.HTTP;
009: import org.w3c.www.http.HttpMessage;
010:
011: import org.w3c.tools.resources.Attribute;
012: import org.w3c.tools.resources.AttributeHolder;
013: import org.w3c.tools.resources.AttributeRegistry;
014: import org.w3c.tools.resources.FramedResource;
015: import org.w3c.tools.resources.IntegerAttribute;
016:
017: import org.w3c.jigsaw.http.Reply;
018: import org.w3c.jigsaw.http.Request;
019:
020: import org.w3c.jigsaw.frames.HTTPFrame;
021:
022: import org.w3c.jigsaw.html.HtmlGenerator;
023:
024: /**
025: * This class implements a GC counter.
026: * It counts the number of GC that has occured since the system was brought up.
027: */
028:
029: class GcCounter {
030: private static int count = 0;
031:
032: private static synchronized void incrCounter() {
033: count++;
034: }
035:
036: private static synchronized int getCount() {
037: return count;
038: }
039:
040: public static int getGcCount() {
041: System.runFinalization();
042: return getCount();
043: }
044:
045: public void finalize() {
046: incrCounter();
047: new GcCounter();
048: }
049:
050: static {
051: new GcCounter();
052: }
053: }
054:
055: /**
056: * Each time you get this resource, it fill run the GC.
057: */
058:
059: public class GcStatFrame extends HTTPFrame {
060: private static int REFRESH_DEFAULT = 30;
061:
062: /**
063: * Attribute index - Our refresh interval.
064: */
065: protected static int ATTR_REFRESH = -1;
066:
067: static {
068: Attribute a = null;
069: Class cls = null;
070: try {
071: cls = Class.forName("org.w3c.jigsaw.status.GcStatFrame");
072: } catch (Exception ex) {
073: ex.printStackTrace();
074: System.exit(1);
075: }
076: // The refresh interval attribute:
077: a = new IntegerAttribute("refresh", new Integer(5),
078: Attribute.EDITABLE);
079: ATTR_REFRESH = AttributeRegistry.registerAttribute(cls, a);
080: }
081:
082: protected Runtime runtime = null;
083:
084: public void registerResource(FramedResource resource) {
085: super .registerOtherResource(resource);
086: }
087:
088: /**
089: * Dump the current memory status.
090: * @param request The request we are to reply to.
091: */
092:
093: protected Reply dumpMemoryStatus(Request request) {
094: HtmlGenerator g = new HtmlGenerator("Memory Status");
095: int refresh = getInt(ATTR_REFRESH, REFRESH_DEFAULT);
096: if (refresh > 0) {
097: g.addMeta("Refresh", Integer.toString(refresh));
098: }
099: addStyleSheet(g);
100: g.append("<h1>Memory status</h1>");
101: long bytes = runtime.freeMemory();
102: long kbytes = bytes / 1024;
103: long mbytes = kbytes / 1024;
104: if (mbytes != 0) {
105: g.append("<p>Free Memory: " + mbytes + "Mb, " + kbytes
106: % 1024 + "Kb, " + bytes % 1024 + " ("
107: + Long.toString(bytes) + ")");
108: } else if (kbytes != 0) {
109: g.append("<p>Free Memory: " + kbytes + "Kb, " + bytes
110: % 1024 + " (" + Long.toString(bytes) + ")");
111: } else {
112: g.append("<p>Free Memory:" + Long.toString(bytes));
113: }
114: bytes = runtime.totalMemory();
115: kbytes = bytes / 1024;
116: mbytes = kbytes / 1024;
117: if (mbytes != 0) {
118: g.append("<p>Total Memory: " + mbytes + "Mb, " + kbytes
119: % 1024 + "Kb, " + bytes % 1024 + " ("
120: + Long.toString(bytes) + ")");
121: } else if (kbytes != 0) {
122: g.append("<p>Total Memory: " + kbytes + "Kb, " + bytes
123: % 1024 + " (" + Long.toString(bytes) + ")");
124: } else {
125: g.append("<p>Total Memory:", Long.toString(bytes));
126: }
127: g.append("<p>GC count: " + GcCounter.getGcCount());
128: g.append("<hr>");
129: // Reply back:
130: Reply reply = request.makeReply(HTTP.OK);
131: reply.setNoCache();
132: reply.setStream(g);
133: reply.setDynamic(true);
134: return reply;
135: }
136:
137: /**
138: * Perform a GC and display memory status.
139: * @param request The request to handle.
140: */
141:
142: public Reply get(Request request) {
143: return dumpMemoryStatus(request);
144: }
145:
146: /**
147: * Initialize the thread lister.
148: * Just get a pointer to our runtime object.
149: * @param values The default attribute values.
150: */
151:
152: public void initialize(Object values[]) {
153: super.initialize(values);
154: runtime = Runtime.getRuntime();
155: }
156: }
|