-
Notifications
You must be signed in to change notification settings - Fork 401
chore: use --execution_log_compact_file to prevent OOM #4916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
basvandijk
merged 32 commits into
master
from
basvandijk/execution_log_compact_to_csv.py
Apr 29, 2025
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
2085acd
chore: use --execution_log_compact_file to prevent OOM
basvandijk e574a8e
docs
basvandijk 0f7f618
Merge remote-tracking branch 'origin/master' into basvandijk/executio…
basvandijk ebf39f9
layout
basvandijk 8904824
little touch
basvandijk 44473d7
.
basvandijk abf364c
inline
basvandijk b4f61d2
use a case statement
basvandijk 7e245d9
better error message
basvandijk 61b38ec
match statement not yet supported in Python-3.8
basvandijk 1759bf6
Don't commit spawn.proto
basvandijk d09082c
fix //bazel:buildifier_test
basvandijk cf978ac
pop the output entry from the id_to_entry dict to preserve memory
basvandijk 56c6bdd
Only build execution_log_compact_to_csv if necessary
basvandijk 4c62e75
Merge remote-tracking branch 'origin/master' into basvandijk/executio…
basvandijk bc7316c
Always fetch the right spawn.proto corresponding to the active bazel …
basvandijk 64b3593
simplification
basvandijk 87d3b65
add --verbose
basvandijk ae38e5e
fix
basvandijk 20eb81f
don't use --verbose
basvandijk 7380d21
don't pop entries from id_to_entry because that causes issues
basvandijk 3dd5da0
fmt
basvandijk 0f61768
fmt
basvandijk cc054eb
fmt
basvandijk 4af3116
bazel run //:ruff-format
basvandijk dbe2af2
apply review suggestions
basvandijk f0c0205
use bazel run
basvandijk 8b3980a
refactor
basvandijk 6faee3c
Merge remote-tracking branch 'origin/master' into basvandijk/executio…
basvandijk 8a69656
include link to spawn.proto
basvandijk fa73dce
Use mktemp for the decompressed execlog
basvandijk 1a7726d
fix
basvandijk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # This Python script converts bazel's compact execution log (--execution_log_compact_file) | ||
| # to a CSV file that contains a row per output file for every bazel target matching the given | ||
| # --whitelist_pat regular expression. Each row contains the columns: target, path, and hash. | ||
| # | ||
| # Example usage: | ||
| # | ||
| # To invoke this script first generate a compact execution log with bazel using for example: | ||
| # | ||
| # $ bazel build //rs/replica:replica --execution_log_compact_file=compact_execlog.zst | ||
| # | ||
| # Then decompress the file: | ||
| # | ||
| # $ zstd -d compact_execlog.zst | ||
| # | ||
| # Finally build and run this script to convert the decompressed file to a CSV: | ||
| # | ||
| # $ bazel build //bazel:execution_log_compact_to_csv && \ | ||
| # bazel-bin/bazel/execution_log_compact_to_csv \ | ||
| # --input_execlog=compact_execlog \ | ||
| # --whitelist_pat='^//' | ||
| # | ||
| # //rs/http_endpoints/public:build_script_,bazel-out/k8-opt-exec-ST-d57f47055a04/bin/rs/http_endpoints/public/build_script_,4463251579c194bf83a24408a184da9a7f46d5777f753e33a77afa5c2287fd1d | ||
| # //rs/http_endpoints/public:build_script,bazel-out/k8-opt/bin/rs/http_endpoints/public/build_script.out_dir/dashboard.rs,f0ba2e4976ac312782190b6391ce945f57708b46126b741f6336b355968a8a84 | ||
| # ... | ||
| # //rs/replica:replica_lib,bazel-out/k8-opt/bin/rs/replica/libic_replica-974340861.rlib,0d45ec60aee4472020f69d0a19ac2ad4519388216f5a21e19152539fa3d0c4ce | ||
| # //rs/replica:replica,bazel-out/k8-opt/bin/rs/replica/replica,0ecf150fbff09dc7b92ac976827fc46179561f2e8e83487840330ce247b61b5d | ||
|
|
||
| import argparse | ||
| import csv | ||
| import re | ||
| import sys | ||
|
|
||
| import google.protobuf.internal | ||
| import spawn_pb2 | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser( | ||
| description="Convert bazel's compact execution log to a CSV file printed to stdout." | ||
| ) | ||
| parser.add_argument( | ||
| "--input_execlog", | ||
| required=True, | ||
| help="Path to the zstd _decompressed_ input file as generated with bazel's --execution_log_compact_file option.", | ||
| ) | ||
| parser.add_argument( | ||
| "--whitelist_pat", | ||
| default=None, | ||
| help="Regex to match target labels to include in the CSV. If omitted, all targets are included.", | ||
| ) | ||
| parser.add_argument( | ||
| "--verbose", | ||
| action="store_true", | ||
| help="Log every ExecLogEntry to stderr. This is useful for debugging.", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| # We intend to log the outputs of Spawns. Spawns refer to their output by ID | ||
| # so we need to keep track of the ID to entry mapping. Important note: | ||
| # if an entry (like a Spawn) refers to another entry (like a File or Directory) | ||
| # it's guaranteed the latter will precede the former. | ||
| id_to_entry = {} | ||
|
basvandijk marked this conversation as resolved.
|
||
|
|
||
| with open(args.input_execlog, "rb") as execlog_file, sys.stdout as csv_file: | ||
| writer = csv.writer(csv_file) | ||
| while True: | ||
| # The execution log is a stream of protobuf messages, | ||
| # each preceded by a varint32 denoting its length. | ||
| msg_len = google.protobuf.internal.decoder._DecodeVarint32(execlog_file) | ||
| if msg_len is None: | ||
| break | ||
|
|
||
| # Parse the ExecLogEntry message. For the message definition see: | ||
| # https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/spawn.proto. | ||
| msg_buf = execlog_file.read(msg_len) | ||
| exec_log_entry = spawn_pb2.ExecLogEntry() | ||
| exec_log_entry.ParseFromString(msg_buf) | ||
|
|
||
| if args.verbose: | ||
| print(exec_log_entry, file=sys.stderr) | ||
|
|
||
| entry_type = exec_log_entry.WhichOneof("type") | ||
| if entry_type in ["file", "directory"]: | ||
| id_to_entry[exec_log_entry.id] = exec_log_entry | ||
| elif entry_type == "spawn": | ||
| label = exec_log_entry.spawn.target_label | ||
| if args.whitelist_pat is not None and not re.match(args.whitelist_pat, label): | ||
| continue | ||
| for output in exec_log_entry.spawn.outputs: | ||
| output_type = output.WhichOneof("type") | ||
| if output_type != "output_id": | ||
| continue | ||
| output_entry = id_to_entry[output.output_id] | ||
| output_entry_type = output_entry.WhichOneof("type") | ||
| if output_entry_type == "file": | ||
| writer.writerow([label, output_entry.file.path, output_entry.file.digest.hash]) | ||
| elif output_entry_type == "directory": | ||
| dir_path = output_entry.directory.path | ||
| for dir_file in output_entry.directory.files: | ||
| writer.writerow([label, dir_path + "/" + dir_file.path, dir_file.digest.hash]) | ||
| else: | ||
| raise ValueError(f"Unexpected output entry type: {output_entry_type}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """ | ||
| This module fetches the spawn.proto file from the bazel source code and turns it into a proto_library target. | ||
| """ | ||
|
|
||
| def _spawn_proto_library_impl(ctx): | ||
| ctx.download( | ||
| # We assume the ExecLogEntry proto message is forward compatible such that we can use the spawn.proto from bazel-7.6.0 even after upgrading bazel. | ||
| url = "https://raw.githubusercontent.com/bazelbuild/bazel/refs/tags/7.6.0/src/main/protobuf/spawn.proto", | ||
| output = "spawn.proto", | ||
| sha256 = "381bcb109e2855d4055fdc75988cc2144e0a92aa8586298123bb662cdefa6afe", | ||
|
basvandijk marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| ctx.file("BUILD.bazel", """ | ||
| load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| proto_library( | ||
| name = "spawn_pb2", | ||
| srcs = ["spawn.proto"], | ||
| deps = [ | ||
| "@com_google_protobuf//:duration_proto", | ||
| "@com_google_protobuf//:timestamp_proto", | ||
| ], | ||
| ) | ||
| """) | ||
|
|
||
| spawn_proto_library = repository_rule(implementation = _spawn_proto_library_impl) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.