Skip to content

Commit c6434d4

Browse files
Use printf instead of echo to write trace file (#5397)
Echo commands were combined into a single block to write to $trace_file more efficiently, reducing the number of system calls. Added error handling to prevent tracing from causing workflow failures. Signed-off-by: Nico Holguin <nholguin@illumina.com> Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com> Co-authored-by: nholguin <nholguin@illumina.com> Co-authored-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
1 parent 18f7de1 commit c6434d4

File tree

2 files changed

+60
-54
lines changed

2 files changed

+60
-54
lines changed

modules/nextflow/src/main/resources/nextflow/executor/command-trace.txt

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,28 @@ nxf_mem_watch() {
114114
done
115115

116116
## result struct: pid %mem vmem rss peak_vmem peak_rss
117-
echo "%mem=${nxf_stat_ret[1]}" >> $trace_file
118-
echo "vmem=${nxf_stat_ret[2]}" >> $trace_file
119-
echo "rss=${nxf_stat_ret[3]}" >> $trace_file
120-
echo "peak_vmem=${nxf_stat_ret[4]}" >> $trace_file
121-
echo "peak_rss=${nxf_stat_ret[5]}" >> $trace_file
122-
echo "vol_ctxt=${nxf_stat_ret[6]}" >> $trace_file
123-
echo "inv_ctxt=${nxf_stat_ret[7]}" >> $trace_file
117+
printf "%s\n" \
118+
"%mem=${nxf_stat_ret[1]}" \
119+
"vmem=${nxf_stat_ret[2]}" \
120+
"rss=${nxf_stat_ret[3]}" \
121+
"peak_vmem=${nxf_stat_ret[4]}" \
122+
"peak_rss=${nxf_stat_ret[5]}" \
123+
"vol_ctxt=${nxf_stat_ret[6]}" \
124+
"inv_ctxt=${nxf_stat_ret[7]}" >> "$trace_file" || >&2 echo "Error: Failed to append to file: $trace_file"
124125
}
125126

126127
nxf_write_trace() {
127-
echo "nextflow.trace/v2" > $trace_file
128-
echo "realtime=$wall_time" >> $trace_file
129-
echo "%cpu=$ucpu" >> $trace_file
130-
echo "cpu_model=$cpu_model" >> $trace_file
131-
echo "rchar=${io_stat1[0]}" >> $trace_file
132-
echo "wchar=${io_stat1[1]}" >> $trace_file
133-
echo "syscr=${io_stat1[2]}" >> $trace_file
134-
echo "syscw=${io_stat1[3]}" >> $trace_file
135-
echo "read_bytes=${io_stat1[4]}" >> $trace_file
136-
echo "write_bytes=${io_stat1[5]}" >> $trace_file
128+
printf "%s\n" \
129+
"nextflow.trace/v2" \
130+
"realtime=$wall_time" \
131+
"%cpu=$ucpu" \
132+
"cpu_model=$cpu_model" \
133+
"rchar=${io_stat1[0]}" \
134+
"wchar=${io_stat1[1]}" \
135+
"syscr=${io_stat1[2]}" \
136+
"syscw=${io_stat1[3]}" \
137+
"read_bytes=${io_stat1[4]}" \
138+
"write_bytes=${io_stat1[5]}" > "$trace_file" || >&2 echo "Error: Failed to write to file: $trace_file"
137139
}
138140

139141
nxf_trace_mac() {
@@ -199,16 +201,17 @@ nxf_trace_linux() {
199201
local wall_time=$((end_millis-start_millis))
200202
[ $NXF_DEBUG = 1 ] && echo "+++ STATS %CPU=$ucpu TIME=$wall_time I/O=${io_stat1[*]}"
201203

202-
echo "nextflow.trace/v2" > $trace_file
203-
echo "realtime=$wall_time" >> $trace_file
204-
echo "%cpu=$ucpu" >> $trace_file
205-
echo "cpu_model=$cpu_model" >> $trace_file
206-
echo "rchar=${io_stat1[0]}" >> $trace_file
207-
echo "wchar=${io_stat1[1]}" >> $trace_file
208-
echo "syscr=${io_stat1[2]}" >> $trace_file
209-
echo "syscw=${io_stat1[3]}" >> $trace_file
210-
echo "read_bytes=${io_stat1[4]}" >> $trace_file
211-
echo "write_bytes=${io_stat1[5]}" >> $trace_file
204+
printf "%s\n" \
205+
"nextflow.trace/v2" \
206+
"realtime=$wall_time" \
207+
"%cpu=$ucpu" \
208+
"cpu_model=$cpu_model" \
209+
"rchar=${io_stat1[0]}" \
210+
"wchar=${io_stat1[1]}" \
211+
"syscr=${io_stat1[2]}" \
212+
"syscw=${io_stat1[3]}" \
213+
"read_bytes=${io_stat1[4]}" \
214+
"write_bytes=${io_stat1[5]}" > "$trace_file" || >&2 echo "Error: Failed to write to file: $trace_file"
212215

213216
## join nxf_mem_watch
214217
[ -e /proc/$mem_proc ] && eval "echo 'DONE' >&$mem_fd" || true

modules/nextflow/src/test/resources/nextflow/executor/test-bash-wrapper-with-trace.txt

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,28 @@ nxf_mem_watch() {
9898
count=$((count+1))
9999
done
100100

101-
echo "%mem=${nxf_stat_ret[1]}" >> $trace_file
102-
echo "vmem=${nxf_stat_ret[2]}" >> $trace_file
103-
echo "rss=${nxf_stat_ret[3]}" >> $trace_file
104-
echo "peak_vmem=${nxf_stat_ret[4]}" >> $trace_file
105-
echo "peak_rss=${nxf_stat_ret[5]}" >> $trace_file
106-
echo "vol_ctxt=${nxf_stat_ret[6]}" >> $trace_file
107-
echo "inv_ctxt=${nxf_stat_ret[7]}" >> $trace_file
101+
printf "%s\n" \
102+
"%mem=${nxf_stat_ret[1]}" \
103+
"vmem=${nxf_stat_ret[2]}" \
104+
"rss=${nxf_stat_ret[3]}" \
105+
"peak_vmem=${nxf_stat_ret[4]}" \
106+
"peak_rss=${nxf_stat_ret[5]}" \
107+
"vol_ctxt=${nxf_stat_ret[6]}" \
108+
"inv_ctxt=${nxf_stat_ret[7]}" >> "$trace_file" || >&2 echo "Error: Failed to append to file: $trace_file"
108109
}
109110

110111
nxf_write_trace() {
111-
echo "nextflow.trace/v2" > $trace_file
112-
echo "realtime=$wall_time" >> $trace_file
113-
echo "%cpu=$ucpu" >> $trace_file
114-
echo "cpu_model=$cpu_model" >> $trace_file
115-
echo "rchar=${io_stat1[0]}" >> $trace_file
116-
echo "wchar=${io_stat1[1]}" >> $trace_file
117-
echo "syscr=${io_stat1[2]}" >> $trace_file
118-
echo "syscw=${io_stat1[3]}" >> $trace_file
119-
echo "read_bytes=${io_stat1[4]}" >> $trace_file
120-
echo "write_bytes=${io_stat1[5]}" >> $trace_file
112+
printf "%s\n" \
113+
"nextflow.trace/v2" \
114+
"realtime=$wall_time" \
115+
"%cpu=$ucpu" \
116+
"cpu_model=$cpu_model" \
117+
"rchar=${io_stat1[0]}" \
118+
"wchar=${io_stat1[1]}" \
119+
"syscr=${io_stat1[2]}" \
120+
"syscw=${io_stat1[3]}" \
121+
"read_bytes=${io_stat1[4]}" \
122+
"write_bytes=${io_stat1[5]}" > "$trace_file" || >&2 echo "Error: Failed to write to file: $trace_file"
121123
}
122124

123125
nxf_trace_mac() {
@@ -173,16 +175,17 @@ nxf_trace_linux() {
173175
local wall_time=$((end_millis-start_millis))
174176
[ $NXF_DEBUG = 1 ] && echo "+++ STATS %CPU=$ucpu TIME=$wall_time I/O=${io_stat1[*]}"
175177

176-
echo "nextflow.trace/v2" > $trace_file
177-
echo "realtime=$wall_time" >> $trace_file
178-
echo "%cpu=$ucpu" >> $trace_file
179-
echo "cpu_model=$cpu_model" >> $trace_file
180-
echo "rchar=${io_stat1[0]}" >> $trace_file
181-
echo "wchar=${io_stat1[1]}" >> $trace_file
182-
echo "syscr=${io_stat1[2]}" >> $trace_file
183-
echo "syscw=${io_stat1[3]}" >> $trace_file
184-
echo "read_bytes=${io_stat1[4]}" >> $trace_file
185-
echo "write_bytes=${io_stat1[5]}" >> $trace_file
178+
printf "%s\n" \
179+
"nextflow.trace/v2" \
180+
"realtime=$wall_time" \
181+
"%cpu=$ucpu" \
182+
"cpu_model=$cpu_model" \
183+
"rchar=${io_stat1[0]}" \
184+
"wchar=${io_stat1[1]}" \
185+
"syscr=${io_stat1[2]}" \
186+
"syscw=${io_stat1[3]}" \
187+
"read_bytes=${io_stat1[4]}" \
188+
"write_bytes=${io_stat1[5]}" > "$trace_file" || >&2 echo "Error: Failed to write to file: $trace_file"
186189

187190
[ -e /proc/$mem_proc ] && eval "echo 'DONE' >&$mem_fd" || true
188191
wait $mem_proc 2>/dev/null || true

0 commit comments

Comments
 (0)