KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > PERF


1 // $Id: PERF.java,v 1.8 2005/04/20 06:31:17 belaban Exp $
2

3 package org.jgroups.protocols;
4
5 import org.jgroups.Event;
6 import org.jgroups.Message;
7 import org.jgroups.View;
8 import org.jgroups.stack.Protocol;
9 import org.jgroups.stack.ProtocolObserver;
10
11 import java.util.Properties JavaDoc;
12 import java.util.Vector JavaDoc;
13
14
15 /**
16  * Measures time taken by each protocol to process a message. PERF has to be the top protocol in a stack. It
17  * adds a special header to the message which travels with the message. Upon initialization,
18  * PERF creates a ProtocolObserver for each protocol layer in the stack. That observer notes the time taken for a
19  * message to travel through the layer it observes, and records the start time, end time and total time
20  * <em>in the message itself</em> (in the header created by PERF). When a message is received by PERF, the statistics
21  * for the message are appended to a file (or displayed in a GUI (not yet done)).<p>
22  * <em>Because of different wall times between different machines, PERF should only be used for members
23  * in the same JVM. For members in different processes, physical clocks have to be synchronized: if they are
24  * only a few milliseconds aprt, the values will be incorrect.</em>
25  * For example, to write all performance data to a file, do the following:
26  * <ol>
27  * <li>Set tracing to on, e.g. <code>trace=true</code> in jgroups.properties
28  * <li>Add a trace statement for PERF.up(): <code>trace0=PERF.up DEBUG /tmp/trace.perf</code>. This will write
29  * all trace output to the given file.
30  * </ol>
31  *
32  * @author Bela Ban Oct 2001
33  * @version $Revision: 1.8 $
34  */

35 public class PERF extends Protocol {
36     boolean details=false;
37     final Vector JavaDoc members=new Vector JavaDoc();
38     Vector JavaDoc protocols=null;
39     static final String JavaDoc name="PERF";
40
41
42     /**
43      * All protocol names have to be unique !
44      */

45     public String JavaDoc getName() {
46         return name;
47     }
48
49
50     public boolean setProperties(Properties JavaDoc props) {
51         super.setProperties(props);
52         String JavaDoc str;
53
54         str=props.getProperty("details");
55         if(str != null) {
56             details=Boolean.valueOf(str).booleanValue();
57             props.remove("details");
58         }
59         if(props.size() > 0) {
60             System.err.println("PERF.setProperties(): these properties are not recognized:");
61             props.list(System.out);
62             return false;
63         }
64         return true;
65     }
66
67
68     public void start() throws Exception JavaDoc {
69         protocols=stack != null ? stack.getProtocols() : null;
70         setupObservers();
71     }
72
73
74     public void up(Event evt) {
75         Message msg;
76         PerfHeader hdr;
77
78         switch(evt.getType()) {
79
80         case Event.MSG:
81             msg=(Message)evt.getArg();
82             hdr=removePerfHeader(msg);
83             if(hdr != null) {
84                 hdr.setEndTime();
85                 hdr.setDone(name, PerfHeader.UP); // we do this here because header is removed, so PassUp won't find it
86
if(log.isTraceEnabled())
87                     log.trace(hdr.printContents(details, protocols) + "\n---------------------------------\n");
88             }
89             break;
90         }
91
92         passUp(evt); // Pass up to the layer above us
93
}
94
95
96     public void down(Event evt) {
97         Message msg;
98
99         switch(evt.getType()) {
100
101         case Event.TMP_VIEW:
102         case Event.VIEW_CHANGE:
103             Vector JavaDoc new_members=((View)evt.getArg()).getMembers();
104             synchronized(members) {
105                 members.removeAllElements();
106                 members.addAll(new_members);
107             }
108             passDown(evt);
109             break;
110
111         case Event.MSG:
112             msg=(Message)evt.getArg();
113             initializeMessage(msg); // Add a PerfHeader to the message
114
break;
115         }
116
117         passDown(evt); // Pass on to the layer below us
118
}
119
120
121     /* ----------------------------------- Private Methods -------------------------------------- */
122
123     /**
124      * Create PerfObservers for all protocols save PERF
125      */

126     void setupObservers() {
127         Protocol p=null;
128         String JavaDoc pname;
129         PerfObserver po=null;
130
131         for(int i=0; i < protocols.size(); i++) {
132             p=(Protocol)protocols.elementAt(i);
133             pname=p.getName();
134             if(pname != null) {
135                 po=new PerfObserver(pname);
136                 p.setObserver(po);
137             }
138         }
139     }
140
141
142     void initializeMessage(Message msg) {
143         PerfHeader hdr=new PerfHeader(msg.getSrc(), msg.getDest());
144         Protocol p;
145         String JavaDoc pname=null;
146
147         if(protocols == null) {
148             System.err.println("PERF.initializeMessage(): 'protocols' variable is null");
149             return;
150         }
151
152         for(int i=0; i < protocols.size(); i++) {
153             p=(Protocol)protocols.elementAt(i);
154             pname=p.getName();
155             if(pname != null) {
156                 hdr.addEntry(pname);
157             }
158         }
159         hdr.setReceived(name, PerfHeader.DOWN); // we do this here because down() didn't yet find a PerfHeader
160
msg.putHeader(name, hdr);
161     }
162
163
164     /**
165      * Removes if present. Otherwise returns null
166      */

167     PerfHeader removePerfHeader(Message m) {
168         Object JavaDoc hdr=null;
169
170         if(m == null || (hdr=m.removeHeader(name)) == null)
171             return null;
172         return (PerfHeader)hdr;
173     }
174
175
176 // Vector getProtocols() {
177
// Vector ret=(Vector)stack.getProtocols().clone();
178
// Protocol p;
179

180 // for(int i=0; i < ret.size(); i++) {
181
// p=(Protocol)ret.elementAt(i);
182
// if(p.getName() != null && p.getName().equals(getName())) {
183
// ret.removeElement(p);
184
// break;
185
// }
186
// }
187
// return ret;
188
// }
189

190     /* -------------------------------- End of Private Methods ---------------------------------- */
191 }
192
193
194 /**
195  * Observes a protocol and adds its timings to the PerfHeader attached to each protocol.
196  */

197 class PerfObserver implements ProtocolObserver {
198     final String JavaDoc prot_name;
199     boolean bottom=false;
200
201     PerfObserver(String JavaDoc prot_name) {
202         this.prot_name=prot_name;
203     }
204
205
206     public void setProtocol(Protocol prot) {
207         if(prot != null && prot.getDownProtocol() == null)
208             bottom=true;
209     }
210
211
212     public boolean up(Event evt, int num_evts) {
213         PerfHeader hdr;
214         if(evt.getType() == Event.MSG) {
215             hdr=getPerfHeader((Message)evt.getArg());
216             if(hdr != null) {
217                 hdr.setReceived(prot_name, PerfHeader.UP);
218                 if(bottom)
219                     hdr.setNetworkReceived();
220             }
221         }
222         return true;
223     }
224
225
226     public boolean passUp(Event evt) {
227         PerfHeader hdr;
228         if(evt.getType() == Event.MSG) {
229             hdr=getPerfHeader((Message)evt.getArg());
230             if(hdr != null) {
231                 hdr.setDone(prot_name, PerfHeader.UP);
232             }
233         }
234         return true;
235     }
236
237
238     public boolean down(Event evt, int num_evts) {
239         PerfHeader hdr;
240         if(evt.getType() == Event.MSG) {
241             hdr=getPerfHeader((Message)evt.getArg());
242             if(hdr != null) {
243                 hdr.setReceived(prot_name, PerfHeader.DOWN);
244             }
245         }
246         return true;
247     }
248
249
250     public boolean passDown(Event evt) {
251         PerfHeader hdr;
252         if(evt.getType() == Event.MSG) {
253             hdr=getPerfHeader((Message)evt.getArg());
254             if(hdr != null) {
255                 hdr.setDone(prot_name, PerfHeader.DOWN);
256                 if(bottom)
257                     hdr.setNetworkSent();
258             }
259         }
260         return true;
261     }
262
263
264     PerfHeader getPerfHeader(Message m) {
265         Object JavaDoc hdr=null;
266
267         if(m == null || (hdr=m.getHeader(PERF.name)) == null)
268             return null;
269         return (PerfHeader)hdr;
270     }
271
272
273 }
274
Popular Tags