1 package org.jgroups.protocols; 2 3 import org.jgroups.stack.Protocol; 4 import org.jgroups.Event; 5 import org.jgroups.Message; 6 import org.jgroups.Address; 7 import org.jgroups.View; 8 9 import java.util.*; 10 11 16 public class STATS extends Protocol { 17 long sent_msgs, sent_bytes, sent_ucasts, sent_mcasts, received_ucasts, received_mcasts; 18 long received_msgs, received_bytes, sent_ucast_bytes, sent_mcast_bytes, received_ucast_bytes, received_mcast_bytes; 19 20 21 HashMap sent=new HashMap(); 22 23 24 HashMap received=new HashMap(); 25 26 static final short UP=1; 27 static final short DOWN=2; 28 29 30 public String getName() { 31 return "STATS"; 32 } 33 34 public boolean setProperties(Properties props) { 35 super.setProperties(props); 36 if(!props.isEmpty()) { 37 log.error("the following properties are not recognized: " + props); 38 return false; 39 } 40 return true; 41 } 42 43 public void resetStats() { 44 sent_msgs=sent_bytes=sent_ucasts=sent_mcasts=received_ucasts=received_mcasts=0; 45 received_msgs=received_bytes=sent_ucast_bytes=sent_mcast_bytes=received_ucast_bytes=received_mcast_bytes=0; 46 sent.clear(); 47 received.clear(); 48 } 49 50 51 public long getSentMessages() {return sent_msgs;} 52 public long getSentBytes() {return sent_bytes;} 53 public long getSentUnicastMessages() {return sent_ucasts;} 54 public long getSentUnicastBytes() {return sent_ucast_bytes;} 55 public long getSentMcastMessages() {return sent_mcasts;} 56 public long getSentMcastBytes() {return sent_mcast_bytes;} 57 58 public long getReceivedMessages() {return received_msgs;} 59 public long getReceivedBytes() {return received_bytes;} 60 public long getReceivedUnicastMessages() {return received_ucasts;} 61 public long getReceivedUnicastBytes() {return received_ucast_bytes;} 62 public long getReceivedMcastMessages() {return received_mcasts;} 63 public long getReceivedMcastBytes() {return received_mcast_bytes;} 64 65 66 public Object up(Event evt) { 67 if(evt.getType() == Event.MSG) { 68 Message msg=(Message)evt.getArg(); 69 updateStats(msg, UP); 70 } 71 else if(evt.getType() == Event.VIEW_CHANGE) { 72 handleViewChange((View)evt.getArg()); 73 } 74 return up_prot.up(evt); 75 } 76 77 78 79 public Object down(Event evt) { 80 if(evt.getType() == Event.MSG) { 81 Message msg=(Message)evt.getArg(); 82 updateStats(msg, DOWN); 83 } 84 else if(evt.getType() == Event.VIEW_CHANGE) { 85 handleViewChange((View)evt.getArg()); 86 } 87 return down_prot.down(evt); 88 } 89 90 91 public String printStats() { 92 Map.Entry entry; 93 Object key, val; 94 StringBuilder sb=new StringBuilder (); 95 sb.append("sent:\n"); 96 for(Iterator it=sent.entrySet().iterator(); it.hasNext();) { 97 entry=(Map.Entry)it.next(); 98 key=entry.getKey(); 99 if(key == null) key="<mcast dest>"; 100 val=entry.getValue(); 101 sb.append(key).append(": ").append(val).append("\n"); 102 } 103 sb.append("\nreceived:\n"); 104 for(Iterator it=received.entrySet().iterator(); it.hasNext();) { 105 entry=(Map.Entry)it.next(); 106 key=entry.getKey(); 107 val=entry.getValue(); 108 sb.append(key).append(": ").append(val).append("\n"); 109 } 110 111 return sb.toString(); 112 } 113 114 private void handleViewChange(View view) { 115 Vector members=view.getMembers(); 116 Set tmp=new LinkedHashSet(members); 117 tmp.add(null); sent.keySet().retainAll(tmp); 119 received.keySet().retainAll(tmp); 120 } 121 122 private void updateStats(Message msg, short direction) { 123 int length; 124 HashMap map; 125 boolean mcast; 126 Address dest, src; 127 128 if(msg == null) return; 129 length=msg.getLength(); 130 dest=msg.getDest(); 131 src=msg.getSrc(); 132 mcast=dest == null || dest.isMulticastAddress(); 133 134 if(direction == UP) { received_msgs++; 136 received_bytes+=length; 137 if(mcast) { 138 received_mcasts++; 139 received_mcast_bytes+=length; 140 } 141 else { 142 received_ucasts++; 143 received_ucast_bytes+=length; 144 } 145 } 146 else { sent_msgs++; 148 sent_bytes+=length; 149 if(mcast) { 150 sent_mcasts++; 151 sent_mcast_bytes+=length; 152 } 153 else { 154 sent_ucasts++; 155 sent_ucast_bytes+=length; 156 } 157 } 158 159 Address key=direction == UP? src : dest; 160 map=direction == UP? received : sent; 161 Entry entry=(Entry)map.get(key); 162 if(entry == null) { 163 entry=new Entry(); 164 map.put(key, entry); 165 } 166 entry.msgs++; 167 entry.bytes+=length; 168 if(mcast) { 169 entry.mcasts++; 170 entry.mcast_bytes+=length; 171 } 172 else { 173 entry.ucasts++; 174 entry.ucast_bytes+=length; 175 } 176 } 177 178 179 180 181 static class Entry { 182 long msgs, bytes, ucasts, mcasts, ucast_bytes, mcast_bytes; 183 184 public String toString() { 185 StringBuilder sb=new StringBuilder (); 186 sb.append(msgs).append(" (").append(bytes).append(" bytes)"); 187 sb.append(": ").append(ucasts).append(" ucasts (").append(ucast_bytes).append(" bytes), "); 188 sb.append(mcasts).append(" mcasts (").append(mcast_bytes).append(" bytes)"); 189 return sb.toString(); 190 } 191 } 192 193 194 195 } 196 | Popular Tags |