Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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;