-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
627a326
commit b659487
Showing
18 changed files
with
571 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
test_generate_dir | ||
priority_workload_cpu_usage.json | ||
scripts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Run performance test for different priority & workload type | ||
|
||
``` | ||
cargo test --package common-runtime --lib -- test_metrics::test_all_cpu_usage --nocapture | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// use core::panicking::panic; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::sync::Arc; | ||
use std::task::{Context, Poll}; | ||
|
||
use futures::FutureExt; | ||
use tokio::time::Sleep; | ||
|
||
use crate::runtime_throttle_count_mode::RuntimeThrottleShareWithFuture; | ||
|
||
enum State { | ||
Common, | ||
Backoff(Pin<Box<Sleep>>), | ||
} | ||
|
||
impl State { | ||
fn unwrap_backoff(&mut self) -> &mut Pin<Box<Sleep>> { | ||
match self { | ||
State::Backoff(sleep) => sleep, | ||
_ => panic!("unwrap_backoff failed"), | ||
} | ||
} | ||
} | ||
|
||
#[pin_project::pin_project] | ||
pub struct ThrottleFuture<F: Future + Send + 'static> { | ||
#[pin] | ||
future: F, | ||
/// priority of this future | ||
handle: Arc<RuntimeThrottleShareWithFuture>, | ||
/// count of pendings, track the pending count for test | ||
pub pend_cnt: u32, | ||
state: State, | ||
} | ||
|
||
impl<F> ThrottleFuture<F> | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send + 'static, | ||
{ | ||
pub fn new(handle: Arc<RuntimeThrottleShareWithFuture>, future: F) -> Self { | ||
Self { | ||
future, | ||
handle, | ||
pend_cnt: 0, | ||
state: State::Common, | ||
} | ||
} | ||
} | ||
|
||
impl<F> Future for ThrottleFuture<F> | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send + 'static, | ||
{ | ||
type Output = F::Output; | ||
|
||
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = self.project(); | ||
|
||
match this.state { | ||
State::Common => {} | ||
State::Backoff(ref mut sleep) => match sleep.poll_unpin(cx) { | ||
Poll::Ready(_) => { | ||
*this.state = State::Common; | ||
} | ||
Poll::Pending => return Poll::Pending, | ||
}, | ||
}; | ||
|
||
if let Some(ratelimiter) = &this.handle.ratelimiter { | ||
*this.pend_cnt += 1; | ||
|
||
if let Err(wait) = ratelimiter.try_wait() { | ||
*this.state = State::Backoff(Box::pin(tokio::time::sleep(wait))); | ||
match this.state.unwrap_backoff().poll_unpin(cx) { | ||
Poll::Ready(_) => { | ||
*this.state = State::Common; | ||
cx.waker().clone().wake(); | ||
return Poll::Pending; | ||
} | ||
Poll::Pending => { | ||
return Poll::Pending; | ||
} | ||
} | ||
} | ||
} | ||
|
||
let poll_res = this.future.poll(cx); | ||
|
||
match poll_res { | ||
Poll::Ready(r) => Poll::Ready(r), | ||
Poll::Pending => { | ||
*this.pend_cnt += 1; | ||
Poll::Pending | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.