Debian Codegraph @

lib/Test2/Harness/Stall/Report.pm

raw
1 package Test2::Harness::Stall::Report;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Carp qw/croak/;
8 use File::Spec();
9
10 use Test2::Harness::Util qw/clean_path write_file_atomic/;
11 use Test2::Harness::Util::JSON qw/encode_json/;
12
13 use Test2::Harness::Util::HashBase qw{
14 <workdir <run_id <report_dir
15 };
16
17 use constant BEGIN_MARKER => '--- YATH STALL REPORT BEGIN ---';
18 use constant END_MARKER => '--- YATH STALL REPORT END ---';
19
20 sub init {
21 my $self = shift;
22
23 croak "You must specify a workdir" unless defined $self->{+WORKDIR};
24
25 # Where yath was run from. Not the workdir: for a plain 'yath test' that is
26 # a File::Temp directory removed when the run ends, and this bundle is the
27 # artifact the whole feature exists to produce.
28 $self->{+REPORT_DIR} //= File::Spec->curdir;
29
30 return;
31 }
32
33 # What the run state looked like, taken from the replay rather than from any
34 # live harness object.
35 sub state_summary {
36 my $self = shift;
37 my ($state, $tasks) = @_;
38
39 my %out = (
40 running => $state->running // 0,
41 total_started => $state->total_started // 0,
42 queue_ended => $state->queue_ended ? 1 : 0,
43 halted_runs => [sort keys %{$state->halted_runs // {}}],
44 stages => $state->stage_readiness // {},
45 );
46
47 $out{running_tasks} = [map { {job_id => $_->{job_id}, file => $_->{rel_file} // $_->{file}} } values %{$state->running_tasks // {}}];
48
49 $out{pending_tasks} = [
50 map { {
51 job_id => $_->{job_id},
52 file => $_->{rel_file} // $_->{file},
53 category => $_->{category},
54 duration => $_->{duration},
55 stage => $_->{stage},
56 conflicts => $_->{conflicts} // [],
57 shares => $_->{shares} // [],
58 } } @$tasks
59 ];
60
61 return \%out;
62 }
63
64 sub headline {
65 my $self = shift;
66 my ($bundle) = @_;
67
68 return sprintf(
69 "yath: no test has started in %.0f seconds (%s threshold %s), with %d pending and %d running.",
70 $bundle->{idle}, $bundle->{tier}, $bundle->{threshold},
71 $bundle->{pending}, $bundle->{running},
72 );
73 }
74
75 sub render {
76 my $self = shift;
77 my ($bundle) = @_;
78
79 my $out = "\n" . BEGIN_MARKER . "\n";
80 $out .= $self->headline($bundle) . "\n";
81 $out .= "This may be benign: the scheduler can also be waiting on something legitimate.\n";
82 $out .= "It never ends the run. Use --stall-report to change or disable this.\n\n";
83
84 my $state = $bundle->{state_summary} // {};
85 $out .= sprintf(
86 "Pending: %d Running: %d Started so far: %d Queue ended: %s\n",
87 scalar(@{$state->{pending_tasks} // []}),
88 $state->{running} // 0,
89 $state->{total_started} // 0,
90 $state->{queue_ended} ? 'yes' : 'no',
91 );
92
93 my $stages = $state->{stages} // {};
94 $out .= "Stages: " . join(', ', map { "$_=" . ($stages->{$_} ? $stages->{$_} : 'down') } sort keys %$stages) . "\n";
95
96 $out .= $self->task_lines('Running', $state->{running_tasks});
97 $out .= $self->task_lines('Pending', $state->{pending_tasks});
98
99 for my $sample (@{$bundle->{samples} // []}) {
100 $out .= sprintf("\nSample %d:\n", $sample->{round});
101 for my $pid (sort { $a <=> $b } keys %{$sample->{procs} // {}}) {
102 my $proc = $sample->{procs}->{$pid};
103 next if $proc->{gone};
104 $out .= sprintf(
105 " pid %-7s %-18s state=%-22s wchan=%-24s syscall=%s\n",
106 $pid,
107 $proc->{name} // '?',
108 $proc->{state} // '?',
109 $proc->{wchan} // '?',
110 defined($proc->{syscall}) ? $proc->{syscall} : 'unavailable (needs ptrace permission)',
111 );
112 }
113 }
114
115 my $traces = $bundle->{traces} // {};
116 if (keys %$traces) {
117 $out .= "\nStack traces:\n";
118 $out .= $traces->{$_} . "\n" for sort keys %$traces;
119 }
120 else {
121 $out .= "\nNo stack traces were produced. A process that cannot run Perl -- one in an\n";
122 $out .= "uninterruptible syscall, or in XS that retries EINTR -- never runs the handler.\n";
123 }
124
125 $out .= "\nFull details: " . $self->json_file($bundle->{round}) . "\n";
126 $out .= END_MARKER . "\n";
127
128 return $out;
129 }
130
131 # The tests themselves, since which ones are stuck waiting is usually the first
132 # thing a reader wants. Bounded: a stalled run can have hundreds pending, and
133 # the full list is in the JSON.
134 sub task_lines {
135 my $self = shift;
136 my ($label, $tasks) = @_;
137
138 $tasks //= [];
139 return '' unless @$tasks;
140
141 my $out = "\n$label (" . scalar(@$tasks) . "):\n";
142
143 my $shown = 0;
144 for my $task (@$tasks) {
145 last if $shown++ >= 20;
146
147 my @extra;
148 push @extra => $task->{category} if $task->{category} && $task->{category} ne 'general';
149 push @extra => 'conflicts: ' . join(',', @{$task->{conflicts}}) if @{$task->{conflicts} // []};
150 push @extra => 'shares: ' . join(',', @{$task->{shares}}) if @{$task->{shares} // []};
151
152 $out .= sprintf(" %s%s\n", $task->{file} // $task->{job_id} // '?', @extra ? ' [' . join('; ', @extra) . ']' : '');
153 }
154
155 $out .= sprintf(" ... and %d more\n", scalar(@$tasks) - $shown) if @$tasks > $shown;
156
157 return $out;
158 }
159
160 sub json_file {
161 my $self = shift;
162 my ($round) = @_;
163
164 # The run id keeps bundles from different runs apart; the round keeps a
165 # run's own reports from overwriting each other.
166 my $name = join('-', 'yath-stall-report', $self->{+RUN_ID} // $$, $round // 1) . '.json';
167
168 return clean_path(File::Spec->catfile($self->{+REPORT_DIR}, $name));
169 }
170
171 # The aux_logs name matters. The collector re-reads that directory on every
172 # poll, picks up files created mid-run, and tags anything ending -STDERR.log as
173 # debug output, so this reaches the yath UI with no new plumbing. error.log
174 # cannot be used: it is the runner's redirected STDERR, opened without append,
175 # so a second writer and the runner would overwrite each other.
176 sub aux_file {
177 my $self = shift;
178
179 my $dir = File::Spec->catdir($self->{+WORKDIR}, 'aux_logs');
180 mkdir($dir);
181
182 return File::Spec->catfile($dir, 'stall-STDERR.log');
183 }
184
185 sub emit {
186 my $self = shift;
187 my ($bundle) = @_;
188
189 my $text = $self->render($bundle);
190 my $json = encode_json($bundle);
191
192 # The terminal and the CI log get the readable form only. The bundle is
193 # large and would drown the report it belongs to.
194 print STDERR $text;
195
196 if (open(my $fh, '>>', $self->aux_file)) {
197 print $fh $text;
198
199 # One physical line, so the collector turns the whole bundle into a
200 # single info facet rather than one per line. encode_json escapes
201 # embedded newlines, so this holds however long the bundle gets. It can
202 # be pulled back out of that facet later; a binary attachment would be
203 # tidier but the aux log carries text only.
204 print $fh $json, "\n";
205
206 close($fh);
207 }
208
209 my $file = $self->json_file($bundle->{round});
210 my $ok = eval { write_file_atomic($file, $json . "\n"); 1 };
211 warn "Could not write stall report '$file': $@" unless $ok;
212
213 return $text;
214 }
215
216 1;
217
218 __END__
219
220 =pod
221
222 =encoding UTF-8
223
224 =head1 NAME
225
226 Test2::Harness::Stall::Report - Render and deliver a stall report.
227
228 =head1 DESCRIPTION
229
230 Turns a capture bundle into something a person and an analysis agent can both
231 read, and puts it where both will find it.
232
233 Reports go to two places. The main process's STDERR is never redirected by
234 yath, so it reaches the CI log directly, even if the event pipeline is itself
235 unhealthy. C<aux_logs/stall-STDERR.log> is picked up by
236 L<Test2::Harness::Collector>, which re-reads that directory on every poll and
237 tags anything ending C<-STDERR.log> as debug output, so it reaches the yath UI
238 with no new plumbing.
239
240 The runner's C<error.log> is deliberately not used: it is opened without
241 append, so a second writer and the runner would overwrite each other.
242
243 The full bundle is also written as JSON, twice. Once to the aux log as a
244 single physical line, so it reaches the yath UI as one info facet and can be
245 pulled back out later. Once to a file, outside the working directory, because
246 for a plain C<yath test> that directory is temporary and is removed when the
247 run ends; that copy goes to the directory yath was run from unless
248 C<--stall-report-dir> says otherwise. The text report is a reduced view; open files, locks, the process
249 tree and C<strace> output live only in the JSON. Begin and end markers let the
250 text be extracted from a log by pattern.
251
252 =head1 SYNOPSIS
253
254 use Test2::Harness::Stall::Report;
255
256 my $report = Test2::Harness::Stall::Report->new(
257 workdir => $workdir,
258 run_id => $run_id,
259 );
260
261 $bundle->{state_summary} = $report->state_summary($state, $tasks);
262 $report->emit($bundle);
263
264 =head1 ATTRIBUTES
265
266 =over 4
267
268 =item $string = $report->workdir()
269
270 The run's working directory, used for the aux log.
271
272 =item $string = $report->run_id()
273
274 Names the JSON bundle, so bundles from different runs can sit together.
275
276 =item $string = $report->report_dir()
277
278 Where the JSON bundle is written. Defaults to the directory yath was run
279 from.
280
281 =back
282
283 =head1 PUBLIC METHODS
284
285 =over 4
286
287 =item $hashref = $report->state_summary($state, $tasks)
288
289 What the run looked like, taken from the replay rather than any live harness
290 object.
291
292 =item $string = $report->render($bundle)
293
294 The human-readable report, between begin and end markers.
295
296 =item $string = $report->emit($bundle)
297
298 Renders, writes to both channels and the JSON bundle, and returns the text.
299
300 =item $string = $report->headline($bundle)
301
302 One line saying what was observed.
303
304 =item $string = $report->task_lines($label, $tasks)
305
306 A bounded list of tests; the full list is in the JSON.
307
308 =item $string = $report->json_file($round)
309
310 Where the bundle for a given report is written.
311
312 =item $string = $report->aux_file()
313
314 The aux log the collector forwards.
315
316 =back
317
318 =head1 SOURCE
319
320 The source code repository for Test2-Harness can be found at
321 L<https://github.com/Test-More/Test2-Harness/>.
322
323 =head1 MAINTAINERS
324
325 =over 4
326
327 =item Chad Granum E<lt>[email protected]<gt>
328
329 =back
330
331 =head1 AUTHORS
332
333 =over 4
334
335 =item Chad Granum E<lt>[email protected]<gt>
336
337 =back
338
339 =head1 COPYRIGHT
340
341 Copyright Chad Granum E<lt>[email protected]<gt>.
342
343 This program is free software; you can redistribute it and/or
344 modify it under the same terms as Perl itself.
345
346 See L<https://dev.perl.org/licenses/>
347
348 =cut