OshiPlatformCache { public CentralProcessor getProcessor() { CentralProcessor ret; wLock.lock(); try { if (!sysInfoCache.containsKey(PlatformResourceType.PROCESSOR)) { HashMap<String, CentralProcessor> cache = new HashMap<>(1); CentralProcessor cp = sysInfo.getHardware().getProcessor(); cache.put(PlatformResourceType.PROCESSOR.getResourceTypeId().getIDString(), cp); sysInfoCache.put(PlatformResourceType.PROCESSOR, cache); } } finally { rLock.lock(); try { wLock.unlock(); ret = (CentralProcessor) sysInfoCache.get(PlatformResourceType.PROCESSOR) .get(PlatformResourceType.PROCESSOR.getResourceTypeId().getIDString()); } finally { rLock.unlock(); } } return ret; } OshiPlatformCache(String feedId, String machineId, String containerId); void refresh(); OperatingSystem getOperatingSystem(); @SuppressWarnings("unchecked") Map<String, OSFileStore> getFileStores(); GlobalMemory getMemory(); CentralProcessor getProcessor(); @SuppressWarnings("unchecked") Map<String, PowerSource> getPowerSources(); Double getPowerSourceMetric(String powerSourceName, ID metricToCollect); Double getProcessorMetric(String processorNumber, ID metricToCollect); Double getFileStoreMetric(String fileStoreNameName, ID metricToCollect); Double getMemoryMetric(ID metricToCollect); Double getOperatingSystemMetric(ID metricId); Double getMetric(PlatformResourceType type, String name, ID metricToCollect); String getMachineId(); String getContainerId();  }
@Test public void getProcessors() { OshiPlatformCache oshi = newOshiPlatformCache(); CentralProcessor processor = oshi.getProcessor(); Assert.assertNotNull(processor); Util.sleep(2000L); for (int i = 0; i < processor.getLogicalProcessorCount(); i++) { String name = processor.getName(); String family; try { family = processor.getFamily(); } catch (UnsupportedOperationException e) { family = ""; print(processor.getClass().getName() + ".getFamily() unsupported on " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch")); } String identifier = processor.getIdentifier(); String model; try { model = processor.getModel(); } catch (UnsupportedOperationException e) { model = ""; print(processor.getClass().getName() + ".getModel() unsupported on " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch")); } String stepping; try { stepping = processor.getStepping(); } catch (UnsupportedOperationException e) { stepping = ""; print(processor.getClass().getName() + ".getStepping() unsupported on " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch")); } String vendor = processor.getVendor(); double systemCpuLoad = processor.getSystemCpuLoad(); double systemLoadAverage = processor.getSystemLoadAverage(); int processorNumber = i; long systemUpTime = processor.getSystemUptime(); long vendorFrequency = processor.getVendorFreq(); boolean isCpu64bit; try { isCpu64bit = processor.isCpu64bit(); } catch (UnsupportedOperationException e) { print(processor.getClass().getName() + ".isCpu64bit() unsupported on " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch")); isCpu64bit = false; } long[] processorCpuLoadTicks = processor.getProcessorCpuLoadTicks()[i]; long[] systemCpuLoadTicks = processor.getSystemCpuLoadTicks(); processor.getProcessorCpuLoadBetweenTicks(); processor.getSystemCpuLoadBetweenTicks(); Util.sleep(1100L); double processorCpuLoadBetweenTicks = processor.getProcessorCpuLoadBetweenTicks()[i]; double systemCpuLoadBetweenTicks = processor.getSystemCpuLoadBetweenTicks(); Assert.assertNotNull(name); Assert.assertNotNull(family); Assert.assertNotNull(identifier); Assert.assertNotNull(model); Assert.assertNotNull(stepping); Assert.assertNotNull(vendor); print("===PROCESSOR #%d ===", i); print(" Name=[%s]", name); print(" Family=[%s]", family); print(" Identifier=[%s]", identifier); print(" Model=[%s]", model); print(" Stepping=[%s]", stepping); print(" Vendor=[%s]", vendor); print(" SystemCpuLoad=[%.2f]", systemCpuLoad); print(" SystemLoadAverage=[%.2f]", systemLoadAverage); print(" ProcessorNumber=[%d]", processorNumber); print(" SystemUpTime=[%s] (%d)", FormatUtil.formatElapsedSecs(systemUpTime), systemUpTime); print(" VendorFreq=[%s] (%d)", FormatUtil.formatHertz(vendorFrequency), vendorFrequency); print(" IsCpu64BitVendor=[%s]", isCpu64bit); print(" ProcessorCpuLoadTicks=[user=%d/nice=%d/system=%d/idle=%d]", processorCpuLoadTicks[0], processorCpuLoadTicks[1], processorCpuLoadTicks[2], processorCpuLoadTicks[3]); print(" SystemCpuLoadTicks=[user=%d/nice=%d/system=%d/idle=%d]", systemCpuLoadTicks[0], systemCpuLoadTicks[1], systemCpuLoadTicks[2], systemCpuLoadTicks[3]); print(" ProcessorCpuLoadBetweenTicks=[%.2f]", processorCpuLoadBetweenTicks); print(" SystemCpuLoadBetweenTicks=[%.2f]", systemCpuLoadBetweenTicks); print(" toString=[%s]", processor.toString()); } }