From beac9e2b83d7119cea11f35ceb85748d69b46ca2 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Mon, 29 Jan 2024 21:16:15 +0800 Subject: [PATCH 01/13] feat: add `--allow` flag Signed-off-by: hi-rustin --- README.md | 15 +++++++++++++++ tokio-console/console.example.toml | 1 + tokio-console/src/config.rs | 24 ++++++++++++++++++++++++ tokio-console/tests/cli-ui.stdout | 15 +++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/README.md b/README.md index b5a4db4d8..230f8d610 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,21 @@ Options: [default: self-wakes lost-waker never-yielded] [possible values: self-wakes, lost-waker, never-yielded] + -A, --allow ... + Allow lint warnings. + + This is a comma-separated list of warnings to allow. + + Each warning is specified by its name, which is one of: * + `self-wakes` -- Warns when a task wakes itself more than a + certain percentage of its total wakeups. Default percentage is + 50%. * `lost-waker` -- Warns when a task is dropped without + being woken. + + * `never-yielded` -- Warns when a task has never yielded. + + [possible values: self-wakes, lost-waker, never-yielded] + --log-dir Path to a directory to write the console's internal logs to. diff --git a/tokio-console/console.example.toml b/tokio-console/console.example.toml index 4b0db2b4e..ca4d5e01e 100644 --- a/tokio-console/console.example.toml +++ b/tokio-console/console.example.toml @@ -5,6 +5,7 @@ warnings = [ 'lost-waker', 'never-yielded', ] +allow_warnings = [] log_directory = '/tmp/tokio-console/logs' retention = '6s' diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index c470a22e9..06576ac69 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -67,6 +67,19 @@ pub struct Config { #[clap(default_values_t = KnownWarnings::default_enabled_warnings())] pub(crate) warnings: Vec, + /// Allow lint warnings. + /// + /// This is a comma-separated list of warnings to allow. + /// + /// Each warning is specified by its name, which is one of: + /// * `self-wakes` -- Warns when a task wakes itself more than a certain percentage of its total wakeups. + /// Default percentage is 50%. + /// * `lost-waker` -- Warns when a task is dropped without being woken. + /// + /// * `never-yielded` -- Warns when a task has never yielded. + #[clap(long = "allow", short = 'A', value_delimiter = ',', num_args = 1..)] + pub(crate) allow_warnings: Vec, + /// Path to a directory to write the console's internal logs to. /// /// [default: /tmp/tokio-console/logs] @@ -299,6 +312,7 @@ struct ConfigFile { default_target_addr: Option, log: Option, warnings: Vec, + allow_warnings: Vec, log_directory: Option, retention: Option, charset: Option, @@ -494,6 +508,13 @@ impl Config { warns.dedup(); warns }, + allow_warnings: { + let mut allow_warnings: Vec = other.allow_warnings; + allow_warnings.extend(self.allow_warnings); + allow_warnings.sort_unstable(); + allow_warnings.dedup(); + allow_warnings + }, retain_for: other.retain_for.or(self.retain_for), view_options: self.view_options.merge_with(other.view_options), subcmd: other.subcmd.or(self.subcmd), @@ -509,6 +530,7 @@ impl Default for Config { filter::Targets::new().with_default(filter::LevelFilter::OFF), )), warnings: KnownWarnings::default_enabled_warnings(), + allow_warnings: Vec::default(), log_directory: Some(default_log_directory()), retain_for: Some(RetainFor::default()), view_options: ViewOptions::default(), @@ -745,6 +767,7 @@ impl From for ConfigFile { log: config.log_filter.map(|filter| filter.to_string()), log_directory: config.log_directory, warnings: config.warnings, + allow_warnings: config.allow_warnings, retention: config.retain_for, charset: Some(CharsetConfig { lang: config.view_options.lang, @@ -768,6 +791,7 @@ impl TryFrom for Config { target_addr: value.target_addr()?, log_filter: value.log_filter()?, warnings: value.warnings.clone(), + allow_warnings: value.allow_warnings.clone(), log_directory: value.log_directory.take(), retain_for: value.retain_for(), view_options: ViewOptions { diff --git a/tokio-console/tests/cli-ui.stdout b/tokio-console/tests/cli-ui.stdout index 519a82dbd..ff49fbc29 100644 --- a/tokio-console/tests/cli-ui.stdout +++ b/tokio-console/tests/cli-ui.stdout @@ -58,6 +58,21 @@ Options: [default: self-wakes lost-waker never-yielded] [possible values: self-wakes, lost-waker, never-yielded] + -A, --allow ... + Allow lint warnings. + + This is a comma-separated list of warnings to allow. + + Each warning is specified by its name, which is one of: * + `self-wakes` -- Warns when a task wakes itself more than a + certain percentage of its total wakeups. Default percentage is + 50%. * `lost-waker` -- Warns when a task is dropped without + being woken. + + * `never-yielded` -- Warns when a task has never yielded. + + [possible values: self-wakes, lost-waker, never-yielded] + --log-dir Path to a directory to write the console's internal logs to. From c7f7809fc0c62b7a76cfc8ee3f44457fcb7d91ee Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Mon, 29 Jan 2024 21:36:06 +0800 Subject: [PATCH 02/13] feat: filter allowed warnings Signed-off-by: hi-rustin --- tokio-console/src/main.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tokio-console/src/main.rs b/tokio-console/src/main.rs index 4f99ee71a..996d6e591 100644 --- a/tokio-console/src/main.rs +++ b/tokio-console/src/main.rs @@ -1,3 +1,5 @@ +use std::collections::BTreeSet; + use color_eyre::{eyre::eyre, Help, SectionExt}; use console_api::tasks::TaskDetails; use state::State; @@ -65,8 +67,14 @@ async fn main() -> color_eyre::Result<()> { // A channel to send the task details update stream (no need to keep outdated details in the memory) let (details_tx, mut details_rx) = mpsc::channel::(2); + let allow_warnings = args.allow_warnings.iter().collect::>(); + let warnings = args + .warnings + .iter() + .filter(|lint| !allow_warnings.contains(lint)); + let mut state = State::default() - .with_task_linters(args.warnings.iter().map(|lint| lint.into())) + .with_task_linters(warnings.map(|lint| lint.into())) .with_retain_for(retain_for); let mut input = Box::pin(input::EventStream::new().try_filter(|event| { future::ready(!matches!( From 5029714e33995f8ec50ef467b1935dda7fbd5ce7 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 7 Feb 2024 22:48:37 +0800 Subject: [PATCH 03/13] feat: add all support Signed-off-by: hi-rustin --- tokio-console/src/config.rs | 67 +++++++++++++++++++++++++++++++------ tokio-console/src/main.rs | 22 +++++++----- 2 files changed, 70 insertions(+), 19 deletions(-) diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index 06576ac69..99438662d 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -6,6 +6,7 @@ use clap::{ArgAction, ArgGroup, CommandFactory, Parser as Clap, Subcommand, Valu use clap_complete::Shell; use color_eyre::eyre::WrapErr; use serde::{Deserialize, Serialize}; +use std::collections::BTreeSet; use std::fmt; use std::fs; use std::ops::Not; @@ -77,8 +78,8 @@ pub struct Config { /// * `lost-waker` -- Warns when a task is dropped without being woken. /// /// * `never-yielded` -- Warns when a task has never yielded. - #[clap(long = "allow", short = 'A', value_delimiter = ',', num_args = 1..)] - pub(crate) allow_warnings: Vec, + #[clap(long = "allow", short = 'A')] + pub(crate) allow_warnings: AllowedWarnings, /// Path to a directory to write the console's internal logs to. /// @@ -139,6 +140,17 @@ pub(crate) enum KnownWarnings { NeverYielded, } +impl From<&str> for KnownWarnings { + fn from(s: &str) -> Self { + match s { + "self-wakes" => KnownWarnings::SelfWakes, + "lost-waker" => KnownWarnings::LostWaker, + "never-yielded" => KnownWarnings::NeverYielded, + _ => panic!("unknown warning: {}", s), + } + } +} + impl From<&KnownWarnings> for warnings::Linter { fn from(warning: &KnownWarnings) -> Self { match warning { @@ -169,6 +181,45 @@ impl KnownWarnings { } } +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] +pub(crate) enum AllowedWarnings { + All, + Some(BTreeSet), +} + +impl From<&str> for AllowedWarnings { + fn from(s: &str) -> Self { + match s { + "all" => AllowedWarnings::All, + _ => { + let warnings = s + .split(',') + .map(KnownWarnings::from) + .collect::>(); + AllowedWarnings::Some(warnings) + } + } + } +} + +impl AllowedWarnings { + fn default() -> Self { + AllowedWarnings::Some(BTreeSet::default()) + } + + fn merge(&self, allowed: &Self) -> Self { + match (self, allowed) { + (AllowedWarnings::All, _) => AllowedWarnings::All, + (_, AllowedWarnings::All) => AllowedWarnings::All, + (AllowedWarnings::Some(a), AllowedWarnings::Some(b)) => { + let mut warnings = a.clone(); + warnings.extend(b.clone()); + AllowedWarnings::Some(warnings) + } + } + } +} + #[derive(Debug, Subcommand, PartialEq, Eq)] pub enum OptionalCmd { /// Generate a `console.toml` config file with the default configuration @@ -312,7 +363,7 @@ struct ConfigFile { default_target_addr: Option, log: Option, warnings: Vec, - allow_warnings: Vec, + allow_warnings: AllowedWarnings, log_directory: Option, retention: Option, charset: Option, @@ -508,13 +559,7 @@ impl Config { warns.dedup(); warns }, - allow_warnings: { - let mut allow_warnings: Vec = other.allow_warnings; - allow_warnings.extend(self.allow_warnings); - allow_warnings.sort_unstable(); - allow_warnings.dedup(); - allow_warnings - }, + allow_warnings: { self.allow_warnings.merge(&other.allow_warnings) }, retain_for: other.retain_for.or(self.retain_for), view_options: self.view_options.merge_with(other.view_options), subcmd: other.subcmd.or(self.subcmd), @@ -530,7 +575,7 @@ impl Default for Config { filter::Targets::new().with_default(filter::LevelFilter::OFF), )), warnings: KnownWarnings::default_enabled_warnings(), - allow_warnings: Vec::default(), + allow_warnings: AllowedWarnings::default(), log_directory: Some(default_log_directory()), retain_for: Some(RetainFor::default()), view_options: ViewOptions::default(), diff --git a/tokio-console/src/main.rs b/tokio-console/src/main.rs index 996d6e591..2a4150257 100644 --- a/tokio-console/src/main.rs +++ b/tokio-console/src/main.rs @@ -1,5 +1,3 @@ -use std::collections::BTreeSet; - use color_eyre::{eyre::eyre, Help, SectionExt}; use console_api::tasks::TaskDetails; use state::State; @@ -17,6 +15,7 @@ use ratatui::{ use tokio::sync::{mpsc, watch}; use crate::{ + config::AllowedWarnings, input::{Event, KeyEvent, KeyEventKind}, view::{bold, UpdateKind}, }; @@ -67,14 +66,21 @@ async fn main() -> color_eyre::Result<()> { // A channel to send the task details update stream (no need to keep outdated details in the memory) let (details_tx, mut details_rx) = mpsc::channel::(2); - let allow_warnings = args.allow_warnings.iter().collect::>(); - let warnings = args - .warnings - .iter() - .filter(|lint| !allow_warnings.contains(lint)); + let warnings = match args.allow_warnings { + AllowedWarnings::All => { + vec![] + } + AllowedWarnings::Some(allow_warnings) => { + let warnings = args + .warnings + .iter() + .filter(|lint| !allow_warnings.contains(lint)); + warnings.collect() + } + }; let mut state = State::default() - .with_task_linters(warnings.map(|lint| lint.into())) + .with_task_linters(warnings.into_iter().map(|lint| lint.into())) .with_retain_for(retain_for); let mut input = Box::pin(input::EventStream::new().try_filter(|event| { future::ready(!matches!( From 3a6b5d6f341fc19e60d194252bfcb4c6d90ed365 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 7 Feb 2024 22:58:14 +0800 Subject: [PATCH 04/13] fix: optional Signed-off-by: hi-rustin --- tokio-console/console.example.toml | 1 - tokio-console/src/config.rs | 17 +++++++++-------- tokio-console/src/main.rs | 23 +++++++++++++---------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/tokio-console/console.example.toml b/tokio-console/console.example.toml index ca4d5e01e..4b0db2b4e 100644 --- a/tokio-console/console.example.toml +++ b/tokio-console/console.example.toml @@ -5,7 +5,6 @@ warnings = [ 'lost-waker', 'never-yielded', ] -allow_warnings = [] log_directory = '/tmp/tokio-console/logs' retention = '6s' diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index 99438662d..c6016e7fd 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -79,7 +79,7 @@ pub struct Config { /// /// * `never-yielded` -- Warns when a task has never yielded. #[clap(long = "allow", short = 'A')] - pub(crate) allow_warnings: AllowedWarnings, + pub(crate) allow_warnings: Option, /// Path to a directory to write the console's internal logs to. /// @@ -203,10 +203,6 @@ impl From<&str> for AllowedWarnings { } impl AllowedWarnings { - fn default() -> Self { - AllowedWarnings::Some(BTreeSet::default()) - } - fn merge(&self, allowed: &Self) -> Self { match (self, allowed) { (AllowedWarnings::All, _) => AllowedWarnings::All, @@ -363,7 +359,7 @@ struct ConfigFile { default_target_addr: Option, log: Option, warnings: Vec, - allow_warnings: AllowedWarnings, + allow_warnings: Option, log_directory: Option, retention: Option, charset: Option, @@ -559,7 +555,12 @@ impl Config { warns.dedup(); warns }, - allow_warnings: { self.allow_warnings.merge(&other.allow_warnings) }, + allow_warnings: { + match (self.allow_warnings, other.allow_warnings) { + (Some(a), Some(b)) => Some(a.merge(&b)), + (a, b) => a.or(b), + } + }, retain_for: other.retain_for.or(self.retain_for), view_options: self.view_options.merge_with(other.view_options), subcmd: other.subcmd.or(self.subcmd), @@ -575,7 +576,7 @@ impl Default for Config { filter::Targets::new().with_default(filter::LevelFilter::OFF), )), warnings: KnownWarnings::default_enabled_warnings(), - allow_warnings: AllowedWarnings::default(), + allow_warnings: None, log_directory: Some(default_log_directory()), retain_for: Some(RetainFor::default()), view_options: ViewOptions::default(), diff --git a/tokio-console/src/main.rs b/tokio-console/src/main.rs index 2a4150257..13b99c2ba 100644 --- a/tokio-console/src/main.rs +++ b/tokio-console/src/main.rs @@ -67,16 +67,19 @@ async fn main() -> color_eyre::Result<()> { let (details_tx, mut details_rx) = mpsc::channel::(2); let warnings = match args.allow_warnings { - AllowedWarnings::All => { - vec![] - } - AllowedWarnings::Some(allow_warnings) => { - let warnings = args - .warnings - .iter() - .filter(|lint| !allow_warnings.contains(lint)); - warnings.collect() - } + Some(allow_warnings) => match allow_warnings { + AllowedWarnings::All => { + vec![] + } + AllowedWarnings::Some(allow_warnings) => { + let warnings = args + .warnings + .iter() + .filter(|lint| !allow_warnings.contains(lint)); + warnings.collect::>() + } + }, + None => args.warnings.iter().collect::>(), }; let mut state = State::default() From af77f0c92a4d31a8e2d2a2f5855e911e8aaa0619 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 7 Feb 2024 23:08:29 +0800 Subject: [PATCH 05/13] test: update output Signed-off-by: hi-rustin --- README.md | 13 +++++++------ tokio-console/src/config.rs | 5 +++-- tokio-console/tests/cli-ui.stdout | 13 +++++++------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 230f8d610..5160e15c1 100644 --- a/README.md +++ b/README.md @@ -234,15 +234,16 @@ Options: This is a comma-separated list of warnings to allow. - Each warning is specified by its name, which is one of: * - `self-wakes` -- Warns when a task wakes itself more than a + Each warning is specified by its name, which is one of: + + * `self-wakes` -- Warns when a task wakes itself more than a certain percentage of its total wakeups. Default percentage is - 50%. * `lost-waker` -- Warns when a task is dropped without - being woken. + 50%. - * `never-yielded` -- Warns when a task has never yielded. + * `lost-waker` -- Warns when a task is dropped without being + woken. - [possible values: self-wakes, lost-waker, never-yielded] + * `never-yielded` -- Warns when a task has never yielded. --log-dir Path to a directory to write the console's internal logs to. diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index c6016e7fd..97071988e 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -63,7 +63,6 @@ pub struct Config { /// * `lost-waker` -- Warns when a task is dropped without being woken. /// /// * `never-yielded` -- Warns when a task has never yielded. - /// #[clap(long = "warn", short = 'W', value_delimiter = ',', num_args = 1..)] #[clap(default_values_t = KnownWarnings::default_enabled_warnings())] pub(crate) warnings: Vec, @@ -73,12 +72,14 @@ pub struct Config { /// This is a comma-separated list of warnings to allow. /// /// Each warning is specified by its name, which is one of: + /// /// * `self-wakes` -- Warns when a task wakes itself more than a certain percentage of its total wakeups. /// Default percentage is 50%. + /// /// * `lost-waker` -- Warns when a task is dropped without being woken. /// /// * `never-yielded` -- Warns when a task has never yielded. - #[clap(long = "allow", short = 'A')] + #[clap(long = "allow", short = 'A', value_delimiter = ',', num_args = 1..)] pub(crate) allow_warnings: Option, /// Path to a directory to write the console's internal logs to. diff --git a/tokio-console/tests/cli-ui.stdout b/tokio-console/tests/cli-ui.stdout index ff49fbc29..83cee3f4d 100644 --- a/tokio-console/tests/cli-ui.stdout +++ b/tokio-console/tests/cli-ui.stdout @@ -63,15 +63,16 @@ Options: This is a comma-separated list of warnings to allow. - Each warning is specified by its name, which is one of: * - `self-wakes` -- Warns when a task wakes itself more than a + Each warning is specified by its name, which is one of: + + * `self-wakes` -- Warns when a task wakes itself more than a certain percentage of its total wakeups. Default percentage is - 50%. * `lost-waker` -- Warns when a task is dropped without - being woken. + 50%. - * `never-yielded` -- Warns when a task has never yielded. + * `lost-waker` -- Warns when a task is dropped without being + woken. - [possible values: self-wakes, lost-waker, never-yielded] + * `never-yielded` -- Warns when a task has never yielded. --log-dir Path to a directory to write the console's internal logs to. From ea8cfdd058cee00f8b6f24f6bb1ea0d22fc576b2 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 7 Feb 2024 23:13:43 +0800 Subject: [PATCH 06/13] refactor: use fromstr trait Signed-off-by: hi-rustin --- tokio-console/src/config.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index 97071988e..9cdfb4a50 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -141,13 +141,15 @@ pub(crate) enum KnownWarnings { NeverYielded, } -impl From<&str> for KnownWarnings { - fn from(s: &str) -> Self { +impl FromStr for KnownWarnings { + type Err = String; + + fn from_str(s: &str) -> Result { match s { - "self-wakes" => KnownWarnings::SelfWakes, - "lost-waker" => KnownWarnings::LostWaker, - "never-yielded" => KnownWarnings::NeverYielded, - _ => panic!("unknown warning: {}", s), + "self-wakes" => Ok(KnownWarnings::SelfWakes), + "lost-waker" => Ok(KnownWarnings::LostWaker), + "never-yielded" => Ok(KnownWarnings::NeverYielded), + _ => Err(format!("unknown warning: {}", s)), } } } @@ -188,16 +190,19 @@ pub(crate) enum AllowedWarnings { Some(BTreeSet), } -impl From<&str> for AllowedWarnings { - fn from(s: &str) -> Self { +impl FromStr for AllowedWarnings { + type Err = String; + + fn from_str(s: &str) -> Result { match s { - "all" => AllowedWarnings::All, + "all" => Ok(AllowedWarnings::All), _ => { let warnings = s .split(',') - .map(KnownWarnings::from) - .collect::>(); - AllowedWarnings::Some(warnings) + .map(|s| s.parse::()) + .collect::, _>>() + .map_err(|err| format!("failed to parse warning: {}", err))?; + Ok(AllowedWarnings::Some(warnings)) } } } From 2e7f50da2bfe9ca9b5e26315d0533bae9719ed0c Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 7 Feb 2024 23:30:19 +0800 Subject: [PATCH 07/13] feat: add possible values Signed-off-by: hi-rustin --- README.md | 4 ++++ tokio-console/src/config.rs | 9 ++++++++- tokio-console/tests/cli-ui.stdout | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5160e15c1..3c94be843 100644 --- a/README.md +++ b/README.md @@ -236,6 +236,8 @@ Options: Each warning is specified by its name, which is one of: + * `all` -- Allow all warnings. + * `self-wakes` -- Warns when a task wakes itself more than a certain percentage of its total wakeups. Default percentage is 50%. @@ -244,6 +246,8 @@ Options: woken. * `never-yielded` -- Warns when a task has never yielded. + + [possible values: all, self-wakes, lost-waker, never-yielded] --log-dir Path to a directory to write the console's internal logs to. diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index 9cdfb4a50..058afbd6d 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -73,13 +73,15 @@ pub struct Config { /// /// Each warning is specified by its name, which is one of: /// + /// * `all` -- Allow all warnings. + /// /// * `self-wakes` -- Warns when a task wakes itself more than a certain percentage of its total wakeups. /// Default percentage is 50%. /// /// * `lost-waker` -- Warns when a task is dropped without being woken. /// /// * `never-yielded` -- Warns when a task has never yielded. - #[clap(long = "allow", short = 'A', value_delimiter = ',', num_args = 1..)] + #[clap(long = "allow", short = 'A', value_delimiter = ',', num_args = 1.., value_parser = PossibleValuesParser::new(AllowedWarnings::possible_values()))] pub(crate) allow_warnings: Option, /// Path to a directory to write the console's internal logs to. @@ -209,6 +211,11 @@ impl FromStr for AllowedWarnings { } impl AllowedWarnings { + fn possible_values() -> &'static [&'static str] { + // NOTE: Please keep this list in sync with the `KnownWarnings` enum. + &["all", "self-wakes", "lost-waker", "never-yielded"] + } + fn merge(&self, allowed: &Self) -> Self { match (self, allowed) { (AllowedWarnings::All, _) => AllowedWarnings::All, diff --git a/tokio-console/tests/cli-ui.stdout b/tokio-console/tests/cli-ui.stdout index 83cee3f4d..226680c5e 100644 --- a/tokio-console/tests/cli-ui.stdout +++ b/tokio-console/tests/cli-ui.stdout @@ -65,6 +65,8 @@ Options: Each warning is specified by its name, which is one of: + * `all` -- Allow all warnings. + * `self-wakes` -- Warns when a task wakes itself more than a certain percentage of its total wakeups. Default percentage is 50%. @@ -73,6 +75,8 @@ Options: woken. * `never-yielded` -- Warns when a task has never yielded. + + [possible values: all, self-wakes, lost-waker, never-yielded] --log-dir Path to a directory to write the console's internal logs to. From 608be2d3a2ca82f3183fdb63b9e930c95d205c39 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 7 Feb 2024 23:45:37 +0800 Subject: [PATCH 08/13] refactor: simplify code Signed-off-by: hi-rustin --- tokio-console/src/main.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/tokio-console/src/main.rs b/tokio-console/src/main.rs index 13b99c2ba..28f486f37 100644 --- a/tokio-console/src/main.rs +++ b/tokio-console/src/main.rs @@ -65,19 +65,14 @@ async fn main() -> color_eyre::Result<()> { let (update_tx, update_rx) = watch::channel(UpdateKind::Other); // A channel to send the task details update stream (no need to keep outdated details in the memory) let (details_tx, mut details_rx) = mpsc::channel::(2); - let warnings = match args.allow_warnings { Some(allow_warnings) => match allow_warnings { - AllowedWarnings::All => { - vec![] - } - AllowedWarnings::Some(allow_warnings) => { - let warnings = args - .warnings - .iter() - .filter(|lint| !allow_warnings.contains(lint)); - warnings.collect::>() - } + AllowedWarnings::All => vec![], + AllowedWarnings::Some(allow_warnings) => args + .warnings + .iter() + .filter(|lint| !allow_warnings.contains(lint)) + .collect::>(), }, None => args.warnings.iter().collect::>(), }; From f51897798df07000e9f40ba010938773f51dd33f Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Wed, 7 Feb 2024 23:48:22 +0800 Subject: [PATCH 09/13] docs: update comments Signed-off-by: hi-rustin --- README.md | 4 ++-- tokio-console/src/config.rs | 4 ++-- tokio-console/tests/cli-ui.stdout | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3c94be843..fe3f3d945 100644 --- a/README.md +++ b/README.md @@ -236,8 +236,6 @@ Options: Each warning is specified by its name, which is one of: - * `all` -- Allow all warnings. - * `self-wakes` -- Warns when a task wakes itself more than a certain percentage of its total wakeups. Default percentage is 50%. @@ -247,6 +245,8 @@ Options: * `never-yielded` -- Warns when a task has never yielded. + If this is set to `all`, all warnings are allowed. + [possible values: all, self-wakes, lost-waker, never-yielded] --log-dir diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index 058afbd6d..99ac05a75 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -73,14 +73,14 @@ pub struct Config { /// /// Each warning is specified by its name, which is one of: /// - /// * `all` -- Allow all warnings. - /// /// * `self-wakes` -- Warns when a task wakes itself more than a certain percentage of its total wakeups. /// Default percentage is 50%. /// /// * `lost-waker` -- Warns when a task is dropped without being woken. /// /// * `never-yielded` -- Warns when a task has never yielded. + /// + /// If this is set to `all`, all warnings are allowed. #[clap(long = "allow", short = 'A', value_delimiter = ',', num_args = 1.., value_parser = PossibleValuesParser::new(AllowedWarnings::possible_values()))] pub(crate) allow_warnings: Option, diff --git a/tokio-console/tests/cli-ui.stdout b/tokio-console/tests/cli-ui.stdout index 226680c5e..1db3373a2 100644 --- a/tokio-console/tests/cli-ui.stdout +++ b/tokio-console/tests/cli-ui.stdout @@ -65,8 +65,6 @@ Options: Each warning is specified by its name, which is one of: - * `all` -- Allow all warnings. - * `self-wakes` -- Warns when a task wakes itself more than a certain percentage of its total wakeups. Default percentage is 50%. @@ -76,6 +74,8 @@ Options: * `never-yielded` -- Warns when a task has never yielded. + If this is set to `all`, all warnings are allowed. + [possible values: all, self-wakes, lost-waker, never-yielded] --log-dir From 5f3ff2980127d9b9e4409ec59f4649b6620e10f8 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Thu, 8 Feb 2024 00:03:59 +0800 Subject: [PATCH 10/13] fix: add possible values Signed-off-by: hi-rustin --- tokio-console/src/config.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index 99ac05a75..427e104a6 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -81,7 +81,9 @@ pub struct Config { /// * `never-yielded` -- Warns when a task has never yielded. /// /// If this is set to `all`, all warnings are allowed. - #[clap(long = "allow", short = 'A', value_delimiter = ',', num_args = 1.., value_parser = PossibleValuesParser::new(AllowedWarnings::possible_values()))] + /// + /// [possible values: all, self-wakes, lost-waker, never-yielded] + #[clap(long = "allow", short = 'A', value_delimiter = ',', num_args = 1..)] pub(crate) allow_warnings: Option, /// Path to a directory to write the console's internal logs to. @@ -211,11 +213,6 @@ impl FromStr for AllowedWarnings { } impl AllowedWarnings { - fn possible_values() -> &'static [&'static str] { - // NOTE: Please keep this list in sync with the `KnownWarnings` enum. - &["all", "self-wakes", "lost-waker", "never-yielded"] - } - fn merge(&self, allowed: &Self) -> Self { match (self, allowed) { (AllowedWarnings::All, _) => AllowedWarnings::All, From 921e35d8a35594f13368025f7f5f46387df6b71e Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Thu, 8 Feb 2024 00:13:51 +0800 Subject: [PATCH 11/13] fix: pass the flags correctly Signed-off-by: hi-rustin --- tokio-console/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index 427e104a6..465dc41f8 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -83,7 +83,7 @@ pub struct Config { /// If this is set to `all`, all warnings are allowed. /// /// [possible values: all, self-wakes, lost-waker, never-yielded] - #[clap(long = "allow", short = 'A', value_delimiter = ',', num_args = 1..)] + #[clap(long = "allow", short = 'A', num_args = 1..)] pub(crate) allow_warnings: Option, /// Path to a directory to write the console's internal logs to. From e7a610eee802133e63e4a22c8e5ed2b44205feaf Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Sat, 10 Feb 2024 09:39:47 +0800 Subject: [PATCH 12/13] fix: address comments Signed-off-by: hi-rustin --- tokio-console/src/config.rs | 13 ++++++++----- tokio-console/src/main.rs | 14 ++++++-------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index 465dc41f8..bf4ee2608 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -187,11 +187,14 @@ impl KnownWarnings { ] } } - +/// An enum representing the types of warnings that are allowed. #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] pub(crate) enum AllowedWarnings { + /// Represents the case where all warnings are allowed. All, - Some(BTreeSet), + /// Represents the case where only some specific warnings are allowed. + /// The allowed warnings are stored in a `BTreeSet` of `KnownWarnings`. + Explicit(BTreeSet), } impl FromStr for AllowedWarnings { @@ -206,7 +209,7 @@ impl FromStr for AllowedWarnings { .map(|s| s.parse::()) .collect::, _>>() .map_err(|err| format!("failed to parse warning: {}", err))?; - Ok(AllowedWarnings::Some(warnings)) + Ok(AllowedWarnings::Explicit(warnings)) } } } @@ -217,10 +220,10 @@ impl AllowedWarnings { match (self, allowed) { (AllowedWarnings::All, _) => AllowedWarnings::All, (_, AllowedWarnings::All) => AllowedWarnings::All, - (AllowedWarnings::Some(a), AllowedWarnings::Some(b)) => { + (AllowedWarnings::Explicit(a), AllowedWarnings::Explicit(b)) => { let mut warnings = a.clone(); warnings.extend(b.clone()); - AllowedWarnings::Some(warnings) + AllowedWarnings::Explicit(warnings) } } } diff --git a/tokio-console/src/main.rs b/tokio-console/src/main.rs index 28f486f37..421c660a7 100644 --- a/tokio-console/src/main.rs +++ b/tokio-console/src/main.rs @@ -66,14 +66,12 @@ async fn main() -> color_eyre::Result<()> { // A channel to send the task details update stream (no need to keep outdated details in the memory) let (details_tx, mut details_rx) = mpsc::channel::(2); let warnings = match args.allow_warnings { - Some(allow_warnings) => match allow_warnings { - AllowedWarnings::All => vec![], - AllowedWarnings::Some(allow_warnings) => args - .warnings - .iter() - .filter(|lint| !allow_warnings.contains(lint)) - .collect::>(), - }, + Some(AllowedWarnings::All) => vec![], + Some(AllowedWarnings::Explicit(allow_warnings)) => args + .warnings + .iter() + .filter(|lint| !allow_warnings.contains(lint)) + .collect::>(), None => args.warnings.iter().collect::>(), }; From 6056e53631682074641f7e4357c24fca5ee3f0df Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 13 Feb 2024 00:22:19 +0800 Subject: [PATCH 13/13] docs: add comment Signed-off-by: hi-rustin --- tokio-console/src/config.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tokio-console/src/config.rs b/tokio-console/src/config.rs index bf4ee2608..5da8ae0c1 100644 --- a/tokio-console/src/config.rs +++ b/tokio-console/src/config.rs @@ -188,6 +188,8 @@ impl KnownWarnings { } } /// An enum representing the types of warnings that are allowed. +// Note: ValueEnum only supports unit variants, so we have to use a custom +// parser for this. #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] pub(crate) enum AllowedWarnings { /// Represents the case where all warnings are allowed.