|
| 1 | +using Dates |
| 2 | +using Pkg |
| 3 | +using Tar |
| 4 | + |
| 5 | +if Base.VERSION < v"1.6" |
| 6 | + throw(ErrorException("The `rr_capture.jl` script requires Julia 1.6 or greater")) |
| 7 | +end |
| 8 | + |
| 9 | +if length(ARGS) < 1 |
| 10 | + throw(ErrorException("Usage: rr_capture.jl [command...]")) |
| 11 | +end |
| 12 | + |
| 13 | +const TIMEOUT = 2 * 60 * 60 # timeout in seconds |
| 14 | + |
| 15 | +# We only use `rr` on the `tester_linux64` builder |
| 16 | +const use_rr_if_builder_is = "tester_linux64" |
| 17 | + |
| 18 | +const run_id = get(ENV, "BUILDKITE_JOB_ID", "unknown") |
| 19 | +const shortcommit = get(ENV, "BUILDKITE_COMMIT", "unknown") |
| 20 | +const builder = get(ENV, "BUILDKITE_STEP_KEY", use_rr_if_builder_is) |
| 21 | +const use_rr = builder == use_rr_if_builder_is |
| 22 | + |
| 23 | +@info "" run_id shortcommit builder use_rr |
| 24 | +@info "" ARGS |
| 25 | + |
| 26 | +# if !use_rr # TODO: uncomment this line |
| 27 | +if true # TODO: delete this line |
| 28 | + @info "We will not run the tests under rr" |
| 29 | + p = run(`$ARGS`) |
| 30 | + exit(p.exitcode) |
| 31 | +end |
| 32 | + |
| 33 | +@info "We will run the tests under rr" |
| 34 | + |
| 35 | +const num_cores = min(Sys.CPU_THREADS, 8, parse(Int, get(ENV, "JULIA_TEST_NUM_CORES", "8")) + 1) |
| 36 | +@info "" num_cores |
| 37 | + |
| 38 | +proc = nothing |
| 39 | + |
| 40 | +new_env = copy(ENV) |
| 41 | +mktempdir() do dir |
| 42 | + Pkg.activate(dir) |
| 43 | + Pkg.add("rr_jll") |
| 44 | + Pkg.add("Zstd_jll") |
| 45 | + |
| 46 | + rr_jll = Base.require(Base.PkgId(Base.UUID((0xe86bdf43_55f7_5ea2_9fd0_e7daa2c0f2b4)), "rr_jll")) |
| 47 | + zstd_jll = Base.require(Base.PkgId(Base.UUID((0x3161d3a3_bdf6_5164_811a_617609db77b4)), "Zstd_jll")) |
| 48 | + rr(func) = Base.invokelatest(rr_jll.rr, func; adjust_LIBPATH=false) |
| 49 | + rr() do rr_path |
| 50 | + capture_script_path = joinpath(dir, "capture_output.sh") |
| 51 | + loader = Sys.WORD_SIZE == 64 ? "/lib64/ld-linux-x86-64.so.2" : "/lib/ld-linux.so.2" |
| 52 | + open(capture_script_path, "w") do io |
| 53 | + write(io, """ |
| 54 | + #!/bin/bash |
| 55 | +
|
| 56 | + $(rr_path) record --nested=detach "\$@" > >(tee -a $(dir)/stdout.log) 2> >(tee -a $(dir)/stderr.log >&2) |
| 57 | + """) |
| 58 | + end |
| 59 | + chmod(capture_script_path, 0o755) |
| 60 | + |
| 61 | + new_env = copy(ENV) |
| 62 | + new_env["_RR_TRACE_DIR"] = joinpath(dir, "rr_traces") |
| 63 | + new_env["RR_LOG"]="all:debug" |
| 64 | + new_env["RR_LOG_BUFFER"]="100000" |
| 65 | + new_env["JULIA_RR"] = capture_script_path |
| 66 | + t_start = time() |
| 67 | + global proc = run(setenv(`$(rr_path) record --num-cores=$(num_cores) $ARGS`, new_env), (stdin, stdout, stderr); wait=false) |
| 68 | + |
| 69 | + # Start asynchronous timer that will kill `rr` |
| 70 | + @async begin |
| 71 | + sleep(TIMEOUT) |
| 72 | + |
| 73 | + # If we've exceeded the timeout and `rr` is still running, kill it. |
| 74 | + if isopen(proc) |
| 75 | + println(stderr, "\n\nProcess timed out. Signalling `rr` for force-cleanup!") |
| 76 | + kill(proc, Base.SIGTERM) |
| 77 | + |
| 78 | + # Give `rr` a chance to cleanup |
| 79 | + sleep(60) |
| 80 | + |
| 81 | + if isopen(proc) |
| 82 | + println(stderr, "\n\n`rr` failed to cleanup within one minute, killing and exiting immediately!") |
| 83 | + kill(proc, Base.SIGKILL) |
| 84 | + exit(1) |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + # Wait for `rr` to finish, either through naturally finishing its run, or `SIGTERM`. |
| 90 | + # If we have to `SIGKILL` |
| 91 | + wait(proc) |
| 92 | + |
| 93 | + # On a non-zero exit code, upload the `rr` trace |
| 94 | + if !success(proc) |
| 95 | + println(stderr, "`rr` returned $(proc.exitcode), packing and uploading traces...") |
| 96 | + |
| 97 | + if !isdir(joinpath(dir, "rr_traces")) |
| 98 | + println(stderr, "No `rr_traces` directory! Did `rr` itself fail?") |
| 99 | + exit(1) |
| 100 | + end |
| 101 | + |
| 102 | + # Clean up non-traces |
| 103 | + rm(joinpath(dir, "rr_traces", "latest-trace")) |
| 104 | + rm(joinpath(dir, "rr_traces", "cpu_lock")) |
| 105 | + |
| 106 | + # Create a directory for the pack files to go |
| 107 | + pack_dir = joinpath(dir, "pack") |
| 108 | + mkdir(pack_dir) |
| 109 | + |
| 110 | + # Pack all traces |
| 111 | + trace_dirs = [joinpath(dir, "rr_traces", f) for f in readdir(joinpath(dir, "rr_traces"))] |
| 112 | + filter!(isdir, trace_dirs) |
| 113 | + run(ignorestatus(`$(rr_path) pack --pack-dir=$pack_dir $(trace_dirs)`)) |
| 114 | + |
| 115 | + # Tar it up |
| 116 | + mkpath("dumps") |
| 117 | + datestr = Dates.format(now(), dateformat"yyyy-mm-dd_HH_MM_SS") |
| 118 | + dst_path = "dumps/rr-run_$(run_id)-gitsha_$(shortcommit)-$(datestr).tar.zst" |
| 119 | + zstd_jll.zstdmt() do zstdp |
| 120 | + tarproc = open(`$zstdp -o $dst_path`, "w") |
| 121 | + Tar.create(dir, tarproc) |
| 122 | + close(tarproc.in) |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | +end |
| 127 | + |
| 128 | +# Pass the exit code back up to Buildkite |
| 129 | +if proc.termsignal != 0 |
| 130 | + ccall(:raise, Cvoid, (Cint,), proc.termsignal) |
| 131 | + exit(1) # Just in case the signal did not cause an exit |
| 132 | +else |
| 133 | + exit(proc.exitcode) |
| 134 | +end |
0 commit comments