001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.web.tomcat.statistics;
023:
024: import java.io.Serializable;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Map;
028: import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
029:
030: /** A session replication statistics collection class.
031: *
032: * @author Scott.Stark@jboss.org
033: * @version $Revision: 57206 $
034: */
035: public class ReplicationStatistics implements Serializable {
036: /** The serial version ID */
037: private static final long serialVersionUID = 9153807780893455734L;
038:
039: /** A HashMap<String, TimeStatistic> of the method invocations */
040: private ConcurrentReaderHashMap ctxStats;
041: /** Time of the last resetStats call */
042: public long lastResetTime = System.currentTimeMillis();
043:
044: public static class TimeStatistic {
045: public long replicationCount;
046: public long minPassivationTime = Long.MAX_VALUE;
047: public long maxPassivationTime;
048: public long totalPassivationTime;
049: public long minReplicationTime = Long.MAX_VALUE;
050: public long maxReplicationTime;
051: public long totalReplicationlTime;
052:
053: public long loadCount;
054: public long minLoadTime = Long.MAX_VALUE;
055: public long maxLoadTime;
056: public long totalLoadlTime;
057:
058: public void reset() {
059: replicationCount = 0;
060: minPassivationTime = Long.MAX_VALUE;
061: maxPassivationTime = 0;
062: totalPassivationTime = 0;
063: minReplicationTime = Long.MAX_VALUE;
064: maxReplicationTime = 0;
065: totalReplicationlTime = 0;
066: loadCount = 0;
067: minLoadTime = Long.MAX_VALUE;
068: maxLoadTime = 0;
069: totalLoadlTime = 0;
070: }
071: }
072:
073: public ReplicationStatistics() {
074: ctxStats = new ConcurrentReaderHashMap();
075: }
076:
077: public void updatePassivationStats(String ctx, long elapsed) {
078: TimeStatistic stat = (TimeStatistic) ctxStats.get(ctx);
079: if (stat == null) {
080: stat = new TimeStatistic();
081: ctxStats.put(ctx, stat);
082: }
083: stat.totalPassivationTime += elapsed;
084: if (stat.minPassivationTime > elapsed)
085: stat.minPassivationTime = elapsed;
086: if (stat.maxPassivationTime < elapsed)
087: stat.maxPassivationTime = elapsed;
088: }
089:
090: /** Update the TimeStatistic for the given ctx. This does not synchronize
091: * on the TimeStatistic so the results are an approximate values.
092: *
093: * @param ctx the method to update the statistics for.
094: * @param elapsed the elapsed time in milliseconds for the invocation.
095: */
096: public void updateReplicationStats(String ctx, long elapsed) {
097: TimeStatistic stat = (TimeStatistic) ctxStats.get(ctx);
098: if (stat == null) {
099: stat = new TimeStatistic();
100: ctxStats.put(ctx, stat);
101: }
102: stat.replicationCount++;
103: stat.totalReplicationlTime += elapsed;
104: if (stat.minReplicationTime > elapsed)
105: stat.minReplicationTime = elapsed;
106: if (stat.maxReplicationTime < elapsed)
107: stat.maxReplicationTime = elapsed;
108: }
109:
110: public void updateLoadStats(String ctx, long elapsed) {
111: TimeStatistic stat = (TimeStatistic) ctxStats.get(ctx);
112: if (stat == null) {
113: stat = new TimeStatistic();
114: ctxStats.put(ctx, stat);
115: }
116: stat.loadCount++;
117: stat.totalLoadlTime += elapsed;
118: if (stat.minLoadTime > elapsed)
119: stat.minLoadTime = elapsed;
120: if (stat.maxLoadTime < elapsed)
121: stat.maxLoadTime = elapsed;
122: }
123:
124: /** Resets all current TimeStatistics.
125: *
126: */
127: public void resetStats() {
128: synchronized (ctxStats) {
129: Iterator iter = ctxStats.values().iterator();
130: while (iter.hasNext()) {
131: TimeStatistic stat = (TimeStatistic) iter.next();
132: stat.reset();
133: }
134: }
135: lastResetTime = System.currentTimeMillis();
136: }
137:
138: public void removeStats(String id) {
139: ctxStats.remove(id);
140: }
141:
142: /** Access the current collection of ctx invocation statistics
143: *
144: * @return A HashMap<String, TimeStatistic> of the ctx invocations
145: */
146: public Map getStats() {
147: return ctxStats;
148: }
149:
150: public String toString() {
151: StringBuffer tmp = new StringBuffer();
152: HashMap copy = new HashMap(ctxStats);
153: Iterator iter = copy.entrySet().iterator();
154: while (iter.hasNext()) {
155: Map.Entry entry = (Map.Entry) iter.next();
156: TimeStatistic stat = (TimeStatistic) entry.getValue();
157: if (stat != null) {
158: tmp.append("[sessionID: ");
159: tmp.append(entry.getKey());
160: tmp.append(", replicationCount=");
161: tmp.append(stat.replicationCount);
162: tmp.append(", minPassivationTime=");
163: tmp.append(stat.minPassivationTime);
164: tmp.append(", maxPassivationTime=");
165: tmp.append(stat.maxPassivationTime);
166: tmp.append(", totalPassivationTime=");
167: tmp.append(stat.totalPassivationTime);
168: tmp.append(", minReplicationTime=");
169: tmp.append(stat.minReplicationTime);
170: tmp.append(", maxReplicationTime=");
171: tmp.append(stat.maxReplicationTime);
172: tmp.append(", totalReplicationlTime=");
173: tmp.append(stat.totalReplicationlTime);
174: tmp.append(", loadCount=");
175: tmp.append(stat.loadCount);
176: tmp.append(", minLoadTime=");
177: tmp.append(stat.minLoadTime);
178: tmp.append(", maxLoadTime=");
179: tmp.append(stat.maxLoadTime);
180: tmp.append(", totaLoadlTime=");
181: tmp.append(stat.totalLoadlTime);
182: tmp.append("];");
183: }
184: }
185: tmp.append(")");
186: return tmp.toString();
187: }
188:
189: }
|