mariadb/os/os0proc.c

402 lines
11 KiB
C
Raw Normal View History

2009-03-25 23:11:11 -07:00
/*****************************************************************************
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*****************************************************************************/
2009-09-07 10:22:53 +00:00
/**************************************************//**
@file os/os0proc.c
The interface to the operating system
process control primitives
Created 9/30/1995 Heikki Tuuri
*******************************************************/
#include "os0proc.h"
#ifdef UNIV_NONINL
#include "os0proc.ic"
#endif
#include "ut0mem.h"
#include "ut0byte.h"
/* FreeBSD for example has only MAP_ANON, Linux has MAP_ANONYMOUS and
MAP_ANON but MAP_ANON is marked as deprecated */
#if defined(MAP_ANONYMOUS)
#define OS_MAP_ANON MAP_ANONYMOUS
#elif defined(MAP_ANON)
#define OS_MAP_ANON MAP_ANON
#endif
UNIV_INTERN ibool os_use_large_pages;
/* Large page size. This may be a boot-time option on some platforms */
UNIV_INTERN ulint os_large_page_size;
2009-09-07 10:22:53 +00:00
/****************************************************************//**
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
2009-09-07 10:22:53 +00:00
the thread id is not the same as one sees in 'top'.
@return process id as a number */
UNIV_INTERN
ulint
os_proc_get_number(void)
/*====================*/
{
#ifdef __WIN__
return((ulint)GetCurrentProcessId());
#else
return((ulint)getpid());
#endif
}
2009-09-07 10:22:53 +00:00
/****************************************************************//**
Allocates large pages memory.
@return allocated memory */
UNIV_INTERN
void*
os_mem_alloc_large(
/*===============*/
2009-09-07 10:22:53 +00:00
ulint* n) /*!< in/out: number of bytes */
{
void* ptr;
ulint size;
#if defined HAVE_LARGE_PAGES && defined UNIV_LINUX
int shmid;
struct shmid_ds buf;
if (!os_use_large_pages || !os_large_page_size) {
goto skip;
}
/* Align block size to os_large_page_size */
ut_ad(ut_is_2pow(os_large_page_size));
size = ut_2pow_round(*n + (os_large_page_size - 1),
os_large_page_size);
shmid = shmget(IPC_PRIVATE, (size_t)size, SHM_HUGETLB | SHM_R | SHM_W);
if (shmid < 0) {
fprintf(stderr, "InnoDB: HugeTLB: Warning: Failed to allocate"
" %lu bytes. errno %d\n", size, errno);
ptr = NULL;
} else {
ptr = shmat(shmid, NULL, 0);
if (ptr == (void *)-1) {
fprintf(stderr, "InnoDB: HugeTLB: Warning: Failed to"
" attach shared memory segment, errno %d\n",
errno);
2010-01-06 04:00:14 -08:00
ptr = NULL;
}
/* Remove the shared memory segment so that it will be
automatically freed after memory is detached or
process exits */
shmctl(shmid, IPC_RMID, &buf);
}
if (ptr) {
*n = size;
2009-03-25 23:11:11 -07:00
os_fast_mutex_lock(&ut_list_mutex);
ut_total_allocated_memory += size;
2009-03-25 23:11:11 -07:00
os_fast_mutex_unlock(&ut_list_mutex);
# ifdef UNIV_SET_MEM_TO_ZERO
memset(ptr, '\0', size);
# endif
2009-03-25 23:11:11 -07:00
UNIV_MEM_ALLOC(ptr, size);
return(ptr);
}
fprintf(stderr, "InnoDB HugeTLB: Warning: Using conventional"
" memory pool\n");
skip:
#endif /* HAVE_LARGE_PAGES && UNIV_LINUX */
#ifdef __WIN__
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
/* Align block size to system page size */
ut_ad(ut_is_2pow(system_info.dwPageSize));
2009-03-25 23:11:11 -07:00
/* system_info.dwPageSize is only 32-bit. Casting to ulint is required
on 64-bit Windows. */
size = *n = ut_2pow_round(*n + (system_info.dwPageSize - 1),
2009-03-25 23:11:11 -07:00
(ulint) system_info.dwPageSize);
ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
if (!ptr) {
fprintf(stderr, "InnoDB: VirtualAlloc(%lu bytes) failed;"
" Windows error %lu\n",
(ulong) size, (ulong) GetLastError());
} else {
2009-03-25 23:11:11 -07:00
os_fast_mutex_lock(&ut_list_mutex);
ut_total_allocated_memory += size;
2009-03-25 23:11:11 -07:00
os_fast_mutex_unlock(&ut_list_mutex);
UNIV_MEM_ALLOC(ptr, size);
}
#elif defined __NETWARE__ || !defined OS_MAP_ANON
size = *n;
ptr = ut_malloc_low(size, TRUE, FALSE);
#else
# ifdef HAVE_GETPAGESIZE
size = getpagesize();
# else
size = UNIV_PAGE_SIZE;
# endif
/* Align block size to system page size */
ut_ad(ut_is_2pow(size));
size = *n = ut_2pow_round(*n + (size - 1), size);
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | OS_MAP_ANON, -1, 0);
if (UNIV_UNLIKELY(ptr == (void*) -1)) {
fprintf(stderr, "InnoDB: mmap(%lu bytes) failed;"
" errno %lu\n",
(ulong) size, (ulong) errno);
ptr = NULL;
} else {
2009-03-25 23:11:11 -07:00
os_fast_mutex_lock(&ut_list_mutex);
ut_total_allocated_memory += size;
2009-03-25 23:11:11 -07:00
os_fast_mutex_unlock(&ut_list_mutex);
UNIV_MEM_ALLOC(ptr, size);
}
#endif
return(ptr);
}
2009-09-07 10:22:53 +00:00
/****************************************************************//**
Frees large pages memory. */
UNIV_INTERN
void
os_mem_free_large(
/*==============*/
2009-09-07 10:22:53 +00:00
void *ptr, /*!< in: pointer returned by
os_mem_alloc_large() */
2009-09-07 10:22:53 +00:00
ulint size) /*!< in: size returned by
os_mem_alloc_large() */
{
2009-03-25 23:11:11 -07:00
os_fast_mutex_lock(&ut_list_mutex);
ut_a(ut_total_allocated_memory >= size);
2009-03-25 23:11:11 -07:00
os_fast_mutex_unlock(&ut_list_mutex);
#if defined HAVE_LARGE_PAGES && defined UNIV_LINUX
if (os_use_large_pages && os_large_page_size && !shmdt(ptr)) {
2009-03-25 23:11:11 -07:00
os_fast_mutex_lock(&ut_list_mutex);
ut_a(ut_total_allocated_memory >= size);
ut_total_allocated_memory -= size;
2009-03-25 23:11:11 -07:00
os_fast_mutex_unlock(&ut_list_mutex);
UNIV_MEM_FREE(ptr, size);
return;
}
#endif /* HAVE_LARGE_PAGES && UNIV_LINUX */
#ifdef __WIN__
2008-12-02 21:06:00 -08:00
/* When RELEASE memory, the size parameter must be 0.
Do not use MEM_RELEASE with MEM_DECOMMIT. */
if (!VirtualFree(ptr, 0, MEM_RELEASE)) {
fprintf(stderr, "InnoDB: VirtualFree(%p, %lu) failed;"
" Windows error %lu\n",
ptr, (ulong) size, (ulong) GetLastError());
} else {
2009-03-25 23:11:11 -07:00
os_fast_mutex_lock(&ut_list_mutex);
ut_a(ut_total_allocated_memory >= size);
ut_total_allocated_memory -= size;
2009-03-25 23:11:11 -07:00
os_fast_mutex_unlock(&ut_list_mutex);
UNIV_MEM_FREE(ptr, size);
}
#elif defined __NETWARE__ || !defined OS_MAP_ANON
ut_free(ptr);
#else
if (munmap(ptr, size)) {
fprintf(stderr, "InnoDB: munmap(%p, %lu) failed;"
" errno %lu\n",
ptr, (ulong) size, (ulong) errno);
} else {
2009-03-25 23:11:11 -07:00
os_fast_mutex_lock(&ut_list_mutex);
ut_a(ut_total_allocated_memory >= size);
ut_total_allocated_memory -= size;
2009-03-25 23:11:11 -07:00
os_fast_mutex_unlock(&ut_list_mutex);
UNIV_MEM_FREE(ptr, size);
}
#endif
}
/****************************************************************//**
Allocates or attaches and reuses shared memory segment.
The content is not cleared automatically.
@return allocated memory */
UNIV_INTERN
void*
os_shm_alloc(
/*=========*/
ulint* n, /*!< in/out: number of bytes */
uint key,
ibool* is_new)
{
void* ptr;
#if defined HAVE_SYS_IPC_H && HAVE_SYS_SHM_H
ulint size;
int shmid;
*is_new = FALSE;
fprintf(stderr,
"InnoDB: The shared memory segment containing the buffer pool is: key %#x (%d).\n",
key, key);
# if defined HAVE_LARGE_PAGES && defined UNIV_LINUX
if (!os_use_large_pages || !os_large_page_size) {
goto skip;
}
/* Align block size to os_large_page_size */
ut_ad(ut_is_2pow(os_large_page_size));
size = ut_2pow_round(*n + (os_large_page_size - 1),
os_large_page_size);
shmid = shmget((key_t)key, (size_t)size,
IPC_CREAT | IPC_EXCL | SHM_HUGETLB | SHM_R | SHM_W);
if (shmid < 0) {
if (errno == EEXIST) {
fprintf(stderr,
"InnoDB: HugeTLB: The shared memory segment exists.\n");
shmid = shmget((key_t)key, (size_t)size,
SHM_HUGETLB | SHM_R | SHM_W);
if (shmid < 0) {
fprintf(stderr,
"InnoDB: HugeTLB: Warning: Failed to allocate %lu bytes. (reuse) errno %d\n",
size, errno);
goto skip;
} else {
fprintf(stderr,
"InnoDB: HugeTLB: The existent shared memory segment is used.\n");
}
} else {
fprintf(stderr,
"InnoDB: HugeTLB: Warning: Failed to allocate %lu bytes. (new) errno %d\n",
size, errno);
goto skip;
}
} else {
*is_new = TRUE;
fprintf(stderr,
"InnoDB: HugeTLB: A new shared memory segment has been created .\n");
}
ptr = shmat(shmid, NULL, 0);
if (ptr == (void *)-1) {
fprintf(stderr,
"InnoDB: HugeTLB: Warning: Failed to attach shared memory segment, errno %d\n",
errno);
ptr = NULL;
}
if (ptr) {
*n = size;
os_fast_mutex_lock(&ut_list_mutex);
ut_total_allocated_memory += size;
os_fast_mutex_unlock(&ut_list_mutex);
UNIV_MEM_ALLOC(ptr, size);
return(ptr);
}
skip:
*is_new = FALSE;
# endif /* HAVE_LARGE_PAGES && defined UNIV_LINUX */
# ifdef HAVE_GETPAGESIZE
size = getpagesize();
# else
size = UNIV_PAGE_SIZE;
# endif
/* Align block size to system page size */
ut_ad(ut_is_2pow(size));
size = *n = ut_2pow_round(*n + (size - 1), size);
shmid = shmget((key_t)key, (size_t)size,
IPC_CREAT | IPC_EXCL | SHM_R | SHM_W);
if (shmid < 0) {
if (errno == EEXIST) {
fprintf(stderr,
"InnoDB: A shared memory segment containing the buffer pool seems to already exist.\n");
shmid = shmget((key_t)key, (size_t)size,
SHM_R | SHM_W);
if (shmid < 0) {
fprintf(stderr,
"InnoDB: Warning: Failed to allocate %lu bytes. (reuse) errno %d\n",
size, errno);
ptr = NULL;
goto end;
} else {
fprintf(stderr,
"InnoDB: The existent shared memory segment is used.\n");
}
} else {
fprintf(stderr,
"InnoDB: Warning: Failed to allocate %lu bytes. (new) errno %d\n",
size, errno);
ptr = NULL;
goto end;
}
} else {
*is_new = TRUE;
fprintf(stderr,
"InnoDB: A new shared memory segment has been created.\n");
}
ptr = shmat(shmid, NULL, 0);
if (ptr == (void *)-1) {
fprintf(stderr,
"InnoDB: Warning: Failed to attach shared memory segment, errno %d\n",
errno);
ptr = NULL;
}
if (ptr) {
*n = size;
os_fast_mutex_lock(&ut_list_mutex);
ut_total_allocated_memory += size;
os_fast_mutex_unlock(&ut_list_mutex);
UNIV_MEM_ALLOC(ptr, size);
}
end:
#else /* HAVE_SYS_IPC_H && HAVE_SYS_SHM_H */
fprintf(stderr, "InnoDB: shared memory segment is not supported.\n");
ptr = NULL;
#endif /* HAVE_SYS_IPC_H && HAVE_SYS_SHM_H */
return(ptr);
}
/****************************************************************//**
Detach shared memory segment. */
UNIV_INTERN
void
os_shm_free(
/*========*/
void *ptr, /*!< in: pointer returned by
os_shm_alloc() */
ulint size) /*!< in: size returned by
os_shm_alloc() */
{
os_fast_mutex_lock(&ut_list_mutex);
ut_a(ut_total_allocated_memory >= size);
os_fast_mutex_unlock(&ut_list_mutex);
#if defined HAVE_SYS_IPC_H && HAVE_SYS_SHM_H
if (!shmdt(ptr)) {
os_fast_mutex_lock(&ut_list_mutex);
ut_a(ut_total_allocated_memory >= size);
ut_total_allocated_memory -= size;
os_fast_mutex_unlock(&ut_list_mutex);
UNIV_MEM_FREE(ptr, size);
}
#else /* HAVE_SYS_IPC_H && HAVE_SYS_SHM_H */
fprintf(stderr, "InnoDB: shared memory segment is not supported.\n");
#endif /* HAVE_SYS_IPC_H && HAVE_SYS_SHM_H */
}