~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/innobase/os/os0file.c

  • Committer: Brian Aker
  • Date: 2009-05-23 17:13:03 UTC
  • mfrom: (1034.1.8 merge)
  • Revision ID: brian@gaz-20090523171303-d28xhutqic0xe2b4
Merge Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
2
 
3
 
Copyright (c) 1995, 2010, Innobase Oy. All Rights Reserved.
4
 
Copyright (c) 2009, Percona Inc.
5
 
 
6
 
Portions of this file contain modifications contributed and copyrighted
7
 
by Percona Inc.. Those modifications are
8
 
gratefully acknowledged and are described briefly in the InnoDB
9
 
documentation. The contributions by Percona Inc. are incorporated with
10
 
their permission, and subject to the conditions contained in the file
11
 
COPYING.Percona.
 
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
12
4
 
13
5
This program is free software; you can redistribute it and/or modify it under
14
6
the terms of the GNU General Public License as published by the Free Software
19
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20
12
 
21
13
You should have received a copy of the GNU General Public License along with
22
 
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
23
 
St, Fifth Floor, Boston, MA 02110-1301 USA
 
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
15
Place, Suite 330, Boston, MA 02111-1307 USA
24
16
 
25
17
*****************************************************************************/
26
18
 
27
 
/**************************************************//**
28
 
@file os/os0file.c
 
19
/******************************************************
29
20
The interface to the operating system file i/o primitives
30
21
 
31
22
Created 10/21/1995 Heikki Tuuri
32
23
*******************************************************/
33
24
 
34
25
#include "os0file.h"
35
 
 
36
 
#ifdef UNIV_NONINL
37
 
#include "os0file.ic"
38
 
#endif
39
 
 
 
26
#include "os0sync.h"
 
27
#include "os0thread.h"
40
28
#include "ut0mem.h"
41
29
#include "srv0srv.h"
42
30
#include "srv0start.h"
43
31
#include "fil0fil.h"
44
32
#include "buf0buf.h"
 
33
 
 
34
#if defined(UNIV_HOTBACKUP) && defined(__WIN__)
 
35
/* Add includes for the _stat() call to compile on Windows */
 
36
#include <sys/types.h>
 
37
#include <sys/stat.h>
45
38
#include <errno.h>
46
 
#include <fcntl.h>
47
 
#include <limits.h>
48
 
#include <unistd.h>
49
 
#ifndef UNIV_HOTBACKUP
50
 
# include "os0sync.h"
51
 
# include "os0thread.h"
52
 
#else /* !UNIV_HOTBACKUP */
53
 
# ifdef __WIN__
54
 
/* Add includes for the _stat() call to compile on Windows */
55
 
#  include <sys/types.h>
56
 
#  include <sys/stat.h>
57
 
# endif /* __WIN__ */
58
 
#endif /* !UNIV_HOTBACKUP */
59
 
 
60
 
#if defined(LINUX_NATIVE_AIO)
61
 
#include <libaio.h>
62
 
#endif
 
39
#endif /* UNIV_HOTBACKUP */
63
40
 
64
41
/* This specifies the file permissions InnoDB uses when it creates files in
65
42
Unix; the value of os_innodb_umask is initialized in ha_innodb.cc to
66
43
my_umask */
67
44
 
68
45
#ifndef __WIN__
69
 
/** Umask for creating files */
70
46
UNIV_INTERN ulint       os_innodb_umask
71
47
                        = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
72
48
#else
73
 
/** Umask for creating files */
74
49
UNIV_INTERN ulint       os_innodb_umask         = 0;
75
50
#endif
76
51
 
82
57
/* We do not call os_file_flush in every os_file_write. */
83
58
#endif /* UNIV_DO_FLUSH */
84
59
 
85
 
#ifndef UNIV_HOTBACKUP
86
60
/* We use these mutexes to protect lseek + file i/o operation, if the
87
61
OS does not provide an atomic pread or pwrite, or similar */
88
62
#define OS_FILE_N_SEEK_MUTEXES  16
91
65
/* In simulated aio, merge at most this many consecutive i/os */
92
66
#define OS_AIO_MERGE_N_CONSECUTIVE      64
93
67
 
94
 
/**********************************************************************
95
 
 
96
 
InnoDB AIO Implementation:
97
 
=========================
98
 
 
99
 
We support native AIO for windows and linux. For rest of the platforms
100
 
we simulate AIO by special io-threads servicing the IO-requests.
101
 
 
102
 
Simulated AIO:
103
 
==============
104
 
 
105
 
In platforms where we 'simulate' AIO following is a rough explanation
106
 
of the high level design.
107
 
There are four io-threads (for ibuf, log, read, write).
108
 
All synchronous IO requests are serviced by the calling thread using
109
 
os_file_write/os_file_read. The Asynchronous requests are queued up
110
 
in an array (there are four such arrays) by the calling thread. 
111
 
Later these requests are picked up by the io-thread and are serviced
112
 
synchronously.
113
 
 
114
 
Windows native AIO:
115
 
==================
116
 
 
117
 
If srv_use_native_aio is not set then windows follow the same
118
 
code as simulated AIO. If the flag is set then native AIO interface
119
 
is used. On windows, one of the limitation is that if a file is opened
120
 
for AIO no synchronous IO can be done on it. Therefore we have an
121
 
extra fifth array to queue up synchronous IO requests.
122
 
There are innodb_file_io_threads helper threads. These threads work
123
 
on the four arrays mentioned above in Simulated AIO. No thread is
124
 
required for the sync array.
125
 
If a synchronous IO request is made, it is first queued in the sync
126
 
array. Then the calling thread itself waits on the request, thus
127
 
making the call synchronous.
128
 
If an AIO request is made the calling thread not only queues it in the
129
 
array but also submits the requests. The helper thread then collects
130
 
the completed IO request and calls completion routine on it.
131
 
 
132
 
Linux native AIO:
133
 
=================
134
 
 
135
 
If we have libaio installed on the system and innodb_use_native_aio
136
 
is set to TRUE we follow the code path of native AIO, otherwise we
137
 
do simulated AIO.
138
 
There are innodb_file_io_threads helper threads. These threads work
139
 
on the four arrays mentioned above in Simulated AIO.
140
 
If a synchronous IO request is made, it is handled by calling
141
 
os_file_write/os_file_read.
142
 
If an AIO request is made the calling thread not only queues it in the
143
 
array but also submits the requests. The helper thread then collects
144
 
the completed IO request and calls completion routine on it.
145
 
 
146
 
**********************************************************************/
147
 
 
148
 
/** Flag: enable debug printout for asynchronous i/o */
 
68
/* If this flag is TRUE, then we will use the native aio of the
 
69
OS (provided we compiled Innobase with it in), otherwise we will
 
70
use simulated aio we build below with threads */
 
71
 
 
72
UNIV_INTERN ibool       os_aio_use_native_aio   = FALSE;
 
73
 
149
74
UNIV_INTERN ibool       os_aio_print_debug      = FALSE;
150
75
 
151
 
#ifdef UNIV_PFS_IO
152
 
/* Keys to register InnoDB I/O with performance schema */
153
 
UNIV_INTERN mysql_pfs_key_t  innodb_file_data_key;
154
 
UNIV_INTERN mysql_pfs_key_t  innodb_file_log_key;
155
 
UNIV_INTERN mysql_pfs_key_t  innodb_file_temp_key;
156
 
#endif /* UNIV_PFS_IO */
157
 
 
158
 
/** The asynchronous i/o array slot structure */
 
76
/* The aio array slot structure */
159
77
typedef struct os_aio_slot_struct       os_aio_slot_t;
160
78
 
161
 
/** The asynchronous i/o array slot structure */
162
79
struct os_aio_slot_struct{
163
 
        ibool           is_read;        /*!< TRUE if a read operation */
164
 
        ulint           pos;            /*!< index of the slot in the aio
 
80
        ibool           is_read;        /* TRUE if a read operation */
 
81
        ulint           pos;            /* index of the slot in the aio
165
82
                                        array */
166
 
        ibool           reserved;       /*!< TRUE if this slot is reserved */
167
 
        time_t          reservation_time;/*!< time when reserved */
168
 
        ulint           len;            /*!< length of the block to read or
 
83
        ibool           reserved;       /* TRUE if this slot is reserved */
 
84
        time_t          reservation_time;/* time when reserved */
 
85
        ulint           len;            /* length of the block to read or
169
86
                                        write */
170
 
        byte*           buf;            /*!< buffer used in i/o */
171
 
        ulint           type;           /*!< OS_FILE_READ or OS_FILE_WRITE */
172
 
        ulint           offset;         /*!< 32 low bits of file offset in
 
87
        byte*           buf;            /* buffer used in i/o */
 
88
        ulint           type;           /* OS_FILE_READ or OS_FILE_WRITE */
 
89
        ulint           offset;         /* 32 low bits of file offset in
173
90
                                        bytes */
174
 
        ulint           offset_high;    /*!< 32 high bits of file offset */
175
 
        os_file_t       file;           /*!< file where to read or write */
176
 
        const char*     name;           /*!< file name or path */
177
 
        ibool           io_already_done;/*!< used only in simulated aio:
 
91
        ulint           offset_high;    /* 32 high bits of file offset */
 
92
        os_file_t       file;           /* file where to read or write */
 
93
        const char*     name;           /* file name or path */
 
94
        ibool           io_already_done;/* used only in simulated aio:
178
95
                                        TRUE if the physical i/o already
179
96
                                        made and only the slot message
180
97
                                        needs to be passed to the caller
181
98
                                        of os_aio_simulated_handle */
182
 
        fil_node_t*     message1;       /*!< message which is given by the */
183
 
        void*           message2;       /*!< the requester of an aio operation
 
99
        fil_node_t*     message1;       /* message which is given by the */
 
100
        void*           message2;       /* the requester of an aio operation
184
101
                                        and which can be used to identify
185
102
                                        which pending aio operation was
186
103
                                        completed */
187
104
#ifdef WIN_ASYNC_IO
188
 
        HANDLE          handle;         /*!< handle object we need in the
 
105
        os_event_t      event;          /* event object we need in the
189
106
                                        OVERLAPPED struct */
190
 
        OVERLAPPED      control;        /*!< Windows control block for the
 
107
        OVERLAPPED      control;        /* Windows control block for the
191
108
                                        aio request */
192
 
#elif defined(LINUX_NATIVE_AIO)
193
 
        struct iocb     control;        /* Linux control block for aio */
194
 
        int             n_bytes;        /* bytes written/read. */
195
 
        int             ret;            /* AIO return code */
196
109
#endif
197
110
};
198
111
 
199
 
/** The asynchronous i/o array structure */
 
112
/* The aio array structure */
200
113
typedef struct os_aio_array_struct      os_aio_array_t;
201
114
 
202
 
/** The asynchronous i/o array structure */
203
115
struct os_aio_array_struct{
204
 
        os_mutex_t      mutex;  /*!< the mutex protecting the aio array */
205
 
        os_event_t      not_full;
206
 
                                /*!< The event which is set to the
207
 
                                signaled state when there is space in
208
 
                                the aio outside the ibuf segment */
209
 
        os_event_t      is_empty;
210
 
                                /*!< The event which is set to the
211
 
                                signaled state when there are no
212
 
                                pending i/os in this array */
213
 
        ulint           n_slots;/*!< Total number of slots in the aio
214
 
                                array.  This must be divisible by
215
 
                                n_threads. */
216
 
        ulint           n_segments;
217
 
                                /*!< Number of segments in the aio
218
 
                                array of pending aio requests. A
219
 
                                thread can wait separately for any one
220
 
                                of the segments. */
221
 
        ulint           cur_seg;/*!< We reserve IO requests in round
222
 
                                robin fashion to different segments.
223
 
                                This points to the segment that is to
224
 
                                be used to service next IO request. */
225
 
        ulint           n_reserved;
226
 
                                /*!< Number of reserved slots in the
227
 
                                aio array outside the ibuf segment */
228
 
        os_aio_slot_t*  slots;  /*!< Pointer to the slots in the array */
 
116
        os_mutex_t      mutex;    /* the mutex protecting the aio array */
 
117
        os_event_t      not_full; /* The event which is set to the signaled
 
118
                                  state when there is space in the aio
 
119
                                  outside the ibuf segment */
 
120
        os_event_t      is_empty; /* The event which is set to the signaled
 
121
                                  state when there are no pending i/os
 
122
                                  in this array */
 
123
        ulint           n_slots;  /* Total number of slots in the aio array.
 
124
                                  This must be divisible by n_threads. */
 
125
        ulint           n_segments;/* Number of segments in the aio array of
 
126
                                  pending aio requests. A thread can wait
 
127
                                  separately for any one of the segments. */
 
128
        ulint           n_reserved;/* Number of reserved slots in the
 
129
                                  aio array outside the ibuf segment */
 
130
        os_aio_slot_t*  slots;    /* Pointer to the slots in the array */
229
131
#ifdef __WIN__
230
 
        HANDLE*         handles;
231
 
                                /*!< Pointer to an array of OS native
232
 
                                event handles where we copied the
233
 
                                handles from slots, in the same
234
 
                                order. This can be used in
235
 
                                WaitForMultipleObjects; used only in
236
 
                                Windows */
237
 
#endif
238
 
 
239
 
#if defined(LINUX_NATIVE_AIO)
240
 
        io_context_t*           aio_ctx;
241
 
                                /* completion queue for IO. There is 
242
 
                                one such queue per segment. Each thread
243
 
                                will work on one ctx exclusively. */
244
 
        struct io_event*        aio_events;
245
 
                                /* The array to collect completed IOs.
246
 
                                There is one such event for each
247
 
                                possible pending IO. The size of the
248
 
                                array is equal to n_slots. */
 
132
        os_native_event_t* native_events;
 
133
                                  /* Pointer to an array of OS native event
 
134
                                  handles where we copied the handles from
 
135
                                  slots, in the same order. This can be used
 
136
                                  in WaitForMultipleObjects; used only in
 
137
                                  Windows */
249
138
#endif
250
139
};
251
140
 
252
 
#if defined(LINUX_NATIVE_AIO)
253
 
/** timeout for each io_getevents() call = 500ms. */
254
 
#define OS_AIO_REAP_TIMEOUT     (500000000UL)
255
 
 
256
 
/** time to sleep, in microseconds if io_setup() returns EAGAIN. */
257
 
#define OS_AIO_IO_SETUP_RETRY_SLEEP     (500000UL)
258
 
 
259
 
/** number of attempts before giving up on io_setup(). */
260
 
#define OS_AIO_IO_SETUP_RETRY_ATTEMPTS  5
261
 
#endif
262
 
 
263
 
/** Array of events used in simulated aio */
 
141
/* Array of events used in simulated aio */
264
142
static os_event_t*      os_aio_segment_wait_events      = NULL;
265
143
 
266
 
/** The aio arrays for non-ibuf i/o and ibuf i/o, as well as sync aio. These
267
 
are NULL when the module has not yet been initialized. @{ */
268
 
static os_aio_array_t*  os_aio_read_array       = NULL; /*!< Reads */
269
 
static os_aio_array_t*  os_aio_write_array      = NULL; /*!< Writes */
270
 
static os_aio_array_t*  os_aio_ibuf_array       = NULL; /*!< Insert buffer */
271
 
static os_aio_array_t*  os_aio_log_array        = NULL; /*!< Redo log */
272
 
static os_aio_array_t*  os_aio_sync_array       = NULL; /*!< Synchronous I/O */
273
 
/* @} */
 
144
/* The aio arrays for non-ibuf i/o and ibuf i/o, as well as sync aio. These
 
145
are NULL when the module has not yet been initialized. */
 
146
static os_aio_array_t*  os_aio_read_array       = NULL;
 
147
static os_aio_array_t*  os_aio_write_array      = NULL;
 
148
static os_aio_array_t*  os_aio_ibuf_array       = NULL;
 
149
static os_aio_array_t*  os_aio_log_array        = NULL;
 
150
static os_aio_array_t*  os_aio_sync_array       = NULL;
274
151
 
275
 
/** Number of asynchronous I/O segments.  Set by os_aio_init(). */
276
152
static ulint    os_aio_n_segments       = ULINT_UNDEFINED;
277
153
 
278
 
/** If the following is TRUE, read i/o handler threads try to
 
154
/* If the following is TRUE, read i/o handler threads try to
279
155
wait until a batch of new read requests have been posted */
280
156
static ibool    os_aio_recommend_sleep_for_read_threads = FALSE;
281
 
#endif /* !UNIV_HOTBACKUP */
282
157
 
283
158
UNIV_INTERN ulint       os_n_file_reads         = 0;
284
159
UNIV_INTERN ulint       os_bytes_read_since_printout = 0;
291
166
 
292
167
UNIV_INTERN ibool       os_has_said_disk_full   = FALSE;
293
168
 
294
 
#ifndef UNIV_HOTBACKUP
295
 
/** The mutex protecting the following counts of pending I/O operations */
 
169
/* The mutex protecting the following counts of pending I/O operations */
296
170
static os_mutex_t       os_file_count_mutex;
297
 
#endif /* !UNIV_HOTBACKUP */
298
 
/** Number of pending os_file_pread() operations */
299
171
UNIV_INTERN ulint       os_file_n_pending_preads  = 0;
300
 
/** Number of pending os_file_pwrite() operations */
301
172
UNIV_INTERN ulint       os_file_n_pending_pwrites = 0;
302
 
/** Number of pending write operations */
303
173
UNIV_INTERN ulint       os_n_pending_writes = 0;
304
 
/** Number of pending read operations */
305
174
UNIV_INTERN ulint       os_n_pending_reads = 0;
306
175
 
307
 
/***********************************************************************//**
308
 
Gets the operating system version. Currently works only on Windows.
309
 
@return OS_WIN95, OS_WIN31, OS_WINNT, OS_WIN2000, OS_WINXP, OS_WINVISTA,
310
 
OS_WIN7. */
 
176
/***************************************************************************
 
177
Gets the operating system version. Currently works only on Windows. */
311
178
UNIV_INTERN
312
179
ulint
313
180
os_get_os_version(void)
314
181
/*===================*/
 
182
                  /* out: OS_WIN95, OS_WIN31, OS_WINNT, OS_WIN2000 */
315
183
{
316
184
#ifdef __WIN__
317
185
        OSVERSIONINFO     os_info;
325
193
        } else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
326
194
                return(OS_WIN95);
327
195
        } else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_NT) {
328
 
                switch (os_info.dwMajorVersion) {
329
 
                case 3:
330
 
                case 4:
331
 
                        return OS_WINNT;
332
 
                case 5:
333
 
                        return (os_info.dwMinorVersion == 0) ? OS_WIN2000
334
 
                                                             : OS_WINXP;
335
 
                case 6:
336
 
                        return (os_info.dwMinorVersion == 0) ? OS_WINVISTA
337
 
                                                             : OS_WIN7;
338
 
                default:
339
 
                        return OS_WIN7;
 
196
                if (os_info.dwMajorVersion <= 4) {
 
197
                        return(OS_WINNT);
 
198
                } else {
 
199
                        return(OS_WIN2000);
340
200
                }
341
201
        } else {
342
202
                ut_error;
349
209
#endif
350
210
}
351
211
 
352
 
/***********************************************************************//**
 
212
/***************************************************************************
353
213
Retrieves the last error number if an error occurs in a file io function.
354
214
The number should be retrieved before any other OS calls (because they may
355
215
overwrite the error number). If the number is not known to this program,
356
 
the OS error number + 100 is returned.
357
 
@return error number, or OS error number + 100 */
 
216
the OS error number + 100 is returned. */
358
217
UNIV_INTERN
359
218
ulint
360
219
os_file_get_last_error(
361
220
/*===================*/
362
 
        ibool   report_all_errors)      /*!< in: TRUE if we want an error message
 
221
                                        /* out: error number, or OS error
 
222
                                        number + 100 */
 
223
        ibool   report_all_errors)      /* in: TRUE if we want an error message
363
224
                                        printed of all errors */
364
225
{
365
226
        ulint   err;
404
265
                                " software or another instance\n"
405
266
                                "InnoDB: of MySQL."
406
267
                                " Please close it to get rid of this error.\n");
407
 
                } else if (err == ERROR_WORKING_SET_QUOTA
408
 
                           || err == ERROR_NO_SYSTEM_RESOURCES) {
409
 
                        fprintf(stderr,
410
 
                                "InnoDB: The error means that there are no"
411
 
                                " sufficient system resources or quota to"
412
 
                                " complete the operation.\n");
413
 
                } else if (err == ERROR_OPERATION_ABORTED) {
414
 
                        fprintf(stderr,
415
 
                                "InnoDB: The error means that the I/O"
416
 
                                " operation has been aborted\n"
417
 
                                "InnoDB: because of either a thread exit"
418
 
                                " or an application request.\n"
419
 
                                "InnoDB: Retry attempt is made.\n");
420
268
                } else {
421
269
                        fprintf(stderr,
422
270
                                "InnoDB: Some operating system error numbers"
423
271
                                " are described at\n"
424
272
                                "InnoDB: "
425
 
                                REFMAN
 
273
                                "http://dev.mysql.com/doc/refman/5.1/en/"
426
274
                                "operating-system-error-codes.html\n");
427
275
                }
428
276
        }
438
286
        } else if (err == ERROR_SHARING_VIOLATION
439
287
                   || err == ERROR_LOCK_VIOLATION) {
440
288
                return(OS_FILE_SHARING_VIOLATION);
441
 
        } else if (err == ERROR_WORKING_SET_QUOTA
442
 
                   || err == ERROR_NO_SYSTEM_RESOURCES) {
443
 
                return(OS_FILE_INSUFFICIENT_RESOURCE);
444
 
        } else if (err == ERROR_OPERATION_ABORTED) {
445
 
                return(OS_FILE_OPERATION_ABORTED);
446
289
        } else {
447
290
                return(100 + err);
448
291
        }
486
329
                                "InnoDB: Some operating system"
487
330
                                " error numbers are described at\n"
488
331
                                "InnoDB: "
489
 
                                REFMAN
 
332
                                "http://dev.mysql.com/doc/refman/5.1/en/"
490
333
                                "operating-system-error-codes.html\n");
491
334
                }
492
335
        }
493
336
 
494
337
        fflush(stderr);
495
338
 
496
 
        switch (err) {
497
 
        case ENOSPC:
 
339
        if (err == ENOSPC) {
498
340
                return(OS_FILE_DISK_FULL);
499
 
        case ENOENT:
 
341
        } else if (err == ENOENT) {
500
342
                return(OS_FILE_NOT_FOUND);
501
 
        case EEXIST:
 
343
        } else if (err == EEXIST) {
502
344
                return(OS_FILE_ALREADY_EXISTS);
503
 
        case EXDEV:
504
 
        case ENOTDIR:
505
 
        case EISDIR:
 
345
        } else if (err == EXDEV || err == ENOTDIR || err == EISDIR) {
506
346
                return(OS_FILE_PATH_ERROR);
507
 
        case EAGAIN:
508
 
                if (srv_use_native_aio) {
509
 
                        return(OS_FILE_AIO_RESOURCES_RESERVED);
510
 
                }
511
 
                break;
512
 
        case EINTR:
513
 
                if (srv_use_native_aio) {
514
 
                        return(OS_FILE_AIO_INTERRUPTED);
515
 
                }
516
 
                break;
 
347
        } else {
 
348
                return(100 + err);
517
349
        }
518
 
        return(100 + err);
519
350
#endif
520
351
}
521
352
 
522
 
/****************************************************************//**
 
353
/********************************************************************
523
354
Does error handling when a file operation fails.
524
355
Conditionally exits (calling exit(3)) based on should_exit value and the
525
 
error type
526
 
@return TRUE if we should retry the operation */
 
356
error type */
527
357
static
528
358
ibool
529
359
os_file_handle_error_cond_exit(
530
360
/*===========================*/
531
 
        const char*     name,           /*!< in: name of a file or NULL */
532
 
        const char*     operation,      /*!< in: operation */
533
 
        ibool           should_exit)    /*!< in: call exit(3) if unknown error
 
361
                                        /* out: TRUE if we should retry the
 
362
                                        operation */
 
363
        const char*     name,           /* in: name of a file or NULL */
 
364
        const char*     operation,      /* in: operation */
 
365
        ibool           should_exit)    /* in: call exit(3) if unknown error
534
366
                                        and this parameter is TRUE */
535
367
{
536
368
        ulint   err;
565
397
        } else if (err == OS_FILE_AIO_RESOURCES_RESERVED) {
566
398
 
567
399
                return(TRUE);
568
 
        } else if (err == OS_FILE_AIO_INTERRUPTED) {
569
 
 
570
 
                return(TRUE);
571
400
        } else if (err == OS_FILE_ALREADY_EXISTS
572
401
                   || err == OS_FILE_PATH_ERROR) {
573
402
 
576
405
 
577
406
                os_thread_sleep(10000000);  /* 10 sec */
578
407
                return(TRUE);
579
 
        } else if (err == OS_FILE_INSUFFICIENT_RESOURCE) {
580
 
 
581
 
                os_thread_sleep(100000);        /* 100 ms */
582
 
                return(TRUE);
583
 
        } else if (err == OS_FILE_OPERATION_ABORTED) {
584
 
 
585
 
                os_thread_sleep(100000);        /* 100 ms */
586
 
                return(TRUE);
587
408
        } else {
588
409
                if (name) {
589
410
                        fprintf(stderr, "InnoDB: File name %s\n", name);
604
425
        return(FALSE);
605
426
}
606
427
 
607
 
/****************************************************************//**
608
 
Does error handling when a file operation fails.
609
 
@return TRUE if we should retry the operation */
 
428
/********************************************************************
 
429
Does error handling when a file operation fails. */
610
430
static
611
431
ibool
612
432
os_file_handle_error(
613
433
/*=================*/
614
 
        const char*     name,   /*!< in: name of a file or NULL */
615
 
        const char*     operation)/*!< in: operation */
 
434
                                /* out: TRUE if we should retry the
 
435
                                operation */
 
436
        const char*     name,   /* in: name of a file or NULL */
 
437
        const char*     operation)/* in: operation */
616
438
{
617
439
        /* exit in case of unknown error */
618
440
        return(os_file_handle_error_cond_exit(name, operation, TRUE));
619
441
}
620
442
 
621
 
/****************************************************************//**
622
 
Does error handling when a file operation fails.
623
 
@return TRUE if we should retry the operation */
 
443
/********************************************************************
 
444
Does error handling when a file operation fails. */
624
445
static
625
446
ibool
626
447
os_file_handle_error_no_exit(
627
448
/*=========================*/
628
 
        const char*     name,   /*!< in: name of a file or NULL */
629
 
        const char*     operation)/*!< in: operation */
 
449
                                /* out: TRUE if we should retry the
 
450
                                operation */
 
451
        const char*     name,   /* in: name of a file or NULL */
 
452
        const char*     operation)/* in: operation */
630
453
{
631
454
        /* don't exit in case of unknown error */
632
455
        return(os_file_handle_error_cond_exit(name, operation, FALSE));
634
457
 
635
458
#undef USE_FILE_LOCK
636
459
#define USE_FILE_LOCK
637
 
#if defined(UNIV_HOTBACKUP) || defined(__WIN__)
 
460
#if defined(UNIV_HOTBACKUP) || defined(__WIN__) || defined(__NETWARE__)
638
461
/* InnoDB Hot Backup does not lock the data files.
639
462
 * On Windows, mandatory locking is used.
640
463
 */
641
464
# undef USE_FILE_LOCK
642
465
#endif
643
466
#ifdef USE_FILE_LOCK
644
 
/****************************************************************//**
645
 
Obtain an exclusive lock on a file.
646
 
@return 0 on success */
 
467
/********************************************************************
 
468
Obtain an exclusive lock on a file. */
647
469
static
648
470
int
649
471
os_file_lock(
650
472
/*=========*/
651
 
        int             fd,     /*!< in: file descriptor */
652
 
        const char*     name)   /*!< in: file name */
 
473
                                /* out: 0 on success */
 
474
        int             fd,     /* in: file descriptor */
 
475
        const char*     name)   /* in: file name */
653
476
{
654
477
        struct flock lk;
655
478
        lk.l_type = F_WRLCK;
674
497
}
675
498
#endif /* USE_FILE_LOCK */
676
499
 
677
 
#ifndef UNIV_HOTBACKUP
678
 
/****************************************************************//**
 
500
/********************************************************************
679
501
Creates the seek mutexes used in positioned reads and writes. */
680
502
UNIV_INTERN
681
503
void
684
506
{
685
507
        ulint   i;
686
508
 
687
 
        os_file_count_mutex = os_mutex_create();
 
509
        os_file_count_mutex = os_mutex_create(NULL);
688
510
 
689
511
        for (i = 0; i < OS_FILE_N_SEEK_MUTEXES; i++) {
690
 
                os_file_seek_mutexes[i] = os_mutex_create();
 
512
                os_file_seek_mutexes[i] = os_mutex_create(NULL);
691
513
        }
692
514
}
693
515
 
694
 
/***********************************************************************//**
 
516
/***************************************************************************
695
517
Creates a temporary file.  This function is like tmpfile(3), but
696
518
the temporary file is created in the MySQL temporary directory.
697
 
@return temporary file handle, or NULL on error */
 
519
On Netware, this function is like tmpfile(3), because the C run-time
 
520
library of Netware does not expose the delete-on-close flag. */
698
521
UNIV_INTERN
699
522
FILE*
700
523
os_file_create_tmpfile(void)
701
524
/*========================*/
 
525
                        /* out: temporary file handle, or NULL on error */
702
526
{
 
527
#ifdef UNIV_HOTBACKUP
 
528
        ut_error;
 
529
 
 
530
        return(NULL);
 
531
#else
 
532
# ifdef __NETWARE__
 
533
        FILE*   file    = tmpfile();
 
534
# else /* __NETWARE__ */
703
535
        FILE*   file    = NULL;
704
536
        int     fd      = innobase_mysql_tmpfile();
705
537
 
706
538
        if (fd >= 0) {
707
539
                file = fdopen(fd, "w+b");
708
540
        }
 
541
# endif /* __NETWARE__ */
709
542
 
710
543
        if (!file) {
711
544
                ut_print_timestamp(stderr);
712
545
                fprintf(stderr,
713
546
                        "  InnoDB: Error: unable to create temporary file;"
714
547
                        " errno: %d\n", errno);
 
548
# ifndef __NETWARE__
715
549
                if (fd >= 0) {
716
550
                        close(fd);
717
551
                }
 
552
# endif /* !__NETWARE__ */
718
553
        }
719
554
 
720
555
        return(file);
 
556
#endif /* UNIV_HOTBACKUP */
721
557
}
722
 
#endif /* !UNIV_HOTBACKUP */
723
558
 
724
 
/***********************************************************************//**
 
559
/***************************************************************************
725
560
The os_file_opendir() function opens a directory stream corresponding to the
726
561
directory named by the dirname argument. The directory stream is positioned
727
562
at the first entry. In both Unix and Windows we automatically skip the '.'
728
 
and '..' items at the start of the directory listing.
729
 
@return directory stream, NULL if error */
 
563
and '..' items at the start of the directory listing. */
730
564
UNIV_INTERN
731
565
os_file_dir_t
732
566
os_file_opendir(
733
567
/*============*/
734
 
        const char*     dirname,        /*!< in: directory name; it must not
 
568
                                        /* out: directory stream, NULL if
 
569
                                        error */
 
570
        const char*     dirname,        /* in: directory name; it must not
735
571
                                        contain a trailing '\' or '/' */
736
 
        ibool           error_is_fatal) /*!< in: TRUE if we should treat an
 
572
        ibool           error_is_fatal) /* in: TRUE if we should treat an
737
573
                                        error as a fatal error; if we try to
738
574
                                        open symlinks then we do not wish a
739
575
                                        fatal error if it happens not to be
780
616
#endif
781
617
}
782
618
 
783
 
/***********************************************************************//**
784
 
Closes a directory stream.
785
 
@return 0 if success, -1 if failure */
 
619
/***************************************************************************
 
620
Closes a directory stream. */
786
621
UNIV_INTERN
787
622
int
788
623
os_file_closedir(
789
624
/*=============*/
790
 
        os_file_dir_t   dir)    /*!< in: directory stream */
 
625
                                /* out: 0 if success, -1 if failure */
 
626
        os_file_dir_t   dir)    /* in: directory stream */
791
627
{
792
628
#ifdef __WIN__
793
629
        BOOL            ret;
814
650
#endif
815
651
}
816
652
 
817
 
/***********************************************************************//**
 
653
/***************************************************************************
818
654
This function returns information of the next file in the directory. We jump
819
 
over the '.' and '..' entries in the directory.
820
 
@return 0 if ok, -1 if error, 1 if at the end of the directory */
 
655
over the '.' and '..' entries in the directory. */
821
656
UNIV_INTERN
822
657
int
823
658
os_file_readdir_next_file(
824
659
/*======================*/
825
 
        const char*     dirname,/*!< in: directory name or path */
826
 
        os_file_dir_t   dir,    /*!< in: directory stream */
827
 
        os_file_stat_t* info)   /*!< in/out: buffer where the info is returned */
 
660
                                /* out: 0 if ok, -1 if error, 1 if at the end
 
661
                                of the directory */
 
662
        const char*     dirname,/* in: directory name or path */
 
663
        os_file_dir_t   dir,    /* in: directory stream */
 
664
        os_file_stat_t* info)   /* in/out: buffer where the info is returned */
828
665
{
829
666
#ifdef __WIN__
830
667
        LPWIN32_FIND_DATA       lpFindFileData;
856
693
                        /* TODO: MySQL has apparently its own symlink
857
694
                        implementation in Windows, dbname.sym can
858
695
                        redirect a database directory:
859
 
                        REFMAN "windows-symbolic-links.html" */
 
696
                        http://dev.mysql.com/doc/refman/5.1/en/
 
697
                        windows-symbolic-links.html */
860
698
                        info->type = OS_FILE_TYPE_LINK;
861
699
                } else if (lpFindFileData->dwFileAttributes
862
700
                           & FILE_ATTRIBUTE_DIRECTORY) {
900
738
#ifdef HAVE_READDIR_R
901
739
        ret = readdir_r(dir, (struct dirent*)dirent_buf, &ent);
902
740
 
903
 
        if (ret != 0
904
 
#ifdef UNIV_AIX
905
 
            /* On AIX, only if we got non-NULL 'ent' (result) value and
906
 
            a non-zero 'ret' (return) value, it indicates a failed
907
 
            readdir_r() call. An NULL 'ent' with an non-zero 'ret'
908
 
            would indicate the "end of the directory" is reached. */
909
 
            && ent != NULL
910
 
#endif
911
 
           ) {
 
741
        if (ret != 0) {
912
742
                fprintf(stderr,
913
743
                        "InnoDB: cannot read directory %s, error %lu\n",
914
744
                        dirname, (ulong)ret);
947
777
        ret = stat(full_path, &statinfo);
948
778
 
949
779
        if (ret) {
950
 
 
951
 
                if (errno == ENOENT) {
952
 
                        /* readdir() returned a file that does not exist,
953
 
                        it must have been deleted in the meantime. Do what
954
 
                        would have happened if the file was deleted before
955
 
                        readdir() - ignore and go to the next entry.
956
 
                        If this is the last entry then info->name will still
957
 
                        contain the name of the deleted file when this
958
 
                        function returns, but this is not an issue since the
959
 
                        caller shouldn't be looking at info when end of
960
 
                        directory is returned. */
961
 
 
962
 
                        ut_free(full_path);
963
 
 
964
 
                        goto next_file;
965
 
                }
966
 
 
967
780
                os_file_handle_error_no_exit(full_path, "stat");
968
781
 
969
782
                ut_free(full_path);
989
802
#endif
990
803
}
991
804
 
992
 
/*****************************************************************//**
 
805
/*********************************************************************
993
806
This function attempts to create a directory named pathname. The new directory
994
807
gets default permissions. On Unix the permissions are (0770 & ~umask). If the
995
808
directory exists already, nothing is done and the call succeeds, unless the
996
 
fail_if_exists arguments is true.
997
 
@return TRUE if call succeeds, FALSE on error */
 
809
fail_if_exists arguments is true. */
998
810
UNIV_INTERN
999
811
ibool
1000
812
os_file_create_directory(
1001
813
/*=====================*/
1002
 
        const char*     pathname,       /*!< in: directory name as
 
814
                                        /* out: TRUE if call succeeds,
 
815
                                        FALSE on error */
 
816
        const char*     pathname,       /* in: directory name as
1003
817
                                        null-terminated string */
1004
 
        ibool           fail_if_exists) /*!< in: if TRUE, pre-existing directory
 
818
        ibool           fail_if_exists) /* in: if TRUE, pre-existing directory
1005
819
                                        is treated as an error. */
1006
820
{
1007
821
#ifdef __WIN__
1034
848
#endif
1035
849
}
1036
850
 
1037
 
/****************************************************************//**
1038
 
NOTE! Use the corresponding macro os_file_create_simple(), not directly
1039
 
this function!
1040
 
A simple function to open or create a file.
1041
 
@return own: handle to the file, not defined if error, error number
1042
 
can be retrieved with os_file_get_last_error */
 
851
/********************************************************************
 
852
A simple function to open or create a file. */
1043
853
UNIV_INTERN
1044
854
os_file_t
1045
 
os_file_create_simple_func(
1046
 
/*=======================*/
1047
 
        const char*     name,   /*!< in: name of the file or path as a
 
855
os_file_create_simple(
 
856
/*==================*/
 
857
                                /* out, own: handle to the file, not defined
 
858
                                if error, error number can be retrieved with
 
859
                                os_file_get_last_error */
 
860
        const char*     name,   /* in: name of the file or path as a
1048
861
                                null-terminated string */
1049
 
        ulint           create_mode,/*!< in: OS_FILE_OPEN if an existing file is
 
862
        ulint           create_mode,/* in: OS_FILE_OPEN if an existing file is
1050
863
                                opened (if does not exist, error), or
1051
864
                                OS_FILE_CREATE if a new file is created
1052
865
                                (if exists, error), or
1053
866
                                OS_FILE_CREATE_PATH if new file
1054
867
                                (if exists, error) and subdirectories along
1055
868
                                its path are created (if needed)*/
1056
 
        ulint           access_type,/*!< in: OS_FILE_READ_ONLY or
 
869
        ulint           access_type,/* in: OS_FILE_READ_ONLY or
1057
870
                                OS_FILE_READ_WRITE */
1058
 
        ibool*          success)/*!< out: TRUE if succeed, FALSE if error */
 
871
        ibool*          success)/* out: TRUE if succeed, FALSE if error */
1059
872
{
1060
873
#ifdef __WIN__
1061
874
        os_file_t       file;
1101
914
                          NULL, /* default security attributes */
1102
915
                          create_flag,
1103
916
                          attributes,
1104
 
                          NULL);        /*!< no template file */
 
917
                          NULL);        /* no template file */
1105
918
 
1106
919
        if (file == INVALID_HANDLE_VALUE) {
1107
920
                *success = FALSE;
1177
990
#endif /* __WIN__ */
1178
991
}
1179
992
 
1180
 
/****************************************************************//**
1181
 
NOTE! Use the corresponding macro
1182
 
os_file_create_simple_no_error_handling(), not directly this function!
1183
 
A simple function to open or create a file.
1184
 
@return own: handle to the file, not defined if error, error number
1185
 
can be retrieved with os_file_get_last_error */
 
993
/********************************************************************
 
994
A simple function to open or create a file. */
1186
995
UNIV_INTERN
1187
996
os_file_t
1188
 
os_file_create_simple_no_error_handling_func(
1189
 
/*=========================================*/
1190
 
        const char*     name,   /*!< in: name of the file or path as a
 
997
os_file_create_simple_no_error_handling(
 
998
/*====================================*/
 
999
                                /* out, own: handle to the file, not defined
 
1000
                                if error, error number can be retrieved with
 
1001
                                os_file_get_last_error */
 
1002
        const char*     name,   /* in: name of the file or path as a
1191
1003
                                null-terminated string */
1192
 
        ulint           create_mode,/*!< in: OS_FILE_OPEN if an existing file
 
1004
        ulint           create_mode,/* in: OS_FILE_OPEN if an existing file
1193
1005
                                is opened (if does not exist, error), or
1194
1006
                                OS_FILE_CREATE if a new file is created
1195
1007
                                (if exists, error) */
1196
 
        ulint           access_type,/*!< in: OS_FILE_READ_ONLY,
 
1008
        ulint           access_type,/* in: OS_FILE_READ_ONLY,
1197
1009
                                OS_FILE_READ_WRITE, or
1198
1010
                                OS_FILE_READ_ALLOW_DELETE; the last option is
1199
1011
                                used by a backup program reading the file */
1200
 
        ibool*          success)/*!< out: TRUE if succeed, FALSE if error */
 
1012
        ibool*          success)/* out: TRUE if succeed, FALSE if error */
1201
1013
{
1202
1014
#ifdef __WIN__
1203
1015
        os_file_t       file;
1224
1036
        } else if (access_type == OS_FILE_READ_ALLOW_DELETE) {
1225
1037
                access = GENERIC_READ;
1226
1038
                share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ
1227
 
                        | FILE_SHARE_WRITE;     /*!< A backup program has to give
 
1039
                        | FILE_SHARE_WRITE;     /* A backup program has to give
1228
1040
                                                mysqld the maximum freedom to
1229
1041
                                                do what it likes with the
1230
1042
                                                file */
1239
1051
                          NULL, /* default security attributes */
1240
1052
                          create_flag,
1241
1053
                          attributes,
1242
 
                          NULL);        /*!< no template file */
 
1054
                          NULL);        /* no template file */
1243
1055
 
1244
1056
        if (file == INVALID_HANDLE_VALUE) {
1245
1057
                *success = FALSE;
1291
1103
#endif /* __WIN__ */
1292
1104
}
1293
1105
 
1294
 
/****************************************************************//**
 
1106
/********************************************************************
1295
1107
Tries to disable OS caching on an opened file descriptor. */
1296
1108
UNIV_INTERN
1297
1109
void
1298
1110
os_file_set_nocache(
1299
1111
/*================*/
1300
 
        int             fd,             /*!< in: file descriptor to alter */
1301
 
        const char*     file_name,      /*!< in: file name, used in the
 
1112
        int             fd,             /* in: file descriptor to alter */
 
1113
        const char*     file_name,      /* in: file name, used in the
1302
1114
                                        diagnostic message */
1303
 
        const char*     operation_name) /*!< in: "open" or "create"; used in the
 
1115
        const char*     operation_name) /* in: "open" or "create"; used in the
1304
1116
                                        diagnostic message */
1305
1117
{
1306
1118
        /* some versions of Solaris may not have DIRECTIO_ON */
1338
1150
#endif
1339
1151
}
1340
1152
 
1341
 
/****************************************************************//**
1342
 
NOTE! Use the corresponding macro os_file_create(), not directly
1343
 
this function!
1344
 
Opens an existing file or creates a new.
1345
 
@return own: handle to the file, not defined if error, error number
1346
 
can be retrieved with os_file_get_last_error */
 
1153
/********************************************************************
 
1154
Opens an existing file or creates a new. */
1347
1155
UNIV_INTERN
1348
1156
os_file_t
1349
 
os_file_create_func(
1350
 
/*================*/
1351
 
        const char*     name,   /*!< in: name of the file or path as a
 
1157
os_file_create(
 
1158
/*===========*/
 
1159
                                /* out, own: handle to the file, not defined
 
1160
                                if error, error number can be retrieved with
 
1161
                                os_file_get_last_error */
 
1162
        const char*     name,   /* in: name of the file or path as a
1352
1163
                                null-terminated string */
1353
 
        ulint           create_mode,/*!< in: OS_FILE_OPEN if an existing file
 
1164
        ulint           create_mode,/* in: OS_FILE_OPEN if an existing file
1354
1165
                                is opened (if does not exist, error), or
1355
1166
                                OS_FILE_CREATE if a new file is created
1356
1167
                                (if exists, error),
1358
1169
                                or an old overwritten;
1359
1170
                                OS_FILE_OPEN_RAW, if a raw device or disk
1360
1171
                                partition should be opened */
1361
 
        ulint           purpose,/*!< in: OS_FILE_AIO, if asynchronous,
 
1172
        ulint           purpose,/* in: OS_FILE_AIO, if asynchronous,
1362
1173
                                non-buffered i/o is desired,
1363
1174
                                OS_FILE_NORMAL, if any normal file;
1364
1175
                                NOTE that it also depends on type, os_aio_..
1365
1176
                                and srv_.. variables whether we really use
1366
1177
                                async i/o or unbuffered i/o: look in the
1367
1178
                                function source code for the exact rules */
1368
 
        ulint           type,   /*!< in: OS_DATA_FILE or OS_LOG_FILE */
1369
 
        ibool*          success)/*!< out: TRUE if succeed, FALSE if error */
 
1179
        ulint           type,   /* in: OS_DATA_FILE or OS_LOG_FILE */
 
1180
        ibool*          success)/* out: TRUE if succeed, FALSE if error */
1370
1181
{
1371
1182
#ifdef __WIN__
1372
1183
        os_file_t       file;
1397
1208
                buffering of writes in the OS */
1398
1209
                attributes = 0;
1399
1210
#ifdef WIN_ASYNC_IO
1400
 
                if (srv_use_native_aio) {
 
1211
                if (os_aio_use_native_aio) {
1401
1212
                        attributes = attributes | FILE_FLAG_OVERLAPPED;
1402
1213
                }
1403
1214
#endif
1404
1215
#ifdef UNIV_NON_BUFFERED_IO
1405
 
# ifndef UNIV_HOTBACKUP
1406
1216
                if (type == OS_LOG_FILE && srv_flush_log_at_trx_commit == 2) {
1407
1217
                        /* Do not use unbuffered i/o to log files because
1408
1218
                        value 2 denotes that we do not flush the log at every
1411
1221
                           == SRV_WIN_IO_UNBUFFERED) {
1412
1222
                        attributes = attributes | FILE_FLAG_NO_BUFFERING;
1413
1223
                }
1414
 
# else /* !UNIV_HOTBACKUP */
1415
 
                attributes = attributes | FILE_FLAG_NO_BUFFERING;
1416
 
# endif /* !UNIV_HOTBACKUP */
1417
 
#endif /* UNIV_NON_BUFFERED_IO */
 
1224
#endif
1418
1225
        } else if (purpose == OS_FILE_NORMAL) {
1419
1226
                attributes = 0;
1420
1227
#ifdef UNIV_NON_BUFFERED_IO
1421
 
# ifndef UNIV_HOTBACKUP
1422
1228
                if (type == OS_LOG_FILE && srv_flush_log_at_trx_commit == 2) {
1423
1229
                        /* Do not use unbuffered i/o to log files because
1424
1230
                        value 2 denotes that we do not flush the log at every
1427
1233
                           == SRV_WIN_IO_UNBUFFERED) {
1428
1234
                        attributes = attributes | FILE_FLAG_NO_BUFFERING;
1429
1235
                }
1430
 
# else /* !UNIV_HOTBACKUP */
1431
 
                attributes = attributes | FILE_FLAG_NO_BUFFERING;
1432
 
# endif /* !UNIV_HOTBACKUP */
1433
 
#endif /* UNIV_NON_BUFFERED_IO */
 
1236
#endif
1434
1237
        } else {
1435
1238
                attributes = 0;
1436
1239
                ut_error;
1453
1256
                          NULL, /* default security attributes */
1454
1257
                          create_flag,
1455
1258
                          attributes,
1456
 
                          NULL);        /*!< no template file */
 
1259
                          NULL);        /* no template file */
1457
1260
 
1458
1261
        if (file == INVALID_HANDLE_VALUE) {
1459
1262
                *success = FALSE;
1460
1263
 
1461
1264
                /* When srv_file_per_table is on, file creation failure may not
1462
1265
                be critical to the whole instance. Do not crash the server in
1463
 
                case of unknown errors.
1464
 
                Please note "srv_file_per_table" is a global variable with
1465
 
                no explicit synchronization protection. It could be
1466
 
                changed during this execution path. It might not have the
1467
 
                same value as the one when building the table definition */
 
1266
                case of unknown errors. */
1468
1267
                if (srv_file_per_table) {
1469
1268
                        retry = os_file_handle_error_no_exit(name,
1470
1269
                                                create_mode == OS_FILE_CREATE ?
1488
1287
        int             create_flag;
1489
1288
        ibool           retry;
1490
1289
        const char*     mode_str        = NULL;
 
1290
        const char*     type_str        = NULL;
 
1291
        const char*     purpose_str     = NULL;
1491
1292
 
1492
1293
try_again:
1493
1294
        ut_a(name);
1507
1308
                ut_error;
1508
1309
        }
1509
1310
 
1510
 
        ut_a(type == OS_LOG_FILE || type == OS_DATA_FILE);
1511
 
        ut_a(purpose == OS_FILE_AIO || purpose == OS_FILE_NORMAL);
1512
 
 
 
1311
        if (type == OS_LOG_FILE) {
 
1312
                type_str = "LOG";
 
1313
        } else if (type == OS_DATA_FILE) {
 
1314
                type_str = "DATA";
 
1315
        } else {
 
1316
                ut_error;
 
1317
        }
 
1318
 
 
1319
        if (purpose == OS_FILE_AIO) {
 
1320
                purpose_str = "AIO";
 
1321
        } else if (purpose == OS_FILE_NORMAL) {
 
1322
                purpose_str = "NORMAL";
 
1323
        } else {
 
1324
                ut_error;
 
1325
        }
 
1326
 
 
1327
#if 0
 
1328
        fprintf(stderr, "Opening file %s, mode %s, type %s, purpose %s\n",
 
1329
                name, mode_str, type_str, purpose_str);
 
1330
#endif
1513
1331
#ifdef O_SYNC
1514
1332
        /* We let O_SYNC only affect log files; note that we map O_DSYNC to
1515
1333
        O_SYNC because the datasync options seemed to corrupt files in 2001
1532
1350
 
1533
1351
                /* When srv_file_per_table is on, file creation failure may not
1534
1352
                be critical to the whole instance. Do not crash the server in
1535
 
                case of unknown errors.
1536
 
                Please note "srv_file_per_table" is a global variable with
1537
 
                no explicit synchronization protection. It could be
1538
 
                changed during this execution path. It might not have the
1539
 
                same value as the one when building the table definition */
 
1353
                case of unknown errors. */
1540
1354
                if (srv_file_per_table) {
1541
1355
                        retry = os_file_handle_error_no_exit(name,
1542
1356
                                                create_mode == OS_FILE_CREATE ?
1595
1409
#endif /* __WIN__ */
1596
1410
}
1597
1411
 
1598
 
/***********************************************************************//**
1599
 
Deletes a file if it exists. The file has to be closed before calling this.
1600
 
@return TRUE if success */
 
1412
/***************************************************************************
 
1413
Deletes a file if it exists. The file has to be closed before calling this. */
1601
1414
UNIV_INTERN
1602
1415
ibool
1603
1416
os_file_delete_if_exists(
1604
1417
/*=====================*/
1605
 
        const char*     name)   /*!< in: file path as a null-terminated string */
 
1418
                                /* out: TRUE if success */
 
1419
        const char*     name)   /* in: file path as a null-terminated string */
1606
1420
{
1607
1421
#ifdef __WIN__
1608
1422
        BOOL    ret;
1657
1471
#endif
1658
1472
}
1659
1473
 
1660
 
/***********************************************************************//**
1661
 
Deletes a file. The file has to be closed before calling this.
1662
 
@return TRUE if success */
 
1474
/***************************************************************************
 
1475
Deletes a file. The file has to be closed before calling this. */
1663
1476
UNIV_INTERN
1664
1477
ibool
1665
1478
os_file_delete(
1666
1479
/*===========*/
1667
 
        const char*     name)   /*!< in: file path as a null-terminated string */
 
1480
                                /* out: TRUE if success */
 
1481
        const char*     name)   /* in: file path as a null-terminated string */
1668
1482
{
1669
1483
#ifdef __WIN__
1670
1484
        BOOL    ret;
1720
1534
#endif
1721
1535
}
1722
1536
 
1723
 
/***********************************************************************//**
1724
 
NOTE! Use the corresponding macro os_file_rename(), not directly this function!
 
1537
/***************************************************************************
1725
1538
Renames a file (can also move it to another directory). It is safest that the
1726
 
file is closed before calling this function.
1727
 
@return TRUE if success */
 
1539
file is closed before calling this function. */
1728
1540
UNIV_INTERN
1729
1541
ibool
1730
 
os_file_rename_func(
1731
 
/*================*/
1732
 
        const char*     oldpath,/*!< in: old file path as a null-terminated
 
1542
os_file_rename(
 
1543
/*===========*/
 
1544
                                /* out: TRUE if success */
 
1545
        const char*     oldpath,/* in: old file path as a null-terminated
1733
1546
                                string */
1734
 
        const char*     newpath)/*!< in: new file path */
 
1547
        const char*     newpath)/* in: new file path */
1735
1548
{
1736
1549
#ifdef __WIN__
1737
1550
        BOOL    ret;
1760
1573
#endif
1761
1574
}
1762
1575
 
1763
 
/***********************************************************************//**
1764
 
NOTE! Use the corresponding macro os_file_close(), not directly this function!
 
1576
/***************************************************************************
1765
1577
Closes a file handle. In case of error, error number can be retrieved with
1766
 
os_file_get_last_error.
1767
 
@return TRUE if success */
 
1578
os_file_get_last_error. */
1768
1579
UNIV_INTERN
1769
1580
ibool
1770
 
os_file_close_func(
1771
 
/*===============*/
1772
 
        os_file_t       file)   /*!< in, own: handle to a file */
 
1581
os_file_close(
 
1582
/*==========*/
 
1583
                                /* out: TRUE if success */
 
1584
        os_file_t       file)   /* in, own: handle to a file */
1773
1585
{
1774
1586
#ifdef __WIN__
1775
1587
        BOOL    ret;
1800
1612
#endif
1801
1613
}
1802
1614
 
1803
 
#ifdef UNIV_HOTBACKUP
1804
 
/***********************************************************************//**
1805
 
Closes a file handle.
1806
 
@return TRUE if success */
 
1615
/***************************************************************************
 
1616
Closes a file handle. */
1807
1617
UNIV_INTERN
1808
1618
ibool
1809
1619
os_file_close_no_error_handling(
1810
1620
/*============================*/
1811
 
        os_file_t       file)   /*!< in, own: handle to a file */
 
1621
                                /* out: TRUE if success */
 
1622
        os_file_t       file)   /* in, own: handle to a file */
1812
1623
{
1813
1624
#ifdef __WIN__
1814
1625
        BOOL    ret;
1835
1646
        return(TRUE);
1836
1647
#endif
1837
1648
}
1838
 
#endif /* UNIV_HOTBACKUP */
1839
1649
 
1840
 
/***********************************************************************//**
1841
 
Gets a file size.
1842
 
@return TRUE if success */
 
1650
/***************************************************************************
 
1651
Gets a file size. */
1843
1652
UNIV_INTERN
1844
1653
ibool
1845
1654
os_file_get_size(
1846
1655
/*=============*/
1847
 
        os_file_t       file,   /*!< in: handle to a file */
1848
 
        ulint*          size,   /*!< out: least significant 32 bits of file
 
1656
                                /* out: TRUE if success */
 
1657
        os_file_t       file,   /* in: handle to a file */
 
1658
        ulint*          size,   /* out: least significant 32 bits of file
1849
1659
                                size */
1850
 
        ulint*          size_high)/*!< out: most significant 32 bits of size */
 
1660
        ulint*          size_high)/* out: most significant 32 bits of size */
1851
1661
{
1852
1662
#ifdef __WIN__
1853
1663
        DWORD   high;
1885
1695
#endif
1886
1696
}
1887
1697
 
1888
 
/***********************************************************************//**
1889
 
Gets file size as a 64-bit integer ib_int64_t.
1890
 
@return size in bytes, -1 if error */
 
1698
/***************************************************************************
 
1699
Gets file size as a 64-bit integer ib_int64_t. */
1891
1700
UNIV_INTERN
1892
1701
ib_int64_t
1893
1702
os_file_get_size_as_iblonglong(
1894
1703
/*===========================*/
1895
 
        os_file_t       file)   /*!< in: handle to a file */
 
1704
                                /* out: size in bytes, -1 if error */
 
1705
        os_file_t       file)   /* in: handle to a file */
1896
1706
{
1897
1707
        ulint   size;
1898
1708
        ulint   size_high;
1908
1718
        return((((ib_int64_t)size_high) << 32) + (ib_int64_t)size);
1909
1719
}
1910
1720
 
1911
 
/***********************************************************************//**
1912
 
Write the specified number of zeros to a newly created file.
1913
 
@return TRUE if success */
 
1721
/***************************************************************************
 
1722
Write the specified number of zeros to a newly created file. */
1914
1723
UNIV_INTERN
1915
1724
ibool
1916
1725
os_file_set_size(
1917
1726
/*=============*/
1918
 
        const char*     name,   /*!< in: name of the file or path as a
 
1727
                                /* out: TRUE if success */
 
1728
        const char*     name,   /* in: name of the file or path as a
1919
1729
                                null-terminated string */
1920
 
        os_file_t       file,   /*!< in: handle to a file */
1921
 
        ulint           size,   /*!< in: least significant 32 bits of file
 
1730
        os_file_t       file,   /* in: handle to a file */
 
1731
        ulint           size,   /* in: least significant 32 bits of file
1922
1732
                                size */
1923
 
        ulint           size_high)/*!< in: most significant 32 bits of size */
 
1733
        ulint           size_high)/* in: most significant 32 bits of size */
1924
1734
{
1925
1735
        ib_int64_t      current_size;
1926
1736
        ib_int64_t      desired_size;
1997
1807
        return(FALSE);
1998
1808
}
1999
1809
 
2000
 
/***********************************************************************//**
2001
 
Truncates a file at its current position.
2002
 
@return TRUE if success */
 
1810
/***************************************************************************
 
1811
Truncates a file at its current position. */
2003
1812
UNIV_INTERN
2004
1813
ibool
2005
1814
os_file_set_eof(
2006
1815
/*============*/
2007
 
        FILE*           file)   /*!< in: file to be truncated */
 
1816
                                /* out: TRUE if success */
 
1817
        FILE*           file)   /* in: file to be truncated */
2008
1818
{
2009
1819
#ifdef __WIN__
2010
1820
        HANDLE h = (HANDLE) _get_osfhandle(fileno(file));
2015
1825
}
2016
1826
 
2017
1827
#ifndef __WIN__
2018
 
/***********************************************************************//**
 
1828
/***************************************************************************
2019
1829
Wrapper to fsync(2) that retries the call on some errors.
2020
1830
Returns the value 0 if successful; otherwise the value -1 is returned and
2021
 
the global variable errno is set to indicate the error.
2022
 
@return 0 if success, -1 otherwise */
 
1831
the global variable errno is set to indicate the error. */
2023
1832
 
2024
1833
static
2025
1834
int
2026
1835
os_file_fsync(
2027
1836
/*==========*/
2028
 
        os_file_t       file)   /*!< in: handle to a file */
 
1837
                                /* out: 0 if success, -1 otherwise */
 
1838
        os_file_t       file)   /* in: handle to a file */
2029
1839
{
2030
1840
        int     ret;
2031
1841
        int     failures;
2063
1873
}
2064
1874
#endif /* !__WIN__ */
2065
1875
 
2066
 
/***********************************************************************//**
2067
 
NOTE! Use the corresponding macro os_file_flush(), not directly this function!
2068
 
Flushes the write buffers of a given file to the disk.
2069
 
@return TRUE if success */
 
1876
/***************************************************************************
 
1877
Flushes the write buffers of a given file to the disk. */
2070
1878
UNIV_INTERN
2071
1879
ibool
2072
 
os_file_flush_func(
2073
 
/*===============*/
2074
 
        os_file_t       file)   /*!< in, own: handle to a file */
 
1880
os_file_flush(
 
1881
/*==========*/
 
1882
                                /* out: TRUE if success */
 
1883
        os_file_t       file)   /* in, own: handle to a file */
2075
1884
{
2076
1885
#ifdef __WIN__
2077
1886
        BOOL    ret;
2163
1972
}
2164
1973
 
2165
1974
#ifndef __WIN__
2166
 
/*******************************************************************//**
2167
 
Does a synchronous read operation in Posix.
2168
 
@return number of bytes read, -1 if error */
 
1975
/***********************************************************************
 
1976
Does a synchronous read operation in Posix. */
2169
1977
static
2170
1978
ssize_t
2171
1979
os_file_pread(
2172
1980
/*==========*/
2173
 
        os_file_t       file,   /*!< in: handle to a file */
2174
 
        void*           buf,    /*!< in: buffer where to read */
2175
 
        ulint           n,      /*!< in: number of bytes to read */
2176
 
        ulint           offset, /*!< in: least significant 32 bits of file
 
1981
                                /* out: number of bytes read, -1 if error */
 
1982
        os_file_t       file,   /* in: handle to a file */
 
1983
        void*           buf,    /* in: buffer where to read */
 
1984
        ulint           n,      /* in: number of bytes to read */
 
1985
        ulint           offset, /* in: least significant 32 bits of file
2177
1986
                                offset from where to read */
2178
 
        ulint           offset_high) /*!< in: most significant 32 bits of
 
1987
        ulint           offset_high) /* in: most significant 32 bits of
2179
1988
                                offset */
2180
1989
{
2181
1990
        off_t   offs;
2182
 
#if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD)
2183
1991
        ssize_t n_bytes;
2184
 
#endif /* HAVE_PREAD && !HAVE_BROKEN_PREAD */
2185
1992
 
2186
1993
        ut_a((offset & 0xFFFFFFFFUL) == offset);
2187
1994
 
2220
2027
        {
2221
2028
                off_t   ret_offset;
2222
2029
                ssize_t ret;
2223
 
#ifndef UNIV_HOTBACKUP
2224
2030
                ulint   i;
2225
 
#endif /* !UNIV_HOTBACKUP */
2226
2031
 
2227
2032
                os_mutex_enter(os_file_count_mutex);
2228
2033
                os_n_pending_reads++;
2229
2034
                os_mutex_exit(os_file_count_mutex);
2230
2035
 
2231
 
#ifndef UNIV_HOTBACKUP
2232
2036
                /* Protect the seek / read operation with a mutex */
2233
2037
                i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2234
2038
 
2235
2039
                os_mutex_enter(os_file_seek_mutexes[i]);
2236
 
#endif /* !UNIV_HOTBACKUP */
2237
2040
 
2238
2041
                ret_offset = lseek(file, offs, SEEK_SET);
2239
2042
 
2243
2046
                        ret = read(file, buf, (ssize_t)n);
2244
2047
                }
2245
2048
 
2246
 
#ifndef UNIV_HOTBACKUP
2247
2049
                os_mutex_exit(os_file_seek_mutexes[i]);
2248
 
#endif /* !UNIV_HOTBACKUP */
2249
2050
 
2250
2051
                os_mutex_enter(os_file_count_mutex);
2251
2052
                os_n_pending_reads--;
2256
2057
#endif
2257
2058
}
2258
2059
 
2259
 
/*******************************************************************//**
2260
 
Does a synchronous write operation in Posix.
2261
 
@return number of bytes written, -1 if error */
 
2060
/***********************************************************************
 
2061
Does a synchronous write operation in Posix. */
2262
2062
static
2263
2063
ssize_t
2264
2064
os_file_pwrite(
2265
2065
/*===========*/
2266
 
        os_file_t       file,   /*!< in: handle to a file */
2267
 
        const void*     buf,    /*!< in: buffer from where to write */
2268
 
        ulint           n,      /*!< in: number of bytes to write */
2269
 
        ulint           offset, /*!< in: least significant 32 bits of file
 
2066
                                /* out: number of bytes written, -1 if error */
 
2067
        os_file_t       file,   /* in: handle to a file */
 
2068
        const void*     buf,    /* in: buffer from where to write */
 
2069
        ulint           n,      /* in: number of bytes to write */
 
2070
        ulint           offset, /* in: least significant 32 bits of file
2270
2071
                                offset where to write */
2271
 
        ulint           offset_high) /*!< in: most significant 32 bits of
 
2072
        ulint           offset_high) /* in: most significant 32 bits of
2272
2073
                                offset */
2273
2074
{
2274
2075
        ssize_t ret;
2323
2124
#else
2324
2125
        {
2325
2126
                off_t   ret_offset;
2326
 
# ifndef UNIV_HOTBACKUP
2327
2127
                ulint   i;
2328
 
# endif /* !UNIV_HOTBACKUP */
2329
2128
 
2330
2129
                os_mutex_enter(os_file_count_mutex);
2331
2130
                os_n_pending_writes++;
2332
2131
                os_mutex_exit(os_file_count_mutex);
2333
2132
 
2334
 
# ifndef UNIV_HOTBACKUP
2335
2133
                /* Protect the seek / write operation with a mutex */
2336
2134
                i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2337
2135
 
2338
2136
                os_mutex_enter(os_file_seek_mutexes[i]);
2339
 
# endif /* UNIV_HOTBACKUP */
2340
2137
 
2341
2138
                ret_offset = lseek(file, offs, SEEK_SET);
2342
2139
 
2362
2159
# endif /* UNIV_DO_FLUSH */
2363
2160
 
2364
2161
func_exit:
2365
 
# ifndef UNIV_HOTBACKUP
2366
2162
                os_mutex_exit(os_file_seek_mutexes[i]);
2367
 
# endif /* !UNIV_HOTBACKUP */
2368
2163
 
2369
2164
                os_mutex_enter(os_file_count_mutex);
2370
2165
                os_n_pending_writes--;
2376
2171
}
2377
2172
#endif
2378
2173
 
2379
 
/*******************************************************************//**
2380
 
NOTE! Use the corresponding macro os_file_read(), not directly this
2381
 
function!
2382
 
Requests a synchronous positioned read operation.
2383
 
@return TRUE if request was successful, FALSE if fail */
 
2174
/***********************************************************************
 
2175
Requests a synchronous positioned read operation. */
2384
2176
UNIV_INTERN
2385
2177
ibool
2386
 
os_file_read_func(
2387
 
/*==============*/
2388
 
        os_file_t       file,   /*!< in: handle to a file */
2389
 
        void*           buf,    /*!< in: buffer where to read */
2390
 
        ulint           offset, /*!< in: least significant 32 bits of file
 
2178
os_file_read(
 
2179
/*=========*/
 
2180
                                /* out: TRUE if request was
 
2181
                                successful, FALSE if fail */
 
2182
        os_file_t       file,   /* in: handle to a file */
 
2183
        void*           buf,    /* in: buffer where to read */
 
2184
        ulint           offset, /* in: least significant 32 bits of file
2391
2185
                                offset where to read */
2392
 
        ulint           offset_high, /*!< in: most significant 32 bits of
 
2186
        ulint           offset_high, /* in: most significant 32 bits of
2393
2187
                                offset */
2394
 
        ulint           n)      /*!< in: number of bytes to read */
 
2188
        ulint           n)      /* in: number of bytes to read */
2395
2189
{
2396
2190
#ifdef __WIN__
2397
2191
        BOOL            ret;
2400
2194
        DWORD           low;
2401
2195
        DWORD           high;
2402
2196
        ibool           retry;
2403
 
#ifndef UNIV_HOTBACKUP
2404
2197
        ulint           i;
2405
 
#endif /* !UNIV_HOTBACKUP */
2406
2198
 
2407
2199
        ut_a((offset & 0xFFFFFFFFUL) == offset);
2408
2200
 
2421
2213
        os_n_pending_reads++;
2422
2214
        os_mutex_exit(os_file_count_mutex);
2423
2215
 
2424
 
#ifndef UNIV_HOTBACKUP
2425
2216
        /* Protect the seek / read operation with a mutex */
2426
2217
        i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2427
2218
 
2428
2219
        os_mutex_enter(os_file_seek_mutexes[i]);
2429
 
#endif /* !UNIV_HOTBACKUP */
2430
2220
 
2431
2221
        ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);
2432
2222
 
2433
2223
        if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
2434
2224
 
2435
 
#ifndef UNIV_HOTBACKUP
2436
2225
                os_mutex_exit(os_file_seek_mutexes[i]);
2437
 
#endif /* !UNIV_HOTBACKUP */
2438
2226
 
2439
2227
                os_mutex_enter(os_file_count_mutex);
2440
2228
                os_n_pending_reads--;
2445
2233
 
2446
2234
        ret = ReadFile(file, buf, (DWORD) n, &len, NULL);
2447
2235
 
2448
 
#ifndef UNIV_HOTBACKUP
2449
2236
        os_mutex_exit(os_file_seek_mutexes[i]);
2450
 
#endif /* !UNIV_HOTBACKUP */
2451
2237
 
2452
2238
        os_mutex_enter(os_file_count_mutex);
2453
2239
        os_n_pending_reads--;
2456
2242
        if (ret && len == n) {
2457
2243
                return(TRUE);
2458
2244
        }
2459
 
#else /* __WIN__ */
 
2245
#else
2460
2246
        ibool   retry;
2461
2247
        ssize_t ret;
2462
2248
 
2475
2261
                "InnoDB: Was only able to read %ld.\n",
2476
2262
                (ulong)n, (ulong)offset_high,
2477
2263
                (ulong)offset, (long)ret);
2478
 
#endif /* __WIN__ */
 
2264
#endif
2479
2265
#ifdef __WIN__
2480
2266
error_handling:
2481
2267
#endif
2501
2287
        return(FALSE);
2502
2288
}
2503
2289
 
2504
 
/*******************************************************************//**
2505
 
NOTE! Use the corresponding macro os_file_read_no_error_handling(),
2506
 
not directly this function!
 
2290
/***********************************************************************
2507
2291
Requests a synchronous positioned read operation. This function does not do
2508
 
any error handling. In case of error it returns FALSE.
2509
 
@return TRUE if request was successful, FALSE if fail */
 
2292
any error handling. In case of error it returns FALSE. */
2510
2293
UNIV_INTERN
2511
2294
ibool
2512
 
os_file_read_no_error_handling_func(
2513
 
/*================================*/
2514
 
        os_file_t       file,   /*!< in: handle to a file */
2515
 
        void*           buf,    /*!< in: buffer where to read */
2516
 
        ulint           offset, /*!< in: least significant 32 bits of file
 
2295
os_file_read_no_error_handling(
 
2296
/*===========================*/
 
2297
                                /* out: TRUE if request was
 
2298
                                successful, FALSE if fail */
 
2299
        os_file_t       file,   /* in: handle to a file */
 
2300
        void*           buf,    /* in: buffer where to read */
 
2301
        ulint           offset, /* in: least significant 32 bits of file
2517
2302
                                offset where to read */
2518
 
        ulint           offset_high, /*!< in: most significant 32 bits of
 
2303
        ulint           offset_high, /* in: most significant 32 bits of
2519
2304
                                offset */
2520
 
        ulint           n)      /*!< in: number of bytes to read */
 
2305
        ulint           n)      /* in: number of bytes to read */
2521
2306
{
2522
2307
#ifdef __WIN__
2523
2308
        BOOL            ret;
2526
2311
        DWORD           low;
2527
2312
        DWORD           high;
2528
2313
        ibool           retry;
2529
 
#ifndef UNIV_HOTBACKUP
2530
2314
        ulint           i;
2531
 
#endif /* !UNIV_HOTBACKUP */
2532
2315
 
2533
2316
        ut_a((offset & 0xFFFFFFFFUL) == offset);
2534
2317
 
2547
2330
        os_n_pending_reads++;
2548
2331
        os_mutex_exit(os_file_count_mutex);
2549
2332
 
2550
 
#ifndef UNIV_HOTBACKUP
2551
2333
        /* Protect the seek / read operation with a mutex */
2552
2334
        i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2553
2335
 
2554
2336
        os_mutex_enter(os_file_seek_mutexes[i]);
2555
 
#endif /* !UNIV_HOTBACKUP */
2556
2337
 
2557
2338
        ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);
2558
2339
 
2559
2340
        if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
2560
2341
 
2561
 
#ifndef UNIV_HOTBACKUP
2562
2342
                os_mutex_exit(os_file_seek_mutexes[i]);
2563
 
#endif /* !UNIV_HOTBACKUP */
2564
2343
 
2565
2344
                os_mutex_enter(os_file_count_mutex);
2566
2345
                os_n_pending_reads--;
2571
2350
 
2572
2351
        ret = ReadFile(file, buf, (DWORD) n, &len, NULL);
2573
2352
 
2574
 
#ifndef UNIV_HOTBACKUP
2575
2353
        os_mutex_exit(os_file_seek_mutexes[i]);
2576
 
#endif /* !UNIV_HOTBACKUP */
2577
2354
 
2578
2355
        os_mutex_enter(os_file_count_mutex);
2579
2356
        os_n_pending_reads--;
2582
2359
        if (ret && len == n) {
2583
2360
                return(TRUE);
2584
2361
        }
2585
 
#else /* __WIN__ */
 
2362
#else
2586
2363
        ibool   retry;
2587
2364
        ssize_t ret;
2588
2365
 
2595
2372
 
2596
2373
                return(TRUE);
2597
2374
        }
2598
 
#endif /* __WIN__ */
 
2375
#endif
2599
2376
#ifdef __WIN__
2600
2377
error_handling:
2601
2378
#endif
2608
2385
        return(FALSE);
2609
2386
}
2610
2387
 
2611
 
/*******************************************************************//**
 
2388
/***********************************************************************
2612
2389
Rewind file to its start, read at most size - 1 bytes from it to str, and
2613
2390
NUL-terminate str. All errors are silently ignored. This function is
2614
2391
mostly meant to be used with temporary files. */
2616
2393
void
2617
2394
os_file_read_string(
2618
2395
/*================*/
2619
 
        FILE*   file,   /*!< in: file to read from */
2620
 
        char*   str,    /*!< in: buffer where to read */
2621
 
        ulint   size)   /*!< in: size of buffer */
 
2396
        FILE*   file,   /* in: file to read from */
 
2397
        char*   str,    /* in: buffer where to read */
 
2398
        ulint   size)   /* in: size of buffer */
2622
2399
{
2623
2400
        size_t  flen;
2624
2401
 
2631
2408
        str[flen] = '\0';
2632
2409
}
2633
2410
 
2634
 
/*******************************************************************//**
2635
 
NOTE! Use the corresponding macro os_file_write(), not directly
2636
 
this function!
2637
 
Requests a synchronous write operation.
2638
 
@return TRUE if request was successful, FALSE if fail */
 
2411
/***********************************************************************
 
2412
Requests a synchronous write operation. */
2639
2413
UNIV_INTERN
2640
2414
ibool
2641
 
os_file_write_func(
2642
 
/*===============*/
2643
 
        const char*     name,   /*!< in: name of the file or path as a
 
2415
os_file_write(
 
2416
/*==========*/
 
2417
                                /* out: TRUE if request was
 
2418
                                successful, FALSE if fail */
 
2419
        const char*     name,   /* in: name of the file or path as a
2644
2420
                                null-terminated string */
2645
 
        os_file_t       file,   /*!< in: handle to a file */
2646
 
        const void*     buf,    /*!< in: buffer from which to write */
2647
 
        ulint           offset, /*!< in: least significant 32 bits of file
 
2421
        os_file_t       file,   /* in: handle to a file */
 
2422
        const void*     buf,    /* in: buffer from which to write */
 
2423
        ulint           offset, /* in: least significant 32 bits of file
2648
2424
                                offset where to write */
2649
 
        ulint           offset_high, /*!< in: most significant 32 bits of
 
2425
        ulint           offset_high, /* in: most significant 32 bits of
2650
2426
                                offset */
2651
 
        ulint           n)      /*!< in: number of bytes to write */
 
2427
        ulint           n)      /* in: number of bytes to write */
2652
2428
{
2653
2429
#ifdef __WIN__
2654
2430
        BOOL            ret;
2656
2432
        DWORD           ret2;
2657
2433
        DWORD           low;
2658
2434
        DWORD           high;
 
2435
        ulint           i;
2659
2436
        ulint           n_retries       = 0;
2660
2437
        ulint           err;
2661
 
#ifndef UNIV_HOTBACKUP
2662
 
        ulint           i;
2663
 
#endif /* !UNIV_HOTBACKUP */
2664
2438
 
2665
2439
        ut_a((offset & 0xFFFFFFFF) == offset);
2666
2440
 
2677
2451
        os_n_pending_writes++;
2678
2452
        os_mutex_exit(os_file_count_mutex);
2679
2453
 
2680
 
#ifndef UNIV_HOTBACKUP
2681
2454
        /* Protect the seek / write operation with a mutex */
2682
2455
        i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2683
2456
 
2684
2457
        os_mutex_enter(os_file_seek_mutexes[i]);
2685
 
#endif /* !UNIV_HOTBACKUP */
2686
2458
 
2687
2459
        ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);
2688
2460
 
2689
2461
        if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
2690
2462
 
2691
 
#ifndef UNIV_HOTBACKUP
2692
2463
                os_mutex_exit(os_file_seek_mutexes[i]);
2693
 
#endif /* !UNIV_HOTBACKUP */
2694
2464
 
2695
2465
                os_mutex_enter(os_file_count_mutex);
2696
2466
                os_n_pending_writes--;
2706
2476
                        "InnoDB: Some operating system error numbers"
2707
2477
                        " are described at\n"
2708
2478
                        "InnoDB: "
2709
 
                        REFMAN "operating-system-error-codes.html\n",
 
2479
                        "http://dev.mysql.com/doc/refman/5.1/en/"
 
2480
                        "operating-system-error-codes.html\n",
2710
2481
                        name, (ulong) offset_high, (ulong) offset,
2711
2482
                        (ulong) GetLastError());
2712
2483
 
2724
2495
        }
2725
2496
# endif /* UNIV_DO_FLUSH */
2726
2497
 
2727
 
#ifndef UNIV_HOTBACKUP
2728
2498
        os_mutex_exit(os_file_seek_mutexes[i]);
2729
 
#endif /* !UNIV_HOTBACKUP */
2730
2499
 
2731
2500
        os_mutex_enter(os_file_count_mutex);
2732
2501
        os_n_pending_writes--;
2779
2548
                        "InnoDB: Some operating system error numbers"
2780
2549
                        " are described at\n"
2781
2550
                        "InnoDB: "
2782
 
                        REFMAN "operating-system-error-codes.html\n");
 
2551
                        "http://dev.mysql.com/doc/refman/5.1/en/"
 
2552
                        "operating-system-error-codes.html\n");
2783
2553
 
2784
2554
                os_has_said_disk_full = TRUE;
2785
2555
        }
2821
2591
                        "InnoDB: Some operating system error numbers"
2822
2592
                        " are described at\n"
2823
2593
                        "InnoDB: "
2824
 
                        REFMAN "operating-system-error-codes.html\n");
 
2594
                        "http://dev.mysql.com/doc/refman/5.1/en/"
 
2595
                        "operating-system-error-codes.html\n");
2825
2596
 
2826
2597
                os_has_said_disk_full = TRUE;
2827
2598
        }
2830
2601
#endif
2831
2602
}
2832
2603
 
2833
 
/*******************************************************************//**
2834
 
Check the existence and type of the given file.
2835
 
@return TRUE if call succeeded */
 
2604
/***********************************************************************
 
2605
Check the existence and type of the given file. */
2836
2606
UNIV_INTERN
2837
2607
ibool
2838
2608
os_file_status(
2839
2609
/*===========*/
2840
 
        const char*     path,   /*!< in:        pathname of the file */
2841
 
        ibool*          exists, /*!< out: TRUE if file exists */
2842
 
        os_file_type_t* type)   /*!< out: type of the file (if it exists) */
 
2610
                                /* out: TRUE if call succeeded */
 
2611
        const char*     path,   /* in:  pathname of the file */
 
2612
        ibool*          exists, /* out: TRUE if file exists */
 
2613
        os_file_type_t* type)   /* out: type of the file (if it exists) */
2843
2614
{
2844
2615
#ifdef __WIN__
2845
2616
        int             ret;
2902
2673
#endif
2903
2674
}
2904
2675
 
2905
 
/*******************************************************************//**
2906
 
This function returns information about the specified file
2907
 
@return TRUE if stat information found */
 
2676
/***********************************************************************
 
2677
This function returns information about the specified file */
2908
2678
UNIV_INTERN
2909
2679
ibool
2910
2680
os_file_get_status(
2911
2681
/*===============*/
2912
 
        const char*     path,           /*!< in:        pathname of the file */
2913
 
        os_file_stat_t* stat_info)      /*!< information of a file in a
 
2682
                                        /* out: TRUE if stat
 
2683
                                        information found */
 
2684
        const char*     path,           /* in:  pathname of the file */
 
2685
        os_file_stat_t* stat_info)      /* information of a file in a
2914
2686
                                        directory */
2915
2687
{
2916
2688
#ifdef __WIN__
2987
2759
#  define OS_FILE_PATH_SEPARATOR        '/'
2988
2760
#endif
2989
2761
 
2990
 
/****************************************************************//**
 
2762
/********************************************************************
2991
2763
The function os_file_dirname returns a directory component of a
2992
2764
null-terminated pathname string.  In the usual case, dirname returns
2993
2765
the string up to, but not including, the final '/', and basename
3013
2785
       "/"            "/"            "/"
3014
2786
       "."            "."            "."
3015
2787
       ".."           "."            ".."
3016
 
 
3017
 
@return own: directory component of the pathname */
 
2788
*/
3018
2789
UNIV_INTERN
3019
2790
char*
3020
2791
os_file_dirname(
3021
2792
/*============*/
3022
 
        const char*     path)   /*!< in: pathname */
 
2793
                                /* out, own: directory component of the
 
2794
                                pathname */
 
2795
        const char*     path)   /* in: pathname */
3023
2796
{
3024
2797
        /* Find the offset of the last slash */
3025
2798
        const char* last_slash = strrchr(path, OS_FILE_PATH_SEPARATOR);
3042
2815
        return(mem_strdupl(path, last_slash - path));
3043
2816
}
3044
2817
 
3045
 
/****************************************************************//**
3046
 
Creates all missing subdirectories along the given path.
3047
 
@return TRUE if call succeeded FALSE otherwise */
 
2818
/********************************************************************
 
2819
Creates all missing subdirectories along the given path. */
3048
2820
UNIV_INTERN
3049
2821
ibool
3050
2822
os_file_create_subdirs_if_needed(
3051
2823
/*=============================*/
3052
 
        const char*     path)   /*!< in: path name */
 
2824
                                /* out: TRUE if call succeeded
 
2825
                                   FALSE otherwise */
 
2826
        const char*     path)   /* in: path name */
3053
2827
{
3054
2828
        char*           subdir;
3055
2829
        ibool           success, subdir_exists;
3082
2856
        return(success);
3083
2857
}
3084
2858
 
3085
 
#ifndef UNIV_HOTBACKUP
3086
 
/****************************************************************//**
3087
 
Returns a pointer to the nth slot in the aio array.
3088
 
@return pointer to slot */
 
2859
/********************************************************************
 
2860
Returns a pointer to the nth slot in the aio array. */
3089
2861
static
3090
2862
os_aio_slot_t*
3091
2863
os_aio_array_get_nth_slot(
3092
2864
/*======================*/
3093
 
        os_aio_array_t*         array,  /*!< in: aio array */
3094
 
        ulint                   index)  /*!< in: index of the slot */
 
2865
                                        /* out: pointer to slot */
 
2866
        os_aio_array_t*         array,  /* in: aio array */
 
2867
        ulint                   index)  /* in: index of the slot */
3095
2868
{
3096
2869
        ut_a(index < array->n_slots);
3097
2870
 
3098
2871
        return((array->slots) + index);
3099
2872
}
3100
2873
 
3101
 
#if defined(LINUX_NATIVE_AIO)
3102
 
/******************************************************************//**
3103
 
Creates an io_context for native linux AIO.
3104
 
@return TRUE on success. */
3105
 
static
3106
 
ibool
3107
 
os_aio_linux_create_io_ctx(
3108
 
/*=======================*/
3109
 
        ulint           max_events,     /*!< in: number of events. */
3110
 
        io_context_t*   io_ctx)         /*!< out: io_ctx to initialize. */
3111
 
{
3112
 
        int     ret;
3113
 
        ulint   retries = 0;
3114
 
 
3115
 
retry:
3116
 
        memset(io_ctx, 0x0, sizeof(*io_ctx));
3117
 
 
3118
 
        /* Initialize the io_ctx. Tell it how many pending
3119
 
        IO requests this context will handle. */
3120
 
 
3121
 
        ret = io_setup(max_events, io_ctx);
3122
 
        if (ret == 0) {
3123
 
#if defined(UNIV_AIO_DEBUG)
3124
 
                fprintf(stderr,
3125
 
                        "InnoDB: Linux native AIO:"
3126
 
                        " initialized io_ctx for segment\n");
3127
 
#endif
3128
 
                /* Success. Return now. */
3129
 
                return(TRUE);
3130
 
        }
3131
 
 
3132
 
        /* If we hit EAGAIN we'll make a few attempts before failing. */
3133
 
 
3134
 
        switch (ret) {
3135
 
        case -EAGAIN:
3136
 
                if (retries == 0) {
3137
 
                        /* First time around. */
3138
 
                        ut_print_timestamp(stderr);
3139
 
                        fprintf(stderr,
3140
 
                                "  InnoDB: Warning: io_setup() failed"
3141
 
                                " with EAGAIN. Will make %d attempts"
3142
 
                                " before giving up.\n",
3143
 
                                OS_AIO_IO_SETUP_RETRY_ATTEMPTS);
3144
 
                }
3145
 
 
3146
 
                if (retries < OS_AIO_IO_SETUP_RETRY_ATTEMPTS) {
3147
 
                        ++retries;
3148
 
                        fprintf(stderr,
3149
 
                                "InnoDB: Warning: io_setup() attempt"
3150
 
                                " %lu failed.\n",
3151
 
                                retries);
3152
 
                        os_thread_sleep(OS_AIO_IO_SETUP_RETRY_SLEEP);
3153
 
                        goto retry;
3154
 
                }
3155
 
 
3156
 
                /* Have tried enough. Better call it a day. */
3157
 
                ut_print_timestamp(stderr);
3158
 
                fprintf(stderr,
3159
 
                        "  InnoDB: Error: io_setup() failed"
3160
 
                        " with EAGAIN after %d attempts.\n",
3161
 
                        OS_AIO_IO_SETUP_RETRY_ATTEMPTS);
3162
 
                break;
3163
 
 
3164
 
        case -ENOSYS:
3165
 
                ut_print_timestamp(stderr);
3166
 
                fprintf(stderr,
3167
 
                        "  InnoDB: Error: Linux Native AIO interface"
3168
 
                        " is not supported on this platform. Please"
3169
 
                        " check your OS documentation and install"
3170
 
                        " appropriate binary of InnoDB.\n");
3171
 
 
3172
 
                break;
3173
 
 
3174
 
        default:
3175
 
                ut_print_timestamp(stderr);
3176
 
                fprintf(stderr,
3177
 
                        "  InnoDB: Error: Linux Native AIO setup"
3178
 
                        " returned following error[%d]\n", -ret);
3179
 
                break;
3180
 
        }
3181
 
 
3182
 
        fprintf(stderr,
3183
 
                "InnoDB: You can disable Linux Native AIO by"
3184
 
                " setting innodb_native_aio = off in my.cnf\n");
3185
 
        return(FALSE);
3186
 
}
3187
 
#endif /* LINUX_NATIVE_AIO */
3188
 
 
3189
 
/******************************************************************//**
3190
 
Creates an aio wait array. Note that we return NULL in case of failure.
3191
 
We don't care about freeing memory here because we assume that a
3192
 
failure will result in server refusing to start up.
3193
 
@return own: aio array, NULL on failure */
 
2874
/****************************************************************************
 
2875
Creates an aio wait array. */
3194
2876
static
3195
2877
os_aio_array_t*
3196
2878
os_aio_array_create(
3197
2879
/*================*/
3198
 
        ulint   n,              /*!< in: maximum number of pending aio
3199
 
                                operations allowed; n must be
3200
 
                                divisible by n_segments */
3201
 
        ulint   n_segments)     /*!< in: number of segments in the aio array */
 
2880
                                /* out, own: aio array */
 
2881
        ulint   n,              /* in: maximum number of pending aio operations
 
2882
                                allowed; n must be divisible by n_segments */
 
2883
        ulint   n_segments)     /* in: number of segments in the aio array */
3202
2884
{
3203
2885
        os_aio_array_t* array;
3204
2886
        ulint           i;
3205
2887
        os_aio_slot_t*  slot;
3206
2888
#ifdef WIN_ASYNC_IO
3207
2889
        OVERLAPPED*     over;
3208
 
#elif defined(LINUX_NATIVE_AIO)
3209
 
        struct io_event*        io_event = NULL;
3210
2890
#endif
3211
2891
        ut_a(n > 0);
3212
2892
        ut_a(n_segments > 0);
3213
2893
 
3214
2894
        array = ut_malloc(sizeof(os_aio_array_t));
3215
2895
 
3216
 
        array->mutex            = os_mutex_create();
 
2896
        array->mutex            = os_mutex_create(NULL);
3217
2897
        array->not_full         = os_event_create(NULL);
3218
2898
        array->is_empty         = os_event_create(NULL);
3219
2899
 
3222
2902
        array->n_slots          = n;
3223
2903
        array->n_segments       = n_segments;
3224
2904
        array->n_reserved       = 0;
3225
 
        array->cur_seg          = 0;
3226
2905
        array->slots            = ut_malloc(n * sizeof(os_aio_slot_t));
3227
2906
#ifdef __WIN__
3228
 
        array->handles          = ut_malloc(n * sizeof(HANDLE));
 
2907
        array->native_events    = ut_malloc(n * sizeof(os_native_event_t));
3229
2908
#endif
3230
 
 
3231
 
#if defined(LINUX_NATIVE_AIO)
3232
 
        array->aio_ctx = NULL;
3233
 
        array->aio_events = NULL;
3234
 
 
3235
 
        /* If we are not using native aio interface then skip this
3236
 
        part of initialization. */
3237
 
        if (!srv_use_native_aio) {
3238
 
                goto skip_native_aio;
3239
 
        }
3240
 
 
3241
 
        /* Initialize the io_context array. One io_context
3242
 
        per segment in the array. */
3243
 
 
3244
 
        array->aio_ctx = ut_malloc(n_segments *
3245
 
                                   sizeof(*array->aio_ctx));
3246
 
        for (i = 0; i < n_segments; ++i) {
3247
 
                if (!os_aio_linux_create_io_ctx(n/n_segments,
3248
 
                                           &array->aio_ctx[i])) {
3249
 
                        /* If something bad happened during aio setup
3250
 
                        we should call it a day and return right away.
3251
 
                        We don't care about any leaks because a failure
3252
 
                        to initialize the io subsystem means that the
3253
 
                        server (or atleast the innodb storage engine)
3254
 
                        is not going to startup. */
3255
 
                        return(NULL);
3256
 
                }
3257
 
        }
3258
 
 
3259
 
        /* Initialize the event array. One event per slot. */
3260
 
        io_event = ut_malloc(n * sizeof(*io_event));
3261
 
        memset(io_event, 0x0, sizeof(*io_event) * n);
3262
 
        array->aio_events = io_event;
3263
 
 
3264
 
skip_native_aio:
3265
 
#endif /* LINUX_NATIVE_AIO */
3266
2909
        for (i = 0; i < n; i++) {
3267
2910
                slot = os_aio_array_get_nth_slot(array, i);
3268
2911
 
3269
2912
                slot->pos = i;
3270
2913
                slot->reserved = FALSE;
3271
2914
#ifdef WIN_ASYNC_IO
3272
 
                slot->handle = CreateEvent(NULL,TRUE, FALSE, NULL);
 
2915
                slot->event = os_event_create(NULL);
3273
2916
 
3274
2917
                over = &(slot->control);
3275
2918
 
3276
 
                over->hEvent = slot->handle;
3277
 
 
3278
 
                *((array->handles) + i) = over->hEvent;
3279
 
 
3280
 
#elif defined(LINUX_NATIVE_AIO)
3281
 
 
3282
 
                memset(&slot->control, 0x0, sizeof(slot->control));
3283
 
                slot->n_bytes = 0;
3284
 
                slot->ret = 0;
 
2919
                over->hEvent = slot->event->handle;
 
2920
 
 
2921
                *((array->native_events) + i) = over->hEvent;
3285
2922
#endif
3286
2923
        }
3287
2924
 
3288
2925
        return(array);
3289
2926
}
3290
2927
 
3291
 
/************************************************************************//**
3292
 
Frees an aio wait array. */
3293
 
static
 
2928
/****************************************************************************
 
2929
Initializes the asynchronous io system. Calls also os_io_init_simple.
 
2930
Creates a separate aio array for
 
2931
non-ibuf read and write, a third aio array for the ibuf i/o, with just one
 
2932
segment, two aio arrays for log reads and writes with one segment, and a
 
2933
synchronous aio array of the specified size. The combined number of segments
 
2934
in the three first aio arrays is the parameter n_segments given to the
 
2935
function. The caller must create an i/o handler thread for each segment in
 
2936
the four first arrays, but not for the sync aio array. */
 
2937
UNIV_INTERN
3294
2938
void
3295
 
os_aio_array_free(
3296
 
/*==============*/
3297
 
        os_aio_array_t* array)  /*!< in, own: array to free */
3298
 
{
3299
 
#ifdef WIN_ASYNC_IO
3300
 
        ulint   i;
3301
 
 
3302
 
        for (i = 0; i < array->n_slots; i++) {
3303
 
                os_aio_slot_t*  slot = os_aio_array_get_nth_slot(array, i);
3304
 
                CloseHandle(slot->handle);
3305
 
        }
3306
 
#endif /* WIN_ASYNC_IO */
3307
 
 
3308
 
#ifdef __WIN__
3309
 
        ut_free(array->handles);
3310
 
#endif /* __WIN__ */
3311
 
        os_mutex_free(array->mutex);
3312
 
        os_event_free(array->not_full);
3313
 
        os_event_free(array->is_empty);
3314
 
 
3315
 
#if defined(LINUX_NATIVE_AIO)
3316
 
        if (srv_use_native_aio) {
3317
 
                ut_free(array->aio_events);
3318
 
                ut_free(array->aio_ctx);
3319
 
        }
3320
 
#endif /* LINUX_NATIVE_AIO */
3321
 
 
3322
 
        ut_free(array->slots);
3323
 
        ut_free(array);
3324
 
}
3325
 
 
3326
 
/***********************************************************************
3327
 
Initializes the asynchronous io system. Creates one array each for ibuf
3328
 
and log i/o. Also creates one array each for read and write where each
3329
 
array is divided logically into n_read_segs and n_write_segs
3330
 
respectively. The caller must create an i/o handler thread for each
3331
 
segment in these arrays. This function also creates the sync array.
3332
 
No i/o handler thread needs to be created for that */
3333
 
UNIV_INTERN
3334
 
ibool
3335
2939
os_aio_init(
3336
2940
/*========*/
3337
 
        ulint   n_per_seg,      /*<! in: maximum number of pending aio
3338
 
                                operations allowed per segment */
3339
 
        ulint   n_read_segs,    /*<! in: number of reader threads */
3340
 
        ulint   n_write_segs,   /*<! in: number of writer threads */
3341
 
        ulint   n_slots_sync)   /*<! in: number of slots in the sync aio
3342
 
                                array */
 
2941
        ulint   n,              /* in: maximum number of pending aio operations
 
2942
                                allowed; n must be divisible by n_segments */
 
2943
        ulint   n_segments,     /* in: combined number of segments in the four
 
2944
                                first aio arrays; must be >= 4 */
 
2945
        ulint   n_slots_sync)   /* in: number of slots in the sync aio array */
3343
2946
{
 
2947
        ulint   n_read_segs;
 
2948
        ulint   n_write_segs;
 
2949
        ulint   n_per_seg;
3344
2950
        ulint   i;
3345
 
        ulint   n_segments = 2 + n_read_segs + n_write_segs;
3346
2951
 
 
2952
        ut_ad(n % n_segments == 0);
3347
2953
        ut_ad(n_segments >= 4);
3348
2954
 
3349
2955
        os_io_init_simple();
3352
2958
                srv_set_io_thread_op_info(i, "not started yet");
3353
2959
        }
3354
2960
 
 
2961
        n_per_seg = n / n_segments;
 
2962
        n_write_segs = (n_segments - 2) / 2;
 
2963
        n_read_segs = n_segments - 2 - n_write_segs;
3355
2964
 
3356
2965
        /* fprintf(stderr, "Array n per seg %lu\n", n_per_seg); */
3357
2966
 
3358
2967
        os_aio_ibuf_array = os_aio_array_create(n_per_seg, 1);
3359
 
        if (os_aio_ibuf_array == NULL) {
3360
 
                goto err_exit;
3361
 
        }
3362
2968
 
3363
2969
        srv_io_thread_function[0] = "insert buffer thread";
3364
2970
 
3365
2971
        os_aio_log_array = os_aio_array_create(n_per_seg, 1);
3366
 
        if (os_aio_log_array == NULL) {
3367
 
                goto err_exit;
3368
 
        }
3369
2972
 
3370
2973
        srv_io_thread_function[1] = "log thread";
3371
2974
 
3372
2975
        os_aio_read_array = os_aio_array_create(n_read_segs * n_per_seg,
3373
2976
                                                n_read_segs);
3374
 
        if (os_aio_read_array == NULL) {
3375
 
                goto err_exit;
3376
 
        }
3377
 
 
3378
2977
        for (i = 2; i < 2 + n_read_segs; i++) {
3379
2978
                ut_a(i < SRV_MAX_N_IO_THREADS);
3380
2979
                srv_io_thread_function[i] = "read thread";
3382
2981
 
3383
2982
        os_aio_write_array = os_aio_array_create(n_write_segs * n_per_seg,
3384
2983
                                                 n_write_segs);
3385
 
        if (os_aio_write_array == NULL) {
3386
 
                goto err_exit;
3387
 
        }
3388
 
 
3389
2984
        for (i = 2 + n_read_segs; i < n_segments; i++) {
3390
2985
                ut_a(i < SRV_MAX_N_IO_THREADS);
3391
2986
                srv_io_thread_function[i] = "write thread";
3392
2987
        }
3393
2988
 
3394
2989
        os_aio_sync_array = os_aio_array_create(n_slots_sync, 1);
3395
 
        if (os_aio_sync_array == NULL) {
3396
 
                goto err_exit;
3397
 
        }
3398
 
 
3399
2990
 
3400
2991
        os_aio_n_segments = n_segments;
3401
2992
 
3409
3000
 
3410
3001
        os_last_printout = time(NULL);
3411
3002
 
3412
 
        return(TRUE);
3413
 
 
3414
 
err_exit:
3415
 
        return(FALSE);
3416
 
 
3417
 
}
3418
 
 
3419
 
/***********************************************************************
3420
 
Frees the asynchronous io system. */
3421
 
UNIV_INTERN
3422
 
void
3423
 
os_aio_free(void)
3424
 
/*=============*/
3425
 
{
3426
 
        ulint   i;
3427
 
 
3428
 
        os_aio_array_free(os_aio_ibuf_array);
3429
 
        os_aio_ibuf_array = NULL;
3430
 
        os_aio_array_free(os_aio_log_array);
3431
 
        os_aio_log_array = NULL;
3432
 
        os_aio_array_free(os_aio_read_array);
3433
 
        os_aio_read_array = NULL;
3434
 
        os_aio_array_free(os_aio_write_array);
3435
 
        os_aio_write_array = NULL;
3436
 
        os_aio_array_free(os_aio_sync_array);
3437
 
        os_aio_sync_array = NULL;
3438
 
 
3439
 
        for (i = 0; i < os_aio_n_segments; i++) {
3440
 
                os_event_free(os_aio_segment_wait_events[i]);
3441
 
        }
3442
 
 
3443
 
        ut_free(os_aio_segment_wait_events);
3444
 
        os_aio_segment_wait_events = 0;
3445
 
        os_aio_n_segments = 0;
3446
3003
}
3447
3004
 
3448
3005
#ifdef WIN_ASYNC_IO
3449
 
/************************************************************************//**
 
3006
/****************************************************************************
3450
3007
Wakes up all async i/o threads in the array in Windows async i/o at
3451
3008
shutdown. */
3452
3009
static
3453
3010
void
3454
3011
os_aio_array_wake_win_aio_at_shutdown(
3455
3012
/*==================================*/
3456
 
        os_aio_array_t* array)  /*!< in: aio array */
 
3013
        os_aio_array_t* array)  /* in: aio array */
3457
3014
{
3458
3015
        ulint   i;
3459
3016
 
3460
3017
        for (i = 0; i < array->n_slots; i++) {
3461
3018
 
3462
 
                SetEvent((array->slots + i)->handle);
 
3019
                os_event_set((array->slots + i)->event);
3463
3020
        }
3464
3021
}
3465
3022
#endif
3466
3023
 
3467
 
/************************************************************************//**
 
3024
/****************************************************************************
3468
3025
Wakes up all async i/o threads so that they know to exit themselves in
3469
3026
shutdown. */
3470
3027
UNIV_INTERN
3480
3037
        os_aio_array_wake_win_aio_at_shutdown(os_aio_write_array);
3481
3038
        os_aio_array_wake_win_aio_at_shutdown(os_aio_ibuf_array);
3482
3039
        os_aio_array_wake_win_aio_at_shutdown(os_aio_log_array);
3483
 
 
3484
 
#elif defined(LINUX_NATIVE_AIO)
3485
 
 
3486
 
        /* When using native AIO interface the io helper threads
3487
 
        wait on io_getevents with a timeout value of 500ms. At
3488
 
        each wake up these threads check the server status.
3489
 
        No need to do anything to wake them up. */
3490
 
 
3491
 
        if (srv_use_native_aio) {
3492
 
                return;
3493
 
        }
3494
 
        /* Fall through to simulated AIO handler wakeup if we are
3495
 
        not using native AIO. */
3496
3040
#endif
3497
3041
        /* This loop wakes up all simulated ai/o threads */
3498
3042
 
3502
3046
        }
3503
3047
}
3504
3048
 
3505
 
/************************************************************************//**
 
3049
/****************************************************************************
3506
3050
Waits until there are no pending writes in os_aio_write_array. There can
3507
3051
be other, synchronous, pending writes. */
3508
3052
UNIV_INTERN
3513
3057
        os_event_wait(os_aio_write_array->is_empty);
3514
3058
}
3515
3059
 
3516
 
/**********************************************************************//**
3517
 
Calculates segment number for a slot.
3518
 
@return segment number (which is the number used by, for example,
3519
 
i/o-handler threads) */
 
3060
/**************************************************************************
 
3061
Calculates segment number for a slot. */
3520
3062
static
3521
3063
ulint
3522
3064
os_aio_get_segment_no_from_slot(
3523
3065
/*============================*/
3524
 
        os_aio_array_t* array,  /*!< in: aio wait array */
3525
 
        os_aio_slot_t*  slot)   /*!< in: slot in this array */
 
3066
                                /* out: segment number (which is the number
 
3067
                                used by, for example, i/o-handler threads) */
 
3068
        os_aio_array_t* array,  /* in: aio wait array */
 
3069
        os_aio_slot_t*  slot)   /* in: slot in this array */
3526
3070
{
3527
3071
        ulint   segment;
3528
3072
        ulint   seg_len;
3550
3094
        return(segment);
3551
3095
}
3552
3096
 
3553
 
/**********************************************************************//**
3554
 
Calculates local segment number and aio array from global segment number.
3555
 
@return local segment number within the aio array */
 
3097
/**************************************************************************
 
3098
Calculates local segment number and aio array from global segment number. */
3556
3099
static
3557
3100
ulint
3558
3101
os_aio_get_array_and_local_segment(
3559
3102
/*===============================*/
3560
 
        os_aio_array_t** array,         /*!< out: aio wait array */
3561
 
        ulint            global_segment)/*!< in: global segment number */
 
3103
                                        /* out: local segment number within
 
3104
                                        the aio array */
 
3105
        os_aio_array_t** array,         /* out: aio wait array */
 
3106
        ulint            global_segment)/* in: global segment number */
3562
3107
{
3563
3108
        ulint   segment;
3564
3109
 
3585
3130
        return(segment);
3586
3131
}
3587
3132
 
3588
 
/*******************************************************************//**
 
3133
/***********************************************************************
3589
3134
Requests for a slot in the aio array. If no slot is available, waits until
3590
 
not_full-event becomes signaled.
3591
 
@return pointer to slot */
 
3135
not_full-event becomes signaled. */
3592
3136
static
3593
3137
os_aio_slot_t*
3594
3138
os_aio_array_reserve_slot(
3595
3139
/*======================*/
3596
 
        ulint           type,   /*!< in: OS_FILE_READ or OS_FILE_WRITE */
3597
 
        os_aio_array_t* array,  /*!< in: aio array */
3598
 
        fil_node_t*     message1,/*!< in: message to be passed along with
3599
 
                                the aio operation */
3600
 
        void*           message2,/*!< in: message to be passed along with
3601
 
                                the aio operation */
3602
 
        os_file_t       file,   /*!< in: file handle */
3603
 
        const char*     name,   /*!< in: name of the file or path as a
 
3140
                                /* out: pointer to slot */
 
3141
        ulint           type,   /* in: OS_FILE_READ or OS_FILE_WRITE */
 
3142
        os_aio_array_t* array,  /* in: aio array */
 
3143
        fil_node_t*     message1,/* in: message to be passed along with
 
3144
                                the aio operation */
 
3145
        void*           message2,/* in: message to be passed along with
 
3146
                                the aio operation */
 
3147
        os_file_t       file,   /* in: file handle */
 
3148
        const char*     name,   /* in: name of the file or path as a
3604
3149
                                null-terminated string */
3605
 
        void*           buf,    /*!< in: buffer where to read or from which
 
3150
        void*           buf,    /* in: buffer where to read or from which
3606
3151
                                to write */
3607
 
        ulint           offset, /*!< in: least significant 32 bits of file
3608
 
                                offset */
3609
 
        ulint           offset_high, /*!< in: most significant 32 bits of
3610
 
                                offset */
3611
 
        ulint           len)    /*!< in: length of the block to read or write */
 
3152
        ulint           offset, /* in: least significant 32 bits of file
 
3153
                                offset */
 
3154
        ulint           offset_high, /* in: most significant 32 bits of
 
3155
                                offset */
 
3156
        ulint           len)    /* in: length of the block to read or write */
3612
3157
{
3613
 
        os_aio_slot_t*  slot = NULL;
 
3158
        os_aio_slot_t*  slot;
3614
3159
#ifdef WIN_ASYNC_IO
3615
3160
        OVERLAPPED*     control;
3616
 
 
3617
 
#elif defined(LINUX_NATIVE_AIO)
3618
 
 
3619
 
        struct iocb*    iocb;
3620
 
        off_t           aio_offset;
3621
 
 
3622
3161
#endif
3623
3162
        ulint           i;
3624
 
        ulint           counter;
3625
 
        ulint           slots_per_seg;
3626
 
        ulint           local_seg;
3627
 
 
3628
 
        /* No need of a mutex. Only reading constant fields */
3629
 
        slots_per_seg = array->n_slots / array->n_segments;
3630
 
 
3631
 
        /* We attempt to keep adjacent blocks in the same local
3632
 
        segment. This can help in merging IO requests when we are
3633
 
        doing simulated AIO */
3634
 
        local_seg = (offset >> (UNIV_PAGE_SIZE_SHIFT + 6))
3635
 
                    % array->n_segments;
3636
 
 
3637
3163
loop:
3638
3164
        os_mutex_enter(array->mutex);
3639
3165
 
3640
3166
        if (array->n_reserved == array->n_slots) {
3641
3167
                os_mutex_exit(array->mutex);
3642
3168
 
3643
 
                if (!srv_use_native_aio) {
 
3169
                if (!os_aio_use_native_aio) {
3644
3170
                        /* If the handler threads are suspended, wake them
3645
3171
                        so that we get more slots */
3646
3172
 
3652
3178
                goto loop;
3653
3179
        }
3654
3180
 
3655
 
        /* We start our search for an available slot from our preferred
3656
 
        local segment and do a full scan of the array. We are
3657
 
        guaranteed to find a slot in full scan. */
3658
 
        for (i = local_seg * slots_per_seg, counter = 0;
3659
 
             counter < array->n_slots; i++, counter++) {
3660
 
 
3661
 
                i %= array->n_slots;
 
3181
        for (i = 0;; i++) {
3662
3182
                slot = os_aio_array_get_nth_slot(array, i);
3663
3183
 
3664
3184
                if (slot->reserved == FALSE) {
3665
 
                        goto found;
 
3185
                        break;
3666
3186
                }
3667
3187
        }
3668
3188
 
3669
 
        /* We MUST always be able to get hold of a reserved slot. */
3670
 
        ut_error;
3671
 
 
3672
 
found:
3673
 
        ut_a(slot->reserved == FALSE);
3674
3189
        array->n_reserved++;
3675
3190
 
3676
3191
        if (array->n_reserved == 1) {
3698
3213
        control = &(slot->control);
3699
3214
        control->Offset = (DWORD)offset;
3700
3215
        control->OffsetHigh = (DWORD)offset_high;
3701
 
        ResetEvent(slot->handle);
3702
 
 
3703
 
#elif defined(LINUX_NATIVE_AIO)
3704
 
 
3705
 
        /* If we are not using native AIO skip this part. */
3706
 
        if (!srv_use_native_aio) {
3707
 
                goto skip_native_aio;
3708
 
        }
3709
 
 
3710
 
        /* Check if we are dealing with 64 bit arch.
3711
 
        If not then make sure that offset fits in 32 bits. */
3712
 
        if (sizeof(aio_offset) == 8) {
3713
 
                aio_offset = offset_high;
3714
 
                aio_offset <<= 32;
3715
 
                aio_offset += offset;
3716
 
        } else {
3717
 
                ut_a(offset_high == 0);
3718
 
                aio_offset = offset;
3719
 
        }
3720
 
 
3721
 
        iocb = &slot->control;
3722
 
 
3723
 
        if (type == OS_FILE_READ) {
3724
 
                io_prep_pread(iocb, file, buf, len, aio_offset);
3725
 
        } else {
3726
 
                ut_a(type == OS_FILE_WRITE);
3727
 
                io_prep_pwrite(iocb, file, buf, len, aio_offset);
3728
 
        }
3729
 
 
3730
 
        iocb->data = (void*)slot;
3731
 
        slot->n_bytes = 0;
3732
 
        slot->ret = 0;
3733
 
        /*fprintf(stderr, "Filled up Linux native iocb.\n");*/
3734
 
        
3735
 
 
3736
 
skip_native_aio:
3737
 
#endif /* LINUX_NATIVE_AIO */
 
3216
        os_event_reset(slot->event);
 
3217
#endif
 
3218
 
3738
3219
        os_mutex_exit(array->mutex);
3739
3220
 
3740
3221
        return(slot);
3741
3222
}
3742
3223
 
3743
 
/*******************************************************************//**
 
3224
/***********************************************************************
3744
3225
Frees a slot in the aio array. */
3745
3226
static
3746
3227
void
3747
3228
os_aio_array_free_slot(
3748
3229
/*===================*/
3749
 
        os_aio_array_t* array,  /*!< in: aio array */
3750
 
        os_aio_slot_t*  slot)   /*!< in: pointer to slot */
 
3230
        os_aio_array_t* array,  /* in: aio array */
 
3231
        os_aio_slot_t*  slot)   /* in: pointer to slot */
3751
3232
{
3752
3233
        ut_ad(array);
3753
3234
        ut_ad(slot);
3769
3250
        }
3770
3251
 
3771
3252
#ifdef WIN_ASYNC_IO
3772
 
 
3773
 
        ResetEvent(slot->handle);
3774
 
 
3775
 
#elif defined(LINUX_NATIVE_AIO)
3776
 
 
3777
 
        if (srv_use_native_aio) {
3778
 
                memset(&slot->control, 0x0, sizeof(slot->control));
3779
 
                slot->n_bytes = 0;
3780
 
                slot->ret = 0;
3781
 
                /*fprintf(stderr, "Freed up Linux native slot.\n");*/
3782
 
        } else {
3783
 
                /* These fields should not be used if we are not
3784
 
                using native AIO. */
3785
 
                ut_ad(slot->n_bytes == 0);
3786
 
                ut_ad(slot->ret == 0);
3787
 
        }
3788
 
 
 
3253
        os_event_reset(slot->event);
3789
3254
#endif
3790
3255
        os_mutex_exit(array->mutex);
3791
3256
}
3792
3257
 
3793
 
/**********************************************************************//**
 
3258
/**************************************************************************
3794
3259
Wakes up a simulated aio i/o-handler thread if it has something to do. */
3795
3260
static
3796
3261
void
3797
3262
os_aio_simulated_wake_handler_thread(
3798
3263
/*=================================*/
3799
 
        ulint   global_segment) /*!< in: the number of the segment in the aio
 
3264
        ulint   global_segment) /* in: the number of the segment in the aio
3800
3265
                                arrays */
3801
3266
{
3802
3267
        os_aio_array_t* array;
3805
3270
        ulint           n;
3806
3271
        ulint           i;
3807
3272
 
3808
 
        ut_ad(!srv_use_native_aio);
 
3273
        ut_ad(!os_aio_use_native_aio);
3809
3274
 
3810
3275
        segment = os_aio_get_array_and_local_segment(&array, global_segment);
3811
3276
 
3832
3297
        }
3833
3298
}
3834
3299
 
3835
 
/**********************************************************************//**
 
3300
/**************************************************************************
3836
3301
Wakes up simulated aio i/o-handler threads if they have something to do. */
3837
3302
UNIV_INTERN
3838
3303
void
3841
3306
{
3842
3307
        ulint   i;
3843
3308
 
3844
 
        if (srv_use_native_aio) {
 
3309
        if (os_aio_use_native_aio) {
3845
3310
                /* We do not use simulated aio: do nothing */
3846
3311
 
3847
3312
                return;
3854
3319
        }
3855
3320
}
3856
3321
 
3857
 
/**********************************************************************//**
 
3322
/**************************************************************************
3858
3323
This function can be called if one wants to post a batch of reads and
3859
3324
prefers an i/o-handler thread to handle them all at once later. You must
3860
3325
call os_aio_simulated_wake_handler_threads later to ensure the threads
3864
3329
os_aio_simulated_put_read_threads_to_sleep(void)
3865
3330
/*============================================*/
3866
3331
{
3867
 
 
3868
 
/* The idea of putting background IO threads to sleep is only for
3869
 
Windows when using simulated AIO. Windows XP seems to schedule
3870
 
background threads too eagerly to allow for coalescing during
3871
 
readahead requests. */
3872
 
#ifdef __WIN__
3873
3332
        os_aio_array_t* array;
3874
3333
        ulint           g;
3875
3334
 
3876
 
        if (srv_use_native_aio) {
3877
 
                /* We do not use simulated aio: do nothing */
3878
 
 
3879
 
                return;
3880
 
        }
3881
 
 
3882
3335
        os_aio_recommend_sleep_for_read_threads = TRUE;
3883
3336
 
3884
3337
        for (g = 0; g < os_aio_n_segments; g++) {
3889
3342
                        os_event_reset(os_aio_segment_wait_events[g]);
3890
3343
                }
3891
3344
        }
3892
 
#endif /* __WIN__ */
3893
 
}
3894
 
 
3895
 
#if defined(LINUX_NATIVE_AIO)
3896
 
/*******************************************************************//**
3897
 
Dispatch an AIO request to the kernel.
3898
 
@return TRUE on success. */
3899
 
static
3900
 
ibool
3901
 
os_aio_linux_dispatch(
3902
 
/*==================*/
3903
 
        os_aio_array_t* array,  /*!< in: io request array. */
3904
 
        os_aio_slot_t*  slot)   /*!< in: an already reserved slot. */
3905
 
{
3906
 
        int             ret;
3907
 
        ulint           io_ctx_index;
3908
 
        struct iocb*    iocb;
3909
 
 
3910
 
        ut_ad(slot != NULL);
3911
 
        ut_ad(array);
3912
 
 
3913
 
        ut_a(slot->reserved);
3914
 
 
3915
 
        /* Find out what we are going to work with.
3916
 
        The iocb struct is directly in the slot.
3917
 
        The io_context is one per segment. */
3918
 
 
3919
 
        iocb = &slot->control;
3920
 
        io_ctx_index = (slot->pos * array->n_segments) / array->n_slots;
3921
 
 
3922
 
        ret = io_submit(array->aio_ctx[io_ctx_index], 1, &iocb);
3923
 
 
3924
 
#if defined(UNIV_AIO_DEBUG)
3925
 
        fprintf(stderr,
3926
 
                "io_submit[%c] ret[%d]: slot[%p] ctx[%p] seg[%lu]\n",
3927
 
                (slot->type == OS_FILE_WRITE) ? 'w' : 'r', ret, slot,
3928
 
                array->aio_ctx[io_ctx_index], (ulong)io_ctx_index);
3929
 
#endif
3930
 
 
3931
 
        /* io_submit returns number of successfully
3932
 
        queued requests or -errno. */
3933
 
        if (UNIV_UNLIKELY(ret != 1)) {
3934
 
                errno = -ret;
3935
 
                return(FALSE);
3936
 
        }
3937
 
 
3938
 
        return(TRUE);
3939
 
}
3940
 
#endif /* LINUX_NATIVE_AIO */
3941
 
 
3942
 
 
3943
 
/*******************************************************************//**
3944
 
NOTE! Use the corresponding macro os_aio(), not directly this function!
3945
 
Requests an asynchronous i/o operation.
3946
 
@return TRUE if request was queued successfully, FALSE if fail */
 
3345
}
 
3346
 
 
3347
/***********************************************************************
 
3348
Requests an asynchronous i/o operation. */
3947
3349
UNIV_INTERN
3948
3350
ibool
3949
 
os_aio_func(
3950
 
/*========*/
3951
 
        ulint           type,   /*!< in: OS_FILE_READ or OS_FILE_WRITE */
3952
 
        ulint           mode,   /*!< in: OS_AIO_NORMAL, ..., possibly ORed
 
3351
os_aio(
 
3352
/*===*/
 
3353
                                /* out: TRUE if request was queued
 
3354
                                successfully, FALSE if fail */
 
3355
        ulint           type,   /* in: OS_FILE_READ or OS_FILE_WRITE */
 
3356
        ulint           mode,   /* in: OS_AIO_NORMAL, ..., possibly ORed
3953
3357
                                to OS_AIO_SIMULATED_WAKE_LATER: the
3954
3358
                                last flag advises this function not to wake
3955
3359
                                i/o-handler threads, but the caller will
3962
3366
                                because i/os are not actually handled until
3963
3367
                                all have been posted: use with great
3964
3368
                                caution! */
3965
 
        const char*     name,   /*!< in: name of the file or path as a
 
3369
        const char*     name,   /* in: name of the file or path as a
3966
3370
                                null-terminated string */
3967
 
        os_file_t       file,   /*!< in: handle to a file */
3968
 
        void*           buf,    /*!< in: buffer where to read or from which
 
3371
        os_file_t       file,   /* in: handle to a file */
 
3372
        void*           buf,    /* in: buffer where to read or from which
3969
3373
                                to write */
3970
 
        ulint           offset, /*!< in: least significant 32 bits of file
 
3374
        ulint           offset, /* in: least significant 32 bits of file
3971
3375
                                offset where to read or write */
3972
 
        ulint           offset_high, /*!< in: most significant 32 bits of
 
3376
        ulint           offset_high, /* in: most significant 32 bits of
3973
3377
                                offset */
3974
 
        ulint           n,      /*!< in: number of bytes to read or write */
3975
 
        fil_node_t*     message1,/*!< in: message for the aio handler
3976
 
                                (can be used to identify a completed
3977
 
                                aio operation); ignored if mode is
3978
 
                                OS_AIO_SYNC */
3979
 
        void*           message2)/*!< in: message for the aio handler
3980
 
                                (can be used to identify a completed
3981
 
                                aio operation); ignored if mode is
3982
 
                                OS_AIO_SYNC */
 
3378
        ulint           n,      /* in: number of bytes to read or write */
 
3379
        fil_node_t*     message1,/* in: messages for the aio handler (these
 
3380
                                can be used to identify a completed aio
 
3381
                                operation); if mode is OS_AIO_SYNC, these
 
3382
                                are ignored */
 
3383
        void*           message2)
3983
3384
{
3984
3385
        os_aio_array_t* array;
3985
3386
        os_aio_slot_t*  slot;
3990
3391
        struct fil_node_struct * dummy_mess1;
3991
3392
        void*           dummy_mess2;
3992
3393
        ulint           dummy_type;
3993
 
#endif /* WIN_ASYNC_IO */
3994
 
#if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO
 
3394
#endif
 
3395
        ulint           err             = 0;
3995
3396
        ibool           retry;
3996
 
#endif
3997
3397
        ulint           wake_later;
3998
3398
 
3999
3399
        ut_ad(file);
4008
3408
 
4009
3409
        if (mode == OS_AIO_SYNC
4010
3410
#ifdef WIN_ASYNC_IO
4011
 
            && !srv_use_native_aio
4012
 
#endif /* WIN_ASYNC_IO */
 
3411
            && !os_aio_use_native_aio
 
3412
#endif
4013
3413
            ) {
4014
3414
                /* This is actually an ordinary synchronous read or write:
4015
3415
                no need to use an i/o-handler thread. NOTE that if we use
4028
3428
                return(os_file_write(name, file, buf, offset, offset_high, n));
4029
3429
        }
4030
3430
 
4031
 
#if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO
4032
3431
try_again:
4033
 
#endif
4034
3432
        if (mode == OS_AIO_NORMAL) {
4035
3433
                if (type == OS_FILE_READ) {
4036
3434
                        array = os_aio_read_array;
4050
3448
                array = os_aio_log_array;
4051
3449
        } else if (mode == OS_AIO_SYNC) {
4052
3450
                array = os_aio_sync_array;
4053
 
 
4054
 
#if defined(LINUX_NATIVE_AIO)
4055
 
                /* In Linux native AIO we don't use sync IO array. */
4056
 
                ut_a(!srv_use_native_aio);
4057
 
#endif /* LINUX_NATIVE_AIO */
4058
3451
        } else {
4059
3452
                array = NULL; /* Eliminate compiler warning */
4060
3453
                ut_error;
4063
3456
        slot = os_aio_array_reserve_slot(type, array, message1, message2, file,
4064
3457
                                         name, buf, offset, offset_high, n);
4065
3458
        if (type == OS_FILE_READ) {
4066
 
                if (srv_use_native_aio) {
 
3459
                if (os_aio_use_native_aio) {
 
3460
#ifdef WIN_ASYNC_IO
4067
3461
                        os_n_file_reads++;
4068
 
                        os_bytes_read_since_printout += n;
4069
 
#ifdef WIN_ASYNC_IO
 
3462
                        os_bytes_read_since_printout += len;
 
3463
 
4070
3464
                        ret = ReadFile(file, buf, (DWORD)n, &len,
4071
3465
                                       &(slot->control));
4072
 
 
4073
 
#elif defined(LINUX_NATIVE_AIO)
4074
 
                        if (!os_aio_linux_dispatch(array, slot)) {
4075
 
                                goto err_exit;
4076
 
                        }
4077
3466
#endif
4078
3467
                } else {
4079
3468
                        if (!wake_later) {
4083
3472
                        }
4084
3473
                }
4085
3474
        } else if (type == OS_FILE_WRITE) {
4086
 
                if (srv_use_native_aio) {
 
3475
                if (os_aio_use_native_aio) {
 
3476
#ifdef WIN_ASYNC_IO
4087
3477
                        os_n_file_writes++;
4088
 
#ifdef WIN_ASYNC_IO
4089
3478
                        ret = WriteFile(file, buf, (DWORD)n, &len,
4090
3479
                                        &(slot->control));
4091
 
 
4092
 
#elif defined(LINUX_NATIVE_AIO)
4093
 
                        if (!os_aio_linux_dispatch(array, slot)) {
4094
 
                                goto err_exit;
4095
 
                        }
4096
3480
#endif
4097
3481
                } else {
4098
3482
                        if (!wake_later) {
4106
3490
        }
4107
3491
 
4108
3492
#ifdef WIN_ASYNC_IO
4109
 
        if (srv_use_native_aio) {
 
3493
        if (os_aio_use_native_aio) {
4110
3494
                if ((ret && len == n)
4111
3495
                    || (!ret && GetLastError() == ERROR_IO_PENDING)) {
4112
3496
                        /* aio was queued successfully! */
4129
3513
                        return(TRUE);
4130
3514
                }
4131
3515
 
4132
 
                goto err_exit;
4133
 
        }
4134
 
#endif /* WIN_ASYNC_IO */
4135
 
        /* aio was queued successfully! */
4136
 
        return(TRUE);
4137
 
 
4138
 
#if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO
4139
 
err_exit:
 
3516
                err = 1; /* Fall through the next if */
 
3517
        }
 
3518
#endif
 
3519
        if (err == 0) {
 
3520
                /* aio was queued successfully! */
 
3521
 
 
3522
                return(TRUE);
 
3523
        }
 
3524
 
4140
3525
        os_aio_array_free_slot(array, slot);
4141
3526
 
4142
3527
        retry = os_file_handle_error(name,
4148
3533
        }
4149
3534
 
4150
3535
        return(FALSE);
4151
 
#endif /* LINUX_NATIVE_AIO || WIN_ASYNC_IO */
4152
3536
}
4153
3537
 
4154
3538
#ifdef WIN_ASYNC_IO
4155
 
/**********************************************************************//**
 
3539
/**************************************************************************
4156
3540
This function is only used in Windows asynchronous i/o.
4157
3541
Waits for an aio operation to complete. This function is used to wait the
4158
3542
for completed requests. The aio array of pending requests is divided
4159
3543
into segments. The thread specifies which segment or slot it wants to wait
4160
3544
for. NOTE: this function will also take care of freeing the aio slot,
4161
 
therefore no other thread is allowed to do the freeing!
4162
 
@return TRUE if the aio operation succeeded */
 
3545
therefore no other thread is allowed to do the freeing! */
4163
3546
UNIV_INTERN
4164
3547
ibool
4165
3548
os_aio_windows_handle(
4166
3549
/*==================*/
4167
 
        ulint   segment,        /*!< in: the number of the segment in the aio
 
3550
                                /* out: TRUE if the aio operation succeeded */
 
3551
        ulint   segment,        /* in: the number of the segment in the aio
4168
3552
                                arrays to wait for; segment 0 is the ibuf
4169
3553
                                i/o thread, segment 1 the log i/o thread,
4170
3554
                                then follow the non-ibuf read threads, and as
4172
3556
                                this is ULINT_UNDEFINED, then it means that
4173
3557
                                sync aio is used, and this parameter is
4174
3558
                                ignored */
4175
 
        ulint   pos,            /*!< this parameter is used only in sync aio:
 
3559
        ulint   pos,            /* this parameter is used only in sync aio:
4176
3560
                                wait for the aio slot at this position */
4177
 
        fil_node_t**message1,   /*!< out: the messages passed with the aio
 
3561
        fil_node_t**message1,   /* out: the messages passed with the aio
4178
3562
                                request; note that also in the case where
4179
3563
                                the aio operation failed, these output
4180
3564
                                parameters are valid and can be used to
4181
3565
                                restart the operation, for example */
4182
3566
        void**  message2,
4183
 
        ulint*  type)           /*!< out: OS_FILE_WRITE or ..._READ */
 
3567
        ulint*  type)           /* out: OS_FILE_WRITE or ..._READ */
4184
3568
{
4185
3569
        ulint           orig_seg        = segment;
4186
3570
        os_aio_array_t* array;
4190
3574
        ibool           ret_val;
4191
3575
        BOOL            ret;
4192
3576
        DWORD           len;
4193
 
        BOOL            retry           = FALSE;
4194
3577
 
4195
3578
        if (segment == ULINT_UNDEFINED) {
4196
3579
                array = os_aio_sync_array;
4208
3591
        n = array->n_slots / array->n_segments;
4209
3592
 
4210
3593
        if (array == os_aio_sync_array) {
4211
 
                WaitForSingleObject(
4212
 
                        os_aio_array_get_nth_slot(array, pos)->handle,
4213
 
                        INFINITE);
 
3594
                os_event_wait(os_aio_array_get_nth_slot(array, pos)->event);
4214
3595
                i = pos;
4215
3596
        } else {
4216
3597
                srv_set_io_thread_op_info(orig_seg, "wait Windows aio");
4217
 
                i = WaitForMultipleObjects((DWORD) n,
4218
 
                                           array->handles + segment * n,
4219
 
                                           FALSE,
4220
 
                                           INFINITE);
4221
 
        }
4222
 
 
4223
 
        if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) {
4224
 
                os_thread_exit(NULL);
 
3598
                i = os_event_wait_multiple(n,
 
3599
                                           (array->native_events)
 
3600
                                           + segment * n);
4225
3601
        }
4226
3602
 
4227
3603
        os_mutex_enter(array->mutex);
4248
3624
#ifdef UNIV_DO_FLUSH
4249
3625
                if (slot->type == OS_FILE_WRITE
4250
3626
                    && !os_do_not_call_flush_at_each_write) {
4251
 
                        if (!os_file_flush(slot->file)) {
4252
 
                                ut_error;
4253
 
                        }
 
3627
                        ut_a(TRUE == os_file_flush(slot->file));
4254
3628
                }
4255
3629
#endif /* UNIV_DO_FLUSH */
4256
 
        } else if (os_file_handle_error(slot->name, "Windows aio")) {
4257
 
 
4258
 
                retry = TRUE;
4259
3630
        } else {
 
3631
                os_file_handle_error(slot->name, "Windows aio");
4260
3632
 
4261
3633
                ret_val = FALSE;
4262
3634
        }
4263
3635
 
4264
3636
        os_mutex_exit(array->mutex);
4265
3637
 
4266
 
        if (retry) {
4267
 
                /* retry failed read/write operation synchronously.
4268
 
                No need to hold array->mutex. */
4269
 
 
4270
 
#ifdef UNIV_PFS_IO
4271
 
                /* This read/write does not go through os_file_read
4272
 
                and os_file_write APIs, need to register with
4273
 
                performance schema explicitly here. */
4274
 
                struct PSI_file_locker* locker = NULL;
4275
 
                register_pfs_file_io_begin(locker, slot->file, slot->len,
4276
 
                                           (slot->type == OS_FILE_WRITE)
4277
 
                                                ? PSI_FILE_WRITE
4278
 
                                                : PSI_FILE_READ,
4279
 
                                            __FILE__, __LINE__);
4280
 
#endif
4281
 
 
4282
 
                switch (slot->type) {
4283
 
                case OS_FILE_WRITE:
4284
 
                        ret = WriteFile(slot->file, slot->buf,
4285
 
                                        slot->len, &len,
4286
 
                                        &(slot->control));
4287
 
 
4288
 
                        break;
4289
 
                case OS_FILE_READ:
4290
 
                        ret = ReadFile(slot->file, slot->buf,
4291
 
                                       slot->len, &len,
4292
 
                                       &(slot->control));
4293
 
 
4294
 
                        break;
4295
 
                default:
4296
 
                        ut_error;
4297
 
                }
4298
 
 
4299
 
#ifdef UNIV_PFS_IO
4300
 
                register_pfs_file_io_end(locker, len);
4301
 
#endif
4302
 
 
4303
 
                if (!ret && GetLastError() == ERROR_IO_PENDING) {
4304
 
                        /* aio was queued successfully!
4305
 
                        We want a synchronous i/o operation on a
4306
 
                        file where we also use async i/o: in Windows
4307
 
                        we must use the same wait mechanism as for
4308
 
                        async i/o */
4309
 
 
4310
 
                        ret = GetOverlappedResult(slot->file,
4311
 
                                                  &(slot->control),
4312
 
                                                  &len, TRUE);
4313
 
                }
4314
 
 
4315
 
                ret_val = ret && len == slot->len;
4316
 
        }
4317
 
 
4318
3638
        os_aio_array_free_slot(array, slot);
4319
3639
 
4320
3640
        return(ret_val);
4321
3641
}
4322
3642
#endif
4323
3643
 
4324
 
#if defined(LINUX_NATIVE_AIO)
4325
 
/******************************************************************//**
4326
 
This function is only used in Linux native asynchronous i/o. This is
4327
 
called from within the io-thread. If there are no completed IO requests
4328
 
in the slot array, the thread calls this function to collect more
4329
 
requests from the kernel.
4330
 
The io-thread waits on io_getevents(), which is a blocking call, with
4331
 
a timeout value. Unless the system is very heavy loaded, keeping the
4332
 
io-thread very busy, the io-thread will spend most of its time waiting
4333
 
in this function.
4334
 
The io-thread also exits in this function. It checks server status at
4335
 
each wakeup and that is why we use timed wait in io_getevents(). */
4336
 
static
4337
 
void
4338
 
os_aio_linux_collect(
4339
 
/*=================*/
4340
 
        os_aio_array_t* array,          /*!< in/out: slot array. */
4341
 
        ulint           segment,        /*!< in: local segment no. */
4342
 
        ulint           seg_size)       /*!< in: segment size. */
4343
 
{
4344
 
        int                     i;
4345
 
        int                     ret;
4346
 
        ulint                   start_pos;
4347
 
        ulint                   end_pos;
4348
 
        struct timespec         timeout;
4349
 
        struct io_event*        events;
4350
 
        struct io_context*      io_ctx;
4351
 
 
4352
 
        /* sanity checks. */
4353
 
        ut_ad(array != NULL);
4354
 
        ut_ad(seg_size > 0);
4355
 
        ut_ad(segment < array->n_segments);
4356
 
 
4357
 
        /* Which part of event array we are going to work on. */
4358
 
        events = &array->aio_events[segment * seg_size];
4359
 
 
4360
 
        /* Which io_context we are going to use. */
4361
 
        io_ctx = array->aio_ctx[segment];
4362
 
 
4363
 
        /* Starting point of the segment we will be working on. */
4364
 
        start_pos = segment * seg_size;
4365
 
 
4366
 
        /* End point. */
4367
 
        end_pos = start_pos + seg_size;
4368
 
 
4369
 
retry:
4370
 
 
4371
 
        /* Go down if we are in shutdown mode.
4372
 
        In case of srv_fast_shutdown == 2, there may be pending
4373
 
        IO requests but that should be OK as we essentially treat
4374
 
        that as a crash of InnoDB. */
4375
 
        if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) {
4376
 
                os_thread_exit(NULL);
4377
 
        }
4378
 
 
4379
 
        /* Initialize the events. The timeout value is arbitrary.
4380
 
        We probably need to experiment with it a little. */
4381
 
        memset(events, 0, sizeof(*events) * seg_size);
4382
 
        timeout.tv_sec = 0;
4383
 
        timeout.tv_nsec = OS_AIO_REAP_TIMEOUT;
4384
 
 
4385
 
        ret = io_getevents(io_ctx, 1, seg_size, events, &timeout);
4386
 
 
4387
 
        /* This error handling is for any error in collecting the
4388
 
        IO requests. The errors, if any, for any particular IO
4389
 
        request are simply passed on to the calling routine. */
4390
 
 
4391
 
        /* Not enough resources! Try again. */
4392
 
        if (ret == -EAGAIN) {
4393
 
                goto retry;
4394
 
        }
4395
 
 
4396
 
        /* Interrupted! I have tested the behaviour in case of an
4397
 
        interrupt. If we have some completed IOs available then
4398
 
        the return code will be the number of IOs. We get EINTR only
4399
 
        if there are no completed IOs and we have been interrupted. */
4400
 
        if (ret == -EINTR) {
4401
 
                goto retry;
4402
 
        }
4403
 
 
4404
 
        /* No pending request! Go back and check again. */
4405
 
        if (ret == 0) {
4406
 
                goto retry;
4407
 
        }
4408
 
 
4409
 
        /* All other errors! should cause a trap for now. */
4410
 
        if (UNIV_UNLIKELY(ret < 0)) {
4411
 
                ut_print_timestamp(stderr);
4412
 
                fprintf(stderr,
4413
 
                        "  InnoDB: unexpected ret_code[%d] from"
4414
 
                        " io_getevents()!\n", ret);
4415
 
                ut_error;
4416
 
        }
4417
 
 
4418
 
        ut_a(ret > 0);
4419
 
 
4420
 
        for (i = 0; i < ret; i++) {
4421
 
                os_aio_slot_t*  slot;
4422
 
                struct iocb*    control;
4423
 
 
4424
 
                control = (struct iocb *)events[i].obj;
4425
 
                ut_a(control != NULL);
4426
 
 
4427
 
                slot = (os_aio_slot_t *) control->data;
4428
 
 
4429
 
                /* Some sanity checks. */
4430
 
                ut_a(slot != NULL);
4431
 
                ut_a(slot->reserved);
4432
 
 
4433
 
#if defined(UNIV_AIO_DEBUG)
4434
 
                fprintf(stderr,
4435
 
                        "io_getevents[%c]: slot[%p] ctx[%p]"
4436
 
                        " seg[%lu]\n",
4437
 
                        (slot->type == OS_FILE_WRITE) ? 'w' : 'r',
4438
 
                        slot, io_ctx, segment);
4439
 
#endif
4440
 
 
4441
 
                /* We are not scribbling previous segment. */
4442
 
                ut_a(slot->pos >= start_pos);
4443
 
 
4444
 
                /* We have not overstepped to next segment. */
4445
 
                ut_a(slot->pos < end_pos);
4446
 
 
4447
 
                /* Mark this request as completed. The error handling
4448
 
                will be done in the calling function. */
4449
 
                os_mutex_enter(array->mutex);
4450
 
                slot->n_bytes = events[i].res;
4451
 
                slot->ret = events[i].res2;
4452
 
                slot->io_already_done = TRUE;
4453
 
                os_mutex_exit(array->mutex);
4454
 
        }
4455
 
 
4456
 
        return;
4457
 
}
4458
 
 
4459
 
/**********************************************************************//**
4460
 
This function is only used in Linux native asynchronous i/o.
4461
 
Waits for an aio operation to complete. This function is used to wait for
4462
 
the completed requests. The aio array of pending requests is divided
4463
 
into segments. The thread specifies which segment or slot it wants to wait
4464
 
for. NOTE: this function will also take care of freeing the aio slot,
4465
 
therefore no other thread is allowed to do the freeing!
4466
 
@return TRUE if the IO was successful */
4467
 
UNIV_INTERN
4468
 
ibool
4469
 
os_aio_linux_handle(
4470
 
/*================*/
4471
 
        ulint   global_seg,     /*!< in: segment number in the aio array
4472
 
                                to wait for; segment 0 is the ibuf
4473
 
                                i/o thread, segment 1 is log i/o thread,
4474
 
                                then follow the non-ibuf read threads,
4475
 
                                and the last are the non-ibuf write
4476
 
                                threads. */
4477
 
        fil_node_t**message1,   /*!< out: the messages passed with the */
4478
 
        void**  message2,       /*!< aio request; note that in case the
4479
 
                                aio operation failed, these output
4480
 
                                parameters are valid and can be used to
4481
 
                                restart the operation. */
4482
 
        ulint*  type)           /*!< out: OS_FILE_WRITE or ..._READ */
4483
 
{
4484
 
        ulint           segment;
4485
 
        os_aio_array_t* array;
4486
 
        os_aio_slot_t*  slot;
4487
 
        ulint           n;
4488
 
        ulint           i;
4489
 
        ibool           ret = FALSE;
4490
 
 
4491
 
        /* Should never be doing Sync IO here. */
4492
 
        ut_a(global_seg != ULINT_UNDEFINED);
4493
 
 
4494
 
        /* Find the array and the local segment. */
4495
 
        segment = os_aio_get_array_and_local_segment(&array, global_seg);
4496
 
        n = array->n_slots / array->n_segments;
4497
 
 
4498
 
        /* Loop until we have found a completed request. */
4499
 
        for (;;) {
4500
 
                os_mutex_enter(array->mutex);
4501
 
                for (i = 0; i < n; ++i) {
4502
 
                        slot = os_aio_array_get_nth_slot(
4503
 
                                        array, i + segment * n);
4504
 
                        if (slot->reserved && slot->io_already_done) {
4505
 
                                /* Something for us to work on. */
4506
 
                                goto found;
4507
 
                        }
4508
 
                }
4509
 
 
4510
 
                os_mutex_exit(array->mutex);
4511
 
 
4512
 
                /* We don't have any completed request.
4513
 
                Wait for some request. Note that we return
4514
 
                from wait iff we have found a request. */
4515
 
 
4516
 
                srv_set_io_thread_op_info(global_seg,
4517
 
                        "waiting for completed aio requests");
4518
 
                os_aio_linux_collect(array, segment, n);
4519
 
        }
4520
 
 
4521
 
found:
4522
 
        /* Note that it may be that there are more then one completed
4523
 
        IO requests. We process them one at a time. We may have a case
4524
 
        here to improve the performance slightly by dealing with all
4525
 
        requests in one sweep. */
4526
 
        srv_set_io_thread_op_info(global_seg,
4527
 
                                "processing completed aio requests");
4528
 
 
4529
 
        /* Ensure that we are scribbling only our segment. */
4530
 
        ut_a(i < n);
4531
 
 
4532
 
        ut_ad(slot != NULL);
4533
 
        ut_ad(slot->reserved);
4534
 
        ut_ad(slot->io_already_done);
4535
 
 
4536
 
        *message1 = slot->message1;
4537
 
        *message2 = slot->message2;
4538
 
 
4539
 
        *type = slot->type;
4540
 
 
4541
 
        if ((slot->ret == 0) && (slot->n_bytes == (long)slot->len)) {
4542
 
                ret = TRUE;
4543
 
 
4544
 
#ifdef UNIV_DO_FLUSH
4545
 
                if (slot->type == OS_FILE_WRITE
4546
 
                    && !os_do_not_call_flush_at_each_write)
4547
 
                    && !os_file_flush(slot->file) {
4548
 
                        ut_error;
4549
 
                }
4550
 
#endif /* UNIV_DO_FLUSH */
4551
 
        } else {
4552
 
                errno = -slot->ret;
4553
 
 
4554
 
                /* os_file_handle_error does tell us if we should retry
4555
 
                this IO. As it stands now, we don't do this retry when
4556
 
                reaping requests from a different context than
4557
 
                the dispatcher. This non-retry logic is the same for
4558
 
                windows and linux native AIO.
4559
 
                We should probably look into this to transparently
4560
 
                re-submit the IO. */
4561
 
                os_file_handle_error(slot->name, "Linux aio");
4562
 
 
4563
 
                ret = FALSE;
4564
 
        }
4565
 
 
4566
 
        os_mutex_exit(array->mutex);
4567
 
 
4568
 
        os_aio_array_free_slot(array, slot);
4569
 
 
4570
 
        return(ret);
4571
 
}
4572
 
#endif /* LINUX_NATIVE_AIO */
4573
 
 
4574
 
/**********************************************************************//**
 
3644
/**************************************************************************
4575
3645
Does simulated aio. This function should be called by an i/o-handler
4576
 
thread.
4577
 
@return TRUE if the aio operation succeeded */
 
3646
thread. */
4578
3647
UNIV_INTERN
4579
3648
ibool
4580
3649
os_aio_simulated_handle(
4581
3650
/*====================*/
4582
 
        ulint   global_segment, /*!< in: the number of the segment in the aio
 
3651
                                /* out: TRUE if the aio operation succeeded */
 
3652
        ulint   global_segment, /* in: the number of the segment in the aio
4583
3653
                                arrays to wait for; segment 0 is the ibuf
4584
3654
                                i/o thread, segment 1 the log i/o thread,
4585
3655
                                then follow the non-ibuf read threads, and as
4586
3656
                                the last are the non-ibuf write threads */
4587
 
        fil_node_t**message1,   /*!< out: the messages passed with the aio
 
3657
        fil_node_t**message1,   /* out: the messages passed with the aio
4588
3658
                                request; note that also in the case where
4589
3659
                                the aio operation failed, these output
4590
3660
                                parameters are valid and can be used to
4591
3661
                                restart the operation, for example */
4592
3662
        void**  message2,
4593
 
        ulint*  type)           /*!< out: OS_FILE_WRITE or ..._READ */
 
3663
        ulint*  type)           /* out: OS_FILE_WRITE or ..._READ */
4594
3664
{
4595
3665
        os_aio_array_t* array;
4596
3666
        ulint           segment;
4609
3679
        ulint           n;
4610
3680
        ulint           i;
4611
3681
 
4612
 
        /* Fix compiler warning */
4613
 
        *consecutive_ios = NULL;
4614
 
 
4615
3682
        memset(consecutive_ios, 0, sizeof(os_aio_slot_t*) * OS_AIO_MERGE_N_CONSECUTIVE);
4616
3683
        segment = os_aio_get_array_and_local_segment(&array, global_segment);
4617
3684
 
4724
3791
                goto wait_for_io;
4725
3792
        }
4726
3793
 
4727
 
        /* if n_consecutive != 0, then we have assigned
4728
 
        something valid to consecutive_ios[0] */
4729
 
        ut_ad(n_consecutive != 0);
4730
 
        ut_ad(consecutive_ios[0] != NULL);
4731
 
 
4732
3794
        slot = consecutive_ios[0];
4733
3795
 
4734
3796
        /* Check if there are several consecutive blocks to read or write */
4901
3963
        goto restart;
4902
3964
}
4903
3965
 
4904
 
/**********************************************************************//**
4905
 
Validates the consistency of an aio array.
4906
 
@return TRUE if ok */
 
3966
/**************************************************************************
 
3967
Validates the consistency of an aio array. */
4907
3968
static
4908
3969
ibool
4909
3970
os_aio_array_validate(
4910
3971
/*==================*/
4911
 
        os_aio_array_t* array)  /*!< in: aio wait array */
 
3972
                                /* out: TRUE if ok */
 
3973
        os_aio_array_t* array)  /* in: aio wait array */
4912
3974
{
4913
3975
        os_aio_slot_t*  slot;
4914
3976
        ulint           n_reserved      = 0;
4937
3999
        return(TRUE);
4938
4000
}
4939
4001
 
4940
 
/**********************************************************************//**
4941
 
Validates the consistency the aio system.
4942
 
@return TRUE if ok */
 
4002
/**************************************************************************
 
4003
Validates the consistency the aio system. */
4943
4004
UNIV_INTERN
4944
4005
ibool
4945
4006
os_aio_validate(void)
4946
4007
/*=================*/
 
4008
                                /* out: TRUE if ok */
4947
4009
{
4948
4010
        os_aio_array_validate(os_aio_read_array);
4949
4011
        os_aio_array_validate(os_aio_write_array);
4954
4016
        return(TRUE);
4955
4017
}
4956
4018
 
4957
 
/**********************************************************************//**
4958
 
Prints pending IO requests per segment of an aio array.
4959
 
We probably don't need per segment statistics but they can help us
4960
 
during development phase to see if the IO requests are being
4961
 
distributed as expected. */
4962
 
static
4963
 
void
4964
 
os_aio_print_segment_info(
4965
 
/*======================*/
4966
 
        FILE*           file,   /*!< in: file where to print */
4967
 
        ulint*          n_seg,  /*!< in: pending IO array */
4968
 
        os_aio_array_t* array)  /*!< in: array to process */
4969
 
{
4970
 
        ulint   i;
4971
 
 
4972
 
        ut_ad(array);
4973
 
        ut_ad(n_seg);
4974
 
        ut_ad(array->n_segments > 0);
4975
 
 
4976
 
        if (array->n_segments == 1) {
4977
 
                return;
4978
 
        }
4979
 
 
4980
 
        fprintf(file, " [");
4981
 
        for (i = 0; i < array->n_segments; i++) {
4982
 
                if (i != 0) {
4983
 
                        fprintf(file, ", ");
4984
 
                }
4985
 
 
4986
 
                fprintf(file, "%lu", n_seg[i]);
4987
 
        }
4988
 
        fprintf(file, "] ");
4989
 
}
4990
 
 
4991
 
/**********************************************************************//**
 
4019
/**************************************************************************
4992
4020
Prints info of the aio arrays. */
4993
4021
UNIV_INTERN
4994
4022
void
4995
4023
os_aio_print(
4996
4024
/*=========*/
4997
 
        FILE*   file)   /*!< in: file where to print */
 
4025
        FILE*   file)   /* in: file where to print */
4998
4026
{
4999
4027
        os_aio_array_t* array;
5000
4028
        os_aio_slot_t*  slot;
5001
4029
        ulint           n_reserved;
5002
 
        ulint           n_res_seg[SRV_MAX_N_IO_THREADS];
5003
4030
        time_t          current_time;
5004
4031
        double          time_elapsed;
5005
4032
        double          avg_bytes_read;
5032
4059
 
5033
4060
        n_reserved = 0;
5034
4061
 
5035
 
        memset(n_res_seg, 0x0, sizeof(n_res_seg));
5036
 
 
5037
4062
        for (i = 0; i < array->n_slots; i++) {
5038
 
                ulint   seg_no;
5039
 
 
5040
4063
                slot = os_aio_array_get_nth_slot(array, i);
5041
4064
 
5042
 
                seg_no = (i * array->n_segments) / array->n_slots;
5043
4065
                if (slot->reserved) {
5044
4066
                        n_reserved++;
5045
 
                        n_res_seg[seg_no]++;
5046
4067
#if 0
5047
4068
                        fprintf(stderr, "Reserved slot, messages %p %p\n",
5048
4069
                                (void*) slot->message1,
5056
4077
 
5057
4078
        fprintf(file, " %lu", (ulong) n_reserved);
5058
4079
 
5059
 
        os_aio_print_segment_info(file, n_res_seg, array);
5060
 
 
5061
4080
        os_mutex_exit(array->mutex);
5062
4081
 
5063
4082
        if (array == os_aio_read_array) {
5134
4153
        os_last_printout = current_time;
5135
4154
}
5136
4155
 
5137
 
/**********************************************************************//**
 
4156
/**************************************************************************
5138
4157
Refreshes the statistics used to print per-second averages. */
5139
4158
UNIV_INTERN
5140
4159
void
5150
4169
}
5151
4170
 
5152
4171
#ifdef UNIV_DEBUG
5153
 
/**********************************************************************//**
 
4172
/**************************************************************************
5154
4173
Checks that all slots in the system have been freed, that is, there are
5155
 
no pending io operations.
5156
 
@return TRUE if all free */
 
4174
no pending io operations. */
5157
4175
UNIV_INTERN
5158
4176
ibool
5159
4177
os_aio_all_slots_free(void)
5160
4178
/*=======================*/
 
4179
                                /* out: TRUE if all free */
5161
4180
{
5162
4181
        os_aio_array_t* array;
5163
4182
        ulint           n_res   = 0;
5210
4229
        return(FALSE);
5211
4230
}
5212
4231
#endif /* UNIV_DEBUG */
5213
 
 
5214
 
#endif /* !UNIV_HOTBACKUP */