1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! Defines WasmEdge Statistics struct.

use crate::WasmEdgeResult;
use wasmedge_sys as sys;

/// Used to collect statistics of the WasmEdge runtime, such as the count of instructions in execution.
#[derive(Debug, Clone)]
pub struct Statistics {
    pub(crate) inner: sys::Statistics,
}
impl Statistics {
    /// Creates a new [Statistics].
    ///
    /// # Error
    ///
    /// If fail to create a [Statistics], then an error is returned.
    pub fn new() -> WasmEdgeResult<Self> {
        let inner = sys::Statistics::create()?;
        Ok(Self { inner })
    }

    /// Returns the instruction count in execution.
    pub fn count(&self) -> u64 {
        self.inner.instr_count()
    }

    /// Returns the instruction count per second in execution.
    ///
    /// # Notice
    ///
    /// For the following cases,
    /// - [Statistics] is not enabled, or
    /// - the total execution time is 0
    ///
    /// The instructions per second could be `NaN`, which represents `divided-by-zero`.
    /// Use the `is_nan` function of F64 to check the return value before use it,
    /// for example,
    ///
    /// ```
    /// use wasmedge_sdk::Statistics;
    ///
    /// // create a Statistics instance
    /// let stat = Statistics::new().expect("fail to create a Statistics");
    ///
    /// // check instruction count per second
    /// assert!(stat.count_per_second().is_nan());
    /// ```
    pub fn count_per_second(&self) -> f64 {
        self.inner.instr_per_sec()
    }

    /// Returns the total cost in execution.
    pub fn cost(&self) -> u64 {
        self.inner.cost_in_total()
    }

    /// Sets the cost of instructions.
    ///
    /// # Arguments
    ///
    /// - `cost_table` specifies the slice of cost table.
    pub fn set_cost_table(&mut self, cost_table: impl AsRef<[u64]>) {
        self.inner.set_cost_table(cost_table)
    }

    /// Sets the cost limit in execution.
    ///
    /// # Arguments
    ///
    /// - `limit` specifies the cost limit.
    pub fn set_cost_limit(&mut self, limit: u64) {
        self.inner.set_cost_limit(limit)
    }
}