Skip to content

Add resource limits extensions for CPU, memory, and shared memory#331

Merged
tfoote merged 7 commits intoosrf:mainfrom
roman-y-wu:main
Aug 8, 2025
Merged

Add resource limits extensions for CPU, memory, and shared memory#331
tfoote merged 7 commits intoosrf:mainfrom
roman-y-wu:main

Conversation

@roman-y-wu
Copy link

  • Enhanced ShmSize extension with build-time shm-size support via --shm-size-build
  • Added CpuLimits extension providing --cpus argument for runtime CPU limits
  • Added MemoryLimits extension providing --memory/-m argument for runtime memory limits
  • Extended core infrastructure to support docker build arguments from extensions
  • Added comprehensive input validation for memory and CPU formats
  • Updated documentation and added test coverage for all new functionality
  • Improved .gitignore with comprehensive Python and development file patterns

These extensions expose docker build and runtime resource limit arguments (shm-size, cpus, memory) to rocker users, providing better control over container resource allocation.

- Enhanced ShmSize extension with build-time shm-size support via --shm-size-build
- Added CpuLimits extension providing --cpus argument for runtime CPU limits
- Added MemoryLimits extension providing --memory/-m argument for runtime memory limits
- Extended core infrastructure to support docker build arguments from extensions
- Added comprehensive input validation for memory and CPU formats
- Updated documentation and added test coverage for all new functionality
- Improved .gitignore with comprehensive Python and development file patterns

These extensions expose docker build and runtime resource limit arguments
(shm-size, cpus, memory) to rocker users, providing better control over
container resource allocation.
@roman-y-wu roman-y-wu requested a review from tfoote as a code owner July 25, 2025 02:04
Copy link
Collaborator

@tfoote tfoote left a comment

Choose a reason for hiding this comment

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

Thanks adding these has been on my wishlist for a while, aka #311 and #239

I have a few requests that I've put in line.

At a high level I think it might make sense to separate out the resource constrant based optoins into a separate constraint_extensions.py file since the extensions.py file is getting quite long. And the resource constraints is a well defined but still decently sized set of possibilities: https://docs.docker.com/engine/containers/resource_constraints/

If you can update this PR with those adjustments I'll be happy to land this.

.gitignore Outdated
dist
deb_dist
*.pyc
# Byte-compiled / optimized / DLL files
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please revert your changes to the .gitignore files. These look like good options for a global git config ignore on your system. We don't want to put ignores for all potential frameworks and tools into every project.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please revert these changes to the .gitignore


@staticmethod
def _validate_memory_format(value):
"""Validate memory format (e.g., '1g', '512m', '1024k')."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks incomplete. From https://docs.docker.com/reference/compose-file/build/#shm_size It refers to a formal "byte-values" definition which allows more variations: https://docs.docker.com/reference/compose-file/extension/#specifying-byte-values

Values express a byte value as a string in {amount}{byte unit} format: The supported units are b (bytes), k or kb (kilo bytes), m or mb (mega bytes) and g or gb (giga bytes).


    2b
    1024kb
    2048k
    300m
    1gb

It would be good to cover that spec as well as link to it for reference.

return args

@staticmethod
def _validate_memory_format(value):
Copy link
Collaborator

Choose a reason for hiding this comment

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

The memory argument validation routine should be reused and not duplicated, it's just a helper function that can be validated and tested separately and then just reused in the two different extensions and potentially in more in the future.

return value
except ValueError:
from argparse import ArgumentTypeError
raise ArgumentTypeError(f"Invalid CPU format: {value}. Expected a number (e.g., '1.5')")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
raise ArgumentTypeError(f"Invalid CPU format: {value}. Expected a number (e.g., '1.5')")
raise ArgumentTypeError(f"Invalid CPU format: {value}. Expected a floating point number (e.g., '1.5')")

@tfoote
Copy link
Collaborator

tfoote commented Jul 28, 2025

The CI looks to have failed, it might be related to the use of EOL images i've updated here: #332

If that's the issue a rebase will help.

roman-y-wu pushed a commit to roman-y-wu/rocker that referenced this pull request Jul 30, 2025
- Move ShmSize, CpuLimits, and MemoryLimits to separate constraint_extensions.py file
- Create reusable validate_memory_format() helper function to avoid code duplication
- Enhance memory validation to support Docker byte-value formats (2b, 1024kb, 300m, 1gb)
- Update CPU error message to "Expected a floating point number (e.g., '1.5')"
- Update setup.py entry points to reference new constraint_extensions module

Addresses all feedback from tfoote in PR osrf#331 review comments.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Move ShmSize, CpuLimits, and MemoryLimits to separate constraint_extensions.py file
- Create reusable validate_memory_format() helper function to avoid code duplication
- Enhance memory validation to support Docker byte-value formats (2b, 1024kb, 300m, 1gb)
- Update CPU error message to "Expected a floating point number (e.g., '1.5')"
- Update setup.py entry points to reference new constraint_extensions module

Addresses all feedback from tfoote in PR osrf#331 review comments.
Copy link
Collaborator

@tfoote tfoote left a comment

Choose a reason for hiding this comment

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

Thanks for the refactors it looks good with the new file and string validation.

Could you add one quick unittest for the validation function.

And with the source in a separate file it would be great to match that with splitting out the test cases into a separate file with the same name pattern.

And the .gitignore changes still need to be reverted.

With those changes this should be good to go. With the rebase I see that the tests are passing now.

Copy link
Collaborator

@tfoote tfoote left a comment

Choose a reason for hiding this comment

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

Thanks for moving the content and the tests to the new locations.

There's duplicate copies of the test now in the test_extension.py and please revert the .gitignore changes and this will be good to go.

.gitignore Outdated
dist
deb_dist
*.pyc
# Byte-compiled / optimized / DLL files
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please revert these changes to the .gitignore

self.assertEqual(build_args, {'shm_size': '2g'})


class CpuLimitsExtensionTest(unittest.TestCase):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is now in test_constraint_extensions.py please remove this duplicate.

self.assertIn('--cpus 2.5', args)


class MemoryLimitsExtensionTest(unittest.TestCase):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is now in test_constraint_extensions.py please remove this duplicate too.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the review!

Copy link
Collaborator

@tfoote tfoote left a comment

Choose a reason for hiding this comment

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

Thanks for iterating and adding these extra features.

@tfoote tfoote merged commit ce67dc6 into osrf:main Aug 8, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for shmem, memory, cpu limits for build stage Enable memory, cpu, and other limits on the container

2 participants