xref: /trafficserver/iocore/aio/I_AIO.h (revision e4398187)
1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /****************************************************************************
25 
26   Async Disk IO operations.
27 
28 
29 
30  ****************************************************************************/
31 #pragma once
32 
33 #include "tscore/ink_platform.h"
34 #include "I_EventSystem.h"
35 #include "records/I_RecProcess.h"
36 
37 static constexpr ts::ModuleVersion AIO_MODULE_PUBLIC_VERSION(1, 0, ts::ModuleVersion::PUBLIC);
38 
39 #define AIO_EVENT_DONE (AIO_EVENT_EVENTS_START + 0)
40 
41 #define AIO_MODE_THREAD 0
42 #define AIO_MODE_NATIVE 1
43 
44 #if TS_USE_LINUX_NATIVE_AIO
45 #define AIO_MODE AIO_MODE_NATIVE
46 #else
47 #define AIO_MODE AIO_MODE_THREAD
48 #endif
49 
50 #define LIO_READ 0x1
51 #define LIO_WRITE 0x2
52 
53 #if AIO_MODE == AIO_MODE_NATIVE
54 
55 #include <libaio.h>
56 
57 #define MAX_AIO_EVENTS 1024
58 
59 typedef struct iocb ink_aiocb;
60 typedef struct io_event ink_io_event_t;
61 
62 // XXX hokey old-school compatibility with ink_aiocb.h ...
63 #define aio_nbytes u.c.nbytes
64 #define aio_offset u.c.offset
65 #define aio_buf u.c.buf
66 
67 #else
68 
69 struct ink_aiocb {
70   int aio_fildes    = 0;
71   void *aio_buf     = nullptr; /* buffer location */
72   size_t aio_nbytes = 0;       /* length of transfer */
73   off_t aio_offset  = 0;       /* file offset */
74 
75   int aio_lio_opcode = 0; /* listio operation */
76   int aio_state      = 0; /* state flag for List I/O */
77   int aio__pad[1];        /* extension padding */
78 };
79 
80 bool ink_aio_thread_num_set(int thread_num);
81 
82 #endif
83 
84 // AIOCallback::thread special values
85 #define AIO_CALLBACK_THREAD_ANY ((EThread *)0) // any regular event thread
86 #define AIO_CALLBACK_THREAD_AIO ((EThread *)-1)
87 
88 struct AIOCallback : public Continuation {
89   // set before calling aio_read/aio_write
90   ink_aiocb aiocb;
91   Action action;
92   EThread *thread   = AIO_CALLBACK_THREAD_ANY;
93   AIOCallback *then = nullptr;
94   // set on return from aio_read/aio_write
95   int64_t aio_result = 0;
96 
97   int ok();
AIOCallbackAIOCallback98   AIOCallback() {}
99 };
100 
101 #if AIO_MODE == AIO_MODE_NATIVE
102 
103 struct AIOVec : public Continuation {
104   Action action;
105   int size;
106   int completed;
107   AIOCallback *first;
108 
AIOVecAIOVec109   AIOVec(int sz, AIOCallback *c) : Continuation(new_ProxyMutex()), size(sz), completed(0), first(c)
110   {
111     action = c->action;
112     SET_HANDLER(&AIOVec::mainEvent);
113   }
114 
115   int mainEvent(int event, Event *e);
116 };
117 
118 struct DiskHandler : public Continuation {
119   Event *trigger_event;
120   io_context_t ctx;
121   ink_io_event_t events[MAX_AIO_EVENTS];
122   Que(AIOCallback, link) ready_list;
123   Que(AIOCallback, link) complete_list;
124   int startAIOEvent(int event, Event *e);
125   int mainAIOEvent(int event, Event *e);
DiskHandlerDiskHandler126   DiskHandler()
127   {
128     SET_HANDLER(&DiskHandler::startAIOEvent);
129     memset(&ctx, 0, sizeof(ctx));
130     int ret = io_setup(MAX_AIO_EVENTS, &ctx);
131     if (ret < 0) {
132       Debug("aio", "io_setup error: %s (%d)", strerror(-ret), -ret);
133     }
134   }
135 };
136 #endif
137 
138 void ink_aio_init(ts::ModuleVersion version);
139 int ink_aio_start();
140 void ink_aio_set_callback(Continuation *error_callback);
141 
142 int ink_aio_read(AIOCallback *op,
143                  int fromAPI = 0); // fromAPI is a boolean to indicate if this is from a API call such as upload proxy feature
144 int ink_aio_write(AIOCallback *op, int fromAPI = 0);
145 int ink_aio_readv(AIOCallback *op,
146                   int fromAPI = 0); // fromAPI is a boolean to indicate if this is from a API call such as upload proxy feature
147 int ink_aio_writev(AIOCallback *op, int fromAPI = 0);
148 AIOCallback *new_AIOCallback();
149