| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package sk.baka.webvm.analyzer.hostos; |
| 20 | |
|
| 21 | |
import java.lang.management.OperatingSystemMXBean; |
| 22 | |
import java.io.BufferedReader; |
| 23 | |
import java.io.File; |
| 24 | |
import java.io.FileReader; |
| 25 | |
import java.lang.management.ManagementFactory; |
| 26 | |
import java.util.Collections; |
| 27 | |
import java.util.List; |
| 28 | |
import java.util.StringTokenizer; |
| 29 | |
import java.util.logging.Level; |
| 30 | |
import java.util.logging.Logger; |
| 31 | |
import sk.baka.tools.IOUtils; |
| 32 | |
import sk.baka.webvm.analyzer.CpuUsage; |
| 33 | |
import sk.baka.webvm.analyzer.ICpuUsageMeasure; |
| 34 | |
import static sk.baka.webvm.misc.Constants.*; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 3 | public final class Cpu { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public static CpuUsage newHostCpu() { |
| 47 | 3 | return new CpuUsage(new CpuUsageLinuxStrategy()); |
| 48 | |
} |
| 49 | 1 | private static final CpuUsage HOST_CPU = newHostCpu(); |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public static boolean isHostCpuSupported() { |
| 56 | 0 | return HOST_CPU.supported(); |
| 57 | |
} |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public static CpuUsage newHostIOCpu() { |
| 64 | 3 | return new CpuUsage(new IOCpuUsageLinuxStrategy()); |
| 65 | |
} |
| 66 | 1 | private static final CpuUsage HOST_IO_CPU = newHostIOCpu(); |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public static boolean isHostIOCpuSupported() { |
| 73 | 0 | return HOST_IO_CPU.supported(); |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public static CpuUsage newJavaCpu() { |
| 81 | 3 | return new CpuUsage(new JavaCpuUsageStrategy()); |
| 82 | |
} |
| 83 | 1 | private static final CpuUsage JAVA_CPU = newJavaCpu(); |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public static boolean isJavaCpuSupported() { |
| 90 | 0 | return JAVA_CPU.supported(); |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | 6 | private static class CpuUsageLinuxStrategy implements ICpuUsageMeasure { |
| 97 | |
|
| 98 | 1 | private static final File PROC_STAT = new File("/proc/stat"); |
| 99 | |
|
| 100 | |
public boolean supported() { |
| 101 | 3 | return PROC_STAT.exists(); |
| 102 | |
} |
| 103 | |
|
| 104 | |
public Object measure() throws Exception { |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | 2 | final BufferedReader in = new BufferedReader(new FileReader(PROC_STAT)); |
| 109 | |
try { |
| 110 | 2 | for (String line = in.readLine(); line != null; line = in.readLine()) { |
| 111 | 2 | if (line.startsWith("cpu ")) { |
| 112 | 2 | final StringTokenizer t = new StringTokenizer(line); |
| 113 | 2 | t.nextToken(); |
| 114 | 2 | final long user = Long.parseLong(t.nextToken()); |
| 115 | 2 | final long nice = Long.parseLong(t.nextToken()); |
| 116 | 2 | final long system = Long.parseLong(t.nextToken()); |
| 117 | 2 | final long idle = Long.parseLong(t.nextToken()); |
| 118 | 2 | return new long[]{user, nice, system, idle}; |
| 119 | |
} |
| 120 | |
} |
| 121 | |
} finally { |
| 122 | 2 | IOUtils.closeQuietly(in); |
| 123 | 0 | } |
| 124 | 0 | throw new IllegalStateException("No cpu line"); |
| 125 | |
} |
| 126 | |
private static final int MEASURE_USER = 0; |
| 127 | |
private static final int MEASURE_NICE = 1; |
| 128 | |
private static final int MEASURE_SYSTEM = 2; |
| 129 | |
private static final int MEASURE_IDLE = 3; |
| 130 | |
|
| 131 | |
public int getAvgCpuUsage(Object m1, Object m2) { |
| 132 | 1 | final long[] me1 = (long[]) m1; |
| 133 | 1 | final long[] me2 = (long[]) m2; |
| 134 | 1 | final long sampleTimeDelta = me2[MEASURE_USER] + me2[MEASURE_NICE] + me2[MEASURE_SYSTEM] + me2[MEASURE_IDLE] - |
| 135 | |
me1[MEASURE_USER] - me1[MEASURE_NICE] - me1[MEASURE_SYSTEM] - me1[MEASURE_IDLE]; |
| 136 | 1 | if (sampleTimeDelta <= 0) { |
| 137 | 0 | return 0; |
| 138 | |
} |
| 139 | 1 | final long cpuIdle = (me2[MEASURE_IDLE] - me1[MEASURE_IDLE]) * HUNDRED_PERCENT / sampleTimeDelta; |
| 140 | 1 | return HUNDRED_PERCENT - ((int) cpuIdle); |
| 141 | |
} |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | 6 | private static class IOCpuUsageLinuxStrategy implements ICpuUsageMeasure { |
| 148 | |
|
| 149 | 1 | private final static File DISKSTATS = new File("/proc/diskstats"); |
| 150 | |
|
| 151 | |
public boolean supported() { |
| 152 | 3 | return DISKSTATS.exists(); |
| 153 | |
} |
| 154 | |
|
| 155 | |
public Object measure() throws Exception { |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | 2 | long weightedMillisSpentIOTotal = 0; |
| 160 | 2 | final BufferedReader in = new BufferedReader(new FileReader(DISKSTATS)); |
| 161 | 2 | final long currentTimeMillis = System.currentTimeMillis(); |
| 162 | |
try { |
| 163 | 62 | for (String line = in.readLine(); line != null; line = in.readLine()) { |
| 164 | 60 | final StringTokenizer t = new StringTokenizer(line); |
| 165 | 60 | final List<Object> tokens = Collections.list(t); |
| 166 | 60 | final String devname = (String) tokens.get(DISKSTATS_DEVNAME); |
| 167 | 60 | if (Character.isDigit(devname.charAt(devname.length() - 1))) { |
| 168 | |
|
| 169 | 56 | continue; |
| 170 | |
} |
| 171 | 4 | final long weightedMillisSpentIO = Long.parseLong((String) tokens.get(DISKSTATS_MILLIS_SPENT_IO)); |
| 172 | 4 | weightedMillisSpentIOTotal += weightedMillisSpentIO; |
| 173 | |
} |
| 174 | |
} finally { |
| 175 | 2 | IOUtils.closeQuietly(in); |
| 176 | 2 | } |
| 177 | 2 | return new long[]{weightedMillisSpentIOTotal, currentTimeMillis}; |
| 178 | |
} |
| 179 | |
private static final int MEASURE_MILLIS_SPENT_IO = 0; |
| 180 | |
private static final int MEASURE_CURRENT_TIME_MILLIS = 1; |
| 181 | |
private static final int DISKSTATS_DEVNAME = 2; |
| 182 | |
private static final int DISKSTATS_MILLIS_SPENT_IO = 12; |
| 183 | |
|
| 184 | |
public int getAvgCpuUsage(Object m1, Object m2) { |
| 185 | 1 | final long[] me1 = (long[]) m1; |
| 186 | 1 | final long[] me2 = (long[]) m2; |
| 187 | 1 | final long sampleTimeDelta = me2[MEASURE_CURRENT_TIME_MILLIS] - me1[MEASURE_CURRENT_TIME_MILLIS]; |
| 188 | 1 | if (sampleTimeDelta <= 0) { |
| 189 | 0 | return 0; |
| 190 | |
} |
| 191 | 1 | long cpuSpentIO = (me2[MEASURE_MILLIS_SPENT_IO] - me1[MEASURE_MILLIS_SPENT_IO]) * HUNDRED_PERCENT / sampleTimeDelta / NUMBER_OF_PROCESSORS; |
| 192 | 1 | return (int) cpuSpentIO; |
| 193 | |
} |
| 194 | |
} |
| 195 | 1 | private final static int NUMBER_OF_PROCESSORS = Runtime.getRuntime().availableProcessors(); |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | 6 | private static class JavaCpuUsageStrategy implements ICpuUsageMeasure { |
| 201 | |
|
| 202 | |
private static final OperatingSystemMXBean BEAN; |
| 203 | |
private static final Class<?> BEAN_CLASS; |
| 204 | 1 | private static final Logger log = Logger.getLogger(JavaCpuUsageStrategy.class.getName()); |
| 205 | |
|
| 206 | |
|
| 207 | |
static { |
| 208 | 1 | OperatingSystemMXBean b = null; |
| 209 | 1 | Class<?> clazz = null; |
| 210 | |
try { |
| 211 | 1 | clazz = Class.forName("com.sun.management.OperatingSystemMXBean"); |
| 212 | 1 | b = OperatingSystemMXBean.class.cast(clazz.cast(ManagementFactory.getOperatingSystemMXBean())); |
| 213 | 0 | } catch (Throwable ex) { |
| 214 | 0 | log.log(Level.INFO, "MemoryJMXStrategy disabled: com.sun.management.OperatingSystemMXBean unavailable", ex); |
| 215 | 1 | } |
| 216 | 1 | BEAN = b; |
| 217 | 1 | BEAN_CLASS = clazz; |
| 218 | 1 | } |
| 219 | |
|
| 220 | |
public boolean supported() { |
| 221 | 3 | return BEAN != null; |
| 222 | |
} |
| 223 | |
|
| 224 | |
public Object measure() throws Exception { |
| 225 | 2 | long processCpuTime = (Long) BEAN_CLASS.getMethod("getProcessCpuTime").invoke(BEAN); |
| 226 | 2 | processCpuTime = processCpuTime / NUMBER_OF_PROCESSORS; |
| 227 | 2 | long totalCpuTime = System.nanoTime(); |
| 228 | 2 | return new long[]{processCpuTime, totalCpuTime}; |
| 229 | |
} |
| 230 | |
private static final int TOTAL_CPU_TIME = 1; |
| 231 | |
private static final int PROCESS_CPU_TIME = 0; |
| 232 | |
|
| 233 | |
public int getAvgCpuUsage(Object m1, Object m2) { |
| 234 | 1 | final long[] me1 = (long[]) m1; |
| 235 | 1 | final long[] me2 = (long[]) m2; |
| 236 | 1 | final long sampleTimeDelta = me2[TOTAL_CPU_TIME] - me1[TOTAL_CPU_TIME]; |
| 237 | 1 | if (sampleTimeDelta <= 0) { |
| 238 | 0 | return 0; |
| 239 | |
} |
| 240 | 1 | return (int) ((me2[PROCESS_CPU_TIME] - me1[PROCESS_CPU_TIME]) * HUNDRED_PERCENT / sampleTimeDelta); |
| 241 | |
} |
| 242 | |
} |
| 243 | |
|
| 244 | 0 | private Cpu() { |
| 245 | 0 | throw new AssertionError(); |
| 246 | |
} |
| 247 | |
} |