fix(runtime): adjust memory resources inside a transaction

This commit is contained in:
Alex Zenla
2024-08-14 23:45:23 -07:00
parent 506d2ccf46
commit 4d183f02ca
3 changed files with 33 additions and 30 deletions

View File

@ -6,6 +6,7 @@ use tokio::sync::Semaphore;
use uuid::Uuid;
use xenclient::XenClient;
use xenplatform::domain::XEN_EXTRA_MEMORY_KB;
use xenstore::{XsdClient, XsdInterface};
use self::{
@ -226,33 +227,34 @@ impl Runtime {
Ok(uuid)
}
pub async fn set_max_memory(&self, domid: u32, max_memory_bytes: u64) -> Result<()> {
pub async fn set_memory_resources(
&self,
domid: u32,
target_memory_bytes: u64,
max_memory_bytes: u64,
) -> Result<()> {
let mut max_memory_bytes = max_memory_bytes + (XEN_EXTRA_MEMORY_KB * 1024);
if target_memory_bytes > max_memory_bytes {
max_memory_bytes = target_memory_bytes + (XEN_EXTRA_MEMORY_KB * 1024);
}
self.context
.xen
.call
.set_max_mem(domid, max_memory_bytes / 1024)
.await?;
let domain_path = self.context.xen.store.get_domain_path(domid).await?;
let tx = self.context.xen.store.transaction().await?;
let max_memory_path = format!("{}/memory/static-max", domain_path);
self.context
.xen
.store
.write_string(max_memory_path, &(max_memory_bytes / 1024).to_string())
tx.write_string(max_memory_path, &(max_memory_bytes / 1024).to_string())
.await?;
Ok(())
}
pub async fn set_target_memory(&self, domid: u32, target_memory_bytes: u64) -> Result<()> {
let domain_path = self.context.xen.store.get_domain_path(domid).await?;
let target_memory_path = format!("{}/memory/target", domain_path);
self.context
.xen
.store
.write_string(
target_memory_path,
&(target_memory_bytes / 1024).to_string(),
)
.await?;
tx.write_string(
target_memory_path,
&(target_memory_bytes / 1024).to_string(),
)
.await?;
tx.commit().await?;
Ok(())
}