aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/AIO.cpp
blob: 7088be633e5122cbe5ab529343ab73ee0ecf0999 (plain) (blame)
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
#include <IO/AIO.h>

#if defined(OS_LINUX)

#    include <Common/Exception.h>

#    include <sys/syscall.h>
#    include <unistd.h>
#    include <utility>


/** Small wrappers for asynchronous I/O.
  */

namespace DB
{
    namespace ErrorCodes
    {
        extern const int CANNOT_IOSETUP;
    }
}


int io_setup(unsigned nr, aio_context_t * ctxp)
{
    return static_cast<int>(syscall(__NR_io_setup, nr, ctxp));
}

int io_destroy(aio_context_t ctx)
{
    return static_cast<int>(syscall(__NR_io_destroy, ctx));
}

int io_submit(aio_context_t ctx, long nr, struct iocb * iocbpp[]) // NOLINT
{
    return static_cast<int>(syscall(__NR_io_submit, ctx, nr, iocbpp));
}

int io_getevents(aio_context_t ctx, long min_nr, long max_nr, io_event * events, struct timespec * timeout) // NOLINT
{
    return static_cast<int>(syscall(__NR_io_getevents, ctx, min_nr, max_nr, events, timeout));
}


AIOContext::AIOContext(unsigned int nr_events)
{
    ctx = 0;
    if (io_setup(nr_events, &ctx) < 0)
        DB::throwFromErrno("io_setup failed", DB::ErrorCodes::CANNOT_IOSETUP);
}

AIOContext::~AIOContext()
{
    if (ctx)
        io_destroy(ctx);
}

AIOContext::AIOContext(AIOContext && rhs) noexcept
{
    *this = std::move(rhs);
}

AIOContext & AIOContext::operator=(AIOContext && rhs) noexcept
{
    std::swap(ctx, rhs.ctx);
    return *this;
}

#elif defined(OS_FREEBSD)

#    include <Common/Exception.h>


/** Small wrappers for asynchronous I/O.
  */

namespace DB
{
namespace ErrorCodes
{
    extern const int CANNOT_IOSETUP;
}
}


int io_setup(void)
{
    return kqueue();
}

int io_destroy(int ctx)
{
    return close(ctx);
}

int io_submit(int ctx, long nr, struct iocb * iocbpp[])
{
    for (long i = 0; i < nr; ++i)
    {
        struct aiocb * iocb = &iocbpp[i]->aio;

        struct sigevent * se = &iocb->aio_sigevent;
        se->sigev_notify_kqueue = ctx;
        se->sigev_notify_kevent_flags = 0;
        se->sigev_notify = SIGEV_KEVENT;
        se->sigev_value.sival_ptr = iocbpp[i];

        switch (iocb->aio_lio_opcode)
        {
            case LIO_READ:
            {
                int r = aio_read(iocb);
                if (r < 0)
                    return r;
                break;
            }
            case LIO_WRITE:
            {
                int r = aio_write(iocb);
                if (r < 0)
                    return r;
                break;
            }
        }
    }

    return static_cast<int>(nr);
}

int io_getevents(int ctx, long, long max_nr, struct kevent * events, struct timespec * timeout)
{
    return kevent(ctx, nullptr, 0, events, static_cast<int>(max_nr), timeout);
}


AIOContext::AIOContext(unsigned int)
{
    ctx = io_setup();
    if (ctx < 0)
        DB::throwFromErrno("io_setup failed", DB::ErrorCodes::CANNOT_IOSETUP);
}

AIOContext::~AIOContext()
{
    io_destroy(ctx);
}

#endif