Skip to content

Commit

Permalink
Never report negative idle time
Browse files Browse the repository at this point in the history
We calculate idle time by subtracting two separately-obtained values,
and apparently it's possible for the CPU-time clock to tick a
millisecond ahead of the monotonic clock.

I think it's safe to ignore that potential difference in general, but
even at the extreme of CPU activity, I'm reasonably confident time
doesn't move backwards.
  • Loading branch information
matthewd committed Jan 28, 2023
1 parent fcbf293 commit c285f36
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
Expand Up @@ -155,7 +155,8 @@ def cpu_time
# Returns the idle time time (in milliseconds) passed since the call to
# +start!+ and the call to +finish!+
def idle_time
duration - cpu_time
diff = duration - cpu_time
diff > 0.0 ? diff : 0.0
end

# Returns the number of allocations made since the call to +start!+ and
Expand Down
2 changes: 1 addition & 1 deletion activesupport/test/log_subscriber_test.rb
Expand Up @@ -111,7 +111,7 @@ def test_event_attributes
assert_operator event.allocations, :>, 0
end
assert_operator event.duration, :>, 0
assert_operator event.idle_time, :>, 0
assert_operator event.idle_time, :>=, 0
end

def test_does_not_send_the_event_if_it_doesnt_match_the_class
Expand Down
2 changes: 1 addition & 1 deletion activesupport/test/notifications_test.rb
Expand Up @@ -37,7 +37,7 @@ def test_subscribe_events
assert event, "should have an event"
assert_operator event.allocations, :>, 0
assert_operator event.cpu_time, :>, 0
assert_operator event.idle_time, :>, 0
assert_operator event.idle_time, :>=, 0
assert_operator event.duration, :>, 0
end

Expand Down

0 comments on commit c285f36

Please sign in to comment.