/** {@inheritDoc} */ @Override public long[][] getProcessorCpuLoadTicks() { long[][] ticks = new long[logicalProcessorCount][4]; int machPort = SystemB.INSTANCE.mach_host_self(); IntByReference procCount = new IntByReference(); PointerByReference procCpuLoadInfo = new PointerByReference(); IntByReference procInfoCount = new IntByReference(); if (0 != SystemB.INSTANCE.host_processor_info( machPort, SystemB.PROCESSOR_CPU_LOAD_INFO, procCount, procCpuLoadInfo, procInfoCount)) { LOG.error("Failed to update CPU Load. Error code: " + Native.getLastError()); return ticks; } int[] cpuTicks = procCpuLoadInfo.getValue().getIntArray(0, procInfoCount.getValue()); for (int cpu = 0; cpu < procCount.getValue(); cpu++) { for (int j = 0; j < 4; j++) { int offset = cpu * SystemB.CPU_STATE_MAX; ticks[cpu][0] = FormatUtil.getUnsignedInt(cpuTicks[offset + SystemB.CPU_STATE_USER]); ticks[cpu][1] = FormatUtil.getUnsignedInt(cpuTicks[offset + SystemB.CPU_STATE_NICE]); ticks[cpu][2] = FormatUtil.getUnsignedInt(cpuTicks[offset + SystemB.CPU_STATE_SYSTEM]); ticks[cpu][3] = FormatUtil.getUnsignedInt(cpuTicks[offset + SystemB.CPU_STATE_IDLE]); } } return ticks; }
/** Updates logical and physical processor counts from sysctl calls */ private void calculateProcessorCounts() { IntByReference size = new IntByReference(SystemB.INT_SIZE); Pointer p = new Memory(size.getValue()); // Get number of logical processors if (0 != SystemB.INSTANCE.sysctlbyname("hw.logicalcpu", p, size, null, 0)) { LOG.error("Failed to get number of logical CPUs. Error code: " + Native.getLastError()); this.logicalProcessorCount = 1; } else this.logicalProcessorCount = p.getInt(0); // Get number of physical processors if (0 != SystemB.INSTANCE.sysctlbyname("hw.physicalcpu", p, size, null, 0)) { LOG.error("Failed to get number of physical CPUs. Error code: " + Native.getLastError()); this.physicalProcessorCount = 1; } else this.physicalProcessorCount = p.getInt(0); }
/** {@inheritDoc} */ @Override public String getName() { if (this.cpuName == null) { IntByReference size = new IntByReference(); if (0 != SystemB.INSTANCE.sysctlbyname("machdep.cpu.brand_string", null, size, null, 0)) { LOG.error("Failed to get Name. Error code: " + Native.getLastError()); return ""; } Pointer p = new Memory(size.getValue() + 1); if (0 != SystemB.INSTANCE.sysctlbyname("machdep.cpu.brand_string", p, size, null, 0)) { LOG.error("Failed to get Name. Error code: " + Native.getLastError()); return ""; } this.cpuName = p.getString(0); } return this.cpuName; }
/** {@inheritDoc} */ @Override public long getSystemUptime() { IntByReference size = new IntByReference(); if (0 != SystemB.INSTANCE.sysctlbyname("kern.boottime", null, size, null, 0)) { LOG.error("Failed to get Boot Time. Error code: " + Native.getLastError()); return 0L; } // This should point to a 16-byte structure. If not, this code is valid if (size.getValue() != 16) throw new UnsupportedOperationException("sysctl kern.boottime should be 16 bytes but isn't."); Pointer p = new Memory(size.getValue() + 1); if (0 != SystemB.INSTANCE.sysctlbyname("kern.boottime", p, size, null, 0)) { LOG.error("Failed to get Boot Time. Error code: " + Native.getLastError()); return 0L; } // p now points to a 16-bit timeval structure for boot time. // First 8 bytes are seconds, second 8 bytes are microseconds (ignore) return System.currentTimeMillis() / 1000 - p.getLong(0); }
/** {@inheritDoc} */ @Override public long[] getSystemCpuLoadTicks() { long[] ticks = new long[curTicks.length]; int machPort = SystemB.INSTANCE.mach_host_self(); HostCpuLoadInfo cpuLoadInfo = new HostCpuLoadInfo(); if (0 != SystemB.INSTANCE.host_statistics( machPort, SystemB.HOST_CPU_LOAD_INFO, cpuLoadInfo, new IntByReference(cpuLoadInfo.size()))) { LOG.error("Failed to get System CPU ticks. Error code: " + Native.getLastError()); return ticks; } // Switch order to match linux ticks[0] = cpuLoadInfo.cpu_ticks[SystemB.CPU_STATE_USER]; ticks[1] = cpuLoadInfo.cpu_ticks[SystemB.CPU_STATE_NICE]; ticks[2] = cpuLoadInfo.cpu_ticks[SystemB.CPU_STATE_SYSTEM]; ticks[3] = cpuLoadInfo.cpu_ticks[SystemB.CPU_STATE_IDLE]; return ticks; }
/** {@inheritDoc} */ @Override public String getFamily() { if (this.cpuFamily == null) { IntByReference size = new IntByReference(SystemB.INT_SIZE); Pointer p = new Memory(size.getValue()); if (0 != SystemB.INSTANCE.sysctlbyname("machdep.cpu.family", p, size, null, 0)) { LOG.error("Failed to get Family. Error code: " + Native.getLastError()); return ""; } this.cpuFamily = Integer.toString(p.getInt(0)); } return this.cpuFamily; }
/** {@inheritDoc} */ @Override public boolean isCpu64bit() { if (this.cpu64 == null) { IntByReference size = new IntByReference(SystemB.INT_SIZE); Pointer p = new Memory(size.getValue()); if (0 != SystemB.INSTANCE.sysctlbyname("hw.cpu64bit_capable", p, size, null, 0)) { LOG.error("Failed to get 64Bit_capable. Error code: " + Native.getLastError()); return false; } this.cpu64 = p.getInt(0) != 0; } return this.cpu64.booleanValue(); }