wasmedge_sys/
statistics.rs1use crate::{ffi, WasmEdgeResult};
4use std::sync::Arc;
5use wasmedge_types::error::WasmEdgeError;
6
7#[derive(Debug, Clone)]
8pub struct Statistics {
10 pub(crate) inner: Arc<InnerStat>,
11}
12impl Statistics {
13 pub fn create() -> WasmEdgeResult<Self> {
19 let ctx = unsafe { ffi::WasmEdge_StatisticsCreate() };
20 match ctx.is_null() {
21 true => Err(Box::new(WasmEdgeError::StatisticsCreate)),
22 false => Ok(Statistics {
23 inner: Arc::new(InnerStat(ctx)),
24 }),
25 }
26 }
27
28 pub fn instr_count(&self) -> u64 {
30 unsafe { ffi::WasmEdge_StatisticsGetInstrCount(self.inner.0) }
31 }
32
33 pub fn instr_per_sec(&self) -> f64 {
55 unsafe { ffi::WasmEdge_StatisticsGetInstrPerSecond(self.inner.0) }
56 }
57
58 pub fn cost_in_total(&self) -> u64 {
60 unsafe { ffi::WasmEdge_StatisticsGetTotalCost(self.inner.0) }
61 }
62
63 pub fn set_cost_table(&mut self, cost_table: impl AsRef<[u64]>) {
69 unsafe {
70 ffi::WasmEdge_StatisticsSetCostTable(
71 self.inner.0,
72 cost_table.as_ref().as_ptr() as *mut _,
73 cost_table.as_ref().len() as u32,
74 )
75 }
76 }
77
78 pub fn set_cost_limit(&mut self, limit: u64) {
84 unsafe { ffi::WasmEdge_StatisticsSetCostLimit(self.inner.0, limit) }
85 }
86
87 pub fn clear(&mut self) {
89 unsafe { ffi::WasmEdge_StatisticsClear(self.inner.0) }
90 }
91
92 #[cfg(feature = "ffi")]
94 #[cfg_attr(docsrs, doc(cfg(feature = "ffi")))]
95 pub fn as_ptr(&self) -> *const ffi::WasmEdge_StatisticsContext {
96 self.inner.0 as *const _
97 }
98}
99impl Drop for Statistics {
100 fn drop(&mut self) {
101 unsafe { ffi::WasmEdge_StatisticsDelete(self.inner.0) }
102 }
103}
104
105#[derive(Debug)]
106pub(crate) struct InnerStat(pub(crate) *mut ffi::WasmEdge_StatisticsContext);
107unsafe impl Send for InnerStat {}
108unsafe impl Sync for InnerStat {}