下面列出了java.lang.management.OperatingSystemMXBean#getAvailableProcessors ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double) method.invoke(operatingSystemMXBean, new Object[0]);
if (load == -1) {
com.sun.management.OperatingSystemMXBean bean =
(com.sun.management.OperatingSystemMXBean) operatingSystemMXBean;
load = bean.getSystemCpuLoad();
}
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN),
(load < 0 ? "" : "load:" + load + ",") + "cpu:" + cpu);
}
CpuMonitorProbe(MonitoredDataResolver resolver, Application application,
Jvm jvm) {
super(2, createItemDescriptors(), resolver);
cpuSupported = jvm.isCpuMonitoringSupported();
gcSupported = jvm.isCollectionTimeSupported();
int pCount = 1;
JmxModel jmxModel = JmxModelFactory.getJmxModelFor(application);
if (jmxModel != null && jmxModel.getConnectionState() == ConnectionState.CONNECTED) {
JvmMXBeans mxbeans = JvmMXBeansFactory.getJvmMXBeans(jmxModel);
if (mxbeans != null) {
OperatingSystemMXBean osbean = mxbeans.getOperatingSystemMXBean();
if (osbean != null) pCount = osbean.getAvailableProcessors();
}
}
processorsCount = pCount;
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double) method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), "Load: " + load + " / CPU: " + cpu);
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double)method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), "Load: " + load + " / CPU: " + cpu);
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double)method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), (load < 0 ? "" : "load:" + load + ",") + "cpu:" + cpu);
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double)method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), "Load: " + load + " / CPU: " + cpu);
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double)method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), (load < 0 ? "" : "load:" + load + ",") + "cpu:" + cpu);
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double)method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), "Load: " + load + " / CPU: " + cpu);
}
private void analyzeCpuUsage(final List<String> details) {
final OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
final double loadAverage = os.getSystemLoadAverage();
final int availableProcs = os.getAvailableProcessors();
if (loadAverage > availableProcs) {
details.add(String.format("1-minute CPU Load Average is %1$.2f, which exceeds the %2$d available cores. CPU is over-utilized.", loadAverage, availableProcs));
} else if (loadAverage > 0.9 * availableProcs) {
details.add(String.format("1-minute CPU Load Average is %1$.2f, which exceeds 90%% of the %2$d available cores. CPU may struggle to keep up.", loadAverage, availableProcs));
}
}
/**
* 系统平均负载(满负荷状态为1.00*CPU核数)
*/
public Double getData() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage");
load = (Double) method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return Double.valueOf(String.format("%.4f", load / cpu));
}
/**
* Get number of available processors.
* @return number of available processors.
*/
public static int getAvailableProcessors() {
final OperatingSystemMXBean bean = ManagementFactory.getOperatingSystemMXBean();
if (bean != null) {
return bean.getAvailableProcessors();
}
throw new IllegalStateException();
}
int getAvailableProcessors() {
JvmMXBeans jmx = getJvmMXBeans();
if (jmx != null) {
OperatingSystemMXBean osMXBean = jmx.getOperatingSystemMXBean();
if (osMXBean != null) {
return osMXBean.getAvailableProcessors();
}
}
return -1;
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage");
load = (Double)method.invoke(operatingSystemMXBean);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), (load < 0 ? "" : "load:" + load + ",") + "cpu:" + cpu);
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double)method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), "Load: " + load + " / CPU: " + cpu);
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double)method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), (load < 0 ? "" : "load:" + load + ",") + "cpu:" + cpu);
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double)method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), "Load: " + load + " / CPU: " + cpu);
}
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double)method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), "Load: " + load + " / CPU: " + cpu);
}
@Override
protected Result check() throws Exception {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load = operatingSystemMXBean.getSystemLoadAverage();
int cpu = operatingSystemMXBean.getAvailableProcessors();
if (load < cpu) {
return Result.healthy();
} else {
return Result.unhealthy("load:%s,cpu:%s", load, cpu);
}
}
public static void setupJvmMetrics() {
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
Metrics.newGauge(new MetricName(ORG_APACHE_BLUR, SYSTEM, LOAD_AVERAGE), new Gauge<Double>() {
@Override
public Double value() {
return operatingSystemMXBean.getSystemLoadAverage();
}
});
Metrics.newGauge(new MetricName(ORG_APACHE_BLUR, JVM, HEAP_USED), new Gauge<Long>() {
@Override
public Long value() {
MemoryUsage usage = memoryMXBean.getHeapMemoryUsage();
return usage.getUsed();
}
});
Method processCpuTimeMethod = null;
for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
if (method.getName().equals("getProcessCpuTime")) {
method.setAccessible(true);
processCpuTimeMethod = method;
}
}
final double availableProcessors = operatingSystemMXBean.getAvailableProcessors();
if (processCpuTimeMethod != null) {
final Method pctm = processCpuTimeMethod;
Metrics.newGauge(new MetricName(ORG_APACHE_BLUR, JVM, CPU_USED), new Gauge<Double>() {
private long start = System.nanoTime();
private long lastCpuTime = getProcessCputTime(pctm, operatingSystemMXBean);
@Override
public Double value() {
long now = System.nanoTime();
long cpuTime = getProcessCputTime(pctm, operatingSystemMXBean);
long time = now - start;
long processTime = cpuTime - lastCpuTime;
try {
return ((processTime / (double) time) / availableProcessors) * 100.0;
} finally {
lastCpuTime = cpuTime;
start = System.nanoTime();
}
}
});
}
}
public MacOSX()
{
final OperatingSystemMXBean b = ManagementFactory.getOperatingSystemMXBean();
m_processors = b.getAvailableProcessors();
m_cpuInfo = b.getName()+" "+b.getVersion()+" "+b.getArch();
}