polkadot_node_core_pvf_common/prepare.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16
17use crate::ArtifactChecksum;
18use codec::{Decode, Encode};
19use std::path::PathBuf;
20
21/// Result from prepare worker if successful.
22#[derive(Debug, Clone, Default, Encode, Decode)]
23pub struct PrepareWorkerSuccess {
24 /// Checksum of the compiled PVF.
25 pub checksum: ArtifactChecksum,
26 /// Stats of the current preparation run.
27 pub stats: PrepareStats,
28}
29
30/// Result of PVF preparation if successful.
31#[derive(Debug, Clone, Default)]
32pub struct PrepareSuccess {
33 /// Checksum of the compiled PVF.
34 pub checksum: ArtifactChecksum,
35 /// Canonical path to the compiled artifact.
36 pub path: PathBuf,
37 /// Size in bytes
38 pub size: u64,
39 /// Stats of the current preparation run.
40 pub stats: PrepareStats,
41}
42
43/// Preparation statistics, including the CPU time and memory taken.
44#[derive(Debug, Clone, Default, Encode, Decode)]
45pub struct PrepareStats {
46 /// The CPU time that elapsed for the preparation job.
47 pub cpu_time_elapsed: std::time::Duration,
48 /// The observed memory statistics for the preparation job.
49 pub memory_stats: MemoryStats,
50 /// The decompressed Wasm code length observed during the preparation.
51 pub observed_wasm_code_len: u32,
52}
53
54/// Helper struct to contain all the memory stats, including `MemoryAllocationStats` and, if
55/// supported by the OS, `ru_maxrss`.
56#[derive(Clone, Debug, Default, Encode, Decode)]
57pub struct MemoryStats {
58 /// Memory stats from `tikv_jemalloc_ctl`, polling-based and not very precise.
59 #[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
60 pub memory_tracker_stats: Option<MemoryAllocationStats>,
61 /// `ru_maxrss` from `getrusage`. `None` if an error occurred.
62 #[cfg(target_os = "linux")]
63 pub max_rss: Option<i64>,
64 /// Peak allocation in bytes measured by tracking allocator
65 pub peak_tracked_alloc: u64,
66}
67
68/// Statistics of collected memory metrics.
69#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
70#[derive(Clone, Debug, Default, Encode, Decode)]
71pub struct MemoryAllocationStats {
72 /// Total resident memory, in bytes.
73 pub resident: u64,
74 /// Total allocated memory, in bytes.
75 pub allocated: u64,
76}
77
78/// The kind of prepare job.
79#[derive(Copy, Clone, Debug, Encode, Decode)]
80pub enum PrepareJobKind {
81 /// Compilation triggered by a candidate validation request.
82 Compilation,
83 /// A prechecking job.
84 Prechecking,
85}