forked from console-rs/console
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(console): add self-wake percentage and warning (console-rs#113)
Depends on console-rs#112 This branch builds on console-rs#112 by calculating the *percentage* of a task's wakeups that were self-wakes, and displaying them in the tasks view. It also adds the beginnings of a rudimentary "Warnings" system, and displays warnings for any tasks who have woken themselves for more than 50% of their wakeups. There is a new `Warn` trait for generating warnings that can be used to add new warnings in the future. The warnings functionality can almost certainly be expanded --- for example, it would be nice to be able to quickly jump to the details view for a task that has warnings. In the future, I could imagine generating web documentation with details about a particular warning and how to fix it, like [clippy has][1], and linking to them from the console. But, this is a start, at least! [1]: https://rust-lang.github.io/rust-clippy/v0.0.174/ ![image](https://user-images.githubusercontent.com/2796466/132146062-9599ba12-cb17-48f8-b15c-4ba91947e282.png) ![image](https://user-images.githubusercontent.com/2796466/132146081-6dac231c-e929-4f93-b6ef-ac16f1dd166d.png) Signed-off-by: Eliza Weisman <[email protected]>
- Loading branch information
Showing
6 changed files
with
171 additions
and
36 deletions.
There are no files selected for viewing
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,28 @@ | ||
pub(crate) trait Percentage { | ||
// Using an extension trait for this is maybe a bit excessive, but making it | ||
// a method has the nice advantage of making it *really* obvious which is | ||
// the total and which is the amount. | ||
fn percent_of(self, total: Self) -> Self; | ||
} | ||
|
||
impl Percentage for usize { | ||
fn percent_of(self, total: Self) -> Self { | ||
percentage(total as f64, self as f64) as Self | ||
} | ||
} | ||
|
||
impl Percentage for u64 { | ||
fn percent_of(self, total: Self) -> Self { | ||
percentage(total as f64, self as f64) as Self | ||
} | ||
} | ||
|
||
pub(crate) fn percentage(total: f64, amount: f64) -> f64 { | ||
debug_assert!( | ||
total >= amount, | ||
"assertion failed: total >= amount; total={}, amount={}", | ||
total, | ||
amount | ||
); | ||
(amount / total) * 100.0 | ||
} |
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,37 @@ | ||
use crate::tasks::Task; | ||
|
||
pub trait Warn<T>: std::fmt::Debug { | ||
fn check(&self, val: &T) -> Option<String>; | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub(crate) struct SelfWakePercent { | ||
min_percent: u64, | ||
} | ||
|
||
impl SelfWakePercent { | ||
pub(crate) const DEFAULT_PERCENT: u64 = 50; | ||
pub(crate) fn new(min_percent: u64) -> Self { | ||
Self { min_percent } | ||
} | ||
} | ||
|
||
impl Default for SelfWakePercent { | ||
fn default() -> Self { | ||
Self::new(Self::DEFAULT_PERCENT) | ||
} | ||
} | ||
|
||
impl Warn<Task> for SelfWakePercent { | ||
fn check(&self, task: &Task) -> Option<String> { | ||
let self_wakes = task.self_wake_percent(); | ||
if self_wakes > self.min_percent { | ||
return Some(format!( | ||
"has woken itself for more than {}% of its total wakeups ({}%)", | ||
self.min_percent, self_wakes | ||
)); | ||
} | ||
|
||
None | ||
} | ||
} |