001: package org.jgroups.protocols;
002:
003: import org.jgroups.stack.Protocol;
004: import org.jgroups.Event;
005: import org.jgroups.Message;
006: import org.jgroups.Address;
007: import org.jgroups.View;
008:
009: import java.util.*;
010:
011: /**
012: * Provides various stats
013: * @author Bela Ban
014: * @version $Id: STATS.java,v 1.3 2006/01/14 14:00:38 belaban Exp $
015: */
016: public class STATS extends Protocol {
017: long sent_msgs, sent_bytes, sent_ucasts, sent_mcasts,
018: received_ucasts, received_mcasts;
019: long received_msgs, received_bytes, sent_ucast_bytes,
020: sent_mcast_bytes, received_ucast_bytes,
021: received_mcast_bytes;
022:
023: /** HashMap<Address,Entry>, maintains stats per target destination */
024: HashMap sent = new HashMap();
025:
026: /** HashMap<Address,Entry>, maintains stats per receiver */
027: HashMap received = new HashMap();
028:
029: static final short UP = 1;
030: static final short DOWN = 2;
031:
032: public String getName() {
033: return "STATS";
034: }
035:
036: public boolean setProperties(Properties props) {
037: super .setProperties(props);
038: down_thread = false; // never use a down thread
039: up_thread = false; // never use an up thread
040:
041: if (props.size() > 0) {
042: log.error("the following properties are not recognized: "
043: + props);
044: return false;
045: }
046: return true;
047: }
048:
049: public void resetStats() {
050: sent_msgs = sent_bytes = sent_ucasts = sent_mcasts = received_ucasts = received_mcasts = 0;
051: received_msgs = received_bytes = sent_ucast_bytes = sent_mcast_bytes = received_ucast_bytes = received_mcast_bytes = 0;
052: sent.clear();
053: received.clear();
054: }
055:
056: public long getSentMessages() {
057: return sent_msgs;
058: }
059:
060: public long getSentBytes() {
061: return sent_bytes;
062: }
063:
064: public long getSentUnicastMessages() {
065: return sent_ucasts;
066: }
067:
068: public long getSentUnicastBytes() {
069: return sent_ucast_bytes;
070: }
071:
072: public long getSentMcastMessages() {
073: return sent_mcasts;
074: }
075:
076: public long getSentMcastBytes() {
077: return sent_mcast_bytes;
078: }
079:
080: public long getReceivedMessages() {
081: return received_msgs;
082: }
083:
084: public long getReceivedBytes() {
085: return received_bytes;
086: }
087:
088: public long getReceivedUnicastMessages() {
089: return received_ucasts;
090: }
091:
092: public long getReceivedUnicastBytes() {
093: return received_ucast_bytes;
094: }
095:
096: public long getReceivedMcastMessages() {
097: return received_mcasts;
098: }
099:
100: public long getReceivedMcastBytes() {
101: return received_mcast_bytes;
102: }
103:
104: public void up(Event evt) {
105: if (evt.getType() == Event.MSG) {
106: Message msg = (Message) evt.getArg();
107: updateStats(msg, UP);
108: } else if (evt.getType() == Event.VIEW_CHANGE) {
109: handleViewChange((View) evt.getArg());
110: }
111: passUp(evt);
112: }
113:
114: public void down(Event evt) {
115: if (evt.getType() == Event.MSG) {
116: Message msg = (Message) evt.getArg();
117: updateStats(msg, DOWN);
118: } else if (evt.getType() == Event.VIEW_CHANGE) {
119: handleViewChange((View) evt.getArg());
120: }
121: passDown(evt);
122: }
123:
124: public String printStats() {
125: Map.Entry entry;
126: Object key, val;
127: StringBuffer sb = new StringBuffer();
128: sb.append("sent:\n");
129: for (Iterator it = sent.entrySet().iterator(); it.hasNext();) {
130: entry = (Map.Entry) it.next();
131: key = entry.getKey();
132: if (key == null)
133: key = "<mcast dest>";
134: val = entry.getValue();
135: sb.append(key).append(": ").append(val).append("\n");
136: }
137: sb.append("\nreceived:\n");
138: for (Iterator it = received.entrySet().iterator(); it.hasNext();) {
139: entry = (Map.Entry) it.next();
140: key = entry.getKey();
141: val = entry.getValue();
142: sb.append(key).append(": ").append(val).append("\n");
143: }
144:
145: return sb.toString();
146: }
147:
148: private void handleViewChange(View view) {
149: Vector members = view.getMembers();
150: Set tmp = new LinkedHashSet(members);
151: tmp.add(null); // for null destination (= mcast)
152: sent.keySet().retainAll(tmp);
153: received.keySet().retainAll(tmp);
154: }
155:
156: private void updateStats(Message msg, short direction) {
157: int length;
158: HashMap map;
159: boolean mcast;
160: Address dest, src;
161:
162: if (msg == null)
163: return;
164: length = msg.getLength();
165: dest = msg.getDest();
166: src = msg.getSrc();
167: mcast = dest == null || dest.isMulticastAddress();
168:
169: if (direction == UP) { // received
170: received_msgs++;
171: received_bytes += length;
172: if (mcast) {
173: received_mcasts++;
174: received_mcast_bytes += length;
175: } else {
176: received_ucasts++;
177: received_ucast_bytes += length;
178: }
179: } else { // sent
180: sent_msgs++;
181: sent_bytes += length;
182: if (mcast) {
183: sent_mcasts++;
184: sent_mcast_bytes += length;
185: } else {
186: sent_ucasts++;
187: sent_ucast_bytes += length;
188: }
189: }
190:
191: Address key = direction == UP ? src : dest;
192: map = direction == UP ? received : sent;
193: Entry entry = (Entry) map.get(key);
194: if (entry == null) {
195: entry = new Entry();
196: map.put(key, entry);
197: }
198: entry.msgs++;
199: entry.bytes += length;
200: if (mcast) {
201: entry.mcasts++;
202: entry.mcast_bytes += length;
203: } else {
204: entry.ucasts++;
205: entry.ucast_bytes += length;
206: }
207: }
208:
209: static class Entry {
210: long msgs, bytes, ucasts, mcasts, ucast_bytes, mcast_bytes;
211:
212: public String toString() {
213: StringBuffer sb = new StringBuffer();
214: sb.append(msgs).append(" (").append(bytes)
215: .append(" bytes)");
216: sb.append(": ").append(ucasts).append(" ucasts (").append(
217: ucast_bytes).append(" bytes), ");
218: sb.append(mcasts).append(" mcasts (").append(mcast_bytes)
219: .append(" bytes)");
220: return sb.toString();
221: }
222: }
223:
224: }
|