forked from External/mage
Added displaying Mem usage in Mage.Client
This commit is contained in:
parent
176a77ff44
commit
ee030c7b06
4 changed files with 135 additions and 73 deletions
|
|
@ -0,0 +1,25 @@
|
|||
package mage.client.util.stats;
|
||||
|
||||
/**
|
||||
* @author noxx
|
||||
*/
|
||||
public class MemoryUsageStatUtil {
|
||||
|
||||
private MemoryUsageStatUtil() {}
|
||||
|
||||
/**
|
||||
* Returns percentage of available memory used at runtime.
|
||||
* If not possible to determine, returns -1.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static float getMemoryFreeStatPercentage() {
|
||||
Runtime runtime = Runtime.getRuntime();
|
||||
if (runtime.maxMemory() != 0) {
|
||||
long usedMem = runtime.totalMemory() - runtime.freeMemory();
|
||||
return (1 - (1.0f*usedMem)/runtime.maxMemory())*100;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
package mage.client.util.stats;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* This updates the mem usage info in the Mage client every MEM_USAGE_UPDATE_TIME ms.
|
||||
*
|
||||
* @author noxx
|
||||
*/
|
||||
public class UpdateMemUsageTask extends SwingWorker<Void, Float> {
|
||||
|
||||
private static final int MEM_USAGE_UPDATE_TIME = 2000;
|
||||
|
||||
private JLabel jLabelToDisplayInfo;
|
||||
|
||||
private static final Logger logger = Logger.getLogger(UpdateMemUsageTask.class);
|
||||
|
||||
public UpdateMemUsageTask(JLabel jLabelToDisplayInfo) {
|
||||
this.jLabelToDisplayInfo = jLabelToDisplayInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground() throws Exception {
|
||||
while (!isCancelled()) {
|
||||
float memUsage = MemoryUsageStatUtil.getMemoryFreeStatPercentage();
|
||||
this.publish(memUsage >= 0 ? Float.valueOf(memUsage) : null);
|
||||
Thread.sleep(MEM_USAGE_UPDATE_TIME);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void process(List<Float> chunks) {
|
||||
if (chunks != null && chunks.size() > 0) {
|
||||
Float memUsage = chunks.get(chunks.size() - 1);
|
||||
if (memUsage != null) {
|
||||
jLabelToDisplayInfo.setText(Math.round(memUsage) + "% Mem free");
|
||||
return;
|
||||
}
|
||||
}
|
||||
jLabelToDisplayInfo.setText("Mem usage unknown");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void done() {
|
||||
try {
|
||||
get();
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
logger.fatal("Update Memory Usage error", ex);
|
||||
} catch (CancellationException ex) {}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue