Debian Codegraph @

hugectl.c

raw symbols SCIP
1 /***************************************************************************
2 * User front end for using huge pages Copyright (C) 2008, IBM *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the Lesser GNU General Public License as *
6 * published by the Free Software Foundation; either version 2.1 of the *
7 * License, or at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the Lesser GNU General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20 /*
21 * hugectl is inspired by numactl as a single front end to a large number of
22 * options for controlling a very specific environment. Eventually it will
23 * have support for controlling the all of the environment variables for
24 * libhugetlbfs, but options will only be added after they have been in the
25 * library for some time and are throughly tested and stable.
26 *
27 * This program should be treated as an ABI for using libhugetlbfs.
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 /* for getopt_long */
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 * getopts return values for options which are long only.
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 * Mapping selectors, one per remappable/backable area as requested
182 * by the user. These are also used as returns from getopts where they
183 * are offset from MAP_BASE, which must be removed before they are compared.
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 * HUGETLB_ELFMAP should be set to either a combination of 'R' and 'W'
209 * which indicate which segments should be remapped. Each may take
210 * an optional page size. It may also be set to 'no' to prevent
211 * remapping.
212 */
213
214 /*
215 * Accumulate sections each with a ':' prefix to simplify later
216 * handling. We will elide the initial ':' before use.
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 * Select which libraries we wish to use. If the path is NULL
281 * use the libraries included with hugectl. If the path is valid
282 * and points to a directory including a libhugetlbfs.so use it
283 * directly. Else path is assumed to be a prefix to the 32/64 bit
284 * directories both of which are added, where available.
285 */
286 if (path) {
287 snprintf(val, sizeof(val), "%s/libhugetlbfs.so", path);
288 if (access(val, F_OK) == 0) {
289 /* $PATH */
290 snprintf(val, sizeof(val), "%s:%s",
291 path, env ? env : "");
292
293 } else {
294 /* [$PATH/LIB32:][$PATH/LIB64:]$LD_LIBRARY_PATH */
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 /* [LIBDIR32:][LIBDIR64:]$LD_LIBRARY_PATH */
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 }