Skip to content

Commit

Permalink
complete update_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Youtirsin committed Nov 2, 2023
1 parent 51a5dd9 commit 469375e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
6 changes: 3 additions & 3 deletions wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use oci_spec::runtime::Spec;
use oci_spec::runtime::{Spec, LinuxResources};

#[cfg(feature = "wasmedge")]
pub fn get_preopens(spec: &Spec) -> Vec<String> {
Expand Down Expand Up @@ -105,3 +105,10 @@ pub(crate) fn get_cgroup_path(spec: &Spec) -> Option<String> {
.and_then(|linux| linux.cgroups_path().as_ref())
.map(|path| path.display().to_string())
}

#[cfg(feature = "wasmedge")]
pub(crate) fn get_linux_resources(spec: &Spec) -> Option<&LinuxResources> {
spec.linux()
.as_ref()
.and_then(|linux| linux.resources().as_ref())
}
26 changes: 18 additions & 8 deletions wasm/src/wasmedge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use containerd_shim::{
task::TaskService,
util::{mkdir, mount_rootfs, read_spec},
},
cgroup::update_resources,
error::Error,
io::Stdio,
monitor::{Subject, Topic},
Expand All @@ -44,7 +45,9 @@ use wasmedge_sdk::{
PluginManager, Vm,
};

use crate::utils::{get_args, get_cgroup_path, get_envs, get_preopens, get_rootfs};
use crate::utils::{
get_args, get_cgroup_path, get_envs, get_linux_resources, get_preopens, get_rootfs,
};

pub type ExecProcess = ProcessTemplate<WasmEdgeExecLifecycle>;
pub type InitProcess = ProcessTemplate<WasmEdgeInitLifecycle>;
Expand Down Expand Up @@ -339,29 +342,36 @@ fn run_wasi_as_child(args: Vec<String>, spec: &Spec, p: &InitProcess) -> Result<
child.stderr(stderr);
}

let pid = child
let mut child = child
.arg("--netns")
.arg(netns)
.arg("--mod-path")
.arg(mod_path)
.spawn()
.map_err(|e| RunError::IO(e))?
.id();
.map_err(|e| RunError::IO(e))?;

if let Some(cgroup_path) = get_cgroup_path(spec) {
// Add child process to Cgroup
if let Err(_) = Cgroup::new(
if let Err(err) = Cgroup::new(
cgroups_rs::hierarchies::auto(),
cgroup_path.trim_start_matches('/'),
)
.and_then(|cgroup| cgroup.add_task_by_tgid(CgroupPid::from(pid as u64)))
.and_then(|cgroup| cgroup.add_task_by_tgid(CgroupPid::from(child.id() as u64)))
{
println!("failed to add to cgroup");
debug!("failed to add to cgroup: {err}");
let _ = child.kill();
return Err(RunError::Cgroup);
}
if let Some(resources) = get_linux_resources(spec) {
update_resources(child.id(), resources).map_err(|err| {
debug!("failed to set linux resources: {err}");
let _ = child.kill();
RunError::Cgroup
})?;
}
}

Ok(pid)
Ok(child.id())
}

// any wasm runtime implementation should implement this function
Expand Down

0 comments on commit 469375e

Please sign in to comment.