|
| 1 | +"""Resource set definitions for build actions""" |
| 2 | + |
| 3 | +load("@bazel_lib//lib:resource_sets.bzl", "resource_set_for") |
| 4 | +load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo", "int_flag", "string_flag") |
| 5 | + |
| 6 | +_DEFAULT_SIZE = "default" |
| 7 | +_SIZES = { |
| 8 | + "enormous": { |
| 9 | + "cpu": 16, |
| 10 | + "mem": 2048, |
| 11 | + }, |
| 12 | + "large": { |
| 13 | + "cpu": 8, |
| 14 | + "mem": 1024, |
| 15 | + }, |
| 16 | + "medium": { |
| 17 | + "cpu": 4, |
| 18 | + "mem": 500, |
| 19 | + }, |
| 20 | + "small": { |
| 21 | + "cpu": 2, |
| 22 | + "mem": 250, |
| 23 | + }, |
| 24 | + "tiny": { |
| 25 | + "cpu": 1, |
| 26 | + "mem": 250, |
| 27 | + }, |
| 28 | +} |
| 29 | + |
| 30 | +def _setting(size, resource, short = True): |
| 31 | + if size == _DEFAULT_SIZE: |
| 32 | + short_name = _DEFAULT_SIZE |
| 33 | + else: |
| 34 | + short_name = "{}_{}".format(size, resource) |
| 35 | + |
| 36 | + if short: |
| 37 | + return short_name |
| 38 | + return "//foreign_cc/settings:size_" + short_name |
| 39 | + |
| 40 | +def create_settings(): |
| 41 | + """create the settings that configure these functions.""" |
| 42 | + string_flag( |
| 43 | + name = "size_default", |
| 44 | + build_setting_default = _DEFAULT_SIZE, |
| 45 | + values = _SIZES.keys() + [_DEFAULT_SIZE], |
| 46 | + visibility = ["//visibility:public"], |
| 47 | + ) |
| 48 | + |
| 49 | + for size, cfg in _SIZES.items(): |
| 50 | + if cfg == None: |
| 51 | + continue |
| 52 | + |
| 53 | + # keep this in sync with the docs and the above helper! |
| 54 | + int_flag( |
| 55 | + name = "size_{}_cpu".format(size), |
| 56 | + build_setting_default = _SIZES[size]["cpu"], |
| 57 | + visibility = ["//visibility:public"], |
| 58 | + ) |
| 59 | + |
| 60 | + int_flag( |
| 61 | + name = "size_{}_mem".format(size), |
| 62 | + build_setting_default = _SIZES[size]["mem"], |
| 63 | + visibility = ["//visibility:public"], |
| 64 | + ) |
| 65 | + |
| 66 | +SIZE_ATTRIBUTES = { |
| 67 | + "resource_size": attr.string( |
| 68 | + values = _SIZES.keys() + [_DEFAULT_SIZE], |
| 69 | + default = _DEFAULT_SIZE, |
| 70 | + mandatory = False, |
| 71 | + doc = ( |
| 72 | + "Set the approximate size of this build. This does two things:" + |
| 73 | + "1. Sets the environment variables to tell the underlying build system " + |
| 74 | + " the requested parallization; examples are CMAKE_BUILD_PARALLEL_LEVEL " + |
| 75 | + " for cmake or MAKEFLAGS for autotools. " + |
| 76 | + "2. Sets the resource_set attribute on the action to tell bazel how many cores " + |
| 77 | + " are being used, so it schedules appropriately." + |
| 78 | + "The sizes map to labels, which can be used to override the meaning of the sizes. See " + |
| 79 | + "@rules_foreign_cc//foreign_cc/settings:size_{size}_{cpu|mem}" |
| 80 | + ), |
| 81 | + ), |
| 82 | + "_size_config": attr.string_keyed_label_dict( |
| 83 | + default = { |
| 84 | + _setting(size, resource): _setting(size, resource, short = False) |
| 85 | + for size in _SIZES.keys() |
| 86 | + for resource in ["cpu", "mem"] |
| 87 | + } | { |
| 88 | + _DEFAULT_SIZE: _setting(size = _DEFAULT_SIZE, resource = None, short = False), |
| 89 | + }, |
| 90 | + ), |
| 91 | +} |
| 92 | + |
| 93 | +def _get_size_config(size_config, size, resource): |
| 94 | + s = size_config.get( |
| 95 | + _setting(size = size, resource = resource), |
| 96 | + ) |
| 97 | + |
| 98 | + if s == None: |
| 99 | + fail("unknown size: ", size) |
| 100 | + |
| 101 | + return s[BuildSettingInfo].value |
| 102 | + |
| 103 | +def get_resource_set(attr): |
| 104 | + """ get the resource set as configured by the settings and attrs |
| 105 | +
|
| 106 | + Args: |
| 107 | + attr: the ctx.attr associated with the target |
| 108 | + Returns: |
| 109 | + A tuple of: |
| 110 | + - the resource_set, or None if it's the bazel default |
| 111 | + - cpu_cores, or 0 if it's the bazel default |
| 112 | + - mem in MB, or 0 if it's the bazel default |
| 113 | + """ |
| 114 | + size = _DEFAULT_SIZE |
| 115 | + if attr.resource_size != _DEFAULT_SIZE: |
| 116 | + size = attr.resource_size |
| 117 | + else: |
| 118 | + size = _get_size_config(attr._size_config, _DEFAULT_SIZE, None) |
| 119 | + |
| 120 | + if size == _DEFAULT_SIZE: |
| 121 | + return None, 0, 0 |
| 122 | + |
| 123 | + cpu_value = _get_size_config(attr._size_config, size, "cpu") |
| 124 | + mem_value = _get_size_config(attr._size_config, size, "mem") |
| 125 | + |
| 126 | + if cpu_value < 0: |
| 127 | + fail("cpu must be >= 0") |
| 128 | + |
| 129 | + if mem_value < 0: |
| 130 | + fail("cpu must be >= 0") |
| 131 | + |
| 132 | + resource_set = resource_set_for( |
| 133 | + cpu_cores = cpu_value, |
| 134 | + mem_mb = mem_value, |
| 135 | + ) |
| 136 | + |
| 137 | + if resource_set: |
| 138 | + actual_resources = resource_set("", "") |
| 139 | + actual_cpu = actual_resources.get("cpu", 0) |
| 140 | + actual_mem = actual_resources.get("mem", 0) |
| 141 | + else: |
| 142 | + actual_cpu = 0 |
| 143 | + actual_mem = 0 |
| 144 | + |
| 145 | + return resource_set, actual_cpu, actual_mem |
0 commit comments