1
/* Copyright (C) 2004 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "mysys_priv.h"
18
#ifdef HAVE_LARGE_PAGES
28
static uint my_get_large_page_size_int(void);
29
static uchar* my_large_malloc_int(size_t size, myf my_flags);
30
static my_bool my_large_free_int(uchar* ptr, myf my_flags);
32
/* Gets the size of large pages from the OS */
34
uint my_get_large_page_size(void)
37
DBUG_ENTER("my_get_large_page_size");
39
if (!(size = my_get_large_page_size_int()))
40
fprintf(stderr, "Warning: Failed to determine large page size\n");
46
General large pages allocator.
47
Tries to allocate memory from large pages pool and falls back to
48
my_malloc_lock() in case of failure
51
uchar* my_large_malloc(size_t size, myf my_flags)
54
DBUG_ENTER("my_large_malloc");
56
if (my_use_large_pages && my_large_page_size)
58
if ((ptr = my_large_malloc_int(size, my_flags)) != NULL)
60
if (my_flags & MY_WME)
61
fprintf(stderr, "Warning: Using conventional memory pool\n");
64
DBUG_RETURN(my_malloc_lock(size, my_flags));
68
General large pages deallocator.
69
Tries to deallocate memory as if it was from large pages pool and falls back
70
to my_free_lock() in case of failure
73
void my_large_free(uchar* ptr, myf my_flags __attribute__((unused)))
75
DBUG_ENTER("my_large_free");
78
my_large_free_int() can only fail if ptr was not allocated with
79
my_large_malloc_int(), i.e. my_malloc_lock() was used so we should free it
82
if (!my_use_large_pages || !my_large_page_size ||
83
!my_large_free_int(ptr, my_flags))
84
my_free_lock(ptr, my_flags);
89
#ifdef HUGETLB_USE_PROC_MEMINFO
90
/* Linux-specific function to determine the size of large pages */
92
uint my_get_large_page_size_int(void)
97
DBUG_ENTER("my_get_large_page_size_int");
99
if (!(f = my_fopen("/proc/meminfo", O_RDONLY, MYF(MY_WME))))
102
while (fgets(buf, sizeof(buf), f))
103
if (sscanf(buf, "Hugepagesize: %u kB", &size))
106
my_fclose(f, MYF(MY_WME));
109
DBUG_RETURN(size * 1024);
111
#endif /* HUGETLB_USE_PROC_MEMINFO */
113
#if HAVE_DECL_SHM_HUGETLB
114
/* Linux-specific large pages allocator */
116
uchar* my_large_malloc_int(size_t size, myf my_flags)
121
DBUG_ENTER("my_large_malloc_int");
123
/* Align block size to my_large_page_size */
124
size = ((size - 1) & ~(my_large_page_size - 1)) + my_large_page_size;
126
shmid = shmget(IPC_PRIVATE, size, SHM_HUGETLB | SHM_R | SHM_W);
129
if (my_flags & MY_WME)
131
"Warning: Failed to allocate %lu bytesx from HugeTLB memory."
132
" errno %d\n", (ulong) size, errno);
137
ptr = (uchar*) shmat(shmid, NULL, 0);
138
if (ptr == (uchar *) -1)
140
if (my_flags& MY_WME)
141
fprintf(stderr, "Warning: Failed to attach shared memory segment,"
142
" errno %d\n", errno);
143
shmctl(shmid, IPC_RMID, &buf);
149
Remove the shared memory segment so that it will be automatically freed
150
after memory is detached or process exits
152
shmctl(shmid, IPC_RMID, &buf);
157
/* Linux-specific large pages deallocator */
159
my_bool my_large_free_int(uchar *ptr, myf my_flags __attribute__((unused)))
161
DBUG_ENTER("my_large_free_int");
162
DBUG_RETURN(shmdt(ptr) == 0);
164
#endif /* HAVE_DECL_SHM_HUGETLB */
166
#endif /* HAVE_LARGE_PAGES */