aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/notifications
diff options
context:
space:
mode:
authorEileen Uchitelle <eileencodes@gmail.com>2018-07-26 12:55:59 -0400
committerEileen Uchitelle <eileencodes@gmail.com>2018-07-26 13:57:51 -0400
commit42fec4b8de8c40d7778f936e200081c0dded1ed0 (patch)
treeb8d270fd84dab28436b934cb35285b024165ee2d /activesupport/lib/active_support/notifications
parent15a72c6c05cfc5250ee04742b4f45463f937d3f7 (diff)
downloadrails-42fec4b8de8c40d7778f936e200081c0dded1ed0.tar.gz
rails-42fec4b8de8c40d7778f936e200081c0dded1ed0.tar.bz2
rails-42fec4b8de8c40d7778f936e200081c0dded1ed0.zip
Add cpu_time, idle_time, and allocations to Event
* Use process clock instead of Time.now This fixes any issues with the system clock changing and also eliminates 2 object allocations per event. * Add start! and finish! methods to the event object so we can record more information * Adds cpu time, idle time, and allocation count for a particular event. Co-authored-by: Aaron Patterson <aaron.patterson@gmail.com>
Diffstat (limited to 'activesupport/lib/active_support/notifications')
-rw-r--r--activesupport/lib/active_support/notifications/instrumenter.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb
index e99f5ee688..ddcd1661e4 100644
--- a/activesupport/lib/active_support/notifications/instrumenter.rb
+++ b/activesupport/lib/active_support/notifications/instrumenter.rb
@@ -63,6 +63,34 @@ module ActiveSupport
@end = ending
@children = []
@duration = nil
+ @cpu_time_start = nil
+ @cpu_time_finish = nil
+ @allocation_count_start = 0
+ @allocation_count_finish = 0
+ end
+
+ def start!
+ @time = now
+ @cpu_time_start = now_cpu
+ @allocation_count_start = now_allocations
+ end
+
+ def finish!
+ @end = now
+ @cpu_time_finish = now_cpu
+ @allocation_count_finish = now_allocations
+ end
+
+ def cpu_time
+ @cpu_time_finish - @cpu_time_start
+ end
+
+ def idle_time
+ duration - cpu_time
+ end
+
+ def allocations
+ @allocation_count_finish - @allocation_count_start
end
# Returns the difference in milliseconds between when the execution of the
@@ -88,6 +116,25 @@ module ActiveSupport
def parent_of?(event)
@children.include? event
end
+
+ private
+ def now
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
+ end
+
+ def now_cpu
+ Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID)
+ end
+
+ if defined?(JRUBY_VERSION)
+ def now_allocations
+ 0
+ end
+ else
+ def now_allocations
+ GC.stat :total_allocated_objects
+ end
+ end
end
end
end