Skip to content

Commit 571779a

Browse files
authored
Merge pull request rails#43502 from Shopify/rework-process-clock-gettime-uses
Refactor `Process.clock_gettime` uses
2 parents 0983917 + 9636b4d commit 571779a

File tree

10 files changed

+38
-37
lines changed

10 files changed

+38
-37
lines changed

activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,13 @@ def attempt_to_checkout_all_existing_connections(raise_on_acquisition_timeout =
527527
end
528528

529529
newly_checked_out = []
530-
timeout_time = Concurrent.monotonic_time + (@checkout_timeout * 2)
530+
timeout_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) + (@checkout_timeout * 2)
531531

532532
@available.with_a_bias_for(Thread.current) do
533533
loop do
534534
synchronize do
535535
return if collected_conns.size == @connections.size && @now_connecting == 0
536-
remaining_timeout = timeout_time - Concurrent.monotonic_time
536+
remaining_timeout = timeout_time - Process.clock_gettime(Process::CLOCK_MONOTONIC)
537537
remaining_timeout = 0 if remaining_timeout < 0
538538
conn = checkout_for_exclusive_access(remaining_timeout)
539539
collected_conns << conn

activerecord/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def no_wait_poll
110110
def wait_poll(timeout)
111111
@num_waiting += 1
112112

113-
t0 = Concurrent.monotonic_time
113+
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
114114
elapsed = 0
115115
loop do
116116
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
@@ -119,7 +119,7 @@ def wait_poll(timeout)
119119

120120
return remove if any?
121121

122-
elapsed = Concurrent.monotonic_time - t0
122+
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0
123123
if elapsed >= timeout
124124
msg = "could not obtain a connection from the pool within %0.3f seconds (waited %0.3f seconds); all pooled connections were in use" %
125125
[timeout, elapsed]

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def initialize(connection, logger = nil, config = {}) # :nodoc:
8888
@logger = logger
8989
@config = config
9090
@pool = ActiveRecord::ConnectionAdapters::NullPool.new
91-
@idle_since = Concurrent.monotonic_time
91+
@idle_since = Process.clock_gettime(Process::CLOCK_MONOTONIC)
9292
@visitor = arel_visitor
9393
@statements = build_statement_pool
9494
@lock = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new
@@ -242,7 +242,7 @@ def expire
242242
"Current thread: #{Thread.current}."
243243
end
244244

245-
@idle_since = Concurrent.monotonic_time
245+
@idle_since = Process.clock_gettime(Process::CLOCK_MONOTONIC)
246246
@owner = nil
247247
else
248248
raise ActiveRecordError, "Cannot expire connection, it is not currently leased."
@@ -265,7 +265,7 @@ def steal! # :nodoc:
265265
# Seconds since this connection was returned to the pool
266266
def seconds_idle # :nodoc:
267267
return 0 if in_use?
268-
Concurrent.monotonic_time - @idle_since
268+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - @idle_since
269269
end
270270

271271
def unprepared_statement

activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def write_query?(sql) # :nodoc:
3030

3131
def explain(arel, binds = [])
3232
sql = "EXPLAIN #{to_sql(arel, binds)}"
33-
start = Concurrent.monotonic_time
33+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
3434
result = exec_query(sql, "EXPLAIN", binds)
35-
elapsed = Concurrent.monotonic_time - start
35+
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
3636

3737
MySQL::ExplainPrettyPrinter.new.pp(result, elapsed)
3838
end

activerecord/lib/active_record/future_result.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ def canceled?
102102

103103
def execute_or_wait
104104
if pending?
105-
start = Concurrent.monotonic_time
105+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
106106
@mutex.synchronize do
107107
if pending?
108108
execute_query(@pool.connection)
109109
else
110-
@lock_wait = (Concurrent.monotonic_time - start) * 1_000
110+
@lock_wait = (Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start)
111111
end
112112
end
113113
else

activerecord/test/cases/connection_pool_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ def test_idle_timeout_configuration
211211

212212
idle_conn.instance_variable_set(
213213
:@idle_since,
214-
Concurrent.monotonic_time - 0.01
214+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - 0.01
215215
)
216216

217217
@pool.flush
218218
assert_equal 1, @pool.connections.length
219219

220220
idle_conn.instance_variable_set(
221221
:@idle_since,
222-
Concurrent.monotonic_time - 0.02
222+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - 0.02
223223
)
224224

225225
@pool.flush
@@ -238,7 +238,7 @@ def test_disable_flush
238238

239239
idle_conn.instance_variable_set(
240240
:@idle_since,
241-
Concurrent.monotonic_time - 1
241+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - 1
242242
)
243243

244244
@pool.flush
@@ -257,7 +257,7 @@ def test_flush
257257

258258
idle_conn.instance_variable_set(
259259
:@idle_since,
260-
Concurrent.monotonic_time - 1000
260+
Process.clock_gettime(Process::CLOCK_MONOTONIC) - 1000
261261
)
262262

263263
@pool.flush(30)

activesupport/lib/active_support/cache/memory_store.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ def prune(target_size, max_time = nil)
8989
return if pruning?
9090
@pruning = true
9191
begin
92-
start_time = Concurrent.monotonic_time
92+
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
9393
cleanup
9494
instrument(:prune, target_size, from: @cache_size) do
9595
keys = synchronize { @data.keys }
9696
keys.each do |key|
9797
delete_entry(key, **options)
98-
return if @cache_size <= target_size || (max_time && Concurrent.monotonic_time - start_time > max_time)
98+
return if @cache_size <= target_size || (max_time && Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time > max_time)
9999
end
100100
end
101101
ensure

activesupport/lib/active_support/notifications/fanout.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ def publish(name, *args)
236236

237237
def start(name, id, payload)
238238
timestack = Thread.current[:_timestack_monotonic] ||= []
239-
timestack.push Concurrent.monotonic_time
239+
timestack.push Process.clock_gettime(Process::CLOCK_MONOTONIC)
240240
end
241241

242242
def finish(name, id, payload)
243243
timestack = Thread.current[:_timestack_monotonic]
244244
started = timestack.pop
245-
@delegate.call(name, started, Concurrent.monotonic_time, id, payload)
245+
@delegate.call(name, started, Process.clock_gettime(Process::CLOCK_MONOTONIC), id, payload)
246246
end
247247
end
248248

activesupport/lib/active_support/notifications/instrumenter.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ class Event
6262
def initialize(name, start, ending, transaction_id, payload)
6363
@name = name
6464
@payload = payload.dup
65-
@time = start
65+
@time = start ? start.to_f * 1_000.0 : start
6666
@transaction_id = transaction_id
67-
@end = ending
67+
@end = ending ? ending.to_f * 1_000.0 : ending
6868
@children = []
69-
@cpu_time_start = 0
70-
@cpu_time_finish = 0
69+
@cpu_time_start = 0.0
70+
@cpu_time_finish = 0.0
7171
@allocation_count_start = 0
7272
@allocation_count_finish = 0
7373
end
@@ -102,7 +102,7 @@ def finish!
102102
# Returns the CPU time (in milliseconds) passed since the call to
103103
# +start!+ and the call to +finish!+
104104
def cpu_time
105-
(@cpu_time_finish - @cpu_time_start) * 1000
105+
@cpu_time_finish - @cpu_time_start
106106
end
107107

108108
# Returns the idle time time (in milliseconds) passed since the call to
@@ -130,7 +130,7 @@ def allocations
130130
#
131131
# @event.duration # => 1000.138
132132
def duration
133-
1000.0 * (self.end - time)
133+
self.end - time
134134
end
135135

136136
def <<(event)
@@ -143,28 +143,30 @@ def parent_of?(event)
143143

144144
private
145145
def now
146-
Concurrent.monotonic_time
146+
Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
147147
end
148148

149149
begin
150-
Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID)
150+
Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :float_millisecond)
151151

152152
def now_cpu
153-
Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID)
153+
Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID, :float_millisecond)
154154
end
155155
rescue
156156
def now_cpu
157-
0
157+
0.0
158158
end
159159
end
160160

161-
if defined?(JRUBY_VERSION)
161+
begin
162+
GC.stat(:total_allocated_objects)
163+
rescue ArgumentError # Likely on JRuby
162164
def now_allocations
163165
0
164166
end
165167
else
166168
def now_allocations
167-
GC.stat :total_allocated_objects
169+
GC.stat(:total_allocated_objects)
168170
end
169171
end
170172
end

activesupport/test/notifications_test.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,11 @@ def test_event_is_pushed_even_without_block
434434

435435
class EventTest < TestCase
436436
def test_events_are_initialized_with_details
437-
time = Time.now
437+
time = Time.now.to_f
438438
event = event(:foo, time, time + 0.01, random_id, {})
439439

440440
assert_equal :foo, event.name
441-
assert_equal time, event.time
442-
assert_in_delta 10.0, event.duration, 0.00001
441+
assert_in_delta 10.0, event.duration, 0.0001
443442
end
444443

445444
def test_event_cpu_time_does_not_raise_error_when_start_or_finished_not_called
@@ -450,14 +449,14 @@ def test_event_cpu_time_does_not_raise_error_when_start_or_finished_not_called
450449
end
451450

452451
def test_events_consumes_information_given_as_payload
453-
event = event(:foo, Concurrent.monotonic_time, Concurrent.monotonic_time + 1, random_id, payload: :bar)
452+
event = event(:foo, Process.clock_gettime(Process::CLOCK_MONOTONIC), Process.clock_gettime(Process::CLOCK_MONOTONIC) + 1, random_id, payload: :bar)
454453
assert_equal Hash[payload: :bar], event.payload
455454
end
456455

457456
def test_event_is_parent_based_on_children
458-
time = Concurrent.monotonic_time
457+
time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
459458

460-
parent = event(:foo, Concurrent.monotonic_time, Concurrent.monotonic_time + 100, random_id, {})
459+
parent = event(:foo, Process.clock_gettime(Process::CLOCK_MONOTONIC), Process.clock_gettime(Process::CLOCK_MONOTONIC) + 100, random_id, {})
461460
child = event(:foo, time, time + 10, random_id, {})
462461
not_child = event(:foo, time, time + 100, random_id, {})
463462

0 commit comments

Comments
 (0)