| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <string.h> |
| 34 |
#include <limits.h> |
| 35 |
|
| 36 |
#define _GNU_SOURCE |
| 37 |
#include <unistd.h> |
| 38 |
#include <getopt.h> |
| 39 |
|
| 40 |
#define REPORT(level, prefix, format, ...) \ |
| 41 |
do { \ |
| 42 |
if (verbose_level >= level) \ |
| 43 |
fprintf(stderr, "hugectl: " prefix ": " format, \ |
| 44 |
##__VA_ARGS__); \ |
| 45 |
} while (0); |
| 46 |
|
| 47 |
#include "libhugetlbfs_debug.h" |
| 48 |
|
| 49 |
extern int errno; |
| 50 |
extern int optind; |
| 51 |
extern char *optarg; |
| 52 |
|
| 53 |
#define OPTION(opts, text) fprintf(stderr, " %-25s %s\n", opts, text) |
| 54 |
#define CONT(text) fprintf(stderr, " %-25s %s\n", "", text) |
| 55 |
|
| 56 |
void print_usage() |
| 57 |
{ |
| 58 |
fprintf(stderr, "hugectl [options] target\n"); |
| 59 |
fprintf(stderr, "options:\n"); |
| 60 |
|
| 61 |
OPTION("--help, -h", "Prints this message"); |
| 62 |
OPTION("--verbose <level>, -v", "Increases/sets tracing levels"); |
| 63 |
|
| 64 |
OPTION("--text[=<size>]", "Requests remapping of the program text"); |
| 65 |
OPTION("--data[=<size>]", "Requests remapping of the program data"); |
| 66 |
OPTION("--bss[=<size>]", "Requests remapping of the program bss"); |
| 67 |
OPTION("--heap[=<size>]", "Requests remapping of the program heap"); |
| 68 |
CONT("(malloc space)"); |
| 69 |
OPTION("--shm", "Requests remapping of shared memory segments"); |
| 70 |
OPTION("--thp", "Setup the heap space to be aligned for merging"); |
| 71 |
CONT("by khugepaged into huge pages. This requires"); |
| 72 |
CONT("kernel support for transparent huge pages to be"); |
| 73 |
CONT("enabled"); |
| 74 |
|
| 75 |
OPTION("--no-preload", "Disable preloading the libhugetlbfs library"); |
| 76 |
OPTION("--no-reserve", "Disable huge page reservation for segments"); |
| 77 |
OPTION("--force-preload", "Force preloading the libhugetlbfs library"); |
| 78 |
|
| 79 |
OPTION("--dry-run", "describe what would be done without doing it"); |
| 80 |
|
| 81 |
OPTION("--library-use-path", "Use the system library path"); |
| 82 |
OPTION("--share-text", "Share text segments between multiple"); |
| 83 |
CONT("application instances"); |
| 84 |
OPTION("--library-path <path>", "Select a library prefix"); |
| 85 |
CONT("(Default: " |
| 86 |
#ifdef LIBDIR32 |
| 87 |
LIBDIR32 ":" |
| 88 |
#endif |
| 89 |
#ifdef LIBDIR32 |
| 90 |
LIBDIR32 ":" |
| 91 |
#endif |
| 92 |
")"); |
| 93 |
} |
| 94 |
|
| 95 |
int opt_dry_run = 0; |
| 96 |
int opt_force_preload = 0; |
| 97 |
int verbose_level = VERBOSITY_DEFAULT; |
| 98 |
|
| 99 |
void verbose_init(void) |
| 100 |
{ |
| 101 |
char *env; |
| 102 |
|
| 103 |
env = getenv("HUGETLB_VERBOSE"); |
| 104 |
if (env) |
| 105 |
verbose_level = atoi(env); |
| 106 |
env = getenv("HUGETLB_DEBUG"); |
| 107 |
if (env) |
| 108 |
verbose_level = VERBOSITY_MAX; |
| 109 |
} |
| 110 |
|
| 111 |
void verbose(char *which) |
| 112 |
{ |
| 113 |
int new_level; |
| 114 |
|
| 115 |
if (which) { |
| 116 |
new_level = atoi(which); |
| 117 |
if (new_level < 0 || new_level > 99) { |
| 118 |
ERROR("%d: verbosity out of range 0-99\n", |
| 119 |
new_level); |
| 120 |
exit(EXIT_FAILURE); |
| 121 |
} |
| 122 |
} else { |
| 123 |
new_level = verbose_level + 1; |
| 124 |
if (new_level == 100) { |
| 125 |
WARNING("verbosity limited to 99\n"); |
| 126 |
new_level--; |
| 127 |
} |
| 128 |
} |
| 129 |
verbose_level = new_level; |
| 130 |
} |
| 131 |
|
| 132 |
void quiet(void) |
| 133 |
{ |
| 134 |
int new_level = verbose_level - 1; |
| 135 |
if (new_level < 0) { |
| 136 |
WARNING("verbosity must be at least 0\n"); |
| 137 |
new_level = 0; |
| 138 |
} |
| 139 |
verbose_level = new_level; |
| 140 |
} |
| 141 |
|
| 142 |
void setup_environment(char *var, char *val) |
| 143 |
{ |
| 144 |
setenv(var, val, 1); |
| 145 |
INFO("%s='%s'\n", var, val); |
| 146 |
|
| 147 |
if (opt_dry_run) |
| 148 |
printf("%s='%s'\n", var, val); |
| 149 |
} |
| 150 |
|
| 151 |
void verbose_expose(void) |
| 152 |
{ |
| 153 |
char level[3]; |
| 154 |
|
| 155 |
if (verbose_level == 99) { |
| 156 |
setup_environment("HUGETLB_DEBUG", "yes"); |
| 157 |
} |
| 158 |
snprintf(level, sizeof(level), "%d", verbose_level); |
| 159 |
setup_environment("HUGETLB_VERBOSE", level); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
#define MAP_BASE 0x1000 |
| 166 |
#define LONG_BASE 0x2000 |
| 167 |
|
| 168 |
#define LONG_NO_PRELOAD (LONG_BASE | 'p') |
| 169 |
#define LONG_NO_RESERVE (LONG_BASE | 'r') |
| 170 |
#define LONG_FORCE_PRELOAD (LONG_BASE | 'F') |
| 171 |
|
| 172 |
#define LONG_DRY_RUN (LONG_BASE | 'd') |
| 173 |
|
| 174 |
#define LONG_SHARE (LONG_BASE | 's') |
| 175 |
#define LONG_NO_LIBRARY (LONG_BASE | 'L') |
| 176 |
#define LONG_LIBRARY (LONG_BASE | 'l') |
| 177 |
|
| 178 |
#define LONG_THP_HEAP ('t') |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
enum { |
| 186 |
MAP_TEXT, |
| 187 |
MAP_DATA, |
| 188 |
MAP_BSS, |
| 189 |
MAP_HEAP, |
| 190 |
MAP_SHM, |
| 191 |
MAP_DISABLE, |
| 192 |
|
| 193 |
MAP_COUNT, |
| 194 |
}; |
| 195 |
char *map_size[MAP_COUNT]; |
| 196 |
|
| 197 |
char default_size[] = "the default hugepage size"; |
| 198 |
#define DEFAULT_SIZE default_size |
| 199 |
|
| 200 |
#define available(buf, ptr) ((int)(sizeof(buf) - (ptr - buf))) |
| 201 |
void setup_mappings(int count) |
| 202 |
{ |
| 203 |
char value[128]; |
| 204 |
char *ptr = value; |
| 205 |
int needed; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if (map_size[MAP_TEXT]) { |
| 219 |
if (map_size[MAP_TEXT] == DEFAULT_SIZE) |
| 220 |
needed = snprintf(ptr, available(value, ptr), ":R"); |
| 221 |
else |
| 222 |
needed = snprintf(ptr, available(value, ptr), |
| 223 |
":R=%s", map_size[MAP_TEXT]); |
| 224 |
ptr += needed; |
| 225 |
if (needed < 0 || available(value, ptr) < 0) { |
| 226 |
ERROR("%s: bad size specification\n", map_size[MAP_TEXT]); |
| 227 |
exit(EXIT_FAILURE); |
| 228 |
} |
| 229 |
} |
| 230 |
if (map_size[MAP_DATA] != 0 || map_size[MAP_BSS] != 0) { |
| 231 |
char *size = map_size[MAP_BSS]; |
| 232 |
if (map_size[MAP_DATA]) |
| 233 |
size = map_size[MAP_DATA]; |
| 234 |
if (map_size[MAP_DATA] != map_size[MAP_BSS]) |
| 235 |
WARNING("data and bss remapped together in %s\n", size); |
| 236 |
|
| 237 |
if (size == DEFAULT_SIZE) |
| 238 |
needed = snprintf(ptr, available(value, ptr), ":W"); |
| 239 |
else |
| 240 |
needed = snprintf(ptr, available(value, ptr), |
| 241 |
":W=%s", size); |
| 242 |
ptr += needed; |
| 243 |
if (needed < 0 || available(value, ptr) < 0) { |
| 244 |
ERROR("%s: bad size specification\n", size); |
| 245 |
exit(EXIT_FAILURE); |
| 246 |
} |
| 247 |
} |
| 248 |
*ptr = '\0'; |
| 249 |
if (ptr != value) |
| 250 |
setup_environment("HUGETLB_ELFMAP", &value[1]); |
| 251 |
|
| 252 |
if (map_size[MAP_DISABLE]) { |
| 253 |
if (ptr != value) |
| 254 |
WARNING("--disable masks requested remap\n"); |
| 255 |
setup_environment("HUGETLB_ELFMAP", "no"); |
| 256 |
} |
| 257 |
|
| 258 |
if (map_size[MAP_HEAP] == DEFAULT_SIZE) |
| 259 |
setup_environment("HUGETLB_MORECORE", "yes"); |
| 260 |
else if (map_size[MAP_HEAP]) |
| 261 |
setup_environment("HUGETLB_MORECORE", map_size[MAP_HEAP]); |
| 262 |
|
| 263 |
if (map_size[MAP_SHM] && map_size[MAP_SHM] != DEFAULT_SIZE) |
| 264 |
WARNING("shm segments may only be mapped in the " |
| 265 |
"default hugepage size\n"); |
| 266 |
if (map_size[MAP_SHM]) |
| 267 |
setup_environment("HUGETLB_SHM", "yes"); |
| 268 |
} |
| 269 |
|
| 270 |
#define LIBRARY_DISABLE ((void *)-1) |
| 271 |
|
| 272 |
void library_path(char *path) |
| 273 |
{ |
| 274 |
char val[PATH_MAX] = ""; |
| 275 |
char *env; |
| 276 |
|
| 277 |
env = getenv("LD_LIBRARY_PATH"); |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
if (path) { |
| 287 |
snprintf(val, sizeof(val), "%s/libhugetlbfs.so", path); |
| 288 |
if (access(val, F_OK) == 0) { |
| 289 |
|
| 290 |
snprintf(val, sizeof(val), "%s:%s", |
| 291 |
path, env ? env : ""); |
| 292 |
|
| 293 |
} else { |
| 294 |
|
| 295 |
snprintf(val, sizeof(val), "" |
| 296 |
#ifdef LIBDIR32 |
| 297 |
"%s/" LIB32 ":" |
| 298 |
#endif |
| 299 |
#ifdef LIBDIR64 |
| 300 |
"%s/" LIB64 ":" |
| 301 |
#endif |
| 302 |
"%s", |
| 303 |
#ifdef LIBDIR32 |
| 304 |
path, |
| 305 |
#endif |
| 306 |
#ifdef LIBDIR64 |
| 307 |
path, |
| 308 |
#endif |
| 309 |
env ? env : ""); |
| 310 |
} |
| 311 |
|
| 312 |
} else { |
| 313 |
|
| 314 |
snprintf(val, sizeof(val), "" |
| 315 |
#ifdef LIBDIR32 |
| 316 |
LIBDIR32 ":" |
| 317 |
#endif |
| 318 |
#ifdef LIBDIR64 |
| 319 |
LIBDIR64 ":" |
| 320 |
#endif |
| 321 |
"%s", env ? env : ""); |
| 322 |
} |
| 323 |
setup_environment("LD_LIBRARY_PATH", val); |
| 324 |
} |
| 325 |
|
| 326 |
void ldpreload(int count) |
| 327 |
{ |
| 328 |
int allowed = 0; |
| 329 |
|
| 330 |
if (map_size[MAP_HEAP]) |
| 331 |
allowed++; |
| 332 |
if (map_size[MAP_SHM]) |
| 333 |
allowed++; |
| 334 |
|
| 335 |
if ((allowed == count) || opt_force_preload) { |
| 336 |
setup_environment("LD_PRELOAD", "libhugetlbfs.so"); |
| 337 |
if (allowed == count) |
| 338 |
INFO("LD_PRELOAD in use for lone --heap/--shm\n"); |
| 339 |
} else { |
| 340 |
WARNING("LD_PRELOAD not appropriate for this map combination\n"); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
int main(int argc, char** argv) |
| 345 |
{ |
| 346 |
int opt_mappings = 0; |
| 347 |
int opt_preload = 1; |
| 348 |
int opt_no_reserve = 0; |
| 349 |
int opt_share = 0; |
| 350 |
int opt_thp_heap = 0; |
| 351 |
char *opt_library = NULL; |
| 352 |
|
| 353 |
char opts[] = "+hvq"; |
| 354 |
int ret = 0, index = 0; |
| 355 |
struct option long_opts[] = { |
| 356 |
{"help", no_argument, NULL, 'h'}, |
| 357 |
{"verbose", required_argument, NULL, 'v' }, |
| 358 |
{"no-preload", no_argument, NULL, LONG_NO_PRELOAD}, |
| 359 |
{"no-reserve", no_argument, NULL, LONG_NO_RESERVE}, |
| 360 |
{"force-preload", |
| 361 |
no_argument, NULL, LONG_FORCE_PRELOAD}, |
| 362 |
{"dry-run", no_argument, NULL, LONG_DRY_RUN}, |
| 363 |
{"library-path", |
| 364 |
required_argument, NULL, LONG_LIBRARY}, |
| 365 |
{"library-use-path", |
| 366 |
no_argument, NULL, LONG_NO_LIBRARY}, |
| 367 |
{"share-text", no_argument, NULL, LONG_SHARE}, |
| 368 |
|
| 369 |
{"disable", optional_argument, NULL, MAP_BASE|MAP_DISABLE}, |
| 370 |
{"text", optional_argument, NULL, MAP_BASE|MAP_TEXT}, |
| 371 |
{"data", optional_argument, NULL, MAP_BASE|MAP_DATA}, |
| 372 |
{"bss", optional_argument, NULL, MAP_BASE|MAP_BSS}, |
| 373 |
{"heap", optional_argument, NULL, MAP_BASE|MAP_HEAP}, |
| 374 |
{"shm", optional_argument, NULL, MAP_BASE|MAP_SHM}, |
| 375 |
{"thp", no_argument, NULL, LONG_THP_HEAP}, |
| 376 |
{0}, |
| 377 |
}; |
| 378 |
|
| 379 |
verbose_init(); |
| 380 |
|
| 381 |
while (ret != -1) { |
| 382 |
ret = getopt_long(argc, argv, opts, long_opts, &index); |
| 383 |
if (ret > 0 && (ret & MAP_BASE)) { |
| 384 |
if (optarg) |
| 385 |
map_size[ret & ~MAP_BASE] = optarg; |
| 386 |
else |
| 387 |
map_size[ret & ~MAP_BASE] = DEFAULT_SIZE; |
| 388 |
opt_mappings++; |
| 389 |
continue; |
| 390 |
} |
| 391 |
switch (ret) { |
| 392 |
case '?': |
| 393 |
print_usage(); |
| 394 |
exit(EXIT_FAILURE); |
| 395 |
|
| 396 |
case 'h': |
| 397 |
print_usage(); |
| 398 |
exit(EXIT_SUCCESS); |
| 399 |
|
| 400 |
case 'v': |
| 401 |
verbose(optarg); |
| 402 |
break; |
| 403 |
|
| 404 |
case 'q': |
| 405 |
quiet(); |
| 406 |
break; |
| 407 |
|
| 408 |
case LONG_THP_HEAP: |
| 409 |
opt_thp_heap = 1; |
| 410 |
INFO("Aligning heap for use with THP\n"); |
| 411 |
break; |
| 412 |
|
| 413 |
case LONG_NO_PRELOAD: |
| 414 |
opt_preload = 0; |
| 415 |
INFO("LD_PRELOAD disabled\n"); |
| 416 |
break; |
| 417 |
|
| 418 |
case LONG_NO_RESERVE: |
| 419 |
opt_no_reserve = 1; |
| 420 |
INFO("MAP_NORESERVE used for huge page mappings\n"); |
| 421 |
break; |
| 422 |
|
| 423 |
case LONG_FORCE_PRELOAD: |
| 424 |
opt_preload = 1; |
| 425 |
opt_force_preload = 1; |
| 426 |
INFO("Forcing ld preload\n"); |
| 427 |
break; |
| 428 |
|
| 429 |
case LONG_DRY_RUN: |
| 430 |
opt_dry_run = 1; |
| 431 |
break; |
| 432 |
|
| 433 |
case LONG_NO_LIBRARY: |
| 434 |
opt_library = LIBRARY_DISABLE; |
| 435 |
INFO("using LD_LIBRARY_PATH to find library\n"); |
| 436 |
break; |
| 437 |
|
| 438 |
case LONG_LIBRARY: |
| 439 |
opt_library = optarg; |
| 440 |
break; |
| 441 |
|
| 442 |
case LONG_SHARE: |
| 443 |
opt_share = 1; |
| 444 |
break; |
| 445 |
|
| 446 |
case -1: |
| 447 |
break; |
| 448 |
|
| 449 |
default: |
| 450 |
WARNING("unparsed option %08x\n", ret); |
| 451 |
ret = -1; |
| 452 |
break; |
| 453 |
} |
| 454 |
} |
| 455 |
index = optind; |
| 456 |
|
| 457 |
if (!opt_dry_run && (argc - index) < 1) { |
| 458 |
print_usage(); |
| 459 |
exit(EXIT_FAILURE); |
| 460 |
} |
| 461 |
|
| 462 |
verbose_expose(); |
| 463 |
|
| 464 |
if (opt_library != LIBRARY_DISABLE) |
| 465 |
library_path(opt_library); |
| 466 |
|
| 467 |
if (opt_mappings) |
| 468 |
setup_mappings(opt_mappings); |
| 469 |
|
| 470 |
if (opt_preload) |
| 471 |
ldpreload(opt_mappings); |
| 472 |
|
| 473 |
if (opt_no_reserve) |
| 474 |
setup_environment("HUGETLB_NO_RESERVE", "yes"); |
| 475 |
|
| 476 |
if (opt_share) |
| 477 |
setup_environment("HUGETLB_SHARE", "1"); |
| 478 |
|
| 479 |
if (opt_thp_heap) |
| 480 |
setup_environment("HUGETLB_MORECORE", "thp"); |
| 481 |
|
| 482 |
if (opt_dry_run) |
| 483 |
exit(EXIT_SUCCESS); |
| 484 |
|
| 485 |
execvp(argv[index], &argv[index]); |
| 486 |
ERROR("exec failed: %s\n", strerror(errno)); |
| 487 |
exit(EXIT_FAILURE); |
| 488 |
} |