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

View file

@ -18,12 +18,12 @@
package org.jdrupes.vmoperator.runner.qemu; 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 * Signals that the connection to the Qemu monitor socket has been
* established successfully. * 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 -> { event.structured(componentPath()).ifPresent(c -> {
if (event instanceof InitialConfiguration) { if (event instanceof InitialConfiguration) {
processInitialConfiguration(c); processInitialConfiguration(c);
return;
} }
updateConfiguration(c);
}); });
} }
@ -323,6 +325,24 @@ public class Runner extends Component {
return mapper.readValue(out.toString(), JsonNode.class); 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. * Handle the start event.
* *
@ -456,10 +476,12 @@ public class Runner extends Component {
* @param event the event * @param event the event
*/ */
@Handler @Handler
public void onQemuMonitorOpened(QemuMonitorOpened event) { public void onQemuMonitorAvailable(QemuMonitorAvailable event) {
Optional.ofNullable(config.vm.currentRam) synchronized (state) {
.ifPresent(qemuMonitor::setCurrentRam); Optional.ofNullable(config.vm.currentRam)
state.set(State.RUNNING); .ifPresent(qemuMonitor::setCurrentRam);
state.set(State.RUNNING);
}
} }
/** /**