1 |
mojays |
2 |
package appl.parallel; |
2 |
|
|
|
3 |
|
|
import java.io.Serializable; |
4 |
|
|
import java.lang.management.ManagementFactory; |
5 |
|
|
import java.lang.management.MemoryPoolMXBean; |
6 |
|
|
import java.lang.management.MemoryUsage; |
7 |
|
|
import java.lang.management.ThreadMXBean; |
8 |
|
|
import java.net.UnknownHostException; |
9 |
|
|
import java.util.List; |
10 |
|
|
import java.util.Properties; |
11 |
|
|
|
12 |
|
|
/** |
13 |
|
|
* Sets the following properties: <br> |
14 |
|
|
* <br> |
15 |
|
|
* Maximum Java Memory <br> |
16 |
|
|
* Free Java Memory<br> |
17 |
|
|
* System Information<br> |
18 |
|
|
* System Architecture<br> |
19 |
|
|
* System Version<br> |
20 |
|
|
* Hostname<br> |
21 |
|
|
* IP<br> |
22 |
|
|
* |
23 |
|
|
* @see ComputingResourceProperties |
24 |
|
|
* @author Dominik Appl |
25 |
|
|
*/ |
26 |
|
|
public class SimpleResourceProperties implements ComputingResourceProperties, |
27 |
|
|
Serializable { |
28 |
|
|
|
29 |
|
|
protected Properties properties = new Properties(); |
30 |
|
|
|
31 |
|
|
public SimpleResourceProperties() { |
32 |
|
|
java.net.InetAddress i; |
33 |
|
|
try { |
34 |
|
|
i = java.net.InetAddress.getLocalHost(); |
35 |
|
|
properties.setProperty("Hostname", i.getHostName()); |
36 |
|
|
|
37 |
|
|
// also the DefaultName of the machine, if not specified |
38 |
|
|
properties.setProperty("Name", i.getHostName()); |
39 |
|
|
properties.setProperty("IP", i.getHostAddress()); |
40 |
|
|
} catch (UnknownHostException e) { |
41 |
|
|
// TODO Auto-generated catch block |
42 |
|
|
e.printStackTrace(); |
43 |
|
|
} |
44 |
|
|
properties.setProperty("Memory Maximum", "" |
45 |
|
|
+ getFormatedMemoryString(Runtime.getRuntime().maxMemory())); |
46 |
|
|
properties.setProperty("Memory Free", "" |
47 |
|
|
+ getFormatedMemoryString(Runtime.getRuntime().freeMemory())); |
48 |
|
|
properties.setProperty("Memory Used", "" |
49 |
|
|
+ getFormatedMemoryString(getUsedMemory())); |
50 |
|
|
properties.setProperty("Operating System", System |
51 |
|
|
.getProperty("os.name")); |
52 |
|
|
properties.setProperty("System Architecture", System |
53 |
|
|
.getProperty("os.arch")); |
54 |
|
|
properties.setProperty("System Version", System |
55 |
|
|
.getProperty("os.version")); |
56 |
|
|
properties.setProperty("Available Processors", Runtime.getRuntime() |
57 |
|
|
.availableProcessors() |
58 |
|
|
+ ""); |
59 |
|
|
|
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/* |
63 |
|
|
* (non-Javadoc) |
64 |
|
|
* |
65 |
|
|
* @see appl.parallel.ComputingResourceProperties#getProperties() |
66 |
|
|
*/ |
67 |
|
|
public Properties getProperties() { |
68 |
|
|
return properties; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
protected long getUsedMemory() { |
72 |
|
|
ThreadMXBean tb = ManagementFactory.getThreadMXBean(); |
73 |
|
|
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); |
74 |
|
|
long sum = 0; |
75 |
|
|
for (MemoryPoolMXBean pool : pools) { |
76 |
|
|
MemoryUsage peak = pool.getPeakUsage(); |
77 |
|
|
sum += peak.getUsed(); |
78 |
|
|
} |
79 |
|
|
return sum; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
protected String getFormatedMemoryString(long bytes) { |
83 |
|
|
long mb = bytes / (1024 * 1024); |
84 |
|
|
long kb = bytes / 1024 - (mb * 1024); |
85 |
|
|
// only the first char of kb |
86 |
|
|
kb = kb / 100; |
87 |
|
|
return mb + "." + kb + " MB"; |
88 |
|
|
|
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
/* |
92 |
|
|
* (non-Javadoc) only indirection |
93 |
|
|
* |
94 |
|
|
* @see appl.parallel.ComputingResourceProperties#getProperty(java.lang.String) |
95 |
|
|
*/ |
96 |
|
|
public String getProperty(String key) { |
97 |
|
|
return properties.getProperty(key); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
/* |
101 |
|
|
* (non-Javadoc) only indirection |
102 |
|
|
* |
103 |
|
|
* @see appl.parallel.ComputingResourceProperties#setProperty(java.lang.String, |
104 |
|
|
* java.lang.String) |
105 |
|
|
*/ |
106 |
|
|
public void setProperty(String key, String value) { |
107 |
|
|
properties.setProperty(key, value); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
public String toString() { |
111 |
|
|
return getProperty("Name"); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/* |
115 |
|
|
* (non-Javadoc) |
116 |
|
|
* |
117 |
|
|
* @see appl.parallel.ComputingResourceProperties#getName() |
118 |
|
|
*/ |
119 |
|
|
public String getName() { |
120 |
|
|
return getProperty("Name"); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
public boolean isAvailable() { |
124 |
|
|
return getProperty("isAvailable").equals("true"); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
public String getIP() { |
128 |
|
|
return getProperty("IP"); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
public String getPort() { |
132 |
|
|
return getProperty("Port"); |
133 |
|
|
} |
134 |
|
|
} |