blob: e2bc7d803278a99da8af47c9fac5eadf45dfa441 [file] [log] [blame] [view]
larsrc754c1202020-07-31 11:59:05 -07001---
larsrc45138122020-07-30 09:06:12 -07002layout: documentation
daroberts73011da52021-01-06 11:59:44 -08003title: Persistent Workers
darobertsce2bdd12021-02-26 11:36:38 -08004category: extending
larsrc754c1202020-07-31 11:59:05 -07005---
larsrc45138122020-07-30 09:06:12 -07006
daroberts73011da52021-01-06 11:59:44 -08007# Persistent Workers
8
Googler54a96a12020-12-23 16:43:39 -08009This page covers how to use persistent workers, the benefits, requirements,
10and how workers affect sandboxing.
11
ranjanih49628472020-11-18 17:57:35 -080012A persistent worker is a long-running process started by the Bazel server, which
13functions as a _wrapper_ around the actual _tool_ (typically a compiler), or is
14the _tool_ itself. In order to benefit from persistent workers, the tool must
15support doing a sequence of compilations, and the wrapper needs to translate
16between the tool's API and the request/response format described below. The same
17worker might be called with and without the `--persistent_worker` flag
18in the same build, and is responsible for appropriately starting and talking to
19the tool, as well as shutting down workers on exit. Each worker instance is
20assigned (but not chrooted to) a separate working directory under
21`<outputBase>/bazel-workers`.
22
23Using persistent workers is an
larsrc45138122020-07-30 09:06:12 -070024[execution strategy](https://docs.bazel.build/versions/master/user-manual.html#strategy-options)
25that decreases start-up overhead, allows more JIT compilation, and enables
26caching of for example the abstract syntax trees in the action execution. This
27strategy achieves these improvements by sending multiple requests to a
ranjanih49628472020-11-18 17:57:35 -080028long-running process.
29
30Persistent workers are implemented for multiple languages, including Java,
larsrc45138122020-07-30 09:06:12 -070031[TypeScript](https://bazelbuild.github.io/rules_nodejs/TypeScript.html),
ranjanih49628472020-11-18 17:57:35 -080032[Scala](https://github.com/bazelbuild/rules_scala),
33[Kotlin](https://github.com/bazelbuild/rules_kotlin), and more.
larsrc45138122020-07-30 09:06:12 -070034
35## Using persistent workers <a name="usage"></a>
36
37[Bazel 0.27 and higher](https://blog.bazel.build/2019/06/19/list-strategy.html)
ranjanih49628472020-11-18 17:57:35 -080038uses persistent workers by default when executing builds, though remote
larsrc45138122020-07-30 09:06:12 -070039execution takes precedence. For actions that do not support persistent workers,
ranjanih49628472020-11-18 17:57:35 -080040Bazel falls back to starting a tool instance for each action. You can explicitly
41set your build to use persistent workers by setting the `worker`
larsrc45138122020-07-30 09:06:12 -070042[strategy](user-manual.html#strategy-options) for the applicable tool mnemonics.
43As a best practice, this example includes specifying `local` as a fallback to
44the `worker` strategy:
45
46```
47bazel build //my:target --strategy=Javac=worker,local
48```
49
50Using the workers strategy instead of the local strategy can boost compilation
Googlerea70a3a2021-02-26 17:40:15 -080051speed significantly, depending on implementation. For Java, builds can be
522–4 times faster, sometimes more for incremental compilation. Compiling
53Bazel is about 2.5 times as fast with workers. For more details, see the
larsrc45138122020-07-30 09:06:12 -070054"[Choosing number of workers](#number-of-workers)" section.
55
56If you also have a remote build environment that matches your local build
57environment, you can use the experimental
58[_dynamic_ strategy](https://blog.bazel.build/2019/02/01/dynamic-spawn-scheduler.html),
59which races a remote execution and a worker execution. To enable the dynamic
60strategy, pass the
61[--experimental_spawn_scheduler](command-line-reference.html#flag--experimental_spawn_scheduler)
62flag. This strategy automatically enables workers, so there is no need to
63specify the `worker` strategy, but you can still use `local` or `sandboxed` as
64fallbacks.
65
66## Choosing number of workers <a name="number-of-workers"></a>
67
68The default number of worker instances per mnemonic is 4, but can be adjusted
69with the
70[`worker_max_instances`](command-line-reference.html#flag--worker_max_instances)
71flag. There is a trade-off between making good use of the available CPUs and the
72amount of JIT compilation and cache hits you get. With more workers, more
73targets will pay start-up costs of running non-JITted code and hitting cold
74caches. If you have a small number of targets to build, a single worker may give
75the best trade-off between compilation speed and resource usage (for example,
76see [issue #8586](https://github.com/bazelbuild/bazel/issues/8586). The
77`worker_max_instances` flag sets the maximum number of worker instances per
78mnemonic and flag set (see below), so in a mixed system you could end up using
79quite a lot of memory if you keep the default value. For incremental builds the
80benefit of multiple worker instances is even smaller.
81
82This graph shows the from-scratch compilation times for Bazel (target
83`//src:bazel`) on a 6-core hyper-threaded Intel Xeon 3.5 GHz Linux workstation
Googlerea70a3a2021-02-26 17:40:15 -080084with 64 GB of RAM. For each worker configuration, five clean builds are run and
85the average of the last four are taken.
larsrc45138122020-07-30 09:06:12 -070086
87<p align="center">
88<img width="596px" alt="Graph of performance improvements of clean builds" src="/assets/workers-clean-chart.png">
89</p>
90
91For this configuration, two workers give the fastest compile, though at only 14%
92improvement compared to one worker. One worker is a good option if you want to
93use less memory.
94
95Incremental compilation typically benefits even more. Clean builds are
96relatively rare, but changing a single file between compiles is common, in
97particular in test-driven development. The above example also has some non-Java
98packaging actions to it that can overshadow the incremental compile time.
99Recompiling the Java sources only
100(`//src/main/java/com/google/devtools/build/lib/bazel:BazelServer_deploy.jar`)
101after changing an internal string constant in
102[AbstractContainerizingSandboxedSpawn.java](https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/sandbox/AbstractContainerizingSandboxedSpawn.java)
103gives a 3x speed-up (average of 20 incremental builds with one warmup build
104discarded):
105
106<p align="center">
107<img width="592px" alt="Graph of performance improvements of incremental builds" src="/assets/workers-incremental-chart.png">
108</p>
109
Googlerea70a3a2021-02-26 17:40:15 -0800110The speed-up depends on the change being made. A speed-up of a
111factor 6 is measured in the above situation when a commonly used constant
112is changed.
larsrc45138122020-07-30 09:06:12 -0700113
114## Modifying persistent workers<a name="options"></a>
115
116You can pass the
117[`--worker_extra_flag`](command-line-reference.html#flag--worker_extra_flag)
118flag to specify start-up flags to workers, keyed by mnemonic. For instance,
119passing `--worker_extra_flag=javac=--debug` turns on debugging for Javac only.
120Only one worker flag can be set per use of this flag, and only for one mnemonic.
121Workers are not just created separately for each mnemonic, but also for
122variations in their start-up flags. Each combination of mnemonic and start-up
123flags is combined into a `WorkerKey`, and for each `WorkerKey` up to
124`worker_max_instances` workers may be created. See the next section for how the
125action configuration can also specify set-up flags.
126
127You can use the
128[`--high_priority_workers`](command-line-reference.html#flag--high_priority_workers)
129flag to specify a mnemonic that should be run in preference to normal-priority
130mnemonics. This can help prioritize actions that are always in the critical
131path. If there are two or more high priority workers executing requests, all
132other workers are prevented from running. This flag can be used multiple times.
133
134Passing the
135[`--worker_sandboxing`](command-line-reference.html#flag--worker_sandboxing)
136flag makes each worker request use a separate sandbox directory for all its
ranjanihd373c722021-01-20 10:01:40 -0800137inputs. Setting up the [sandbox](sandboxing.md) takes some extra time,
138especially on macOS, but gives a better correctness guarantee.
larsrc45138122020-07-30 09:06:12 -0700139
steinmane92b72b2020-10-06 11:00:17 -0700140You can use the `--experimental_worker_allow_json_protocol` flag to allow
141workers to communicate with Bazel through JSON instead of protocol buffers
142(protobuf). The worker and the rule that consumes it can then be modified to
143support JSON.
144
larsrc45138122020-07-30 09:06:12 -0700145The
146[`--worker_quit_after_build`](command-line-reference.html#flag--worker_quit_after_build)
147flag is mainly useful for debugging and profiling. This flag forces all workers
148to quit once a build is done. You can also pass
149[`--worker_verbose`](command-line-reference.html#flag--worker_verbose) to get
larsrc441586b2020-08-04 10:00:05 -0700150more output about what the workers are doing.
151
152Workers store their logs in the `<outputBase>/bazel-workers` directory, for
153example
154`/tmp/_bazel_larsrc/191013354bebe14fdddae77f2679c3ef/bazel-workers/worker-1-Javac.log`.
155The file name includes the worker id and the mnemonic. Since there can be more
156than one `WorkerKey` per mnemonic, you may see more than `worker_max_instances`
157log files for a given mnemonic.
larsrc45138122020-07-30 09:06:12 -0700158
159For Android builds, see details at the
160[Android Build Performance page](android-build-performance.html).
161
162## Implementing persistent workers<a name="implementation"></a>
163
steinman38835eb2020-11-11 14:19:56 -0800164See the [creating persistent workers](creating-workers.html) page for
165information on how to make a worker.
166
steinmane92b72b2020-10-06 11:00:17 -0700167This example shows a Starlark configuration for a worker that uses JSON:
larsrc45138122020-07-30 09:06:12 -0700168
169```python
170args_file = ctx.actions.declare_file(ctx.label.name + "_args_file")
171ctx.actions.write(
172 output = args_file,
173 content = "\n".join(["-g", "-source", "1.5"] + ctx.files.srcs),
174)
175ctx.actions.run(
176 mnemonic = "SomeCompiler",
177 executable = "bin/some_compiler_wrapper",
178 inputs = inputs,
179 outputs = outputs,
180 arguments = [ "-max_mem=4G", "@%s" % args_file.path],
steinmane92b72b2020-10-06 11:00:17 -0700181 execution_requirements = {
182 "supports-workers" : "1", "requires-worker-protocol" : "json" }
larsrc45138122020-07-30 09:06:12 -0700183)
184```
larsrc45138122020-07-30 09:06:12 -0700185With this definition, the first use of this action would start with executing
186the command line `/bin/some_compiler -max_mem=4G --persistent_worker`. A request
187to compile `Foo.java` would then look like:
188
189```prototext
190arguments: [ "-g", "-source", "1.5", "Foo.java" ]
191inputs: [
192 {path: "symlinkfarm/input1" digest: "d49a..." },
193 {path: "symlinkfarm/input2", digest: "093d..."},
194]
195```
196
ranjanih49628472020-11-18 17:57:35 -0800197The worker receives this on `stdin` in JSON format (because
198`requires-worker-protocol` is set to JSON, and
steinmane92b72b2020-10-06 11:00:17 -0700199`--experimental_worker_allow_json_protocol` is passed to the build to enable
ranjanih49628472020-11-18 17:57:35 -0800200this option). The worker then performs the action, and sends a JSON-formatted
201`WorkResponse` to Bazel on its stdout. Bazel then parses this response and
202manually converts it to a `WorkResponse` proto. To communicate
203with the associated worker using binary-encoded protobuf instead of JSON,
204`requires-worker-protocol` would be set to `proto`, like this:
205
steinmane92b72b2020-10-06 11:00:17 -0700206```
207 execution_requirements = {
208 "supports-workers" : "1" ,
209 "requires-worker-protocol" : "proto"
210 }
211```
212If you do not include `requires-worker-protocol` in the execution requirements,
213Bazel will default the worker communication to use protobuf.
214
215Bazel derives the `WorkerKey` from the mnemonic and the shared flags, so if this
larsrc45138122020-07-30 09:06:12 -0700216configuration allowed changing the `max_mem` parameter, a separate worker would
217be spawned for each value used. This can lead to excessive memory consumption if
218too many variations are used.
219
220Each worker can currently only process one request at a time. The experimental
221[multiplex workers](multiplex-worker.html) feature allows using multiple
222threads, if the underlying tool is multithreaded and the wrapper is set up to
223understand this.
224
225In [this GitHub repo](https://github.com/Ubehebe/bazel-worker-examples), you can
Joe Lencioni48245272020-11-16 08:43:41 -0800226see example worker wrappers written in Java as well as in Python. If you are
227working in JavaScript or TypeScript, the [@bazel/worker
228package](https://www.npmjs.com/package/@bazel/worker) and
229[nodejs worker example](https://github.com/bazelbuild/rules_nodejs/tree/stable/examples/worker)
230might be helpful.
larsrc45138122020-07-30 09:06:12 -0700231
232## How do workers affect sandboxing? <a name="sandboxing"></a>
233
ranjanihd373c722021-01-20 10:01:40 -0800234Using the `worker` strategy by default does not run the action in a
235[sandbox](sandboxing.md), similar to the `local` strategy. You can set
236the `--worker_sandboxing` flag to run all workers inside sandboxes, making sure
237each execution of the tool only sees the input files it's supposed to have. The
238tool may still leak information between requests internally, for instance
239through a cache. Using `dynamic` strategy [requires workers to be sandboxed](https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/exec/SpawnStrategyRegistry.java).
larsrc45138122020-07-30 09:06:12 -0700240
241To allow correct use of compiler caches with workers, a digest is passed along
242with each input file. Thus the compiler or the wrapper can check if the input is
243still valid without having to read the file.
244
245Even when using the input digests to guard against unwanted caching, sandboxed
246workers offer less strict sandboxing than a pure sandbox, because the tool may
247keep other internal state that has been affected by previous requests.
248
249## Further reading <a name="further-reading"></a>
250
251For more information on persistent workers, see:
252
253* [Original persistent workers blog post](https://blog.bazel.build/2015/12/10/java-workers.html)
254* [Haskell implementation description](https://www.tweag.io/blog/2019-09-25-bazel-ghc-persistent-worker-internship/)
255* [Blog post by Mike Morearty](https://medium.com/@mmorearty/how-to-create-a-persistent-worker-for-bazel-7738bba2cabb)
256* [Front End Development with Bazel: Angular/TypeScript and Persistent Workers
257 w/ Asana](https://www.youtube.com/watch?v=0pgERydGyqo)
258* [Bazel strategies explained](https://jmmv.dev/2019/12/bazel-strategies.html)
259* [Informative worker strategy discussion on the bazel-discuss mailing list](https://groups.google.com/forum/#!msg/bazel-discuss/oAEnuhYOPm8/ol7hf4KWJgAJ)