本文整理匯總了Java中com.sun.management.OperatingSystemMXBean.getAvailableProcessors方法的典型用法代碼示例。如果您正苦於以下問題:Java OperatingSystemMXBean.getAvailableProcessors方法的具體用法?Java OperatingSystemMXBean.getAvailableProcessors怎麽用?Java OperatingSystemMXBean.getAvailableProcessors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.management.OperatingSystemMXBean
的用法示例。
在下文中一共展示了OperatingSystemMXBean.getAvailableProcessors方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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)));
}
示例2: 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);
}
示例3: getCartridgeStatistics
import com.sun.management.OperatingSystemMXBean; //導入方法依賴的package包/類
public CartridgeStatistics getCartridgeStatistics() throws IOException {
OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
double totalMemory = (double) (osBean.getTotalPhysicalMemorySize() / MB);
double usedMemory = (double) ((totalMemory - (osBean.getFreePhysicalMemorySize() / MB)));
double loadAvg = (double) osBean.getSystemLoadAverage();
// assume system cores = available cores to JVM
int cores = osBean.getAvailableProcessors();
double memoryConsumption = (usedMemory / totalMemory) * 100;
double loadAvgPercentage = (loadAvg / cores) * 100;
if (log.isDebugEnabled()) {
log.debug("Memory consumption: [totalMemory] " + totalMemory + "Mb [usedMemory] " + usedMemory + "Mb: " + memoryConsumption + "%");
log.debug("Processor consumption: [loadAverage] " + loadAvg + " [cores] " + cores + ": " + loadAvgPercentage + "%");
}
return (new CartridgeStatistics(memoryConsumption, loadAvgPercentage));
}
示例4: getRuntimeInfo
import com.sun.management.OperatingSystemMXBean; //導入方法依賴的package包/類
@Override
public RuntimeInfo getRuntimeInfo(SS serverStataus)
throws IOException {
MBeanServerConnection mbsc = serverStataus.getConnector().getMBeanServerConnection();
RuntimeMXBean rmbean = newPlatformMXBeanProxy(mbsc, RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
OperatingSystemMXBean osbean = newPlatformMXBeanProxy(mbsc, OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
HashMap<String, String> map = new HashMap<String, String>(rmbean.getSystemProperties());
RuntimeInfo rti = new RuntimeInfo(rmbean.getBootClassPath(),
rmbean.getClassPath(),
rmbean.getInputArguments(),
rmbean.getLibraryPath(),
rmbean.getName(),
map,
osbean.getAvailableProcessors(),
osbean.getSystemLoadAverage());
return rti;
}
示例5: getCpu
import com.sun.management.OperatingSystemMXBean; //導入方法依賴的package包/類
protected double getCpu() {
OperatingSystemMXBean osbean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
RuntimeMXBean runbean = java.lang.management.ManagementFactory.getRuntimeMXBean();
long uptime = runbean.getUptime();
long processCpuTime = osbean.getProcessCpuTime();
//cpu count
int processors = osbean.getAvailableProcessors();
//uptime in milliseconds ,and processCpuTime in nao seconds
double cpu = (processCpuTime - lastProcessCpuTime) / ((uptime - lastUptime) * 10000f * processors);
lastProcessCpuTime = processCpuTime;
lastUptime = uptime;
return (int) cpu;
}
示例6: PerformanceStatus
import com.sun.management.OperatingSystemMXBean; //導入方法依賴的package包/類
public PerformanceStatus() {
OperatingSystemMXBean bean= (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
heapMemoryUsage= memBean.getHeapMemoryUsage();
nonHeapMemoryUsage = memBean.getNonHeapMemoryUsage();
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
currentThreadCount=threadBean.getThreadCount();
daemonThreadCount= threadBean.getDaemonThreadCount();
beanCreatedThreadCount= threadBean.getTotalStartedThreadCount();
peakThreadCount = threadBean.getPeakThreadCount();
ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
loadedClassCount=classLoadingBean.getLoadedClassCount();
totalLoadedClassCount=classLoadingBean.getTotalLoadedClassCount();
unloadedClassCount=classLoadingBean.getUnloadedClassCount();
committedVirtualMemorySize = (bean.getCommittedVirtualMemorySize());
freePhysicalMemorySize =(bean.getFreePhysicalMemorySize());
totalPhysicalMemorySize =(bean.getTotalPhysicalMemorySize());
freeSwapSpaceSize =(bean.getFreeSwapSpaceSize());
totalSwapSpaceSize =(bean.getTotalSwapSpaceSize());
processCpuTime =(bean.getProcessCpuTime());
availableProcessors =bean.getAvailableProcessors();
processCpuLoad =bean.getProcessCpuLoad();
systemCpuLoad =bean.getSystemCpuLoad();
systemLoadAverage =bean.getSystemLoadAverage();
appStartUpTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(runtimeBean.getStartTime()));
runtime = (new Date().getTime() - runtimeBean.getStartTime())/1000;
os = bean.getName()+" "+bean.getVersion();
if(HostInfo.isLinux()){
try {
availableMemory = (LinuxInfoUtil.getAvailableMemKB()*1024l);
} catch (Throwable ignored) {
}
}
File[] roots = File.listRoots();
for(File file:roots){
rootFiles.add(new RootFile(file.getAbsolutePath(),file.getTotalSpace(),file.getFreeSpace()));
}
getGCStatus();
}
示例7: VMSummary
import com.sun.management.OperatingSystemMXBean; //導入方法依賴的package包/類
public VMSummary(){
MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
MemoryUsage u = bean.getHeapMemoryUsage();
heapCommitedMemory= (u.getCommitted());
heapUsedMemory=(u.getUsed());
heapMaxMemory=(u.getMax());
u = bean.getNonHeapMemoryUsage();
nonHeapCommitedMemory=(u.getCommitted());
nonHeapUsedMemory=(u.getUsed());
nonHeapMaxMemory=(u.getMax());
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
currentThreadCount=threadBean.getThreadCount();
daemonThreadCount= threadBean.getDaemonThreadCount();
totalStartedThreadCount= threadBean.getTotalStartedThreadCount();
peakThreadCount = threadBean.getPeakThreadCount();
ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
loadedClassCount=classLoadingBean.getLoadedClassCount();
totalLoadedClassCount=classLoadingBean.getTotalLoadedClassCount();
unloadedClassCount=classLoadingBean.getUnloadedClassCount();
getGCStatus();
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
classPath = runtimeBean.getClassPath();
libraryPath = runtimeBean.getLibraryPath();
vmOptions = TextUtils.join(" ",runtimeBean.getInputArguments());
bootClassPath = runtimeBean.getBootClassPath();
upTime = runtimeBean.getUptime();
vmName=runtimeBean.getVmName();
vmVendor= runtimeBean.getVmVendor();
OperatingSystemMXBean osBean= (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
processCpuTime = osBean.getProcessCpuTime();
jdkVersion=System.getProperty("java.version");
jitCompiler=System.getProperty("java.vm.name");
os = osBean.getName() + " "+osBean.getVersion();
osArch = osBean.getArch();
availableProcessors = osBean.getAvailableProcessors();
commitedVirtualMemory = osBean.getCommittedVirtualMemorySize();
freePhysicalMemorySize =(osBean.getFreePhysicalMemorySize());
totalPhysicalMemorySize =(osBean.getTotalPhysicalMemorySize());
freeSwapSpaceSize =(osBean.getFreeSwapSpaceSize());
totalSwapSpaceSize =(osBean.getTotalSwapSpaceSize());
List<GarbageCollectorMXBean> beans = ManagementFactory.getGarbageCollectorMXBeans();
gcInfos = new ArrayList<>(beans.size());
for (GarbageCollectorMXBean b : beans) {
GCBean gcBean = new GCBean();
gcBean.name =b.getName();
gcBean.gcCount = b.getCollectionCount();
gcBean.gcTime = b.getCollectionTime();
gcInfos.add(gcBean);
}
}
示例8: getCPU
import com.sun.management.OperatingSystemMXBean; //導入方法依賴的package包/類
public static int getCPU() {
OperatingSystemMXBean os = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
return os.getAvailableProcessors();
}
示例9: run
import com.sun.management.OperatingSystemMXBean; //導入方法依賴的package包/類
@Override
public void run() {
try {
while (runLogLoop) {
try {
final EventLogManager eventLogManager = EventLogManager.getInstance();
// Code inspiré de http://knight76.blogspot.fr/2009/05/how-to-get-java-cpu-usage-jvm-instance.html et http://www.docjar.com/html/api/sun/tools/jconsole/SummaryTab$Result.java.html
final SystemEventLog systemEventLog = eventLogManager.addEventLog(SystemEventLog.class);
final OperatingSystemMXBean osMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
final long processCpuTime = osMXBean.getProcessCpuTime();
final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
final long uptime = runtimeMXBean.getUptime();
final int availableProcessors = osMXBean.getAvailableProcessors();
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
systemEventLog.setHeapMemoryUsed(memoryMXBean.getHeapMemoryUsage().getUsed());
systemEventLog.setNonHeapMemoryUsed(memoryMXBean.getNonHeapMemoryUsage().getUsed());
final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
systemEventLog.setThreadCount(threadMXBean.getThreadCount());
systemEventLog.setPeakThreadCount(threadMXBean.getPeakThreadCount());
// Compute the cpuUsage
if (prevUptime > 0L && uptime > prevUptime) {
// elapsedCpu is in ns and elapsedTime is in ms.
final long elapsedCpu = processCpuTime - prevProcessCpuTime;
final long elapsedTime = uptime - prevUptime;
// cpuUsage could go higher than 100% because elapsedTime
// and elapsedCpu are not fetched simultaneously. Limit to
// 99% to avoid Plotter showing a scale from 0% to 200%.
systemEventLog.setCpuUsage(Math.min(100F, elapsedCpu / (elapsedTime * 10000F * availableProcessors))); // elapsedCpu / (elapsedTime * 1000F * 1000F * availableProcessors)) * 100 => pour l'avoir en %
}
systemEventLog.setUptime(uptime);
systemEventLog.setProcessCpuTime(processCpuTime);
systemEventLog.setAvailableProcessors(availableProcessors);
// We now log all threads in RUNNABLE or BLOCKED state
final ThreadInfo[] allThreads = threadMXBean.dumpAllThreads(false, false);
final List<SystemEventLog.Thread> loggedThreads = new LinkedList<SystemEventLog.Thread>();
for (final ThreadInfo threadInfo : allThreads) {
if (threadInfo != null) { // It seems that sometime (with JRockit) threadInfo is null
final State threadState = threadInfo.getThreadState();
if ((threadState == State.BLOCKED || threadState == State.RUNNABLE) && PatternUtils.matches(threadInfo.getThreadName(), threadNameIncludes, threadNameExcludes)) {
final long threadId = threadInfo.getThreadId();
loggedThreads.add(new SystemEventLog.Thread(threadInfo, threadMXBean.getThreadUserTime(threadId), threadMXBean.getThreadCpuTime(threadId)));
}
}
}
systemEventLog.setBlockedOrRunningThreads(loggedThreads.toArray(new SystemEventLog.Thread[loggedThreads.size()]));
eventLogManager.fire(systemEventLog);
SystemEventLogger.this.prevUptime = uptime;
SystemEventLogger.this.prevProcessCpuTime = processCpuTime;
Thread.sleep(cpuComputationDeltaMillis);
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
}
} finally {
stop();
}
}
示例10: PerformanceMonitor
import com.sun.management.OperatingSystemMXBean; //導入方法依賴的package包/類
private PerformanceMonitor() {
osBean = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
availableProcessors = osBean.getAvailableProcessors();
}
示例11: PerformanceMonitor
import com.sun.management.OperatingSystemMXBean; //導入方法依賴的package包/類
public PerformanceMonitor()
{
ArrayList<MemoryPoolMXBean> hb = new ArrayList();
for (MemoryPoolMXBean p : ManagementFactory.getMemoryPoolMXBeans()) {
if (p.getType() == MemoryType.HEAP) {
hb.add(p);
}
}
hb.trimToSize();
hbean_ = hb;
num_mem_ = hbean_.size();
heap_start_ = new ArrayList(num_mem_);
heap_peak_ = new ArrayList(num_mem_);
heap_last_ = new ArrayList(num_mem_);
heap_committed_sum_ = 0;
heap_used_sum_ = 0;
for (int i=0 ; i<num_mem_ ; i++) {
MemoryPoolMXBean mb = hbean_.get(i);
mb.resetPeakUsage();
heap_peak_.add(mb.getPeakUsage());
MemoryUsage mu = mb.getUsage();
heap_start_.add(mu);
heap_last_.add(mu);
heap_committed_sum_ += mu.getCommitted()>>>MB_SHIFT;
heap_used_sum_ += mu.getUsed()>>>MB_SHIFT;
}
heap_cnt_ = 1;
gcbeans_ = ManagementFactory.getGarbageCollectorMXBeans();
num_gc_ = gcbeans_.size();
gc_time_ = new long[num_gc_];
gc_last_ = new long[num_gc_];
gc_cnt_ = new long[num_gc_];
for (int i=0 ; i<num_gc_ ; i++) {
GarbageCollectorMXBean gc = gcbeans_.get(i);
gc_cnt_[i] = gc.getCollectionCount();
gc_time_[i] = gc.getCollectionTime();
gc_last_[i] = gc_time_[i];
}
osbean_ = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
cpu_start_ = osbean_.getProcessCpuTime();
cpu_last_ = cpu_start_;
num_cpu_ = osbean_.getAvailableProcessors();
load_last_ = osbean_.getSystemLoadAverage();
swap_total_ = osbean_.getTotalSwapSpaceSize();
swap_last_ = osbean_.getFreeSwapSpaceSize();
thbean_ = (ThreadMXBean) ManagementFactory.getThreadMXBean();
th_daemon_ = thbean_.getDaemonThreadCount();
th_peak_ = thbean_.getPeakThreadCount();
th_count_ = thbean_.getThreadCount();
th_total_started_ = thbean_.getTotalStartedThreadCount();
time_start_ = System.currentTimeMillis();
time_last_ = time_start_;
time_prev_ = time_last_;
cpu_prev_ = cpu_last_;
instant_gc_ = 0.0;
instant_cpu_ = 0.0;
}