KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jgroups.protocols;
4
5 import org.jgroups.Header;
6 import org.jgroups.Message;
7 import org.jgroups.stack.Protocol;
8 import org.jgroups.util.Util;
9
10 import java.io.*;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Vector JavaDoc;
14
15
16 /**
17  * Inserted by PERF into each message. Records the time taken by each protocol to process the message to
18  * which this header is attached. Travels down through the stack and up the other stack with the message.
19  *
20  * @author Bela Ban
21  */

22 public class PerfHeader extends Header {
23     Object JavaDoc sender=null;
24     Object JavaDoc receiver=null;
25     long start_time=0; // time when header was created
26
long end_time=0; // time when header was received
27
long network_send=0; // time the packet was put on the network
28
long network_recv=0; // time the packet was received from the network
29
long network_time=0; // time spent on the network (between bottom layers)
30
HashMap JavaDoc down=new HashMap JavaDoc(); // key=protocol name, val=PerfEntry
31
HashMap JavaDoc up=new HashMap JavaDoc(); // key=protocol name, val=PerfEntry
32
final static int UP=1;
33     final static int DOWN=2;
34     final static String JavaDoc classname="org.jgroups.protocols.PerfHeader";
35     static long size=0;
36     private static Message msg2;
37
38
39     static {
40         size=Util.sizeOf(classname);
41         if(size <= 0) size=400;
42     }
43
44
45     // Needed for externalization
46
public PerfHeader() {
47     }
48
49
50     public PerfHeader(Object JavaDoc sender, Object JavaDoc receiver) {
51         this.sender=sender;
52         this.receiver=receiver;
53         start_time=System.currentTimeMillis();
54     }
55
56
57     public String JavaDoc toString() {
58         return "[PerfHeader]";
59     }
60
61
62     public String JavaDoc printContents(boolean detailed) {
63         return printContents(detailed, null);
64     }
65
66
67     public String JavaDoc printContents(boolean detailed, Vector JavaDoc prots) {
68         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
69         String JavaDoc key;
70         PerfEntry val;
71         Protocol p;
72
73         if(sender != null)
74             sb.append("sender=").append(sender).append('\n');
75         if(receiver != null)
76             sb.append("receiver=").append(receiver).append('\n');
77
78         if(detailed)
79             sb.append("start_time=").append(start_time).append("\nend_time=").append(end_time).append('\n');
80
81         if(end_time >= start_time)
82             sb.append("total time=").append((end_time - start_time)).append('\n');
83         else
84             sb.append("total time=n/a\n");
85
86         if(detailed) {
87             if(network_send > 0) sb.append("network_send=").append(network_send).append('\n');
88             if(network_recv > 0) sb.append("network_recv=").append(network_recv).append('\n');
89         }
90
91         if(network_time > 0)
92             sb.append("network=").append(network_time).append('\n');
93
94         sb.append("\nDOWN\n-----\n");
95         if(prots != null) {
96             for(int i=0; i < prots.size(); i++) {
97                 p=(Protocol)prots.elementAt(i);
98                 key=p.getName();
99                 val=(PerfEntry)down.get(key);
100                 sb.append(key).append(':').append('\t').append(val.printContents(detailed)).append('\n');
101             }
102         }
103         else
104             for(Iterator JavaDoc it=down.keySet().iterator(); it.hasNext();) {
105                 key=(String JavaDoc)it.next();
106                 val=(PerfEntry)down.get(key);
107                 sb.append(key).append(':').append('\t').append(val.printContents(detailed)).append('\n');
108             }
109
110         sb.append("\nUP\n-----\n");
111         if(prots != null) {
112             for(int i=prots.size() - 1; i >= 0; i--) {
113                 p=(Protocol)prots.elementAt(i);
114                 key=p.getName();
115                 val=(PerfEntry)up.get(key);
116                 sb.append(key).append(':').append('\t').append(val.printContents(detailed)).append('\n');
117             }
118         }
119         else
120             for(Iterator JavaDoc it=up.keySet().iterator(); it.hasNext();) {
121                 key=(String JavaDoc)it.next();
122                 val=(PerfEntry)up.get(key);
123                 sb.append(key).append(':').append('\t').append(val.printContents(detailed)).append('\n');
124             }
125
126
127         return sb.toString();
128     }
129
130
131     public void setEndTime() {
132         end_time=System.currentTimeMillis();
133     }
134
135
136     public void setReceived(String JavaDoc prot_name, int type) {
137         PerfEntry entry=getEntry(prot_name, type);
138         long t=System.currentTimeMillis();
139         if(entry != null)
140             entry.setReceived(t);
141     }
142
143     public void setDone(String JavaDoc prot_name, int type) {
144         PerfEntry entry=getEntry(prot_name, type);
145         long t=System.currentTimeMillis();
146         if(entry != null)
147             entry.setDone(t);
148     }
149
150     public void setNetworkSent() {
151         network_send=System.currentTimeMillis();
152     }
153
154
155     public void setNetworkReceived() {
156         network_recv=System.currentTimeMillis();
157         if(network_send > 0 && network_recv > network_send)
158             network_time=network_recv - network_send;
159     }
160
161
162     /**
163      * Adds a new entry to both hashtables
164      */

165     public void addEntry(String JavaDoc prot_name) {
166         up.put(prot_name, new PerfEntry());
167         down.put(prot_name, new PerfEntry());
168     }
169
170
171     public void writeExternal(ObjectOutput out) throws IOException {
172         out.writeObject(sender);
173         out.writeObject(receiver);
174         out.writeLong(start_time);
175         out.writeLong(end_time);
176         out.writeLong(network_send);
177         out.writeLong(network_recv);
178         out.writeLong(network_time);
179         writeHashtable(down, out);
180         writeHashtable(up, out);
181     }
182
183
184     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
185         sender=in.readObject();
186         receiver=in.readObject();
187         start_time=in.readLong();
188         end_time=in.readLong();
189         network_send=in.readLong();
190         network_recv=in.readLong();
191         network_time=in.readLong();
192         down=readHashtable(in);
193         up=readHashtable(in);
194     }
195
196
197     public long size() {
198         return size;
199     }
200
201
202     void writeHashtable(HashMap JavaDoc h, ObjectOutput out) {
203         String JavaDoc key;
204         PerfEntry val;
205
206         try {
207             if(h == null) {
208                 out.writeInt(0);
209                 return;
210             }
211             out.writeInt(h.size());
212             for(Iterator JavaDoc it=h.keySet().iterator(); it.hasNext();) {
213                 key=(String JavaDoc)it.next();
214                 val=(PerfEntry)h.get(key);
215                 if(key == null || val == null) {
216                     System.err.println("PerfHeader.writeHashtable(): key or val is null");
217                     continue;
218                 }
219                 out.writeObject(key);
220                 out.writeObject(val);
221             }
222         }
223         catch(Exception JavaDoc ex) {
224             System.err.println("PerfHeader.writeHashtable(): " + ex);
225         }
226     }
227
228
229     HashMap JavaDoc readHashtable(ObjectInput in) {
230         HashMap JavaDoc h=new HashMap JavaDoc();
231         int num=0;
232         String JavaDoc key;
233         PerfEntry val;
234
235         try {
236             num=in.readInt();
237             if(num == 0)
238                 return h;
239             for(int i=0; i < num; i++) {
240                 key=(String JavaDoc)in.readObject();
241                 val=(PerfEntry)in.readObject();
242                 h.put(key, val);
243             }
244         }
245         catch(Exception JavaDoc ex) {
246             System.err.println("PerfHeader.readHashtable(): " + ex);
247         }
248
249         return h;
250     }
251
252
253     PerfEntry getEntry(String JavaDoc prot_name, int type) {
254         HashMap JavaDoc tmp=null;
255         PerfEntry entry=null;
256
257         if(prot_name == null) return null;
258         if(type == UP)
259             tmp=up;
260         else
261             if(type == DOWN) tmp=down;
262         if(tmp == null) return null;
263         entry=(PerfEntry)tmp.get(prot_name);
264         if(entry == null)
265             System.err.println("PerfHeader.getEntry(): protocol \"" + prot_name + "\" not found");
266         return entry;
267     }
268
269
270     public static void main(String JavaDoc[] args) {
271         PerfHeader hdr=new PerfHeader(), hdr2;
272         Message msg;
273         ByteArrayOutputStream out_stream;
274         ByteArrayInputStream in_stream;
275         ObjectOutputStream out;
276         ObjectInputStream in;
277         byte[] out_buf, in_buf;
278
279
280         hdr.addEntry("GMS");
281         hdr.addEntry("GMS");
282         hdr.addEntry("FRAG");
283         hdr.addEntry("FRAG");
284         hdr.addEntry("UDP");
285         hdr.addEntry("UDP");
286
287
288         msg=new Message();
289         msg.putHeader("PERF", hdr);
290
291
292         hdr.setReceived("GMS", PerfHeader.DOWN);
293         Util.sleep(2);
294         hdr.setDone("GMS", PerfHeader.DOWN);
295
296         hdr.setReceived("FRAG", PerfHeader.DOWN);
297         Util.sleep(20);
298         hdr.setDone("FRAG", PerfHeader.DOWN);
299
300
301         long len=msg.size();
302         System.out.println("Size is " + len);
303
304
305         hdr.setReceived("UDP", PerfHeader.DOWN);
306         Util.sleep(12);
307         hdr.setDone("UDP", PerfHeader.DOWN);
308
309
310         Util.sleep(30);
311
312         hdr.setReceived("UDP", PerfHeader.UP);
313         hdr.setDone("UDP", PerfHeader.UP);
314
315         hdr.setReceived("FRAG", PerfHeader.UP);
316         Util.sleep(23);
317         hdr.setDone("FRAG", PerfHeader.UP);
318
319         hdr.setReceived("GMS", PerfHeader.UP);
320         Util.sleep(3);
321         hdr.setDone("GMS", PerfHeader.UP);
322
323
324         hdr.setEndTime();
325
326         System.out.println(hdr.printContents(true));
327
328         try {
329             System.out.println("Saving hdr to byte buffer");
330             out_stream=new ByteArrayOutputStream(256);
331             out=new ObjectOutputStream(out_stream);
332             out.writeObject(msg);
333             out_buf=out_stream.toByteArray();
334
335             System.out.println("Constructing hdr2 from byte buffer");
336             in_buf=out_buf; // ref
337

338             in_stream=new ByteArrayInputStream(in_buf);
339             in=new ObjectInputStream(in_stream);
340
341             msg2=(Message)in.readObject();
342             hdr2=(PerfHeader)msg2.removeHeader("PERF");
343             System.out.println(hdr2.printContents(true));
344         }
345         catch(Exception JavaDoc ex) {
346             System.err.println(ex);
347         }
348
349
350     }
351
352
353 }
354
355
356 /**
357  * Entry specific for 1 protocol layer. Records time message was received by that layer and when message was passed on
358  */

359 class PerfEntry implements Externalizable {
360     long received=0;
361     long done=0;
362     long total=-1;
363
364
365     // Needed for externalization
366
public PerfEntry() {
367
368     }
369
370
371     public long getReceived() {
372         return received;
373     }
374
375     public long getDone() {
376         return done;
377     }
378
379     public long getTotal() {
380         return total;
381     }
382
383     public void setReceived(long r) {
384         received=r;
385     }
386
387     public void setDone(long d) {
388         done=d;
389         if(received > 0 && done > 0 && done >= received)
390             total=done - received;
391     }
392
393     public String JavaDoc toString() {
394         if(total >= 0)
395             return "time: " + total;
396         else
397             return "time: n/a";
398     }
399
400
401     public String JavaDoc printContents(boolean detailed) {
402         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
403         if(detailed) {
404             if(received > 0) sb.append("received=").append(received);
405             if(done > 0) {
406                 if(received > 0) sb.append(", ");
407                 sb.append("done=").append(done);
408             }
409         }
410         if(detailed && (received > 0 || done > 0)) sb.append(", ");
411         sb.append(toString());
412         return sb.toString();
413     }
414
415
416     public void writeExternal(ObjectOutput out) throws IOException {
417         out.writeLong(received);
418         out.writeLong(done);
419         out.writeLong(total);
420     }
421
422
423     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
424         received=in.readLong();
425         done=in.readLong();
426         total=in.readLong();
427     }
428
429
430 }
431
Popular Tags