1 | package de.uka.ipd.sdq.measurement.strategies.system; |
2 | |
3 | import java.io.File; |
4 | import java.lang.management.ManagementFactory; |
5 | |
6 | import javax.measure.quantity.Duration; |
7 | import javax.measure.unit.SI; |
8 | |
9 | import org.jscience.physics.amount.Amount; |
10 | |
11 | /** |
12 | * Helper class for monitoring system resources. |
13 | * |
14 | * @author Thomas Zolynski |
15 | * |
16 | */ |
17 | public final class SystemResourcesUtil { |
18 | |
19 | /** |
20 | * Sun's implementation of OperatingSystemMXBean is used. |
21 | * Eclipse is not very fond of this import (Forbidden references), so you might have to |
22 | * change your preferences (Java -> Compiler -> Errors/Warnings -> Forbidden references) |
23 | */ |
24 | private static final com.sun.management.OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); |
25 | |
26 | /** |
27 | * This systems' temp directory. Due to inconsistencies |
28 | * between different systems a file separator is added. |
29 | */ |
30 | public static final File TEMP_DIR = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator")); |
31 | |
32 | /** |
33 | * Returns the total size of the physical memory in bytes. |
34 | * |
35 | * @return total memory in bytes |
36 | */ |
37 | public static long getTotalPhysicalMemorySize() { |
38 | return os.getTotalPhysicalMemorySize(); |
39 | } |
40 | |
41 | /** |
42 | * Returns the free physical memory in bytes. |
43 | * |
44 | * @return free memory in bytes |
45 | */ |
46 | public static long getFreePhysicalMemorySize() { |
47 | return os.getFreePhysicalMemorySize(); |
48 | } |
49 | |
50 | /** |
51 | * Returns the free space of the temp directory |
52 | * |
53 | * @return free space in bytes |
54 | */ |
55 | public static long getFreeTempDirectorySize() { |
56 | return TEMP_DIR.getUsableSpace(); |
57 | } |
58 | |
59 | /** |
60 | * Returns the number of CPU cores. |
61 | * |
62 | * @return CPU cores |
63 | */ |
64 | public static int getCPUCores() { |
65 | return os.getAvailableProcessors(); |
66 | } |
67 | |
68 | /** |
69 | * Returns the CPU time used by this JVM |
70 | * @return CPU usage in nanoseconds |
71 | */ |
72 | public static double getCPUProcessTimeNS() { |
73 | return os.getProcessCpuTime(); |
74 | } |
75 | |
76 | /** |
77 | * Returns the CPU time used by this JVM |
78 | * @return CPU usage as a JScience object |
79 | */ |
80 | public static Amount<Duration> getCPUProcessTime() { |
81 | return Amount.valueOf(getCPUProcessTimeNS(), SI.NANO(SI.SECOND)); |
82 | } |
83 | |
84 | } |