RAM size adaption works.

This commit is contained in:
Michael Lipp 2023-07-30 12:31:23 +02:00
parent 6be7239d49
commit 9fd2e282b1
4 changed files with 40 additions and 13 deletions

View file

@ -19,6 +19,7 @@
package org.jdrupes.vmoperator.runner.qemu;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@ -35,7 +36,6 @@ import java.util.regex.Pattern;
import org.jdrupes.vmoperator.util.Dto;
import org.jdrupes.vmoperator.util.FsdUtils;
// TODO: Auto-generated Javadoc
/**
* The configuration information from the configuration file.
*/
@ -50,7 +50,7 @@ class Configuration implements Dto {
@SuppressWarnings({ "PMD.FieldNamingConventions",
"PMD.VariableNamingConventions" })
private static final Pattern memorySize
= Pattern.compile("\\s*(\\d+)\\s*([^\\s]*)");
= Pattern.compile("^\\s*(\\d+(\\.\\d+)?)\\s*([A-Za-z]*)\\s*");
static {
// SI units and common abbreviations
@ -105,6 +105,7 @@ class Configuration implements Dto {
* @param amount the amount
* @return the big integer
*/
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public static BigInteger parseMemory(Object amount) {
if (amount == null) {
return (BigInteger) amount;
@ -119,12 +120,16 @@ class Configuration implements Dto {
if (!matcher.matches()) {
throw new NumberFormatException(amount.toString());
}
var unit = unitMap.get(matcher.group(2));
var unit = BigInteger.ONE;
if (matcher.group(3) != null) {
unit = unitMap.get(matcher.group(3));
if (unit == null) {
throw new NumberFormatException(amount.toString());
}
}
var number = matcher.group(1);
return new BigInteger(number).multiply(unit);
return new BigDecimal(number).multiply(new BigDecimal(unit))
.toBigInteger();
}
/**

View file

@ -220,7 +220,7 @@ public class QemuMonitor extends Component {
var response
= ((Runner) channel()).mapper().readValue(line, JsonNode.class);
if (response.has("QMP")) {
fire(new QemuMonitorOpened());
fire(new QemuMonitorAvailable());
}
} catch (JsonProcessingException e) {
throw new IOException(e);

View file

@ -18,12 +18,12 @@
package org.jdrupes.vmoperator.runner.qemu;
import org.jgrapes.io.events.Opened;
import org.jgrapes.core.Event;
/**
* Signals that the connection to the Qemu monitor socket has been
* established successfully.
*/
public class QemuMonitorOpened extends Opened<Void> {
public class QemuMonitorAvailable extends Event<Void> {
}

View file

@ -226,7 +226,9 @@ public class Runner extends Component {
event.structured(componentPath()).ifPresent(c -> {
if (event instanceof InitialConfiguration) {
processInitialConfiguration(c);
return;
}
updateConfiguration(c);
});
}
@ -323,6 +325,24 @@ public class Runner extends Component {
return mapper.readValue(out.toString(), JsonNode.class);
}
@SuppressWarnings("unchecked")
private void updateConfiguration(Map<String, Object> conf) {
Optional.ofNullable((Map<String, Object>) conf.get("vm"))
.map(vm -> vm.get("currentRam")).map(Configuration::parseMemory)
.ifPresent(cr -> {
if (config.vm.currentRam != null
&& config.vm.currentRam.equals(cr)) {
return;
}
synchronized (state) {
config.vm.currentRam = cr;
if (state.get() == State.RUNNING) {
qemuMonitor.setCurrentRam(cr);
}
}
});
}
/**
* Handle the start event.
*
@ -456,11 +476,13 @@ public class Runner extends Component {
* @param event the event
*/
@Handler
public void onQemuMonitorOpened(QemuMonitorOpened event) {
public void onQemuMonitorAvailable(QemuMonitorAvailable event) {
synchronized (state) {
Optional.ofNullable(config.vm.currentRam)
.ifPresent(qemuMonitor::setCurrentRam);
state.set(State.RUNNING);
}
}
/**
* On process exited.