Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory stats collection during submission running #4807

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.any? { |c| c.id.starts_with?(container.id) || container.id.starts_with?(container.id) } && 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)
end

# Gathering stats still takes a long time, so if we spent enough time on
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it takes a long time, is there a performance impact (since we now do it 5 times more often)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not noticeably on my machine. The runtime is because docker does some sleeps internally to gather the data, IIUC.

# 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