mirror of
https://github.com/nushell/nushell.git
synced 2025-06-08 19:17:16 +02:00
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> This PR adds a flag to `debug profile` to output the duration field as Value::Duration. Without the flag, the behavior is same as before: a column named `duration_ms` which is `Value::Float`. With the flag, there is instead a column named just `duration` which is `Value::Duration`. Additionally, this PR changes the time tracking to use nanoseconds instead of float seconds, so it can be output as either milliseconds or `Duration` (which uses nanoseconds internally). I don't think overflow is a concern here, because the maximum amount of time a `Duration` can store is over 292 years, and if a Nushell instruction takes longer than that to run then I think we might have bigger issues. # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> * Adds a flag `--duration-values` to `debug profile` which the `duration_ms` field in `debug profile` to a `duration` field which uses proper `duration` values. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --> N/A
182 lines
7.9 KiB
Rust
182 lines
7.9 KiB
Rust
use nu_engine::{command_prelude::*, ClosureEvalOnce};
|
|
use nu_protocol::{
|
|
debugger::{DurationMode, Profiler, ProfilerOptions},
|
|
engine::Closure,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct DebugProfile;
|
|
|
|
impl Command for DebugProfile {
|
|
fn name(&self) -> &str {
|
|
"debug profile"
|
|
}
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
|
Signature::build("debug profile")
|
|
.required(
|
|
"closure",
|
|
SyntaxShape::Closure(None),
|
|
"The closure to profile.",
|
|
)
|
|
.switch("spans", "Collect spans of profiled elements", Some('s'))
|
|
.switch(
|
|
"expand-source",
|
|
"Collect full source fragments of profiled elements",
|
|
Some('e'),
|
|
)
|
|
.switch(
|
|
"values",
|
|
"Collect pipeline element output values",
|
|
Some('v'),
|
|
)
|
|
.switch("lines", "Collect line numbers", Some('l'))
|
|
.switch(
|
|
"duration-values",
|
|
"Report instruction duration as duration values rather than milliseconds",
|
|
Some('d'),
|
|
)
|
|
.named(
|
|
"max-depth",
|
|
SyntaxShape::Int,
|
|
"How many blocks/closures deep to step into (default 2)",
|
|
Some('m'),
|
|
)
|
|
.input_output_types(vec![(Type::Any, Type::table())])
|
|
.category(Category::Debug)
|
|
}
|
|
|
|
fn description(&self) -> &str {
|
|
"Profile pipeline elements in a closure."
|
|
}
|
|
|
|
fn extra_description(&self) -> &str {
|
|
r#"The profiler profiles every evaluated instruction inside a closure, stepping into all
|
|
commands calls and other blocks/closures.
|
|
|
|
The output can be heavily customized. By default, the following columns are included:
|
|
- depth : Depth of the instruction. Each entered block adds one level of depth. How many
|
|
blocks deep to step into is controlled with the --max-depth option.
|
|
- id : ID of the instruction
|
|
- parent_id : ID of the instruction that created the parent scope
|
|
- source : Source code that generated the instruction. If the source code has multiple lines,
|
|
only the first line is used and `...` is appended to the end. Full source code can
|
|
be shown with the --expand-source flag.
|
|
- pc : The index of the instruction within the block.
|
|
- instruction : The pretty printed instruction being evaluated.
|
|
- duration : How long it took to run the instruction.
|
|
- (optional) span : Span associated with the instruction. Can be viewed via the `view span`
|
|
command. Enabled with the --spans flag.
|
|
- (optional) output : The output value of the instruction. Enabled with the --values flag.
|
|
|
|
To illustrate the depth and IDs, consider `debug profile { do { if true { echo 'spam' } } }`. A unique ID is generated each time an instruction is executed, and there are two levels of depth:
|
|
|
|
```
|
|
depth id parent_id source pc instruction
|
|
0 0 0 debug profile { do { if true { 'spam' } } } 0 <start>
|
|
1 1 0 { if true { 'spam' } } 0 load-literal %1, closure(2164)
|
|
1 2 0 { if true { 'spam' } } 1 push-positional %1
|
|
1 3 0 { do { if true { 'spam' } } } 2 redirect-out caller
|
|
1 4 0 { do { if true { 'spam' } } } 3 redirect-err caller
|
|
1 5 0 do 4 call decl 7 "do", %0
|
|
2 6 5 true 0 load-literal %1, bool(true)
|
|
2 7 5 if 1 not %1
|
|
2 8 5 if 2 branch-if %1, 5
|
|
2 9 5 'spam' 3 load-literal %0, string("spam")
|
|
2 10 5 if 4 jump 6
|
|
2 11 5 { if true { 'spam' } } 6 return %0
|
|
1 12 0 { do { if true { 'spam' } } } 5 return %0
|
|
```
|
|
|
|
Each block entered increments depth by 1 and each block left decrements it by one. This way you can
|
|
control the profiling granularity. Passing --max-depth=1 to the above would stop inside the `do`
|
|
at `if true { 'spam' }`. The id is used to identify each element. The parent_id tells you that the
|
|
instructions inside the block are being executed because of `do` (5), which in turn was spawned from
|
|
the root `debug profile { ... }`.
|
|
|
|
For a better understanding of how instructions map to source code, see the `view ir` command.
|
|
|
|
Note: In some cases, the ordering of pipeline elements might not be intuitive. For example,
|
|
`[ a bb cc ] | each { $in | str length }` involves some implicit collects and lazy evaluation
|
|
confusing the id/parent_id hierarchy. The --expr flag is helpful for investigating these issues."#
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let closure: Closure = call.req(engine_state, stack, 0)?;
|
|
let collect_spans = call.has_flag(engine_state, stack, "spans")?;
|
|
let collect_expanded_source = call.has_flag(engine_state, stack, "expanded-source")?;
|
|
let collect_values = call.has_flag(engine_state, stack, "values")?;
|
|
let collect_lines = call.has_flag(engine_state, stack, "lines")?;
|
|
let duration_values = call.has_flag(engine_state, stack, "duration-values")?;
|
|
let max_depth = call
|
|
.get_flag(engine_state, stack, "max-depth")?
|
|
.unwrap_or(2);
|
|
|
|
let duration_mode = match duration_values {
|
|
true => DurationMode::Value,
|
|
false => DurationMode::Milliseconds,
|
|
};
|
|
let profiler = Profiler::new(
|
|
ProfilerOptions {
|
|
max_depth,
|
|
collect_spans,
|
|
collect_source: true,
|
|
collect_expanded_source,
|
|
collect_values,
|
|
collect_exprs: false,
|
|
collect_instructions: true,
|
|
collect_lines,
|
|
duration_mode,
|
|
},
|
|
call.span(),
|
|
);
|
|
|
|
let lock_err = |_| ShellError::GenericError {
|
|
error: "Profiler Error".to_string(),
|
|
msg: "could not lock debugger, poisoned mutex".to_string(),
|
|
span: Some(call.head),
|
|
help: None,
|
|
inner: vec![],
|
|
};
|
|
|
|
engine_state
|
|
.activate_debugger(Box::new(profiler))
|
|
.map_err(lock_err)?;
|
|
|
|
let result = ClosureEvalOnce::new(engine_state, stack, closure).run_with_input(input);
|
|
|
|
// Return potential errors
|
|
let pipeline_data = result?;
|
|
|
|
// Collect the output
|
|
let _ = pipeline_data.into_value(call.span());
|
|
|
|
Ok(engine_state
|
|
.deactivate_debugger()
|
|
.map_err(lock_err)?
|
|
.report(engine_state, call.span())?
|
|
.into_pipeline_data())
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Profile config evaluation",
|
|
example: "debug profile { source $nu.config-path }",
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Profile config evaluation with more granularity",
|
|
example: "debug profile { source $nu.config-path } --max-depth 4",
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
}
|