Search in sources :

Example 1 with CentralProcessor

use of oshi.hardware.CentralProcessor in project UniversalMediaServer by UniversalMediaServer.

the class SystemInformation method getSystemInfo.

/**
 * Collects and returns system information.
 *
 * @return A {@link List} of {@link String}s containing the collected system
 *         information.
 */
public static List<String> getSystemInfo() {
    List<String> result = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    long jvmMemory = Runtime.getRuntime().maxMemory();
    OperatingSystem os = null;
    CentralProcessor processor = null;
    GlobalMemory memory = null;
    try {
        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        os = systemInfo.getOperatingSystem();
        processor = hardware.getProcessor();
        memory = hardware.getMemory();
    } catch (Error e) {
        LOGGER.debug("Could not retrieve system information: {}", e.getMessage());
        LOGGER.trace("", e);
    }
    sb.append("JVM: ").append(System.getProperty("java.vm.name")).append(" ").append(System.getProperty("java.version")).append(" (").append(System.getProperty("sun.arch.data.model")).append("-bit) by ").append(System.getProperty("java.vendor"));
    result.add(sb.toString());
    sb.setLength(0);
    sb.append("OS: ");
    if (os != null && isNotBlank(os.toString())) {
        sb.append(os.toString()).append(" ").append(getOSBitness()).append("-bit");
    } else {
        sb.append(System.getProperty("os.name")).append(" ").append(getOSBitness()).append("-bit ");
        sb.append(System.getProperty("os.version"));
    }
    result.add(sb.toString());
    sb.setLength(0);
    if (processor != null) {
        sb.append("CPU: ").append(processor.getName()).append(" with ").append(processor.getPhysicalProcessorCount());
        if (processor.getPhysicalProcessorCount() > 1) {
            sb.append(" cores");
        } else {
            sb.append(" core");
        }
        if (processor.getLogicalProcessorCount() != processor.getPhysicalProcessorCount()) {
            sb.append(" (").append(processor.getLogicalProcessorCount());
            if (processor.getLogicalProcessorCount() > 1) {
                sb.append(" virtual cores)");
            } else {
                sb.append(" virtual core)");
            }
        }
        result.add(sb.toString());
        sb.setLength(0);
    }
    if (memory != null) {
        sb.append("Physical Memory: ").append(StringUtil.formatBytes(memory.getTotal(), true));
        result.add(sb.toString());
        sb.setLength(0);
        sb.append("Free Memory: ").append(StringUtil.formatBytes(memory.getAvailable(), true));
        result.add(sb.toString());
        sb.setLength(0);
    }
    sb.append("Maximum JVM Memory: ");
    if (jvmMemory == Long.MAX_VALUE) {
        sb.append("Unlimited");
    } else {
        sb.append(StringUtil.formatBytes(jvmMemory, true));
    }
    result.add(sb.toString());
    return result;
}
Also used : OperatingSystem(oshi.software.os.OperatingSystem) HardwareAbstractionLayer(oshi.hardware.HardwareAbstractionLayer) GlobalMemory(oshi.hardware.GlobalMemory) SystemInfo(oshi.SystemInfo) CentralProcessor(oshi.hardware.CentralProcessor) ArrayList(java.util.ArrayList)

Example 2 with CentralProcessor

use of oshi.hardware.CentralProcessor in project graylog2-server by Graylog2.

the class OshiOsProbe method osStats.

@Override
public OsStats osStats() {
    final HardwareAbstractionLayer hardware = service.getHal();
    final GlobalMemory globalMemory = hardware.getMemory();
    final Memory mem = Memory.create(globalMemory.getTotal(), globalMemory.getAvailable(), (short) (globalMemory.getAvailable() * 100 / globalMemory.getTotal()), globalMemory.getTotal() - globalMemory.getAvailable(), (short) ((globalMemory.getTotal() - globalMemory.getAvailable()) * 100 / globalMemory.getTotal()), globalMemory.getAvailable(), globalMemory.getTotal() - globalMemory.getAvailable());
    final VirtualMemory virtualMemory = globalMemory.getVirtualMemory();
    final Swap swap = Swap.create(virtualMemory.getSwapTotal(), virtualMemory.getSwapTotal() - virtualMemory.getSwapUsed(), virtualMemory.getSwapUsed());
    final CentralProcessor centralProcessor = hardware.getProcessor();
    long[] prevTicks = centralProcessor.getSystemCpuLoadTicks();
    long[] ticks = centralProcessor.getSystemCpuLoadTicks();
    short user = (short) (ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()]);
    short sys = (short) (ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()]);
    short idle = (short) (ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()]);
    short steal = (short) (ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()]);
    final CentralProcessor.ProcessorIdentifier processorIdentifier = centralProcessor.getProcessorIdentifier();
    final Processor proc = Processor.create(processorIdentifier.getName(), processorIdentifier.getVendor(), ((int) processorIdentifier.getVendorFreq() / 1000000), centralProcessor.getLogicalProcessorCount(), centralProcessor.getPhysicalPackageCount(), centralProcessor.getLogicalProcessorCount() / centralProcessor.getPhysicalPackageCount(), -1, sys, user, idle, steal);
    return OsStats.create(centralProcessor.getSystemLoadAverage(3), service.getOs().getSystemUptime(), proc, mem, swap);
}
Also used : HardwareAbstractionLayer(oshi.hardware.HardwareAbstractionLayer) GlobalMemory(oshi.hardware.GlobalMemory) CentralProcessor(oshi.hardware.CentralProcessor) GlobalMemory(oshi.hardware.GlobalMemory) VirtualMemory(oshi.hardware.VirtualMemory) CentralProcessor(oshi.hardware.CentralProcessor) VirtualMemory(oshi.hardware.VirtualMemory)

Example 3 with CentralProcessor

use of oshi.hardware.CentralProcessor in project yyl_example by Relucent.

the class OshiExample method infoIdentifier.

public static void infoIdentifier(SystemInfo si) {
    OperatingSystem operatingSystem = si.getOperatingSystem();
    HardwareAbstractionLayer hardwareAbstractionLayer = si.getHardware();
    CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();
    ComputerSystem computerSystem = hardwareAbstractionLayer.getComputerSystem();
    String vendor = operatingSystem.getManufacturer();
    String processorSerialNumber = computerSystem.getSerialNumber();
    String uuid = computerSystem.getHardwareUUID();
    String processorIdentifier = centralProcessor.getProcessorIdentifier().getIdentifier();
    int processors = centralProcessor.getLogicalProcessorCount();
    println(// 
    String.format("%08x", vendor.hashCode()) + "-" + String.format("%08x", processorSerialNumber.hashCode()) + // 
    "-" + String.format("%08x", uuid.hashCode()) + // 
    "-" + String.format("%08x", processorIdentifier.hashCode()) + // 
    "-" + processors);
}
Also used : OperatingSystem(oshi.software.os.OperatingSystem) HardwareAbstractionLayer(oshi.hardware.HardwareAbstractionLayer) ComputerSystem(oshi.hardware.ComputerSystem) CentralProcessor(oshi.hardware.CentralProcessor)

Example 4 with CentralProcessor

use of oshi.hardware.CentralProcessor in project yyl_example by Relucent.

the class OshiExample method infoCpu.

private static void infoCpu(SystemInfo si) {
    println("CPU");
    CentralProcessor cpu = si.getHardware().getProcessor();
    int cores = cpu.getLogicalProcessorCount();
    // 系统范围的CPU负载滴答计数器
    long[] previousTicks = cpu.getSystemCpuLoadTicks();
    // 等待一定时间
    sleep(1000L);
    long[] currentTicks = cpu.getSystemCpuLoadTicks();
    // 获取一段时间内的CPU负载标记差
    long user = getTickDifference(previousTicks, currentTicks, TickType.USER);
    long nice = getTickDifference(previousTicks, currentTicks, TickType.NICE);
    long system = getTickDifference(previousTicks, currentTicks, TickType.SYSTEM);
    long idle = getTickDifference(previousTicks, currentTicks, TickType.IDLE);
    long iowait = getTickDifference(previousTicks, currentTicks, TickType.IOWAIT);
    long irq = getTickDifference(previousTicks, currentTicks, TickType.IRQ);
    long softIrq = getTickDifference(previousTicks, currentTicks, TickType.SOFTIRQ);
    long steal = getTickDifference(previousTicks, currentTicks, TickType.STEAL);
    long total = Math.max(user + nice + system + idle + iowait + irq + softIrq + steal, 0);
    // CPU信息
    println(cpu.toString());
    // 核心数
    println("   Cores:   " + cores);
    // 系统使用率
    println("   Sys:     " + percentage(system, total));
    // 用户使用率
    println("   Used:    " + percentage(user, total));
    // 当前等待率
    println("   Wait:    " + percentage(iowait, total));
    // 当前空闲率
    println("   Free:    " + percentage(idle, total));
}
Also used : CentralProcessor(oshi.hardware.CentralProcessor)

Aggregations

CentralProcessor (oshi.hardware.CentralProcessor)4 HardwareAbstractionLayer (oshi.hardware.HardwareAbstractionLayer)3 GlobalMemory (oshi.hardware.GlobalMemory)2 OperatingSystem (oshi.software.os.OperatingSystem)2 ArrayList (java.util.ArrayList)1 SystemInfo (oshi.SystemInfo)1 ComputerSystem (oshi.hardware.ComputerSystem)1 VirtualMemory (oshi.hardware.VirtualMemory)1