os0proc.c 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/******************************************************
The interface to the operating system
process control primitives

(c) 1995 Innobase Oy

Created 9/30/1995 Heikki Tuuri
*******************************************************/

#include "os0proc.h"
#ifdef UNIV_NONINL
#include "os0proc.ic"
#endif

15 16 17 18 19 20 21 22
#include "ut0mem.h"
#include "ut0byte.h"


/*
How to get AWE to compile on Windows?
-------------------------------------

23 24
In the project settings of the innobase project the Visual C++ source,
__WIN2000__ has to be defined.
25

26 27
The Visual C++ has to be relatively recent and _WIN32_WINNT has to be
defined to a value >= 0x0500 when windows.h is included.
28

29
#define _WIN32_WINNT	0x0500
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

Where does AWE work?
-------------------

See the error message in os_awe_allocate_physical_mem().

How to assign privileges for mysqld to use AWE?
-----------------------------------------------

See the error message in os_awe_enable_lock_pages_in_mem().

Use Windows AWE functions in this order
---------------------------------------

(1) os_awe_enable_lock_pages_in_mem();
(2) os_awe_allocate_physical_mem();
(3) os_awe_allocate_virtual_mem_window();
(4) os_awe_map_physical_mem_to_window().

To test 'AWE' in a computer which does not have the AWE API,
you can compile with UNIV_SIMULATE_AWE defined in this file.
*/

#ifdef UNIV_SIMULATE_AWE
/* If we simulate AWE, we allocate the 'physical memory' here */
byte*		os_awe_simulate_mem;
ulint		os_awe_simulate_mem_size;
os_awe_t*	os_awe_simulate_page_info;
byte*		os_awe_simulate_window;
ulint		os_awe_simulate_window_size;
/* In simulated AWE the following contains a NULL pointer or a pointer
to a mapped 'physical page' for each 4 kB page in the AWE window */
byte**		os_awe_simulate_map;
#endif

65
#ifdef __WIN2000__
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
os_awe_t*	os_awe_page_info;
ulint		os_awe_n_pages;
byte*		os_awe_window;
ulint		os_awe_window_size;
#endif

/********************************************************************
Windows AWE support. Tries to enable the "lock pages in memory" privilege for
the current process so that the current process can allocate memory-locked
virtual address space to act as the window where AWE maps physical memory. */

ibool
os_awe_enable_lock_pages_in_mem(void)
/*=================================*/
				/* out: TRUE if success, FALSE if error;
				prints error info to stderr if no success */
{
#ifdef UNIV_SIMULATE_AWE

	return(TRUE);

87
#elif defined(__WIN2000__)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  	struct {
    	DWORD 			Count;
    	LUID_AND_ATTRIBUTES 	Privilege[1];
  	} 	Info;
	HANDLE	hProcess;
  	HANDLE	Token;
  	BOOL 	Result;

	hProcess = GetCurrentProcess();

  	/* Open the token of the current process */

  	Result = OpenProcessToken(hProcess,
                              TOKEN_ADJUST_PRIVILEGES,
                              &Token);
  	if (Result != TRUE) {
    		fprintf(stderr,
			"InnoDB: AWE: Cannot open process token, error %lu\n",
			(ulint)GetLastError());
    		return(FALSE);
  	}

  	Info.Count = 1;

    	Info.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;

  	/* Get the local unique identifier (LUID) of the SE_LOCK_MEMORY
	privilege */

  	Result = LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME,
                                  &(Info.Privilege[0].Luid));
  	if (Result != TRUE)  {
    		fprintf(stderr,
	"InnoDB: AWE: Cannot get local privilege value for %s, error %lu.\n",
			SE_LOCK_MEMORY_NAME, (ulint)GetLastError());

    		return(FALSE);
  	}

  	/* Try to adjust the privilege */

  	Result = AdjustTokenPrivileges(Token, FALSE,
                                   (PTOKEN_PRIVILEGES)&Info,
                                   0, NULL, NULL);
  	/* Check the result */

  	if (Result != TRUE)  {
    		fprintf(stderr,
		"InnoDB: AWE: Cannot adjust process token privileges, error %u.\n",
			GetLastError());
    		return(FALSE);
  	} else if (GetLastError() != ERROR_SUCCESS) {
      		fprintf(stderr,
"InnoDB: AWE: Cannot enable SE_LOCK_MEMORY privilege, error %lu.\n"
"InnoDB: In Windows XP Home you cannot use AWE. In Windows 2000 and XP\n"
"InnoDB: Professional you must go to the Control Panel, to\n"
"InnoDB: Security Settings, to Local Policies, and enable\n"
"InnoDB: the 'lock pages in memory' privilege for the user who runs\n"
"InnoDB: the MySQL server.\n", GetLastError());

		return(FALSE);
	}

	CloseHandle(Token);

	return(TRUE);
#else
155
#ifdef __WIN__
156 157 158 159
	fprintf(stderr,
"InnoDB: AWE: Error: to use AWE you must use a ...-nt MySQL executable.\n");
#endif	
	return(FALSE);
160
#endif
161
}
162

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/********************************************************************
Allocates physical RAM memory up to 64 GB in an Intel 32-bit x86
processor. */

ibool
os_awe_allocate_physical_mem(
/*=========================*/
				/* out: TRUE if success */
	os_awe_t** page_info,	/* out, own: array of opaque data containing
				the info for allocated physical memory pages;
				each allocated 4 kB physical memory page has
				one slot of type os_awe_t in the array */
	ulint	  n_megabytes)	/* in: number of megabytes to allocate */
{
#ifdef UNIV_SIMULATE_AWE
	os_awe_simulate_page_info = ut_malloc(sizeof(os_awe_t) *
		n_megabytes * ((1024 * 1024) / OS_AWE_X86_PAGE_SIZE));

	os_awe_simulate_mem = ut_align(ut_malloc(
					4096 + 1024 * 1024 * n_megabytes),
					4096);
	os_awe_simulate_mem_size = n_megabytes * 1024 * 1024;

	*page_info = os_awe_simulate_page_info;

	return(TRUE);

190
#elif defined(__WIN2000__)
191
	BOOL		bResult;
192
  	os_awe_t 	NumberOfPages;		/* Question: why does Windows
193 194 195 196
  						use the name ULONG_PTR for
  						a scalar integer type? Maybe
  						because we may also refer to
  						&NumberOfPages? */
197
  	os_awe_t 	NumberOfPagesInitial;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  	SYSTEM_INFO 	sSysInfo;
  	int 		PFNArraySize;

	if (n_megabytes > 64 * 1024) {

		fprintf(stderr,
"InnoDB: AWE: Error: tried to allocate %lu MB.\n"
"InnoDB: AWE cannot allocate more than 64 GB in any computer.\n", n_megabytes);

		return(FALSE);
	}

  	GetSystemInfo(&sSysInfo);  /* fill the system information structure */

  	if ((ulint)OS_AWE_X86_PAGE_SIZE != (ulint)sSysInfo.dwPageSize) {
		fprintf(stderr,
"InnoDB: AWE: Error: this computer has a page size of %lu.\n"
"InnoDB: Should be 4096 bytes for InnoDB AWE support to work.\n",
			(ulint)sSysInfo.dwPageSize);

		return(FALSE);
	}

  	/* Calculate the number of pages of memory to request */

  	NumberOfPages = n_megabytes * ((1024 * 1024) / OS_AWE_X86_PAGE_SIZE);
 
 	/* Calculate the size of page_info for allocated physical pages */

227
  	PFNArraySize = NumberOfPages * sizeof(os_awe_t);
228

229
   	*page_info = (os_awe_t*)HeapAlloc(GetProcessHeap(), 0, PFNArraySize);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

	if (*page_info == NULL) {
    		fprintf(stderr,
"InnoDB: AWE: Failed to allocate page info array from process heap, error %lu\n",
			(ulint)GetLastError());

    		return(FALSE);
  	}

	ut_total_allocated_memory += PFNArraySize;

  	/* Enable this process' privilege to lock pages to physical memory */

	if (!os_awe_enable_lock_pages_in_mem()) {

		return(FALSE);
	}

  	/* Allocate the physical memory */

  	NumberOfPagesInitial = NumberOfPages;

	os_awe_page_info = *page_info;
	os_awe_n_pages = (ulint)NumberOfPages;

	/* Compilation note: if the compiler complains the function is not
	defined, see the note at the start of this file */

 	bResult = AllocateUserPhysicalPages(GetCurrentProcess(),
                                       &NumberOfPages,
                                       *page_info);
  	if (bResult != TRUE) {
    		fprintf(stderr,
"InnoDB: AWE: Cannot allocate physical pages, error %lu.\n",
			(ulint)GetLastError());

    		return(FALSE);
  	}

  	if (NumberOfPagesInitial != NumberOfPages) {
    		fprintf(stderr,
"InnoDB: AWE: Error: allocated only %lu pages of %lu requested.\n"
"InnoDB: Check that you have enough free RAM.\n"
"InnoDB: In Windows XP Professional and 2000 Professional\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
274
"InnoDB: Windows PAE size is max 4 GB. In 2000 and .NET\n"
275 276 277
"InnoDB: Advanced Servers and 2000 Datacenter Server it is 32 GB,\n"
"InnoDB: and in .NET Datacenter Server it is 64 GB.\n"
"InnoDB: A Microsoft web page said that the processor must be an Intel\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
278
"InnoDB: processor.\n",
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
			(ulint)NumberOfPages,
			(ulint)NumberOfPagesInitial);

    		return(FALSE);
  	}

	fprintf(stderr,
"InnoDB: Using Address Windowing Extensions (AWE); allocated %lu MB\n",
		n_megabytes);

	return(TRUE);	
#else
	return(FALSE);
#endif
}

/********************************************************************
Allocates a window in the virtual address space where we can map then
pages of physical memory. */

byte*
os_awe_allocate_virtual_mem_window(
/*===============================*/
			/* out, own: allocated memory, or NULL if did not
			succeed */
	ulint	size)	/* in: virtual memory allocation size in bytes, must
			be < 2 GB */
{
#ifdef UNIV_SIMULATE_AWE
	ulint	i;

	os_awe_simulate_window = ut_align(ut_malloc(4096 + size), 4096);
	os_awe_simulate_window_size = size;

	os_awe_simulate_map = ut_malloc(sizeof(byte*) * (size / 4096));

	for (i = 0; i < (size / 4096); i++) {
		*(os_awe_simulate_map + i) = NULL;
	}

	return(os_awe_simulate_window);
	
321
#elif defined(__WIN2000__)
322 323
	byte*	ptr;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
324
	if (size > (ulint)0x7FFFFFFFUL) {
325 326 327 328 329 330 331 332 333 334 335
		fprintf(stderr,
"InnoDB: AWE: Cannot allocate %lu bytes of virtual memory\n", size);

		return(NULL);
	}
	
	ptr = VirtualAlloc(NULL, (SIZE_T)size, MEM_RESERVE | MEM_PHYSICAL,
							PAGE_READWRITE);
	if (ptr == NULL) {
		fprintf(stderr,
"InnoDB: AWE: Cannot allocate %lu bytes of virtual memory, error %lu\n",
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
336
		size, (ulint)GetLastError());
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

		return(NULL);
	}

	os_awe_window = ptr;
	os_awe_window_size = size;

	ut_total_allocated_memory += size;

	return(ptr);
#else
	return(NULL);
#endif
}

/********************************************************************
With this function you can map parts of physical memory allocated with
the ..._allocate_physical_mem to the virtual address space allocated with
the previous function. Intel implements this so that the process page
tables are updated accordingly. A test on a 1.5 GHz AMD processor and XP
showed that this takes < 1 microsecond, much better than the estimated 80 us
for copying a 16 kB page memory to memory. But, the operation will at least
partially invalidate the translation lookaside buffer (TLB) of all
processors. Under a real-world load the performance hit may be bigger. */

ibool
os_awe_map_physical_mem_to_window(
/*==============================*/
					/* out: TRUE if success; the function
					calls exit(1) in case of an error */
	byte*		ptr,		/* in: a page-aligned pointer to
					somewhere in the virtual address
					space window; we map the physical mem
					pages here */
	ulint		n_mem_pages,	/* in: number of 4 kB mem pages to
					map */
	os_awe_t*	page_info)	/* in: array of page infos for those
					pages; each page has one slot in the
					array */
{
#ifdef UNIV_SIMULATE_AWE
	ulint	i;
	byte**	map;
	byte*	page;
	byte*	phys_page;

	ut_a(ptr >= os_awe_simulate_window);
	ut_a(ptr < os_awe_simulate_window + os_awe_simulate_window_size);
	ut_a(page_info >= os_awe_simulate_page_info);
	ut_a(page_info < os_awe_simulate_page_info +
			 		(os_awe_simulate_mem_size / 4096));

	/* First look if some other 'physical pages' are mapped at ptr,
	and copy them back to where they were if yes */

	map = os_awe_simulate_map
			+ ((ulint)(ptr - os_awe_simulate_window)) / 4096;
	page = ptr;
		
	for (i = 0; i < n_mem_pages; i++) {
		if (*map != NULL) {
			ut_memcpy(*map, page, 4096);
		}
		map++;
		page += 4096;
	}

	/* Then copy to ptr the 'physical pages' determined by page_info; we
	assume page_info is a segment of the array we created at the start */

	phys_page = os_awe_simulate_mem
			+ (ulint)(page_info - os_awe_simulate_page_info)
			  * 4096;

	ut_memcpy(ptr, phys_page, n_mem_pages * 4096);

	/* Update the map */

	map = os_awe_simulate_map
			+ ((ulint)(ptr - os_awe_simulate_window)) / 4096;

	for (i = 0; i < n_mem_pages; i++) {
		*map = phys_page;

		map++;
		phys_page += 4096;
	}

	return(TRUE);
	
427
#elif defined(__WIN2000__)
428
	BOOL		bResult;
429
	os_awe_t	n_pages;
430

431
	n_pages = (os_awe_t)n_mem_pages;
432 433 434 435 436 437 438 439 440 441 442 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
	
	if (!(ptr >= os_awe_window)) {
		fprintf(stderr,
"InnoDB: AWE: Error: trying to map to address %lx but AWE window start %lx\n",
		(ulint)ptr, (ulint)os_awe_window);
		ut_a(0);
	}

	if (!(ptr <= os_awe_window + os_awe_window_size - UNIV_PAGE_SIZE)) {
		fprintf(stderr,
"InnoDB: AWE: Error: trying to map to address %lx but AWE window end %lx\n",
		(ulint)ptr, (ulint)os_awe_window + os_awe_window_size);
		ut_a(0);
	}

	if (!(page_info >= os_awe_page_info)) {
		fprintf(stderr,
"InnoDB: AWE: Error: trying to map page info at %lx but array start %lx\n",
		(ulint)page_info, (ulint)os_awe_page_info);
		ut_a(0);
	}

	if (!(page_info <= os_awe_page_info + (os_awe_n_pages - 4))) {
		fprintf(stderr,
"InnoDB: AWE: Error: trying to map page info at %lx but array end %lx\n",
		(ulint)page_info, (ulint)(os_awe_page_info + os_awe_n_pages));
		ut_a(0);
	}

	bResult = MapUserPhysicalPages((PVOID)ptr, n_pages, page_info);

	if (bResult != TRUE) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
"  InnoDB: AWE: Mapping of %lu physical pages to address %lx failed,\n"
"InnoDB: error %lu.\n"
"InnoDB: Cannot continue operation.\n",
			n_mem_pages, (ulint)ptr, (ulint)GetLastError());
		exit(1);
	}

	return(TRUE);
#else
	return(FALSE);
#endif
}	
478

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
/********************************************************************
Converts the current process id to a number. It is not guaranteed that the
number is unique. In Linux returns the 'process number' of the current
thread. That number is the same as one sees in 'top', for example. In Linux
the thread id is not the same as one sees in 'top'. */

ulint
os_proc_get_number(void)
/*====================*/
{
#ifdef __WIN__
	return((ulint)GetCurrentProcessId());
#else
	return((ulint)getpid());
#endif
}

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 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 613 614 615 616 617 618 619 620 621 622 623 624 625
/********************************************************************
Allocates non-cacheable memory. */

void*
os_mem_alloc_nocache(
/*=================*/
			/* out: allocated memory */
	ulint	n)	/* in: number of bytes */
{
#ifdef __WIN__
	void*	ptr;

      	ptr = VirtualAlloc(NULL, n, MEM_COMMIT,
					PAGE_READWRITE | PAGE_NOCACHE);
	ut_a(ptr);

	return(ptr);
#else
	return(ut_malloc(n));
#endif
}

#ifdef notdefined
/********************************************************************
Creates a new process. */

ibool
os_process_create(
/*==============*/
	char*		name,	/* in: name of the executable to start
				or its full path name */
	char*		cmd,	/* in: command line for the starting
				process, or NULL if no command line
				specified */
	os_process_t*	proc,	/* out: handle to the process */
	os_process_id_t* id)	/* out: process id */

{
	BOOL			ret;
	PROCESS_INFORMATION	pinfo;
	STARTUPINFO		sinfo;

	/* The following assignments are default for the startupinfo
	structure */
	sinfo.cb		= sizeof(STARTUPINFO);
	sinfo.lpReserved	= NULL;
	sinfo.lpDesktop		= NULL;
	sinfo.cbReserved2	= 0;
	sinfo.lpReserved	= NULL;
	
	ret = CreateProcess(name,
			      cmd,
			      NULL,	/* No security attributes */
			      NULL,	/* No thread security attrs */
			      FALSE,	/* Do not inherit handles */
			      0,	/* No creation flags */
			      NULL,	/* No environment */
			      NULL,	/* Same current directory */
			      &sinfo,
			      &pinfo);

	*proc = pinfo.hProcess;
	*id   = pinfo.dwProcessId;

	return(ret);
}

/**************************************************************************
Exits a process. */

void
os_process_exit(
/*============*/
	ulint	code)	/* in: exit code */
{
	ExitProcess((UINT)code);
}

/**************************************************************************
Gets a process exit code. */

ibool
os_process_get_exit_code(
/*=====================*/
				/* out: TRUE if succeed, FALSE if fail */
	os_process_t	proc,	/* in: handle to the process */
	ulint*		code)	/* out: exit code */
{
	DWORD		ex_code;
	BOOL		ret;

	ret = GetExitCodeProcess(proc, &ex_code);

	*code = (ulint)ex_code;
	
	return(ret);
}
#endif /* notdedfined */

/********************************************************************
Sets the priority boost for threads released from waiting within the current
process. */

void
os_process_set_priority_boost(
/*==========================*/
	ibool	do_boost)	/* in: TRUE if priority boost should be done,
				FALSE if not */
{
#ifdef __WIN__
	ibool	no_boost;

	if (do_boost) {
		no_boost = FALSE;
	} else {
		no_boost = TRUE;
	}

	ut_a(TRUE == 1);

/* Does not do anything currently!
	SetProcessPriorityBoost(GetCurrentProcess(), no_boost);
*/
	printf(
        "Warning: process priority boost setting currently not functional!\n"
	);
#else
	UT_NOT_USED(do_boost);
#endif
}