Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit f95b1c2

Browse files
author
parity-processbot
committed
Merge remote-tracking branch 'origin/master' into gav-polkadot-opengov
2 parents b24a265 + 10daabc commit f95b1c2

File tree

15 files changed

+413
-335
lines changed

15 files changed

+413
-335
lines changed

Cargo.lock

Lines changed: 181 additions & 181 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/core/candidate-validation/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#![warn(missing_docs)]
2525

2626
use polkadot_node_core_pvf::{
27-
InvalidCandidate as WasmInvalidCandidate, PrepareError, Pvf, ValidationError, ValidationHost,
27+
InvalidCandidate as WasmInvalidCandidate, PrepareError, PrepareStats, Pvf, ValidationError,
28+
ValidationHost,
2829
};
2930
use polkadot_node_primitives::{
3031
BlockData, InvalidCandidate, PoV, ValidationResult, POV_BOMB_LIMIT, VALIDATION_CODE_BOMB_LIMIT,
@@ -654,7 +655,7 @@ trait ValidationBackend {
654655
validation_result
655656
}
656657

657-
async fn precheck_pvf(&mut self, pvf: Pvf) -> Result<Duration, PrepareError>;
658+
async fn precheck_pvf(&mut self, pvf: Pvf) -> Result<PrepareStats, PrepareError>;
658659
}
659660

660661
#[async_trait]
@@ -680,7 +681,7 @@ impl ValidationBackend for ValidationHost {
680681
.map_err(|_| ValidationError::InternalError("validation was cancelled".into()))?
681682
}
682683

683-
async fn precheck_pvf(&mut self, pvf: Pvf) -> Result<Duration, PrepareError> {
684+
async fn precheck_pvf(&mut self, pvf: Pvf) -> Result<PrepareStats, PrepareError> {
684685
let (tx, rx) = oneshot::channel();
685686
if let Err(err) = self.precheck_pvf(pvf, tx).await {
686687
// Return an IO error if there was an error communicating with the host.

node/core/candidate-validation/src/tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl ValidationBackend for MockValidateCandidateBackend {
377377
result
378378
}
379379

380-
async fn precheck_pvf(&mut self, _pvf: Pvf) -> Result<Duration, PrepareError> {
380+
async fn precheck_pvf(&mut self, _pvf: Pvf) -> Result<PrepareStats, PrepareError> {
381381
unreachable!()
382382
}
383383
}
@@ -894,11 +894,11 @@ fn pov_decompression_failure_is_invalid() {
894894
}
895895

896896
struct MockPreCheckBackend {
897-
result: Result<Duration, PrepareError>,
897+
result: Result<PrepareStats, PrepareError>,
898898
}
899899

900900
impl MockPreCheckBackend {
901-
fn with_hardcoded_result(result: Result<Duration, PrepareError>) -> Self {
901+
fn with_hardcoded_result(result: Result<PrepareStats, PrepareError>) -> Self {
902902
Self { result }
903903
}
904904
}
@@ -914,7 +914,7 @@ impl ValidationBackend for MockPreCheckBackend {
914914
unreachable!()
915915
}
916916

917-
async fn precheck_pvf(&mut self, _pvf: Pvf) -> Result<Duration, PrepareError> {
917+
async fn precheck_pvf(&mut self, _pvf: Pvf) -> Result<PrepareStats, PrepareError> {
918918
self.result.clone()
919919
}
920920
}
@@ -931,7 +931,7 @@ fn precheck_works() {
931931

932932
let (check_fut, check_result) = precheck_pvf(
933933
ctx.sender(),
934-
MockPreCheckBackend::with_hardcoded_result(Ok(Duration::default())),
934+
MockPreCheckBackend::with_hardcoded_result(Ok(PrepareStats::default())),
935935
relay_parent,
936936
validation_code_hash,
937937
)
@@ -977,7 +977,7 @@ fn precheck_invalid_pvf_blob_compression() {
977977

978978
let (check_fut, check_result) = precheck_pvf(
979979
ctx.sender(),
980-
MockPreCheckBackend::with_hardcoded_result(Ok(Duration::default())),
980+
MockPreCheckBackend::with_hardcoded_result(Ok(PrepareStats::default())),
981981
relay_parent,
982982
validation_code_hash,
983983
)

node/core/pvf/src/artifacts.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
1616

17-
use crate::{error::PrepareError, host::PrepareResultSender};
17+
use crate::{error::PrepareError, host::PrepareResultSender, prepare::PrepareStats};
1818
use always_assert::always;
1919
use polkadot_parachain::primitives::ValidationCodeHash;
2020
use std::{
@@ -101,8 +101,8 @@ pub enum ArtifactState {
101101
/// This is updated when we get the heads up for this artifact or when we just discover
102102
/// this file.
103103
last_time_needed: SystemTime,
104-
/// The CPU time that was taken preparing this artifact.
105-
cpu_time_elapsed: Duration,
104+
/// Stats produced by successful preparation.
105+
prepare_stats: PrepareStats,
106106
},
107107
/// A task to prepare this artifact is scheduled.
108108
Preparing {
@@ -177,12 +177,12 @@ impl Artifacts {
177177
&mut self,
178178
artifact_id: ArtifactId,
179179
last_time_needed: SystemTime,
180-
cpu_time_elapsed: Duration,
180+
prepare_stats: PrepareStats,
181181
) {
182182
// See the precondition.
183183
always!(self
184184
.artifacts
185-
.insert(artifact_id, ArtifactState::Prepared { last_time_needed, cpu_time_elapsed })
185+
.insert(artifact_id, ArtifactState::Prepared { last_time_needed, prepare_stats })
186186
.is_none());
187187
}
188188

node/core/pvf/src/error.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
1616

17+
use crate::prepare::PrepareStats;
1718
use parity_scale_codec::{Decode, Encode};
18-
use std::{any::Any, fmt, time::Duration};
19+
use std::{any::Any, fmt};
1920

20-
/// Result of PVF preparation performed by the validation host. Contains the elapsed CPU time if
21+
/// Result of PVF preparation performed by the validation host. Contains stats about the preparation if
2122
/// successful
22-
pub type PrepareResult = Result<Duration, PrepareError>;
23+
pub type PrepareResult = Result<PrepareStats, PrepareError>;
2324

2425
/// An error that occurred during the prepare part of the PVF pipeline.
2526
#[derive(Debug, Clone, Encode, Decode)]

node/core/pvf/src/host.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,9 @@ async fn handle_precheck_pvf(
456456

457457
if let Some(state) = artifacts.artifact_state_mut(&artifact_id) {
458458
match state {
459-
ArtifactState::Prepared { last_time_needed, cpu_time_elapsed } => {
459+
ArtifactState::Prepared { last_time_needed, prepare_stats } => {
460460
*last_time_needed = SystemTime::now();
461-
let _ = result_sender.send(Ok(*cpu_time_elapsed));
461+
let _ = result_sender.send(Ok(prepare_stats.clone()));
462462
},
463463
ArtifactState::Preparing { waiting_for_response, num_failures: _ } =>
464464
waiting_for_response.push(result_sender),
@@ -725,8 +725,8 @@ async fn handle_prepare_done(
725725
}
726726

727727
*state = match result {
728-
Ok(cpu_time_elapsed) =>
729-
ArtifactState::Prepared { last_time_needed: SystemTime::now(), cpu_time_elapsed },
728+
Ok(prepare_stats) =>
729+
ArtifactState::Prepared { last_time_needed: SystemTime::now(), prepare_stats },
730730
Err(error) => {
731731
let last_time_failed = SystemTime::now();
732732
let num_failures = *num_failures + 1;
@@ -834,7 +834,7 @@ fn pulse_every(interval: std::time::Duration) -> impl futures::Stream<Item = ()>
834834
#[cfg(test)]
835835
mod tests {
836836
use super::*;
837-
use crate::{InvalidCandidate, PrepareError};
837+
use crate::{prepare::PrepareStats, InvalidCandidate, PrepareError};
838838
use assert_matches::assert_matches;
839839
use futures::future::BoxFuture;
840840

@@ -1056,8 +1056,12 @@ mod tests {
10561056
let mut builder = Builder::default();
10571057
builder.cleanup_pulse_interval = Duration::from_millis(100);
10581058
builder.artifact_ttl = Duration::from_millis(500);
1059-
builder.artifacts.insert_prepared(artifact_id(1), mock_now, Duration::default());
1060-
builder.artifacts.insert_prepared(artifact_id(2), mock_now, Duration::default());
1059+
builder
1060+
.artifacts
1061+
.insert_prepared(artifact_id(1), mock_now, PrepareStats::default());
1062+
builder
1063+
.artifacts
1064+
.insert_prepared(artifact_id(2), mock_now, PrepareStats::default());
10611065
let mut test = builder.build();
10621066
let mut host = test.host_handle();
10631067

@@ -1129,7 +1133,7 @@ mod tests {
11291133
test.from_prepare_queue_tx
11301134
.send(prepare::FromQueue {
11311135
artifact_id: artifact_id(1),
1132-
result: Ok(Duration::default()),
1136+
result: Ok(PrepareStats::default()),
11331137
})
11341138
.await
11351139
.unwrap();
@@ -1145,7 +1149,7 @@ mod tests {
11451149
test.from_prepare_queue_tx
11461150
.send(prepare::FromQueue {
11471151
artifact_id: artifact_id(2),
1148-
result: Ok(Duration::default()),
1152+
result: Ok(PrepareStats::default()),
11491153
})
11501154
.await
11511155
.unwrap();
@@ -1197,7 +1201,7 @@ mod tests {
11971201
test.from_prepare_queue_tx
11981202
.send(prepare::FromQueue {
11991203
artifact_id: artifact_id(1),
1200-
result: Ok(Duration::default()),
1204+
result: Ok(PrepareStats::default()),
12011205
})
12021206
.await
12031207
.unwrap();
@@ -1304,7 +1308,7 @@ mod tests {
13041308
test.from_prepare_queue_tx
13051309
.send(prepare::FromQueue {
13061310
artifact_id: artifact_id(2),
1307-
result: Ok(Duration::default()),
1311+
result: Ok(PrepareStats::default()),
13081312
})
13091313
.await
13101314
.unwrap();
@@ -1454,7 +1458,7 @@ mod tests {
14541458
test.from_prepare_queue_tx
14551459
.send(prepare::FromQueue {
14561460
artifact_id: artifact_id(1),
1457-
result: Ok(Duration::default()),
1461+
result: Ok(PrepareStats::default()),
14581462
})
14591463
.await
14601464
.unwrap();
@@ -1630,7 +1634,7 @@ mod tests {
16301634
test.from_prepare_queue_tx
16311635
.send(prepare::FromQueue {
16321636
artifact_id: artifact_id(1),
1633-
result: Ok(Duration::default()),
1637+
result: Ok(PrepareStats::default()),
16341638
})
16351639
.await
16361640
.unwrap();

node/core/pvf/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub mod testing;
108108
pub use sp_tracing;
109109

110110
pub use error::{InvalidCandidate, PrepareError, PrepareResult, ValidationError};
111+
pub use prepare::PrepareStats;
111112
pub use priority::Priority;
112113
pub use pvf::Pvf;
113114

node/core/pvf/src/metrics.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
//! Prometheus metrics related to the validation host.
1818
19+
use crate::prepare::MemoryStats;
1920
use polkadot_node_metrics::metrics::{self, prometheus};
2021

2122
/// Validation host metrics.
@@ -73,24 +74,24 @@ impl Metrics {
7374
self.0.as_ref().map(|metrics| metrics.execution_time.start_timer())
7475
}
7576

76-
/// Observe max_rss for preparation.
77-
pub(crate) fn observe_preparation_max_rss(&self, max_rss: f64) {
77+
/// Observe memory stats for preparation.
78+
#[allow(unused_variables)]
79+
pub(crate) fn observe_preparation_memory_metrics(&self, memory_stats: MemoryStats) {
7880
if let Some(metrics) = &self.0 {
79-
metrics.preparation_max_rss.observe(max_rss);
80-
}
81-
}
81+
#[cfg(target_os = "linux")]
82+
if let Some(max_rss) = memory_stats.max_rss {
83+
metrics.preparation_max_rss.observe(max_rss as f64);
84+
}
8285

83-
/// Observe max resident memory for preparation.
84-
pub(crate) fn observe_preparation_max_resident(&self, max_resident_kb: f64) {
85-
if let Some(metrics) = &self.0 {
86-
metrics.preparation_max_resident.observe(max_resident_kb);
87-
}
88-
}
86+
#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
87+
if let Some(tracker_stats) = memory_stats.memory_tracker_stats {
88+
// We convert these stats from B to KB to match the unit of `ru_maxrss` from `getrusage`.
89+
let max_resident_kb = (tracker_stats.resident / 1024) as f64;
90+
let max_allocated_kb = (tracker_stats.allocated / 1024) as f64;
8991

90-
/// Observe max allocated memory for preparation.
91-
pub(crate) fn observe_preparation_max_allocated(&self, max_allocated_kb: f64) {
92-
if let Some(metrics) = &self.0 {
93-
metrics.preparation_max_allocated.observe(max_allocated_kb);
92+
metrics.preparation_max_resident.observe(max_resident_kb);
93+
metrics.preparation_max_allocated.observe(max_allocated_kb);
94+
}
9495
}
9596
}
9697
}
@@ -106,8 +107,11 @@ struct MetricsInner {
106107
execute_finished: prometheus::Counter<prometheus::U64>,
107108
preparation_time: prometheus::Histogram,
108109
execution_time: prometheus::Histogram,
110+
#[cfg(target_os = "linux")]
109111
preparation_max_rss: prometheus::Histogram,
112+
#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
110113
preparation_max_allocated: prometheus::Histogram,
114+
#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
111115
preparation_max_resident: prometheus::Histogram,
112116
}
113117

@@ -226,6 +230,7 @@ impl metrics::Metrics for Metrics {
226230
)?,
227231
registry,
228232
)?,
233+
#[cfg(target_os = "linux")]
229234
preparation_max_rss: prometheus::register(
230235
prometheus::Histogram::with_opts(
231236
prometheus::HistogramOpts::new(
@@ -238,6 +243,7 @@ impl metrics::Metrics for Metrics {
238243
)?,
239244
registry,
240245
)?,
246+
#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
241247
preparation_max_resident: prometheus::register(
242248
prometheus::Histogram::with_opts(
243249
prometheus::HistogramOpts::new(
@@ -250,6 +256,7 @@ impl metrics::Metrics for Metrics {
250256
)?,
251257
registry,
252258
)?,
259+
#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))]
253260
preparation_max_allocated: prometheus::register(
254261
prometheus::Histogram::with_opts(
255262
prometheus::HistogramOpts::new(

0 commit comments

Comments
 (0)