|
| 1 | +#################################### |
| 2 | + CPU and Memory Resource Limits |
| 3 | +#################################### |
| 4 | + |
| 5 | +Overview |
| 6 | +======== |
| 7 | + |
| 8 | +This feature adds the ability to specify CPU and memory limits for Docker containers in buildrunner configurations. These limits are passed to Docker's ``create_host_config`` method, allowing fine-grained control over container resource usage. |
| 9 | + |
| 10 | +.. contents:: |
| 11 | + :local: |
| 12 | + |
| 13 | +Configuration Options |
| 14 | +===================== |
| 15 | + |
| 16 | +The following resource limit fields are available for both run steps and service containers: |
| 17 | + |
| 18 | +- ``mem-limit``: Memory limit (string like "512m", "1g" or integer in bytes) |
| 19 | +- ``cpu-shares``: CPU shares (relative weight, default is 1024) |
| 20 | +- ``cpu-period``: CPU period in microseconds (default is 100000) |
| 21 | +- ``cpu-quota``: CPU quota in microseconds |
| 22 | + |
| 23 | +Usage Examples |
| 24 | +============== |
| 25 | + |
| 26 | +Memory Limit |
| 27 | +------------ |
| 28 | + |
| 29 | +.. code:: yaml |
| 30 | +
|
| 31 | + steps: |
| 32 | + my-step: |
| 33 | + run: |
| 34 | + image: ubuntu:20.04 |
| 35 | + mem-limit: "512m" # or 536870912 for bytes |
| 36 | + cmd: echo "Running with memory limit" |
| 37 | +
|
| 38 | +CPU Limits |
| 39 | +---------- |
| 40 | + |
| 41 | +.. code:: yaml |
| 42 | +
|
| 43 | + steps: |
| 44 | + my-step: |
| 45 | + run: |
| 46 | + image: ubuntu:20.04 |
| 47 | + cpu-shares: 512 # Relative weight (default 1024) |
| 48 | + cpu-period: 100000 # Period in microseconds |
| 49 | + cpu-quota: 50000 # Quota in microseconds (50% of one CPU) |
| 50 | + cmd: echo "Running with CPU limits" |
| 51 | +
|
| 52 | +Combined Limits |
| 53 | +--------------- |
| 54 | + |
| 55 | +.. code:: yaml |
| 56 | +
|
| 57 | + steps: |
| 58 | + my-step: |
| 59 | + run: |
| 60 | + image: ubuntu:20.04 |
| 61 | + mem-limit: "1g" |
| 62 | + cpu-shares: 1024 |
| 63 | + cpu-period: 100000 |
| 64 | + cpu-quota: 100000 |
| 65 | + cmd: echo "Running with all limits" |
| 66 | +
|
| 67 | +Service Container Limits |
| 68 | +------------------------- |
| 69 | + |
| 70 | +.. code:: yaml |
| 71 | +
|
| 72 | + steps: |
| 73 | + my-step: |
| 74 | + run: |
| 75 | + image: ubuntu:20.04 |
| 76 | + services: |
| 77 | + database: |
| 78 | + image: postgres:13 |
| 79 | + mem-limit: "256m" |
| 80 | + cpu-shares: 512 |
| 81 | + env: |
| 82 | + POSTGRES_PASSWORD: example |
| 83 | + cmd: echo "Main container with limited database service" |
| 84 | +
|
| 85 | +Docker Resource Limit Parameters |
| 86 | +================================= |
| 87 | + |
| 88 | +Memory Limit (``mem-limit``) |
| 89 | +----------------------------- |
| 90 | + |
| 91 | +- **Accepts**: String (e.g., "512m", "1g", "2g") or integer (bytes) |
| 92 | +- **Description**: Sets the maximum amount of memory the container can use |
| 93 | +- **Behavior**: Docker will enforce this limit and may kill the container if exceeded |
| 94 | + |
| 95 | +CPU Shares (``cpu-shares``) |
| 96 | +---------------------------- |
| 97 | + |
| 98 | +- **Accepts**: Integer |
| 99 | +- **Default**: 1024 |
| 100 | +- **Description**: Relative weight for CPU time allocation |
| 101 | +- **Behavior**: Higher values get more CPU time when there's contention |
| 102 | + |
| 103 | +CPU Period (``cpu-period``) |
| 104 | +---------------------------- |
| 105 | + |
| 106 | +- **Accepts**: Integer (microseconds) |
| 107 | +- **Default**: 100000 (100ms) |
| 108 | +- **Description**: Defines the period for CPU quota enforcement |
| 109 | + |
| 110 | +CPU Quota (``cpu-quota``) |
| 111 | +-------------------------- |
| 112 | + |
| 113 | +- **Accepts**: Integer (microseconds) |
| 114 | +- **Description**: Limits CPU time within the period |
| 115 | +- **Example**: quota=50000, period=100000 = 50% of one CPU core |
| 116 | + |
| 117 | +Implementation Details |
| 118 | +====================== |
| 119 | + |
| 120 | +Configuration Model |
| 121 | +------------------- |
| 122 | + |
| 123 | +Resource limit fields are defined in the ``RunAndServicesBase`` class in ``buildrunner/config/models_step.py``. These fields are inherited by both ``StepRun`` and ``Service`` configurations. |
| 124 | + |
| 125 | +Docker Runner |
| 126 | +------------- |
| 127 | + |
| 128 | +The ``DockerRunner.start()`` method in ``buildrunner/docker/runner.py`` accepts resource limit parameters and passes them to Docker's ``create_host_config`` method. |
| 129 | + |
| 130 | +Run Task Integration |
| 131 | +-------------------- |
| 132 | + |
| 133 | +The run task layer in ``buildrunner/steprunner/tasks/run.py`` extracts resource limits from step and service configurations and passes them to the Docker runner for both main containers and service containers. |
| 134 | + |
| 135 | +See Also |
| 136 | +======== |
| 137 | + |
| 138 | +- :doc:`global-configuration` - Global buildrunner configuration options |
| 139 | +- ``examples/resource-limits-example.yaml`` - Complete working examples |
| 140 | + |
0 commit comments