package art.servers.controller;
|
|
import art.library.model.devices.application.ApplicationStatus;
|
import art.library.model.devices.application.status.ApplicationStatusMemory;
|
import art.library.model.devices.application.status.ApplicationStatusOperatingSystem;
|
import art.library.model.devices.application.status.ApplicationStatusRuntime;
|
import art.library.model.devices.application.status.ApplicationStatusThread;
|
import art.library.utils.windows.System32;
|
import com.sun.management.OperatingSystemMXBean;
|
import static java.lang.Thread.sleep;
|
import java.lang.management.ManagementFactory;
|
import java.lang.management.MemoryMXBean;
|
import java.lang.management.MemoryUsage;
|
import java.lang.management.RuntimeMXBean;
|
import java.lang.management.ThreadMXBean;
|
import java.util.Enumeration;
|
import java.util.Hashtable;
|
import art.servers.Shared;
|
|
|
public class ControllerProcessInformation extends Controller
|
{
|
public float cores = Runtime.getRuntime().availableProcessors();
|
public long systemTime = 0;
|
public long systemProcessesUsageTime = 0;
|
public long cpuLoadPercentage = 0;
|
public double processorPercentage = 0;
|
private String name = null;
|
private ApplicationStatus status = null;
|
|
public ControllerProcessInformation()
|
{
|
this.name = Shared.getMessage("Controller process information");
|
this.setName("Controller process information");
|
this.status = new ApplicationStatus();
|
start();
|
}
|
|
|
|
public ApplicationStatus getApplicationStatus()
|
{
|
return status;
|
}
|
|
|
public void run()
|
{
|
Shared.traceInformation(name, Shared.getMessage("Starting"));
|
|
// Update realtime
|
|
while ((isInterrupted() == false) && (exit == false))
|
{
|
long timestamp = System.currentTimeMillis();
|
|
try
|
{
|
if (Shared.isServerEnabled() == true)
|
{
|
update();
|
}
|
}
|
catch (Exception exception)
|
{
|
}
|
|
long timetowait = (1000 - (System.currentTimeMillis() - timestamp));
|
|
try
|
{
|
if (timetowait > 0)
|
{
|
sleep(timetowait);
|
}
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
Shared.traceInformation(name, Shared.getMessage("Finishing"));
|
}
|
|
|
|
|
|
|
public void update()
|
{
|
if ((System.getProperty("os.name").toLowerCase().contains("windows") == true))
|
{
|
windows();
|
}
|
|
|
long timestamp = System.currentTimeMillis();
|
memoryMXBean(status, timestamp);
|
threadMXBean(status, timestamp);
|
runtimeMXBean(status, timestamp);
|
operatingSystemMXBean(status, timestamp);
|
|
}
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="Windows">
|
|
private void windows()
|
{
|
try
|
{
|
Hashtable systemProcesses = (Hashtable)System32.listProcessesHashtable();
|
|
// CPU del sistema
|
|
long newsystemProcessesUsageTime = 0;
|
long newsystemTime = System.currentTimeMillis();
|
|
|
Enumeration enumeration = systemProcesses.elements();
|
|
while (enumeration.hasMoreElements())
|
{
|
art.library.utils.windows.SystemProcess systemProcess = (art.library.utils.windows.SystemProcess)enumeration.nextElement();
|
newsystemProcessesUsageTime = newsystemProcessesUsageTime + systemProcess.lpKernelTime + systemProcess.lpUserTime;
|
}
|
|
this.processorPercentage = (((float)(newsystemProcessesUsageTime - this.systemProcessesUsageTime) / (float)(newsystemTime - this.systemTime)) / this.cores) * 100.0f;
|
if (this.processorPercentage < 0) Shared.controllerProcessInformation.processorPercentage = 0;
|
|
this.systemTime = newsystemTime;
|
this.systemProcessesUsageTime = newsystemProcessesUsageTime;
|
|
}
|
catch (Exception exception)
|
{
|
}
|
|
}
|
|
// </editor-fold>
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="MX">
|
|
|
|
private void memoryMXBean(ApplicationStatus status, long timestamp)
|
{
|
try
|
{
|
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
|
if (status.memory == null) status.memory = new ApplicationStatusMemory();
|
status.memory.timestamp = timestamp;
|
|
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
|
MemoryUsage nonheapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
|
|
status.memory.heapMemoryUsage_init = heapMemoryUsage.getInit();
|
status.memory.heapMemoryUsage_max = heapMemoryUsage.getMax();
|
status.memory.heapMemoryUsage_used = heapMemoryUsage.getUsed();
|
status.memory.heapMemoryUsage_committed = heapMemoryUsage.getCommitted();
|
|
status.memory.nonheapMemoryUsage_init = nonheapMemoryUsage.getInit();
|
status.memory.nonheapMemoryUsage_max = nonheapMemoryUsage.getMax();
|
status.memory.nonheapMemoryUsage_used = nonheapMemoryUsage.getUsed();
|
status.memory.nonheapMemoryUsage_committed = nonheapMemoryUsage.getCommitted();
|
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
|
private void threadMXBean(ApplicationStatus status, long timestamp)
|
{
|
try
|
{
|
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
|
if (status.thread == null) status.thread = new ApplicationStatusThread();
|
status.thread.timestamp = timestamp;
|
|
long[] threadsid = threadMXBean.getAllThreadIds();
|
long threadCpuTime = 0;
|
long threadUserTime = 0;
|
|
for (long threadid : threadsid)
|
{
|
try
|
{
|
long currentThreadCpuTime = threadMXBean.getThreadCpuTime(threadid);
|
long currentThreadUserTime = threadMXBean.getThreadUserTime(threadid);
|
if (currentThreadCpuTime > 0) threadCpuTime = threadCpuTime + currentThreadCpuTime;
|
if (currentThreadUserTime > 0) threadUserTime = threadUserTime + currentThreadUserTime;
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
status.thread.threadCpuTime = threadCpuTime;
|
status.thread.threadUserTime = threadUserTime;
|
status.thread.totalStartedThreadCount = threadMXBean.getTotalStartedThreadCount();
|
status.thread.threadCount = threadMXBean.getThreadCount();
|
status.thread.peakThreadCount = threadMXBean.getPeakThreadCount();
|
status.thread.daemonThreadCount = threadMXBean.getDaemonThreadCount();
|
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
private void runtimeMXBean(ApplicationStatus status, long timestamp)
|
{
|
try
|
{
|
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
|
if (status.runtime == null) status.runtime = new ApplicationStatusRuntime();
|
status.runtime.timestamp = timestamp;
|
|
status.runtime.startTime = runtimeMXBean.getStartTime();
|
status.runtime.upTime = runtimeMXBean.getUptime();
|
status.runtime.userTimeZone = runtimeMXBean.getSystemProperties().get("Time zone");
|
status.runtime.javaVmSpecificationVersion = runtimeMXBean.getSystemProperties().get("java.vm.specification.version");
|
status.runtime.javaRuntimeVersion = runtimeMXBean.getSystemProperties().get("java.runtime.version");
|
status.runtime.javaClassVersion = runtimeMXBean.getSystemProperties().get("java.class.version");
|
status.runtime.userCountry = runtimeMXBean.getSystemProperties().get("user.country");
|
status.runtime.userLanguage = runtimeMXBean.getSystemProperties().get("user.language");
|
status.runtime.userTimeZone = runtimeMXBean.getSystemProperties().get("user.timezone");
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
|
private void operatingSystemMXBean(ApplicationStatus status, long timestamp)
|
{
|
try
|
{
|
OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
|
if (status.system == null) status.system = new ApplicationStatusOperatingSystem();
|
status.system.timestamp = timestamp;
|
status.system.committedVirtualMemorySize = operatingSystemMXBean.getCommittedVirtualMemorySize();
|
status.system.name = operatingSystemMXBean.getName();
|
status.system.version = operatingSystemMXBean.getVersion();
|
status.system.arch = operatingSystemMXBean.getArch();
|
status.system.freePhysicalMemorySize = operatingSystemMXBean.getFreePhysicalMemorySize();
|
status.system.freeSwapSpaceSize = operatingSystemMXBean.getFreeSwapSpaceSize();
|
status.system.processCpuLoad = operatingSystemMXBean.getProcessCpuLoad();
|
status.system.processCpuTime = operatingSystemMXBean.getProcessCpuTime();
|
status.system.systemCpuLoad = operatingSystemMXBean.getSystemCpuLoad();
|
status.system.totalPhysicalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize();
|
status.system.totalSwapSpaceSize = operatingSystemMXBean.getTotalSwapSpaceSize();
|
status.system.systemLoadAverage = operatingSystemMXBean.getSystemLoadAverage();
|
status.system.availableProcessors = operatingSystemMXBean.getAvailableProcessors();
|
status.system.totalPhysicalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize();
|
status.system.systemLoadAverage = this.processorPercentage;
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
|
// </editor-fold>
|
|
|
|
}
|