dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / ptools / plgrp / plgrp.c
blob6fae36e9a71dc16998a1fb6332c4beba847979b4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * The plgrp utility allows a user to display and modify the home lgroup and
29 * lgroup affinities of the specified threads
32 #include <ctype.h>
33 #include <errno.h>
34 #include <libintl.h>
35 #include <libproc.h>
36 #include <locale.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <strings.h>
41 #include <unistd.h>
42 #include <libgen.h>
43 #include <sys/lgrp_user.h>
47 * Delimiters
49 #define DELIMIT_AFF '/' /* lgroup affinity from lgroups */
50 #define DELIMIT_LGRP "," /* lgroups from each other */
51 #define DELIMIT_LWP "/" /* thread/LWP IDs from process ID */
52 #define DELIMIT_RANGE '-' /* range of IDs (eg. lgroup) */
53 #define DELIMIT_AFF_LST ',' /* list of affinities from another list */
56 * Exit values other than EXIT_{SUCCESS,FAILURE}
58 #define EXIT_NONFATAL 2 /* non-fatal errors */
61 * Header and format strings
63 #define HDR_PLGRP_AFF_GET " PID/LWPID HOME AFFINITY\n"
64 #define HDR_PLGRP_AFF_SET " PID/LWPID HOME AFFINITY\n"
65 #define HDR_PLGRP_HOME_GET " PID/LWPID HOME\n"
66 #define HDR_PLGRP_HOME_SET " PID/LWPID HOME\n"
69 * Part of the HDR_PLGRP_AFF_SET header used to calculate space needed to
70 * represent changing home as old => new
72 #define HDR_PLGRP_HOME_CHANGE "HOME "
74 #define FMT_AFF "%d/%s"
75 #define FMT_AFF_STR "%s"
76 #define FMT_HOME "%-6d"
77 #define FMT_NEWHOME "%d => %d"
78 #define FMT_THREAD "%8d/%-8d"
81 * How much to allocate for lgroup bitmap array as it grows
83 #define LGRP_BITMAP_CHUNK 8
86 * Strings that can be given for lgroups
88 #define LGRP_ALL_STR "all"
89 #define LGRP_LEAVES_STR "leaves"
90 #define LGRP_ROOT_STR "root"
93 * Strings corresponding to lgroup affinities
95 #define LGRP_AFF_NONE_STR "none"
96 #define LGRP_AFF_STRONG_STR "strong"
97 #define LGRP_AFF_WEAK_STR "weak"
100 * Invalid value for lgroup affinity
102 #define LGRP_AFF_INVALID -1
105 * Number of args needed for lgroup system call
107 #define LGRPSYS_NARGS 3
109 #ifndef TEXT_DOMAIN /* should be defined by cc -D */
110 #define TEXT_DOMAIN "SYS_TEST" /* use this only if it wasn't */
111 #endif
114 * plgrp(1) operations
116 typedef enum plgrp_ops {
117 PLGRP_AFFINITY_GET,
118 PLGRP_AFFINITY_SET,
119 PLGRP_HOME_GET,
120 PLGRP_HOME_SET,
121 PLGRP_NO_OP
122 } plgrp_ops_t;
125 * Arguments specified to plgrp(1) and any state needed to do everything
126 * that plgrp(1) does for one operation from inside Plwp_iter_all()
128 typedef struct plgrp_args {
129 struct ps_prochandle *Ph; /* proc handle for process */
130 const char *lwps; /* LWPs */
131 lgrp_id_t *lgrps; /* lgroups */
132 lgrp_affinity_t *affs; /* lgroup affinities */
133 int nlgrps; /* number of lgroups */
134 int nelements; /* number of elements */
135 int index; /* index */
136 int nthreads; /* threads processed */
137 plgrp_ops_t op; /* operation */
138 } plgrp_args_t;
141 * How many signals caught from terminal
142 * We bail out as soon as possible when interrupt is set
144 static int interrupt = 0;
147 * How many non-fatal errors ocurred
149 static int nerrors = 0;
152 * Name of this program
154 static char *progname;
157 * Root of the lgroup hierarchy
159 static lgrp_id_t root = LGRP_NONE;
162 * Bitmap of all lgroups in the system
164 static char *lgrps_bitmap = NULL;
167 * Size of lgrps_bitmap array
169 static int lgrps_bitmap_nelements = 0;
172 * Macro LGRP_VALID returns true when lgrp is present in the system.
174 #define LGRP_VALID(lgrp) (lgrps_bitmap[lgrp] != 0)
178 * Maximum lgroup value.
180 static int max_lgrpid = LGRP_NONE;
183 * Total possible number of lgroups
185 #define NLGRPS (max_lgrpid + 1)
188 static void
189 usage(int rc)
191 (void) fprintf(stderr,
192 gettext("Usage:\t%s [-h] <pid> | <core> [/lwps] ...\n"), progname);
193 (void) fprintf(stderr,
194 gettext("\t%s [-F] -a <lgroup list> <pid>[/lwps] ...\n"), progname);
195 (void) fprintf(stderr,
196 gettext("\t%s [-F] -A <lgroup list>/none|weak|strong[,...] "
197 " <pid>[/lwps] ...\n"), progname);
198 (void) fprintf(stderr,
199 gettext("\t%s [-F] -H <lgroup list> <pid>[/lwps] ...\n"), progname);
200 (void) fprintf(stderr,
201 gettext("\n\twhere <lgroup list> is a comma separated list of\n"
202 "\tone or more of the following:\n\n"
203 "\t - lgroup ID\n"
204 "\t - Range of lgroup IDs specified as\n"
205 "\t\t<start lgroup ID>-<end lgroup ID>\n"
206 "\t - \"all\"\n"
207 "\t - \"root\"\n"
208 "\t - \"leaves\"\n\n"));
210 exit(rc);
214 * Handler for catching signals from terminal
216 /* ARGSUSED */
217 static void
218 intr(int sig)
220 interrupt++;
225 * Return string name for given lgroup affinity
227 static char *
228 lgrp_affinity_string(lgrp_affinity_t aff)
230 char *rc = "unknown";
232 switch (aff) {
233 case LGRP_AFF_STRONG:
234 rc = "strong";
235 break;
236 case LGRP_AFF_WEAK:
237 rc = "weak";
238 break;
239 case LGRP_AFF_NONE:
240 rc = "none";
241 break;
242 default:
243 break;
246 return (rc);
251 * Add a new lgroup into lgroup array in "arg", growing lgroup and affinity
252 * arrays if necessary
254 static void
255 lgrps_add_lgrp(plgrp_args_t *arg, int id)
258 if (arg->nlgrps == arg->nelements) {
259 arg->nelements += LGRP_BITMAP_CHUNK;
261 arg->lgrps = reallocarray(arg->lgrps, arg->nelements,
262 sizeof (lgrp_id_t));
263 if (arg->lgrps == NULL) {
264 (void) fprintf(stderr, gettext("%s: out of memory\n"),
265 progname);
266 exit(EXIT_FAILURE);
269 arg->affs = reallocarray(arg->affs, arg->nelements,
270 sizeof (lgrp_affinity_t));
272 if (arg->affs == NULL) {
273 (void) fprintf(stderr, gettext("%s: out of memory\n"),
274 progname);
275 exit(EXIT_FAILURE);
279 arg->lgrps[arg->nlgrps] = id;
280 arg->affs[arg->nlgrps] = LGRP_AFF_INVALID;
281 arg->nlgrps++;
286 * Return an array having '1' for each lgroup present in given subtree under
287 * specified lgroup in lgroup hierarchy
289 static void
290 lgrps_bitmap_init(lgrp_cookie_t cookie, lgrp_id_t lgrpid, char **bitmap_array,
291 int *bitmap_nelements)
293 lgrp_id_t *children;
294 int i;
295 int nchildren;
297 if (lgrpid < 0) {
298 lgrpid = lgrp_root(cookie);
299 if (lgrpid < 0)
300 return;
304 * If new lgroup cannot fit, grow the array and fill unused portion
305 * with zeroes.
307 while (lgrpid >= *bitmap_nelements) {
308 *bitmap_nelements += LGRP_BITMAP_CHUNK;
309 *bitmap_array = reallocarray(*bitmap_array,
310 *bitmap_nelements, sizeof (char));
311 if (*bitmap_array == NULL) {
312 (void) fprintf(stderr, gettext("%s: out of memory\n"),
313 progname);
314 exit(EXIT_FAILURE);
316 bzero(*bitmap_array + NLGRPS,
317 (*bitmap_nelements - NLGRPS) * sizeof (char));
321 * Insert lgroup into bitmap and update max lgroup ID seen so far
323 (*bitmap_array)[lgrpid] = 1;
324 if (lgrpid > max_lgrpid)
325 max_lgrpid = lgrpid;
328 * Get children of specified lgroup and insert descendants of each
329 * of them
331 nchildren = lgrp_children(cookie, lgrpid, NULL, 0);
332 if (nchildren > 0) {
333 children = malloc(nchildren * sizeof (lgrp_id_t));
334 if (children == NULL) {
335 (void) fprintf(stderr, gettext("%s: out of memory\n"),
336 progname);
337 exit(EXIT_FAILURE);
339 if (lgrp_children(cookie, lgrpid, children, nchildren) !=
340 nchildren) {
341 free(children);
342 return;
345 for (i = 0; i < nchildren; i++)
346 lgrps_bitmap_init(cookie, children[i], bitmap_array,
347 bitmap_nelements);
349 free(children);
355 * Parse lgroup affinity from given string
357 * Return lgroup affinity or LGRP_AFF_INVALID if string doesn't match any
358 * existing lgroup affinity and return pointer to position just after affinity
359 * string.
361 static lgrp_affinity_t
362 parse_lgrp_affinity(char *string, char **next)
364 int rc = LGRP_AFF_INVALID;
366 if (string == NULL)
367 return (LGRP_AFF_INVALID);
370 * Skip delimiter
372 if (string[0] == DELIMIT_AFF)
373 string++;
376 * Return lgroup affinity matching string
378 if (strncmp(string, LGRP_AFF_NONE_STR, strlen(LGRP_AFF_NONE_STR))
379 == 0) {
380 rc = LGRP_AFF_NONE;
381 *next = string + strlen(LGRP_AFF_NONE_STR);
382 } else if (strncmp(string,
383 LGRP_AFF_WEAK_STR, strlen(LGRP_AFF_WEAK_STR)) == 0) {
384 rc = LGRP_AFF_WEAK;
385 *next = string + strlen(LGRP_AFF_WEAK_STR);
386 } else if (strncmp(string, LGRP_AFF_STRONG_STR,
387 strlen(LGRP_AFF_STRONG_STR)) == 0) {
388 rc = LGRP_AFF_STRONG;
389 *next = string + strlen(LGRP_AFF_STRONG_STR);
392 return (rc);
397 * Parse lgroups from given string
398 * Returns the set containing all lgroups parsed or NULL.
400 static int
401 parse_lgrps(lgrp_cookie_t cookie, plgrp_args_t *arg, char *s)
403 lgrp_id_t i;
404 char *token;
406 if (cookie == LGRP_COOKIE_NONE || s == NULL || NLGRPS <= 0)
407 return (0);
410 * Parse first lgroup (if any)
412 token = strtok(s, DELIMIT_LGRP);
413 if (token == NULL)
414 return (-1);
416 do {
418 * Parse lgroups
420 if (isdigit(*token)) {
421 lgrp_id_t first;
422 lgrp_id_t last;
423 char *p;
426 * lgroup ID(s)
428 * Can be <lgroup ID>[-<lgroup ID>]
430 p = strchr(token, DELIMIT_RANGE);
431 first = atoi(token);
432 if (p == NULL)
433 last = first;
434 else
435 last = atoi(++p);
437 for (i = first; i <= last; i++) {
439 * Add valid lgroups to lgroup array
441 if ((i >= 0) && (i < NLGRPS) && LGRP_VALID(i))
442 lgrps_add_lgrp(arg, i);
443 else {
444 (void) fprintf(stderr,
445 gettext("%s: bad lgroup %d\n"),
446 progname, i);
447 nerrors++;
450 } else if (strncmp(token, LGRP_ALL_STR,
451 strlen(LGRP_ALL_STR)) == 0) {
453 * Add "all" lgroups to lgroups array
455 for (i = 0; i < NLGRPS; i++) {
456 if (LGRP_VALID(i))
457 lgrps_add_lgrp(arg, i);
459 } else if (strncmp(token, LGRP_ROOT_STR,
460 strlen(LGRP_ROOT_STR)) == 0) {
461 if (root < 0)
462 root = lgrp_root(cookie);
463 lgrps_add_lgrp(arg, root);
464 } else if (strncmp(token, LGRP_LEAVES_STR,
465 strlen(LGRP_LEAVES_STR)) == 0) {
467 * Add leaf lgroups to lgroups array
469 for (i = 0; i < NLGRPS; i++) {
470 if (LGRP_VALID(i) &&
471 lgrp_children(cookie, i, NULL, 0) == 0)
472 lgrps_add_lgrp(arg, i);
474 } else {
475 return (-1);
477 } while (token = strtok(NULL, DELIMIT_LGRP));
479 return (0);
483 * Print array of lgroup IDs, collapsing any consecutive runs of IDs into a
484 * range (eg. 2,3,4 into 2-4)
486 static void
487 print_lgrps(lgrp_id_t *lgrps, int nlgrps)
489 lgrp_id_t start;
490 lgrp_id_t end;
491 int i;
494 * Initial range consists of the first element
496 start = end = lgrps[0];
498 for (i = 1; i < nlgrps; i++) {
499 lgrp_id_t lgrpid;
501 lgrpid = lgrps[i];
502 if (lgrpid == end + 1) {
504 * Got consecutive lgroup ID, so extend end of range
505 * without printing anything since the range may extend
506 * further
508 end = lgrpid;
509 } else {
511 * Next lgroup ID is not consecutive, so print lgroup
512 * IDs gotten so far.
514 if (end == start) { /* same value */
515 (void) printf("%d,", (int)start);
516 } else if (end > start + 1) { /* range */
517 (void) printf("%d-%d,", (int)start, (int)end);
518 } else { /* different values */
519 (void) printf("%d,%d,", (int)start, (int)end);
523 * Try finding consecutive range starting from this
524 * lgroup ID
526 start = end = lgrpid;
531 * Print last lgroup ID(s)
533 if (end == start) {
534 (void) printf("%d", (int)start);
535 } else if (end > start + 1) {
536 (void) printf("%d-%d", (int)start, (int)end);
537 } else {
538 (void) printf("%d,%d", (int)start, (int)end);
543 * Print lgroup affinities given array of lgroups, corresponding array of
544 * affinities, and number of elements.
545 * Skip any lgroups set to LGRP_NONE or having invalid affinity.
547 static void
548 print_affinities(lgrp_id_t *lgrps, lgrp_affinity_t *affs, int nelements)
550 int i;
551 lgrp_id_t *lgrps_none;
552 lgrp_id_t *lgrps_strong;
553 lgrp_id_t *lgrps_weak;
554 int nlgrps_none;
555 int nlgrps_strong;
556 int nlgrps_weak;
558 nlgrps_strong = nlgrps_weak = nlgrps_none = 0;
560 lgrps_strong = malloc(nelements * sizeof (lgrp_id_t));
561 lgrps_weak = malloc(nelements * sizeof (lgrp_id_t));
562 lgrps_none = malloc(nelements * sizeof (lgrp_id_t));
564 if (lgrps_strong == NULL || lgrps_weak == NULL || lgrps_none == NULL) {
565 (void) fprintf(stderr, gettext("%s: out of memory\n"),
566 progname);
567 interrupt = 1;
568 return;
572 * Group lgroups by affinity
574 for (i = 0; i < nelements; i++) {
575 lgrp_id_t lgrpid = lgrps[i];
578 * Skip any lgroups set to LGRP_NONE
580 if (lgrpid == LGRP_NONE)
581 continue;
583 switch (affs[i]) {
584 case LGRP_AFF_STRONG:
585 lgrps_strong[nlgrps_strong++] = lgrpid;
586 break;
587 case LGRP_AFF_WEAK:
588 lgrps_weak[nlgrps_weak++] = lgrpid;
589 break;
590 case LGRP_AFF_NONE:
591 lgrps_none[nlgrps_none++] = lgrpid;
592 break;
593 default:
595 * Skip any lgroups with invalid affinity.
597 break;
602 * Print all lgroups with same affinity together
604 if (nlgrps_strong) {
605 print_lgrps(lgrps_strong, nlgrps_strong);
606 (void) printf("/%s", lgrp_affinity_string(LGRP_AFF_STRONG));
607 if (nlgrps_weak || nlgrps_none)
608 (void) printf("%c", DELIMIT_AFF_LST);
611 if (nlgrps_weak) {
612 print_lgrps(lgrps_weak, nlgrps_weak);
613 (void) printf("/%s", lgrp_affinity_string(LGRP_AFF_WEAK));
614 if (nlgrps_none)
615 (void) printf("%c", DELIMIT_AFF_LST);
618 if (nlgrps_none) {
619 print_lgrps(lgrps_none, nlgrps_none);
620 (void) printf("/%s", lgrp_affinity_string(LGRP_AFF_NONE));
623 free(lgrps_strong);
624 free(lgrps_weak);
625 free(lgrps_none);
630 * Print heading for specified operation
632 static void
633 print_heading(plgrp_ops_t op)
636 switch (op) {
637 case PLGRP_AFFINITY_GET:
638 (void) printf(HDR_PLGRP_AFF_GET);
639 break;
641 case PLGRP_AFFINITY_SET:
642 (void) printf(HDR_PLGRP_AFF_SET);
643 break;
645 case PLGRP_HOME_GET:
646 (void) printf(HDR_PLGRP_HOME_GET);
647 break;
649 case PLGRP_HOME_SET:
650 (void) printf(HDR_PLGRP_HOME_SET);
651 break;
653 default:
654 break;
659 * Use /proc to call lgrp_affinity_get() in another process
661 static lgrp_affinity_t
662 Plgrp_affinity_get(struct ps_prochandle *Ph, idtype_t idtype, id_t id,
663 lgrp_id_t lgrp)
665 lgrp_affinity_args_t args;
666 argdes_t Pargd[3];
667 argdes_t *Pargdp;
668 int Pnargs;
669 int Pretval;
670 sysret_t retval;
671 int syscall;
674 * Fill in arguments needed for syscall(SYS_lgrpsys,
675 * LGRP_SYS_AFFINITY_GET, 0, &args)
677 syscall = SYS_lgrpsys;
679 args.idtype = idtype;
680 args.id = id;
681 args.lgrp = lgrp;
682 args.aff = LGRP_AFF_INVALID;
685 * Fill out /proc argument descriptors for syscall(SYS_lgrpsys,
686 * LGRP_SYS_AFFINITY_GET, idtype, id)
688 Pnargs = LGRPSYS_NARGS;
689 Pargdp = &Pargd[0];
690 Pargdp->arg_value = LGRP_SYS_AFFINITY_GET;
691 Pargdp->arg_object = NULL;
692 Pargdp->arg_type = AT_BYVAL;
693 Pargdp->arg_inout = AI_INPUT;
694 Pargdp->arg_size = 0;
695 Pargdp++;
697 Pargdp->arg_value = 0;
698 Pargdp->arg_object = NULL;
699 Pargdp->arg_type = AT_BYVAL;
700 Pargdp->arg_inout = AI_INPUT;
701 Pargdp->arg_size = 0;
702 Pargdp++;
704 Pargdp->arg_value = 0;
705 Pargdp->arg_object = &args;
706 Pargdp->arg_type = AT_BYREF;
707 Pargdp->arg_inout = AI_INPUT;
708 Pargdp->arg_size = sizeof (lgrp_affinity_args_t);
709 Pargdp++;
712 * Have agent LWP call syscall with appropriate arguments in target
713 * process
715 Pretval = Psyscall(Ph, &retval, syscall, Pnargs, &Pargd[0]);
716 if (Pretval) {
717 errno = (Pretval < 0) ? ENOSYS : Pretval;
718 return (LGRP_AFF_INVALID);
721 return (retval.sys_rval1);
726 * Use /proc to call lgrp_affinity_set() in another process
728 static int
729 Plgrp_affinity_set(struct ps_prochandle *Ph, idtype_t idtype, id_t id,
730 lgrp_id_t lgrp, lgrp_affinity_t aff)
732 lgrp_affinity_args_t args;
733 argdes_t Pargd[3];
734 argdes_t *Pargdp;
735 int Pnargs;
736 int Pretval;
737 sysret_t retval;
738 int syscall;
741 * Fill in arguments needed for syscall(SYS_lgrpsys,
742 * LGRP_SYS_AFFINITY_SET, 0, &args)
744 syscall = SYS_lgrpsys;
746 args.idtype = idtype;
747 args.id = id;
748 args.lgrp = lgrp;
749 args.aff = aff;
752 * Fill out /proc argument descriptors for syscall(SYS_lgrpsys,
753 * LGRP_SYS_AFFINITY_SET, idtype, id)
755 Pnargs = LGRPSYS_NARGS;
756 Pargdp = &Pargd[0];
757 Pargdp->arg_value = LGRP_SYS_AFFINITY_SET;
758 Pargdp->arg_object = NULL;
759 Pargdp->arg_type = AT_BYVAL;
760 Pargdp->arg_inout = AI_INPUT;
761 Pargdp->arg_size = 0;
762 Pargdp++;
764 Pargdp->arg_value = 0;
765 Pargdp->arg_object = NULL;
766 Pargdp->arg_type = AT_BYVAL;
767 Pargdp->arg_inout = AI_INPUT;
768 Pargdp->arg_size = 0;
769 Pargdp++;
771 Pargdp->arg_value = 0;
772 Pargdp->arg_object = &args;
773 Pargdp->arg_type = AT_BYREF;
774 Pargdp->arg_inout = AI_INPUT;
775 Pargdp->arg_size = sizeof (lgrp_affinity_args_t);
776 Pargdp++;
779 * Have agent LWP call syscall with appropriate arguments in
780 * target process
782 Pretval = Psyscall(Ph, &retval, syscall, Pnargs, &Pargd[0]);
783 if (Pretval) {
784 errno = (Pretval < 0) ? ENOSYS : Pretval;
785 return (-1);
788 return (retval.sys_rval1);
792 * Use /proc to call lgrp_home() in another process
794 static lgrp_id_t
795 Plgrp_home(struct ps_prochandle *Ph, idtype_t idtype, id_t id)
797 argdes_t Pargd[3];
798 argdes_t *Pargdp;
799 int Pnargs;
800 int Pretval;
801 sysret_t retval;
802 int syscall;
805 * Fill in arguments needed for syscall(SYS_lgrpsys,
806 * LGRP_SYS_HOME, idtype, id)
808 syscall = SYS_lgrpsys;
811 * Fill out /proc argument descriptors for syscall(SYS_lgrpsys,
812 * LGRP_SYS_HOME, idtype, id)
814 Pnargs = LGRPSYS_NARGS;
815 Pargdp = &Pargd[0];
816 Pargdp->arg_value = LGRP_SYS_HOME;
817 Pargdp->arg_object = NULL;
818 Pargdp->arg_type = AT_BYVAL;
819 Pargdp->arg_inout = AI_INPUT;
820 Pargdp->arg_size = 0;
821 Pargdp++;
823 Pargdp->arg_value = idtype;
824 Pargdp->arg_object = NULL;
825 Pargdp->arg_type = AT_BYVAL;
826 Pargdp->arg_inout = AI_INPUT;
827 Pargdp->arg_size = 0;
828 Pargdp++;
830 Pargdp->arg_value = id;
831 Pargdp->arg_object = NULL;
832 Pargdp->arg_type = AT_BYVAL;
833 Pargdp->arg_inout = AI_INPUT;
834 Pargdp->arg_size = 0;
835 Pargdp++;
838 * Have agent LWP call syscall with appropriate arguments in
839 * target process
841 Pretval = Psyscall(Ph, &retval, syscall, Pnargs, &Pargd[0]);
842 if (Pretval) {
843 errno = (Pretval < 0) ? ENOSYS : Pretval;
844 return (-1);
847 return (retval.sys_rval1);
851 * Use /proc to call lgrp_affinity_set(3LGRP) to set home lgroup of given
852 * thread
854 static int
855 Plgrp_home_set(struct ps_prochandle *Ph, idtype_t idtype, id_t id,
856 lgrp_id_t lgrp)
858 return (Plgrp_affinity_set(Ph, idtype, id, lgrp,
859 LGRP_AFF_STRONG));
864 * Do plgrp(1) operation on specified thread
866 static int
867 do_op(plgrp_args_t *plgrp_args, id_t pid, id_t lwpid,
868 const lwpsinfo_t *lwpsinfo)
870 lgrp_affinity_t *affs;
871 lgrp_affinity_t *cur_affs;
872 lgrp_id_t home;
873 int i;
874 lgrp_affinity_t *init_affs;
875 lgrp_id_t *lgrps;
876 lgrp_id_t *lgrps_changed;
877 int nlgrps;
878 lgrp_id_t old_home;
879 lgrp_id_t lgrpid;
880 struct ps_prochandle *Ph;
881 int nchanged;
884 * No args, so nothing to do.
886 if (plgrp_args == NULL)
887 return (0);
890 * Unpack plgrp(1) arguments and state needed to process this LWP
892 Ph = plgrp_args->Ph;
893 lgrps = plgrp_args->lgrps;
894 affs = plgrp_args->affs;
895 nlgrps = plgrp_args->nlgrps;
897 switch (plgrp_args->op) {
899 case PLGRP_HOME_GET:
901 * Get and display home lgroup for given LWP
903 home = lwpsinfo->pr_lgrp;
904 (void) printf(FMT_HOME"\n", (int)home);
905 break;
907 case PLGRP_AFFINITY_GET:
909 * Get and display this LWP's home lgroup and affinities
910 * for specified lgroups
912 home = lwpsinfo->pr_lgrp;
913 (void) printf(FMT_HOME, (int)home);
916 * Collect affinity values
918 for (i = 0; i < nlgrps; i++) {
919 affs[i] = Plgrp_affinity_get(Ph, P_LWPID, lwpid,
920 lgrps[i]);
922 if (affs[i] == LGRP_AFF_INVALID) {
923 nerrors++;
924 (void) fprintf(stderr,
925 gettext("%s: cannot get affinity"
926 " for lgroup %d for %d/%d: %s\n"),
927 progname, lgrps[i], pid, lwpid,
928 strerror(errno));
933 * Print affinities for each type.
935 print_affinities(lgrps, affs, nlgrps);
936 (void) printf("\n");
938 break;
940 case PLGRP_HOME_SET:
942 * Get home lgroup before and after setting it and display
943 * change. If more than one lgroup and one LWP are specified,
944 * then home LWPs to lgroups in round robin fashion.
946 old_home = lwpsinfo->pr_lgrp;
948 i = plgrp_args->index;
949 if (Plgrp_home_set(Ph, P_LWPID, lwpid, lgrps[i]) != 0) {
950 nerrors++;
951 (void) fprintf(stderr,
952 gettext("%s: cannot set home lgroup of %d/%d"
953 " to lgroup %d: %s\n"),
954 progname, pid, lwpid, lgrps[i],
955 strerror(errno));
956 (void) printf("\n");
957 } else {
958 int len;
959 int width = strlen(HDR_PLGRP_HOME_CHANGE);
961 home = Plgrp_home(Ph, P_LWPID, lwpid);
963 if (home < 0) {
964 (void) fprintf(stderr,
965 gettext("%s cannot get home lgroup for"
966 " %d/%d: %s\n"),
967 progname, pid, lwpid, strerror(errno));
968 nerrors++;
971 len = printf(FMT_NEWHOME, (int)old_home, (int)home);
972 if (len < width)
973 (void) printf("%*c\n", (int)(width - len), ' ');
976 plgrp_args->index = (i + 1) % nlgrps;
978 break;
980 case PLGRP_AFFINITY_SET:
982 * Set affinities for specified lgroups and print old and new
983 * affinities and any resulting change in home lgroups
987 * Get initial home lgroup as it may change.
989 old_home = lwpsinfo->pr_lgrp;
992 * Need to allocate arrays indexed by lgroup (ID) for
993 * affinities and lgroups because user may specify affinity
994 * for same lgroup multiple times....
996 * Keeping these arrays by lgroup (ID) eliminates any
997 * duplication and makes it easier to just print initial and
998 * final lgroup affinities (instead of trying to keep a list
999 * of lgroups specified which may include duplicates)
1001 init_affs = malloc(NLGRPS * sizeof (lgrp_affinity_t));
1002 cur_affs = malloc(NLGRPS * sizeof (lgrp_affinity_t));
1003 lgrps_changed = malloc(NLGRPS * sizeof (lgrp_id_t));
1005 if (init_affs == NULL || cur_affs == NULL ||
1006 lgrps_changed == NULL) {
1007 (void) fprintf(stderr, gettext("%s: out of memory\n"),
1008 progname);
1009 Prelease(Ph, PRELEASE_RETAIN);
1010 free(init_affs);
1011 free(cur_affs);
1012 nerrors++;
1013 return (EXIT_NONFATAL);
1017 * Initialize current and initial lgroup affinities and
1018 * lgroups changed
1020 for (lgrpid = 0; lgrpid < NLGRPS; lgrpid++) {
1022 if (!LGRP_VALID(lgrpid)) {
1023 init_affs[lgrpid] = LGRP_AFF_INVALID;
1024 } else {
1025 init_affs[lgrpid] =
1026 Plgrp_affinity_get(Ph, P_LWPID,
1027 lwpid, lgrpid);
1029 if (init_affs[lgrpid] == LGRP_AFF_INVALID) {
1030 nerrors++;
1031 (void) fprintf(stderr,
1032 gettext("%s: cannot get"
1033 " affinity for lgroup %d"
1034 " for %d/%d: %s\n"),
1035 progname, lgrpid, pid, lwpid,
1036 strerror(errno));
1040 cur_affs[lgrpid] = init_affs[lgrpid];
1041 lgrps_changed[lgrpid] = LGRP_NONE;
1045 * Change affinities.
1047 for (i = 0; i < nlgrps; i++) {
1048 lgrp_affinity_t aff = affs[i];
1050 lgrpid = lgrps[i];
1053 * If the suggested affinity is the same as the current
1054 * one, skip this lgroup.
1056 if (aff == cur_affs[lgrpid])
1057 continue;
1060 * Set affinity to the new value
1062 if (Plgrp_affinity_set(Ph, P_LWPID, lwpid, lgrpid,
1063 aff) < 0) {
1064 nerrors++;
1065 (void) fprintf(stderr,
1066 gettext("%s: cannot set"
1067 " %s affinity for lgroup %d"
1068 " for %d/%d: %s\n"),
1069 progname, lgrp_affinity_string(aff),
1070 lgrpid, pid, lwpid,
1071 strerror(errno));
1072 continue;
1076 * Get the new value and verify that it changed as
1077 * expected.
1079 cur_affs[lgrpid] =
1080 Plgrp_affinity_get(Ph, P_LWPID, lwpid, lgrpid);
1082 if (cur_affs[lgrpid] == LGRP_AFF_INVALID) {
1083 nerrors++;
1084 (void) fprintf(stderr,
1085 gettext("%s: cannot get"
1086 " affinity for lgroup %d"
1087 " for %d/%d: %s\n"),
1088 progname, lgrpid, pid, lwpid,
1089 strerror(errno));
1090 continue;
1093 if (aff != cur_affs[lgrpid]) {
1094 (void) fprintf(stderr,
1095 gettext("%s: affinity for"
1096 " lgroup %d is set to %d instead of %d"
1097 " for %d/%d\n"),
1098 progname, lgrpid, cur_affs[lgrpid], aff,
1099 pid, lwpid);
1100 nerrors++;
1105 * Compare current and initial affinities and mark lgroups with
1106 * changed affinities.
1108 nchanged = 0;
1109 for (lgrpid = 0; lgrpid < NLGRPS; lgrpid++) {
1110 if (init_affs[lgrpid] != cur_affs[lgrpid]) {
1111 lgrps_changed[lgrpid] = lgrpid;
1112 nchanged++;
1116 if (nchanged == 0) {
1118 * Nothing changed, so just print current affinities for
1119 * specified lgroups.
1121 for (i = 0; i < nlgrps; i++) {
1122 lgrps_changed[lgrps[i]] = lgrps[i];
1125 (void) printf("%-*d",
1126 (int)strlen(HDR_PLGRP_HOME_CHANGE),
1127 (int)old_home);
1129 print_affinities(lgrps_changed, cur_affs, NLGRPS);
1130 (void) printf("\n");
1131 } else {
1132 int width = strlen(HDR_PLGRP_HOME_CHANGE);
1135 * Some lgroup affinities changed, so display old
1136 * and new home lgroups for thread and its old and new
1137 * affinities for affected lgroups
1139 home = Plgrp_home(Ph, P_LWPID, lwpid);
1140 if (home < 0) {
1141 (void) fprintf(stderr,
1142 gettext("%s: cannot get home"
1143 " for %d/%d: %s\n"),
1144 progname, pid, lwpid, strerror(errno));
1145 nerrors++;
1147 if (old_home != home) {
1148 int len;
1151 * Fit string into fixed width
1153 len = printf(FMT_NEWHOME,
1154 (int)old_home, (int)home);
1155 if (len < width)
1156 (void) printf("%*c", width - len, ' ');
1157 } else {
1158 (void) printf("%-*d", width, (int)home);
1162 * Print change in affinities from old to new
1164 print_affinities(lgrps_changed, init_affs, NLGRPS);
1165 (void) printf(" => ");
1166 print_affinities(lgrps_changed, cur_affs, NLGRPS);
1167 (void) printf("\n");
1170 free(lgrps_changed);
1171 free(init_affs);
1172 free(cur_affs);
1174 break;
1176 default:
1177 break;
1180 return (0);
1185 * Routine called by Plwp_iter_all() as it iterates through LWPs of another
1186 * process
1188 /* ARGSUSED */
1189 static int
1190 Plwp_iter_handler(void *arg, const lwpstatus_t *lwpstatus,
1191 const lwpsinfo_t *lwpsinfo)
1193 id_t lwpid;
1194 struct ps_prochandle *Ph;
1195 const pstatus_t *pstatus;
1196 plgrp_args_t *plgrp_args;
1199 * Nothing to do if no arguments
1201 if (arg == NULL || interrupt)
1202 return (0);
1205 * Unpack plgrp(1) arguments and state needed to process this LWP
1207 plgrp_args = arg;
1208 Ph = plgrp_args->Ph;
1211 * Just return if no /proc handle for process
1213 if (Ph == NULL)
1214 return (0);
1216 pstatus = Pstatus(Ph);
1219 * Skip agent LWP and any LWPs that weren't specified
1221 lwpid = lwpsinfo->pr_lwpid;
1222 if (lwpid == pstatus->pr_agentid ||
1223 !proc_lwp_in_set(plgrp_args->lwps, lwpid))
1224 return (0);
1226 plgrp_args->nthreads++;
1229 * Do all plgrp(1) operations specified on given thread
1231 (void) printf(FMT_THREAD" ", (int)pstatus->pr_pid, (int)lwpid);
1232 return (do_op(plgrp_args, pstatus->pr_pid, lwpid, lwpsinfo));
1236 * Get target process specified in "pidstring" argument to do operation(s)
1237 * specified in "plgrp_todo" using /proc and agent LWP
1239 static void
1240 do_process(char *pidstring, plgrp_args_t *plgrp_todo, int force)
1242 int error;
1243 const char *lwps;
1244 struct ps_prochandle *Ph;
1247 * Nothing to do, so return.
1249 if (plgrp_todo == NULL || interrupt)
1250 return;
1253 * Grab target process or core and return
1254 * /proc handle for process and string of LWP
1255 * IDs
1257 Ph = proc_arg_xgrab(pidstring, NULL,
1258 PR_ARG_ANY, force | PGRAB_RETAIN | PGRAB_NOSTOP, &error, &lwps);
1259 if (Ph == NULL) {
1260 (void) fprintf(stderr,
1261 gettext("%s: Unable to grab process %s: %s\n"),
1262 progname, pidstring, Pgrab_error(error));
1263 nerrors++;
1264 return;
1268 * Fill in remaining plgrp(1) arguments and state needed to do
1269 * plgrp(1) operation(s) on desired LWPs in our handler
1270 * called by Plwp_iter_all() as it iterates over LWPs
1271 * in given process
1273 plgrp_todo->Ph = Ph;
1274 plgrp_todo->lwps = lwps;
1277 * Iterate over LWPs in process and do specified
1278 * operation(s) on those specified
1280 if (Plwp_iter_all(Ph, Plwp_iter_handler, plgrp_todo) != 0) {
1281 (void) fprintf(stderr,
1282 gettext("%s: error iterating over threads\n"),
1283 progname);
1284 nerrors++;
1287 Prelease(Ph, PRELEASE_RETAIN);
1292 * Parse command line and kick off any resulting actions
1294 * plgrp(1) has the following command line syntax:
1296 * plgrp [-h] <pid> | <core> [/lwps] ...
1297 * plgrp [-F] -a <lgroup>,... <pid>[/lwps] ...
1298 * plgrp [-F] -H <lgroup>,... <pid>[/lwps] ...
1299 * plgrp [-F] -A <lgroup>,... [/none|weak|strong] ... <pid>[/lwps] ...
1301 * where <lgroup> is an lgroup ID, "all", "root", "leaves".
1304 main(int argc, char *argv[])
1306 lgrp_affinity_t aff;
1307 char *affstring;
1308 int c;
1309 lgrp_cookie_t cookie;
1310 int Fflag;
1311 int i;
1312 int opt_seen;
1313 plgrp_args_t plgrp_todo;
1314 char *s;
1316 (void) setlocale(LC_ALL, "");
1317 (void) textdomain(TEXT_DOMAIN);
1319 opt_seen = 0;
1322 * Get name of program
1324 progname = basename(argv[0]);
1327 * Not much to do when only name of program given
1329 if (argc == 1)
1330 usage(0);
1333 * Catch signals from terminal, so they can be handled asynchronously
1334 * when we're ready instead of when we're not (;-)
1336 if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
1337 (void) sigset(SIGHUP, intr);
1338 if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
1339 (void) sigset(SIGINT, intr);
1340 if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
1341 (void) sigset(SIGQUIT, intr);
1342 (void) sigset(SIGPIPE, intr);
1343 (void) sigset(SIGTERM, intr);
1346 * Take snapshot of lgroup hierarchy
1348 cookie = lgrp_init(LGRP_VIEW_OS);
1349 if (cookie == LGRP_COOKIE_NONE) {
1350 (void) fprintf(stderr,
1351 gettext("%s: Fatal error: cannot get lgroup"
1352 " information from the OS: %s\n"),
1353 progname, strerror(errno));
1354 return (EXIT_FAILURE);
1357 root = lgrp_root(cookie);
1358 lgrps_bitmap_init(cookie, root, &lgrps_bitmap, &lgrps_bitmap_nelements);
1361 * Remember arguments and state needed to do plgrp(1) operation
1362 * on desired LWPs
1364 bzero(&plgrp_todo, sizeof (plgrp_args_t));
1365 plgrp_todo.op = PLGRP_HOME_GET;
1368 * Parse options
1370 opterr = 0;
1371 Fflag = 0;
1372 while (!interrupt && (c = getopt(argc, argv, "a:A:FhH:")) != -1) {
1374 * Parse option and only allow one option besides -F to be
1375 * specified
1377 switch (c) {
1379 case 'h': /* Get home lgroup */
1381 * Only allow one option (besides -F) to be specified
1383 if (opt_seen)
1384 usage(EXIT_FAILURE);
1385 opt_seen = 1;
1387 plgrp_todo.op = PLGRP_HOME_GET;
1388 break;
1390 case 'H': /* Set home lgroup */
1393 * Fail if already specified option (besides -F)
1394 * or no more arguments
1396 if (opt_seen || optind >= argc) {
1397 usage(EXIT_FAILURE);
1399 opt_seen = 1;
1401 plgrp_todo.op = PLGRP_HOME_SET;
1403 if (parse_lgrps(cookie, &plgrp_todo, optarg) < 0)
1404 usage(EXIT_FAILURE);
1406 /* If there are no valid lgroups exit immediately */
1407 if (plgrp_todo.nlgrps == 0) {
1408 (void) fprintf(stderr,
1409 gettext("%s: no valid lgroups"
1410 " specified for -%c\n\n"),
1411 progname, c);
1412 usage(EXIT_FAILURE);
1415 break;
1417 case 'a': /* Get lgroup affinity */
1420 * Fail if already specified option (besides -F)
1421 * or no more arguments
1423 if (opt_seen || optind >= argc) {
1424 usage(EXIT_FAILURE);
1426 opt_seen = 1;
1428 plgrp_todo.op = PLGRP_AFFINITY_GET;
1430 if (parse_lgrps(cookie, &plgrp_todo, optarg) < 0)
1431 usage(EXIT_FAILURE);
1433 /* If there are no valid lgroups exit immediately */
1434 if (plgrp_todo.nlgrps == 0) {
1435 (void) fprintf(stderr,
1436 gettext("%s: no valid lgroups specified"
1437 " for -%c\n\n"),
1438 progname, c);
1439 usage(EXIT_FAILURE);
1442 break;
1444 case 'A': /* Set lgroup affinity */
1447 * Fail if already specified option (besides -F)
1448 * or no more arguments
1450 if (opt_seen || optind >= argc) {
1451 usage(EXIT_FAILURE);
1453 opt_seen = 1;
1455 plgrp_todo.op = PLGRP_AFFINITY_SET;
1458 * 'affstring' is the unparsed prtion of the affinity
1459 * specification like 1,2/none,2/weak,0/strong
1461 * 'next' is the next affinity specification to parse.
1463 affstring = optarg;
1464 while (affstring != NULL && strlen(affstring) > 0) {
1465 char *next;
1468 * affstring points to the first affinity
1469 * specification. Split the string by
1470 * DELIMIT_AFF separator and parse lgroups and
1471 * affinity value separately.
1473 s = strchr(affstring, DELIMIT_AFF);
1474 if (s == NULL) {
1475 (void) fprintf(stderr,
1476 gettext("%s: invalid "
1477 "syntax >%s<\n"),
1478 progname, affstring);
1479 usage(EXIT_FAILURE);
1482 aff = parse_lgrp_affinity(s, &next);
1483 if (aff == LGRP_AFF_INVALID) {
1484 (void) fprintf(stderr,
1485 gettext("%s: invalid "
1486 "affinity >%s<\n"),
1487 progname, affstring);
1488 usage(EXIT_FAILURE);
1492 * next should either point to the empty string
1493 * or to the DELIMIT_AFF_LST separator.
1495 if (*next != '\0') {
1496 if (*next != DELIMIT_AFF_LST) {
1497 (void) fprintf(stderr,
1498 gettext("%s: invalid "
1499 "syntax >%s<\n"),
1500 progname, next);
1501 usage(EXIT_FAILURE);
1503 *next = '\0';
1504 next++;
1509 * Now parse the list of lgroups
1511 if (parse_lgrps(cookie, &plgrp_todo,
1512 affstring) < 0) {
1513 usage(EXIT_FAILURE);
1517 * Set desired affinity for specified lgroup to
1518 * the specified affinity.
1520 for (i = 0; i < plgrp_todo.nlgrps; i++) {
1521 if (plgrp_todo.affs[i] ==
1522 LGRP_AFF_INVALID)
1523 plgrp_todo.affs[i] = aff;
1527 * We processed the leftmost element of the
1528 * list. Advance affstr to the remaining part of
1529 * the list. and repeat.
1531 affstring = next;
1535 * If there are no valid lgroups, exit immediately
1537 if (plgrp_todo.nlgrps == 0) {
1538 (void) fprintf(stderr,
1539 gettext("%s: no valid lgroups specified "
1540 "for -%c\n\n"), progname, c);
1541 usage(EXIT_FAILURE);
1544 break;
1546 case 'F': /* Force */
1549 * Only allow one occurrence
1551 if (Fflag != 0) {
1552 usage(EXIT_FAILURE);
1556 * Set flag to force /proc to grab process even though
1557 * it's been grabbed by another process already
1559 Fflag = PGRAB_FORCE;
1560 break;
1562 case '?': /* Unrecognized option */
1563 default:
1564 usage(EXIT_FAILURE);
1565 break;
1571 * Should have more arguments left at least for PID or core
1573 if (optind >= argc)
1574 usage(EXIT_FAILURE);
1576 (void) lgrp_fini(cookie);
1579 * Print heading and process each [pid | core]/lwps argument
1581 print_heading(plgrp_todo.op);
1582 (void) proc_initstdio();
1584 for (i = optind; i < argc && !interrupt; i++) {
1585 (void) proc_flushstdio();
1586 do_process(argv[i], &plgrp_todo, Fflag);
1589 (void) proc_finistdio();
1591 if (plgrp_todo.nthreads == 0) {
1592 (void) fprintf(stderr, gettext("%s: no matching LWPs found\n"),
1593 progname);
1596 return ((nerrors ||interrupt) ? EXIT_NONFATAL : EXIT_SUCCESS);