| 1 |
libhugetlbfs HOWTO |
| 2 |
================== |
| 3 |
|
| 4 |
Author: David Gibson <[email protected]>, Adam Litke <[email protected]>, and others |
| 5 |
Last updated: December 07, 2011 |
| 6 |
|
| 7 |
Introduction |
| 8 |
============ |
| 9 |
|
| 10 |
In Linux(TM), access to hugepages is provided through a virtual file |
| 11 |
system, "hugetlbfs". The libhugetlbfs library interface works with |
| 12 |
hugetlbfs to provide more convenient specific application-level |
| 13 |
services. In particular libhugetlbfs has three main functions: |
| 14 |
|
| 15 |
* library functions |
| 16 |
libhugetlbfs provides functions that allow an applications to |
| 17 |
explicitly allocate and use hugepages more easily they could by |
| 18 |
directly accessing the hugetblfs filesystem |
| 19 |
|
| 20 |
* hugepage malloc() |
| 21 |
libhugetlbfs can be used to make an existing application use hugepages |
| 22 |
for all its malloc() calls. This works on an existing (dynamically |
| 23 |
linked) application binary without modification. |
| 24 |
|
| 25 |
* hugepage text/data/BSS |
| 26 |
libhugetlbfs, in conjunction with included special linker scripts can |
| 27 |
be used to make an application which will store its executable text, |
| 28 |
its initialized data or BSS, or all of the above in hugepages. This |
| 29 |
requires relinking an application, but does not require source-level |
| 30 |
modifications. |
| 31 |
|
| 32 |
This HOWTO explains how to use the libhugetlbfs library. It is for |
| 33 |
application developers or system administrators who wish to use any of |
| 34 |
the above functions. |
| 35 |
|
| 36 |
The libhugetlbfs library is a focal point to simplify and standardise |
| 37 |
the use of the kernel API. |
| 38 |
|
| 39 |
Prerequisites |
| 40 |
============= |
| 41 |
|
| 42 |
Hardware prerequisites |
| 43 |
---------------------- |
| 44 |
|
| 45 |
You will need a CPU with some sort of hugepage support, which is |
| 46 |
handled by your kernel. This covers recent x86, AMD64, 64-bit |
| 47 |
PowerPC(R) (POWER4, PPC970 and later), and IBM System z CPUs. |
| 48 |
|
| 49 |
Currently, only x86, AMD64 and PowerPC are fully supported by |
| 50 |
libhugetlbfs. IA64 and Sparc64 have a working malloc, and SH64 |
| 51 |
should also but it has not been tested. IA64, Sparc64, and SH64 |
| 52 |
do not support segment remapping at this time. IBM System z supports |
| 53 |
malloc and also segment remapping with --hugetlbfs-align. |
| 54 |
|
| 55 |
Kernel prerequisites |
| 56 |
-------------------- |
| 57 |
|
| 58 |
To use all the features of libhugetlbfs you will need a 2.6.16 or |
| 59 |
later kernel. Many things will work with earlier kernels, but they |
| 60 |
have important bugs and missing features. The later sections of the |
| 61 |
HOWTO assume a 2.6.16 or later kernel. The kernel must also have |
| 62 |
hugepages enabled, that is to say the CONFIG_HUGETLB_PAGE and |
| 63 |
CONFIG_HUGETLBFS options must be switched on. |
| 64 |
|
| 65 |
To check if hugetlbfs is enabled, use one of the following methods: |
| 66 |
|
| 67 |
* (Preferred) Use "grep hugetlbfs /proc/filesystems" to see if |
| 68 |
hugetlbfs is a supported file system. |
| 69 |
* On kernels which support /proc/config.gz (for example SLES10 |
| 70 |
kernels), you can search for the CONFIG_HUGETLB_PAGE and |
| 71 |
CONFIG_HUGETLBFS options in /proc/config.gz |
| 72 |
* Finally, attempt to mount hugetlbfs. If it works, the required |
| 73 |
hugepage support is enabled. |
| 74 |
|
| 75 |
Any kernel which meets the above test (even old ones) should support |
| 76 |
at least basic libhugetlbfs functions, although old kernels may have |
| 77 |
serious bugs. |
| 78 |
|
| 79 |
The MAP_PRIVATE flag instructs the kernel to return a memory area that |
| 80 |
is private to the requesting process. To use MAP_PRIVATE mappings, |
| 81 |
libhugetlbfs's automatic malloc() (morecore) feature, or the hugepage |
| 82 |
text, data, or BSS features, you will need a kernel with hugepage |
| 83 |
Copy-on-Write (CoW) support. The 2.6.16 kernel has this. |
| 84 |
|
| 85 |
PowerPC note: The malloc()/morecore features will generate warnings if |
| 86 |
used on PowerPC chips with a kernel where hugepage mappings don't |
| 87 |
respect the mmap() hint address (the "hint address" is the first |
| 88 |
parameter to mmap(), when MAP_FIXED is not specified; the kernel is |
| 89 |
not required to mmap() at this address, but should do so when |
| 90 |
possible). 2.6.16 and later kernels do honor the hint address. |
| 91 |
Hugepage malloc()/morecore should still work without this patch, but |
| 92 |
the size of the hugepage heap will be limited (to around 256M for |
| 93 |
32-bit and 1TB for 64-bit). |
| 94 |
|
| 95 |
The 2.6.27 kernel introduced support for multiple huge page sizes for |
| 96 |
systems with the appropriate hardware support. Unless specifically |
| 97 |
requested, libhugetlbfs will continue to use the default huge page size. |
| 98 |
|
| 99 |
Toolchain prerequisites |
| 100 |
----------------------- |
| 101 |
|
| 102 |
The library uses a number of GNU specific features, so you will need to use |
| 103 |
both gcc and GNU binutils. For PowerPC and AMD64 systems you will need a |
| 104 |
"biarch" compiler, which can build both 32-bit and 64-bit binaries. To use |
| 105 |
hugepage text and data segments, GNU binutils version 2.17 (or later) is |
| 106 |
recommended. Older versions will work with restricted functionality. |
| 107 |
|
| 108 |
Configuration prerequisites |
| 109 |
--------------------------- |
| 110 |
|
| 111 |
Direct access to hugepage pool has been deprecated in favor of the |
| 112 |
hugeadm utility. This utility can be used for finding the available |
| 113 |
hugepage pools and adjusting their minimum and maximum sizes depending |
| 114 |
on kernel support. |
| 115 |
|
| 116 |
To list all availabe hugepage pools and their current min and max values: |
| 117 |
hugeadm --pool-list |
| 118 |
|
| 119 |
To set the 2MB pool minimum to 10 pages: |
| 120 |
hugeadm --pool-pages-min 2MB:10 |
| 121 |
|
| 122 |
Note: that the max pool size will be adjusted to keep the same number of |
| 123 |
overcommit pages available if the kernel support is available when min |
| 124 |
pages are adjusted |
| 125 |
|
| 126 |
To add 15 pages to the maximum for 2MB pages: |
| 127 |
hugeadm --pool-pages-min 2MB:-5 |
| 128 |
|
| 129 |
For more information see man 8 hugeadm |
| 130 |
|
| 131 |
The raw kernel interfaces (as described below) are still available. |
| 132 |
|
| 133 |
In kernels before 2.6.24, hugepages must be allocated at boot-time via |
| 134 |
the hugepages= command-line parameter or at run-time via the |
| 135 |
/proc/sys/vm/nr_hugepages sysctl. If memory is restricted on the system, |
| 136 |
boot-time allocation is recommended. Hugepages so allocated will be in |
| 137 |
the static hugepage pool. |
| 138 |
|
| 139 |
In kernels starting with 2.6.24, the hugepage pool can grown on-demand. |
| 140 |
If this feature should be used, /proc/sys/vm/nr_overcommit_hugepages |
| 141 |
should be set to the maximum size of the hugepage pool. No hugepages |
| 142 |
need to be allocated via /proc/sys/vm/nr_hugepages or hugepages= in this |
| 143 |
case. Hugepages so allocated will be in the dynamic hugepage pool. |
| 144 |
|
| 145 |
For the running of the libhugetlbfs testsuite (see below), allocating 25 |
| 146 |
static hugepages is recommended. Due to memory restrictions, the number |
| 147 |
of hugepages requested may not be allocated if the allocation is |
| 148 |
attempted at run-time. Users should verify the actual number of |
| 149 |
hugepages allocated by: |
| 150 |
|
| 151 |
hugeadm --pool-list |
| 152 |
|
| 153 |
or |
| 154 |
|
| 155 |
grep HugePages_Total /proc/meminfo |
| 156 |
|
| 157 |
With 25 hugepages allocated, most tests should succeed. However, with |
| 158 |
smaller hugepages sizes, many more hugepages may be necessary. |
| 159 |
|
| 160 |
To use libhugetlbfs features, as well as to run the testsuite, hugetlbfs |
| 161 |
must be mounted. Each hugetlbfs mount point is associated with a page |
| 162 |
size. To choose the size, use the pagesize mount option. If this option |
| 163 |
is omitted, the default huge page size will be used. |
| 164 |
|
| 165 |
To mount the default huge page size: |
| 166 |
|
| 167 |
mkdir -p /mnt/hugetlbfs |
| 168 |
mount -t hugetlbfs none /mnt/hugetlbfs |
| 169 |
|
| 170 |
To mount 64KB pages (assuming hardware support): |
| 171 |
|
| 172 |
mkdir -p /mnt/hugetlbfs-64K |
| 173 |
mount -t hugetlbfs none -opagesize=64k /mnt/hugetlbfs-64K |
| 174 |
|
| 175 |
If hugepages should be available to non-root users, the permissions on |
| 176 |
the mountpoint need to be set appropriately. |
| 177 |
|
| 178 |
Installation |
| 179 |
============ |
| 180 |
|
| 181 |
1. Type "make" to build the library |
| 182 |
|
| 183 |
This will create "obj32" and/or "obj64" under the top level |
| 184 |
libhugetlbfs directory, and build, respectively, 32-bit and 64-bit |
| 185 |
shared and static versions (as applicable) of the library into each |
| 186 |
directory. This will also build (but not run) the testsuite. |
| 187 |
|
| 188 |
On i386 systems, only the 32-bit library will be built. On PowerPC |
| 189 |
and AMD64 systems, both 32-bit and 64-bit versions will be built (the |
| 190 |
32-bit AMD64 version is identical to the i386 version). |
| 191 |
|
| 192 |
2. Run the testsuite with "make check" |
| 193 |
|
| 194 |
Running the testsuite is a good idea to ensure that the library is |
| 195 |
working properly, and is quite quick (under 3 minutes on a 2GHz Apple |
| 196 |
G5). "make func" will run the just the functionality tests, rather |
| 197 |
than stress tests (a subset of "make check") which is much quicker. |
| 198 |
The testsuite contains tests both for the library's features and for |
| 199 |
the underlying kernel hugepage functionality. |
| 200 |
|
| 201 |
NOTE: The testsuite must be run as the root user. |
| 202 |
|
| 203 |
WARNING: The testsuite contains testcases explicitly designed to test |
| 204 |
for a number of hugepage related kernel bugs uncovered during the |
| 205 |
library's development. Some of these testcases WILL CRASH HARD a |
| 206 |
kernel without the relevant fixes. 2.6.16 contains all such fixes for |
| 207 |
all testcases included as of this writing. |
| 208 |
|
| 209 |
3. (Optional) Install to system paths with "make install" |
| 210 |
|
| 211 |
This will install the library images to the system lib/lib32/lib64 as |
| 212 |
appropriate, the helper utilities and the manual pages. By default |
| 213 |
it will install under /usr/local. To put it somewhere else use |
| 214 |
PREFIX=/path/to/install on the make command line. For example: |
| 215 |
|
| 216 |
make install PREFIX=/opt/hugetlbfs |
| 217 |
Will install under /opt/hugetlbfs. |
| 218 |
|
| 219 |
"make install" will also install the linker scripts and wrapper for ld |
| 220 |
used for hugepage test/data/BSS (see below for details). |
| 221 |
|
| 222 |
Alternatively, you can use the library from the directory in which it |
| 223 |
was built, using the LD_LIBRARY_PATH environment variable. |
| 224 |
|
| 225 |
To only install library with linker scripts, the manual pages or the helper |
| 226 |
utilities separetly, use the install-libs, install-man and install-bin targets |
| 227 |
respectively. This can be useful when you with to install the utilities but |
| 228 |
not override the distribution-supported version of libhugetlbfs for example. |
| 229 |
|
| 230 |
Usage |
| 231 |
===== |
| 232 |
|
| 233 |
Using hugepages for malloc() (morecore) |
| 234 |
--------------------------------------- |
| 235 |
|
| 236 |
This feature allows an existing (dynamically linked) binary executable |
| 237 |
to use hugepages for all its malloc() calls. To run a program using |
| 238 |
the automatic hugepage malloc() feature, you must set several |
| 239 |
environment variables: |
| 240 |
|
| 241 |
1. Set LD_PRELOAD=libhugetlbfs.so |
| 242 |
This tells the dynamic linker to load the libhugetlbfs shared |
| 243 |
library, even though the program wasn't originally linked against it. |
| 244 |
|
| 245 |
Note: If the program is linked against libhugetlbfs, preloading the |
| 246 |
library may lead to application crashes. You should skip this |
| 247 |
step in that case. |
| 248 |
|
| 249 |
2. Set LD_LIBRARY_PATH to the directory containing libhugetlbfs.so |
| 250 |
This is only necessary if you haven't installed libhugetlbfs.so to a |
| 251 |
system default path. If you set LD_LIBRARY_PATH, make sure the |
| 252 |
directory referenced contains the right version of the library |
| 253 |
(32-bit or 64-bit) as appropriate to the binary you want to run. |
| 254 |
|
| 255 |
3. Set HUGETLB_MORECORE |
| 256 |
This enables the hugepage malloc() feature, instructing libhugetlbfs |
| 257 |
to override libc's normal morecore() function with a hugepage |
| 258 |
version and use it for malloc(). From this point all malloc()s |
| 259 |
should come from hugepage memory until it runs out. This option can |
| 260 |
be specified in two ways: |
| 261 |
|
| 262 |
To use the default huge page size: |
| 263 |
HUGETLB_MORECORE=yes |
| 264 |
|
| 265 |
To use a specific huge page size: |
| 266 |
HUGETLB_MORECORE=<pagesize> |
| 267 |
|
| 268 |
To use Transparent Huge Pages (THP): |
| 269 |
HUGETLB_MORECORE=thp |
| 270 |
|
| 271 |
Note: This option requires a kernel that supports Transparent Huge Pages |
| 272 |
|
| 273 |
Usually it's preferable to set these environment variables on the |
| 274 |
command line of the program you wish to run, rather than using |
| 275 |
"export", because you'll only want to enable the hugepage malloc() for |
| 276 |
particular programs, not everything. |
| 277 |
|
| 278 |
Examples: |
| 279 |
|
| 280 |
If you've installed libhugetlbfs in the default place (under |
| 281 |
/usr/local) which is in the system library search path use: |
| 282 |
$ LD_PRELOAD=libhugetlbfs.so HUGETLB_MORECORE=yes <your app command line> |
| 283 |
|
| 284 |
If you have built libhugetlbfs in ~/libhugetlbfs and haven't installed |
| 285 |
it yet, the following would work for a 64-bit program: |
| 286 |
|
| 287 |
$ LD_PRELOAD=libhugetlbfs.so LD_LIBRARY_PATH=~/libhugetlbfs/obj64 \ |
| 288 |
HUGETLB_MORECORE=yes <your app command line> |
| 289 |
|
| 290 |
Under some circumstances, you might want to specify the address where |
| 291 |
the hugepage heap is located. You can do this by setting the |
| 292 |
HUGETLB_MORECORE_HEAPBASE environment variable to the heap address in |
| 293 |
hexadecimal. NOTE: this will not work on PowerPC systems with old kernels |
| 294 |
which don't respect the hugepage hint address; see Kernel Prerequisites |
| 295 |
above. Also note that this option is ignored for THP morecore. |
| 296 |
|
| 297 |
By default, the hugepage heap begins at roughly the same place a |
| 298 |
normal page heap would, rounded up by an amount determined by your |
| 299 |
platform. For 32-bit PowerPC binaries the normal page heap address is |
| 300 |
rounded-up to a multiple of 256MB (that is, putting it in the next MMU |
| 301 |
segment); for 64-bit PowerPC binaries the address is rounded-up to a |
| 302 |
multiple of 1TB. On all other platforms the address is rounded-up to |
| 303 |
the size of a hugepage. |
| 304 |
|
| 305 |
By default, the hugepage heap will be prefaulted by libhugetlbfs to |
| 306 |
guarantee enough hugepages exist and are reserved for the application |
| 307 |
(if this was not done, applications could receive a SIGKILL signal if |
| 308 |
hugepages needed for the heap are used by another application before |
| 309 |
they are faulted in). This leads to local-node allocations when no |
| 310 |
memory policy is in place for hugepages. Therefore, it is recommended to |
| 311 |
use |
| 312 |
|
| 313 |
$ numactl --interleave=all <your app command line> |
| 314 |
|
| 315 |
to regain some of the performance impact of local-node allocations on |
| 316 |
large NUMA systems. This can still result in poor performance for those |
| 317 |
applications which carefully place their threads on particular nodes |
| 318 |
(such as by using OpenMP). In that case, thread-local allocation is |
| 319 |
preferred so users should select a memory policy that corresponds to |
| 320 |
the run-time behavior of the process' CPU usage. Users can specify |
| 321 |
HUGETLB_NO_PREFAULT to prevent the prefaulting of hugepages and instead |
| 322 |
rely on run-time faulting of hugepages. NOTE: specifying |
| 323 |
HUGETLB_NO_PREFAULT on a system where hugepages are available to and |
| 324 |
used by many process can result in some applications receving SIGKILL, |
| 325 |
so its use is not recommended in high-availability or production |
| 326 |
environments. |
| 327 |
|
| 328 |
By default, the hugepage heap does not shrink. To enable hugepage heap |
| 329 |
shrinking, set HUGETLB_MORECORE_SHRINK=yes. NB: We have been seeing some |
| 330 |
unexpected behavior from glibc's malloc when this is enabled. |
| 331 |
|
| 332 |
Using hugepage shared memory |
| 333 |
---------------------------- |
| 334 |
|
| 335 |
Hugepages are used for shared memory segments if the SHM_HUGETLB flag is |
| 336 |
set when calling shmget() and the pool is large enough. For hugepage-unaware |
| 337 |
applications, libhugetlbfs overrides shmget and adds the SHM_HUGETLB if the |
| 338 |
environment variable HUGETLB_SHM is set to "yes". The steps to use hugepages |
| 339 |
with applications not linked to libhugetlbfs are similar to morecore except |
| 340 |
for step 3. |
| 341 |
|
| 342 |
1. Set LD_PRELOAD=libhugetlbfs.so |
| 343 |
This tells the dynamic linker to load the libhugetlbfs shared |
| 344 |
library, even though the program wasn't originally linked against it. |
| 345 |
|
| 346 |
Note: If the program is linked against libhugetlbfs, preloading the |
| 347 |
library may lead to application crashes. You should skip this |
| 348 |
step in that case. |
| 349 |
|
| 350 |
2. Set LD_LIBRARY_PATH to the directory containing libhugetlbfs.so |
| 351 |
This is only necessary if you haven't installed libhugetlbfs.so to a |
| 352 |
system default path. If you set LD_LIBRARY_PATH, make sure the |
| 353 |
directory referenced contains the right version of the library |
| 354 |
(32-bit or 64-bit) as appropriate to the binary you want to run. |
| 355 |
|
| 356 |
3. Set HUGETLB_SHM=yes |
| 357 |
The shmget() call is overridden whether the application is linked or the |
| 358 |
libhugetlbfs library is preloaded. When this environment variable is set, |
| 359 |
the SHM_HUGETLB flag is added to the call and the size parameter is aligned |
| 360 |
to back the shared memory segment with huge pages. In the event hugepages |
| 361 |
cannot be used, small pages will be used instead and a warning will be |
| 362 |
printed to explain the failure. |
| 363 |
|
| 364 |
Note: It is not possible to select any huge page size other than the |
| 365 |
system default for this option. If the kernel supports multiple |
| 366 |
huge page sizes, the size used for shared memory can be changed by |
| 367 |
altering the default huge page size via the default_hugepagesz |
| 368 |
kernel boot parameter. |
| 369 |
|
| 370 |
Using hugepage text, data, or BSS |
| 371 |
--------------------------------- |
| 372 |
|
| 373 |
To use the hugepage text, data, or BSS segments feature, you need to specially |
| 374 |
link your application. How this is done depends on the version of GNU ld. To |
| 375 |
support ld versions older than 2.17, libhugetlbfs provides custom linker |
| 376 |
scripts that must be used to achieve the required binary layout. With version |
| 377 |
2.17 or later, the system default linker scripts should be used. |
| 378 |
|
| 379 |
To link an application for hugepages, you should use the the ld.hugetlbfs |
| 380 |
script included with libhugetlbfs in place of your normal linker. Without any |
| 381 |
special options this will simply invoke GNU ld with the same parameters. When |
| 382 |
it is invoked with options detailed in the following sections, ld.hugetlbfs |
| 383 |
will call the system linker with all of the options necessary to link for |
| 384 |
hugepages. If a custom linker script is required, it will also be selected. |
| 385 |
|
| 386 |
If you installed ld.hugetlbfs using "make install", or if you run it |
| 387 |
from the place where you built libhugetlbfs, it should automatically |
| 388 |
be able to find the libhugetlbfs linker scripts. Otherwise you may |
| 389 |
need to explicitly instruct it where to find the scripts with the |
| 390 |
option: |
| 391 |
--hugetlbfs-script-path=/path/to/scripts |
| 392 |
(The linker scripts are in the ldscripts/ subdirectory of the |
| 393 |
libhugetlbfs source tree). |
| 394 |
|
| 395 |
Linking the application with binutils-2.17 or later: |
| 396 |
---------------------------------------------------- |
| 397 |
|
| 398 |
This method will use the system default linker scripts. Only one linker option |
| 399 |
is required to prepare the application for hugepages: |
| 400 |
|
| 401 |
--hugetlbfs-align |
| 402 |
|
| 403 |
will instruct ld.hugetlbfs to call GNU ld with two options that increase the |
| 404 |
alignment of the resulting binary. For reference, the options passed to ld are: |
| 405 |
|
| 406 |
-z common-page-size=<value> and |
| 407 |
-z max-page-size=<value> |
| 408 |
|
| 409 |
Linking the application with binutils-2.16 or older: |
| 410 |
---------------------------------------------------- |
| 411 |
|
| 412 |
To link a program with a custom linker script, one of the following linker |
| 413 |
options should be specified: |
| 414 |
|
| 415 |
--hugetlbfs-link=B |
| 416 |
|
| 417 |
will link the application to store BSS data (only) into hugepages |
| 418 |
|
| 419 |
--hugetlbfs-link=BDT |
| 420 |
|
| 421 |
will link the application to store text, initialized data and BSS data |
| 422 |
into hugepages. |
| 423 |
|
| 424 |
These are the only two available options when using custom linker scripts. |
| 425 |
|
| 426 |
A note about the custom libhugetlbfs linker scripts: |
| 427 |
---------------------------------------------------- |
| 428 |
|
| 429 |
Linker scripts are usually distributed with GNU binutils and they may contain a |
| 430 |
partial implementation of new linker features. As binutils evolves, the linker |
| 431 |
scripts supplied with previous versions become obsolete and are upgraded. |
| 432 |
|
| 433 |
Libhugetlbfs distributes one set of linker scripts that must work across |
| 434 |
several Linux distributions and binutils versions. This has worked well for |
| 435 |
some time but binutils-2.17 (including some late 2.16 builds) have made changes |
| 436 |
that are impossible to accomodate without breaking the libhugetlbfs linker |
| 437 |
scripts for older versions of binutils. This is why the linker scripts (and |
| 438 |
the --hugetlbfs-link ld.hugetlbfs option) have been deprecated for binutils >= |
| 439 |
2.17 configurations. |
| 440 |
|
| 441 |
If you are using a late 2.16 binutils version (such as 2.16.91) and are |
| 442 |
experiencing problems with huge page text, data, and bss, you can check |
| 443 |
binutils for the incompatibility with the following command: |
| 444 |
|
| 445 |
ld --verbose | grep SPECIAL |
| 446 |
|
| 447 |
If any matches are returned, then the libhugetlbfs linker scripts may not work |
| 448 |
correctly. In this case you should upgrade to binutils >= 2.17 and use the |
| 449 |
--hugetlbfs-align linking method. |
| 450 |
|
| 451 |
Linking via gcc: |
| 452 |
---------------- |
| 453 |
|
| 454 |
In many cases it's normal to link an application by invoking gcc, |
| 455 |
which will then invoke the linker with appropriate options, rather |
| 456 |
than invoking ld directly. In such cases it's usually best to |
| 457 |
convince gcc to invoke the ld.hugetlbfs script instead of the system |
| 458 |
linker, rather than modifying your build procedure to invoke the |
| 459 |
ld.hugetlbfs directly; the compilers may often add special libraries |
| 460 |
or other linker options which can be fiddly to reproduce by hand. |
| 461 |
To make this easier, 'make install' will install ld.hugetlbfs into |
| 462 |
$PREFIX/share/libhugetlbfs and create an 'ld' symlink to it. |
| 463 |
|
| 464 |
Then with gcc, you invoke it as a linker with two options: |
| 465 |
|
| 466 |
-B $PREFIX/share/libhugetlbfs |
| 467 |
|
| 468 |
This option tells gcc to look in a non-standard location for the |
| 469 |
linker, thus finding our script rather than the normal linker. This |
| 470 |
can optionally be set in the CFLAGS environment variable. |
| 471 |
|
| 472 |
-Wl,--hugetlbfs-align |
| 473 |
OR -Wl,--hugetlbfs-link=B |
| 474 |
OR -Wl,--hugetlbfs-link=BDT |
| 475 |
|
| 476 |
This option instructs gcc to pass the option after the comma down to the |
| 477 |
linker, thus invoking the special behaviour of the ld.hugetblfs script. This |
| 478 |
can optionally be set in the LDFLAGS environment variable. |
| 479 |
|
| 480 |
If you use a compiler other than gcc, you will need to consult its |
| 481 |
documentation to see how to convince it to invoke ld.hugetlbfs in |
| 482 |
place of the system linker. |
| 483 |
|
| 484 |
Running the application: |
| 485 |
------------------------ |
| 486 |
|
| 487 |
The specially-linked application needs the libhugetlbfs library, so |
| 488 |
you might need to set the LD_LIBRARY_PATH environment variable so the |
| 489 |
application can locate libhugetlbfs.so. Depending on the method used to link |
| 490 |
the application, the HUGETLB_ELFMAP environment variable can be used to control |
| 491 |
how hugepages will be used. |
| 492 |
|
| 493 |
When using --hugetlbfs-link: |
| 494 |
---------------------------- |
| 495 |
|
| 496 |
The custom linker script determines which segments may be remapped into |
| 497 |
hugepages and this remapping will occur by default. The following setting will |
| 498 |
disable remapping entirely: |
| 499 |
|
| 500 |
HUGETLB_ELFMAP=no |
| 501 |
|
| 502 |
When using --hugetlbfs-align: |
| 503 |
----------------------------- |
| 504 |
|
| 505 |
This method of linking an application permits greater flexibility at runtime. |
| 506 |
Using HUGETLB_ELFMAP, it is possible to control which program segments are |
| 507 |
placed in hugepages. The following four settings will cause the indicated |
| 508 |
segments to be placed in hugepages: |
| 509 |
|
| 510 |
HUGETLB_ELFMAP=R Read-only segments (text) |
| 511 |
HUGETLB_ELFMAP=W Writable segments (data/BSS) |
| 512 |
HUGETLB_ELFMAP=RW All segments (text/data/BSS) |
| 513 |
HUGETLB_ELFMAP=no No segments |
| 514 |
|
| 515 |
It is possible to select specific huge page sizes for read-only and writable |
| 516 |
segments by using the following advanced syntax: |
| 517 |
|
| 518 |
HUGETLB_ELFMAP=[R[=<pagesize>]:[W[=<pagesize>]] |
| 519 |
|
| 520 |
For example: |
| 521 |
|
| 522 |
Place read-only segments into 64k pages and writable into 16M pages |
| 523 |
HUGETLB_ELFMAP=R=64k:W=16M |
| 524 |
|
| 525 |
Use the default for read-only segments, 1G pages for writable segments |
| 526 |
HUGETLB_ELFMAP=R:W=1G |
| 527 |
|
| 528 |
Use 16M pages for writable segments only |
| 529 |
HUGETLB_ELFMAP=W=16M |
| 530 |
|
| 531 |
Default remapping behavior: |
| 532 |
--------------------------- |
| 533 |
|
| 534 |
If --hugetlbfs-link was used to link an application, the chosen remapping mode |
| 535 |
is saved in the binary and becomes the default behavior. Setting |
| 536 |
HUGETLB_ELFMAP=no will disable all remapping and is the only way to modify the |
| 537 |
default behavior. |
| 538 |
|
| 539 |
For applications linked with --hugetlbfs-align, the default behavior is to not |
| 540 |
remap any segments into huge pages. To set or display the default remapping |
| 541 |
mode for a binary, the included hugeedit command can be used: |
| 542 |
|
| 543 |
hugeedit [options] target-executable |
| 544 |
options: |
| 545 |
--text,--data Remap the specified segment into huge pages by default |
| 546 |
--disable Do not remap any segments by default |
| 547 |
|
| 548 |
When target-executable is the only argument, hugeedit will display the default |
| 549 |
remapping mode without making any modifications. |
| 550 |
|
| 551 |
When a binary is remapped according to its default remapping policy, the |
| 552 |
system default huge page size will be used. |
| 553 |
|
| 554 |
Environment variables: |
| 555 |
---------------------- |
| 556 |
|
| 557 |
There are a number of private environment variables which can affect |
| 558 |
libhugetlbfs: |
| 559 |
HUGETLB_DEFAULT_PAGE_SIZE |
| 560 |
Override the system default huge page size for all uses |
| 561 |
except hugetlb-backed shared memory |
| 562 |
|
| 563 |
HUGETLB_RESTRICT_EXE |
| 564 |
By default, libhugetlbfs will act on any program that it |
| 565 |
is loaded with, either via LD_PRELOAD or by explicitly |
| 566 |
linking with -lhugetlbfs. |
| 567 |
|
| 568 |
There are situations in which it is desirable to restrict |
| 569 |
libhugetlbfs' actions to specific programs. For example, |
| 570 |
some ISV applications are wrapped in a series of scripts |
| 571 |
that invoke bash, python, and/or perl. It is more |
| 572 |
convenient to set the environment variables related |
| 573 |
to libhugetlbfs before invoking the wrapper scripts, |
| 574 |
yet this has the unintended and undesirable consequence |
| 575 |
of causing the script interpreters to use and consume |
| 576 |
hugepages. There is no obvious benefit to causing the |
| 577 |
script interpreters to use hugepages, and there is a |
| 578 |
clear disadvantage: fewer hugepages are available to |
| 579 |
the actual application. |
| 580 |
|
| 581 |
To address this scenario, set HUGETLB_RESTRICT_EXE to a |
| 582 |
colon-separated list of programs to which the other |
| 583 |
libhugetlbfs environment variables should apply. (If |
| 584 |
not set, libhugetlbfs will attempt to apply the requested |
| 585 |
actions to all programs.) For example, |
| 586 |
|
| 587 |
HUGETLB_RESTRICT_EXE="hpcc:long_hpcc" |
| 588 |
|
| 589 |
will restrict libhugetlbfs' actions to programs named |
| 590 |
/home/fred/hpcc and /bench/long_hpcc but not /usr/hpcc_no. |
| 591 |
|
| 592 |
HUGETLB_ELFMAP |
| 593 |
Control or disable segment remapping (see above) |
| 594 |
|
| 595 |
HUGETLB_MINIMAL_COPY |
| 596 |
If equal to "no", the entire segment will be copied; |
| 597 |
otherwise, only the necessary parts will be, which can |
| 598 |
be much more efficient (default) |
| 599 |
|
| 600 |
HUGETLB_FORCE_ELFMAP |
| 601 |
Explained in "Partial segment remapping" |
| 602 |
|
| 603 |
HUGETLB_MORECORE |
| 604 |
HUGETLB_MORECORE_HEAPBASE |
| 605 |
HUGETLB_NO_PREFAULT |
| 606 |
Explained in "Using hugepages for malloc() |
| 607 |
(morecore)" |
| 608 |
|
| 609 |
HUGETLB_VERBOSE |
| 610 |
Specify the verbosity level of debugging output from 1 |
| 611 |
to 99 (default is 1) |
| 612 |
HUGETLB_PATH |
| 613 |
Specify the path to the hugetlbfs mount point |
| 614 |
HUGETLB_SHARE |
| 615 |
Explained in "Sharing remapped segments" |
| 616 |
HUGETLB_DEBUG |
| 617 |
Set to 1 if an application segfaults. Gives very detailed output |
| 618 |
and runs extra diagnostics. |
| 619 |
|
| 620 |
Sharing remapped segments: |
| 621 |
-------------------------- |
| 622 |
|
| 623 |
By default, when libhugetlbfs uses anonymous, unlinked hugetlbfs files |
| 624 |
to store remapped program segment data. This means that if the same |
| 625 |
program is started multiple times using hugepage segments, multiple |
| 626 |
huge pages will be used to store the same program data. |
| 627 |
|
| 628 |
The reduce this wastage, libugetlbfs can be instructed to allow |
| 629 |
sharing segments between multiple invocations of a program. To do |
| 630 |
this, you must set the HUGETLB_SHARE variable must be set for all the |
| 631 |
processes in question. This variable has two possible values: |
| 632 |
anything but 1: the default, indicates no segments should be shared |
| 633 |
1: indicates that read-only segments (i.e. the program text, |
| 634 |
in most cases) should be shared, read-write segments (data and bss) |
| 635 |
will not be shared. |
| 636 |
|
| 637 |
If the HUGETLB_MINIMAL_COPY variable is set for any program using |
| 638 |
shared segments, it must be set to the same value for all invocations |
| 639 |
of that program. |
| 640 |
|
| 641 |
Segment sharing is implemented by creating persistent files in a |
| 642 |
hugetlbfs containing the necessary segment data. By default, these |
| 643 |
files are stored in a subdirectory of the first located hugetlbfs |
| 644 |
filesystem, named 'elflink-uid-XXX' where XXX is the uid of the |
| 645 |
process using sharing. This directory must be owned by the uid in |
| 646 |
question, and have mode 0700. If it doesn't exist, libhugetlbfs will |
| 647 |
create it automatically. This means that (by default) separate |
| 648 |
invocations of the same program by different users will not share huge |
| 649 |
pages. |
| 650 |
|
| 651 |
The location for storing the hugetlbfs page files can be changed by |
| 652 |
setting the HUGETLB_SHARE_PATH environment variable. If set, this |
| 653 |
variable must contain the path of an accessible, already created |
| 654 |
directory located in a hugetlbfs filesystem. The owner and mode of |
| 655 |
this directory are not checked, so this method can be used to allow |
| 656 |
processes of multiple uids to share huge pages. IMPORTANT SECURITY |
| 657 |
NOTE: any process sharing hugepages can insert arbitrary executable |
| 658 |
code into any other process sharing hugepages in the same directory. |
| 659 |
Therefore, when using HUGETLB_SHARE_PATH, the directory created *must* |
| 660 |
allow access only to a set of uids who are mutually trusted. |
| 661 |
|
| 662 |
The files created in hugetlbfs for sharing are persistent, and must be |
| 663 |
manually deleted to free the hugepages in question. Future versions |
| 664 |
of libhugetlbfs should include tools and scripts to automate this |
| 665 |
cleanup. |
| 666 |
|
| 667 |
Partial segment remapping |
| 668 |
------------------------- |
| 669 |
|
| 670 |
libhugetlbfs has limited support for remapping a normal, non-relinked |
| 671 |
binary's data, text and BSS into hugepages. To enable this feature, |
| 672 |
HUGETLB_FORCE_ELFMAP must be set to "yes". |
| 673 |
|
| 674 |
Partial segment remapping is not guaranteed to work. Most importantly, a |
| 675 |
binary's segments must be large enough even when not relinked by |
| 676 |
libhugetlbfs: |
| 677 |
|
| 678 |
architecture address minimum segment size |
| 679 |
------------ ------- -------------------- |
| 680 |
i386, x86_64 all hugepage size |
| 681 |
ppc32 all 256M |
| 682 |
ppc64 0-4G 256M |
| 683 |
ppc64 4G-1T 1020G |
| 684 |
ppc64 1T+ 1T |
| 685 |
|
| 686 |
The raw size, though, is not sufficient to indicate if the code will |
| 687 |
succeed, due to alignment. Since the binary is not relinked, however, |
| 688 |
this is relatively straightforward to 'test and see'. |
| 689 |
|
| 690 |
NOTE: You must use LD_PRELOAD to load libhugetlbfs.so when using |
| 691 |
partial remapping. |
| 692 |
|
| 693 |
|
| 694 |
Examples |
| 695 |
======== |
| 696 |
|
| 697 |
Example 1: Application Developer |
| 698 |
--------------------------------- |
| 699 |
|
| 700 |
To have a program use hugepages, complete the following steps: |
| 701 |
|
| 702 |
1. Make sure you are working with kernel 2.6.16 or greater. |
| 703 |
|
| 704 |
2. Modify the build procedure so your application is linked against |
| 705 |
libhugetlbfs. |
| 706 |
|
| 707 |
For the remapping, you link against the library with the appropriate |
| 708 |
linker script (if necessary or desired). Linking against the library |
| 709 |
should result in transparent usage of hugepages. |
| 710 |
|
| 711 |
Example 2: End Users and System Administrators |
| 712 |
----------------------------------------------- |
| 713 |
|
| 714 |
To have an application use libhugetlbfs, complete the following steps: |
| 715 |
|
| 716 |
1. Make sure you are using kernel 2.6.16. |
| 717 |
|
| 718 |
2. Make sure the library is in the path, which you can set with the |
| 719 |
LD_LIBRARY_PATH environment variable. You might need to set other |
| 720 |
environment variables, including LD_PRELOAD as described above. |
| 721 |
|
| 722 |
|
| 723 |
Troubleshooting |
| 724 |
=============== |
| 725 |
|
| 726 |
The library has a certain amount of debugging code built in, which can |
| 727 |
be controlled with the environment variable HUGETLB_VERBOSE. By |
| 728 |
default the debug level is "1" which means the library will only print |
| 729 |
relatively serious error messages. Setting HUGETLB_VERBOSE=2 or |
| 730 |
higher will enable more debug messages (at present 2 is the highest |
| 731 |
debug level, but that may change). Setting HUGETLB_VERBOSE=0 will |
| 732 |
silence the library completely, even in the case of errors - the only |
| 733 |
exception is in cases where the library has to abort(), which can |
| 734 |
happen if something goes wrong in the middle of unmapping and |
| 735 |
remapping segments for the text/data/bss feature. |
| 736 |
|
| 737 |
If an application fails to run, set the environment variable HUGETLB_DEBUG |
| 738 |
to 1. This causes additional diagnostics to be run. This information should |
| 739 |
be included when sending bug reports to the libhugetlbfs team. |
| 740 |
|
| 741 |
Specific Scenarios: |
| 742 |
------------------- |
| 743 |
|
| 744 |
ISSUE: When using the --hugetlbfs-align or -zmax-page-size link options, the |
| 745 |
linker complains about truncated relocations and the build fails. |
| 746 |
|
| 747 |
TRY: Compile the program with the --relax linker option. Either add |
| 748 |
-Wl,--relax to CFLAGS or --relax to LDFLAGS. |
| 749 |
|
| 750 |
ISSUE: When using the xB linker script with a 32 bit binary on an x86 host with |
| 751 |
NX support enabled, the binary segfaults. |
| 752 |
|
| 753 |
TRY: Recompiling with the --hugetlbfs-align options and use the new relinking |
| 754 |
method or booting your kernel with noexec32=off. |
| 755 |
|
| 756 |
|
| 757 |
Trademarks |
| 758 |
========== |
| 759 |
|
| 760 |
This work represents the view of the author and does not necessarily |
| 761 |
represent the view of IBM. |
| 762 |
|
| 763 |
PowerPC is a registered trademark of International Business Machines |
| 764 |
Corporation in the United States, other countries, or both. Linux is |
| 765 |
a trademark of Linus Torvalds in the United States, other countries, |
| 766 |
or both. |