55# include <sys/types.h>
60#if defined(LINUX_NATIVE_AIO)
71 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
80UNIV_INTERN ibool os_do_not_call_flush_at_each_write = FALSE;
88#define OS_FILE_N_SEEK_MUTEXES 16
89UNIV_INTERN
os_mutex_t os_file_seek_mutexes[OS_FILE_N_SEEK_MUTEXES];
92#define OS_AIO_MERGE_N_CONSECUTIVE 64
153UNIV_INTERN mysql_pfs_key_t innodb_file_data_key;
154UNIV_INTERN mysql_pfs_key_t innodb_file_log_key;
155UNIV_INTERN mysql_pfs_key_t innodb_file_temp_key;
192#elif defined(LINUX_NATIVE_AIO)
239#if defined(LINUX_NATIVE_AIO)
240 io_context_t* aio_ctx;
244 struct io_event* aio_events;
252#if defined(LINUX_NATIVE_AIO)
254#define OS_AIO_REAP_TIMEOUT (500000000UL)
257#define OS_AIO_IO_SETUP_RETRY_SLEEP (500000UL)
260#define OS_AIO_IO_SETUP_RETRY_ATTEMPTS 5
264static os_event_t* os_aio_segment_wait_events = NULL;
268static os_aio_array_t* os_aio_read_array = NULL;
269static os_aio_array_t* os_aio_write_array = NULL;
270static os_aio_array_t* os_aio_ibuf_array = NULL;
271static os_aio_array_t* os_aio_log_array = NULL;
272static os_aio_array_t* os_aio_sync_array = NULL;
276static ulint os_aio_n_segments = ULINT_UNDEFINED;
280static ibool os_aio_recommend_sleep_for_read_threads = FALSE;
283UNIV_INTERN ulint os_n_file_reads = 0;
284UNIV_INTERN ulint os_bytes_read_since_printout = 0;
285UNIV_INTERN ulint os_n_file_writes = 0;
286UNIV_INTERN ulint os_n_fsyncs = 0;
287UNIV_INTERN ulint os_n_file_reads_old = 0;
288UNIV_INTERN ulint os_n_file_writes_old = 0;
289UNIV_INTERN ulint os_n_fsyncs_old = 0;
290UNIV_INTERN time_t os_last_printout;
292UNIV_INTERN ibool os_has_said_disk_full = FALSE;
294#ifndef UNIV_HOTBACKUP
313os_aio_validate_skip(
void)
317# define OS_AIO_VALIDATE_SKIP 13
321 static int os_aio_validate_count = OS_AIO_VALIDATE_SKIP;
327 if (--os_aio_validate_count > 0) {
331 os_aio_validate_count = OS_AIO_VALIDATE_SKIP;
343os_get_os_version(
void)
346 OSVERSIONINFO os_info;
348 os_info.dwOSVersionInfoSize =
sizeof(OSVERSIONINFO);
350 ut_a(GetVersionEx(&os_info));
352 if (os_info.dwPlatformId == VER_PLATFORM_WIN32s) {
354 }
else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
356 }
else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_NT) {
357 switch (os_info.dwMajorVersion) {
362 return (os_info.dwMinorVersion == 0) ?
OS_WIN2000
387 ibool report_all_errors)
394 err = (ulint) GetLastError();
396 if (report_all_errors
397 || (err != ERROR_DISK_FULL && err != ERROR_FILE_EXISTS)) {
401 " InnoDB: Operating system error number %lu"
402 " in a file operation.\n", (ulong) err);
404 if (err == ERROR_PATH_NOT_FOUND) {
406 "InnoDB: The error means the system"
407 " cannot find the path specified.\n");
411 "InnoDB: If you are installing InnoDB,"
412 " remember that you must create\n"
413 "InnoDB: directories yourself, InnoDB"
414 " does not create them.\n");
416 }
else if (err == ERROR_ACCESS_DENIED) {
418 "InnoDB: The error means mysqld does not have"
419 " the access rights to\n"
420 "InnoDB: the directory. It may also be"
421 " you have created a subdirectory\n"
422 "InnoDB: of the same name as a data file.\n");
423 }
else if (err == ERROR_SHARING_VIOLATION
424 || err == ERROR_LOCK_VIOLATION) {
426 "InnoDB: The error means that another program"
427 " is using InnoDB's files.\n"
428 "InnoDB: This might be a backup or antivirus"
429 " software or another instance\n"
431 " Please close it to get rid of this error.\n");
432 }
else if (err == ERROR_WORKING_SET_QUOTA
433 || err == ERROR_NO_SYSTEM_RESOURCES) {
435 "InnoDB: The error means that there are no"
436 " sufficient system resources or quota to"
437 " complete the operation.\n");
438 }
else if (err == ERROR_OPERATION_ABORTED) {
440 "InnoDB: The error means that the I/O"
441 " operation has been aborted\n"
442 "InnoDB: because of either a thread exit"
443 " or an application request.\n"
444 "InnoDB: Retry attempt is made.\n");
447 "InnoDB: Some operating system error numbers"
448 " are described at\n"
451 "operating-system-error-codes.html\n");
457 if (err == ERROR_FILE_NOT_FOUND) {
459 }
else if (err == ERROR_DISK_FULL) {
460 return(OS_FILE_DISK_FULL);
461 }
else if (err == ERROR_FILE_EXISTS) {
462 return(OS_FILE_ALREADY_EXISTS);
463 }
else if (err == ERROR_SHARING_VIOLATION
464 || err == ERROR_LOCK_VIOLATION) {
465 return(OS_FILE_SHARING_VIOLATION);
466 }
else if (err == ERROR_WORKING_SET_QUOTA
467 || err == ERROR_NO_SYSTEM_RESOURCES) {
468 return(OS_FILE_INSUFFICIENT_RESOURCE);
469 }
else if (err == ERROR_OPERATION_ABORTED) {
470 return(OS_FILE_OPERATION_ABORTED);
477 if (report_all_errors
478 || (err != ENOSPC && err != EEXIST)) {
482 " InnoDB: Operating system error number %lu"
483 " in a file operation.\n", (ulong) err);
487 "InnoDB: The error means the system"
488 " cannot find the path specified.\n");
492 "InnoDB: If you are installing InnoDB,"
493 " remember that you must create\n"
494 "InnoDB: directories yourself, InnoDB"
495 " does not create them.\n");
497 }
else if (err == EACCES) {
499 "InnoDB: The error means mysqld does not have"
500 " the access rights to\n"
501 "InnoDB: the directory.\n");
503 if (strerror((
int)err) != NULL) {
505 "InnoDB: Error number %lu"
507 err, strerror((
int)err));
511 "InnoDB: Some operating system"
512 " error numbers are described at\n"
515 "operating-system-error-codes.html\n");
523 return(OS_FILE_DISK_FULL);
527 return(OS_FILE_ALREADY_EXISTS);
531 return(OS_FILE_PATH_ERROR);
533 if (srv_use_native_aio) {
534 return(OS_FILE_AIO_RESOURCES_RESERVED);
538 if (srv_use_native_aio) {
539 return(OS_FILE_AIO_INTERRUPTED);
554os_file_handle_error_cond_exit(
557 const char* operation,
565 if (err == OS_FILE_DISK_FULL) {
568 if (os_has_said_disk_full) {
576 " InnoDB: Encountered a problem with"
582 " InnoDB: Disk is full. Try to clean the disk"
583 " to free space.\n");
585 os_has_said_disk_full = TRUE;
590 }
else if (err == OS_FILE_AIO_RESOURCES_RESERVED) {
593 }
else if (err == OS_FILE_AIO_INTERRUPTED) {
596 }
else if (err == OS_FILE_ALREADY_EXISTS
597 || err == OS_FILE_PATH_ERROR) {
600 }
else if (err == OS_FILE_SHARING_VIOLATION) {
604 }
else if (err == OS_FILE_INSUFFICIENT_RESOURCE) {
608 }
else if (err == OS_FILE_OPERATION_ABORTED) {
614 fprintf(stderr,
"InnoDB: File name %s\n", name);
617 fprintf(stderr,
"InnoDB: File operation call: '%s'.\n",
621 fprintf(stderr,
"InnoDB: Cannot continue operation.\n");
640 const char* operation)
643 return(os_file_handle_error_cond_exit(name, operation, TRUE));
651os_file_handle_error_no_exit(
654 const char* operation)
657 return(os_file_handle_error_cond_exit(name, operation, FALSE));
662#if defined(UNIV_HOTBACKUP) || defined(__WIN__)
685 lk.l_whence = SEEK_SET;
686 lk.l_start = lk.l_len = 0;
687 if (fcntl(fd, F_SETLK, &lk) == -1) {
689 "InnoDB: Unable to lock %s, error: %d\n", name, errno);
691 if (errno == EAGAIN || errno == EACCES) {
693 "InnoDB: Check that you do not already have"
694 " another drizzled process\n"
695 "InnoDB: using the same InnoDB data"
706#ifndef UNIV_HOTBACKUP
718 for (i = 0; i < OS_FILE_N_SEEK_MUTEXES; i++) {
736 file = fdopen(fd,
"w+b");
742 " InnoDB: Error: unable to create temporary file;"
743 " errno: %d\n", errno);
765 ibool error_is_fatal)
773 LPWIN32_FIND_DATA lpFindFileData;
774 char path[OS_FILE_MAX_PATH + 3];
776 ut_a(strlen(dirname) < OS_FILE_MAX_PATH);
778 strcpy(path, dirname);
779 strcpy(path + strlen(path),
"\\*");
785 lpFindFileData =
ut_malloc(
sizeof(WIN32_FIND_DATA));
787 dir = FindFirstFile((LPCTSTR) path, lpFindFileData);
791 if (dir == INVALID_HANDLE_VALUE) {
793 if (error_is_fatal) {
794 os_file_handle_error(dirname,
"opendir");
802 dir = opendir(dirname);
804 if (dir == NULL && error_is_fatal) {
805 os_file_handle_error(dirname,
"opendir");
824 ret = FindClose(dir);
827 os_file_handle_error_no_exit(NULL,
"closedir");
839 os_file_handle_error_no_exit(NULL,
"closedir");
856 os_file_stat_t* info)
859 LPWIN32_FIND_DATA lpFindFileData;
862 lpFindFileData =
ut_malloc(
sizeof(WIN32_FIND_DATA));
864 ret = FindNextFile(dir, lpFindFileData);
867 ut_a(strlen((
char *) lpFindFileData->cFileName)
870 if (strcmp((
char *) lpFindFileData->cFileName,
".") == 0
871 || strcmp((
char *) lpFindFileData->cFileName,
"..") == 0) {
876 strcpy(info->
name, (
char *) lpFindFileData->cFileName);
878 info->
size = (ib_int64_t)(lpFindFileData->nFileSizeLow)
879 + (((ib_int64_t)(lpFindFileData->nFileSizeHigh))
882 if (lpFindFileData->dwFileAttributes
883 & FILE_ATTRIBUTE_REPARSE_POINT) {
889 info->
type = OS_FILE_TYPE_LINK;
890 }
else if (lpFindFileData->dwFileAttributes
891 & FILE_ATTRIBUTE_DIRECTORY) {
892 info->
type = OS_FILE_TYPE_DIR;
898 info->
type = OS_FILE_TYPE_FILE;
906 }
else if (GetLastError() == ERROR_NO_MORE_FILES) {
910 os_file_handle_error_no_exit(dirname,
911 "readdir_next_file");
918 struct stat statinfo;
920 char dirent_buf[
sizeof(
struct dirent)
921 + _POSIX_PATH_MAX + 100];
930 ret = readdir_r(dir, (
struct dirent*)dirent_buf, &ent);
942 "InnoDB: cannot read directory %s, error %lu\n",
943 dirname, (ulong)ret);
954 ut_a(strlen(ent->d_name) < _POSIX_PATH_MAX + 100 - 1);
963 ut_a(strlen(ent->d_name) < OS_FILE_MAX_PATH);
965 if (strcmp(ent->d_name,
".") == 0 || strcmp(ent->d_name,
"..") == 0) {
970 strcpy(info->
name, ent->d_name);
972 full_path =
static_cast<char*
>(
ut_malloc(strlen(dirname) + strlen(ent->d_name) + 10));
974 sprintf(full_path,
"%s/%s", dirname, ent->d_name);
976 ret = stat(full_path, &statinfo);
980 if (errno == ENOENT) {
996 os_file_handle_error_no_exit(full_path,
"stat");
1003 info->
size = (ib_int64_t)statinfo.st_size;
1005 if (S_ISDIR(statinfo.st_mode)) {
1006 info->
type = OS_FILE_TYPE_DIR;
1007 }
else if (S_ISLNK(statinfo.st_mode)) {
1008 info->
type = OS_FILE_TYPE_LINK;
1009 }
else if (S_ISREG(statinfo.st_mode)) {
1010 info->
type = OS_FILE_TYPE_FILE;
1012 info->
type = OS_FILE_TYPE_UNKNOWN;
1031 const char* pathname,
1033 ibool fail_if_exists)
1039 rcode = CreateDirectory((LPCTSTR) pathname, NULL);
1041 || (GetLastError() == ERROR_ALREADY_EXISTS
1042 && !fail_if_exists))) {
1044 os_file_handle_error(pathname,
"CreateDirectory");
1053 rcode = mkdir(pathname, 0770);
1055 if (!(rcode == 0 || (errno == EEXIST && !fail_if_exists))) {
1057 os_file_handle_error(pathname,
"mkdir");
1093 DWORD attributes = 0;
1100 create_flag = OPEN_EXISTING;
1101 }
else if (create_mode == OS_FILE_CREATE) {
1102 create_flag = CREATE_NEW;
1103 }
else if (create_mode == OS_FILE_CREATE_PATH) {
1109 create_flag = CREATE_NEW;
1110 create_mode = OS_FILE_CREATE;
1116 if (access_type == OS_FILE_READ_ONLY) {
1117 access = GENERIC_READ;
1118 }
else if (access_type == OS_FILE_READ_WRITE) {
1119 access = GENERIC_READ | GENERIC_WRITE;
1125 file = CreateFile((LPCTSTR) name,
1127 FILE_SHARE_READ | FILE_SHARE_WRITE,
1135 if (file == INVALID_HANDLE_VALUE) {
1138 retry = os_file_handle_error(name,
1158 if (access_type == OS_FILE_READ_ONLY) {
1159 create_flag = O_RDONLY;
1161 create_flag = O_RDWR;
1163 }
else if (create_mode == OS_FILE_CREATE) {
1164 create_flag = O_RDWR | O_CREAT | O_EXCL;
1165 }
else if (create_mode == OS_FILE_CREATE_PATH) {
1171 create_flag = O_RDWR | O_CREAT | O_EXCL;
1172 create_mode = OS_FILE_CREATE;
1178 if (create_mode == OS_FILE_CREATE) {
1179 file = open(name, create_flag, S_IRUSR | S_IWUSR
1180 | S_IRGRP | S_IWGRP);
1182 file = open(name, create_flag);
1188 retry = os_file_handle_error(name,
1195 }
else if (access_type == OS_FILE_READ_WRITE
1196 && os_file_lock(file, name)) {
1235 DWORD attributes = 0;
1236 DWORD share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
1241 create_flag = OPEN_EXISTING;
1242 }
else if (create_mode == OS_FILE_CREATE) {
1243 create_flag = CREATE_NEW;
1249 if (access_type == OS_FILE_READ_ONLY) {
1250 access = GENERIC_READ;
1251 }
else if (access_type == OS_FILE_READ_WRITE) {
1252 access = GENERIC_READ | GENERIC_WRITE;
1253 }
else if (access_type == OS_FILE_READ_ALLOW_DELETE) {
1254 access = GENERIC_READ;
1255 share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ
1265 file = CreateFile((LPCTSTR) name,
1273 if (file == INVALID_HANDLE_VALUE) {
1287 if (access_type == OS_FILE_READ_ONLY) {
1288 create_flag = O_RDONLY;
1290 create_flag = O_RDWR;
1292 }
else if (create_mode == OS_FILE_CREATE) {
1293 create_flag = O_RDWR | O_CREAT | O_EXCL;
1299 if (create_mode == OS_FILE_CREATE) {
1300 file = open(name, create_flag, S_IRUSR | S_IWUSR
1301 | S_IRGRP | S_IWGRP);
1303 file = open(name, create_flag);
1309 }
else if (access_type == OS_FILE_READ_WRITE
1310 && os_file_lock(file, name)) {
1330 const char* file_name,
1331 const char* operation_name)
1336#if defined(UNIV_SOLARIS) && defined(DIRECTIO_ON)
1337 if (directio(fd, DIRECTIO_ON) == -1) {
1339 errno_save = (int)errno;
1342 " InnoDB: Failed to set DIRECTIO_ON "
1343 "on file %s: %s: %s, continuing anyway\n",
1344 file_name, operation_name, strerror(errno_save));
1346#elif defined(O_DIRECT)
1347 if (fcntl(fd, F_SETFL, O_DIRECT) == -1) {
1349 errno_save = (int)errno;
1352 " InnoDB: Failed to set O_DIRECT "
1353 "on file %s: %s: %s, continuing anyway\n",
1354 file_name, operation_name, strerror(errno_save));
1355 if (errno_save == EINVAL) {
1358 " InnoDB: O_DIRECT is known to result in "
1359 "'Invalid argument' on Linux on tmpfs, "
1360 "see MySQL Bug#26662\n");
1366 (void)operation_name;
1402 DWORD share_mode = FILE_SHARE_READ;
1409 if (create_mode == OS_FILE_OPEN_RAW) {
1410 create_flag = OPEN_EXISTING;
1411 share_mode = FILE_SHARE_WRITE;
1413 || create_mode == OS_FILE_OPEN_RETRY) {
1414 create_flag = OPEN_EXISTING;
1415 }
else if (create_mode == OS_FILE_CREATE) {
1416 create_flag = CREATE_NEW;
1417 }
else if (create_mode == OS_FILE_OVERWRITE) {
1418 create_flag = CREATE_ALWAYS;
1424 if (purpose == OS_FILE_AIO) {
1429 if (srv_use_native_aio) {
1430 attributes = attributes | FILE_FLAG_OVERLAPPED;
1433#ifdef UNIV_NON_BUFFERED_IO
1434# ifndef UNIV_HOTBACKUP
1435 if (type == OS_LOG_FILE && srv_flush_log_at_trx_commit == 2) {
1439 }
else if (srv_win_file_flush_method
1441 attributes = attributes | FILE_FLAG_NO_BUFFERING;
1444 attributes = attributes | FILE_FLAG_NO_BUFFERING;
1447 }
else if (purpose == OS_FILE_NORMAL) {
1449#ifdef UNIV_NON_BUFFERED_IO
1450# ifndef UNIV_HOTBACKUP
1451 if (type == OS_LOG_FILE && srv_flush_log_at_trx_commit == 2) {
1455 }
else if (srv_win_file_flush_method
1457 attributes = attributes | FILE_FLAG_NO_BUFFERING;
1460 attributes = attributes | FILE_FLAG_NO_BUFFERING;
1468 file = CreateFile((LPCTSTR) name,
1469 GENERIC_READ | GENERIC_WRITE,
1487 if (file == INVALID_HANDLE_VALUE) {
1498 retry = os_file_handle_error_no_exit(name,
1499 create_mode == OS_FILE_CREATE ?
1502 retry = os_file_handle_error(name,
1503 create_mode == OS_FILE_CREATE ?
1519 const char* mode_str = NULL;
1524 if (create_mode ==
OS_FILE_OPEN || create_mode == OS_FILE_OPEN_RAW
1525 || create_mode == OS_FILE_OPEN_RETRY) {
1528 create_flag = O_RDONLY;
1530 create_flag = O_RDWR;
1531 }
else if (create_mode == OS_FILE_CREATE) {
1532 mode_str =
"CREATE";
1533 create_flag = O_RDWR | O_CREAT | O_EXCL;
1534 }
else if (create_mode == OS_FILE_OVERWRITE) {
1535 mode_str =
"OVERWRITE";
1536 create_flag = O_RDWR | O_CREAT | O_TRUNC;
1543 ut_a(purpose == OS_FILE_AIO || purpose == OS_FILE_NORMAL);
1549 if (type == OS_LOG_FILE
1553 fprintf(stderr,
"Using O_SYNC for file %s\n", name);
1556 create_flag = create_flag | O_SYNC;
1573 retry = os_file_handle_error_no_exit(name,
1574 create_mode == OS_FILE_CREATE ?
1577 retry = os_file_handle_error(name,
1578 create_mode == OS_FILE_CREATE ?
1593 if (type != OS_LOG_FILE
1605 if (create_mode != OS_FILE_OPEN_RAW && os_file_lock(file, name)) {
1607 if (create_mode == OS_FILE_OPEN_RETRY) {
1610 fputs(
" InnoDB: Retrying to lock"
1611 " the first data file\n",
1613 for (i = 0; i < 100; i++) {
1615 if (!os_file_lock(file, name)) {
1621 fputs(
" InnoDB: Unable to open the first data file\n",
1651 ret = DeleteFile((LPCTSTR)name);
1657 if (GetLastError() == ERROR_FILE_NOT_FOUND) {
1665 if (count > 100 && 0 == (count % 10)) {
1667 "InnoDB: Warning: cannot delete file %s\n"
1668 "InnoDB: Are you running ibbackup"
1669 " to back up the file?\n", name);
1687 if (ret != 0 && errno != ENOENT) {
1688 os_file_handle_error_no_exit(name,
"delete");
1713 ret = DeleteFile((LPCTSTR)name);
1719 if (GetLastError() == ERROR_FILE_NOT_FOUND) {
1728 if (count > 100 && 0 == (count % 10)) {
1730 "InnoDB: Warning: cannot delete file %s\n"
1731 "InnoDB: Are you running ibbackup"
1732 " to back up the file?\n", name);
1751 os_file_handle_error_no_exit(name,
"delete");
1769 const char* oldpath,
1771 const char* newpath)
1776 ret = MoveFile((LPCTSTR)oldpath, (LPCTSTR)newpath);
1782 os_file_handle_error_no_exit(oldpath,
"rename");
1788 ret = rename(oldpath, newpath);
1791 os_file_handle_error_no_exit(oldpath,
"rename");
1816 ret = CloseHandle(file);
1822 os_file_handle_error(NULL,
"close");
1831 os_file_handle_error(NULL,
"close");
1840#ifdef UNIV_HOTBACKUP
1846os_file_close_no_error_handling(
1855 ret = CloseHandle(file);
1893 low = GetFileSize(file, &high);
1895 if ((low == 0xFFFFFFFF) && (GetLastError() != NO_ERROR)) {
1906 offs = lseek(file, 0, SEEK_END);
1908 if (offs == ((off_t)-1)) {
1913 if (
sizeof(off_t) > 4) {
1914 *size = (ulint)(offs & 0xFFFFFFFFUL);
1915 *size_high = (ulint)(offs >> 32);
1917 *size = (ulint) offs;
1945 return((((ib_int64_t)size_high) << 32) + (ib_int64_t)size);
1962 ib_int64_t current_size;
1963 ib_int64_t desired_size;
1969 ut_a(size == (size & 0xFFFFFFFF));
1972 desired_size = (ib_int64_t)size + (((ib_int64_t)size_high) << 32);
1975 buf_size =
ut_min(64, (ulint) (desired_size / UNIV_PAGE_SIZE))
1977 buf2 =
static_cast<unsigned char *
>(
ut_malloc(buf_size + UNIV_PAGE_SIZE));
1980 buf =
static_cast<unsigned char *
>(
ut_align(buf2, UNIV_PAGE_SIZE));
1983 memset(buf, 0, buf_size);
1985 if (desired_size >= (ib_int64_t)(100 * 1024 * 1024)) {
1987 fprintf(stderr,
"InnoDB: Progress in MB:");
1990 while (current_size < desired_size) {
1993 if (desired_size - current_size < (ib_int64_t) buf_size) {
1994 n_bytes = (ulint) (desired_size - current_size);
1999 ret = os_file_write(name, file, buf,
2000 (ulint)(current_size & 0xFFFFFFFF),
2001 (ulint)(current_size >> 32),
2005 goto error_handling;
2009 if ((ib_int64_t) (current_size + n_bytes) / (ib_int64_t)(100 * 1024 * 1024)
2010 != current_size / (ib_int64_t)(100 * 1024 * 1024)) {
2012 fprintf(stderr,
" %lu00",
2013 (ulong) ((current_size + n_bytes)
2014 / (ib_int64_t)(100 * 1024 * 1024)));
2017 current_size += n_bytes;
2020 if (desired_size >= (ib_int64_t)(100 * 1024 * 1024)) {
2022 fprintf(stderr,
"\n");
2027 ret = os_file_flush(file);
2047 HANDLE h = (HANDLE) _get_osfhandle(fileno(file));
2048 return(SetEndOfFile(h));
2050 return(!ftruncate(fileno(file), ftell(file)));
2078 if (ret == -1 && errno == ENOLCK) {
2080 if (failures % 100 == 0) {
2084 " InnoDB: fsync(): "
2085 "No locks available; retrying\n");
2120 ret = FlushFileBuffers(file);
2131 == ERROR_INVALID_FUNCTION) {
2135 os_file_handle_error(NULL,
"flush");
2145#if defined(HAVE_DARWIN_THREADS)
2148# define F_FULLFSYNC 51
2149# elif F_FULLFSYNC != 51
2150# error "F_FULLFSYNC != 51: ABI incompatibility with Mac OS X 10.3"
2157 if (!srv_have_fullfsync) {
2161 ret = os_file_fsync(file);
2163 ret = fcntl(file, F_FULLFSYNC, NULL);
2168 ret = os_file_fsync(file);
2172 ret = os_file_fsync(file);
2190 " InnoDB: Error: the OS said file flush did not succeed\n");
2192 os_file_handle_error(NULL,
"flush");
2219#if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD)
2223 ut_a((offset & 0xFFFFFFFFUL) == offset);
2228 if (
sizeof(off_t) > 4) {
2229 offs = (off_t)offset + (((off_t)offset_high) << 32);
2232 offs = (off_t)offset;
2234 if (offset_high > 0) {
2236 "InnoDB: Error: file read at offset > 4 GB\n");
2242#if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD)
2248 n_bytes = pread(file, buf, (ssize_t)n, offs);
2260#ifndef UNIV_HOTBACKUP
2268#ifndef UNIV_HOTBACKUP
2270 i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2275 ret_offset = lseek(file, offs, SEEK_SET);
2277 if (ret_offset < 0) {
2280 ret = read(file, buf, (ssize_t)n);
2283#ifndef UNIV_HOTBACKUP
2314 ut_a((offset & 0xFFFFFFFFUL) == offset);
2319 if (
sizeof(off_t) > 4) {
2320 offs = (off_t)offset + (((off_t)offset_high) << 32);
2322 offs = (off_t)offset;
2324 if (offset_high > 0) {
2326 "InnoDB: Error: file write"
2327 " at offset > 4 GB\n");
2336#if defined(HAVE_PWRITE) && !defined(HAVE_BROKEN_PREAD)
2342 ret = pwrite(file, buf, (ssize_t)n, offs);
2349# ifdef UNIV_DO_FLUSH
2352 && !os_do_not_call_flush_at_each_write) {
2358 ut_a(TRUE == os_file_flush(file));
2366# ifndef UNIV_HOTBACKUP
2374# ifndef UNIV_HOTBACKUP
2376 i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2381 ret_offset = lseek(file, offs, SEEK_SET);
2383 if (ret_offset < 0) {
2389 ret = write(file, buf, (ssize_t)n);
2391# ifdef UNIV_DO_FLUSH
2394 && !os_do_not_call_flush_at_each_write) {
2400 ut_a(TRUE == os_file_flush(file));
2405# ifndef UNIV_HOTBACKUP
2443#ifndef UNIV_HOTBACKUP
2449 ut_a((offset & 0xFFFFFFFFUL) == offset);
2450 ut_a((n & 0xFFFFFFFFUL) == n);
2453 os_bytes_read_since_printout += n;
2460 low = (DWORD) offset;
2461 high = (DWORD) offset_high;
2467#ifndef UNIV_HOTBACKUP
2469 i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2474 ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);
2476 if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
2478#ifndef UNIV_HOTBACKUP
2486 goto error_handling;
2489 ret = ReadFile(file, buf, (DWORD) n, &len, NULL);
2491#ifndef UNIV_HOTBACKUP
2499 if (ret && len == n) {
2506 os_bytes_read_since_printout += n;
2509 ret = os_file_pread(file, buf, n, offset, offset_high);
2511 if ((ulint)ret == n) {
2517 "InnoDB: Error: tried to read %lu bytes at offset %lu %lu.\n"
2518 "InnoDB: Was only able to read %ld.\n",
2519 (ulong)n, (ulong)offset_high,
2520 (ulong)offset, (
long)ret);
2525 retry = os_file_handle_error(NULL,
"read");
2532 "InnoDB: Fatal error: cannot read from file."
2533 " OS error number %lu.\n",
2535 (ulong) GetLastError()
2572#ifndef UNIV_HOTBACKUP
2578 ut_a((offset & 0xFFFFFFFFUL) == offset);
2579 ut_a((n & 0xFFFFFFFFUL) == n);
2582 os_bytes_read_since_printout += n;
2589 low = (DWORD) offset;
2590 high = (DWORD) offset_high;
2596#ifndef UNIV_HOTBACKUP
2598 i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2603 ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);
2605 if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
2607#ifndef UNIV_HOTBACKUP
2615 goto error_handling;
2618 ret = ReadFile(file, buf, (DWORD) n, &len, NULL);
2620#ifndef UNIV_HOTBACKUP
2628 if (ret && len == n) {
2635 os_bytes_read_since_printout += n;
2638 ret = os_file_pread(file, buf, n, offset, offset_high);
2640 if ((ulint)ret == n) {
2648 retry = os_file_handle_error_no_exit(NULL,
"read");
2676 flen = fread(str, 1, size - 1, file);
2705 ulint n_retries = 0;
2707#ifndef UNIV_HOTBACKUP
2713 ut_a((offset & 0xFFFFFFFFUL) == offset);
2714 ut_a((n & 0xFFFFFFFFUL) == n);
2725 low = (DWORD) offset;
2726 high = (DWORD) offset_high;
2732#ifndef UNIV_HOTBACKUP
2734 i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
2739 ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);
2741 if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
2743#ifndef UNIV_HOTBACKUP
2754 " InnoDB: Error: File pointer positioning to"
2755 " file %s failed at\n"
2756 "InnoDB: offset %lu %lu. Operating system"
2757 " error number %lu.\n"
2758 "InnoDB: Some operating system error numbers"
2759 " are described at\n"
2761 REFMAN
"operating-system-error-codes.html\n",
2762 name, (ulong) offset_high, (ulong) offset,
2763 (ulong) GetLastError());
2768 ret = WriteFile(file, buf, (DWORD) n, &len, NULL);
2773# ifdef UNIV_DO_FLUSH
2774 if (!os_do_not_call_flush_at_each_write) {
2775 ut_a(TRUE == os_file_flush(file));
2779#ifndef UNIV_HOTBACKUP
2787 if (ret && len == n) {
2796 if (GetLastError() == ERROR_LOCK_VIOLATION && n_retries < 100) {
2805 if (!os_has_said_disk_full) {
2807 err = (ulint)GetLastError();
2812 " InnoDB: Error: Write to file %s failed"
2813 " at offset %lu %lu.\n"
2814 "InnoDB: %lu bytes should have been written,"
2815 " only %lu were written.\n"
2816 "InnoDB: Operating system error number %lu.\n"
2817 "InnoDB: Check that your OS and file system"
2818 " support files of this size.\n"
2819 "InnoDB: Check also that the disk is not full"
2820 " or a disk quota exceeded.\n",
2821 name, (ulong) offset_high, (ulong) offset,
2822 (ulong) n, (ulong) len, (ulong) err);
2824 if (strerror((
int)err) != NULL) {
2826 "InnoDB: Error number %lu means '%s'.\n",
2827 (ulong) err, strerror((
int)err));
2831 "InnoDB: Some operating system error numbers"
2832 " are described at\n"
2834 REFMAN
"operating-system-error-codes.html\n");
2836 os_has_said_disk_full = TRUE;
2843 ret = os_file_pwrite(file, buf, n, offset, offset_high);
2845 if ((ulint)ret == n) {
2850 if (!os_has_said_disk_full) {
2855 " InnoDB: Error: Write to file %s failed"
2856 " at offset %lu %lu.\n"
2857 "InnoDB: %lu bytes should have been written,"
2858 " only %ld were written.\n"
2859 "InnoDB: Operating system error number %lu.\n"
2860 "InnoDB: Check that your OS and file system"
2861 " support files of this size.\n"
2862 "InnoDB: Check also that the disk is not full"
2863 " or a disk quota exceeded.\n",
2864 name, offset_high, offset, n, (
long int)ret,
2866 if (strerror(errno) != NULL) {
2868 "InnoDB: Error number %lu means '%s'.\n",
2869 (ulint)errno, strerror(errno));
2873 "InnoDB: Some operating system error numbers"
2874 " are described at\n"
2876 REFMAN
"operating-system-error-codes.html\n");
2878 os_has_said_disk_full = TRUE;
2894 os_file_type_t* type)
2898 struct _stat statinfo;
2900 ret = _stat(path, &statinfo);
2901 if (ret && (errno == ENOENT || errno == ENOTDIR)) {
2908 os_file_handle_error_no_exit(path,
"stat");
2913 if (_S_IFDIR & statinfo.st_mode) {
2914 *type = OS_FILE_TYPE_DIR;
2915 }
else if (_S_IFREG & statinfo.st_mode) {
2916 *type = OS_FILE_TYPE_FILE;
2918 *type = OS_FILE_TYPE_UNKNOWN;
2926 struct stat statinfo;
2928 ret = stat(path, &statinfo);
2929 if (ret && (errno == ENOENT || errno == ENOTDIR)) {
2936 os_file_handle_error_no_exit(path,
"stat");
2941 if (S_ISDIR(statinfo.st_mode)) {
2942 *type = OS_FILE_TYPE_DIR;
2943 }
else if (S_ISLNK(statinfo.st_mode)) {
2944 *type = OS_FILE_TYPE_LINK;
2945 }
else if (S_ISREG(statinfo.st_mode)) {
2946 *type = OS_FILE_TYPE_FILE;
2948 *type = OS_FILE_TYPE_UNKNOWN;
2965 os_file_stat_t* stat_info)
2970 struct _stat statinfo;
2972 ret = _stat(path, &statinfo);
2973 if (ret && (errno == ENOENT || errno == ENOTDIR)) {
2980 os_file_handle_error_no_exit(path,
"stat");
2984 if (_S_IFDIR & statinfo.st_mode) {
2985 stat_info->
type = OS_FILE_TYPE_DIR;
2986 }
else if (_S_IFREG & statinfo.st_mode) {
2987 stat_info->
type = OS_FILE_TYPE_FILE;
2989 stat_info->
type = OS_FILE_TYPE_UNKNOWN;
2992 stat_info->
ctime = statinfo.st_ctime;
2993 stat_info->
atime = statinfo.st_atime;
2994 stat_info->
mtime = statinfo.st_mtime;
2995 stat_info->
size = statinfo.st_size;
3000 struct stat statinfo;
3002 ret = stat(path, &statinfo);
3004 if (ret && (errno == ENOENT || errno == ENOTDIR)) {
3011 os_file_handle_error_no_exit(path,
"stat");
3016 if (S_ISDIR(statinfo.st_mode)) {
3017 stat_info->
type = OS_FILE_TYPE_DIR;
3018 }
else if (S_ISLNK(statinfo.st_mode)) {
3019 stat_info->
type = OS_FILE_TYPE_LINK;
3020 }
else if (S_ISREG(statinfo.st_mode)) {
3021 stat_info->
type = OS_FILE_TYPE_FILE;
3023 stat_info->
type = OS_FILE_TYPE_UNKNOWN;
3026 stat_info->
ctime = statinfo.st_ctime;
3027 stat_info->
atime = statinfo.st_atime;
3028 stat_info->
mtime = statinfo.st_mtime;
3029 stat_info->
size = statinfo.st_size;
3037# define OS_FILE_PATH_SEPARATOR '\\'
3039# define OS_FILE_PATH_SEPARATOR '/'
3077 const char* last_slash = strrchr(path, OS_FILE_PATH_SEPARATOR);
3086 if (last_slash == path) {
3107 ibool success, subdir_exists;
3108 os_file_type_t type;
3111 if (strlen(subdir) == 1
3112 && (*subdir == OS_FILE_PATH_SEPARATOR || *subdir ==
'.')) {
3121 if (success && !subdir_exists) {
3137#ifndef UNIV_HOTBACKUP
3143os_aio_array_get_nth_slot(
3145 os_aio_array_t* array,
3148 ut_a(index < array->n_slots);
3150 return((array->
slots) + index);
3153#if defined(LINUX_NATIVE_AIO)
3159os_aio_linux_create_io_ctx(
3162 io_context_t* io_ctx)
3168 memset(io_ctx, 0x0,
sizeof(*io_ctx));
3173 ret = io_setup(max_events, io_ctx);
3175#if defined(UNIV_AIO_DEBUG)
3177 "InnoDB: Linux native AIO:"
3178 " initialized io_ctx for segment\n");
3192 " InnoDB: Warning: io_setup() failed"
3193 " with EAGAIN. Will make %d attempts"
3194 " before giving up.\n",
3195 OS_AIO_IO_SETUP_RETRY_ATTEMPTS);
3198 if (retries < OS_AIO_IO_SETUP_RETRY_ATTEMPTS) {
3201 "InnoDB: Warning: io_setup() attempt"
3211 " InnoDB: Error: io_setup() failed"
3212 " with EAGAIN after %d attempts.\n",
3213 OS_AIO_IO_SETUP_RETRY_ATTEMPTS);
3219 " InnoDB: Error: Linux Native AIO interface"
3220 " is not supported on this platform. Please"
3221 " check your OS documentation and install"
3222 " appropriate binary of InnoDB.\n");
3229 " InnoDB: Error: Linux Native AIO setup"
3230 " returned following error[%d]\n", -ret);
3235 "InnoDB: You can disable Linux Native AIO by"
3236 " setting innodb_native_aio = off in my.cnf\n");
3255 os_aio_array_t* array;
3257 os_aio_slot_t* slot;
3260#elif defined(LINUX_NATIVE_AIO)
3261 struct io_event* aio_event = NULL;
3264 ut_a(n_segments > 0);
3266 array =
static_cast<os_aio_array_t *
>(
ut_malloc(
sizeof(os_aio_array_t)));
3278 array->
slots =
static_cast<os_aio_slot_t *
>(
ut_malloc(n *
sizeof(os_aio_slot_t)));
3280 array->handles =
ut_malloc(n *
sizeof(HANDLE));
3283#if defined(LINUX_NATIVE_AIO)
3284 array->aio_ctx = NULL;
3285 array->aio_events = NULL;
3289 if (!srv_use_native_aio) {
3290 goto skip_native_aio;
3296 array->aio_ctx = (io_context**)
ut_malloc(n_segments *
3297 sizeof(*array->aio_ctx));
3298 for (i = 0; i < n_segments; ++i) {
3299 if (!os_aio_linux_create_io_ctx(n/n_segments,
3300 &array->aio_ctx[i])) {
3312 aio_event = (io_event*)
ut_malloc(n *
sizeof(io_event));
3313 memset(aio_event, 0x0,
sizeof(io_event) * n);
3314 array->aio_events = aio_event;
3318 for (i = 0; i < n; i++) {
3319 slot = os_aio_array_get_nth_slot(array, i);
3324 slot->handle = CreateEvent(NULL,TRUE, FALSE, NULL);
3326 over = &(slot->control);
3328 over->hEvent = slot->handle;
3330 *((array->handles) + i) = over->hEvent;
3332#elif defined(LINUX_NATIVE_AIO)
3334 memset(&slot->control, 0x0,
sizeof(slot->control));
3349 os_aio_array_t* array)
3354 for (i = 0; i < array->
n_slots; i++) {
3355 os_aio_slot_t* slot = os_aio_array_get_nth_slot(array, i);
3356 CloseHandle(slot->handle);
3367#if defined(LINUX_NATIVE_AIO)
3368 if (srv_use_native_aio) {
3397 ulint n_segments = 2 + n_read_segs + n_write_segs;
3399 ut_ad(n_segments >= 4);
3403 for (i = 0; i < n_segments; i++) {
3410 os_aio_ibuf_array = os_aio_array_create(n_per_seg, 1);
3411 if (os_aio_ibuf_array == NULL) {
3415 srv_io_thread_function[0] =
"insert buffer thread";
3417 os_aio_log_array = os_aio_array_create(n_per_seg, 1);
3418 if (os_aio_log_array == NULL) {
3422 srv_io_thread_function[1] =
"log thread";
3424 os_aio_read_array = os_aio_array_create(n_read_segs * n_per_seg,
3426 if (os_aio_read_array == NULL) {
3430 for (i = 2; i < 2 + n_read_segs; i++) {
3431 ut_a(i < SRV_MAX_N_IO_THREADS);
3432 srv_io_thread_function[i] =
"read thread";
3435 os_aio_write_array = os_aio_array_create(n_write_segs * n_per_seg,
3437 if (os_aio_write_array == NULL) {
3441 for (i = 2 + n_read_segs; i < n_segments; i++) {
3442 ut_a(i < SRV_MAX_N_IO_THREADS);
3443 srv_io_thread_function[i] =
"write thread";
3446 os_aio_sync_array = os_aio_array_create(n_slots_sync, 1);
3447 if (os_aio_sync_array == NULL) {
3452 os_aio_n_segments = n_segments;
3456 os_aio_segment_wait_events =
static_cast<os_event_t *
>(
ut_malloc(n_segments *
sizeof(
void*)));
3458 for (i = 0; i < n_segments; i++) {
3462 os_last_printout = time(NULL);
3480 os_aio_array_free(os_aio_ibuf_array);
3481 os_aio_ibuf_array = NULL;
3482 os_aio_array_free(os_aio_log_array);
3483 os_aio_log_array = NULL;
3484 os_aio_array_free(os_aio_read_array);
3485 os_aio_read_array = NULL;
3486 os_aio_array_free(os_aio_write_array);
3487 os_aio_write_array = NULL;
3488 os_aio_array_free(os_aio_sync_array);
3489 os_aio_sync_array = NULL;
3491 for (i = 0; i < os_aio_n_segments; i++) {
3495 ut_free(os_aio_segment_wait_events);
3496 os_aio_segment_wait_events = 0;
3497 os_aio_n_segments = 0;
3506os_aio_array_wake_win_aio_at_shutdown(
3508 os_aio_array_t* array)
3512 for (i = 0; i < array->
n_slots; i++) {
3514 SetEvent((array->
slots + i)->handle);
3531 os_aio_array_wake_win_aio_at_shutdown(os_aio_read_array);
3532 os_aio_array_wake_win_aio_at_shutdown(os_aio_write_array);
3533 os_aio_array_wake_win_aio_at_shutdown(os_aio_ibuf_array);
3534 os_aio_array_wake_win_aio_at_shutdown(os_aio_log_array);
3536#elif defined(LINUX_NATIVE_AIO)
3543 if (srv_use_native_aio) {
3551 for (i = 0; i < os_aio_n_segments; i++) {
3565 os_event_wait(os_aio_write_array->is_empty);
3574os_aio_get_segment_no_from_slot(
3576 os_aio_array_t* array,
3577 os_aio_slot_t* slot)
3582 if (array == os_aio_ibuf_array) {
3585 }
else if (array == os_aio_log_array) {
3588 }
else if (array == os_aio_read_array) {
3589 seg_len = os_aio_read_array->n_slots
3590 / os_aio_read_array->n_segments;
3592 segment = 2 + slot->
pos / seg_len;
3594 ut_a(array == os_aio_write_array);
3595 seg_len = os_aio_write_array->n_slots
3596 / os_aio_write_array->n_segments;
3598 segment = os_aio_read_array->n_segments + 2
3599 + slot->
pos / seg_len;
3610os_aio_get_array_and_local_segment(
3612 os_aio_array_t** array,
3613 ulint global_segment)
3617 ut_a(global_segment < os_aio_n_segments);
3619 if (global_segment == 0) {
3620 *array = os_aio_ibuf_array;
3623 }
else if (global_segment == 1) {
3624 *array = os_aio_log_array;
3627 }
else if (global_segment < os_aio_read_array->n_segments + 2) {
3628 *array = os_aio_read_array;
3630 segment = global_segment - 2;
3632 *array = os_aio_write_array;
3634 segment = global_segment - (os_aio_read_array->n_segments + 2);
3646os_aio_array_reserve_slot(
3649 os_aio_array_t* array,
3665 os_aio_slot_t* slot = NULL;
3667 OVERLAPPED* control;
3669#elif defined(LINUX_NATIVE_AIO)
3677 ulint slots_per_seg;
3681 ut_a((len & 0xFFFFFFFFUL) == len);
3690 local_seg = (offset >> (UNIV_PAGE_SIZE_SHIFT + 6))
3699 if (!srv_use_native_aio) {
3714 for (i = local_seg * slots_per_seg, counter = 0;
3715 counter < array->
n_slots; i++, counter++) {
3718 slot = os_aio_array_get_nth_slot(array, i);
3748 slot->
buf =
static_cast<unsigned char *
>(buf);
3754 control = &(slot->control);
3755 control->Offset = (DWORD)offset;
3756 control->OffsetHigh = (DWORD)offset_high;
3757 ResetEvent(slot->handle);
3759#elif defined(LINUX_NATIVE_AIO)
3762 if (!srv_use_native_aio) {
3763 goto skip_native_aio;
3768 if (
sizeof(aio_offset) == 8) {
3769 aio_offset = offset_high;
3771 aio_offset += offset;
3773 ut_a(offset_high == 0);
3774 aio_offset = offset;
3777 iocb = &slot->control;
3780 io_prep_pread(iocb, file, buf, len, aio_offset);
3782 ut_a(type == OS_FILE_WRITE);
3783 io_prep_pwrite(iocb, file, buf, len, aio_offset);
3786 iocb->data = (
void*)slot;
3803os_aio_array_free_slot(
3805 os_aio_array_t* array,
3806 os_aio_slot_t* slot)
3829 ResetEvent(slot->handle);
3831#elif defined(LINUX_NATIVE_AIO)
3833 if (srv_use_native_aio) {
3834 memset(&slot->control, 0x0,
sizeof(slot->control));
3841 ut_ad(slot->n_bytes == 0);
3842 ut_ad(slot->ret == 0);
3853os_aio_simulated_wake_handler_thread(
3855 ulint global_segment)
3858 os_aio_array_t* array;
3859 os_aio_slot_t* slot;
3864 ut_ad(!srv_use_native_aio);
3866 segment = os_aio_get_array_and_local_segment(&array, global_segment);
3874 for (i = 0; i < n; i++) {
3875 slot = os_aio_array_get_nth_slot(array, i + segment * n);
3887 os_event_set(os_aio_segment_wait_events[global_segment]);
3900 if (srv_use_native_aio) {
3906 os_aio_recommend_sleep_for_read_threads = FALSE;
3908 for (i = 0; i < os_aio_n_segments; i++) {
3909 os_aio_simulated_wake_handler_thread(i);
3929 os_aio_array_t* array;
3932 if (srv_use_native_aio) {
3938 os_aio_recommend_sleep_for_read_threads = TRUE;
3940 for (g = 0; g < os_aio_n_segments; g++) {
3941 os_aio_get_array_and_local_segment(&array, g);
3943 if (array == os_aio_read_array) {
3951#if defined(LINUX_NATIVE_AIO)
3957os_aio_linux_dispatch(
3959 os_aio_array_t* array,
3960 os_aio_slot_t* slot)
3966 ut_ad(slot != NULL);
3975 iocb = &slot->control;
3978 ret = io_submit(array->aio_ctx[io_ctx_index], 1, &iocb);
3980#if defined(UNIV_AIO_DEBUG)
3982 "io_submit[%c] ret[%d]: slot[%p] ctx[%p] seg[%lu]\n",
3983 (slot->
type == OS_FILE_WRITE) ?
'w' :
'r', ret, slot,
3984 array->aio_ctx[io_ctx_index], (ulong)io_ctx_index);
3989 if (UNIV_UNLIKELY(ret != 1)) {
4040 os_aio_array_t* array;
4041 os_aio_slot_t* slot;
4045 DWORD len = (DWORD) n;
4050#if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO
4060 ut_ad(os_aio_validate_skip());
4062 ut_ad((n & 0xFFFFFFFFUL) == n);
4070 && !srv_use_native_aio
4081 return(os_file_read(file, buf, offset,
4085 ut_a(type == OS_FILE_WRITE);
4087 return(os_file_write(
name, file, buf, offset, offset_high, n));
4090#if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO
4095 array = os_aio_read_array;
4097 array = os_aio_write_array;
4106 array = os_aio_ibuf_array;
4109 array = os_aio_log_array;
4111 array = os_aio_sync_array;
4113#if defined(LINUX_NATIVE_AIO)
4115 ut_a(!srv_use_native_aio);
4122 slot = os_aio_array_reserve_slot(type, array, message1, message2, file,
4123 name, buf, offset, offset_high, n);
4125 if (srv_use_native_aio) {
4127 os_bytes_read_since_printout += n;
4129 ret = ReadFile(file, buf, (DWORD)n, &len,
4132#elif defined(LINUX_NATIVE_AIO)
4133 if (!os_aio_linux_dispatch(array, slot)) {
4139 os_aio_simulated_wake_handler_thread(
4140 os_aio_get_segment_no_from_slot(
4144 }
else if (type == OS_FILE_WRITE) {
4145 if (srv_use_native_aio) {
4148 ret = WriteFile(file, buf, (DWORD)n, &len,
4151#elif defined(LINUX_NATIVE_AIO)
4152 if (!os_aio_linux_dispatch(array, slot)) {
4158 os_aio_simulated_wake_handler_thread(
4159 os_aio_get_segment_no_from_slot(
4168 if (srv_use_native_aio) {
4169 if ((ret && len == n)
4170 || (!ret && GetLastError() == ERROR_IO_PENDING)) {
4179 retval = os_aio_windows_handle(ULINT_UNDEFINED,
4197#if defined LINUX_NATIVE_AIO || defined WIN_ASYNC_IO
4199 os_aio_array_free_slot(array, slot);
4201 retry = os_file_handle_error(
name,
4203 ?
"aio read" :
"aio write");
4224os_aio_windows_handle(
4244 ulint orig_seg = segment;
4245 os_aio_array_t* array;
4246 os_aio_slot_t* slot;
4254 if (segment == ULINT_UNDEFINED) {
4255 array = os_aio_sync_array;
4258 segment = os_aio_get_array_and_local_segment(&array, segment);
4264 ut_ad(os_aio_validate_skip());
4265 ut_ad(segment < array->n_segments);
4269 if (array == os_aio_sync_array) {
4270 WaitForSingleObject(
4271 os_aio_array_get_nth_slot(array, pos)->
handle,
4276 i = WaitForMultipleObjects((DWORD) n,
4277 array->handles + segment * n,
4288 slot = os_aio_array_get_nth_slot(array, i + segment * n);
4292 if (orig_seg != ULINT_UNDEFINED) {
4294 "get windows aio return value");
4297 ret = GetOverlappedResult(slot->
file, &(slot->control), &len, TRUE);
4304 if (ret && len == slot->
len) {
4308 if (slot->
type == OS_FILE_WRITE
4309 && !os_do_not_call_flush_at_each_write) {
4310 if (!os_file_flush(slot->
file)) {
4315 }
else if (os_file_handle_error(slot->
name,
"Windows aio")) {
4333 struct PSI_file_locker* locker = NULL;
4334 register_pfs_file_io_begin(locker, slot->
file, slot->
len,
4335 (slot->
type == OS_FILE_WRITE)
4338 __FILE__, __LINE__);
4341 ut_a((slot->
len & 0xFFFFFFFFUL) == slot->
len);
4343 switch (slot->
type) {
4345 ret = WriteFile(slot->
file, slot->
buf,
4346 (DWORD) slot->
len, &len,
4351 ret = ReadFile(slot->
file, slot->
buf,
4352 (DWORD) slot->
len, &len,
4361 register_pfs_file_io_end(locker, len);
4364 if (!ret && GetLastError() == ERROR_IO_PENDING) {
4371 ret = GetOverlappedResult(slot->
file,
4376 ret_val = ret && len == slot->
len;
4379 os_aio_array_free_slot(array, slot);
4385#if defined(LINUX_NATIVE_AIO)
4399os_aio_linux_collect(
4401 os_aio_array_t* array,
4409 struct timespec timeout;
4410 struct io_event* events;
4411 struct io_context* io_ctx;
4414 ut_ad(array != NULL);
4415 ut_ad(seg_size > 0);
4416 ut_ad(segment < array->n_segments);
4419 events = &array->aio_events[segment * seg_size];
4422 io_ctx = array->aio_ctx[segment];
4425 start_pos = segment * seg_size;
4428 end_pos = start_pos + seg_size;
4442 memset(events, 0,
sizeof(*events) * seg_size);
4444 timeout.tv_nsec = OS_AIO_REAP_TIMEOUT;
4446 ret = io_getevents(io_ctx, 1, seg_size, events, &timeout);
4453 if (ret == -EAGAIN) {
4461 if (ret == -EINTR) {
4471 if (UNIV_UNLIKELY(ret < 0)) {
4474 " InnoDB: unexpected ret_code[%d] from"
4475 " io_getevents()!\n", ret);
4481 for (i = 0; i < ret; i++) {
4482 os_aio_slot_t* slot;
4483 struct iocb* control;
4485 control = (
struct iocb *)events[i].obj;
4486 ut_a(control != NULL);
4488 slot = (os_aio_slot_t *) control->data;
4494#if defined(UNIV_AIO_DEBUG)
4496 "io_getevents[%c]: slot[%p] ctx[%p]"
4498 (slot->
type == OS_FILE_WRITE) ?
'w' :
'r',
4499 slot, io_ctx, segment);
4511 slot->n_bytes = events[i].res;
4512 slot->ret = events[i].res2;
4546 os_aio_array_t* array;
4547 os_aio_slot_t* slot;
4553 ut_a(global_seg != ULINT_UNDEFINED);
4556 segment = os_aio_get_array_and_local_segment(&array, global_seg);
4562 for (i = 0; i < n; ++i) {
4563 slot = os_aio_array_get_nth_slot(
4564 array, i + segment * n);
4578 "waiting for completed aio requests");
4579 os_aio_linux_collect(array, segment, n);
4588 "processing completed aio requests");
4593 ut_ad(slot != NULL);
4602 if ((slot->ret == 0) && (slot->n_bytes == (
long)slot->
len)) {
4606 if (slot->
type == OS_FILE_WRITE
4607 && !os_do_not_call_flush_at_each_write)
4608 && !os_file_flush(slot->
file) {
4622 os_file_handle_error(slot->
name,
"Linux aio");
4629 os_aio_array_free_slot(array, slot);
4643 ulint global_segment,
4656 os_aio_array_t* array;
4658 os_aio_slot_t* slot;
4659 os_aio_slot_t* slot2;
4660 os_aio_slot_t* consecutive_ios[OS_AIO_MERGE_N_CONSECUTIVE];
4661 ulint n_consecutive;
4664 ulint lowest_offset;
4668 byte* combined_buf2;
4674 *consecutive_ios = NULL;
4676 memset(consecutive_ios, 0,
sizeof(os_aio_slot_t*) * OS_AIO_MERGE_N_CONSECUTIVE);
4677 segment = os_aio_get_array_and_local_segment(&array, global_segment);
4684 "looking for i/o requests (a)");
4685 ut_ad(os_aio_validate_skip());
4686 ut_ad(segment < array->n_segments);
4692 if (array == os_aio_read_array
4693 && os_aio_recommend_sleep_for_read_threads) {
4698 goto recommended_sleep;
4704 "looking for i/o requests (b)");
4709 for (i = 0; i < n; i++) {
4710 slot = os_aio_array_get_nth_slot(array, i + segment * n);
4716 "InnoDB: i/o for slot %lu"
4717 " already done, returning\n",
4734 lowest_offset = ULINT_MAX;
4736 for (i = 0; i < n; i++) {
4737 slot = os_aio_array_get_nth_slot(array, i + segment * n);
4740 age = (ulint)difftime(time(NULL),
4743 if ((age >= 2 && age > biggest_age)
4744 || (age >= 2 && age == biggest_age
4745 && slot->
offset < lowest_offset)) {
4748 consecutive_ios[0] = slot;
4753 lowest_offset = slot->
offset;
4758 if (n_consecutive == 0) {
4763 lowest_offset = ULINT_MAX;
4765 for (i = 0; i < n; i++) {
4766 slot = os_aio_array_get_nth_slot(array,
4772 consecutive_ios[0] = slot;
4776 lowest_offset = slot->
offset;
4781 if (n_consecutive == 0) {
4790 ut_ad(n_consecutive != 0);
4791 ut_ad(consecutive_ios[0] != NULL);
4793 slot = consecutive_ios[0];
4798 for (i = 0; i < n; i++) {
4799 slot2 = os_aio_array_get_nth_slot(array, i + segment * n);
4801 if (slot2->
reserved && slot2 != slot
4811 consecutive_ios[n_consecutive] = slot2;
4816 if (n_consecutive < OS_AIO_MERGE_N_CONSECUTIVE) {
4818 goto consecutive_loop;
4832 slot = consecutive_ios[0];
4834 for (i = 0; i < n_consecutive; i++) {
4835 total_len += consecutive_ios[i]->
len;
4838 if (n_consecutive == 1) {
4840 combined_buf = slot->
buf;
4841 combined_buf2 = NULL;
4843 combined_buf2 =
static_cast<unsigned char *
>(
ut_malloc(total_len + UNIV_PAGE_SIZE));
4845 ut_a(combined_buf2);
4847 combined_buf =
static_cast<unsigned char *
>(
ut_align(combined_buf2, UNIV_PAGE_SIZE));
4856 if (slot->
type == OS_FILE_WRITE && n_consecutive > 1) {
4860 for (i = 0; i < n_consecutive; i++) {
4862 ut_memcpy(combined_buf + offs, consecutive_ios[i]->buf,
4863 consecutive_ios[i]->len);
4864 offs += consecutive_ios[i]->
len;
4872 "InnoDB: doing i/o of type %lu at offset %lu %lu,"
4875 (ulong) slot->
offset, (ulong) total_len);
4879 if (slot->
type == OS_FILE_WRITE) {
4880 ret = os_file_write(slot->
name, slot->
file, combined_buf,
4884 ret = os_file_read(slot->
file, combined_buf,
4893 "aio: %lu consecutive %lu:th segment, first offs %lu blocks\n",
4894 n_consecutive, global_segment, slot->
offset / UNIV_PAGE_SIZE);
4901 for (i = 0; i < n_consecutive; i++) {
4903 ut_memcpy(consecutive_ios[i]->buf, combined_buf + offs,
4904 consecutive_ios[i]->len);
4905 offs += consecutive_ios[i]->
len;
4909 if (combined_buf2) {
4917 for (i = 0; i < n_consecutive; i++) {
4936 os_aio_array_free_slot(array, slot);
4953 os_event_wait(os_aio_segment_wait_events[global_segment]);
4957 "InnoDB: i/o handler thread for i/o"
4958 " segment %lu wakes up\n",
4959 (ulong) global_segment);
4970os_aio_array_validate(
4972 os_aio_array_t* array)
4974 os_aio_slot_t* slot;
4975 ulint n_reserved = 0;
4985 for (i = 0; i < array->
n_slots; i++) {
4986 slot = os_aio_array_get_nth_slot(array, i);
5009 os_aio_array_validate(os_aio_read_array);
5010 os_aio_array_validate(os_aio_write_array);
5011 os_aio_array_validate(os_aio_ibuf_array);
5012 os_aio_array_validate(os_aio_log_array);
5013 os_aio_array_validate(os_aio_sync_array);
5025os_aio_print_segment_info(
5029 os_aio_array_t* array)
5041 fprintf(file,
" [");
5044 fprintf(file,
", ");
5047 fprintf(file,
"%lu", n_seg[i]);
5049 fprintf(file,
"] ");
5060 os_aio_array_t* array;
5061 os_aio_slot_t* slot;
5063 ulint n_res_seg[SRV_MAX_N_IO_THREADS];
5064 time_t current_time;
5065 double time_elapsed;
5066 double avg_bytes_read;
5069 for (i = 0; i < srv_n_file_io_threads; i++) {
5070 fprintf(file,
"I/O thread %lu state: %s (%s)", (ulong) i,
5071 srv_io_thread_op_info[i],
5072 srv_io_thread_function[i]);
5075 if (os_aio_segment_wait_events[i]->is_set) {
5076 fprintf(file,
" ev set");
5080 fprintf(file,
"\n");
5083 fputs(
"Pending normal aio reads:", file);
5085 array = os_aio_read_array;
5096 memset(n_res_seg, 0x0,
sizeof(n_res_seg));
5098 for (i = 0; i < array->
n_slots; i++) {
5101 slot = os_aio_array_get_nth_slot(array, i);
5106 n_res_seg[seg_no]++;
5108 fprintf(stderr,
"Reserved slot, messages %p %p\n",
5118 fprintf(file,
" %lu", (ulong) n_reserved);
5120 os_aio_print_segment_info(file, n_res_seg, array);
5124 if (array == os_aio_read_array) {
5125 fputs(
", aio writes:", file);
5127 array = os_aio_write_array;
5132 if (array == os_aio_write_array) {
5133 fputs(
",\n ibuf aio reads:", file);
5134 array = os_aio_ibuf_array;
5139 if (array == os_aio_ibuf_array) {
5140 fputs(
", log i/o's:", file);
5141 array = os_aio_log_array;
5146 if (array == os_aio_log_array) {
5147 fputs(
", sync i/o's:", file);
5148 array = os_aio_sync_array;
5154 current_time = time(NULL);
5155 time_elapsed = 0.001 + difftime(current_time, os_last_printout);
5158 "Pending flushes (fsync) log: %lu; buffer pool: %lu\n"
5159 "%lu OS file reads, %lu OS file writes, %lu OS fsyncs\n",
5160 (ulong) fil_n_pending_log_flushes,
5161 (ulong) fil_n_pending_tablespace_flushes,
5162 (ulong) os_n_file_reads, (ulong) os_n_file_writes,
5163 (ulong) os_n_fsyncs);
5167 "%lu pending preads, %lu pending pwrites\n",
5172 if (os_n_file_reads == os_n_file_reads_old) {
5173 avg_bytes_read = 0.0;
5175 avg_bytes_read = (double) os_bytes_read_since_printout
5176 / (os_n_file_reads - os_n_file_reads_old);
5180 "%.2f reads/s, %lu avg bytes/read,"
5181 " %.2f writes/s, %.2f fsyncs/s\n",
5182 (os_n_file_reads - os_n_file_reads_old)
5184 (ulong)avg_bytes_read,
5185 (os_n_file_writes - os_n_file_writes_old)
5187 (os_n_fsyncs - os_n_fsyncs_old)
5190 os_n_file_reads_old = os_n_file_reads;
5191 os_n_file_writes_old = os_n_file_writes;
5192 os_n_fsyncs_old = os_n_fsyncs;
5193 os_bytes_read_since_printout = 0;
5195 os_last_printout = current_time;
5205 os_n_file_reads_old = os_n_file_reads;
5206 os_n_file_writes_old = os_n_file_writes;
5207 os_n_fsyncs_old = os_n_fsyncs;
5208 os_bytes_read_since_printout = 0;
5210 os_last_printout = time(NULL);
5220os_aio_all_slots_free(
void)
5223 os_aio_array_t* array;
5226 array = os_aio_read_array;
5234 array = os_aio_write_array;
5242 array = os_aio_ibuf_array;
5250 array = os_aio_log_array;
5258 array = os_aio_sync_array;
UNIV_INTERN int innobase_mysql_tmpfile(void)
UNIV_INLINE char * mem_strdup(const char *str)
UNIV_INLINE char * mem_strdupl(const char *str, ulint len)
UNIV_INTERN ibool os_file_read_no_error_handling_func(os_file_t file, void *buf, ulint offset, ulint offset_high, ulint n)
UNIV_INTERN ibool os_file_status(const char *path, ibool *exists, os_file_type_t *type)
UNIV_INTERN ulint os_file_get_last_error(ibool report_all_errors)
UNIV_INTERN int os_file_closedir(os_file_dir_t dir)
UNIV_INTERN ibool os_file_write_func(const char *name, os_file_t file, const void *buf, ulint offset, ulint offset_high, ulint n)
UNIV_INTERN char * os_file_dirname(const char *path)
struct fil_node_struct fil_node_t
UNIV_INTERN ibool os_aio_simulated_handle(ulint segment, fil_node_t **message1, void **message2, ulint *type)
UNIV_INTERN ibool os_file_close_func(os_file_t file)
UNIV_INTERN ibool os_file_read_func(os_file_t file, void *buf, ulint offset, ulint offset_high, ulint n)
UNIV_INTERN void os_file_set_nocache(int fd, const char *file_name, const char *operation_name)
UNIV_INTERN os_file_t os_file_create_func(const char *name, ulint create_mode, ulint purpose, ulint type, ibool *success)
UNIV_INTERN void os_aio_simulated_put_read_threads_to_sleep(void)
UNIV_INTERN void os_aio_simulated_wake_handler_threads(void)
FILE * os_file_create_tmpfile(void)
UNIV_INTERN os_file_t os_file_create_simple_func(const char *name, ulint create_mode, ulint access_type, ibool *success)
UNIV_INTERN ibool os_file_rename_func(const char *oldpath, const char *newpath)
UNIV_INTERN int os_file_readdir_next_file(const char *dirname, os_file_dir_t dir, os_file_stat_t *info)
UNIV_INTERN void os_aio_print(FILE *file)
UNIV_INTERN void os_aio_wait_until_no_pending_writes(void)
ulint os_file_n_pending_pwrites
UNIV_INTERN os_file_t os_file_create_simple_no_error_handling_func(const char *name, ulint create_mode, ulint access_type, ibool *success)
UNIV_INTERN void os_aio_wake_all_threads_at_shutdown(void)
UNIV_INTERN ibool os_file_get_size(os_file_t file, ulint *size, ulint *size_high)
ulint os_n_pending_writes
UNIV_INTERN ibool os_file_flush_func(os_file_t file)
UNIV_INTERN ibool os_aio_func(ulint type, ulint mode, const char *name, os_file_t file, void *buf, ulint offset, ulint offset_high, ulint n, fil_node_t *message1, void *message2)
#define OS_FILE_LOG_BLOCK_SIZE
UNIV_INTERN void os_file_read_string(FILE *file, char *str, ulint size)
UNIV_INTERN ibool os_file_create_subdirs_if_needed(const char *path)
UNIV_INTERN ibool os_file_set_size(const char *name, os_file_t file, ulint size, ulint size_high)
UNIV_INTERN ibool os_file_set_eof(FILE *file)
UNIV_INTERN ibool os_file_get_status(const char *path, os_file_stat_t *stat_info)
UNIV_INTERN ibool os_file_delete_if_exists(const char *name)
ulint os_file_n_pending_preads
#define OS_FILE_NOT_FOUND
#define OS_AIO_SIMULATED_WAKE_LATER
UNIV_INTERN void os_aio_refresh_stats(void)
UNIV_INTERN ibool os_file_delete(const char *name)
UNIV_INTERN void os_io_init_simple(void)
UNIV_INTERN ibool os_aio_validate(void)
UNIV_INTERN ib_int64_t os_file_get_size_as_iblonglong(os_file_t file)
UNIV_INTERN ibool os_file_create_directory(const char *pathname, ibool fail_if_exists)
UNIV_INTERN os_file_dir_t os_file_opendir(const char *dirname, ibool error_is_fatal)
UNIV_INTERN ib_int64_t os_event_reset(os_event_t event)
UNIV_INTERN void os_mutex_enter(os_mutex_t mutex)
UNIV_INTERN os_event_t os_event_create(const char *name)
UNIV_INTERN void os_event_set(os_event_t event)
UNIV_INTERN void os_event_free(os_event_t event)
os_event_struct_t * os_event_t
UNIV_INTERN os_mutex_t os_mutex_create(void)
os_mutex_str_t * os_mutex_t
UNIV_INTERN void os_mutex_exit(os_mutex_t mutex)
UNIV_INTERN void os_mutex_free(os_mutex_t mutex)
UNIV_INTERN void os_thread_exit(void *exit_value)
UNIV_INTERN void os_thread_sleep(ulint tm)
my_bool srv_file_per_table
UNIV_INTERN void srv_set_io_thread_op_info(ulint i, const char *str)
@ SRV_SHUTDOWN_EXIT_THREADS
ibool srv_is_being_started
ibool srv_start_raw_disk_in_use
char name[OS_FILE_MAX_PATH]
UNIV_INLINE void * ut_align(const void *ptr, ulint align_no)
UNIV_INLINE void * ut_memcpy(void *dest, const void *sour, ulint n)
UNIV_INTERN void ut_free(void *ptr)
UNIV_INTERN void * ut_malloc(ulint n)
UNIV_INLINE ulint ut_min(ulint n1, ulint n2)
UNIV_INTERN void ut_print_timestamp(FILE *file)