Debian Codegraph @

www/asyncvfs.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>An Asynchronous I/O Module For SQLite</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118
119
120 <h1 align="center">An Asynchronous I/O Module For SQLite</h1>
121
122 <hr>
123 <p><font size=+1><b>NOTE:</b>
124 <a href="wal.html">WAL mode</a> with <a href="pragma.html#pragma_synchronous">PRAGMA synchronous</a> set to NORMAL avoids calls to
125 fsync() during transaction commit and only invokes fsync() during
126 a <a href="wal.html#ckpt">checkpoint</a> operation. The use of <a href="wal.html">WAL mode</a> largely obviates the
127 need for this asynchronous I/O module. Hence, this module is no longer
128 supported. The source code continues to exist in the SQLite source tree,
129 but it is not a part of any standard build and is no longer maintained.
130 This documentation is retained for historical reference.</font></p><hr>
131
132 <p>Normally, when SQLite writes to a database file, it waits until the write
133 operation is finished before returning control to the calling application.
134 Since writing to the file-system is usually very slow compared with CPU
135 bound operations, this can be a performance bottleneck. The asynchronous I/O
136 backend is an extension that causes SQLite to perform all write requests
137 using a separate thread running in the background. Although this does not
138 reduce the overall system resources (CPU, disk bandwidth etc.), it does
139 allow SQLite to return control to the caller quickly even when writing to
140 the database.
141
142 <h2>1.0 FUNCTIONALITY</h2>
143
144 <p>With asynchronous I/O, write requests are handled by a separate thread
145 running in the background. This means that the thread that initiates
146 a database write does not have to wait for (sometimes slow) disk I/O
147 to occur. The write seems to happen very quickly, though in reality
148 it is happening at its usual slow pace in the background.
149
150 <p>Asynchronous I/O appears to give better responsiveness, but at a price.
151 You lose the Durable property. With the default I/O backend of SQLite,
152 once a write completes, you know that the information you wrote is
153 safely on disk. With the asynchronous I/O, this is not the case. If
154 your program crashes or if a power loss occurs after the database
155 write but before the asynchronous write thread has completed, then the
156 database change might never make it to disk and the next user of the
157 database might not see your change.
158
159 <p>You lose Durability with asynchronous I/O, but you still retain the
160 other parts of ACID: Atomic, Consistent, and Isolated. Many
161 applications get along fine without the Durability.
162
163 <h3>1.1 How it Works</h3>
164
165 <p>Asynchronous I/O works by creating an SQLite <a href="c3ref/vfs.html">VFS object</a>
166 and registering it with <a href="c3ref/vfs_find.html">sqlite3_vfs_register()</a>.
167 When files opened via
168 this VFS are written to (using the vfs xWrite() method), the data is not
169 written directly to disk, but is placed in the "write-queue" to be
170 handled by the background thread.
171
172 <p>When files opened with the asynchronous VFS are read from
173 (using the vfs xRead() method), the data is read from the file on
174 disk and the write-queue, so that from the point of view of
175 the vfs reader the xWrite() appears to have already completed.
176
177 <p>The asynchronous I/O VFS is registered (and unregistered) by calls to the
178 API functions sqlite3async_initialize() and sqlite3async_shutdown().
179 See section "Compilation and Usage" below for details.
180
181 <h3>1.2 Limitations</h3>
182
183 <p>In order to gain experience with the main ideas surrounding asynchronous
184 IO, this implementation is deliberately kept simple. Additional
185 capabilities may be added in the future.
186
187 <p>For example, as currently implemented, if writes are happening at a
188 steady stream that exceeds the I/O capability of the background writer
189 thread, the queue of pending write operations will grow without bound.
190 If this goes on for long enough, the host system could run out of memory.
191 A more sophisticated module could to keep track of the quantity of
192 pending writes and stop accepting new write requests when the queue of
193 pending writes grows too large.
194
195 <h3>1.3 Locking and Concurrency</h3>
196
197 <p>Multiple connections from within a single process that use this
198 implementation of asynchronous IO may access a single database
199 file concurrently. From the point of view of the user, if all
200 connections are from within a single process, there is no difference
201 between the concurrency offered by "normal" SQLite and SQLite
202 using the asynchronous backend.
203
204 <p>If file-locking is enabled (it is enabled by default), then connections
205 from multiple processes may also read and write the database file.
206 However concurrency is reduced as follows:
207
208 <ul>
209 <li><p> When a connection using asynchronous IO begins a database
210 transaction, the database is locked immediately. However the
211 lock is not released until after all relevant operations
212 in the write-queue have been flushed to disk. This means
213 (for example) that the database may remain locked for some
214 time after a "<a href="lang_transaction.html">COMMIT</a>" or "<a href="lang_transaction.html">ROLLBACK</a>" is issued.
215
216 <li><p> If an application using asynchronous IO executes transactions
217 in quick succession, other database users may be effectively
218 locked out of the database. This is because when a <a href="lang_transaction.html">BEGIN</a>
219 is executed, a database lock is established immediately. But
220 when the corresponding COMMIT or ROLLBACK occurs, the lock
221 is not released until the relevant part of the write-queue
222 has been flushed through. As a result, if a COMMIT is followed
223 by a BEGIN before the write-queue is flushed through, the database
224 is never unlocked,preventing other processes from accessing
225 the database.
226 </ul>
227
228 <p>File-locking may be disabled at runtime using the sqlite3async_control()
229 API (see below). This may improve performance when an NFS or other
230 network file-system, as the synchronous round-trips to the server be
231 required to establish file locks are avoided. However, if multiple
232 connections attempt to access the same database file when file-locking
233 is disabled, application crashes and database corruption is a likely
234 outcome.
235
236
237 <h2>2.0 COMPILATION AND USAGE</h2>
238
239 <p>
240 The asynchronous IO extension consists of a single file of C code
241 (sqlite3async.c), and a header file (sqlite3async.h), located in the
242 <a href="https://www.sqlite.org/src/dir?name=ext/async">
243 <tt>ext/async/</tt> subfolder</a> of the SQLite source tree, that defines the
244 C API used by applications to activate and control the modules
245 functionality.
246
247 <p>
248 To use the asynchronous IO extension, compile sqlite3async.c as
249 part of the application that uses SQLite. Then use the APIs defined
250 in sqlite3async.h to initialize and configure the module.
251
252 <p>
253 The asynchronous IO VFS API is described in detail in comments in
254 sqlite3async.h. Using the API usually consists of the following steps:
255
256 <ol>
257 <li><p>Register the asynchronous IO VFS with SQLite by calling the
258 sqlite3async_initialize() function.
259
260 <li><p>Create a background thread to perform write operations and call
261 sqlite3async_run().
262
263 <li><p>Use the normal SQLite API to read and write to databases via
264 the asynchronous IO VFS.
265 </ol>
266
267 <p>Refer to comments in the
268 <a href="https://www.sqlite.org/src/finfo?name=ext/async/sqlite3async.h">
269 sqlite3async.h header file</a> for details.
270
271
272 <h2>3.0 PORTING</h2>
273
274 <p>Currently the asynchronous IO extension is compatible with win32 systems
275 and systems that support the pthreads interface, including Mac OS X, Linux,
276 and other varieties of Unix.
277
278 <p>To port the asynchronous IO extension to another platform, the user must
279 implement mutex and condition variable primitives for the new platform.
280 Currently there is no externally available interface to allow this, but
281 modifying the code within sqlite3async.c to include the new platforms
282 concurrency primitives is relatively easy. Search within sqlite3async.c
283 for the comment string "PORTING FUNCTIONS" for details. Then implement
284 new versions of each of the following:
285
286 <blockquote><pre>
287 static void async_mutex_enter(int eMutex);
288 static void async_mutex_leave(int eMutex);
289 static void async_cond_wait(int eCond, int eMutex);
290 static void async_cond_signal(int eCond);
291 static void async_sched_yield(void);
292 </pre></blockquote>
293
294 <p>The functionality required of each of the above functions is described
295 in comments in sqlite3async.c.
296
297