Skip to content

Commit

Permalink
Fix memory stats collection during submission running
Browse files Browse the repository at this point in the history
  • Loading branch information
chvp committed Jul 4, 2023
1 parent 03654a6 commit 468a63a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions app/runners/submission_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,17 @@ def execute

timer = Thread.new do
while Time.zone.now - before_time < time_limit
sleep 0.2
next if Rails.env.test?
# Check if container is still alive
next unless Docker::Container.all.any? { |c| c.id.starts_with?(container.id) || container.id.starts_with?(container.id) } && container.refresh!.info['State']['Running']

stats = container.stats
# We check the maximum memory usage every 200ms. This is obviously monotonic, but these stats aren't available after the container is/has stopped.
memory = stats['memory_stats']['max_usage'] / (1024.0 * 1024.0) if stats['memory_stats']&.fetch('max_usage', nil)
before_stats = Time.zone.now
# Check if container is still running
if !Rails.env.test? && (Docker::Container.all.select { |c| c.id.starts_with?(container.id) || container.id.starts_with?(container.id) }.any? && container.refresh!.info['State']['Running'])
# If we don't pass these extra options gathering stats takes 1+ seconds (https://github.com/moby/moby/issues/23188#issuecomment-223211481)
stats = container.stats({ 'one-shot': true, stream: false })
memory = [stats['memory_stats']['usage'] / (1024.0 * 1024.0), memory].max if stats['memory_stats']&.fetch('usage', nil)

Check warning on line 170 in app/runners/submission_runner.rb

View check run for this annotation

Codecov / codecov/patch

app/runners/submission_runner.rb#L169-L170

Added lines #L169 - L170 were not covered by tests
end

# Gathering stats still takes a long time, so if we spent enough time on
# that (aka, it didn't go wrong), skip sleeping
sleep 0.2 if (Time.zone.now - before_stats).in_milliseconds < 200
end
timeout_mutex.synchronize do
container.stop
Expand Down

0 comments on commit 468a63a

Please sign in to comment.