os0file.c 74.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/******************************************************
The interface to the operating system file i/o primitives

(c) 1995 Innobase Oy

Created 10/21/1995 Heikki Tuuri
*******************************************************/

#include "os0file.h"
#include "os0sync.h"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
11
#include "os0thread.h"
12
#include "ut0mem.h"
13
#include "srv0srv.h"
14
#include "fil0fil.h"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
15
#include "buf0buf.h"
16

17
#undef HAVE_FDATASYNC
18 19 20 21 22 23 24

#ifdef POSIX_ASYNC_IO
/* We assume in this case that the OS has standard Posix aio (at least SunOS
2.6, HP-UX 11i and AIX 4.3 have) */

#endif

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
25
/* This specifies the file permissions InnoDB uses when it creates files in
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
26 27 28 29 30 31 32 33 34
Unix; the value of os_innodb_umask is initialized in ha_innodb.cc to
my_umask */

#ifndef __WIN__
ulint	os_innodb_umask		= S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
#else
ulint	os_innodb_umask		= 0;
#endif

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
35 36 37 38
/* If the following is set to TRUE, we do not call os_file_flush in every
os_file_write. We can set this TRUE if the doublewrite buffer is used. */
ibool	os_do_not_call_flush_at_each_write	= FALSE;

39 40 41 42 43 44
/* We use these mutexes to protect lseek + file i/o operation, if the
OS does not provide an atomic pread or pwrite, or similar */
#define OS_FILE_N_SEEK_MUTEXES	16
os_mutex_t	os_file_seek_mutexes[OS_FILE_N_SEEK_MUTEXES];

/* In simulated aio, merge at most this many consecutive i/os */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
45
#define OS_AIO_MERGE_N_CONSECUTIVE	64
46 47 48 49 50 51 52

/* If this flag is TRUE, then we will use the native aio of the
OS (provided we compiled Innobase with it in), otherwise we will
use simulated aio we build below with threads */

ibool	os_aio_use_native_aio	= FALSE;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
53 54
ibool	os_aio_print_debug	= FALSE;

55 56 57 58 59 60 61 62
/* The aio array slot structure */
typedef struct os_aio_slot_struct	os_aio_slot_t;

struct os_aio_slot_struct{
	ibool		is_read;	/* TRUE if a read operation */
	ulint		pos;		/* index of the slot in the aio
					array */
	ibool		reserved;	/* TRUE if this slot is reserved */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
63
	time_t		reservation_time;/* time when reserved */
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	ulint		len;		/* length of the block to read or
					write */
	byte*		buf;		/* buffer used in i/o */
	ulint		type;		/* OS_FILE_READ or OS_FILE_WRITE */
	ulint		offset;		/* 32 low bits of file offset in
					bytes */
	ulint		offset_high;	/* 32 high bits of file offset */
	os_file_t	file;		/* file where to read or write */
	char*		name;		/* file name or path */
	ibool		io_already_done;/* used only in simulated aio:
					TRUE if the physical i/o already
					made and only the slot message
					needs to be passed to the caller
					of os_aio_simulated_handle */
	void*		message1;	/* message which is given by the */
	void*		message2;	/* the requester of an aio operation
					and which can be used to identify
					which pending aio operation was
					completed */
#ifdef WIN_ASYNC_IO
84 85
        os_event_t	event;		/* event object we need in the
					OVERLAPPED struct */
86 87 88 89 90 91 92 93 94 95 96 97 98
	OVERLAPPED	control;	/* Windows control block for the
					aio request */
#elif defined(POSIX_ASYNC_IO)
	struct aiocb	control;	/* Posix control block for aio
					request */
#endif
};

/* The aio array structure */
typedef struct os_aio_array_struct	os_aio_array_t;

struct os_aio_array_struct{
	os_mutex_t	mutex;	  /* the mutex protecting the aio array */
99
	os_event_t	not_full; /* The event which is set to the signaled
100 101
				  state when there is space in the aio
				  outside the ibuf segment */
102 103 104
	os_event_t	is_empty; /* The event which is set to the signaled
				  state when there are no pending i/os
				  in this array */
105 106 107 108 109 110 111 112
	ulint		n_slots;  /* Total number of slots in the aio array.
				  This must be divisible by n_threads. */
	ulint		n_segments;/* Number of segments in the aio array of
				  pending aio requests. A thread can wait
				  separately for any one of the segments. */
	ulint		n_reserved;/* Number of reserved slots in the
				  aio array outside the ibuf segment */
	os_aio_slot_t* 	slots;	  /* Pointer to the slots in the array */
113 114 115 116 117 118
#ifdef __WIN__
	os_native_event_t* native_events;	 
				  /* Pointer to an array of OS native event
				  handles where we copied the handles from
				  slots, in the same order. This can be used
				  in WaitForMultipleObjects; used only in
119
				  Windows */
120
#endif
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
};

/* Array of events used in simulated aio */
os_event_t*	os_aio_segment_wait_events	= NULL;

/* The aio arrays for non-ibuf i/o and ibuf i/o, as well as sync aio. These
are NULL when the module has not yet been initialized. */
os_aio_array_t*	os_aio_read_array	= NULL;
os_aio_array_t*	os_aio_write_array	= NULL;
os_aio_array_t*	os_aio_ibuf_array	= NULL;
os_aio_array_t*	os_aio_log_array	= NULL;
os_aio_array_t*	os_aio_sync_array	= NULL;

ulint	os_aio_n_segments	= ULINT_UNDEFINED;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
136 137 138 139
/* If the following is TRUE, read i/o handler threads try to
wait until a batch of new read requests have been posted */
ibool	os_aio_recommend_sleep_for_read_threads	= FALSE;

140
ulint	os_n_file_reads		= 0;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
141
ulint	os_bytes_read_since_printout = 0;
142 143 144 145 146 147 148
ulint	os_n_file_writes	= 0;
ulint	os_n_fsyncs		= 0;
ulint	os_n_file_reads_old	= 0;
ulint	os_n_file_writes_old	= 0;
ulint	os_n_fsyncs_old		= 0;
time_t	os_last_printout;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
149 150
ibool	os_has_said_disk_full	= FALSE;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
151 152 153 154 155 156
/* The mutex protecting the following counts of pending pread and pwrite
operations */
os_mutex_t os_file_count_mutex;
ulint	os_file_n_pending_preads  = 0;
ulint	os_file_n_pending_pwrites = 0;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
157

158 159 160 161 162 163
/***************************************************************************
Gets the operating system version. Currently works only on Windows. */

ulint
os_get_os_version(void)
/*===================*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
164
                  /* out: OS_WIN95, OS_WIN31, OS_WINNT, OS_WIN2000 */
165 166
{
#ifdef __WIN__
167 168 169 170 171 172 173 174 175 176 177
  	OSVERSIONINFO     os_info;

  	os_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

	ut_a(GetVersionEx(&os_info));

  	if (os_info.dwPlatformId == VER_PLATFORM_WIN32s) {
    		return(OS_WIN31);
  	} else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
    		return(OS_WIN95);
  	} else if (os_info.dwPlatformId == VER_PLATFORM_WIN32_NT) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
178 179 180 181 182
		if (os_info.dwMajorVersion <= 4) {
    			return(OS_WINNT);
    		} else {
			return(OS_WIN2000);
    		}
183 184 185 186
  	} else {
    		ut_error;
    		return(0);
  	}
187
#else
188
  	ut_error;
189

190
  	return(0);
191 192 193
#endif
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/***************************************************************************
Retrieves the last error number if an error occurs in a file io function.
The number should be retrieved before any other OS calls (because they may
overwrite the error number). If the number is not known to this program,
the OS error number + 100 is returned. */

ulint
os_file_get_last_error(void)
/*========================*/
		/* out: error number, or OS error number + 100 */
{
	ulint	err;

#ifdef __WIN__

	err = (ulint) GetLastError();

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
211
	if (err != ERROR_DISK_FULL && err != ERROR_FILE_EXISTS) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
212 213
		ut_print_timestamp(stderr);
	     	fprintf(stderr,
214
  "  InnoDB: Operating system error number %lu in a file operation.\n"
215
  "InnoDB: See http://www.innodb.com/ibman.php for installation help.\n",
216
		err);
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
217

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
218
		if (err == ERROR_PATH_NOT_FOUND) {
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
219 220 221 222
		         fprintf(stderr,
  "InnoDB: The error means the system cannot find the path specified.\n"
  "InnoDB: In installation you must create directories yourself, InnoDB\n"
  "InnoDB: does not create them.\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
223
		} else if (err == ERROR_ACCESS_DENIED) {
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
224 225 226 227
		         fprintf(stderr,
  "InnoDB: The error means mysqld does not have the access rights to\n"
  "InnoDB: the directory. It may also be you have created a subdirectory\n"
  "InnoDB: of the same name as a data file.\n"); 
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
228
		} else {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
229
			 fprintf(stderr,
230
  "InnoDB: See section 13.2 at http://www.innodb.com/ibman.php\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
231
  "InnoDB: about operating system error numbers.\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
232
		}
233
	}
234

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
235 236
	fflush(stderr);

237 238 239 240 241 242 243 244 245 246
	if (err == ERROR_FILE_NOT_FOUND) {
		return(OS_FILE_NOT_FOUND);
	} else if (err == ERROR_DISK_FULL) {
		return(OS_FILE_DISK_FULL);
	} else if (err == ERROR_FILE_EXISTS) {
		return(OS_FILE_ALREADY_EXISTS);
	} else {
		return(100 + err);
	}
#else
247 248
	err = (ulint) errno;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
249
	if (err != ENOSPC && err != EEXIST) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
250 251 252
		ut_print_timestamp(stderr);

	     	fprintf(stderr,
253
  "  InnoDB: Operating system error number %lu in a file operation.\n"
254
  "InnoDB: See http://www.innodb.com/ibman.php for installation help.\n",
255
		err);
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
256

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
257
		if (err == ENOENT) {
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
258 259 260 261
		         fprintf(stderr,
  "InnoDB: The error means the system cannot find the path specified.\n"
  "InnoDB: In installation you must create directories yourself, InnoDB\n"
  "InnoDB: does not create them.\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
262
		} else if (err == EACCES) {
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
263 264 265
		         fprintf(stderr,
  "InnoDB: The error means mysqld does not have the access rights to\n"
  "InnoDB: the directory.\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
266
		} else {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
267 268 269 270 271 272
			 if (strerror((int)err) != NULL) {
				fprintf(stderr,
  "InnoDB: Error number %lu means '%s'.\n", err, strerror((int)err));
			 }

			 fprintf(stderr,
273
  "InnoDB: See also section 13.2 at http://www.innodb.com/ibman.php\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
274
  "InnoDB: about operating system error numbers.\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
275
		}
276
	}
277

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
278 279
	fflush(stderr);

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	if (err == ENOSPC ) {
		return(OS_FILE_DISK_FULL);
#ifdef POSIX_ASYNC_IO
	} else if (err == EAGAIN) {
		return(OS_FILE_AIO_RESOURCES_RESERVED);
#endif
	} else if (err == ENOENT) {
		return(OS_FILE_NOT_FOUND);
	} else if (err == EEXIST) {
		return(OS_FILE_ALREADY_EXISTS);
	} else {
		return(100 + err);
	}
#endif
}

/********************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
297
Does error handling when a file operation fails. */
298 299 300 301
static
ibool
os_file_handle_error(
/*=================*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
302 303
				/* out: TRUE if we should retry the
				operation */
304
	const char*	name,	/* in: name of a file or NULL */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
305
	const char*	operation)/* in: operation */
306 307 308 309 310 311
{
	ulint	err;

	err = os_file_get_last_error();
	
	if (err == OS_FILE_DISK_FULL) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
312 313 314 315 316 317 318
		/* We only print a warning about disk full once */

		if (os_has_said_disk_full) {

			return(FALSE);
		}
	
319
		if (name) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
320 321 322
			ut_print_timestamp(stderr);
			fprintf(stderr,
	"  InnoDB: Encountered a problem with file %s\n", name);
323
		}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
324 325

		ut_print_timestamp(stderr);
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
326
	        fprintf(stderr,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
327
	"  InnoDB: Disk is full. Try to clean the disk to free space.\n");
328

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
329 330
		os_has_said_disk_full = TRUE;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
331 332
		fflush(stderr);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
333
		return(FALSE);
334

335 336
	} else if (err == OS_FILE_AIO_RESOURCES_RESERVED) {
		return(TRUE);
337 338

	} else if (err == OS_FILE_ALREADY_EXISTS) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
339

340
		return(FALSE);
341
	} else {
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
342 343 344
	        if (name) {
	                fprintf(stderr, "InnoDB: File name %s\n", name);
	        }
345
	  
346 347
		fprintf(stderr, "InnoDB: File operation call: '%s'.\n",
							       operation);
348 349
		fprintf(stderr, "InnoDB: Cannot continue operation.\n");

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
350 351
		fflush(stderr);

352
		exit(1);
353 354 355 356 357
	}

	return(FALSE);	
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
358 359 360 361 362 363 364 365 366
/********************************************************************
Creates the seek mutexes used in positioned reads and writes. */

void
os_io_init_simple(void)
/*===================*/
{
	ulint	i;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
367 368
	os_file_count_mutex = os_mutex_create(NULL);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
369 370 371 372 373
	for (i = 0; i < OS_FILE_N_SEEK_MUTEXES; i++) {
		os_file_seek_mutexes[i] = os_mutex_create(NULL);
	}
}

374 375 376 377 378 379 380 381 382 383
#ifndef UNIV_HOTBACKUP
/*************************************************************************
Creates a temporary file. This function is defined in ha_innodb.cc. */

int
innobase_mysql_tmpfile(void);
/*========================*/
			/* out: temporary file descriptor, or < 0 on error */
#endif /* !UNIV_HOTBACKUP */

384
/***************************************************************************
385
Creates a temporary file. */
386 387 388 389

FILE*
os_file_create_tmpfile(void)
/*========================*/
390
			/* out: temporary file handle, or NULL on error */
391
{
392
	FILE*	file	= NULL;
393
	int	fd	= -1;
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
#ifdef UNIV_HOTBACKUP
	int	tries;
	for (tries = 10; tries--; ) {
		char*	name = tempnam(fil_path_to_mysql_datadir, "ib");
		if (!name) {
			break;
		}

		fd = open(name,
# ifdef __WIN__
			O_SEQUENTIAL | O_SHORT_LIVED | O_TEMPORARY |
# endif /* __WIN__ */
			O_CREAT | O_EXCL | O_RDWR,
			S_IREAD | S_IWRITE);
		if (fd >= 0) {
# ifndef __WIN__
			unlink(name);
# endif /* !__WIN__ */
			free(name);
			break;
414
		}
415 416 417 418 419 420 421 422 423 424 425 426 427

		ut_print_timestamp(stderr);
		fprintf(stderr, "  InnoDB: Warning: "
			"unable to create temporary file %s, retrying\n",
			name);
		free(name);
	}
#else /* UNIV_HOTBACKUP */
	fd = innobase_mysql_tmpfile();
#endif /* UNIV_HOTBACKUP */

	if (fd >= 0) {
		file = fdopen(fd, "w+b");
428
	}
429 430

	if (!file) {
431
		ut_print_timestamp(stderr);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
432 433 434
		fprintf(stderr,
			"  InnoDB: Error: unable to create temporary file;"
			" errno: %d\n", errno);
435 436 437
		if (fd >= 0) {
			close(fd);
		}
438
	}
439

440 441 442
	return(file);
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
/********************************************************************
A simple function to open or create a file. */

os_file_t
os_file_create_simple(
/*==================*/
			/* out, own: handle to the file, not defined if error,
			error number can be retrieved with os_get_last_error */
	char*	name,	/* in: name of the file or path as a null-terminated
			string */
	ulint	create_mode,/* in: OS_FILE_OPEN if an existing file is opened
			(if does not exist, error), or OS_FILE_CREATE if a new
			file is created (if exists, error) */
	ulint	access_type,/* in: OS_FILE_READ_ONLY or OS_FILE_READ_WRITE */
	ibool*	success)/* out: TRUE if succeed, FALSE if error */
{
#ifdef __WIN__
	os_file_t	file;
	DWORD		create_flag;
	DWORD		access;
	DWORD		attributes	= 0;
	ibool		retry;
	
try_again:	
	ut_a(name);

	if (create_mode == OS_FILE_OPEN) {
		create_flag = OPEN_EXISTING;
	} else if (create_mode == OS_FILE_CREATE) {
		create_flag = CREATE_NEW;
	} else {
		create_flag = 0;
		ut_error;
	}

	if (access_type == OS_FILE_READ_ONLY) {
		access = GENERIC_READ;
	} else if (access_type == OS_FILE_READ_WRITE) {
		access = GENERIC_READ | GENERIC_WRITE;
	} else {
		access = 0;
		ut_error;
	}

	file = CreateFile(name,
			access,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
489 490
			FILE_SHARE_READ,/* file can be read also by other
					processes */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
491 492 493 494 495 496 497 498
			NULL,	/* default security attributes */
			create_flag,
			attributes,
			NULL);	/* no template file */

	if (file == INVALID_HANDLE_VALUE) {
		*success = FALSE;

499
		retry = os_file_handle_error(name,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
500 501
				create_mode == OS_FILE_OPEN ?
				"open" : "create");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
502 503 504 505 506 507 508 509
		if (retry) {
			goto try_again;
		}
	} else {
		*success = TRUE;
	}

	return(file);
510
#else /* __WIN__ */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	os_file_t	file;
	int		create_flag;
	ibool		retry;
	
try_again:	
	ut_a(name);

	if (create_mode == OS_FILE_OPEN) {
		if (access_type == OS_FILE_READ_ONLY) {
			create_flag = O_RDONLY;
		} else {
			create_flag = O_RDWR;
		}
	} else if (create_mode == OS_FILE_CREATE) {
		create_flag = O_RDWR | O_CREAT | O_EXCL;
	} else {
		create_flag = 0;
		ut_error;
	}

	if (create_mode == OS_FILE_CREATE) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
532 533
	        file = open(name, create_flag, S_IRUSR | S_IWUSR
						| S_IRGRP | S_IWGRP);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
534 535 536 537 538 539 540
        } else {
                file = open(name, create_flag);
        }
	
	if (file == -1) {
		*success = FALSE;

541
		retry = os_file_handle_error(name,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
542 543
				create_mode == OS_FILE_OPEN ?
				"open" : "create");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
544 545 546 547 548 549 550 551
		if (retry) {
			goto try_again;
		}
	} else {
		*success = TRUE;
	}

	return(file);	
552
#endif /* __WIN__ */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
553
}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

/********************************************************************
A simple function to open or create a file. */

os_file_t
os_file_create_simple_no_error_handling(
/*====================================*/
			/* out, own: handle to the file, not defined if error,
			error number can be retrieved with os_get_last_error */
	char*	name,	/* in: name of the file or path as a null-terminated
			string */
	ulint	create_mode,/* in: OS_FILE_OPEN if an existing file is opened
			(if does not exist, error), or OS_FILE_CREATE if a new
			file is created (if exists, error) */
	ulint	access_type,/* in: OS_FILE_READ_ONLY or OS_FILE_READ_WRITE */
	ibool*	success)/* out: TRUE if succeed, FALSE if error */
{
#ifdef __WIN__
	os_file_t	file;
	DWORD		create_flag;
	DWORD		access;
	DWORD		attributes	= 0;
	
	ut_a(name);

	if (create_mode == OS_FILE_OPEN) {
		create_flag = OPEN_EXISTING;
	} else if (create_mode == OS_FILE_CREATE) {
		create_flag = CREATE_NEW;
	} else {
		create_flag = 0;
		ut_error;
	}

	if (access_type == OS_FILE_READ_ONLY) {
		access = GENERIC_READ;
	} else if (access_type == OS_FILE_READ_WRITE) {
		access = GENERIC_READ | GENERIC_WRITE;
	} else {
		access = 0;
		ut_error;
	}

	file = CreateFile(name,
			access,
			FILE_SHARE_READ,/* file can be read also by other
					processes */
			NULL,	/* default security attributes */
			create_flag,
			attributes,
			NULL);	/* no template file */

	if (file == INVALID_HANDLE_VALUE) {
		*success = FALSE;
	} else {
		*success = TRUE;
	}

	return(file);
613
#else /* __WIN__ */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
	os_file_t	file;
	int		create_flag;
	
	ut_a(name);

	if (create_mode == OS_FILE_OPEN) {
		if (access_type == OS_FILE_READ_ONLY) {
			create_flag = O_RDONLY;
		} else {
			create_flag = O_RDWR;
		}
	} else if (create_mode == OS_FILE_CREATE) {
		create_flag = O_RDWR | O_CREAT | O_EXCL;
	} else {
		create_flag = 0;
		ut_error;
	}

	if (create_mode == OS_FILE_CREATE) {
	        file = open(name, create_flag, S_IRUSR | S_IWUSR
						| S_IRGRP | S_IWGRP);
        } else {
                file = open(name, create_flag);
        }
	
	if (file == -1) {
		*success = FALSE;
	} else {
		*success = TRUE;
	}

	return(file);	
646
#endif /* __WIN__ */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
647 648
}

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
/********************************************************************
Opens an existing file or creates a new. */

os_file_t
os_file_create(
/*===========*/
			/* out, own: handle to the file, not defined if error,
			error number can be retrieved with os_get_last_error */
	char*	name,	/* in: name of the file or path as a null-terminated
			string */
	ulint	create_mode, /* in: OS_FILE_OPEN if an existing file is opened
			(if does not exist, error), or OS_FILE_CREATE if a new
			file is created (if exists, error), OS_FILE_OVERWRITE
			if a new is created or an old overwritten */
	ulint	purpose,/* in: OS_FILE_AIO, if asynchronous, non-buffered i/o
664 665 666 667 668
			is desired, OS_FILE_NORMAL, if any normal file;
			NOTE that it also depends on type, os_aio_.. and srv_..
			variables whether we really use async i/o or
			unbuffered i/o: look in the function source code for
			the exact rules */
669
	ulint	type,	/* in: OS_DATA_FILE or OS_LOG_FILE */
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
	ibool*	success)/* out: TRUE if succeed, FALSE if error */
{
#ifdef __WIN__
	os_file_t	file;
	DWORD		create_flag;
	DWORD		attributes;
	ibool		retry;
	
try_again:	
	ut_a(name);

	if (create_mode == OS_FILE_OPEN) {
		create_flag = OPEN_EXISTING;
	} else if (create_mode == OS_FILE_CREATE) {
		create_flag = CREATE_NEW;
	} else if (create_mode == OS_FILE_OVERWRITE) {
		create_flag = CREATE_ALWAYS;
	} else {
		create_flag = 0;
		ut_error;
	}

	if (purpose == OS_FILE_AIO) {
693 694
		/* If specified, use asynchronous (overlapped) io and no
		buffering of writes in the OS */
695 696 697 698 699 700 701
		attributes = 0;
#ifdef WIN_ASYNC_IO
		if (os_aio_use_native_aio) {
			attributes = attributes | FILE_FLAG_OVERLAPPED;
		}
#endif			
#ifdef UNIV_NON_BUFFERED_IO
702
		if (type == OS_LOG_FILE && srv_flush_log_at_trx_commit == 2) {
703
		        /* Do not use unbuffered i/o to log files because
704 705 706 707 708
		        value 2 denotes that we do not flush the log at every
		        commit, but only once per second */
		} else if (srv_win_file_flush_method ==
						      SRV_WIN_IO_UNBUFFERED) {
		        attributes = attributes | FILE_FLAG_NO_BUFFERING;
709
		}
710 711
#endif
	} else if (purpose == OS_FILE_NORMAL) {
712
	        attributes = 0;
713
#ifdef UNIV_NON_BUFFERED_IO
714 715 716 717
		if (type == OS_LOG_FILE && srv_flush_log_at_trx_commit == 2) {
		        /* Do not use unbuffered i/o to log files because
		        value 2 denotes that we do not flush the log at every
		        commit, but only once per second */
718 719 720
		} else if (srv_win_file_flush_method ==
						      SRV_WIN_IO_UNBUFFERED) {
		        attributes = attributes | FILE_FLAG_NO_BUFFERING;
721
		}
722 723 724 725 726 727 728 729 730
#endif
	} else {
		attributes = 0;
		ut_error;
	}

	file = CreateFile(name,
			GENERIC_READ | GENERIC_WRITE, /* read and write
							access */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
731 732 733 734 735 736 737 738
			FILE_SHARE_READ,/* File can be read also by other
					processes; we must give the read
					permission because of ibbackup. We do
					not give the write permission to
					others because if one would succeed to
					start 2 instances of mysqld on the
					SAME files, that could cause severe
					database corruption! */
739 740 741 742 743 744 745 746
			NULL,	/* default security attributes */
			create_flag,
			attributes,
			NULL);	/* no template file */

	if (file == INVALID_HANDLE_VALUE) {
		*success = FALSE;

747
		retry = os_file_handle_error(name,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
748 749
				create_mode == OS_FILE_OPEN ?
				"open" : "create");
750 751
		if (retry) {
			goto try_again;
752 753 754 755 756 757
		}
	} else {
		*success = TRUE;
	}

	return(file);
758
#else /* __WIN__ */
759 760 761
	os_file_t	file;
	int		create_flag;
	ibool		retry;
762 763 764
	const char*	mode_str	= NULL;
	const char*	type_str	= NULL;
	const char*	purpose_str	= NULL;
765 766 767 768 769
	
try_again:	
	ut_a(name);

	if (create_mode == OS_FILE_OPEN) {
770 771
		mode_str = "OPEN";

772 773
		create_flag = O_RDWR;
	} else if (create_mode == OS_FILE_CREATE) {
774 775
		mode_str = "CREATE";

776 777
		create_flag = O_RDWR | O_CREAT | O_EXCL;
	} else if (create_mode == OS_FILE_OVERWRITE) {
778 779
		mode_str = "OVERWRITE";

780 781 782 783 784 785
		create_flag = O_RDWR | O_CREAT | O_TRUNC;
	} else {
		create_flag = 0;
		ut_error;
	}

786 787 788 789 790
	if (type == OS_LOG_FILE) {
		type_str = "LOG";
	} else if (type == OS_DATA_FILE) {
		type_str = "DATA";
	} else {
791
	        ut_error;
792 793 794 795 796 797 798
	}
	  
	if (purpose == OS_FILE_AIO) {
		purpose_str = "AIO";
	} else if (purpose == OS_FILE_NORMAL) {
		purpose_str = "NORMAL";
	} else {
799
	        ut_error;
800
	}
801

802
/*	fprintf(stderr, "Opening file %s, mode %s, type %s, purpose %s\n",
803
			       name, mode_str, type_str, purpose_str); */
804
#ifdef O_SYNC
805 806 807 808
        /* We let O_SYNC only affect log files; note that we map O_DSYNC to
	O_SYNC because the datasync options seemed to corrupt files in 2001
	in both Linux and Solaris */
	if (type == OS_LOG_FILE
809 810
	    && srv_unix_file_flush_method == SRV_UNIX_O_DSYNC) {

811
/*		fprintf(stderr, "Using O_SYNC for file %s\n", name); */
812

813
	        create_flag = create_flag | O_SYNC;
814
	}
815 816 817 818 819 820
#endif
#ifdef O_DIRECT
        /* We let O_DIRECT only affect data files */
	if (type != OS_LOG_FILE
	    && srv_unix_file_flush_method == SRV_UNIX_O_DIRECT) {

821
/*		fprintf(stderr, "Using O_DIRECT for file %s\n", name); */
822 823 824

	        create_flag = create_flag | O_DIRECT;
	}
825
#endif
826
	if (create_mode == OS_FILE_CREATE) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
827
	        file = open(name, create_flag, os_innodb_umask);
828 829 830 831 832 833 834
        } else {
                file = open(name, create_flag);
        }
	
	if (file == -1) {
		*success = FALSE;

835
		retry = os_file_handle_error(name,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
836 837
				create_mode == OS_FILE_OPEN ?
				"open" : "create");
838 839
		if (retry) {
			goto try_again;
840 841 842 843 844 845
		}
	} else {
		*success = TRUE;
	}

	return(file);	
846
#endif /* __WIN__ */
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
}

/***************************************************************************
Closes a file handle. In case of error, error number can be retrieved with
os_file_get_last_error. */

ibool
os_file_close(
/*==========*/
				/* out: TRUE if success */
	os_file_t	file)	/* in, own: handle to a file */
{
#ifdef __WIN__
	BOOL	ret;

	ut_a(file);

	ret = CloseHandle(file);

	if (ret) {
		return(TRUE);
	}

870
	os_file_handle_error(NULL, "close");
871 872 873 874 875 876 877
	return(FALSE);
#else
	int	ret;

	ret = close(file);

	if (ret == -1) {
878
		os_file_handle_error(NULL, "close");
879 880 881 882 883 884 885
		return(FALSE);
	}

	return(TRUE);
#endif
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
/***************************************************************************
Closes a file handle. */

ibool
os_file_close_no_error_handling(
/*============================*/
				/* out: TRUE if success */
	os_file_t	file)	/* in, own: handle to a file */
{
#ifdef __WIN__
	BOOL	ret;

	ut_a(file);

	ret = CloseHandle(file);

	if (ret) {
		return(TRUE);
	}

	return(FALSE);
#else
	int	ret;

	ret = close(file);

	if (ret == -1) {

		return(FALSE);
	}

	return(TRUE);
#endif
}

921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
/***************************************************************************
Gets a file size. */

ibool
os_file_get_size(
/*=============*/
				/* out: TRUE if success */
	os_file_t	file,	/* in: handle to a file */
	ulint*		size,	/* out: least significant 32 bits of file
				size */
	ulint*		size_high)/* out: most significant 32 bits of size */
{
#ifdef __WIN__
	DWORD	high;
	DWORD	low;

	low = GetFileSize(file, &high);

	if ((low == 0xFFFFFFFF) && (GetLastError() != NO_ERROR)) {
		return(FALSE);
	}

	*size = low;
	*size_high = high;

	return(TRUE);
#else
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
948 949 950 951
	off_t	offs;

	offs = lseek(file, 0, SEEK_END);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
952 953 954 955 956
	if (offs == ((off_t)-1)) {

		return(FALSE);
	}
	
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
957 958
	if (sizeof(off_t) > 4) {
	        *size = (ulint)(offs & 0xFFFFFFFF);
959
		*size_high = (ulint)(offs >> 32);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
960 961 962 963
	} else {
		*size = (ulint) offs;
		*size_high = 0;
	}
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
	
	return(TRUE);	
#endif
}

/***************************************************************************
Sets a file size. This function can be used to extend or truncate a file. */

ibool
os_file_set_size(
/*=============*/
				/* out: TRUE if success */
	char*		name,	/* in: name of the file or path as a
				null-terminated string */
	os_file_t	file,	/* in: handle to a file */
	ulint		size,	/* in: least significant 32 bits of file
				size */
	ulint		size_high)/* in: most significant 32 bits of size */
{
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
983 984 985 986 987
	ib_longlong	offset;
	ib_longlong	low;
	ulint   	n_bytes;
	ibool		ret;
	byte*   	buf;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
988
	byte*   	buf2;
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
989 990
	ulint   	i;

991
	ut_a(size == (size & 0xFFFFFFFF));
992

993
	/* We use a very big 8 MB buffer in writing because Linux may be
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
994
	extremely slow in fsync on 1 MB writes */
995

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
996 997 998 999
	buf2 = ut_malloc(UNIV_PAGE_SIZE * 513);

	/* Align the buffer for possible raw i/o */
	buf = ut_align(buf2, UNIV_PAGE_SIZE);
1000 1001

	/* Write buffer full of zeros */
1002
	for (i = 0; i < UNIV_PAGE_SIZE * 512; i++) {
1003 1004 1005 1006
	        buf[i] = '\0';
	}

	offset = 0;
1007
	low = (ib_longlong)size + (((ib_longlong)size_high) << 32);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1008 1009

	if (low >= (ib_longlong)(100 * 1024 * 1024)) {
1010
				
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1011 1012 1013
		fprintf(stderr, "InnoDB: Progress in MB:");
	}

1014
	while (offset < low) {
1015
	        if (low - offset < UNIV_PAGE_SIZE * 512) {
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1016
	        	n_bytes = (ulint)(low - offset);
1017
	        } else {
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1018
	        	n_bytes = UNIV_PAGE_SIZE * 512;
1019 1020
	        }
	  
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1021 1022
	        ret = os_file_write(name, file, buf,
				(ulint)(offset & 0xFFFFFFFF),
1023
				    (ulint)(offset >> 32),
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1024
				n_bytes);
1025
	        if (!ret) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1026
			ut_free(buf2);
1027 1028
	         	goto error_handling;
	        }
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
				
		/* Print about progress for each 100 MB written */
		if ((offset + n_bytes) / (ib_longlong)(100 * 1024 * 1024)
		    != offset / (ib_longlong)(100 * 1024 * 1024)) {

		        fprintf(stderr, " %lu00",
				(ulint)((offset + n_bytes)
					/ (ib_longlong)(100 * 1024 * 1024)));
		}
		
1039 1040 1041
	        offset += n_bytes;
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1042 1043 1044 1045 1046
	if (low >= (ib_longlong)(100 * 1024 * 1024)) {
				
		fprintf(stderr, "\n");
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1047
	ut_free(buf2);
1048 1049 1050 1051 1052 1053 1054 1055

	ret = os_file_flush(file);

	if (ret) {
	        return(TRUE);
	}

error_handling:
1056
	return(FALSE);
1057 1058
}

1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
/***************************************************************************
Truncates a file at its current position. */

ibool
os_file_set_eof(
/*============*/
				/* out: TRUE if success */
	FILE*		file)	/* in: file to be truncated */
{
#ifdef __WIN__
	HANDLE h = (HANDLE) _get_osfhandle(fileno(file));
	return(SetEndOfFile(h));
#else /* __WIN__ */
	return(!ftruncate(fileno(file), ftell(file)));
#endif /* __WIN__ */
}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
/***************************************************************************
Flushes the write buffers of a given file to the disk. */

ibool
os_file_flush(
/*==========*/
				/* out: TRUE if success */
	os_file_t	file)	/* in, own: handle to a file */
{
#ifdef __WIN__
	BOOL	ret;

	ut_a(file);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1090 1091
	os_n_fsyncs++;

1092 1093 1094 1095 1096 1097
	ret = FlushFileBuffers(file);

	if (ret) {
		return(TRUE);
	}

1098
	os_file_handle_error(NULL, "flush");
1099

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1100 1101
	/* It is a fatal error if a file flush does not succeed, because then
	the database can get corrupt on disk */
1102
	ut_error;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1103

1104 1105 1106
	return(FALSE);
#else
	int	ret;
1107

1108
#ifdef HAVE_FDATASYNC
1109 1110
	ret = fdatasync(file);
#else
1111
/*	fprintf(stderr, "Flushing to file %p\n", file); */
1112
	ret = fsync(file);
1113
#endif
1114 1115
	os_n_fsyncs++;

1116 1117 1118 1119
	if (ret == 0) {
		return(TRUE);
	}
	
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
1120 1121 1122 1123 1124 1125 1126
	/* Since Linux returns EINVAL if the 'file' is actually a raw device,
	we choose to ignore that error */

	if (errno == EINVAL) {
	        return(TRUE);
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1127 1128
	ut_print_timestamp(stderr);
	
1129
	fprintf(stderr,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1130
		"  InnoDB: Error: the OS said file flush did not succeed\n");
1131

1132
	os_file_handle_error(NULL, "flush");
1133

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1134 1135
	/* It is a fatal error if a file flush does not succeed, because then
	the database can get corrupt on disk */
1136
	ut_error;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1137

1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
	return(FALSE);
#endif
}

#ifndef __WIN__
/***********************************************************************
Does a synchronous read operation in Posix. */
static
ssize_t
os_file_pread(
/*==========*/
				/* out: number of bytes read, -1 if error */
	os_file_t	file,	/* in: handle to a file */
	void*		buf,	/* in: buffer where to read */
	ulint		n,	/* in: number of bytes to read */	
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1153 1154 1155 1156
	ulint		offset,	/* in: least significant 32 bits of file
				offset from where to read */
	ulint		offset_high) /* in: most significant 32 bits of
				offset */
1157
{
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1158
        off_t	offs;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1159
	ssize_t	n_bytes;
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1160

1161
	ut_a((offset & 0xFFFFFFFF) == offset);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1162 1163 1164 1165 1166
        
        /* If off_t is > 4 bytes in size, then we assume we can pass a
	64-bit address */

        if (sizeof(off_t) > 4) {
1167 1168
	        offs = (off_t)offset + (((off_t)offset_high) << 32);
        				
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1169 1170 1171 1172 1173 1174 1175 1176
        } else {
        	offs = (off_t)offset;

        	if (offset_high > 0) {
        		fprintf(stderr,
			"InnoDB: Error: file read at offset > 4 GB\n");
		}
        }
1177

1178 1179
	os_n_file_reads++;

monty@mysql.com's avatar
monty@mysql.com committed
1180
#if defined(HAVE_PREAD) && !defined(HAVE_BROKEN_PREAD)
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
        os_mutex_enter(os_file_count_mutex);
	os_file_n_pending_preads++;
        os_mutex_exit(os_file_count_mutex);

        n_bytes = pread(file, buf, n, offs);

        os_mutex_enter(os_file_count_mutex);
	os_file_n_pending_preads--;
        os_mutex_exit(os_file_count_mutex);

	return(n_bytes);
1192
#else
1193
	{
1194 1195 1196 1197 1198 1199 1200 1201
	ssize_t	ret;
	ulint	i;

	/* Protect the seek / read operation with a mutex */
	i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
	
	os_mutex_enter(os_file_seek_mutexes[i]);

1202
	ret = lseek(file, offs, 0);
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214

	if (ret < 0) {
		os_mutex_exit(os_file_seek_mutexes[i]);

		return(ret);
	}
	
	ret = read(file, buf, n);

	os_mutex_exit(os_file_seek_mutexes[i]);

	return(ret);
1215
	}
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
#endif
}

/***********************************************************************
Does a synchronous write operation in Posix. */
static
ssize_t
os_file_pwrite(
/*===========*/
				/* out: number of bytes written, -1 if error */
	os_file_t	file,	/* in: handle to a file */
	void*		buf,	/* in: buffer from where to write */
	ulint		n,	/* in: number of bytes to write */	
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1229 1230 1231 1232
	ulint		offset,	/* in: least significant 32 bits of file
				offset where to write */
	ulint		offset_high) /* in: most significant 32 bits of
				offset */
1233
{
1234
	ssize_t	ret;
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1235 1236
        off_t	offs;

1237
	ut_a((offset & 0xFFFFFFFF) == offset);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1238 1239 1240 1241 1242

        /* If off_t is > 4 bytes in size, then we assume we can pass a
	64-bit address */

        if (sizeof(off_t) > 4) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1243
	  	offs = (off_t)offset + (((off_t)offset_high) << 32);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1244 1245 1246 1247 1248 1249 1250 1251
        } else {
        	offs = (off_t)offset;

        	if (offset_high > 0) {
        		fprintf(stderr,
			"InnoDB: Error: file write at offset > 4 GB\n");
		}
        }
1252

1253 1254
	os_n_file_writes++;

monty@mysql.com's avatar
monty@mysql.com committed
1255
#if defined(HAVE_PWRITE) && !defined(HAVE_BROKEN_PREAD)
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1256 1257 1258 1259
        os_mutex_enter(os_file_count_mutex);
	os_file_n_pending_pwrites++;
        os_mutex_exit(os_file_count_mutex);

1260
	ret = pwrite(file, buf, n, offs);
1261

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1262 1263 1264 1265
        os_mutex_enter(os_file_count_mutex);
	os_file_n_pending_pwrites--;
        os_mutex_exit(os_file_count_mutex);

1266
	if (srv_unix_file_flush_method != SRV_UNIX_LITTLESYNC
1267
	    && srv_unix_file_flush_method != SRV_UNIX_NOSYNC
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1268 1269
	    && !os_do_not_call_flush_at_each_write) {
	    	
1270 1271 1272
	        /* Always do fsync to reduce the probability that when
                the OS crashes, a database page is only partially
                physically written to disk. */
1273

1274 1275
	        ut_a(TRUE == os_file_flush(file));
	}
1276 1277

        return(ret);
1278
#else
1279
	{
1280 1281 1282 1283 1284 1285 1286
	ulint	i;

	/* Protect the seek / write operation with a mutex */
	i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
	
	os_mutex_enter(os_file_seek_mutexes[i]);

1287
	ret = lseek(file, offs, 0);
1288 1289 1290 1291 1292 1293 1294 1295 1296

	if (ret < 0) {
		os_mutex_exit(os_file_seek_mutexes[i]);

		return(ret);
	}
	
	ret = write(file, buf, n);

1297
	if (srv_unix_file_flush_method != SRV_UNIX_LITTLESYNC
1298
	    && srv_unix_file_flush_method != SRV_UNIX_NOSYNC
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1299
	    && !os_do_not_call_flush_at_each_write) {
1300

1301 1302 1303 1304 1305 1306
	        /* Always do fsync to reduce the probability that when
                the OS crashes, a database page is only partially
                physically written to disk. */

	        ut_a(TRUE == os_file_flush(file));
	}
1307

1308 1309 1310
	os_mutex_exit(os_file_seek_mutexes[i]);

	return(ret);
1311
	}
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
#endif
}
#endif

/***********************************************************************
Requests a synchronous positioned read operation. */

ibool
os_file_read(
/*=========*/
				/* out: TRUE if request was
				successful, FALSE if fail */
	os_file_t	file,	/* in: handle to a file */
	void*		buf,	/* in: buffer where to read */
	ulint		offset,	/* in: least significant 32 bits of file
				offset where to read */
	ulint		offset_high, /* in: most significant 32 bits of
				offset */
	ulint		n)	/* in: number of bytes to read */	
{
#ifdef __WIN__
	BOOL		ret;
	DWORD		len;
	DWORD		ret2;
	DWORD		low;
	DWORD		high;
	ibool		retry;
	ulint		i;
	
1341
	ut_a((offset & 0xFFFFFFFF) == offset);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1342

1343
	os_n_file_reads++;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1344
	os_bytes_read_since_printout += n;
1345

1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
try_again:	
	ut_ad(file);
	ut_ad(buf);
	ut_ad(n > 0);

	low = offset;
	high = offset_high;

	/* Protect the seek / read operation with a mutex */
	i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
	
	os_mutex_enter(os_file_seek_mutexes[i]);

	ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);

	if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {

		os_mutex_exit(os_file_seek_mutexes[i]);

		goto error_handling;
	} 
	
	ret = ReadFile(file, buf, n, &len, NULL);

	os_mutex_exit(os_file_seek_mutexes[i]);
	
	if (ret && len == n) {
		return(TRUE);
	}		
#else
	ibool	retry;
	ssize_t	ret;
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1378

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1379 1380
	os_bytes_read_since_printout += n;

1381
try_again:
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1382
	ret = os_file_pread(file, buf, n, offset, offset_high);
1383

1384
	if ((ulint)ret == n) {
1385 1386 1387 1388

		return(TRUE);
	}
#endif	
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1389
#ifdef __WIN__
1390
error_handling:
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1391
#endif
1392
	retry = os_file_handle_error(NULL, "read"); 
1393 1394 1395 1396

	if (retry) {
		goto try_again;
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
       
	fprintf(stderr,
"InnoDB: Fatal error: cannot read from file. OS error number %lu.\n",
#ifdef __WIN__
		(ulint)GetLastError()
#else
		(ulint)errno
#endif
		);
	fflush(stderr);

1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
	ut_error;

	return(FALSE);
}

/***********************************************************************
Requests a synchronous write operation. */

ibool
os_file_write(
/*==========*/
				/* out: TRUE if request was
				successful, FALSE if fail */
	char*		name,	/* in: name of the file or path as a
				null-terminated string */
	os_file_t	file,	/* in: handle to a file */
	void*		buf,	/* in: buffer from which to write */
	ulint		offset,	/* in: least significant 32 bits of file
				offset where to write */
	ulint		offset_high, /* in: most significant 32 bits of
				offset */
	ulint		n)	/* in: number of bytes to write */	
{
#ifdef __WIN__
	BOOL		ret;
	DWORD		len;
	DWORD		ret2;
	DWORD		low;
	DWORD		high;
	ulint		i;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1438
	ulint		n_retries	= 0;
1439
	ulint		err;
1440

1441
	ut_a((offset & 0xFFFFFFFF) == offset);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1442

1443
	os_n_file_writes++;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1444

1445 1446 1447
	ut_ad(file);
	ut_ad(buf);
	ut_ad(n > 0);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1448
retry:
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
	low = offset;
	high = offset_high;
	
	/* Protect the seek / write operation with a mutex */
	i = ((ulint) file) % OS_FILE_N_SEEK_MUTEXES;
	
	os_mutex_enter(os_file_seek_mutexes[i]);

	ret2 = SetFilePointer(file, low, &high, FILE_BEGIN);

	if (ret2 == 0xFFFFFFFF && GetLastError() != NO_ERROR) {

		os_mutex_exit(os_file_seek_mutexes[i]);
		
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1463 1464 1465 1466
		ut_print_timestamp(stderr);

		fprintf(stderr,
"  InnoDB: Error: File pointer positioning to file %s failed at\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1467
"InnoDB: offset %lu %lu. Operating system error number %lu.\n"
1468
"InnoDB: Look from section 13.2 at http://www.innodb.com/ibman.php\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1469
"InnoDB: what the error number means.\n",
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1470 1471 1472 1473
			name, offset_high, offset,
			(ulint)GetLastError());

		return(FALSE);
1474 1475 1476
	} 

	ret = WriteFile(file, buf, n, &len, NULL);
1477 1478 1479 1480
	
	/* Always do fsync to reduce the probability that when the OS crashes,
	a database page is only partially physically written to disk. */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1481
	if (!os_do_not_call_flush_at_each_write) {
1482 1483
		ut_a(TRUE == os_file_flush(file));
	}
1484 1485

	os_mutex_exit(os_file_seek_mutexes[i]);
1486

1487
	if (ret && len == n) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1488

1489 1490
		return(TRUE);
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1491

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1492 1493 1494
	/* If some background file system backup tool is running, then, at
	least in Windows 2000, we may get here a specific error. Let us
	retry the operation 100 times, with 1 second waits. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1495
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1496
	if (GetLastError() == ERROR_LOCK_VIOLATION && n_retries < 100) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1497

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1498 1499
		os_thread_sleep(1000000);
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1500 1501 1502 1503 1504
		n_retries++;

		goto retry;
	}	
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1505 1506
	if (!os_has_said_disk_full) {
	
1507 1508
		err = (ulint)GetLastError();

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1509 1510 1511 1512 1513 1514 1515
		ut_print_timestamp(stderr);

		fprintf(stderr,
"  InnoDB: Error: Write to file %s failed at offset %lu %lu.\n"
"InnoDB: %lu bytes should have been written, only %lu were written.\n"
"InnoDB: Operating system error number %lu.\n"
"InnoDB: Check that your OS and file system support files of this size.\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1516
"InnoDB: Check also that the disk is not full or a disk quota exceeded.\n",
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1517
			name, offset_high, offset, n, (ulint)len,
1518 1519 1520 1521 1522 1523 1524 1525
			err);

		if (strerror((int)err) != NULL) {
			fprintf(stderr,
"InnoDB: Error number %lu means '%s'.\n", err, strerror((int)err));
		}

		fprintf(stderr,
1526
"InnoDB: See also section 13.2 at http://www.innodb.com/ibman.php\n"
1527
"InnoDB: about operating system error numbers.\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1528 1529 1530 1531 1532

		os_has_said_disk_full = TRUE;
	}

	return(FALSE);
1533 1534 1535
#else
	ssize_t	ret;
	
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1536
	ret = os_file_pwrite(file, buf, n, offset, offset_high);
1537

1538
	if ((ulint)ret == n) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1539

1540 1541 1542
		return(TRUE);
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1543
	if (!os_has_said_disk_full) {
1544
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1545
		ut_print_timestamp(stderr);
1546

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1547 1548
		fprintf(stderr,
"  InnoDB: Error: Write to file %s failed at offset %lu %lu.\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1549
"InnoDB: %lu bytes should have been written, only %ld were written.\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1550 1551
"InnoDB: Operating system error number %lu.\n"
"InnoDB: Check that your OS and file system support files of this size.\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1552
"InnoDB: Check also that the disk is not full or a disk quota exceeded.\n",
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1553
			name, offset_high, offset, n, (long int)ret,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1554
							(ulint)errno);
1555 1556 1557 1558 1559 1560
		if (strerror(errno) != NULL) {
			fprintf(stderr,
"InnoDB: Error number %lu means '%s'.\n", (ulint)errno, strerror(errno));
		}

		fprintf(stderr,
1561
"InnoDB: See also section 13.2 at http://www.innodb.com/ibman.php\n"
1562 1563
"InnoDB: about operating system error numbers.\n");

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1564 1565 1566 1567 1568
		os_has_said_disk_full = TRUE;
	}

	return(FALSE);	
#endif
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
}

/********************************************************************
Returns a pointer to the nth slot in the aio array. */
static
os_aio_slot_t*
os_aio_array_get_nth_slot(
/*======================*/
					/* out: pointer to slot */
	os_aio_array_t*		array,	/* in: aio array */
	ulint			index)	/* in: index of the slot */
{
	ut_a(index < array->n_slots);

	return((array->slots) + index);
}

/****************************************************************************
Creates an aio wait array. */
static
os_aio_array_t*
os_aio_array_create(
/*================*/
				/* out, own: aio array */
	ulint	n,		/* in: maximum number of pending aio operations
				allowed; n must be divisible by n_segments */
	ulint	n_segments) 	/* in: number of segments in the aio array */
{
	os_aio_array_t*	array;
	ulint		i;
	os_aio_slot_t*	slot;
#ifdef WIN_ASYNC_IO
	OVERLAPPED*	over;
#endif	
	ut_a(n > 0);
	ut_a(n_segments > 0);

	array = ut_malloc(sizeof(os_aio_array_t));

	array->mutex 		= os_mutex_create(NULL);
	array->not_full		= os_event_create(NULL);
1610 1611 1612 1613
	array->is_empty		= os_event_create(NULL);

	os_event_set(array->is_empty);
	
1614 1615 1616 1617
	array->n_slots  	= n;
	array->n_segments	= n_segments;
	array->n_reserved	= 0;
	array->slots		= ut_malloc(n * sizeof(os_aio_slot_t));
1618 1619 1620
#ifdef __WIN__
	array->native_events	= ut_malloc(n * sizeof(os_native_event_t));
#endif	
1621 1622 1623 1624 1625 1626
	for (i = 0; i < n; i++) {
		slot = os_aio_array_get_nth_slot(array, i);

		slot->pos = i;
		slot->reserved = FALSE;
#ifdef WIN_ASYNC_IO
1627 1628
		slot->event = os_event_create(NULL);

1629 1630
		over = &(slot->control);

1631
		over->hEvent = slot->event->handle;
1632

1633
		*((array->native_events) + i) = over->hEvent;
1634 1635 1636 1637 1638 1639 1640
#endif
	}
	
	return(array);
}

/****************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1641 1642
Initializes the asynchronous io system. Calls also os_io_init_simple.
Creates a separate aio array for
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
non-ibuf read and write, a third aio array for the ibuf i/o, with just one
segment, two aio arrays for log reads and writes with one segment, and a
synchronous aio array of the specified size. The combined number of segments
in the three first aio arrays is the parameter n_segments given to the
function. The caller must create an i/o handler thread for each segment in
the four first arrays, but not for the sync aio array. */

void
os_aio_init(
/*========*/
	ulint	n,		/* in: maximum number of pending aio operations
				allowed; n must be divisible by n_segments */
	ulint	n_segments,	/* in: combined number of segments in the four
				first aio arrays; must be >= 4 */
	ulint	n_slots_sync)	/* in: number of slots in the sync aio array */
{
	ulint	n_read_segs;
	ulint	n_write_segs;
	ulint	n_per_seg;
	ulint	i;
#ifdef POSIX_ASYNC_IO
	sigset_t   sigset;
#endif
	ut_ad(n % n_segments == 0);
	ut_ad(n_segments >= 4);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1669 1670
	os_io_init_simple();

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1671
	for (i = 0; i < n_segments; i++) {
1672
	        srv_set_io_thread_op_info(i, "not started yet");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1673 1674
	}

1675 1676 1677 1678
	n_per_seg = n / n_segments;
	n_write_segs = (n_segments - 2) / 2;
	n_read_segs = n_segments - 2 - n_write_segs;
	
1679
	/* fprintf(stderr, "Array n per seg %lu\n", n_per_seg); */
1680

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1681 1682
	os_aio_ibuf_array = os_aio_array_create(n_per_seg, 1);

1683
	srv_io_thread_function[0] = "insert buffer thread";
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1684 1685 1686

	os_aio_log_array = os_aio_array_create(n_per_seg, 1);

1687
	srv_io_thread_function[1] = "log thread";
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1688

1689 1690
	os_aio_read_array = os_aio_array_create(n_read_segs * n_per_seg,
							n_read_segs);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1691
	for (i = 2; i < 2 + n_read_segs; i++) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1692
		ut_a(i < SRV_MAX_N_IO_THREADS);
1693
	        srv_io_thread_function[i] = "read thread";
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1694 1695
	}

1696 1697
	os_aio_write_array = os_aio_array_create(n_write_segs * n_per_seg,
							n_write_segs);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1698
	for (i = 2 + n_read_segs; i < n_segments; i++) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1699
		ut_a(i < SRV_MAX_N_IO_THREADS);
1700
	        srv_io_thread_function[i] = "write thread";
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1701
	}
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714

	os_aio_sync_array = os_aio_array_create(n_slots_sync, 1);

	os_aio_n_segments = n_segments;

	os_aio_validate();

	os_aio_segment_wait_events = ut_malloc(n_segments * sizeof(void*));

	for (i = 0; i < n_segments; i++) {
		os_aio_segment_wait_events[i] = os_event_create(NULL);
	}

1715 1716
	os_last_printout = time(NULL);

1717
#ifdef POSIX_ASYNC_IO
1718 1719 1720 1721 1722
	/* Block aio signals from the current thread and its children:
	for this to work, the current thread must be the first created
	in the database, so that all its children will inherit its
	signal mask */
	
1723 1724
	/* TODO: to work MySQL needs the SIGALARM signal; the following
	will not work yet! */
1725 1726 1727 1728 1729 1730
        sigemptyset(&sigset);
	sigaddset(&sigset, SIGRTMIN + 1 + 0);
	sigaddset(&sigset, SIGRTMIN + 1 + 1);
	sigaddset(&sigset, SIGRTMIN + 1 + 2);
	sigaddset(&sigset, SIGRTMIN + 1 + 3);

1731
	pthread_sigmask(SIG_BLOCK, &sigset, NULL); */
1732 1733
#endif
}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748

#ifdef WIN_ASYNC_IO
/****************************************************************************
Wakes up all async i/o threads in the array in Windows async i/o at
shutdown. */
static
void
os_aio_array_wake_win_aio_at_shutdown(
/*==================================*/
	os_aio_array_t*	array)	/* in: aio array */
{
	ulint	i;

	for (i = 0; i < array->n_slots; i++) {

1749
	        os_event_set((array->slots + i)->event);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
	}
}
#endif

/****************************************************************************
Wakes up all async i/o threads so that they know to exit themselves in
shutdown. */

void
os_aio_wake_all_threads_at_shutdown(void)
/*=====================================*/
{
	ulint	i;

#ifdef WIN_ASYNC_IO
        /* This code wakes up all ai/o threads in Windows native aio */
	os_aio_array_wake_win_aio_at_shutdown(os_aio_read_array);
	os_aio_array_wake_win_aio_at_shutdown(os_aio_write_array);
	os_aio_array_wake_win_aio_at_shutdown(os_aio_ibuf_array);
	os_aio_array_wake_win_aio_at_shutdown(os_aio_log_array);
#endif
	/* This loop wakes up all simulated ai/o threads */

	for (i = 0; i < os_aio_n_segments; i++) {
	    	
		os_event_set(os_aio_segment_wait_events[i]);
	}	
}
1778
				
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
/****************************************************************************
Waits until there are no pending writes in os_aio_write_array. There can
be other, synchronous, pending writes. */

void
os_aio_wait_until_no_pending_writes(void)
/*=====================================*/
{
	os_event_wait(os_aio_write_array->is_empty);
}

1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
/**************************************************************************
Calculates segment number for a slot. */
static
ulint
os_aio_get_segment_no_from_slot(
/*============================*/
				/* out: segment number (which is the number
				used by, for example, i/o-handler threads) */
	os_aio_array_t*	array,	/* in: aio wait array */
	os_aio_slot_t*	slot)	/* in: slot in this array */
{
	ulint	segment;
	ulint	seg_len;

	if (array == os_aio_ibuf_array) {
		segment = 0;

	} else if (array == os_aio_log_array) {
		segment = 1;
		
	} else if (array == os_aio_read_array) {
		seg_len = os_aio_read_array->n_slots /
				os_aio_read_array->n_segments;

		segment = 2 + slot->pos / seg_len;
	} else {
		ut_a(array == os_aio_write_array);
		seg_len = os_aio_write_array->n_slots /
				os_aio_write_array->n_segments;

		segment = os_aio_read_array->n_segments + 2
				+ slot->pos / seg_len;
	}

	return(segment);
}

/**************************************************************************
Calculates local segment number and aio array from global segment number. */
static
ulint
os_aio_get_array_and_local_segment(
/*===============================*/
					/* out: local segment number within
					the aio array */
	os_aio_array_t** array,		/* out: aio wait array */
	ulint		 global_segment)/* in: global segment number */
{
	ulint	segment;

	ut_a(global_segment < os_aio_n_segments);	

	if (global_segment == 0) {
		*array = os_aio_ibuf_array;
		segment = 0;

	} else if (global_segment == 1) {
		*array = os_aio_log_array;
		segment = 0;
		
	} else if (global_segment < os_aio_read_array->n_segments + 2) {
		*array = os_aio_read_array;

		segment = global_segment - 2;
	} else {
		*array = os_aio_write_array;

		segment = global_segment - (os_aio_read_array->n_segments + 2);
	}

	return(segment);
}

/***********************************************************************
Gets an integer value designating a specified aio array. This is used
to give numbers to signals in Posix aio. */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1866 1867

#if !defined(WIN_ASYNC_IO) && defined(POSIX_ASYNC_IO)
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
static
ulint
os_aio_get_array_no(
/*================*/
	os_aio_array_t*	array)	/* in: aio array */
{	
	if (array == os_aio_ibuf_array) {
	
		return(0);

	} else if (array == os_aio_log_array) {

		return(1);

	} else if (array == os_aio_read_array) {

		return(2);
	} else if (array == os_aio_write_array) {

		return(3);
	} else {
1889
		ut_error;
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915

		return(0);
	}
}

/***********************************************************************
Gets the aio array for its number. */
static
os_aio_array_t*
os_aio_get_array_from_no(
/*=====================*/
			/* out: aio array */
	ulint	n)	/* in: array number */
{	
	if (n == 0) {
		return(os_aio_ibuf_array);
	} else if (n == 1) {

		return(os_aio_log_array);
	} else if (n == 2) {

		return(os_aio_read_array);
	} else if (n == 3) {

		return(os_aio_write_array);
	} else {
1916
		ut_error;
1917 1918 1919 1920

		return(NULL);
	}
}
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1921
#endif /* if !defined(WIN_ASYNC_IO) && defined(POSIX_ASYNC_IO) */
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984

/***********************************************************************
Requests for a slot in the aio array. If no slot is available, waits until
not_full-event becomes signaled. */
static
os_aio_slot_t*
os_aio_array_reserve_slot(
/*======================*/
				/* out: pointer to slot */
	ulint		type,	/* in: OS_FILE_READ or OS_FILE_WRITE */
	os_aio_array_t*	array,	/* in: aio array */
	void*		message1,/* in: message to be passed along with
				the aio operation */
	void*		message2,/* in: message to be passed along with
				the aio operation */
	os_file_t	file,	/* in: file handle */
	char*		name,	/* in: name of the file or path as a
				null-terminated string */
	void*		buf,	/* in: buffer where to read or from which
				to write */
	ulint		offset,	/* in: least significant 32 bits of file
				offset */
	ulint		offset_high, /* in: most significant 32 bits of
				offset */
	ulint		len)	/* in: length of the block to read or write */
{
	os_aio_slot_t*	slot;
#ifdef WIN_ASYNC_IO
	OVERLAPPED*	control;

#elif defined(POSIX_ASYNC_IO)

	struct aiocb*	control;
#endif
	ulint		i;
loop:
	os_mutex_enter(array->mutex);

	if (array->n_reserved == array->n_slots) {
		os_mutex_exit(array->mutex);

		if (!os_aio_use_native_aio) {
			/* If the handler threads are suspended, wake them
			so that we get more slots */

			os_aio_simulated_wake_handler_threads();
		}
		
		os_event_wait(array->not_full);

		goto loop;
	}

	for (i = 0;; i++) {
		slot = os_aio_array_get_nth_slot(array, i);

		if (slot->reserved == FALSE) {
			break;
		}
	}

	array->n_reserved++;

1985 1986 1987 1988
	if (array->n_reserved == 1) {
		os_event_reset(array->is_empty);
	}

1989 1990 1991 1992 1993
	if (array->n_reserved == array->n_slots) {
		os_event_reset(array->not_full);
	}
	
	slot->reserved = TRUE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1994
	slot->reservation_time = time(NULL);
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
	slot->message1 = message1;
	slot->message2 = message2;
	slot->file     = file;
	slot->name     = name;
	slot->len      = len;
	slot->type     = type;
	slot->buf      = buf;
	slot->offset   = offset;
	slot->offset_high = offset_high;
	slot->io_already_done = FALSE;
	
#ifdef WIN_ASYNC_IO		
	control = &(slot->control);
	control->Offset = (DWORD)offset;
	control->OffsetHigh = (DWORD)offset_high;
2010
	os_event_reset(slot->event);
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029

#elif defined(POSIX_ASYNC_IO)

#if (UNIV_WORD_SIZE == 8)
	offset = offset + (offset_high << 32);
#else
	ut_a(offset_high == 0);
#endif 
	control = &(slot->control);
	control->aio_fildes = file;
	control->aio_buf = buf;
	control->aio_nbytes = len;
	control->aio_offset = offset;
	control->aio_reqprio = 0;
	control->aio_sigevent.sigev_notify = SIGEV_SIGNAL;
	control->aio_sigevent.sigev_signo =
			SIGRTMIN + 1 + os_aio_get_array_no(array);
			/* TODO: How to choose the signal numbers? */
/*
2030 2031
	fprintf(stderr, "AIO signal number %lu\n",
		(ulint) control->aio_sigevent.sigev_signo);
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
*/
	control->aio_sigevent.sigev_value.sival_ptr = slot;
#endif
	os_mutex_exit(array->mutex);

	return(slot);
}

/***********************************************************************
Frees a slot in the aio array. */
static
void
os_aio_array_free_slot(
/*===================*/
	os_aio_array_t*	array,	/* in: aio array */
	os_aio_slot_t*	slot)	/* in: pointer to slot */
{
	ut_ad(array);
	ut_ad(slot);

	os_mutex_enter(array->mutex);

	ut_ad(slot->reserved);
	
	slot->reserved = FALSE;

	array->n_reserved--;

	if (array->n_reserved == array->n_slots - 1) {
		os_event_set(array->not_full);
	}

2064 2065 2066 2067
	if (array->n_reserved == 0) {
		os_event_set(array->is_empty);
	}

2068
#ifdef WIN_ASYNC_IO		
2069
	os_event_reset(slot->event);
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
#endif
	os_mutex_exit(array->mutex);
}

/**************************************************************************
Wakes up a simulated aio i/o-handler thread if it has something to do. */
static
void
os_aio_simulated_wake_handler_thread(
/*=================================*/
	ulint	global_segment)	/* in: the number of the segment in the aio
				arrays */
{
	os_aio_array_t*	array;
	os_aio_slot_t*	slot;
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
2085
	ulint		segment;
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
	ulint		n;
	ulint		i;

	ut_ad(!os_aio_use_native_aio);

	segment = os_aio_get_array_and_local_segment(&array, global_segment);

	n = array->n_slots / array->n_segments;

	/* Look through n slots after the segment * n'th slot */

	os_mutex_enter(array->mutex);

	for (i = 0; i < n; i++) {
		slot = os_aio_array_get_nth_slot(array, i + segment * n);

		if (slot->reserved) {
			/* Found an i/o request */
			
			break;
		}
	}

	os_mutex_exit(array->mutex);

	if (i < n) {
		os_event_set(os_aio_segment_wait_events[global_segment]);
	}
}

/**************************************************************************
Wakes up simulated aio i/o-handler threads if they have something to do. */

void
os_aio_simulated_wake_handler_threads(void)
/*=======================================*/
{
	ulint	i;

	if (os_aio_use_native_aio) {
		/* We do not use simulated aio: do nothing */

		return;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2129 2130 2131
	}	

	os_aio_recommend_sleep_for_read_threads	= FALSE;
2132 2133 2134 2135 2136 2137

	for (i = 0; i < os_aio_n_segments; i++) {
		os_aio_simulated_wake_handler_thread(i);
	}
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
/**************************************************************************
This function can be called if one wants to post a batch of reads and
prefers an i/o-handler thread to handle them all at once later. You must
call os_aio_simulated_wake_handler_threads later to ensure the threads
are not left sleeping! */

void
os_aio_simulated_put_read_threads_to_sleep(void)
/*============================================*/
{
	os_aio_array_t*	array;
	ulint		g;

	os_aio_recommend_sleep_for_read_threads	= TRUE;

	for (g = 0; g < os_aio_n_segments; g++) {
		os_aio_get_array_and_local_segment(&array, g);

		if (array == os_aio_read_array) {
		
			os_event_reset(os_aio_segment_wait_events[g]);
		}
	}
}

2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
/***********************************************************************
Requests an asynchronous i/o operation. */

ibool
os_aio(
/*===*/
				/* out: TRUE if request was queued
				successfully, FALSE if fail */
	ulint		type,	/* in: OS_FILE_READ or OS_FILE_WRITE */
	ulint		mode,	/* in: OS_AIO_NORMAL, ..., possibly ORed
				to OS_AIO_SIMULATED_WAKE_LATER: the
				last flag advises this function not to wake
				i/o-handler threads, but the caller will
				do the waking explicitly later, in this
				way the caller can post several requests in
				a batch; NOTE that the batch must not be
				so big that it exhausts the slots in aio
				arrays! NOTE that a simulated batch
				may introduce hidden chances of deadlocks,
				because i/os are not actually handled until
				all have been posted: use with great
				caution! */
	char*		name,	/* in: name of the file or path as a
				null-terminated string */
	os_file_t	file,	/* in: handle to a file */
	void*		buf,	/* in: buffer where to read or from which
				to write */
	ulint		offset,	/* in: least significant 32 bits of file
				offset where to read or write */
	ulint		offset_high, /* in: most significant 32 bits of
				offset */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2194
	ulint		n,	/* in: number of bytes to read or write */
2195 2196 2197 2198 2199 2200 2201 2202 2203
	void*		message1,/* in: messages for the aio handler (these
				can be used to identify a completed aio
				operation); if mode is OS_AIO_SYNC, these
				are ignored */
	void*		message2)
{
	os_aio_array_t*	array;
	os_aio_slot_t*	slot;
#ifdef WIN_ASYNC_IO
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2204
	ibool		retval;
2205 2206 2207 2208
	BOOL		ret		= TRUE;
	DWORD		len		= n;
	void*		dummy_mess1;
	void*		dummy_mess2;
2209
	ulint		dummy_type;
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
#endif
	ulint		err		= 0;
	ibool		retry;
	ulint		wake_later;

	ut_ad(file);
	ut_ad(buf);
	ut_ad(n > 0);
	ut_ad(n % OS_FILE_LOG_BLOCK_SIZE == 0);
	ut_ad(offset % OS_FILE_LOG_BLOCK_SIZE == 0);
	ut_ad(os_aio_validate());

	wake_later = mode & OS_AIO_SIMULATED_WAKE_LATER;
	mode = mode & (~OS_AIO_SIMULATED_WAKE_LATER);
	
	if (mode == OS_AIO_SYNC
#ifdef WIN_ASYNC_IO
				&& !os_aio_use_native_aio
#endif
	) {
		/* This is actually an ordinary synchronous read or write:
		no need to use an i/o-handler thread. NOTE that if we use
		Windows async i/o, Windows does not allow us to use
		ordinary synchronous os_file_read etc. on the same file,
		therefore we have built a special mechanism for synchronous
		wait in the Windows case. */

		if (type == OS_FILE_READ) {
2238 2239
			return(os_file_read(file, buf, offset,
							offset_high, n));
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
		}

		ut_a(type == OS_FILE_WRITE);

		return(os_file_write(name, file, buf, offset, offset_high, n));
	}

try_again:
	if (mode == OS_AIO_NORMAL) {
		if (type == OS_FILE_READ) {
			array = os_aio_read_array;
		} else {
			array = os_aio_write_array;
		}
	} else if (mode == OS_AIO_IBUF) {
		ut_ad(type == OS_FILE_READ);
2256 2257 2258 2259
		/* Reduce probability of deadlock bugs in connection with ibuf:
		do not let the ibuf i/o handler sleep */

		wake_later = FALSE;
2260 2261 2262 2263 2264 2265 2266 2267

		array = os_aio_ibuf_array;
	} else if (mode == OS_AIO_LOG) {

		array = os_aio_log_array;
	} else if (mode == OS_AIO_SYNC) {
		array = os_aio_sync_array;
	} else {
2268
		array = NULL; /* Eliminate compiler warning */
2269 2270 2271 2272 2273 2274 2275 2276
		ut_error;
	}
	
	slot = os_aio_array_reserve_slot(type, array, message1, message2, file,
					name, buf, offset, offset_high, n);
	if (type == OS_FILE_READ) {
		if (os_aio_use_native_aio) {
#ifdef WIN_ASYNC_IO
2277
			os_n_file_reads++;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2278 2279
			os_bytes_read_since_printout += len;
			
2280 2281 2282 2283 2284
			ret = ReadFile(file, buf, (DWORD)n, &len,
							&(slot->control));
#elif defined(POSIX_ASYNC_IO)
			slot->control.aio_lio_opcode = LIO_READ;
			err = (ulint) aio_read(&(slot->control));
2285
			fprintf(stderr, "Starting POSIX aio read %lu\n", err);
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
#endif
		} else {
			if (!wake_later) {
				os_aio_simulated_wake_handler_thread(
				 os_aio_get_segment_no_from_slot(array, slot));
			}
		}
	} else if (type == OS_FILE_WRITE) {
		if (os_aio_use_native_aio) {
#ifdef WIN_ASYNC_IO
2296
			os_n_file_writes++;
2297 2298 2299 2300 2301
			ret = WriteFile(file, buf, (DWORD)n, &len,
							&(slot->control));
#elif defined(POSIX_ASYNC_IO)
			slot->control.aio_lio_opcode = LIO_WRITE;
			err = (ulint) aio_write(&(slot->control));
2302
			fprintf(stderr, "Starting POSIX aio write %lu\n", err);
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
#endif
		} else {
			if (!wake_later) {
				os_aio_simulated_wake_handler_thread(
				 os_aio_get_segment_no_from_slot(array, slot));
			}
		}
	} else {
		ut_error;
	}

#ifdef WIN_ASYNC_IO
	if (os_aio_use_native_aio) {
		if ((ret && len == n)
2317
		    || (!ret && GetLastError() == ERROR_IO_PENDING)) {
2318 2319 2320 2321 2322 2323 2324
			/* aio was queued successfully! */
		
	    		if (mode == OS_AIO_SYNC) {
	    		    /* We want a synchronous i/o operation on a file
	    		    where we also use async i/o: in Windows we must
	    		    use the same wait mechanism as for async i/o */
	    		
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2325
	    		    retval = os_aio_windows_handle(ULINT_UNDEFINED,
2326 2327
					slot->pos,
		    			&dummy_mess1, &dummy_mess2,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2328 2329 2330
					&dummy_type);

			    return(retval);
2331 2332 2333 2334 2335
	    		}

			return(TRUE);
		}

2336
		err = 1; /* Fall through the next if */
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
	}
#endif
	if (err == 0) {
		/* aio was queued successfully! */

		return(TRUE);
	}

	os_aio_array_free_slot(array, slot);

2347
	retry = os_file_handle_error(name,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2348
			type == OS_FILE_READ ? "aio read" : "aio write");
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
	if (retry) {

		goto try_again;
	}	

	return(FALSE);
}

#ifdef WIN_ASYNC_IO
/**************************************************************************
This function is only used in Windows asynchronous i/o.
Waits for an aio operation to complete. This function is used to wait the
for completed requests. The aio array of pending requests is divided
into segments. The thread specifies which segment or slot it wants to wait
for. NOTE: this function will also take care of freeing the aio slot,
therefore no other thread is allowed to do the freeing! */

ibool
os_aio_windows_handle(
/*==================*/
				/* out: TRUE if the aio operation succeeded */
	ulint	segment,	/* in: the number of the segment in the aio
				arrays to wait for; segment 0 is the ibuf
				i/o thread, segment 1 the log i/o thread,
				then follow the non-ibuf read threads, and as
				the last are the non-ibuf write threads; if
				this is ULINT_UNDEFINED, then it means that
				sync aio is used, and this parameter is
				ignored */
	ulint	pos,		/* this parameter is used only in sync aio:
				wait for the aio slot at this position */  
	void**	message1,	/* out: the messages passed with the aio
				request; note that also in the case where
				the aio operation failed, these output
				parameters are valid and can be used to
				restart the operation, for example */
2385 2386
	void**	message2,
	ulint*	type)		/* out: OS_FILE_WRITE or ..._READ */
2387
{
2388
	ulint		orig_seg	= segment;
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
	os_aio_array_t*	array;
	os_aio_slot_t*	slot;
	ulint		n;
	ulint		i;
	ibool		ret_val;
	BOOL		ret;
	DWORD		len;

	if (segment == ULINT_UNDEFINED) {
		array = os_aio_sync_array;
		segment = 0;
	} else {
		segment = os_aio_get_array_and_local_segment(&array, segment);
	}
	
	/* NOTE! We only access constant fields in os_aio_array. Therefore
	we do not have to acquire the protecting mutex yet */

	ut_ad(os_aio_validate());
	ut_ad(segment < array->n_segments);

	n = array->n_slots / array->n_segments;

	if (array == os_aio_sync_array) {
2413
		os_event_wait(os_aio_array_get_nth_slot(array, pos)->event);
2414 2415
		i = pos;
	} else {
2416
		srv_set_io_thread_op_info(orig_seg, "wait Windows aio");
2417 2418
		i = os_event_wait_multiple(n,
				(array->native_events) + segment * n);
2419 2420 2421 2422 2423 2424 2425 2426
	}

	os_mutex_enter(array->mutex);

	slot = os_aio_array_get_nth_slot(array, i + segment * n);

	ut_a(slot->reserved);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2427
	if (orig_seg != ULINT_UNDEFINED) {
2428 2429
		srv_set_io_thread_op_info(orig_seg,
					"get windows aio return value");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2430 2431
	}

2432 2433 2434 2435 2436
	ret = GetOverlappedResult(slot->file, &(slot->control), &len, TRUE);

	*message1 = slot->message1;
	*message2 = slot->message2;

2437 2438
	*type = slot->type;

2439 2440
	if (ret && len == slot->len) {
		ret_val = TRUE;
2441

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2442 2443
		if (slot->type == OS_FILE_WRITE
				&& !os_do_not_call_flush_at_each_write) {
2444 2445
		         ut_a(TRUE == os_file_flush(slot->file));
		}
2446
	} else {
2447
		os_file_handle_error(slot->name, "Windows aio");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2448
		
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
		ret_val = FALSE;
	}		  

	os_mutex_exit(array->mutex);

	os_aio_array_free_slot(array, slot);
	
	return(ret_val);
}
#endif

#ifdef POSIX_ASYNC_IO

/**************************************************************************
This function is only used in Posix asynchronous i/o. Waits for an aio
operation to complete. */

ibool
os_aio_posix_handle(
/*================*/
				/* out: TRUE if the aio operation succeeded */
	ulint	array_no,	/* in: array number 0 - 3 */
	void**	message1,	/* out: the messages passed with the aio
				request; note that also in the case where
				the aio operation failed, these output
				parameters are valid and can be used to
				restart the operation, for example */
	void**	message2)
{
	os_aio_array_t*	array;
	os_aio_slot_t*	slot;
	siginfo_t	info;
	sigset_t	sigset;
	sigset_t        proc_sigset;
	sigset_t        thr_sigset;
	int		ret;
	int             i;
	int             sig;
	
        sigemptyset(&sigset);
	sigaddset(&sigset, SIGRTMIN + 1 + array_no);

	pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);

	/*
	sigprocmask(0, NULL, &proc_sigset);
	pthread_sigmask(0, NULL, &thr_sigset);

	for (i = 32 ; i < 40; i++) {
2498
		fprintf(stderr, "%lu : %lu %lu\n", (ulint)i,
2499 2500 2501 2502 2503 2504 2505 2506 2507
		 (ulint)sigismember(&proc_sigset, i),
		 (ulint)sigismember(&thr_sigset, i));
	}
	*/

	ret = sigwaitinfo(&sigset, &info);

	if (sig != SIGRTMIN + 1 + array_no) {

2508
		ut_error;
2509 2510 2511 2512
	
		return(FALSE);
	}
	
2513
	fputs("Handling POSIX aio\n", stderr);
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525

	array = os_aio_get_array_from_no(array_no);

	os_mutex_enter(array->mutex);

	slot = info.si_value.sival_ptr;

	ut_a(slot->reserved);

	*message1 = slot->message1;
	*message2 = slot->message2;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2526 2527
	if (slot->type == OS_FILE_WRITE
				&& !os_do_not_call_flush_at_each_write) {
2528 2529 2530
		ut_a(TRUE == os_file_flush(slot->file));
	}

2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556
	os_mutex_exit(array->mutex);

	os_aio_array_free_slot(array, slot);
	
	return(TRUE);
}
#endif

/**************************************************************************
Does simulated aio. This function should be called by an i/o-handler
thread. */

ibool
os_aio_simulated_handle(
/*====================*/
				/* out: TRUE if the aio operation succeeded */
	ulint	global_segment,	/* in: the number of the segment in the aio
				arrays to wait for; segment 0 is the ibuf
				i/o thread, segment 1 the log i/o thread,
				then follow the non-ibuf read threads, and as
				the last are the non-ibuf write threads */
	void**	message1,	/* out: the messages passed with the aio
				request; note that also in the case where
				the aio operation failed, these output
				parameters are valid and can be used to
				restart the operation, for example */
2557 2558
	void**	message2,
	ulint*	type)		/* out: OS_FILE_WRITE or ..._READ */
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
{
	os_aio_array_t*	array;
	ulint		segment;
	os_aio_slot_t*	slot;
	os_aio_slot_t*	slot2;
	os_aio_slot_t*	consecutive_ios[OS_AIO_MERGE_N_CONSECUTIVE];
	ulint		n_consecutive;
	ulint		total_len;
	ulint		offs;
	ulint		lowest_offset;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2569 2570
	ulint		biggest_age;
	ulint		age;
2571
	byte*		combined_buf;
2572
	byte*		combined_buf2= 0;	/* Remove warning */
2573 2574 2575
	ibool		ret;
	ulint		n;
	ulint		i;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2576
	ulint		len2;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2577
	
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
	segment = os_aio_get_array_and_local_segment(&array, global_segment);
	
restart:
	/* NOTE! We only access constant fields in os_aio_array. Therefore
	we do not have to acquire the protecting mutex yet */

	ut_ad(os_aio_validate());
	ut_ad(segment < array->n_segments);

	n = array->n_slots / array->n_segments;

	/* Look through n slots after the segment * n'th slot */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2591 2592 2593 2594 2595 2596 2597 2598 2599
	if (array == os_aio_read_array
	    && os_aio_recommend_sleep_for_read_threads) {

		/* Give other threads chance to add several i/os to the array
		at once. */

		goto recommended_sleep;
	}
	
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609
	os_mutex_enter(array->mutex);

	/* Check if there is a slot for which the i/o has already been
	done */
	
	for (i = 0; i < n; i++) {
		slot = os_aio_array_get_nth_slot(array, i + segment * n);

		if (slot->reserved && slot->io_already_done) {

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2610 2611 2612 2613 2614
			if (os_aio_print_debug) {
				fprintf(stderr,
"InnoDB: i/o for slot %lu already done, returning\n", i);
			}

2615 2616 2617 2618 2619 2620 2621 2622
			ret = TRUE;
			
			goto slot_io_done;
		}
	}

	n_consecutive = 0;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2623 2624 2625
	/* If there are at least 2 seconds old requests, then pick the oldest
	one to prevent starvation. If several requests have the same age,
	then pick the one at the lowest offset. */
2626

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2627
	biggest_age = 0;
2628
	lowest_offset = ULINT_MAX;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2629

2630 2631 2632
	for (i = 0; i < n; i++) {
		slot = os_aio_array_get_nth_slot(array, i + segment * n);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2633 2634 2635
		if (slot->reserved) {
		        age = (ulint)difftime(time(NULL),
						slot->reservation_time);
2636

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
			if ((age >= 2 && age > biggest_age)
			    || (age >= 2 && age == biggest_age
			        && slot->offset < lowest_offset)) {

			        /* Found an i/o request */
				consecutive_ios[0] = slot;

				n_consecutive = 1;

				biggest_age = age;
				lowest_offset = slot->offset;
			}
		}
	}

	if (n_consecutive == 0) {
	        /* There were no old requests. Look for an i/o request at the
		lowest offset in the array (we ignore the high 32 bits of the
		offset in these heuristics) */

		lowest_offset = ULINT_MAX;
	
		for (i = 0; i < n; i++) {
		        slot = os_aio_array_get_nth_slot(array,
							i + segment * n);
2662

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2663
			if (slot->reserved && slot->offset < lowest_offset) {
2664

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2665 2666 2667 2668 2669 2670 2671
			        /* Found an i/o request */
				consecutive_ios[0] = slot;

				n_consecutive = 1;

				lowest_offset = slot->offset;
			}
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728
		}
	}

	if (n_consecutive == 0) {

		/* No i/o requested at the moment */

		goto wait_for_io;
	}

	slot = consecutive_ios[0];

	/* Check if there are several consecutive blocks to read or write */

consecutive_loop:	
	for (i = 0; i < n; i++) {
		slot2 = os_aio_array_get_nth_slot(array, i + segment * n);

		if (slot2->reserved && slot2 != slot
		    && slot2->offset == slot->offset + slot->len
		    && slot->offset + slot->len > slot->offset /* check that
						sum does not wrap over */
		    && slot2->offset_high == slot->offset_high
		    && slot2->type == slot->type
		    && slot2->file == slot->file) {

			/* Found a consecutive i/o request */

			consecutive_ios[n_consecutive] = slot2;
			n_consecutive++;

			slot = slot2;

			if (n_consecutive < OS_AIO_MERGE_N_CONSECUTIVE) {
			
				goto consecutive_loop;
			} else {
				break;
			}
		}
	}

	/* We have now collected n_consecutive i/o requests in the array;
	allocate a single buffer which can hold all data, and perform the
	i/o */

	total_len = 0;
	slot = consecutive_ios[0];
	
	for (i = 0; i < n_consecutive; i++) {
		total_len += consecutive_ios[i]->len;
	}

	if (n_consecutive == 1) {
		/* We can use the buffer of the i/o request */
		combined_buf = slot->buf;
	} else {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2729
		combined_buf2 = ut_malloc(total_len + UNIV_PAGE_SIZE);
2730

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2731 2732 2733
		ut_a(combined_buf2);

		combined_buf = ut_align(combined_buf2, UNIV_PAGE_SIZE);
2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752
	}
	
	/* We release the array mutex for the time of the i/o: NOTE that
	this assumes that there is just one i/o-handler thread serving
	a single segment of slots! */

	os_mutex_exit(array->mutex);

	if (slot->type == OS_FILE_WRITE && n_consecutive > 1) {
		/* Copy the buffers to the combined buffer */
		offs = 0;
		
		for (i = 0; i < n_consecutive; i++) {

			ut_memcpy(combined_buf + offs, consecutive_ios[i]->buf,
						consecutive_ios[i]->len);
			offs += consecutive_ios[i]->len;
		}
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2753
	
2754
	srv_set_io_thread_op_info(global_segment, "doing file i/o");
2755

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2756 2757 2758 2759 2760 2761 2762
	if (os_aio_print_debug) {
		fprintf(stderr,
"InnoDB: doing i/o of type %lu at offset %lu %lu, length %lu\n",
			slot->type, slot->offset_high, slot->offset,
			total_len);
	}

2763 2764
	/* Do the i/o with ordinary, synchronous i/o functions: */
	if (slot->type == OS_FILE_WRITE) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2765
		if (array == os_aio_write_array) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2766 2767
			if ((total_len % UNIV_PAGE_SIZE != 0)
			    || (slot->offset % UNIV_PAGE_SIZE != 0)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2768 2769 2770 2771
				fprintf(stderr,
"InnoDB: Error: trying a displaced write to %s %lu %lu, len %lu\n",
					slot->name, slot->offset_high,
					slot->offset, total_len);
2772
				ut_error;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2773 2774
			}
			  
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
			/* Do a 'last millisecond' check that the page end
			is sensible; reported page checksum errors from
			Linux seem to wipe over the page end */

			for (len2 = 0; len2 + UNIV_PAGE_SIZE <= total_len;
						len2 += UNIV_PAGE_SIZE) {
				if (mach_read_from_4(combined_buf + len2
						+ FIL_PAGE_LSN + 4)
				    != mach_read_from_4(combined_buf + len2
				    		+ UNIV_PAGE_SIZE
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2785
				    	- FIL_PAGE_END_LSN_OLD_CHKSUM + 4)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2786 2787 2788
				    	ut_print_timestamp(stderr);
				    	fprintf(stderr,
"  InnoDB: ERROR: The page to be written seems corrupt!\n");
2789 2790 2791
				    	fprintf(stderr,
"InnoDB: Writing a block of %lu bytes, currently writing at offset %lu\n",
					(ulong)total_len, (ulong)len2);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2792
					buf_page_print(combined_buf + len2);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2793 2794 2795 2796 2797 2798
				    	fprintf(stderr,
"InnoDB: ERROR: The page to be written seems corrupt!\n");
				}
			}
		}
	
2799 2800 2801 2802 2803 2804 2805 2806
		ret = os_file_write(slot->name, slot->file, combined_buf,
				slot->offset, slot->offset_high, total_len);
	} else {
		ret = os_file_read(slot->file, combined_buf,
				slot->offset, slot->offset_high, total_len);
	}

	ut_a(ret);
2807
	srv_set_io_thread_op_info(global_segment, "file i/o done");
2808

2809 2810 2811
/* fprintf(stderr,
	"aio: %lu consecutive %lu:th segment, first offs %lu blocks\n",
	n_consecutive, global_segment, slot->offset / UNIV_PAGE_SIZE); */
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825

	if (slot->type == OS_FILE_READ && n_consecutive > 1) {
		/* Copy the combined buffer to individual buffers */
		offs = 0;
		
		for (i = 0; i < n_consecutive; i++) {

			ut_memcpy(consecutive_ios[i]->buf, combined_buf + offs, 
						consecutive_ios[i]->len);
			offs += consecutive_ios[i]->len;
		}
	}

	if (n_consecutive > 1) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2826
		ut_free(combined_buf2);
2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847
	}

	os_mutex_enter(array->mutex);

	/* Mark the i/os done in slots */

	for (i = 0; i < n_consecutive; i++) {
		consecutive_ios[i]->io_already_done = TRUE;
	}

	/* We return the messages for the first slot now, and if there were
	several slots, the messages will be returned with subsequent calls
	of this function */
	
slot_io_done:

	ut_a(slot->reserved);

	*message1 = slot->message1;
	*message2 = slot->message2;

2848 2849
	*type = slot->type;

2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863
	os_mutex_exit(array->mutex);

	os_aio_array_free_slot(array, slot);
	
	return(ret);

wait_for_io:
	/* We wait here until there again can be i/os in the segment
	of this thread */
	
	os_event_reset(os_aio_segment_wait_events[global_segment]);

	os_mutex_exit(array->mutex);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2864
recommended_sleep:
2865
	srv_set_io_thread_op_info(global_segment, "waiting for i/o request");
2866

2867 2868
	os_event_wait(os_aio_segment_wait_events[global_segment]);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2869 2870 2871 2872 2873 2874
	if (os_aio_print_debug) {
		fprintf(stderr,
"InnoDB: i/o handler thread for i/o segment %lu wakes up\n",
			global_segment);
	}
	
2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
	goto restart;
}

/**************************************************************************
Validates the consistency of an aio array. */
static
ibool
os_aio_array_validate(
/*==================*/
				/* out: TRUE if ok */
	os_aio_array_t*	array)	/* in: aio wait array */
{
	os_aio_slot_t*	slot;
	ulint		n_reserved	= 0;
	ulint		i;
	
	ut_a(array);

	os_mutex_enter(array->mutex);

	ut_a(array->n_slots > 0);
	ut_a(array->n_segments > 0);
	
	for (i = 0; i < array->n_slots; i++) {
		slot = os_aio_array_get_nth_slot(array, i);
	
		if (slot->reserved) {
			n_reserved++;
			ut_a(slot->len > 0);
		}
	}

	ut_a(array->n_reserved == n_reserved);

	os_mutex_exit(array->mutex);

	return(TRUE);
}

/**************************************************************************
Validates the consistency the aio system. */

ibool
os_aio_validate(void)
/*=================*/
				/* out: TRUE if ok */
{
	os_aio_array_validate(os_aio_read_array);
	os_aio_array_validate(os_aio_write_array);
	os_aio_array_validate(os_aio_ibuf_array);
	os_aio_array_validate(os_aio_log_array);
	os_aio_array_validate(os_aio_sync_array);

	return(TRUE);
}

/**************************************************************************
Prints info of the aio arrays. */

void
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2935 2936
os_aio_print(
/*=========*/
2937
	FILE*	file)	/* in: file where to print */
2938 2939 2940 2941
{
	os_aio_array_t*	array;
	os_aio_slot_t*	slot;
	ulint		n_reserved;
2942 2943
	time_t		current_time;
	double		time_elapsed;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2944
	double		avg_bytes_read;
2945
	ulint		i;
2946

2947
	for (i = 0; i < srv_n_file_io_threads; i++) {
2948
		fprintf(file, "I/O thread %lu state: %s (%s)\n", i,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
2949 2950
					srv_io_thread_op_info[i],
					srv_io_thread_function[i]);
2951 2952
	}

2953
	fputs("Pending normal aio reads:", file);
2954

2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
	array = os_aio_read_array;
loop:
	ut_a(array);
	
	os_mutex_enter(array->mutex);

	ut_a(array->n_slots > 0);
	ut_a(array->n_segments > 0);
	
	n_reserved = 0;

	for (i = 0; i < array->n_slots; i++) {
		slot = os_aio_array_get_nth_slot(array, i);
	
		if (slot->reserved) {
			n_reserved++;
2971 2972 2973
			/* fprintf(stderr, "Reserved slot, messages %p %p\n",
				slot->message1, slot->message2); */
			ut_a(slot->len > 0);
2974 2975 2976 2977 2978
		}
	}

	ut_a(array->n_reserved == n_reserved);

2979
	fprintf(file, " %lu", n_reserved);
2980 2981 2982 2983
	
	os_mutex_exit(array->mutex);

	if (array == os_aio_read_array) {
2984
		fputs(", aio writes:", file);
2985
	
2986 2987 2988 2989 2990 2991
		array = os_aio_write_array;

		goto loop;
	}

	if (array == os_aio_write_array) {
2992
		fputs(",\n ibuf aio reads:", file);
2993 2994 2995 2996 2997 2998
		array = os_aio_ibuf_array;

		goto loop;
	}

	if (array == os_aio_ibuf_array) {
2999
		fputs(", log i/o's:", file);
3000 3001 3002 3003 3004 3005
		array = os_aio_log_array;

		goto loop;
	}

	if (array == os_aio_log_array) {
3006
		fputs(", sync i/o's:", file);
3007 3008 3009 3010
		array = os_aio_sync_array;

		goto loop;
	}
3011

3012
	putc('\n', file);
3013
	current_time = time(NULL);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3014
	time_elapsed = 0.001 + difftime(current_time, os_last_printout);
3015

3016 3017
	fprintf(file,
		"Pending flushes (fsync) log: %lu; buffer pool: %lu\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3018
		"%lu OS file reads, %lu OS file writes, %lu OS fsyncs\n",
3019
		fil_n_pending_log_flushes, fil_n_pending_tablespace_flushes,
3020
		os_n_file_reads, os_n_file_writes, os_n_fsyncs);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3021

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3022
	if (os_file_n_pending_preads != 0 || os_file_n_pending_pwrites != 0) {
3023
		fprintf(file,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3024 3025 3026 3027
		    "%lu pending preads, %lu pending pwrites\n",
		    os_file_n_pending_preads, os_file_n_pending_pwrites);
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3028 3029 3030 3031 3032 3033 3034
	if (os_n_file_reads == os_n_file_reads_old) {
		avg_bytes_read = 0.0;
	} else {
		avg_bytes_read = os_bytes_read_since_printout /
				(os_n_file_reads - os_n_file_reads_old);
	}

3035
	fprintf(file,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3036
"%.2f reads/s, %lu avg bytes/read, %.2f writes/s, %.2f fsyncs/s\n",
3037 3038
		(os_n_file_reads - os_n_file_reads_old)
		/ time_elapsed,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3039
		(ulint)avg_bytes_read,
3040 3041 3042 3043 3044 3045 3046 3047
		(os_n_file_writes - os_n_file_writes_old)
		/ time_elapsed,
		(os_n_fsyncs - os_n_fsyncs_old)
		/ time_elapsed);

	os_n_file_reads_old = os_n_file_reads;
	os_n_file_writes_old = os_n_file_writes;
	os_n_fsyncs_old = os_n_fsyncs;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3048
	os_bytes_read_since_printout = 0;
3049 3050
	
	os_last_printout = current_time;
3051 3052
}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067
/**************************************************************************
Refreshes the statistics used to print per-second averages. */

void
os_aio_refresh_stats(void)
/*======================*/
{
	os_n_file_reads_old = os_n_file_reads;
	os_n_file_writes_old = os_n_file_writes;
	os_n_fsyncs_old = os_n_fsyncs;
	os_bytes_read_since_printout = 0;
	
	os_last_printout = time(NULL);
}

3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
/**************************************************************************
Checks that all slots in the system have been freed, that is, there are
no pending io operations. */

ibool
os_aio_all_slots_free(void)
/*=======================*/
				/* out: TRUE if all free */
{
	os_aio_array_t*	array;
	ulint		n_res	= 0;
	
	array = os_aio_read_array;

	os_mutex_enter(array->mutex);

	n_res += array->n_reserved; 
	
	os_mutex_exit(array->mutex);

	array = os_aio_write_array;

	os_mutex_enter(array->mutex);

	n_res += array->n_reserved; 
	
	os_mutex_exit(array->mutex);

	array = os_aio_ibuf_array;

	os_mutex_enter(array->mutex);

	n_res += array->n_reserved; 
	
	os_mutex_exit(array->mutex);

	array = os_aio_log_array;

	os_mutex_enter(array->mutex);

	n_res += array->n_reserved; 
	
	os_mutex_exit(array->mutex);

	array = os_aio_sync_array;

	os_mutex_enter(array->mutex);

	n_res += array->n_reserved; 
	
	os_mutex_exit(array->mutex);

	if (n_res == 0) {

		return(TRUE);
	}

	return(FALSE);
}