From 069328799df5b3c06a8d977529f347cfd518e077 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Mon, 18 Mar 2024 15:30:05 -0700 Subject: [PATCH] chore: add some cpu resource_set values as well --- docs/resource_sets.md | 7 +++++-- lib/resource_sets.bzl | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/docs/resource_sets.md b/docs/resource_sets.md index 30faa61..ec89a35 100644 --- a/docs/resource_sets.md +++ b/docs/resource_sets.md @@ -4,8 +4,11 @@ Utilities for rules that expose resource_set on ctx.actions.run[_shell] Workaround for https://github.com/bazelbuild/bazel/issues/15187 -By default, Bazel allocates 1 cpu and 250M of RAM: -https://github.com/bazelbuild/bazel/blob/058f943037e21710837eda9ca2f85b5f8538c8c5/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java#L77 +Note, this workaround only provides some fixed values for either CPU or Memory. + +Rule authors who are ALSO the BUILD author might know better, and can +write custom resource_set functions for use within their own repository. +This seems to be the use case that Google engineers imagined. diff --git a/lib/resource_sets.bzl b/lib/resource_sets.bzl index 1dae839..0207154 100644 --- a/lib/resource_sets.bzl +++ b/lib/resource_sets.bzl @@ -2,11 +2,16 @@ Workaround for https://github.com/bazelbuild/bazel/issues/15187 -By default, Bazel allocates 1 cpu and 250M of RAM: -https://github.com/bazelbuild/bazel/blob/058f943037e21710837eda9ca2f85b5f8538c8c5/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java#L77 +Note, this workaround only provides some fixed values for either CPU or Memory. + +Rule authors who are ALSO the BUILD author might know better, and can +write custom resource_set functions for use within their own repository. +This seems to be the use case that Google engineers imagined. """ resource_set_values = [ + "cpu_2", + "cpu_4", "default", "mem_512m", "mem_1g", @@ -17,6 +22,12 @@ resource_set_values = [ "mem_32g", ] +def _resource_set_cpu_2(_, __): + return {"cpu": 2} + +def _resource_set_cpu_4(_, __): + return {"cpu": 4} + def _resource_set_mem_512m(_, __): return {"memory": 512} @@ -40,6 +51,10 @@ def _resource_set_mem_32g(_, __): # buildifier: disable=function-docstring def resource_set(attr): + if attr.resource_set == "cpu_2": + return _resource_set_cpu_2 + if attr.resource_set == "cpu_4": + return _resource_set_cpu_4 if attr.resource_set == "default": return None if attr.resource_set == "mem_512m": @@ -65,6 +80,7 @@ resource_set_attr = { Used with --experimental_action_resource_set to reserve more RAM/CPU, preventing Bazel overscheduling resource-intensive actions. By default, Bazel allocates 1 CPU and 250M of RAM. + https://github.com/bazelbuild/bazel/blob/058f943037e21710837eda9ca2f85b5f8538c8c5/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java#L77 """, default = "default", values = resource_set_values,