| 1 |
package Test2::Harness::Stall::Capture; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/croak/; |
| 8 |
use Config qw/%Config/; |
| 9 |
use File::Spec(); |
| 10 |
use Time::HiRes qw/sleep time/; |
| 11 |
|
| 12 |
use Test2::Harness::Util::HashBase qw{ |
| 13 |
<workdir <stall_dir |
| 14 |
<root_pid |
| 15 |
}; |
| 16 |
|
| 17 |
# Rounds of per-process sampling, and the gap between them. More than one |
| 18 |
# sample is what separates a process wedged on a single operation from one |
| 19 |
# looping without progress, which is the first question any analysis asks. |
| 20 |
use constant ROUNDS => 3; |
| 21 |
use constant ROUND_GAP => 2; |
| 22 |
use constant SIG_WAIT => 1; |
| 23 |
use constant MAX_FIELD => 8192; |
| 24 |
|
| 25 |
# The whole capture blocks the render loop. Nothing is being rendered during a |
| 26 |
# stall, but keep the bound small enough that a benign report is not felt. |
| 27 |
use constant SHELL_SECONDS => 1; |
| 28 |
|
| 29 |
sub init { |
| 30 |
my $self = shift; |
| 31 |
|
| 32 |
croak "You must specify a workdir" unless defined $self->{+WORKDIR}; |
| 33 |
|
| 34 |
$self->{+STALL_DIR} //= File::Spec->catdir($self->{+WORKDIR}, 'stall'); |
| 35 |
$self->{+ROOT_PID} //= $$; |
| 36 |
|
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
sub have_proc { return ($^O eq 'linux' && -d '/proc') ? 1 : 0 } |
| 41 |
|
| 42 |
sub read_proc { |
| 43 |
my $self = shift; |
| 44 |
my ($pid, $what) = @_; |
| 45 |
|
| 46 |
my $path = File::Spec->catfile('/proc', $pid, $what); |
| 47 |
|
| 48 |
open(my $fh, '<', $path) or return; |
| 49 |
local $/; |
| 50 |
my $out = <$fh>; |
| 51 |
close($fh); |
| 52 |
|
| 53 |
# /proc/PID/syscall and friends are gated on ptrace permission. When it is |
| 54 |
# denied the open succeeds and the read comes back empty, so the read is |
| 55 |
# what has to be checked, not the open. |
| 56 |
return unless defined $out && length $out; |
| 57 |
|
| 58 |
$out =~ s/\0/ /g; |
| 59 |
$out =~ s/\s+\z//; |
| 60 |
|
| 61 |
return substr($out, 0, MAX_FIELD); |
| 62 |
} |
| 63 |
|
| 64 |
sub list_fds { |
| 65 |
my $self = shift; |
| 66 |
my ($pid) = @_; |
| 67 |
|
| 68 |
my $dir = File::Spec->catdir('/proc', $pid, 'fd'); |
| 69 |
|
| 70 |
opendir(my $dh, $dir) or return; |
| 71 |
my @fds = grep { m/^\d+$/ } readdir($dh); |
| 72 |
closedir($dh); |
| 73 |
|
| 74 |
my %out; |
| 75 |
for my $fd (sort { $a <=> $b } @fds) { |
| 76 |
my $target = readlink(File::Spec->catfile($dir, $fd)) // next; |
| 77 |
$out{$fd} = $target; |
| 78 |
} |
| 79 |
|
| 80 |
return \%out; |
| 81 |
} |
| 82 |
|
| 83 |
sub proc_snapshot { |
| 84 |
my $self = shift; |
| 85 |
my ($pid) = @_; |
| 86 |
|
| 87 |
my %out = (pid => $pid); |
| 88 |
|
| 89 |
unless ($self->have_proc) { |
| 90 |
# Not the same as gone: the process is alive, we simply cannot see it. |
| 91 |
$out{unavailable} = "no /proc on $^O"; |
| 92 |
return \%out; |
| 93 |
} |
| 94 |
|
| 95 |
my $status = $self->read_proc($pid, 'status'); |
| 96 |
unless (defined $status) { |
| 97 |
$out{gone} = 1; |
| 98 |
return \%out; |
| 99 |
} |
| 100 |
|
| 101 |
for my $field (qw/Name State PPid Threads SigBlk SigIgn SigCgt/) { |
| 102 |
next unless $status =~ m/^\Q$field\E:\s*(.*)$/m; |
| 103 |
$out{lc($field)} = $1; |
| 104 |
} |
| 105 |
|
| 106 |
$out{cmdline} = $self->read_proc($pid, 'cmdline'); |
| 107 |
$out{wchan} = $self->read_proc($pid, 'wchan'); |
| 108 |
$out{syscall} = $self->read_proc($pid, 'syscall'); |
| 109 |
$out{stack} = $self->read_proc($pid, 'stack'); |
| 110 |
$out{fds} = $self->list_fds($pid); |
| 111 |
|
| 112 |
return \%out; |
| 113 |
} |
| 114 |
|
| 115 |
# Every descendant of the runner, so a wedged process that is not on the |
| 116 |
# whitelist still appears in the report even though it is never signalled. |
| 117 |
sub all_pids { |
| 118 |
my $self = shift; |
| 119 |
|
| 120 |
opendir(my $dh, '/proc') or return; |
| 121 |
my @pids = grep { m/^\d+$/ } readdir($dh); |
| 122 |
closedir($dh); |
| 123 |
|
| 124 |
return \@pids; |
| 125 |
} |
| 126 |
|
| 127 |
sub process_tree { |
| 128 |
my $self = shift; |
| 129 |
my ($root) = @_; |
| 130 |
|
| 131 |
return unless $root && $self->have_proc; |
| 132 |
|
| 133 |
my $pids = $self->all_pids or return; |
| 134 |
|
| 135 |
my %parent; |
| 136 |
for my $pid (@$pids) { |
| 137 |
my $stat = $self->read_proc($pid, 'stat') or next; |
| 138 |
|
| 139 |
# comm can contain spaces and parens, so parse after the last ')'. |
| 140 |
my $tail = substr($stat, rindex($stat, ')') + 1); |
| 141 |
next unless $tail =~ m/^\s+\S+\s+(\d+)/; |
| 142 |
$parent{$pid} = $1; |
| 143 |
} |
| 144 |
|
| 145 |
my %keep = ($root => 1); |
| 146 |
my $added = 1; |
| 147 |
while ($added) { |
| 148 |
$added = 0; |
| 149 |
for my $pid (keys %parent) { |
| 150 |
next if $keep{$pid}; |
| 151 |
next unless $keep{$parent{$pid}}; |
| 152 |
$keep{$pid} = 1; |
| 153 |
$added++; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return [sort { $a <=> $b } keys %keep]; |
| 158 |
} |
| 159 |
|
| 160 |
sub system_snapshot { |
| 161 |
my $self = shift; |
| 162 |
|
| 163 |
my %out; |
| 164 |
|
| 165 |
return \%out unless $self->have_proc; |
| 166 |
|
| 167 |
for my $file (qw/loadavg meminfo locks/) { |
| 168 |
open(my $fh, '<', "/proc/$file") or next; |
| 169 |
local $/; |
| 170 |
my $content = <$fh>; |
| 171 |
close($fh); |
| 172 |
next unless defined $content; |
| 173 |
|
| 174 |
# meminfo is long and only its head is ever useful here. |
| 175 |
if ($file eq 'meminfo') { |
| 176 |
my @lines = split /\n/, $content; |
| 177 |
$content = join("\n", @lines[0 .. ($#lines < 8 ? $#lines : 8)]); |
| 178 |
} |
| 179 |
|
| 180 |
$out{$file} = substr($content, 0, MAX_FIELD); |
| 181 |
} |
| 182 |
|
| 183 |
return \%out; |
| 184 |
} |
| 185 |
|
| 186 |
sub workdir_filesystem { |
| 187 |
my $self = shift; |
| 188 |
|
| 189 |
return unless $self->have_proc; |
| 190 |
|
| 191 |
my $seconds = SHELL_SECONDS; |
| 192 |
my $dir = $self->{+WORKDIR}; |
| 193 |
$dir =~ s/'/'\\''/g; |
| 194 |
|
| 195 |
# Bounded, and quoted: a hung mount is a plausible cause of the stall being |
| 196 |
# diagnosed, and an unbounded df against one would hang the render loop. |
| 197 |
my $out = `timeout $seconds df -PT '$dir' 2>/dev/null`; |
| 198 |
return unless defined $out && length $out; |
| 199 |
|
| 200 |
return substr($out, 0, MAX_FIELD); |
| 201 |
} |
| 202 |
|
| 203 |
sub strace { |
| 204 |
my $self = shift; |
| 205 |
my ($pid) = @_; |
| 206 |
|
| 207 |
# Usually denied. Yama checks the tracer against the target's ancestry and |
| 208 |
# strace is a freshly exec'd process, so it is never an ancestor of the |
| 209 |
# scheduler no matter who launches it. Kept because some CI containers run |
| 210 |
# with ptrace_scope=0 or CAP_SYS_PTRACE; /proc/PID/syscall and wchan answer |
| 211 |
# the same question when it is not available. |
| 212 |
my $seconds = SHELL_SECONDS; |
| 213 |
my $out = `timeout $seconds strace -qq -p $pid -e trace=all 2>&1`; |
| 214 |
|
| 215 |
return unless defined $out && length $out; |
| 216 |
|
| 217 |
return substr($out, 0, MAX_FIELD); |
| 218 |
} |
| 219 |
|
| 220 |
sub read_traces { |
| 221 |
my $self = shift; |
| 222 |
|
| 223 |
my $dir = $self->{+STALL_DIR}; |
| 224 |
opendir(my $dh, $dir) or return {}; |
| 225 |
my @files = grep { m/^stack-\d+-\d+\.txt$/ } readdir($dh); |
| 226 |
closedir($dh); |
| 227 |
|
| 228 |
my %out; |
| 229 |
for my $file (@files) { |
| 230 |
my $path = File::Spec->catfile($dir, $file); |
| 231 |
|
| 232 |
open(my $fh, '<', $path) or next; |
| 233 |
local $/; |
| 234 |
my $content = <$fh>; |
| 235 |
close($fh); |
| 236 |
next unless defined $content; |
| 237 |
$out{$file} = substr($content, 0, MAX_FIELD); |
| 238 |
|
| 239 |
# Consume them, so the next report shows only its own traces. Filtering |
| 240 |
# by round number instead would need every process to have been |
| 241 |
# signalled in every round, which a stage that comes up late has not. |
| 242 |
unlink($path); |
| 243 |
} |
| 244 |
|
| 245 |
return \%out; |
| 246 |
} |
| 247 |
|
| 248 |
# Bit for a signal number in the SigCgt / SigIgn masks of /proc/PID/status. |
| 249 |
sub _sig_bit { return 1 << ($_[1] - 1) } |
| 250 |
|
| 251 |
# Not a constant: SIGUSR1 is 10 on x86 but 16 on MIPS and 30 on SPARC, and |
| 252 |
# have_proc only narrows this to Linux. |
| 253 |
my $USR1; |
| 254 |
|
| 255 |
sub usr1_number { |
| 256 |
return $USR1 //= do { |
| 257 |
my @names = split ' ', $Config{sig_name}; |
| 258 |
my @nums = split ' ', $Config{sig_num}; |
| 259 |
|
| 260 |
my %map; |
| 261 |
@map{@names} = @nums; |
| 262 |
|
| 263 |
$map{USR1} // 10; |
| 264 |
}; |
| 265 |
} |
| 266 |
|
| 267 |
sub catches_usr1 { |
| 268 |
my $self = shift; |
| 269 |
my ($proc) = @_; |
| 270 |
|
| 271 |
my $mask = $proc->{sigcgt} or return 0; |
| 272 |
return 0 unless $mask =~ m/^[0-9a-fA-F]+$/; |
| 273 |
|
| 274 |
# The field is a 64 bit mask written as hex, which overflows hex() on a 32 |
| 275 |
# bit perl. Only the low word holds the ordinary signals, and SIGUSR1 is |
| 276 |
# one of them. |
| 277 |
return (hex(substr($mask, -8)) & $self->_sig_bit($self->usr1_number)) ? 1 : 0; |
| 278 |
} |
| 279 |
|
| 280 |
sub kill_pid { return kill('USR1', $_[1]) } |
| 281 |
|
| 282 |
sub signal_pids { |
| 283 |
my $self = shift; |
| 284 |
my ($pids, $procs) = @_; |
| 285 |
|
| 286 |
$procs //= {}; |
| 287 |
|
| 288 |
my @sent; |
| 289 |
for my $pid (@$pids) { |
| 290 |
|
| 291 |
# Never a process group. A negative pid here would signal one, and |
| 292 |
# SIGUSR1's default action is to terminate. |
| 293 |
next unless $pid && $pid > 0; |
| 294 |
|
| 295 |
# Only a process that actually installed the handler. A test job sheds |
| 296 |
# it when the runner restores the original %SIG, so a signal there |
| 297 |
# kills a running test; and a pid recorded earlier may since have |
| 298 |
# exited and been recycled by something unrelated. SigCgt answers both |
| 299 |
# exactly, and proc_snapshot already collected it moments ago. |
| 300 |
if ($self->have_proc) { |
| 301 |
my $proc = $procs->{$pid}; |
| 302 |
next unless $proc && !$proc->{gone}; |
| 303 |
next unless $self->catches_usr1($proc); |
| 304 |
} |
| 305 |
|
| 306 |
push @sent => $pid if $self->kill_pid($pid); |
| 307 |
} |
| 308 |
|
| 309 |
return \@sent; |
| 310 |
} |
| 311 |
|
| 312 |
sub collect { |
| 313 |
my $self = shift; |
| 314 |
my ($info) = @_; |
| 315 |
|
| 316 |
my $whitelist = $info->{harness_pids} // []; |
| 317 |
|
| 318 |
mkdir($self->{+STALL_DIR}); |
| 319 |
|
| 320 |
my %out = ( |
| 321 |
time => time, |
| 322 |
tier => $info->{tier}, |
| 323 |
idle => $info->{idle}, |
| 324 |
threshold => $info->{threshold}, |
| 325 |
pending => $info->{pending}, |
| 326 |
running => $info->{running}, |
| 327 |
round => $info->{round}, |
| 328 |
harness_pids => $whitelist, |
| 329 |
have_proc => $self->have_proc, |
| 330 |
system => $self->system_snapshot, |
| 331 |
filesystem => $self->workdir_filesystem, |
| 332 |
tree => $self->process_tree($self->{+ROOT_PID}), |
| 333 |
samples => [], |
| 334 |
); |
| 335 |
|
| 336 |
my $tree = $out{tree} // $whitelist; |
| 337 |
|
| 338 |
for my $round (1 .. ROUNDS) { |
| 339 |
# Collect before signalling. A process in an uninterruptible syscall |
| 340 |
# never runs its handler, and that is exactly the case most in need of |
| 341 |
# description, so the external evidence must not depend on a reply. |
| 342 |
my %sample = (round => $round, procs => {}); |
| 343 |
my %seen; |
| 344 |
$sample{procs}->{$_} = $self->proc_snapshot($_) |
| 345 |
for grep { !$seen{$_}++ } @$tree, @$whitelist; |
| 346 |
|
| 347 |
$sample{strace} = $self->strace($info->{scheduler_pid}) |
| 348 |
if $round == 1 && $info->{scheduler_pid}; |
| 349 |
|
| 350 |
$sample{signalled} = $self->signal_pids($whitelist, $sample{procs}); |
| 351 |
|
| 352 |
push @{$out{samples}} => \%sample; |
| 353 |
|
| 354 |
sleep(ROUND_GAP) if $round < ROUNDS; |
| 355 |
} |
| 356 |
|
| 357 |
# Whatever arrived. A missing trace is itself evidence: no stack plus an |
| 358 |
# uninterruptible state says the process could not run Perl at all. |
| 359 |
sleep(SIG_WAIT); |
| 360 |
$out{traces} = $self->read_traces(); |
| 361 |
|
| 362 |
return \%out; |
| 363 |
} |
| 364 |
|
| 365 |
1; |
| 366 |
|
| 367 |
__END__ |
| 368 |
|
| 369 |
=pod |
| 370 |
|
| 371 |
=encoding UTF-8 |
| 372 |
|
| 373 |
=head1 NAME |
| 374 |
|
| 375 |
Test2::Harness::Stall::Capture - Collect evidence about a stalled run from |
| 376 |
outside the stuck processes. |
| 377 |
|
| 378 |
=head1 DESCRIPTION |
| 379 |
|
| 380 |
Gathers everything obtainable without the stuck process's cooperation, then |
| 381 |
asks each harness process for its Perl call stack. |
| 382 |
|
| 383 |
The order matters. A process in an uninterruptible syscall, or in XS that |
| 384 |
retries C<EINTR>, never runs a signal handler, so external evidence must not |
| 385 |
depend on a reply. A missing stack is itself a finding: no stack plus an |
| 386 |
uninterruptible state says the process could not run Perl at all. |
| 387 |
|
| 388 |
Each report samples several times a few seconds apart. Frames and syscalls that |
| 389 |
move between samples mean a loop making no progress; frames that do not mean |
| 390 |
the process is stuck on one operation. That distinction is the first question |
| 391 |
any analysis asks. |
| 392 |
|
| 393 |
C<strace> is attempted and usually denied: Yama checks the tracer against the |
| 394 |
target's ancestry, and C<strace> is a freshly exec'd process, so it is never an |
| 395 |
ancestor of the scheduler. It is kept for hosts that permit it, and |
| 396 |
C</proc/PID/wchan> and C</proc/PID/syscall> answer the same question when it |
| 397 |
does not. Every shell-out is bounded, because this blocks the caller. |
| 398 |
|
| 399 |
Everything here is Linux-specific and degrades to the state dump and the stack |
| 400 |
traces elsewhere. |
| 401 |
|
| 402 |
=head1 SYNOPSIS |
| 403 |
|
| 404 |
use Test2::Harness::Stall::Capture; |
| 405 |
|
| 406 |
my $capture = Test2::Harness::Stall::Capture->new( |
| 407 |
workdir => $workdir, |
| 408 |
root_pid => $$, |
| 409 |
); |
| 410 |
|
| 411 |
my $bundle = $capture->collect(\%info); |
| 412 |
|
| 413 |
=head1 ATTRIBUTES |
| 414 |
|
| 415 |
=over 4 |
| 416 |
|
| 417 |
=item $string = $capture->workdir() |
| 418 |
|
| 419 |
The run's working directory. |
| 420 |
|
| 421 |
=item $string = $capture->stall_dir() |
| 422 |
|
| 423 |
Where the signalled processes leave their stack traces. Defaults to C<stall> |
| 424 |
inside the working directory. |
| 425 |
|
| 426 |
=item $int = $capture->root_pid() |
| 427 |
|
| 428 |
Root of the process tree to describe. Defaults to the current process. |
| 429 |
|
| 430 |
=back |
| 431 |
|
| 432 |
=head1 PUBLIC METHODS |
| 433 |
|
| 434 |
=over 4 |
| 435 |
|
| 436 |
=item $hashref = $capture->collect(\%info) |
| 437 |
|
| 438 |
Runs the whole capture and returns the bundle. C<%info> carries what the |
| 439 |
detector observed, and the pids to signal as C<harness_pids>. |
| 440 |
|
| 441 |
=item $bool = $capture->have_proc() |
| 442 |
|
| 443 |
True on a platform where C</proc> can be read. |
| 444 |
|
| 445 |
=item $hashref = $capture->proc_snapshot($pid) |
| 446 |
|
| 447 |
State, wait channel, syscall, command line, caught signals and open files for |
| 448 |
one process. |
| 449 |
|
| 450 |
=item $string = $capture->read_proc($pid, $what) |
| 451 |
|
| 452 |
One C</proc/PID> file, or nothing. Some of these are gated on ptrace |
| 453 |
permission, and when it is refused the open succeeds and the read comes back |
| 454 |
empty -- so the read is what must be checked. |
| 455 |
|
| 456 |
=item $hashref = $capture->list_fds($pid) |
| 457 |
|
| 458 |
File descriptor number to what it points at. |
| 459 |
|
| 460 |
=item $arrayref = $capture->process_tree($pid) |
| 461 |
|
| 462 |
Every descendant of a pid, including it. |
| 463 |
|
| 464 |
=item $arrayref = $capture->all_pids() |
| 465 |
|
| 466 |
Every pid on the system, or nothing where there is no C</proc>. |
| 467 |
|
| 468 |
=item $bool = $capture->kill_pid($pid) |
| 469 |
|
| 470 |
Sends C<SIGUSR1> to one pid. |
| 471 |
|
| 472 |
=item $arrayref = $capture->signal_pids(\@pids, \%procs) |
| 473 |
|
| 474 |
Sends C<SIGUSR1> to each pid that this round's snapshot shows alive and |
| 475 |
catching that signal, and returns those actually signalled. A process that did |
| 476 |
not install the handler is never signalled, because the default action would |
| 477 |
terminate it. Where there is no C</proc> that check cannot be made and every |
| 478 |
positive pid in the list is signalled. Never signals a process group. |
| 479 |
|
| 480 |
=item $bool = $capture->catches_usr1(\%proc) |
| 481 |
|
| 482 |
Whether a snapshot shows the process catching C<SIGUSR1>, read from C<SigCgt>. |
| 483 |
|
| 484 |
=item $int = $capture->usr1_number() |
| 485 |
|
| 486 |
The local signal number for C<SIGUSR1>. |
| 487 |
|
| 488 |
=item $hashref = $capture->system_snapshot() |
| 489 |
|
| 490 |
Load, memory and the lock table. |
| 491 |
|
| 492 |
=item $string = $capture->workdir_filesystem() |
| 493 |
|
| 494 |
Filesystem type and free space for the working directory. |
| 495 |
|
| 496 |
=item $string = $capture->strace($pid) |
| 497 |
|
| 498 |
Best-effort C<strace> output, or its refusal. |
| 499 |
|
| 500 |
=item $hashref = $capture->read_traces() |
| 501 |
|
| 502 |
Stack traces written since the last call, keyed by filename. Consumes them. |
| 503 |
|
| 504 |
=back |
| 505 |
|
| 506 |
=head1 SOURCE |
| 507 |
|
| 508 |
The source code repository for Test2-Harness can be found at |
| 509 |
L<https://github.com/Test-More/Test2-Harness/>. |
| 510 |
|
| 511 |
=head1 MAINTAINERS |
| 512 |
|
| 513 |
=over 4 |
| 514 |
|
| 515 |
=item Chad Granum E<lt>[email protected]<gt> |
| 516 |
|
| 517 |
=back |
| 518 |
|
| 519 |
=head1 AUTHORS |
| 520 |
|
| 521 |
=over 4 |
| 522 |
|
| 523 |
=item Chad Granum E<lt>[email protected]<gt> |
| 524 |
|
| 525 |
=back |
| 526 |
|
| 527 |
=head1 COPYRIGHT |
| 528 |
|
| 529 |
Copyright Chad Granum E<lt>[email protected]<gt>. |
| 530 |
|
| 531 |
This program is free software; you can redistribute it and/or |
| 532 |
modify it under the same terms as Perl itself. |
| 533 |
|
| 534 |
See L<https://dev.perl.org/licenses/> |
| 535 |
|
| 536 |
=cut |