This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
rr_capture.jl
108 lines (88 loc) · 3.79 KB
/
rr_capture.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Pkg, Dates, Tar
if length(ARGS) < 3
println(stderr, "Usage: rr_capture.jl [buildnumber] [shortcommit] [command...]")
exit(2)
end
const TIMEOUT = 2*60*60 # seconds
run_id = popfirst!(ARGS)
shortcommit = popfirst!(ARGS)
num_cores = min(Sys.CPU_THREADS, 8, parse(Int, get(ENV, "JULIA_TEST_NUM_CORES", "8")) + 1)
proc = nothing
new_env = copy(ENV)
mktempdir() do dir
Pkg.activate(dir)
Pkg.add("rr_jll")
Pkg.add("Zstd_jll")
rr_jll = Base.require(Base.PkgId(Base.UUID((0xe86bdf43_55f7_5ea2_9fd0_e7daa2c0f2b4)), "rr_jll"))
zstd_jll = Base.require(Base.PkgId(Base.UUID((0x3161d3a3_bdf6_5164_811a_617609db77b4)), "Zstd_jll"))
rr(func) = Base.invokelatest(rr_jll.rr, func; adjust_LIBPATH=false)
rr() do rr_path
capture_script_path = joinpath(dir, "capture_output.sh")
loader = Sys.WORD_SIZE == 64 ? "/lib64/ld-linux-x86-64.so.2" : "/lib/ld-linux.so.2"
open(capture_script_path, "w") do io
write(io, """
#!/bin/bash
$(rr_path) record --nested=detach "\$@" > >(tee -a $(dir)/stdout.log) 2> >(tee -a $(dir)/stderr.log >&2)
""")
end
chmod(capture_script_path, 0o755)
new_env = copy(ENV)
new_env["_RR_TRACE_DIR"] = joinpath(dir, "rr_traces")
new_env["JULIA_RR"] = capture_script_path
t_start = time()
global proc = run(setenv(`$(rr_path) record --num-cores=$(num_cores) $ARGS`, new_env), (stdin, stdout, stderr); wait=false)
# Start asynchronous timer that will kill `rr`
@async begin
sleep(TIMEOUT)
# If we've exceeded the timeout and `rr` is still running, kill it.
if isopen(proc)
println(stderr, "\n\nProcess timed out. Signalling `rr` for force-cleanup!")
kill(proc, Base.SIGTERM)
# Give `rr` a chance to cleanup
sleep(60)
if isopen(proc)
println(stderr, "\n\n`rr` failed to cleanup within one minute, killing and exiting immediately!")
kill(proc, Base.SIGKILL)
exit(1)
end
end
end
# Wait for `rr` to finish, either through naturally finishing its run, or `SIGTERM`.
# If we have to `SIGKILL`
wait(proc)
# On a non-zero exit code, upload the `rr` trace
if !success(proc)
println(stderr, "`rr` returned $(proc.exitcode), packing and uploading traces...")
if !isdir(joinpath(dir, "rr_traces"))
println(stderr, "No `rr_traces` directory! Did `rr` itself fail?")
exit(1)
end
# Clean up non-traces
rm(joinpath(dir, "rr_traces", "latest-trace"))
rm(joinpath(dir, "rr_traces", "cpu_lock"))
# Create a directory for the pack files to go
pack_dir = joinpath(dir, "pack")
mkdir(pack_dir)
# Pack all traces
trace_dirs = [joinpath(dir, "rr_traces", f) for f in readdir(joinpath(dir, "rr_traces"))]
filter!(isdir, trace_dirs)
run(ignorestatus(`$(rr_path) pack --pack-dir=$pack_dir $(trace_dirs)`))
# Tar it up
mkpath("dumps")
datestr = Dates.format(now(), dateformat"yyyy-mm-dd_HH_MM_SS")
dst_path = "dumps/rr-run_$(run_id)-gitsha_$(shortcommit)-$(datestr).tar.zst"
zstd_jll.zstdmt() do zstdp
tarproc = open(`$zstdp -o $dst_path`, "w")
Tar.create(dir, tarproc)
close(tarproc.in)
end
end
end
end
# Pass the exit code back up to buildbot
if proc.termsignal != 0
ccall(:raise, Cvoid, (Cint,), proc.termsignal)
exit(1) # Just in case the signal did not cause an exit
else
exit(proc.exitcode)
end