--experimental_action_listeners是否计入--jobs?

时间:2017-06-09 05:16:29

标签: bazel

我们将clang-tidy作为动作监听器运行。它可以轻松消耗与gcc编译一样多的CPU,如果不是更多的话。

这些是针对--jobs计数收取的,还是我们必须自己计划听众(例如,当"正确调整"机器/容器/什么)?

1 个答案:

答案 0 :(得分:2)

是的。

演示

我正在运行3个动作+ 3个额外动作。他们每个人都睡3秒钟,并在他们开始和结束时打印。

使用--jobs=2,您可以看到leaf2和leaf3一起构建(从15:10:00开始),然后是leaf1和leaf1侦听器(在15:10:03),最后是leaf2侦听器和leaf3侦听器(15:10:06)。

  $ bazel clean >&/dev/null && time bazel build //:root --jobs=2 --experimental_action_listener="//:clang-tidy-listener" 2>/dev/null
Start action listener at 15:10:03, argc=1, argv=(--extra_action_file=bazel-out/local-fastbuild/extra_actions/clang-tidy/cc4b710d2c96f16eb0bcc5df6f009f08.xa)
Done action listener at 15:10:06
Start action listener at 15:10:06, argc=1, argv=(--extra_action_file=bazel-out/local-fastbuild/extra_actions/clang-tidy/3001ea27a871bf7b111ee4bbbc1d79dc.xa)
Done action listener at 15:10:09
Start action listener at 15:10:06, argc=1, argv=(--extra_action_file=bazel-out/local-fastbuild/extra_actions/clang-tidy/f88e13a970ce3a66354583c477067b29.xa)
Done action listener at 15:10:09

real    0m9.698s
user    0m0.004s
sys     0m0.012s


  $ cat bazel-genfiles/root.txt
leaf1.txt
start: 15:10:03
start: 15:10:06

leaf2.txt
start: 15:10:00
start: 15:10:03

leaf3.txt
start: 15:10:00
start: 15:10:03

使用--jobs=8,所有操作均于15:08:58开始:

  $ bazel clean >&/dev/null && time bazel build //:root --jobs=8 --experimental_action_listener="//:clang-tidy-listener" 2>/dev/null
Start action listener at 15:08:58, argc=1, argv=(--extra_action_file=bazel-out/local-fastbuild/extra_actions/clang-tidy/f88e13a970ce3a66354583c477067b29.xa)
Done action listener at 15:09:01
Start action listener at 15:08:58, argc=1, argv=(--extra_action_file=bazel-out/local-fastbuild/extra_actions/clang-tidy/3001ea27a871bf7b111ee4bbbc1d79dc.xa)
Done action listener at 15:09:01
Start action listener at 15:08:58, argc=1, argv=(--extra_action_file=bazel-out/local-fastbuild/extra_actions/clang-tidy/cc4b710d2c96f16eb0bcc5df6f009f08.xa)
Done action listener at 15:09:01

real    0m3.609s
user    0m0.004s
sys     0m0.016s


  $ cat bazel-genfiles/root.txt
leaf1.txt
start: 15:08:58
start: 15:09:01

leaf2.txt
start: 15:08:58
start: 15:09:01

leaf3.txt
start: 15:08:58
start: 15:09:01

来源

//:BUILD

load(":myrule.bzl", "myrule")

genrule(
    name = "root",
    srcs = [
        "leaf1",
        "leaf2",
        "leaf3",
    ],
    outs = ["root.txt"],
    cmd = "( for f in $(SRCS); do basename $$f ; cat $$f ; echo ; done ; ) > $@",
)

[myrule(name = "leaf%d" % i) for i in [1, 2, 3]]

sh_binary(
    name = "listener",
    srcs = ["listener.sh"],
)

action_listener(
    name = "clang-tidy-listener",
    mnemonics = ["FooAction"],
    extra_actions = [":clang-tidy"],
)

extra_action(
    name = "clang-tidy",
    tools = [":listener"],
    cmd = "$(location :listener) --extra_action_file=$(EXTRA_ACTION_FILE)",
)

//:myrule.bzl

# Based on https://bazel.build/versions/master/docs/skylark/cookbook.html#simple-shell-command
def _impl(ctx):
  output = ctx.outputs.out
  ctx.action(
       outputs=[output],
       mnemonic="FooAction",
       command="( echo \"start: $(date +%%H:%%M:%%S)\" ; sleep 3 ; echo \"start: $(date +%%H:%%M:%%S)\" ; ) > %s" % output.path)

myrule = rule(
    implementation=_impl,
    outputs={"out": "%{name}.txt"},
)

//:listener.sh

#!/bin/bash
echo "Start action listener at $(date +%H:%M:%S), argc=$#, argv=($@)"
sleep 3
echo "Done action listener at $(date +%H:%M:%S)"