Skip to content

Commit

Permalink
test: add unit test for per block device metrics
Browse files Browse the repository at this point in the history
Add unit test for block inc metrics and shared metrics.

Signed-off-by: Sudan Landge <sudanl@amazon.com>
  • Loading branch information
Sudan Landge committed Oct 27, 2023
1 parent 333f88e commit f2209b5
Showing 1 changed file with 178 additions and 0 deletions.
178 changes: 178 additions & 0 deletions src/vmm/src/devices/virtio/block/block_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,181 @@ impl BlockDeviceMetrics {
.add(other.io_engine_throttled_events.fetch_diff());
}
}

#[cfg(test)]
pub mod tests {
use super::*;

#[test]
fn test_max_block_dev_metrics() {
// Note: this test has nothing to do with
// block structure or IRQs, this is just to allocate
// metrics for max number of devices that system can have.
// we have 5-23 irq for block devices on x86_64 so max
// 19 block devices and, although on aarch64 we have more
// devices but we stick to 19 to keep test common.
const MAX_BLOCK_DEVICES: usize = 19;

assert!(BLOCK_METRICS.read().is_ok());
assert!(BLOCK_METRICS.write().is_ok());

for i in 0..MAX_BLOCK_DEVICES {
let devn: String = format!("drv{}", i);
BlockMetricsPerDevice::alloc(devn.clone());
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(&devn)
.unwrap()
.activate_fails
.inc();
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(&devn)
.unwrap()
.read_bytes
.add(10);
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(&devn)
.unwrap()
.write_bytes
.add(5);
}

for i in 0..MAX_BLOCK_DEVICES {
let devn: String = format!("drv{}", i);
assert!(
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(&devn)
.unwrap()
.activate_fails
.count()
>= 1
);
assert!(
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(&devn)
.unwrap()
.read_bytes
.count()
>= 10
);
assert_eq!(
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(&devn)
.unwrap()
.write_bytes
.count(),
5
);
}
}

#[test]
fn test_signle_block_dev_metrics() {
// Use drv0 so that we can check thread safety with the
// `test_max_block_dev_metrics` which also uses the same name.
let devn = "drv0";

assert!(BLOCK_METRICS.read().is_ok());
assert!(BLOCK_METRICS.write().is_ok());

BlockMetricsPerDevice::alloc(String::from(devn));
assert!(BLOCK_METRICS.read().is_ok());
assert!(BLOCK_METRICS.read().unwrap().metrics.get(devn).is_some());

BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(devn)
.unwrap()
.activate_fails
.inc();
assert!(
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(devn)
.unwrap()
.activate_fails
.count()
> 0,
"{}",
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(devn)
.unwrap()
.activate_fails
.count()
);
// we expect only 2 tests (this and test_max_block_dev_metrics)
// to update activate_fails count for drv0.
assert!(
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(devn)
.unwrap()
.activate_fails
.count()
<= 2,
"{}",
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(devn)
.unwrap()
.activate_fails
.count()
);

BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(devn)
.unwrap()
.activate_fails
.inc();
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(devn)
.unwrap()
.read_bytes
.add(5);
assert!(
BLOCK_METRICS
.read()
.unwrap()
.metrics
.get(devn)
.unwrap()
.read_bytes
.count()
>= 5
);
}
}

0 comments on commit f2209b5

Please sign in to comment.