Compute compact execution log entries outside the global lock (#32637)

CompactSpawnLogContext wrote every entry from a method synchronized on the context. The lock was held while an entry was computed, and computing an entry logs every entry it references. The first action to mention an input set thus held the lock while it walked the nested set, looked up metadata, statted and digested output files, traversed directories and expanded runfiles trees. The lock was also taken to look up an entry that had already been logged, and held while serializing every entry, including the Spawn entry itself. Every action thread queued behind whichever thread was computing an entry, even if all of its own inputs had been logged before.

A fully remote cached `bazel test //...` of a large monorepo (2.1M actions, hundreds of jobs, 16 cores) took 2,967 s with --execution_log_compact_file and --experimental_enable_execution_graph_log and 2,381 s without them. Thread dumps taken during the execution phase showed on average 234 action threads blocked entering the synchronized methods of CompactSpawnLogContext, while the class accounted for only 3% of the CPU samples of a smaller build: the cost was the waiting, not the work.

With this change:

* Looking up the ID of an entry that has already been logged doesn't take the global lock. The map from keys to IDs is now an ObjectToIntMap (added in 226971067322f3b982433bdfeb7a8697d2e81ac0), which is split into 1024 shards, each guarded by its own monitor. A lookup holds the monitor of one shard for the duration of a single map operation.
* An entry is computed, which includes logging every entry it references, without holding a lock. While it is being computed, its key is mapped to a placeholder in a ConcurrentHashMap, which only holds the entries that are being computed at a given time. A thread that needs the same entry waits on the placeholder instead of computing the entry again, so that an entry with a key is still computed and written only once. Threads only wait for entries they need: a thread logging a spawn whose inputs are unrelated, or have been logged already, never waits. The wait is recorded in the trace profile as `logEntry/waitForPendingEntry`.
* Spawn and SymlinkAction entries, which have no ID, are serialized and written without holding a lock.
* The global lock is only held to assign the next ID to a fully computed entry and hand the entry to the output stream. Doing both atomically keeps IDs consecutive in the order in which entries appear in the log, which SpawnLogReconstructor requires.

The log thus contains the same entries as before; only their order, which already depended on the order in which actions completed, and therefore their IDs can differ.

An entry is still written after every entry it references, so the log can still be parsed in a single pass:

* An entry is written only after it has been computed, at which point an ID has been returned for every entry it references.
* An ID is returned only after the entry it identifies has been handed to the output stream: the thread that computes an entry returns the ID after the write, and every other thread obtains the ID from the map or the placeholder, both of which are updated after the write.
* The output stream writes entries in the order in which they were handed to it.

The ID lookup and the registration of a placeholder are not made under a common lock, so another thread can write the entry between the two. A thread that has registered a placeholder therefore looks up the ID once more before it computes the entry. This is sufficient because a placeholder is only removed after the ID of its entry has been stored. testConcurrentLogging fails without the second lookup.

Waiting for an entry computed by another thread can't deadlock: a thread only waits for entries referenced by the entry it is computing, and an entry never references itself, directly or transitively. If the computation fails or is interrupted, the placeholder is removed and the waiting threads compute the entry themselves, so a failure is still only reported for the spawn that ran into it.

Memory use should be no higher than before. Like the Object2IntOpenHashMap it replaces, ObjectToIntMap stores a key reference and an int per slot, and it grows a shard by a quarter instead of doubling it. The ConcurrentHashMap of placeholders is bounded by the number of entries being computed at a time, not by the number of entries in the log. The current revision has not been measured. For a build whose log has 9.3M entries with an ID, the single Object2IntOpenHashMap retained 218 MB. Earlier revisions of this change retained 204 MB (1024 Object2IntOpenHashMaps, each guarded by its own monitor) and 664 MB (a ConcurrentHashMap with boxed values) for the same build.

Threads only contend for a shard if they look up keys of the same shard at the same instant. With the revision that used 1024 Object2IntOpenHashMaps, which took locks in the same way, no thread was observed blocked on one of those monitors in thread dumps taken every minute across four fully remote cached `bazel test //...` builds of the monorepo above (500 jobs).

testSlowEntryIsComputedOnceWithoutBlockingUnrelatedSpawns fails without this change.

Closes #32637

COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/bazel/pull/32637 from alexander-scott:compact-execlog-entries-outside-lock 543024dcd476227d646ff763d341d879462f608e
PiperOrigin-RevId: 1028111826
Change-Id: Ifde821eda03637d1cd089c41347f5694c89fb4f2
3 files changed
tree: 71cf4ecf4274686e7a6e4a19963eb1ac7e97952e
  1. .bazelci/
  2. .devcontainer/
  3. .gemini/
  4. .github/
  5. docs/
  6. examples/
  7. scripts/
  8. src/
  9. third_party/
  10. tools/
  11. .bazelrc
  12. .bazelversion
  13. .gitattributes
  14. .gitignore
  15. AGENTS.md
  16. AUTHORS
  17. bazel_downloader.cfg
  18. BUILD
  19. CHANGELOG.md
  20. CODE_OF_CONDUCT.md
  21. CODEOWNERS
  22. combine_distfiles.py
  23. combine_distfiles_to_tar.sh
  24. compile.sh
  25. CONTRIBUTING.md
  26. CONTRIBUTORS
  27. distdir.bzl
  28. extensions.bzl
  29. LICENSE
  30. maven_install.json
  31. MODULE.bazel
  32. MODULE.bazel.lock
  33. oneversion_allowlist.csv
  34. oneversion_allowlist_for_tests.csv
  35. pyproject.toml
  36. README.md
  37. repositories.bzl
  38. requirements.txt
  39. SECURITY.md
  40. verify_module_bazel_lock.sh
README.md

Bazel

{Fast, Correct} - Choose two

Build and test software of any size, quickly and reliably.

  • Speed up your builds and tests: Bazel rebuilds only what is necessary. With advanced local and distributed caching, optimized dependency analysis and parallel execution, you get fast and incremental builds.

  • One tool, multiple languages: Build and test Java, C++, Android, iOS, Go, and a wide variety of other language platforms. Bazel runs on Windows, macOS, and Linux.

  • Scalable: Bazel helps you scale your organization, codebase, and continuous integration solution. It handles codebases of any size, in multiple repositories or a huge monorepo.

  • Extensible to your needs: Easily add support for new languages and platforms with Bazel's familiar extension language. Share and re-use language rules written by the growing Bazel community.

Getting Started

Documentation

Reporting a Vulnerability

To report a security issue, please email security@bazel.build with a description of the issue, the steps you took to create the issue, affected versions, and, if known, mitigations for the issue. Our vulnerability management team will respond within 3 working days of your email. If the issue is confirmed as a vulnerability, we will open a Security Advisory. This project follows a 90 day disclosure timeline.

Contributing to Bazel

See CONTRIBUTING.md

Build status