# Copyright (c) 2024-2026, PostgreSQL Global Development Group
|
|
=pod
|
|
=head1 NAME
|
|
TestAio - helpers for writing AIO related tests
|
|
=cut
|
|
package TestAio;
|
|
use strict;
|
use warnings FATAL => 'all';
|
|
use PostgreSQL::Test::Cluster;
|
use PostgreSQL::Test::Utils;
|
use Text::ParseWords qw(shellwords); # [PG_CUSTOM: CFS]
|
use Test::More;
|
|
|
=pod
|
|
=head1 METHODS
|
|
=over
|
|
=item TestAio::supported_io_methods()
|
|
Return an array of all the supported values for the io_method GUC
|
|
=cut
|
|
sub supported_io_methods()
|
{
|
my @io_methods = ('worker');
|
|
push(@io_methods, "io_uring") if have_io_uring();
|
|
# Return sync last, as it will least commonly fail
|
push(@io_methods, "sync");
|
|
return @io_methods;
|
}
|
|
|
=item TestAio::configure()
|
|
Prepare a cluster for AIO test
|
|
=cut
|
|
sub configure
|
{
|
my $node = shift;
|
|
$node->append_conf(
|
'postgresql.conf', qq(
|
shared_preload_libraries=test_aio
|
log_min_messages = 'DEBUG3'
|
log_statement=all
|
log_error_verbosity=default
|
restart_after_crash=false
|
temp_buffers=100
|
));
|
|
}
|
|
|
=pod
|
|
=item TestAio::have_io_uring()
|
|
Return if io_uring is supported
|
|
=cut
|
|
sub have_io_uring
|
{
|
# To detect if io_uring is supported, we look at the error message for
|
# assigning an invalid value to an enum GUC, which lists all the valid
|
# options. We need to use -C to deal with running as administrator on
|
# windows, the superuser check is omitted if -C is used.
|
my ($stdout, $stderr) =
|
run_command [qw(postgres -C invalid -c io_method=invalid)];
|
die "can't determine supported io_method values"
|
unless $stderr =~ m/Available values: ([^\.]+)\./;
|
my $methods = $1;
|
note "supported io_method values are: $methods";
|
|
# [PG_CUSTOM: CFS]
|
# Build support alone is not sufficient: the kernel or the seccomp policy
|
# of the test environment (e.g. the default docker profile) may deny
|
# io_uring setup at runtime. seccomp cannot be detected reliably from
|
# userspace, so probe the environment with a throwaway initdb run and
|
# skip the io_uring tests when io_uring is not actually available.
|
|
# Keep the vanilla behaviour for builds without io_uring support.
|
return 0 unless ($methods =~ m/io_uring/);
|
|
# Fast path: io_uring unconditionally disabled by the kernel, no need
|
# to run the probe.
|
if (open(my $sysctl, '<', '/proc/sys/kernel/io_uring_disabled'))
|
{
|
my $io_uring_disabled = <$sysctl> // '';
|
close $sysctl;
|
chomp $io_uring_disabled;
|
if ($io_uring_disabled eq '2')
|
{
|
note "io_uring is disabled by the kernel";
|
return 0;
|
}
|
}
|
|
# Mirror Cluster::init(): same initdb options and the same
|
# PG_TEST_INITDB_EXTRA_OPTS inheritance (e.g. the license file), with
|
# io_method appended last so it wins over an io_method in the
|
# environment. The probe data directory is created below tmp_check and
|
# is removed by the test harness. Note that resource exhaustion (e.g.
|
# EMFILE) yields the same error message; the full diagnostic is printed
|
# below. A seccomp policy that allows io_uring_setup but denies
|
# io_uring_enter is detected by the probe as well: the initdb run
|
# performs real I/O through AIO, so the submit or wait failure message
|
# skips this section too.
|
my $probe_pgdata = PostgreSQL::Test::Utils::tempdir('io_uring_probe');
|
my @probe_cmd =
|
('initdb', '--no-sync', '--pgdata' => $probe_pgdata,
|
'--auth' => 'trust');
|
push @probe_cmd, shellwords($ENV{PG_TEST_INITDB_EXTRA_OPTS})
|
if defined $ENV{PG_TEST_INITDB_EXTRA_OPTS};
|
push @probe_cmd, '-c', 'io_method=io_uring';
|
note('Running: ' . join(' ', @probe_cmd));
|
my ($probe_stdout, $probe_stderr) = run_command(\@probe_cmd);
|
my $probe_ret = $?;
|
|
if ($probe_ret != 0)
|
{
|
# Any other initdb failure (e.g. a missing license file) must stay
|
# loud instead of silently skipping the io_uring tests.
|
die "io_uring probe initdb failed: $probe_stderr\n"
|
unless $probe_stderr =~
|
m{could not setup io_uring queue|io_uring (?:submit|wait) failed};
|
note "io_uring is not available at runtime, skipping io_uring tests";
|
note $probe_stderr;
|
return 0;
|
}
|
|
return ($methods =~ m/io_uring/) ? 1 : 0;
|
}
|
|
1;
|