1 /*
2 * memtoy: commands.c - command line interface
3 *
4 * A brute force/ad hoc command interpreter:
5 * + parse commands [interactive or batch]
6 * + convert/validate arguments
7 * + some general/administrative commands herein
8 * + actual segment management routines in segment.c
9 */
10 /*
11 * Copyright (c) 2005 Hewlett-Packard, Inc
12 * All rights reserved.
13 */
14
15 /*
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */
30
31 #include "config.h"
32
33 #ifdef HAVE_NUMA_V2
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <sys/mman.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <numa.h>
41 #include <numaif.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <sys/syscall.h>
48
49 #include "memtoy.h"
50 #include "test.h"
51
52 #define CMD_SUCCESS 0
53 #define CMD_ERROR 1
54 #define CMDBUFSZ 256
55
56 #ifndef __NR_migrate_pages
57 #define __NR_migrate_pages 0
58 #endif
59
60 #ifndef MPOL_MF_WAIT
61 #define MPOL_MF_WAIT (1<<2) /* Wait for existing pages to migrate */
62 #endif
63
nodemask_isset(nodemask_t * mask,int node)64 static inline int nodemask_isset(nodemask_t * mask, int node)
65 {
66 if ((unsigned)node >= NUMA_NUM_NODES)
67 return 0;
68 if (mask->n[node / (8 * sizeof(unsigned long))] &
69 (1UL << (node % (8 * sizeof(unsigned long)))))
70 return 1;
71 return 0;
72 }
73
nodemask_set(nodemask_t * mask,int node)74 static inline void nodemask_set(nodemask_t * mask, int node)
75 {
76 mask->n[node / (8 * sizeof(unsigned long))] |=
77 (1UL << (node % (8 * sizeof(unsigned long))));
78 }
79
80 static char *whitespace = " \t";
81
get_next_arg(char * args,char * nextarg)82 inline char *get_next_arg(char *args, char *nextarg)
83 {
84 return nextarg ? nextarg + strspn(nextarg, whitespace) : args + strnlen(args, CMDBUFSZ);
85 }
86
87 /*
88 * =========================================================================
89 */
90 static int help_me(char *); /* forward reference */
91
92 /*
93 * required_arg -- check for a required argument; issue message if not there
94 *
95 * return true if arg [something] exists; else return false
96 */
required_arg(char * arg,char * arg_name)97 static bool required_arg(char *arg, char *arg_name)
98 {
99 glctx_t *gcp = &glctx;
100
101 if (*arg != '\0')
102 return true;
103
104 fprintf(stderr, "%s: command '%s' missing required argument: %s\n\n",
105 gcp->program_name, gcp->cmd_name, arg_name);
106 help_me(gcp->cmd_name);
107
108 return false;
109 }
110
111 /*
112 * size_kmgp() -- convert ascii arg to numeric and scale as requested
113 */
114 #define KILO_SHIFT 10
size_kmgp(char * arg)115 static size_t size_kmgp(char *arg)
116 {
117 size_t argval;
118 char *next;
119
120 argval = strtoul(arg, &next, 0);
121 if (*next == '\0')
122 return argval;
123
124 switch (tolower(*next)) {
125 case 'p': /* pages */
126 argval *= glctx.pagesize;
127 break;
128
129 case 'k':
130 argval <<= KILO_SHIFT;
131 break;
132
133 case 'm':
134 argval <<= KILO_SHIFT * 2;
135 break;
136
137 case 'g':
138 argval <<= KILO_SHIFT * 3;
139 break;
140
141 default:
142 return BOGUS_SIZE; /* bogus chars after number */
143 }
144
145 return argval;
146 }
147
get_scaled_value(char * args,char * what)148 static size_t get_scaled_value(char *args, char *what)
149 {
150 glctx_t *gcp = &glctx;
151 size_t size = size_kmgp(args);
152
153 if (size == BOGUS_SIZE) {
154 fprintf(stderr, "%s: segment %s must be numeric value"
155 " followed by optional k, m, g or p [pages] scale factor.\n",
156 gcp->program_name, what);
157 }
158
159 return size;
160 }
161
get_range(char * args,range_t * range,char ** nextarg)162 static int get_range(char *args, range_t * range, char **nextarg)
163 {
164
165 if (isdigit(*args)) {
166 char *nextarg;
167
168 args = strtok_r(args, whitespace, &nextarg);
169 range->offset = get_scaled_value(args, "offset");
170 if (range->offset == BOGUS_SIZE)
171 return CMD_ERROR;
172 args = get_next_arg(args, nextarg);
173
174 /*
175 * <length> ... only if offset specified
176 */
177 if (*args != '\0') {
178 args = strtok_r(args, whitespace, &nextarg);
179 if (*args != '*') {
180 range->length =
181 get_scaled_value(args, "length");
182 if (range->length == BOGUS_SIZE)
183 return CMD_ERROR;
184 } else
185 range->length = 0; /* map to end of file */
186 args = get_next_arg(args, nextarg);
187 }
188 }
189
190 *nextarg = args;
191 return CMD_SUCCESS;
192 }
193
get_shared(char * args)194 static int get_shared(char *args)
195 {
196 glctx_t *gcp = &glctx;
197 int segflag = MAP_PRIVATE;
198
199 if (!strcmp(args, "shared"))
200 segflag = MAP_SHARED;
201 else if (*args != '\0' && strcmp(args, "private")) {
202 fprintf(stderr, "%s: anon seg access type must be one of: "
203 "'private' or 'shared'\n", gcp->program_name);
204 return -1;
205 }
206 return segflag;
207 }
208
209 /*
210 * get_access() - check args for 'read'\'write'
211 * return:
212 * 1 = read
213 * 2 = write
214 * 0 = neither [error]
215 */
get_access(char * args)216 static int get_access(char *args)
217 {
218 glctx_t *gcp = &glctx;
219 int axcs = 1;
220 int len = strlen(args);
221
222 if (tolower(*args) == 'w')
223 axcs = 2;
224 else if (len != 0 && tolower(*args) != 'r') {
225 fprintf(stderr,
226 "%s: segment access must be 'r[ead]' or 'w[rite]'\n",
227 gcp->program_name);
228 return 0;
229 }
230
231 return axcs;
232 }
233
numa_supported(void)234 static bool numa_supported(void)
235 {
236 glctx_t *gcp = &glctx;
237
238 if (gcp->numa_max_node <= 0) {
239 fprintf(stderr, "%s: no NUMA support on this platform\n",
240 gcp->program_name);
241 return false;
242 }
243 return true;
244 }
245
246 static struct policies {
247 char *pol_name;
248 int pol_flag;
249 } policies[] = {
250 {
251 "default", MPOL_DEFAULT}, {
252 "preferred", MPOL_PREFERRED}, {
253 "bind", MPOL_BIND}, {
254 "interleaved", MPOL_INTERLEAVE}, {
255 NULL, -1}
256 };
257
258 /*
259 * get_mbind_policy() - parse <policy> argument to mbind command
260 *
261 * format: <mpol>[+<flags>]
262 * <mpol> is one of the policies[] above.
263 * '+<flags>' = modifiers to mbind() call. parsed by get_mbind_flags()
264 */
get_mbind_policy(char * args,char ** nextarg)265 static int get_mbind_policy(char *args, char **nextarg)
266 {
267 glctx_t *gcp = &glctx;
268 struct policies *polp;
269 char *pol;
270
271 pol = args;
272 args += strcspn(args, " +");
273
274 for (polp = policies; polp->pol_name != NULL; ++polp) {
275 size_t plen = args - pol;
276
277 if (strncmp(pol, polp->pol_name, plen))
278 continue;
279
280 *nextarg = args;
281 return polp->pol_flag;
282 }
283
284 fprintf(stderr, "%s: unrecognized policy %s\n",
285 gcp->program_name, pol);
286 return CMD_ERROR;
287 }
288
289 /*
290 * get_mbind_flags() - parse mbind(2) modifier flags
291 *
292 * format: +move[+wait]
293 * 'move' specifies that currently allocated pages should be migrated.
294 * => MPOL_MF_MOVE
295 * 'wait' [only if 'move' specified] specifies that mbind(2) should not
296 * return until all pages that can be migrated have been.
297 * => MPOL_MF_WAIT
298 *
299 * returns flags on success; -1 on error
300 */
get_mbind_flags(char * args,char ** nextarg)301 static int get_mbind_flags(char *args, char **nextarg)
302 {
303 glctx_t *gcp = &glctx;
304 char *arg;
305 int flags = 0;
306
307 arg = args;
308 args += strcspn(args, " +");
309
310 if (strncmp(arg, "move", args - arg))
311 goto flags_err;
312
313 flags = MPOL_MF_MOVE;
314
315 if (*args == '+') {
316 ++args;
317 if (*args == '\0') {
318 fprintf(stderr, "%s: expected 'wait' after '+'\n",
319 gcp->program_name);
320 return -1;
321 }
322 arg = strtok_r(args, " ", &args);
323 if (strncmp(arg, "wait", strlen(arg)))
324 goto flags_err;
325
326 flags |= MPOL_MF_WAIT;
327 }
328
329 *nextarg = args;
330 return flags;
331
332 flags_err:
333 fprintf(stderr, "%s: unrecognized mbind flag: %s\n",
334 gcp->program_name, arg);
335 return -1;
336
337 }
338
339 /*
340 * get_nodemask() -- get nodemask from comma-separated list of node ids.
341 *
342 * N.B., caller must free returned nodemask
343 */
get_nodemask(char * args)344 static nodemask_t *get_nodemask(char *args)
345 {
346 glctx_t *gcp = &glctx;
347 nodemask_t *nmp = (nodemask_t *) calloc(1, sizeof(nodemask_t));
348 char *next;
349 int node;
350 while (*args != '\0') {
351 if (!isdigit(*args)) {
352 fprintf(stderr, "%s: expected digit for <node/list>\n",
353 gcp->program_name);
354 goto out_err;
355 }
356
357 node = strtoul(args, &next, 10);
358
359 if (node > gcp->numa_max_node) {
360 fprintf(stderr, "%s: node ids must be <= %d\n",
361 gcp->program_name, gcp->numa_max_node);
362 goto out_err;
363 }
364
365 nodemask_set(nmp, node);
366
367 if (*next == '\0')
368 return nmp;
369 if (*next != ',') {
370 break;
371 }
372 args = next + 1;
373 }
374
375 out_err:
376 free(nmp);
377 return NULL;
378 }
379
380 /*
381 * get_arg_nodeid_list() -- get list [array] of node ids from comma-separated list.
382 *
383 * on success, returns count of id's in list; on error -1
384 */
get_arg_nodeid_list(char * args,unsigned int * list)385 static int get_arg_nodeid_list(char *args, unsigned int *list)
386 {
387 glctx_t *gcp;
388 char *next;
389 nodemask_t my_allowed_nodes;
390 int node, count = 0;
391
392 gcp = &glctx;
393 my_allowed_nodes = numa_get_membind_compat();
394 while (*args != '\0') {
395 if (!isdigit(*args)) {
396 fprintf(stderr, "%s: expected digit for <node/list>\n",
397 gcp->program_name);
398 return -1;
399 }
400
401 node = strtoul(args, &next, 10);
402
403 if (node > gcp->numa_max_node) {
404 fprintf(stderr, "%s: node ids must be <= %d\n",
405 gcp->program_name, gcp->numa_max_node);
406 return -1;
407 }
408
409 if (!nodemask_isset(&my_allowed_nodes, node)) {
410 fprintf(stderr,
411 "%s: node %d is not in my allowed node mask\n",
412 gcp->program_name, node);
413 return -1;
414 }
415
416 *(list + count++) = node;
417
418 if (*next == '\0')
419 return count;
420 if (*next != ',') {
421 break;
422 }
423
424 if (count >= gcp->numa_max_node) {
425 fprintf(stderr, "%s: too many node ids in list\n",
426 gcp->program_name);
427 }
428 args = next + 1;
429 }
430
431 return -1;
432 }
433
434 /*
435 * get_current_nodeid_list() - fill arg array with nodes from
436 * current thread's allowed node mask. return # of nodes in
437 * mask.
438 */
get_current_nodeid_list(unsigned int * fromids)439 static int get_current_nodeid_list(unsigned int *fromids)
440 {
441 glctx_t *gcp = &glctx;
442 nodemask_t my_allowed_nodes;
443 int nr_nodes = 0, max_node = gcp->numa_max_node;
444 int node;
445
446 my_allowed_nodes = numa_get_membind_compat();
447 for (node = 0; node <= max_node; ++node) {
448 if (nodemask_isset(&my_allowed_nodes, node))
449 *(fromids + nr_nodes++) = node;
450 }
451
452 /*
453 * shouldn't happen, but let 'em know if it does
454 */
455 if (nr_nodes == 0)
456 fprintf(stderr, "%s: my allowed node mask is empty !!???\n",
457 gcp->program_name);
458 return nr_nodes;
459 }
460
461 /*
462 * NOTE (garrcoop): Get rid of an -Wunused warning. This wasn't deleted because
463 * I don't know what the original intent was for this code.
464 */
465 #if 0
466 static void not_implemented()
467 {
468 glctx_t *gcp = &glctx;
469
470 fprintf(stderr, "%s: %s not implemented yet\n",
471 gcp->program_name, gcp->cmd_name);
472 }
473 #endif
474
475 /*
476 * =========================================================================
477 */
quit(char * args)478 static int quit(char *args)
479 {
480 exit(0); /* let cleanup() do its thing */
481 }
482
show_pid(char * args)483 static int show_pid(char *args)
484 {
485 glctx_t *gcp = &glctx;
486
487 printf("%s: pid = %d\n", gcp->program_name, getpid());
488
489 return CMD_SUCCESS;
490 }
491
pause_me(char * args)492 static int pause_me(char *args)
493 {
494 // glctx_t *gcp = &glctx;
495
496 pause();
497 reset_signal();
498
499 return CMD_SUCCESS;
500 }
501
502 static char *numa_header = " Node Total Mem[MB] Free Mem[MB]\n";
numa_info(char * args)503 static int numa_info(char *args)
504 {
505 glctx_t *gcp = &glctx;
506 unsigned int *nodeids;
507 int nr_nodes, i;
508 bool do_header = true;
509
510 if (!numa_supported())
511 return CMD_ERROR;
512
513 nodeids = calloc(gcp->numa_max_node, sizeof(*nodeids));
514 nr_nodes = get_current_nodeid_list(nodeids);
515 if (nr_nodes < 0)
516 return CMD_ERROR;
517
518 for (i = 0; i < nr_nodes; ++i) {
519 int node = nodeids[i];
520 long node_size, node_free;
521
522 node_size = numa_node_size(node, &node_free);
523 if (node_size < 0) {
524 fprintf(stderr,
525 "%s: numa_node_size() failed for node %d\n",
526 gcp->program_name, node);
527 return CMD_ERROR;
528 }
529
530 if (do_header) {
531 do_header = false;
532 puts(numa_header);
533 }
534 printf(" %3d %9ld %8ld\n", node,
535 node_size / (1024 * 1024), node_free / (1024 * 1024));
536 }
537
538 return CMD_SUCCESS;
539 }
540
541 /*
542 * migrate <to-node-id[s]> [<from-node-id[s]>]
543 *
544 * Node id[s] - single node id or comma-separated list
545 * <to-node-id[s]> - 1-for-1 with <from-node-id[s]>, OR
546 * if <from-node-id[s]> omitted, <to-node-id[s]> must be
547 * a single node id.
548 */
migrate_process(char * args)549 static int migrate_process(char *args)
550 {
551 glctx_t *gcp = &glctx;
552 unsigned int *fromids, *toids;
553 char *idlist, *nextarg;
554 struct timeval t_start, t_end;
555 int nr_to, nr_from;
556 int nr_migrated;
557 int ret = CMD_ERROR;
558
559 if (!numa_supported())
560 return CMD_ERROR;
561
562 toids = calloc(gcp->numa_max_node, sizeof(*toids));
563 fromids = calloc(gcp->numa_max_node, sizeof(*fromids));
564
565 /*
566 * <to-node-id[s]>
567 */
568 if (!required_arg(args, "<to-node-id[s]>"))
569 return CMD_ERROR;
570 idlist = strtok_r(args, whitespace, &nextarg);
571 nr_to = get_arg_nodeid_list(idlist, toids);
572 if (nr_to <= 0)
573 goto out_free;
574 args = nextarg + strspn(nextarg, whitespace);
575
576 if (*args != '\0') {
577 /*
578 * apparently, <from-node-id[s]> present
579 */
580 idlist = strtok_r(args, whitespace, &nextarg);
581 nr_from = get_arg_nodeid_list(idlist, fromids);
582 if (nr_from <= 0)
583 goto out_free;
584 if (nr_from != nr_to) {
585 fprintf(stderr,
586 "%s: # of 'from' ids must = # of 'to' ids\n",
587 gcp->program_name);
588 goto out_free;
589 }
590 } else {
591 int i;
592
593 /*
594 * no <from-node-id[s]>, nr_to must == 1,
595 * get fromids from memory policy.
596 */
597 if (nr_to > 1) {
598 fprintf(stderr, "%s: # to ids must = 1"
599 " when no 'from' ids specified\n",
600 gcp->program_name);
601 goto out_free;
602 }
603 nr_from = get_current_nodeid_list(fromids);
604 if (nr_from <= 0)
605 goto out_free;
606
607 /*
608 * remove 'to' node from 'from' list. to and from
609 * lists can't intersect.
610 */
611 for (i = nr_from - 1; i >= 0; --i) {
612 if (*toids == *(fromids + i)) {
613 while (i <= nr_from) {
614 *(fromids + i) = *(fromids + i + 1);
615 ++i;
616 }
617 --nr_from;
618 break;
619 }
620 }
621
622 /*
623 * fill out nr_from toids with the single 'to' node
624 */
625 for (; nr_to < nr_from; ++nr_to)
626 *(toids + nr_to) = *toids; /* toids[0] */
627 }
628
629 gettimeofday(&t_start, NULL);
630 nr_migrated =
631 syscall(__NR_migrate_pages, getpid(), nr_from, fromids, toids);
632 if (nr_migrated < 0) {
633 int err = errno;
634 fprintf(stderr, "%s: migrate_pages failed - %s\n",
635 gcp->program_name, strerror(err));
636 goto out_free;
637 }
638 gettimeofday(&t_end, NULL);
639 printf("%s: migrated %d pages in %6.3fsecs\n",
640 gcp->program_name, nr_migrated,
641 (float)(tv_diff_usec(&t_start, &t_end)) / 1000000.0);
642 ret = CMD_SUCCESS;
643
644 out_free:
645 free(toids);
646 free(fromids);
647 return ret;
648 }
649
show_seg(char * args)650 static int show_seg(char *args)
651 {
652 glctx_t *gcp = &glctx;
653
654 char *segname = NULL, *nextarg;
655
656 args += strspn(args, whitespace);
657 if (*args != '\0')
658 segname = strtok_r(args, whitespace, &nextarg);
659
660 if (!segment_show(segname))
661 return CMD_ERROR;
662
663 return CMD_SUCCESS;
664 }
665
666 /*
667 * anon_seg: <seg-name> <size>[kmgp] [private|shared]
668 */
anon_seg(char * args)669 static int anon_seg(char *args)
670 {
671 glctx_t *gcp = &glctx;
672
673 char *segname, *nextarg;
674 range_t range = { 0L, 0L };
675 int segflag = 0;
676
677 args += strspn(args, whitespace);
678
679 if (!required_arg(args, "<seg-name>"))
680 return CMD_ERROR;
681 segname = strtok_r(args, whitespace, &nextarg);
682 args = nextarg + strspn(nextarg, whitespace);
683
684 if (!required_arg(args, "<size>"))
685 return CMD_ERROR;
686 args = strtok_r(args, whitespace, &nextarg);
687 range.length = get_scaled_value(args, "size");
688 if (range.length == BOGUS_SIZE)
689 return CMD_ERROR;
690 args = get_next_arg(args, nextarg);
691
692 if (*args != '\0') {
693 segflag = get_shared(args);
694 if (segflag == -1)
695 return CMD_ERROR;
696 }
697
698 if (!segment_register(SEGT_ANON, segname, &range, segflag))
699 return CMD_ERROR;
700
701 return CMD_SUCCESS;
702 }
703
704 /*
705 * file_seg: <path-name> [<offset>[kmgp] <length>[kmgp] [private|shared]]
706 */
file_seg(char * args)707 static int file_seg(char *args)
708 {
709 glctx_t *gcp = &glctx;
710
711 char *pathname, *nextarg;
712 range_t range = { 0L, 0L };
713 int segflag = MAP_PRIVATE;
714
715 args += strspn(args, whitespace);
716
717 if (!required_arg(args, "<path-name>"))
718 return CMD_ERROR;
719 pathname = strtok_r(args, whitespace, &nextarg);
720 args = get_next_arg(args, nextarg);
721
722 /*
723 * offset, length are optional
724 */
725 if (get_range(args, &range, &nextarg) == CMD_ERROR)
726 return CMD_ERROR;
727 args = nextarg;
728
729 if (*args != '\0') {
730 segflag = get_shared(args);
731 if (segflag == -1)
732 return CMD_ERROR;
733 }
734
735 if (!segment_register(SEGT_FILE, pathname, &range, segflag))
736 return CMD_ERROR;
737
738 return CMD_SUCCESS;
739 }
740
741 /*
742 * deletefile <file-name>
743 */
delete_file(char * args)744 static int delete_file(char *args)
745 {
746 glctx_t *gcp = &glctx;
747 char *filename, *nextarg;
748
749 args += strspn(args, whitespace);
750 if (!required_arg(args, "<file-name>"))
751 return CMD_ERROR;
752 filename = strtok_r(args, whitespace, &nextarg);
753
754 if (remove(filename)) {
755 fprintf(stderr, "%s: deletefile failed - %s\n",
756 gcp->program_name, strerror(errno));
757 return CMD_ERROR;
758 }
759
760 return CMD_SUCCESS;
761 }
762
763 /*
764 * createfile <file-name> <size>[k|m|g|p]]
765 */
create_file(char * args)766 static int create_file(char *args)
767 {
768 glctx_t *gcp = &glctx;
769 char *filename, *nextarg;
770 size_t len;
771 int fd;
772
773 args += strspn(args, whitespace);
774 if (!required_arg(args, "<file-name>"))
775 return CMD_ERROR;
776 filename = strtok_r(args, whitespace, &nextarg);
777 args = nextarg + strspn(nextarg, whitespace);
778
779 if (!required_arg(args, "<size>"))
780 return CMD_ERROR;
781 args = strtok_r(args, whitespace, &nextarg);
782 len = get_scaled_value(args, "size");
783 if (len == BOGUS_SIZE)
784 return CMD_ERROR;
785 args = get_next_arg(args, nextarg);
786
787 fd = open(filename, O_RDWR | O_CREAT, 0600);
788 if (fd < 0) {
789 fprintf(stderr, "%s: createfile failed - %s\n",
790 gcp->program_name, strerror(errno));
791 return CMD_ERROR;
792 }
793
794 if (posix_fallocate(fd, 0, len)) {
795 fprintf(stderr, "%s: createfile failed - %s\n",
796 gcp->program_name, strerror(errno));
797 return CMD_ERROR;
798 }
799 close(fd);
800 return CMD_SUCCESS;
801 }
802
803 /*
804 * remove_seg: <seg-name> [<seg-name> ...]
805 */
remove_seg(char * args)806 static int remove_seg(char *args)
807 {
808 glctx_t *gcp = &glctx;
809
810 args += strspn(args, whitespace);
811 if (!required_arg(args, "<seg-name>"))
812 return CMD_ERROR;
813
814 while (*args != '\0') {
815 char *segname, *nextarg;
816
817 segname = strtok_r(args, whitespace, &nextarg);
818 args = nextarg + strspn(nextarg, whitespace);
819
820 segment_remove(segname);
821 }
822 return 0;
823 }
824
825 /*
826 * touch_seg: <seg-name> [<offset> <length>] [read|write]
827 */
touch_seg(char * args)828 static int touch_seg(char *args)
829 {
830 glctx_t *gcp = &glctx;
831
832 char *segname, *nextarg;
833 range_t range = { 0L, 0L };
834 int axcs;
835
836 args += strspn(args, whitespace);
837 if (!required_arg(args, "<seg-name>"))
838 return CMD_ERROR;
839 segname = strtok_r(args, whitespace, &nextarg);
840 args = get_next_arg(args, nextarg);
841
842 /*
843 * offset, length are optional
844 */
845 if (get_range(args, &range, &nextarg) == CMD_ERROR)
846 return CMD_ERROR;
847 args = nextarg;
848
849 axcs = get_access(args);
850 if (axcs == 0)
851 return CMD_ERROR;
852
853 if (!segment_touch(segname, &range, axcs - 1))
854 return CMD_ERROR;
855
856 return CMD_SUCCESS;
857 }
858
859 /*
860 * unmap <seg-name> - unmap specified segment, but remember name/size/...
861 */
unmap_seg(char * args)862 static int unmap_seg(char *args)
863 {
864 glctx_t *gcp = &glctx;
865 char *segname, *nextarg;
866
867 args += strspn(args, whitespace);
868 if (!required_arg(args, "<seg-name>"))
869 return CMD_ERROR;
870 segname = strtok_r(args, whitespace, &nextarg);
871 args = get_next_arg(args, nextarg);
872
873 if (!segment_unmap(segname))
874 return CMD_ERROR;
875
876 return CMD_SUCCESS;
877 }
878
879 /*
880 * map <seg-name> [<offset>[k|m|g|p] <length>[k|m|g|p]] [<seg-share>]
881 */
map_seg(char * args)882 static int map_seg(char *args)
883 {
884 glctx_t *gcp = &glctx;
885
886 char *segname, *nextarg;
887 range_t range = { 0L, 0L };
888 range_t *rangep = NULL;
889 int segflag = MAP_PRIVATE;
890
891 args += strspn(args, whitespace);
892 if (!required_arg(args, "<seg-name>"))
893 return CMD_ERROR;
894 segname = strtok_r(args, whitespace, &nextarg);
895 args = get_next_arg(args, nextarg);
896
897 /*
898 * offset, length are optional
899 */
900 if (get_range(args, &range, &nextarg) == CMD_ERROR)
901 return CMD_ERROR;
902 if (args != nextarg) {
903 rangep = ⦥ /* override any registered range */
904 args = nextarg;
905 }
906
907 if (*args != '\0') {
908 segflag = get_shared(args);
909 if (segflag == -1)
910 return CMD_ERROR;
911 }
912
913 if (!segment_map(segname, rangep, segflag))
914 return CMD_ERROR;
915
916 return CMD_SUCCESS;
917 }
918
919 /*
920 * mbind <seg-name> [<offset>[kmgp] <length>[kmgp]] <policy> <node-list>
921 */
mbind_seg(char * args)922 static int mbind_seg(char *args)
923 {
924 glctx_t *gcp = &glctx;
925
926 char *segname, *nextarg;
927 range_t range = { 0L, 0L };
928 nodemask_t *nodemask = NULL;
929 int policy, flags = 0;
930 int ret;
931
932 if (!numa_supported())
933 return CMD_ERROR;
934
935 args += strspn(args, whitespace);
936 if (!required_arg(args, "<seg-name>"))
937 return CMD_ERROR;
938 segname = strtok_r(args, whitespace, &nextarg);
939 args = get_next_arg(args, nextarg);
940
941 /*
942 * offset, length are optional
943 */
944 if (get_range(args, &range, &nextarg) == CMD_ERROR)
945 return CMD_ERROR;
946 args = nextarg;
947
948 if (!required_arg(args, "<policy>"))
949 return CMD_ERROR;
950 policy = get_mbind_policy(args, &nextarg);
951 if (policy < 0)
952 return CMD_ERROR;
953
954 args = get_next_arg(args, nextarg);
955 if (*args == '+') {
956 flags = get_mbind_flags(++args, &nextarg);
957 if (flags == -1)
958 return CMD_ERROR;
959 }
960 args = nextarg + strspn(nextarg, whitespace);
961
962 if (policy != MPOL_DEFAULT) {
963 if (!required_arg(args, "<node/list>"))
964 return CMD_ERROR;
965 nodemask = get_nodemask(args);
966 if (nodemask == NULL)
967 return CMD_ERROR;
968 }
969
970 ret = CMD_SUCCESS;
971 #if 1 // for testing
972 if (!segment_mbind(segname, &range, policy, nodemask, flags))
973 ret = CMD_ERROR;
974 #endif
975
976 if (nodemask != NULL)
977 free(nodemask);
978 return ret;
979 }
980
981 /*
982 * shmem_seg - create [shmget] and register a SysV shared memory segment
983 * of specified size
984 */
shmem_seg(char * args)985 static int shmem_seg(char *args)
986 {
987 glctx_t *gcp = &glctx;
988
989 char *segname, *nextarg;
990 range_t range = { 0L, 0L };
991
992 args += strspn(args, whitespace);
993
994 if (!required_arg(args, "<seg-name>"))
995 return CMD_ERROR;
996 segname = strtok_r(args, whitespace, &nextarg);
997 args = get_next_arg(args, nextarg);
998
999 if (!required_arg(args, "<size>"))
1000 return CMD_ERROR;
1001 args = strtok_r(args, whitespace, &nextarg);
1002 range.length = get_scaled_value(args, "size");
1003 if (range.length == BOGUS_SIZE)
1004 return CMD_ERROR;
1005 args = get_next_arg(args, nextarg);
1006
1007 if (!segment_register(SEGT_SHM, segname, &range, MAP_SHARED))
1008 return CMD_ERROR;
1009
1010 return CMD_SUCCESS;
1011 }
1012
1013 /*
1014 * where <seg-name> [<offset>[kmgp] <length>[kmgp]] - show node location
1015 * of specified range of segment.
1016 *
1017 * NOTE: if neither <offset> nor <length> specified, <offset> defaults
1018 * to 0 [start of segment], as usual, and length defaults to 64 pages
1019 * rather than the entire segment. Suitable for a "quick look" at where
1020 * segment resides.
1021 */
where_seg(char * args)1022 static int where_seg(char *args)
1023 {
1024 glctx_t *gcp = &glctx;
1025
1026 char *segname, *nextarg;
1027 range_t range = { 0L, 0L };
1028 int ret;
1029
1030 if (!numa_supported())
1031 return CMD_ERROR;
1032
1033 args += strspn(args, whitespace);
1034 if (!required_arg(args, "<seg-name>"))
1035 return CMD_ERROR;
1036 segname = strtok_r(args, whitespace, &nextarg);
1037 args = get_next_arg(args, nextarg);
1038
1039 /*
1040 * offset, length are optional
1041 */
1042 if (get_range(args, &range, &nextarg) == CMD_ERROR)
1043 return CMD_ERROR;
1044 if (args == nextarg)
1045 range.length = 64 * gcp->pagesize; /* default length */
1046
1047 if (!segment_location(segname, &range))
1048 return CMD_ERROR;
1049
1050 return CMD_SUCCESS;
1051 }
1052
1053 #if 0
1054 static int command(char *args)
1055 {
1056 glctx_t *gcp = &glctx;
1057
1058 return CMD_SUCCESS;
1059 }
1060
1061 #endif
1062 /*
1063 * =========================================================================
1064 */
1065 typedef int (*cmd_func_t) (char *);
1066
1067 struct command {
1068 char *cmd_name;
1069 cmd_func_t cmd_func; /* */
1070 char *cmd_help;
1071
1072 } cmd_table[] = {
1073 {
1074 .cmd_name = "quit",.cmd_func = quit,.cmd_help =
1075 "quit - just what you think\n"
1076 "\tEOF on stdin has the same effect\n"}, {
1077 .cmd_name = "help",.cmd_func = help_me,.cmd_help =
1078 "help - show this help\n"
1079 "help <command> - display help for just <command>\n"}, {
1080 .cmd_name = "pid",.cmd_func = show_pid,.cmd_help =
1081 "pid - show process id of this session\n"}, {
1082 .cmd_name = "pause",.cmd_func = pause_me,.cmd_help =
1083 "pause - pause program until signal"
1084 " -- e.g., INT, USR1\n"}, {
1085 .cmd_name = "numa",.cmd_func = numa_info,.cmd_help =
1086 "numa - display numa info as seen by this program.\n"
1087 "\tshows nodes from which program may allocate memory\n"
1088 "\twith total and free memory.\n"}, {
1089 .cmd_name = "migrate",.cmd_func = migrate_process,.cmd_help =
1090 "migrate <to-node-id[s]> [<from-node-id[s]>] - \n"
1091 "\tmigrate this process' memory from <from-node-id[s]>\n"
1092 "\tto <to-node-id[s]>. Specify multiple node ids as a\n"
1093 "\tcomma-separated list. TODO - more info\n"}, {
1094 .cmd_name = "show",.cmd_func = show_seg,.cmd_help =
1095 "show [<name>] - show info for segment[s]; default all\n"},
1096 {
1097 .cmd_name = "anon",.cmd_func = anon_seg,.cmd_help =
1098 "anon <seg-name> <seg-size>[k|m|g|p] [<seg-share>] -\n"
1099 "\tdefine a MAP_ANONYMOUS segment of specified size\n"
1100 "\t<seg-share> := private|shared - default = private\n"}, {
1101 .cmd_name = "file",.cmd_func = file_seg,.cmd_help =
1102 "file <pathname> [<offset>[k|m|g|p] <length>[k|m|g|p]] [<seg-share>] -\n"
1103 "\tdefine a mapped file segment of specified length starting at the\n"
1104 "\tspecified offset into the file. <offset> and <length> may be\n"
1105 "\tomitted and specified on the map command.\n"
1106 "\t<seg-share> := private|shared - default = private\n"}, {
1107 .cmd_name = "createfile", .cmd_func = create_file, .cmd_help =
1108 "createfile <file-name> <size>[k|m|g|p]]",}, {
1109 .cmd_name = "deletefile", .cmd_func = delete_file, .cmd_help =
1110 "deletefile <file-name>"}, {
1111 .cmd_name = "shm",.cmd_func = shmem_seg,.cmd_help =
1112 "shm <seg-name> <seg-size>[k|m|g|p] - \n"
1113 "\tdefine a shared memory segment of specified size.\n"
1114 "\tYou may need to increase limits [/proc/sys/kernel/shmmax].\n"
1115 "\tUse map/unmap to attach/detach\n"}, {
1116 .cmd_name = "remove",.cmd_func = remove_seg,.cmd_help =
1117 "remove <seg-name> [<seg-name> ...] - remove the named segment[s]\n"},
1118 {
1119 .cmd_name = "map",.cmd_func = map_seg,.cmd_help =
1120 "map <seg-name> [<offset>[k|m|g|p] <length>[k|m|g|p]] [<seg-share>] - \n"
1121 "\tmmap()/shmat() a previously defined, currently unmapped() segment.\n"
1122 "\t<offset> and <length> apply only to mapped files.\n"
1123 "\tUse <length> of '*' or '0' to map to the end of the file.\n"},
1124 {
1125 .cmd_name = "unmap",.cmd_func = unmap_seg,.cmd_help =
1126 "unmap <seg-name> - unmap specified segment, but remember name/size/...\n"},
1127 {
1128 .cmd_name = "touch",.cmd_func = touch_seg,.cmd_help =
1129 "touch <seg-name> [<offset>[k|m|g|p] <length>[k|m|g|p]] [read|write] - \n"
1130 "\tread [default] or write the named segment from <offset> through\n"
1131 "\t<offset>+<length>. If <offset> and <length> omitted, touches all\n"
1132 "\t of mapped segment.\n"}, {
1133 .cmd_name = "mbind",.cmd_func = mbind_seg,.cmd_help =
1134 "mbind <seg-name> [<offset>[k|m|g|p] <length>[k|m|g|p]]\n"
1135 " <policy>[+move[+wait]] [<node/list>] - \n"
1136 "\tset the numa policy for the specified range of the name segment\n"
1137 "\tto policy -- one of {default, bind, preferred, interleaved}.\n"
1138 "\t<node/list> specifies a node id or a comma separated list of\n"
1139 "\tnode ids. <node> is ignored for 'default' policy, and only\n"
1140 "\tthe first node is used for 'preferred' policy.\n"
1141 "\t'+move' specifies that currently allocated pages be prepared\n"
1142 "\t for migration on next touch\n"
1143 "\t'+wait' [valid only with +move] specifies that pages mbind()\n"
1144 " touch the pages and wait for migration before returning.\n"},
1145 {
1146 .cmd_name = "where",.cmd_func = where_seg,.cmd_help =
1147 "where <seg-name> [<offset>[k|m|g|p] <length>[k|m|g|p]] - \n"
1148 "\tshow the node location of pages in the specified range\n"
1149 "\tof the specified segment. <offset> defaults to start of\n"
1150 "\tsegment; <length> defaults to 64 pages.\n"},
1151 #if 0 /* template for new commands */
1152 {
1153 .cmd_name = "",.cmd_func =,.cmd_help =},
1154 #endif
1155 {
1156 .cmd_name = NULL}
1157 };
1158
help_me(char * args)1159 static int help_me(char *args)
1160 {
1161 struct command *cmdp = cmd_table;
1162 char *cmd, *nextarg;
1163 int cmdlen;
1164 bool match = false;
1165
1166 args += strspn(args, whitespace);
1167 if (*args != '\0') {
1168 cmd = strtok_r(args, whitespace, &nextarg);
1169 cmdlen = strlen(cmd);
1170 } else {
1171 cmd = NULL;
1172 cmdlen = 0;
1173 }
1174
1175 for (cmdp = cmd_table; cmdp->cmd_name != NULL; ++cmdp) {
1176 if (cmd == NULL || !strncmp(cmd, cmdp->cmd_name, cmdlen)) {
1177 printf("%s\n", cmdp->cmd_help);
1178 match = true;
1179 }
1180 }
1181
1182 if (!match) {
1183 printf("unrecognized command: %s\n", cmd);
1184 printf("\tuse 'help' for a complete list of commands\n");
1185 return CMD_ERROR;
1186 }
1187
1188 return CMD_SUCCESS;
1189 }
1190
1191 /*
1192 * =========================================================================
1193 */
1194
unique_abbrev(char * cmd,size_t clen,struct command * cmdp)1195 static bool unique_abbrev(char *cmd, size_t clen, struct command *cmdp)
1196 {
1197 for (; cmdp->cmd_name != NULL; ++cmdp) {
1198 if (!strncmp(cmd, cmdp->cmd_name, clen))
1199 return false; /* match: not unique */
1200 }
1201 return true;
1202 }
1203
parse_command(char * cmdline)1204 static int parse_command(char *cmdline)
1205 {
1206 glctx_t *gcp = &glctx;
1207 char *cmd, *args;
1208 struct command *cmdp;
1209
1210 cmdline += strspn(cmdline, whitespace); /* possibly redundant */
1211
1212 cmd = strtok_r(cmdline, whitespace, &args);
1213
1214 for (cmdp = cmd_table; cmdp->cmd_name != NULL; ++cmdp) {
1215 size_t clen = strlen(cmd);
1216 int ret;
1217
1218 if (strncmp(cmd, cmdp->cmd_name, clen))
1219 continue;
1220 if (!unique_abbrev(cmd, clen, cmdp + 1)) {
1221 fprintf(stderr, "%s: ambiguous command: %s\n",
1222 gcp->program_name, cmd);
1223 return CMD_ERROR;
1224 }
1225 gcp->cmd_name = cmdp->cmd_name;
1226 ret = cmdp->cmd_func(args);
1227 gcp->cmd_name = NULL;
1228 return ret;
1229 }
1230
1231 fprintf(stderr, "%s: unrecognized command %s\n", __FUNCTION__, cmd);
1232 return CMD_ERROR;
1233 }
1234
process_commands()1235 void process_commands()
1236 {
1237 glctx_t *gcp = &glctx;
1238
1239 char cmdbuf[CMDBUFSZ];
1240
1241 do {
1242 char *cmdline;
1243 size_t cmdlen;
1244
1245 if (is_option(INTERACTIVE))
1246 printf("%s>", gcp->program_name);
1247
1248 cmdline = fgets(cmdbuf, CMDBUFSZ, stdin);
1249 if (cmdline == NULL) {
1250 printf("%s\n",
1251 is_option(INTERACTIVE) ? "" : "EOF on stdin");
1252 exit(0); /* EOF */
1253 }
1254 if (cmdline[0] == '\n')
1255 continue;
1256
1257 /*
1258 * trim trailing newline, if any
1259 */
1260 cmdlen = strlen(cmdline);
1261 if (cmdline[cmdlen - 1] == '\n')
1262 cmdline[--cmdlen] = '\0';
1263
1264 cmdline += strspn(cmdline, whitespace);
1265 cmdlen -= (cmdline - cmdbuf);
1266
1267 if (cmdlen == 0) {
1268 //TODO: interactive help?
1269 continue; /* ignore blank lines */
1270 }
1271
1272 if (*cmdline == '#')
1273 continue; /* comments */
1274
1275 /*
1276 * trim trailing whitespace for ease of parsing
1277 */
1278 while (strchr(whitespace, cmdline[cmdlen - 1]))
1279 cmdline[--cmdlen] = '\0';
1280
1281 if (cmdlen == 0)
1282 continue;
1283
1284 /*
1285 * reset signals just before parsing a command.
1286 * non-interactive: exit on SIGQUIT
1287 */
1288 if (signalled(gcp)) {
1289 if (!is_option(INTERACTIVE) &&
1290 gcp->siginfo->si_signo == SIGQUIT)
1291 exit(0);
1292 reset_signal();
1293 }
1294
1295 /*
1296 * non-interactive: errors are fatal
1297 */
1298 if (!is_option(INTERACTIVE)) {
1299 vprint("%s>%s\n", gcp->program_name, cmdline);
1300 if (parse_command(cmdline) == CMD_ERROR) {
1301 fprintf(stderr, "%s: command error\n",
1302 gcp->program_name);
1303 exit(4);
1304 }
1305 } else
1306 parse_command(cmdline);
1307
1308 } while (1);
1309 }
1310 #endif
1311