Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ public CpuStat getCpuStat() {
}

public MemStat getMemStat() {
return this.memStat;
final long dom0MinMem = getHostReservedMemMb();
return new MemStat(dom0MinMem);
}

public VirtualRoutingResource getVirtRouterResource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@ public Answer execute(final GetHostStatsCommand command, final LibvirtComputingR
final MemStat memStat = libvirtComputingResource.getMemStat();

final double cpuUtil = cpuStat.getCpuUsedPercent();
memStat.refresh();
final double totMem = memStat.getTotal();
final double freeMem = memStat.getAvailable();

final Pair<Double, Double> nicStats = libvirtComputingResource.getNicStats(
libvirtComputingResource.getPublicBridgeName());

final HostStatsEntry hostStats = new HostStatsEntry(command.getHostId(), cpuUtil, nicStats.first() / 1024,
nicStats.second() / 1024, "host", totMem, freeMem, 0, 0);
nicStats.second() / 1024, "host", memStat.getTotal() /1024, memStat.getAvailable() /1024, 0, 0);
return new GetHostStatsAnswer(command, hostStats);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,8 @@ public void testGetHostStatsCommand() {
when(this.libvirtComputingResource.getMemStat()).thenReturn(memStat);
when(this.libvirtComputingResource.getNicStats(Matchers.anyString())).thenReturn(new Pair<>(1.0d, 1.0d));
when(cpuStat.getCpuUsedPercent()).thenReturn(0.5d);
when(memStat.getAvailable()).thenReturn(1500.5d);
when(memStat.getTotal()).thenReturn(15000d);
when(memStat.getAvailable()).thenReturn(1500L);
when(memStat.getTotal()).thenReturn(15000L);

final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
Expand Down
23 changes: 15 additions & 8 deletions cosmic-common/src/main/java/com/cloud/utils/linux/MemStat.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,32 @@ public class MemStat {
protected static final String FREE_KEY = "MemFree";
protected static final String CACHE_KEY = "Cached";
protected static final String TOTAL_KEY = "MemTotal";
long reservedMemory;

private final Map<String, Double> memStats = new HashMap<>();
private final Map<String, Long> memStats = new HashMap<>();

public MemStat() {
this(0);
}

public Double getTotal() {
return memStats.get(TOTAL_KEY);
public MemStat(long reservedMemory) {
this.reservedMemory = reservedMemory;
this.refresh();
}

public Double getAvailable() {
public Long getTotal() {
return memStats.get(TOTAL_KEY) - reservedMemory;
}

public Long getAvailable() {
return getFree() + getCache();
}

public Double getFree() {
return memStats.get(FREE_KEY);
public Long getFree() {
return memStats.get(FREE_KEY) - reservedMemory;
}

public Double getCache() {
public Long getCache() {
return memStats.get(CACHE_KEY);
}

Expand All @@ -48,7 +55,7 @@ protected void parseFromScanner(final Scanner scanner) {
while (scanner.hasNext()) {
final String[] stats = scanner.next().split("\\:\\s+");
if (stats.length == 2) {
memStats.put(stats[0], Double.valueOf(stats[1].replaceAll("\\s+\\w+", "")));
memStats.put(stats[0], Long.valueOf(stats[1].replaceAll("\\s+\\w+", "")) * 1024L);
}
}
}
Expand Down
50 changes: 35 additions & 15 deletions cosmic-common/src/test/java/com/cloud/utils/linux/MemStatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,57 @@

import java.util.Scanner;

import org.junit.Assert;
import org.junit.Test;
import org.junit.Assert;
import org.junit.Test;

import java.util.Scanner;

public class MemStatTest {
final String memInfo = "MemTotal: 5830236 kB\n" +
"MemFree: 156752 kB\n" +
"Buffers: 326836 kB\n" +
"Cached: 2606764 kB\n" +
"SwapCached: 0 kB\n" +
"Active: 4260808 kB\n" +
"Inactive: 949392 kB\n";

@Test
public void getMemInfoParseTest() {
final String memInfo = "MemTotal: 5830236 kB\n" +
"MemFree: 156752 kB\n" +
"Buffers: 326836 kB\n" +
"Cached: 2606764 kB\n" +
"SwapCached: 0 kB\n" +
"Active: 4260808 kB\n" +
"Inactive: 949392 kB\n";

MemStat memStat = null;
try {
memStat = new MemStat();
} catch (final RuntimeException ex) {
} catch (RuntimeException ex) {
// If test isn't run on linux we'll fail creation of linux-specific MemStat class due
// to dependency on /proc/meminfo if we don't catch here.
// We are really only interested in testing the parsing algorithm and getters.
if (memStat == null) {
throw ex;
}
}
final Scanner scanner = new Scanner(memInfo);
Scanner scanner = new Scanner(memInfo);
memStat.parseFromScanner(scanner);

Assert.assertEquals(memStat.getTotal(), Long.valueOf(5970161664L));
Assert.assertEquals(memStat.getAvailable(), Long.valueOf(2829840384L));
Assert.assertEquals(memStat.getFree(), Long.valueOf(160514048L));
Assert.assertEquals(memStat.getCache(), Long.valueOf(2669326336L));
}

@Test
public void reservedMemoryTest() {
MemStat memStat = null;
try {
memStat = new MemStat(1024);
} catch (RuntimeException ex) {
if (memStat == null) {
throw ex;
}
}
Scanner scanner = new Scanner(memInfo);
memStat.parseFromScanner(scanner);

Assert.assertEquals(memStat.getTotal(), Double.valueOf(5830236));
Assert.assertEquals(memStat.getAvailable(), Double.valueOf(2763516));
Assert.assertEquals(memStat.getFree(), Double.valueOf(156752));
Assert.assertEquals(memStat.getCache(), Double.valueOf(2606764));
Assert.assertEquals(memStat.getTotal(), Long.valueOf(5970160640L));
}
}
}