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

fix(console): fix calculation of busy time during poll #405

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions tokio-console/src/state/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,17 @@ impl Task {
}

pub(crate) fn busy(&self, since: SystemTime) -> Duration {
if let (Some(last_poll_started), None) =
(self.stats.last_poll_started, self.stats.last_poll_ended)
{
let busy_time_in_poll = |started| {
// in this case the task is being polled at the moment
let current_time_in_poll = since.duration_since(last_poll_started).unwrap_or_default();
return self.stats.busy + current_time_in_poll;
let current_time_in_poll = since.duration_since(started).unwrap_or_default();
self.stats.busy + current_time_in_poll
};

match (self.stats.last_poll_started, self.stats.last_poll_ended) {
(Some(started), Some(ended)) if started > ended => busy_time_in_poll(started),
(Some(started), _) => busy_time_in_poll(started),
Copy link
Member

Choose a reason for hiding this comment

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

fwiw, i think we could combine these two match arms, since Option<T> also implements PartialOrd when T: PartialOrd, and None compares less than Some...so we could write

Suggested change
(Some(started), Some(ended)) if started > ended => busy_time_in_poll(started),
(Some(started), _) => busy_time_in_poll(started),
(Some(started), ended) if Some(started) > ended => busy_time_in_poll(started),

but on the other hand, I think having two match arms might be more understandable, so I'm not sure if this change would actually be better...

Copy link
Member

Choose a reason for hiding this comment

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

actually, thinking about it, this would allow us to get rid of the busy_time_in_poll closure, so this whole function could be

    pub(crate) fn busy(&self, since: SystemTime) -> Duration {
        if self.started > self.ended {
            debug_assert!(
                self.started.is_some(),
                "if `started` is greater than `ended`, it should always be `Some`",
            );
            if let Some(started) = started {
                // the task is currently being polled
                let current_time_in_poll = since.duration_since(started).unwrap_or_default();
                return self.stats.busy + current_time_in_poll;
            }
        }

        self.stats.busy
    }

or something...do you think that's substantially clearer?

Copy link
Member

Choose a reason for hiding this comment

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

the debug_assert! could also just be an expect:

    pub(crate) fn busy(&self, since: SystemTime) -> Duration {
        if self.started > self.ended {
            // the task is currently being polled
            let started = self.started
                .expect("if `started` is greater than `ended`, it should always be `Some`");
            let current_time_in_poll = since.duration_since(started).unwrap_or_default();
            return self.stats.busy + current_time_in_poll;
        }

        self.stats.busy
    }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What about this variant on your second option? It's a little weird to safely unwrap the started time and then use the Option, but it seemed a bit cleaner and doesn't require the .expect or the debug_assert!.

    pub(crate) fn busy(&self, since: SystemTime) -> Duration {
        if let Some(started) = self.stats.last_poll_started {
            if self.stats.last_poll_started > self.stats.last_poll_ended {
                // in this case the task is being polled at the moment
                let current_time_in_poll = since.duration_since(started).unwrap_or_default();
                return self.stats.busy + current_time_in_poll;
            }
        }
        self.stats.busy
    }

Copy link
Member

Choose a reason for hiding this comment

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

ah, yeah, that's nicer. good call.

_ => self.stats.busy,
}
self.stats.busy
}

pub(crate) fn idle(&self, since: SystemTime) -> Duration {
Expand Down