aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/notifications/instrumenter.rb47
-rw-r--r--activesupport/lib/active_support/subscriber.rb7
2 files changed, 50 insertions, 4 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
diff --git a/activesupport/lib/active_support/subscriber.rb b/activesupport/lib/active_support/subscriber.rb
index 8ad39f7a05..54c770ac35 100644
--- a/activesupport/lib/active_support/subscriber.rb
+++ b/activesupport/lib/active_support/subscriber.rb
@@ -79,7 +79,8 @@ module ActiveSupport
end
def start(name, id, payload)
- e = ActiveSupport::Notifications::Event.new(name, Time.now, nil, id, payload)
+ e = ActiveSupport::Notifications::Event.new(name, nil, nil, id, payload)
+ e.start!
parent = event_stack.last
parent << e if parent
@@ -87,9 +88,8 @@ module ActiveSupport
end
def finish(name, id, payload)
- finished = Time.now
event = event_stack.pop
- event.end = finished
+ event.finish!
event.payload.merge!(payload)
method = name.split(".".freeze).first
@@ -97,7 +97,6 @@ module ActiveSupport
end
private
-
def event_stack
SubscriberQueueRegistry.instance.get_queue(@queue_key)
end