1 23 24 package org.objectweb.clif.probe.cpu; 25 26 import org.objectweb.clif.supervisor.api.ClifException; 27 import org.objectweb.clif.probe.util.AbstractDumbInsert; 28 import org.objectweb.clif.storage.api.ProbeEvent; 29 import org.objectweb.lewys.probe.Probe; 30 import org.objectweb.lewys.common.exceptions.NoResourceToProbeException; 31 32 33 37 public class Insert extends AbstractDumbInsert 38 { 39 private static final short OS_UNDEF = -1; 40 private static final short OS_LINUX = 0; 41 private static final short OS_WINDOWS = 1; 42 43 44 private Probe probe; 45 private int[] resourceIds; 46 private short os = OS_UNDEF; 47 private long[] previousValues = null; 48 49 50 public Insert() 51 throws ClifException 52 { 53 try 54 { 55 if (System.getProperty("os.name").equalsIgnoreCase("linux")) 56 { 57 probe = new org.objectweb.lewys.probe.linux.CpuProbe(); 58 resourceIds = new int[] { 0, 2, 3 }; os = OS_LINUX; 60 } 61 else if (System.getProperty("os.name").startsWith("Windows")) 62 { 63 probe = new org.objectweb.lewys.probe.windows.CpuProbe(); 64 resourceIds = new int[] { 1, 2, 0 }; os = OS_WINDOWS; 66 } 67 else 68 { 69 throw new ClifException("No CPU probe available for " + System.getProperty("os.name")); 70 } 71 } 72 catch (NoResourceToProbeException ex) 73 { 74 throw new ClifException("Can't set CPU probe", ex); 75 } 76 } 77 78 79 public ProbeEvent doProbe() 80 { 81 CPUEvent event = null; 82 try 83 { 84 long[] values = probe.getValue(resourceIds); 85 switch(os) 86 { 87 case OS_LINUX: 88 values[2] += values[0] + values[1]; break; 90 } 91 if (previousValues == null) 92 { 93 previousValues = values; 94 } 95 else 96 { 97 long[] result = new long[3]; 98 long delta = values[2] - previousValues[2]; 99 if (delta != 0) 100 { 101 result[1] = 100 * (values[0] - previousValues[0]); 102 result[2] = 100 * (values[1] - previousValues[1]); 103 result[0] = (result[1] + result[2]) / delta; 104 result[1] /= delta; 105 result[2] /= delta; 106 if (result[0] <= 100 && result[1] <= 100 && result[2] <= 100) 107 { 108 previousValues = values; 109 event = new CPUEvent( 110 System.currentTimeMillis(), 111 probeId, 112 result); 113 } 114 } 115 } 116 } 117 catch (Exception ex) 118 { 119 ex.printStackTrace(System.err); 120 } 121 return event; 122 } 123 } 124 | Popular Tags |