Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add disk interface #10

Merged
merged 2 commits into from
Apr 17, 2024
Merged
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
7 changes: 7 additions & 0 deletions Cargo.lock

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

73 changes: 73 additions & 0 deletions crates/op/host-op-system/src/disk/host.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2023-2024 Optimatist Technology Co., Ltd. All rights reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
//
// This file is part of PSH.
//
// PSH is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// PSH is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Perf-event-rs. If not,
// see <https://www.gnu.org/licenses/>.

use crate::{
profiling::system::disk::{
self, DiskOperationStat as GuestDiskOperationStat, DiskStat as GuestDiskStat,
},
SysCtx,
};

impl From<&procfs::DiskStat> for GuestDiskStat {
fn from(value: &procfs::DiskStat) -> Self {
let read = GuestDiskOperationStat {
operations: value.reads,
sectors: value.sectors_read,
merged: value.merged,
time: value.time_reading,
};
let write = GuestDiskOperationStat {
operations: value.writes,
sectors: value.sectors_written,
merged: value.writes_merged,
time: value.time_writing,
};
let discard = value
.discards
.zip(value.sectors_discarded)
.zip(value.discards_merged)
.zip(value.time_discarding)
.map(
|(((operations, sectors), merged), time)| GuestDiskOperationStat {
operations,
sectors,
merged,
time,
},
);
Self {
name: value.name.clone(),
major: value.major,
minor: value.minor,
read,
write,
discard,
in_progress: value.in_progress,
time_in_progress: value.time_in_progress,
weighted_time_in_progress: value.weighted_time_in_progress,
flushes: value.flushes,
time_flushing: value.time_flushing,
}
}
}

impl disk::Host for SysCtx {
fn stat(&mut self) -> wasmtime::Result<Result<Vec<GuestDiskStat>, String>> {
let disks = match procfs::diskstats() {
Ok(disks) => Ok(disks.iter().map(Into::into).collect()),
Err(err) => Err(err.to_string()),
};
Ok(disks)
}
}
15 changes: 15 additions & 0 deletions crates/op/host-op-system/src/disk/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2023-2024 Optimatist Technology Co., Ltd. All rights reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
//
// This file is part of PSH.
//
// PSH is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// PSH is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Perf-event-rs. If not,
// see <https://www.gnu.org/licenses/>.

mod host;
1 change: 1 addition & 0 deletions crates/op/host-op-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// see <https://www.gnu.org/licenses/>.

mod cpu;
mod disk;
mod interrupt;
mod memory;
mod network;
Expand Down
14 changes: 14 additions & 0 deletions examples/wasi/get_disks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "get_disks"
version = "0.1.0"
edition = "2021"

[package.metadata.component]
package = "component:get-disks"

[package.metadata.component.dependencies]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wit-bindgen = { version = "0.20.0", default-features = false, features = ["realloc"] }
32 changes: 32 additions & 0 deletions examples/wasi/get_disks/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2023-2024 Optimatist Technology Co., Ltd. All rights reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
//
// This file is part of PSH.
//
// PSH is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// PSH is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Perf-event-rs. If not,
// see <https://www.gnu.org/licenses/>.
use std::fs;
use std::ops::Not;
use std::process::Command;

fn main() {
let _ = fs::remove_file("src/bindings.rs");
let mut cmd = Command::new("wit-bindgen");
cmd.args(["rust", "--stubs", "--out-dir", "src/", "../../../wit/"]);

let output = cmd
.output()
.unwrap_or_else(|it| panic!("Failed to generate bindings: \n{}", it));
if output.stderr.is_empty().not() {
panic!(
"Failed to generate bindings: \n{}",
String::from_utf8(output.stderr).unwrap()
);
}
}
90 changes: 90 additions & 0 deletions examples/wasi/get_disks/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2023-2024 Optimatist Technology Co., Ltd. All rights reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
//
// This file is part of PSH.
//
// PSH is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// PSH is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Perf-event-rs. If not,
// see <https://www.gnu.org/licenses/>.

#[rustfmt::skip]
mod bindings;

use std::{collections::HashMap, error::Error};

use bindings::profiling::system::disk::{self, DiskStat};

pub struct Usage {
bps: f64,
ops: f64,
}

pub struct DiskUsage {
dev: String,
read: Usage,
write: Usage,
}

fn usage(pre: &DiskStat, post: &DiskStat, dur: std::time::Duration) -> DiskUsage {
let ms = dur.as_millis() as f64;
let read_bps = (post.read.sectors - pre.read.sectors) as f64 * 512.0 * 1000.0 / ms;
let write_bps = (post.write.sectors - pre.write.sectors) as f64 * 512.0 * 1000.0 / ms;
let rps = (post.read.operations - pre.read.operations) as f64 * 1000.0 / ms;
let wps = (post.write.operations - pre.write.operations) as f64 * 1000.0 / ms;
DiskUsage {
dev: pre.name.clone(),
read: Usage {
bps: read_bps,
ops: rps,
},
write: Usage {
bps: write_bps,
ops: wps,
},
}
}

fn differential(pre: &Vec<DiskStat>, post: &Vec<DiskStat>, dur: std::time::Duration) {
let pre: HashMap<_, _> = pre.iter().map(|d| (d.name.clone(), d)).collect();
let post: HashMap<_, _> = post.iter().map(|d| (d.name.clone(), d)).collect();
let mut usages: Vec<_> = pre
.iter()
.filter_map(|(name, &pre_stat)| match post.get(name) {
Some(&post_stat) => Some(usage(pre_stat, post_stat, dur)),
None => None,
})
.collect();

usages.sort_unstable_by(|lhs, rhs| rhs.write.bps.total_cmp(&lhs.write.bps));

for du in usages {
println!(
"{:16}: Read {:8.2} KiB/s {:6.2} O/s, Write: {:8.2} KiB/s {:6.2} O/s",
du.dev,
du.read.bps / 1024.0,
du.read.ops,
du.write.bps / 1024.0,
du.write.ops
);
}
println!();
}

fn main() -> Result<(), Box<dyn Error>> {
let mut pre_now = std::time::Instant::now();
let mut pre = disk::stat()?;
let dur = std::time::Duration::from_secs(1);
loop {
std::thread::sleep(dur);
let post_now = std::time::Instant::now();
let post = disk::stat()?;
differential(&pre, &post, post_now - pre_now);
pre = post;
pre_now = post_now;
}
}
2 changes: 1 addition & 1 deletion src/psh-sdk-wit
Loading