Java Code Examples for java.lang.management.OperatingSystemMXBean#getAvailableProcessors()

The following examples show how to use java.lang.management.OperatingSystemMXBean#getAvailableProcessors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CpuMonitorProbe.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: LoadStatusChecker.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: Environment.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: SystemLoadHealthCheck.java    From watcher with Apache License 2.0 5 votes vote down vote up
@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);
	}
	
}
 
Example 5
Source File: LoadStatusChecker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: LoadStatusChecker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 7
Source File: LoadStatusChecker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: LoadStatusChecker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: LoadStatusChecker.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: JmxSupport.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
int getAvailableProcessors() {
    JvmMXBeans jmx = getJvmMXBeans();
    if (jmx != null) {
        OperatingSystemMXBean osMXBean = jmx.getOperatingSystemMXBean();

        if (osMXBean != null) {
            return osMXBean.getAvailableProcessors();
        }
    }
    return -1;
}
 
Example 11
Source File: OperatingSystemMetrice.java    From neural with MIT License 5 votes vote down vote up
/**
 * 系统平均负载(满负荷状态为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));
}
 
Example 12
Source File: DiagnosticAnalysisTask.java    From nifi with Apache License 2.0 5 votes vote down vote up
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));
    }
}
 
Example 13
Source File: LoadStatusChecker.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 14
Source File: LoadStatusChecker.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: LoadStatusChecker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 16
Source File: LoadStatusChecker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 17
Source File: LoadStatusChecker.java    From dubbox with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 18
Source File: LoadStatusChecker.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 19
Source File: ThriftServer.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
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();
        }
      }
    });
  }
}
 
Example 20
Source File: MacOSX.java    From database with GNU General Public License v2.0 3 votes vote down vote up
public MacOSX()
{
	final OperatingSystemMXBean b = ManagementFactory.getOperatingSystemMXBean();

	m_processors = b.getAvailableProcessors();
	
	m_cpuInfo = b.getName()+" "+b.getVersion()+" "+b.getArch();

}