Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Times built-in #351

Merged
merged 5 commits into from
Mar 12, 2024
Merged

Times built-in #351

merged 5 commits into from
Mar 12, 2024

Conversation

magicant
Copy link
Owner

@magicant magicant commented Mar 10, 2024

Summary by CodeRabbit

  • New Features
    • Added a times built-in command to the Yash shell for tracking and displaying accumulated user and system times.
    • Introduced formatting capabilities for the times command output, ensuring times are displayed in a human-readable format.
    • Enhanced the Yash shell's environment handling to support time tracking, including the addition of a Times struct for representing CPU time values and methods for retrieving these values from the system.

@magicant magicant self-assigned this Mar 10, 2024
@magicant magicant added the enhancement New feature or request label Mar 10, 2024
@magicant magicant added this to the POSIX 2018 scripting milestone Mar 10, 2024
Copy link

coderabbitai bot commented Mar 10, 2024

Walkthrough

The Yash shell has been enhanced with a new times module, aimed at improving time tracking and execution monitoring. This addition includes a built-in command for displaying user and system times, a formatting mechanism for outputting these times in a human-readable format, and syntax parsing for command line arguments. Furthermore, the underlying system infrastructure has been updated to support these features, including the introduction of a Times struct for representing CPU time values and modifications to both real and virtual system implementations to retrieve and store these values.

Changes

Files Change Summaries
yash-builtin/src/times/format.rs Provides functionality to format the result of the times built-in command in Yash shell.
yash-env/src/system.rs Added a new method times to the System trait to return consumed CPU times.
yash-env/src/system/real.rs Added a new method times to retrieve system times using the nix library in RealSystem implementation.

🐰✨

In the shell of Yash, a new module blooms,
Tracking time as the code consumes.
Ticks to times, in format so clear,
A rabbit's work, for all to cheer.
🕒🎉
With syntax parsed, and systems aligned,
A leap in tech, that's one of a kind.
🐾

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5d57650 and 9ba0755.
Files selected for processing (7)
  • yash-builtin/src/lib.rs (2 hunks)
  • yash-builtin/src/times.rs (1 hunks)
  • yash-builtin/src/times/format.rs (1 hunks)
  • yash-builtin/src/times/syntax.rs (1 hunks)
  • yash-env/src/system.rs (3 hunks)
  • yash-env/src/system/real.rs (3 hunks)
  • yash-env/src/system/virtual.rs (4 hunks)
Additional comments: 12
yash-builtin/src/times/syntax.rs (3)
  • 27-37: The Error enum design for handling parsing errors in the times built-in command is well-structured and follows Rust's standard practices for error handling.
  • 39-55: The implementation of MessageBase for Error is correctly done, providing clear and concise error messages and annotations for different types of parsing errors.
  • 57-67: The parse function is well-implemented, correctly handling the parsing of command line arguments for the times built-in command, including the validation of no operands and options.
yash-builtin/src/times.rs (1)
  • 82-95: The main function for the times built-in command is correctly implemented, effectively parsing command line arguments, retrieving system times, formatting the output, and handling errors.
yash-builtin/src/times/format.rs (1)
  • 55-108: The tests for the formatting functions in the times module are comprehensive and well-structured, covering a wide range of scenarios to ensure correctness.
yash-builtin/src/lib.rs (2)
  • 78-79: The addition of the times module to the list of built-in command modules is correctly done.
  • 269-275: The integration of the times built-in command into the BUILTINS array is correctly done, maintaining the alphabetical order and specifying the appropriate execute function.
yash-env/src/system/real.rs (1)
  • 265-286: The implementation of the times method in the RealSystem is correctly done, using safe practices for uninitialized memory, error handling, and type conversions to retrieve and return system times.
yash-env/src/system.rs (1)
  • 403-425: The definition of the Times struct is well-structured and aligns with the PR objectives by representing consumed CPU time values. The inclusion of fields for both the current process and its children, as well as the ticks_per_second field for converting clock ticks to seconds, is thoughtful and comprehensive. This struct will serve as the return type for the times method, facilitating detailed time tracking and analysis.

One minor suggestion for improvement is to include documentation examples demonstrating how to use the Times struct, especially the conversion of clock ticks to seconds. This would enhance the usability and understandability of the new feature for developers and users alike.

Consider adding documentation examples that demonstrate how to use the Times struct, particularly focusing on converting clock ticks to seconds. This will improve the usability and clarity of the new time tracking functionality.

yash-env/src/system/virtual.rs (3)
  • 68-68: The import of the Times struct is correctly added to support the new time tracking functionality. This ensures that the Times struct is accessible within this module for implementing the associated logic.
  • 1055-1055: The addition of the times field in the SystemState struct is crucial for storing consumed CPU time. This aligns with the PR objectives to enhance time tracking capabilities within the Yash shell. It's important to ensure that this field is properly initialized and managed throughout the system's lifecycle to accurately reflect CPU time consumption.
  • 619-622: The implementation of the times method in the VirtualSystem struct is a key component of the new time tracking functionality. This method provides a way to retrieve the consumed CPU time, encapsulated within the Times struct, from the SystemState. It's essential that this method correctly handles any potential errors and returns the Times struct as expected. The current implementation directly returns the times field from SystemState, assuming successful retrieval. Given the context of a virtual system primarily used for testing, this straightforward approach seems appropriate. However, in a more complex or real-world scenario, additional error handling or validation might be necessary.

Comment on lines +199 to +200
/// Returns consumed CPU times.
fn times(&self) -> nix::Result<Times>;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition of the times method to the System trait is a crucial enhancement for supporting the new time tracking functionality. This method is expected to return consumed CPU times, which aligns with the PR objectives. However, it's important to ensure that all implementors of the System trait provide a meaningful implementation of this method to avoid runtime errors or unimplemented method panics.

Ensure that all concrete implementations of the System trait (e.g., RealSystem, VirtualSystem) provide an implementation for the times method. This is crucial for maintaining the integrity of the system and avoiding runtime issues.

Comment on lines 21 to 33
/// Formats a single time.
///
/// This function panics if the `ticks_per_second` is zero.
fn format_one_time<W>(ticks: u64, ticks_per_second: u64, result: &mut W) -> std::fmt::Result
where
W: std::fmt::Write,
{
let seconds = ticks / ticks_per_second;
let minutes = seconds / 60;
let sub_minute_ticks = ticks - minutes * 60 * ticks_per_second;
let seconds = sub_minute_ticks as f64 / ticks_per_second as f64;
write!(result, "{minutes}m{seconds:.6}s")
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format_one_time function correctly formats time values but panics if ticks_per_second is zero. Consider handling this case more gracefully, perhaps by returning an error, to improve robustness.

yash-builtin/src/times/format.rs Outdated Show resolved Hide resolved
This change makes the `Times` struct contain seconds in f64 instead of
clock ticks in u64.

The `tms` struct returned by the underlying `times` function contains
values in `clock_t`. The previous implementation assumed that it is an
integral type, but POSIX allows it to be a floating-point type. This
change makes the `Times` struct use f64 for all time values, which
can be cast from any types `clock_t` may be.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9ba0755 and 907f500.
Files selected for processing (3)
  • yash-builtin/src/times/format.rs (1 hunks)
  • yash-env/src/system.rs (3 hunks)
  • yash-env/src/system/real.rs (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • yash-builtin/src/times/format.rs
  • yash-env/src/system.rs
Additional comments: 3
yash-env/src/system/real.rs (3)
  • 36-36: The import of the Times struct indicates its use within this file, likely for representing time values related to the new times method. Ensure that the Times struct is well-documented and its fields are appropriately named to reflect their purpose clearly.
  • 58-58: The use of MaybeUninit is appropriate for handling uninitialized data structures in Rust, especially when dealing with FFI. This usage is a good practice for safely managing memory that will be initialized by external library calls.
  • 262-288: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-284]

Overall, the file follows Rust best practices for FFI and system interactions. The new times method is correctly implemented with appropriate safety checks. Ensure that all new code is covered by unit tests, especially to verify the correct behavior of the times method under various system conditions.

yash-env/src/system/real.rs Show resolved Hide resolved
@magicant magicant merged commit 43432a2 into master Mar 12, 2024
5 checks passed
@magicant magicant deleted the times-builtin branch March 12, 2024 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant