本文整理汇总了Java中com.sun.management.OperatingSystemMXBean类的典型用法代码示例。如果您正苦于以下问题:Java OperatingSystemMXBean类的具体用法?Java OperatingSystemMXBean怎么用?Java OperatingSystemMXBean使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OperatingSystemMXBean类属于com.sun.management包,在下文中一共展示了OperatingSystemMXBean类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
public static void main(String[] argv) throws Exception {
OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
ManagementFactory.getOperatingSystemMXBean();
double load;
for(int i=0; i<10; i++) {
load = mbean.getProcessCpuLoad();
if((load<0.0 || load>1.0) && load != -1.0) {
throw new RuntimeException("getProcessCpuLoad() returns " + load
+ " which is not in the [0.0,1.0] interval");
}
try {
Thread.sleep(200);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
示例2: start
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
@Override
public void start() throws Exception {
systemMBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
// A random identifier
String pid = UUID.randomUUID().toString();
// Get the kafka producer config
JsonObject config = config();
// Create the producer
producer = KafkaWriteStream.create(vertx.getDelegate(), config.getMap(), String.class, JsonObject.class);
// Publish the metircs in Kafka
vertx.setPeriodic(1000, id -> {
JsonObject metrics = new JsonObject();
metrics.put("CPU", systemMBean.getProcessCpuLoad());
metrics.put("Mem", systemMBean.getTotalPhysicalMemorySize() - systemMBean.getFreePhysicalMemorySize());
producer.write(new ProducerRecord<>("the_topic", new JsonObject().put(pid, metrics)));
});
}
示例3: getCpuUsage
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
public synchronized double getCpuUsage() {
if(lastSystemTime == 0) {
baselineCounters();
return 0;
}
long systemTime = System.nanoTime();
long processCpuTime = 0;
if(ManagementFactory.getOperatingSystemMXBean() instanceof OperatingSystemMXBean) {
processCpuTime = ((OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean()).getProcessCpuTime();
}
double cpuUsage = (double) (processCpuTime - lastProcessCpuTime) / (systemTime - lastSystemTime);
lastSystemTime = systemTime;
lastProcessCpuTime = processCpuTime;
return cpuUsage / availableProcessors;
}
示例4: getPhysicalMemory
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
/**
* Gets the amount of physical memory (RAM) in this machine, in bytes.
*
* @return the amount of RAM, in bytes; or -1 if the amount of RAM
* cannot be queried.
*/
public static long getPhysicalMemory() {
final Object o = ManagementFactory.getOperatingSystemMXBean();
try {
if (o instanceof OperatingSystemMXBean) {
final OperatingSystemMXBean osb = (OperatingSystemMXBean) o;
return osb.getTotalPhysicalMemorySize();
}
}
catch (NoClassDefFoundError e) {
// com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
}
// We didn't get a com.sun.management.OperatingSystemMXBean.
return -1;
}
示例5: reportCPU
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
private Optional<Double> reportCPU(OperatingSystemMXBean operatingSystemMXBean, RuntimeMXBean runtimeMXBean) {
int availableProcessors = divideByNbCores ? operatingSystemMXBean.getAvailableProcessors() : 1;
long upTime = runtimeMXBean.getUptime();
long processCpuTime = operatingSystemMXBean.getProcessCpuTime();
if (lastMeasures == null
|| lastMeasures.availableProcessors != availableProcessors
|| lastMeasures.processCpuTime < 0) {
lastMeasures = new Measures(availableProcessors, upTime, processCpuTime);
return Optional.empty();
}
long elapsedCpu = processCpuTime - lastMeasures.processCpuTime;
long elapsedTime = upTime - lastMeasures.upTime;
lastMeasures = new Measures(availableProcessors, upTime, processCpuTime);
return Optional.of(Math.min(99D, elapsedCpu / (elapsedTime * 10000D * availableProcessors)));
}
示例6: getSystemStats
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
Map<String, Object> getSystemStats() {
Map<String, Object> stats = new HashMap<>();
OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
double cpuUsage = osBean.getProcessCpuLoad() < 0 ? 0 : osBean.getProcessCpuLoad() * 100.0;
stats.put("javaVersion", System.getProperty("java.version"));
stats.put("osName", System.getProperty("os.name"));
stats.put("osArch", System.getProperty("os.arch"));
stats.put("osVersion", System.getProperty("os.version"));
stats.put("maxMemory", Runtime.getRuntime().maxMemory());
stats.put("totalMemory", Runtime.getRuntime().totalMemory());
stats.put("freeMemory", Runtime.getRuntime().freeMemory());
stats.put("cores", Long.parseLong(String.valueOf(Runtime.getRuntime().availableProcessors()))); // Is this really necessary?
stats.put("cpuUsage", Math.min(round(cpuUsage), 100.0));
return stats;
}
示例7: main
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
public static void main(String[] argv) throws Exception {
OperatingSystemMXBean mbean = (com.sun.management.OperatingSystemMXBean)
ManagementFactory.getOperatingSystemMXBean();
double load;
for(int i=0; i<10; i++) {
load = mbean.getSystemCpuLoad();
if((load<0.0 || load>1.0) && load != -1.0) {
throw new RuntimeException("getSystemCpuLoad() returns " + load
+ " which is not in the [0.0,1.0] interval");
}
try {
Thread.sleep(200);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
示例8: main
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
public static void main(String... args) throws Exception {
OperatingSystemMXBean bean = (OperatingSystemMXBean)
ManagementFactory.getOperatingSystemMXBean();
List<String> failedGetterNames = new ArrayList<String>();
List<String> testedGetterNames = Arrays.asList(
"getTotalSwapSpaceSize", "getFreeSwapSpaceSize",
"getTotalPhysicalMemorySize", "getFreePhysicalMemorySize");
for (String getterName : testedGetterNames) {
Method getter = OperatingSystemMXBean.class.getMethod(getterName);
long value = (Long) getter.invoke(bean);
if (value == MEMORYSTATUS_OVERFLOW) {
failedGetterNames.add(getterName);
}
}
if (!failedGetterNames.isEmpty()) {
throw new AssertionError(failedGetterNames);
}
System.out.println("Test passed.");
}
示例9: main
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
public static void main(String[] args) {
OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
System.out.println("Platform: " + operatingSystemMXBean.getArch());
System.out.println("Name: " + operatingSystemMXBean.getName());
System.out.println("Version: " + operatingSystemMXBean.getVersion());
System.out.println("Available processors: " + operatingSystemMXBean.getAvailableProcessors());
System.out.println("Average load: " + operatingSystemMXBean.getSystemLoadAverage());
System.out.println("Committed virtual memory : " + operatingSystemMXBean.getCommittedVirtualMemorySize());
System.out.println("Total physical memory: " + operatingSystemMXBean.getTotalPhysicalMemorySize());
System.out.println("Free physical memory: " + operatingSystemMXBean.getFreePhysicalMemorySize());
System.out.println("Total swap space: " + operatingSystemMXBean.getTotalSwapSpaceSize());
System.out.println("Free swap space: " + operatingSystemMXBean.getFreeSwapSpaceSize());
System.out.println("Process CPU load: " + operatingSystemMXBean.getProcessCpuLoad() + "%");
System.out.println("Process CPU time: " + operatingSystemMXBean.getProcessCpuTime());
System.out.println("System CPU load: " + operatingSystemMXBean.getSystemCpuLoad() + "%");
}
示例10: showCPU
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
public static void showCPU() {
OperatingSystemMXBean osbean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
RuntimeMXBean runbean = ManagementFactory.getRuntimeMXBean();
long bfprocesstime = osbean.getProcessCpuTime();
long bfuptime = runbean.getUptime();
long ncpus = osbean.getAvailableProcessors();
// for (int i = 0; i < 1000000; ++i) {
// ncpus = osbean.getAvailableProcessors();
// }
long afprocesstime = osbean.getProcessCpuTime();
long afuptime = runbean.getUptime();
float cal = (afprocesstime - bfprocesstime) / ((afuptime - bfuptime) * 10000f);
float usage = Math.min(99f, cal);
System.out.println("Calculation: " + cal);
System.out.println("CPU Usage: " + usage);
}
示例11: getProcessTime
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
public int getProcessTime()
{
int cpuPercent = 0;
if ( (bean instanceof com.sun.management.OperatingSystemMXBean) )
{
currCPUMilliesUsed = (bean.getProcessCpuTime()/1000000);
currCPUCalendar = Calendar.getInstance();
long milliesPassed = (currCPUCalendar.getTimeInMillis() - lastCPUCalendar.getTimeInMillis());
long cpuMilliesDiff = (currCPUMilliesUsed - lastCPUMilliesUsed);
cpuPercent = (int)Math.round((cpuMilliesDiff / (milliesPassed * 0.001))/10);
if (cpuPercent > 100) {cpuPercent = 100;}
lastCPUMilliesUsed = currCPUMilliesUsed;
lastCPUCalendar.setTimeInMillis(currCPUCalendar.getTimeInMillis());
}
else
{
cpuPercent = 50;
}
return cpuPercent;
}
示例12: instantiateCPUMetrics
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
private static void instantiateCPUMetrics(MetricGroup metrics) {
try {
final OperatingSystemMXBean mxBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
metrics.<Double, Gauge<Double>>gauge("Load", new Gauge<Double> () {
@Override
public Double getValue() {
return mxBean.getProcessCpuLoad();
}
});
metrics.<Long, Gauge<Long>>gauge("Time", new Gauge<Long> () {
@Override
public Long getValue() {
return mxBean.getProcessCpuTime();
}
});
} catch (Exception e) {
LOG.warn("Cannot access com.sun.management.OperatingSystemMXBean.getProcessCpuLoad()" +
" - CPU load metrics will not be available.", e);
}
}
示例13: execute
import com.sun.management.OperatingSystemMXBean; //导入依赖的package包/类
@Override
public void execute(AgentOrder agentOrder) {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
if (operatingSystemMXBean == null) {
return;
}
long totalSwapSpace = operatingSystemMXBean.getTotalSwapSpaceSize();
long freeSwapSpace = operatingSystemMXBean.getFreeSwapSpaceSize();
long usedSwapSpace = totalSwapSpace - freeSwapSpace;
OsEventBuilder eventBuilder = OsEventBuilder.createSwapBuilder(this.getEventBuilderData());
eventBuilder.setTotalSwapSpace(totalSwapSpace);
eventBuilder.setFreeSwapSpace(freeSwapSpace);
eventBuilder.setUsedSwapSpace(usedSwapSpace);
this.sendEvent(eventBuilder.toEvent());
}