1 |
/** |
2 |
* |
3 |
*/ |
4 |
package appl.parallel.event; |
5 |
|
6 |
import java.io.ByteArrayOutputStream; |
7 |
import java.io.IOException; |
8 |
import java.io.ObjectOutputStream; |
9 |
import java.io.Serializable; |
10 |
|
11 |
/** |
12 |
* A TransferEvent is event which is associated with the size of a data transfer |
13 |
* in bytes. It is measured by serializing the data into byte stream. This is |
14 |
* very slow! So use this events carefully. You should check if monitoring for |
15 |
* this event type is enabled, before generating it! See {@link CommEventSink} |
16 |
* for details. |
17 |
* |
18 |
* @author Dominik Appl |
19 |
*/ |
20 |
public class TransferEvent extends CommEvent { |
21 |
|
22 |
int size = 0; |
23 |
|
24 |
/** |
25 |
* @param src |
26 |
* the source of the event |
27 |
* @param target |
28 |
* the target of the event |
29 |
* @param type |
30 |
* the type of the event |
31 |
* @param toTransferObjects |
32 |
* the objects transfered (Warning: the size will be measured by |
33 |
* serializing the object. This is slow!) |
34 |
* @see #getObjectSize(Object) |
35 |
*/ |
36 |
public TransferEvent(String src, String target, CommType type, |
37 |
Object... toTransferObjects) { |
38 |
super(src, target, type); |
39 |
for (Object object : toTransferObjects) { |
40 |
size += getObjectSize(object); |
41 |
} |
42 |
} |
43 |
|
44 |
/** |
45 |
* Determines the object size in a very primitive way (serializing to a byte |
46 |
* array and returning the byteSize) I do not have to mention here |
47 |
* explicitly that this is slow, do I? There is no real alternative, because |
48 |
* in java exists. no function like <code>sizeOf</code> in C++ |
49 |
* |
50 |
* @param object |
51 |
* a {@link Serializable} object |
52 |
* @return the size in bytes |
53 |
*/ |
54 |
protected int getObjectSize(Object object) { |
55 |
if (object == null) |
56 |
return 0; |
57 |
|
58 |
byte[] byteArray = null; |
59 |
try { |
60 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
61 |
ObjectOutputStream oos = new ObjectOutputStream(baos); |
62 |
oos.writeObject(object); |
63 |
oos.close(); |
64 |
byteArray = baos.toByteArray(); |
65 |
baos.close(); |
66 |
} catch (IOException e) { |
67 |
e.printStackTrace(); |
68 |
return 0; |
69 |
} |
70 |
return byteArray.length; |
71 |
} |
72 |
|
73 |
public int getSize() { |
74 |
return size; |
75 |
} |
76 |
|
77 |
public String toString() { |
78 |
StringBuffer returnString = new StringBuffer(); |
79 |
returnString.append(type.toString()); |
80 |
returnString.append(" Transfer size: ").append(size).append(" bytes"); |
81 |
if (!src.equals("")) |
82 |
returnString.append(" source: ").append(src); |
83 |
if (!target.equals("")) |
84 |
returnString.append(" target: ").append(target); |
85 |
|
86 |
return returnString.toString(); |
87 |
} |
88 |
|
89 |
} |