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]
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
43 #include <sys/lgrp_user.h>
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 */
114 * plgrp(1) operations
116 typedef enum plgrp_ops
{
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 */
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)
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"
204 "\t - Range of lgroup IDs specified as\n"
205 "\t\t<start lgroup ID>-<end lgroup ID>\n"
208 "\t - \"leaves\"\n\n"));
214 * Handler for catching signals from terminal
225 * Return string name for given lgroup affinity
228 lgrp_affinity_string(lgrp_affinity_t aff
)
230 char *rc
= "unknown";
233 case LGRP_AFF_STRONG
:
251 * Add a new lgroup into lgroup array in "arg", growing lgroup and affinity
252 * arrays if necessary
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
,
263 if (arg
->lgrps
== NULL
) {
264 (void) fprintf(stderr
, gettext("%s: out of memory\n"),
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"),
279 arg
->lgrps
[arg
->nlgrps
] = id
;
280 arg
->affs
[arg
->nlgrps
] = LGRP_AFF_INVALID
;
286 * Return an array having '1' for each lgroup present in given subtree under
287 * specified lgroup in lgroup hierarchy
290 lgrps_bitmap_init(lgrp_cookie_t cookie
, lgrp_id_t lgrpid
, char **bitmap_array
,
291 int *bitmap_nelements
)
298 lgrpid
= lgrp_root(cookie
);
304 * If new lgroup cannot fit, grow the array and fill unused portion
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"),
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
)
328 * Get children of specified lgroup and insert descendants of each
331 nchildren
= lgrp_children(cookie
, lgrpid
, NULL
, 0);
333 children
= malloc(nchildren
* sizeof (lgrp_id_t
));
334 if (children
== NULL
) {
335 (void) fprintf(stderr
, gettext("%s: out of memory\n"),
339 if (lgrp_children(cookie
, lgrpid
, children
, nchildren
) !=
345 for (i
= 0; i
< nchildren
; i
++)
346 lgrps_bitmap_init(cookie
, children
[i
], bitmap_array
,
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
361 static lgrp_affinity_t
362 parse_lgrp_affinity(char *string
, char **next
)
364 int rc
= LGRP_AFF_INVALID
;
367 return (LGRP_AFF_INVALID
);
372 if (string
[0] == DELIMIT_AFF
)
376 * Return lgroup affinity matching string
378 if (strncmp(string
, LGRP_AFF_NONE_STR
, strlen(LGRP_AFF_NONE_STR
))
381 *next
= string
+ strlen(LGRP_AFF_NONE_STR
);
382 } else if (strncmp(string
,
383 LGRP_AFF_WEAK_STR
, strlen(LGRP_AFF_WEAK_STR
)) == 0) {
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
);
397 * Parse lgroups from given string
398 * Returns the set containing all lgroups parsed or NULL.
401 parse_lgrps(lgrp_cookie_t cookie
, plgrp_args_t
*arg
, char *s
)
406 if (cookie
== LGRP_COOKIE_NONE
|| s
== NULL
|| NLGRPS
<= 0)
410 * Parse first lgroup (if any)
412 token
= strtok(s
, DELIMIT_LGRP
);
420 if (isdigit(*token
)) {
428 * Can be <lgroup ID>[-<lgroup ID>]
430 p
= strchr(token
, DELIMIT_RANGE
);
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
);
444 (void) fprintf(stderr
,
445 gettext("%s: bad lgroup %d\n"),
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
++) {
457 lgrps_add_lgrp(arg
, i
);
459 } else if (strncmp(token
, LGRP_ROOT_STR
,
460 strlen(LGRP_ROOT_STR
)) == 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
++) {
471 lgrp_children(cookie
, i
, NULL
, 0) == 0)
472 lgrps_add_lgrp(arg
, i
);
477 } while (token
= strtok(NULL
, DELIMIT_LGRP
));
483 * Print array of lgroup IDs, collapsing any consecutive runs of IDs into a
484 * range (eg. 2,3,4 into 2-4)
487 print_lgrps(lgrp_id_t
*lgrps
, int nlgrps
)
494 * Initial range consists of the first element
496 start
= end
= lgrps
[0];
498 for (i
= 1; i
< nlgrps
; 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
511 * Next lgroup ID is not consecutive, so print lgroup
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
526 start
= end
= lgrpid
;
531 * Print last lgroup ID(s)
534 (void) printf("%d", (int)start
);
535 } else if (end
> start
+ 1) {
536 (void) printf("%d-%d", (int)start
, (int)end
);
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.
548 print_affinities(lgrp_id_t
*lgrps
, lgrp_affinity_t
*affs
, int nelements
)
551 lgrp_id_t
*lgrps_none
;
552 lgrp_id_t
*lgrps_strong
;
553 lgrp_id_t
*lgrps_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"),
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
)
584 case LGRP_AFF_STRONG
:
585 lgrps_strong
[nlgrps_strong
++] = lgrpid
;
588 lgrps_weak
[nlgrps_weak
++] = lgrpid
;
591 lgrps_none
[nlgrps_none
++] = lgrpid
;
595 * Skip any lgroups with invalid affinity.
602 * Print all lgroups with same affinity together
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
);
612 print_lgrps(lgrps_weak
, nlgrps_weak
);
613 (void) printf("/%s", lgrp_affinity_string(LGRP_AFF_WEAK
));
615 (void) printf("%c", DELIMIT_AFF_LST
);
619 print_lgrps(lgrps_none
, nlgrps_none
);
620 (void) printf("/%s", lgrp_affinity_string(LGRP_AFF_NONE
));
630 * Print heading for specified operation
633 print_heading(plgrp_ops_t op
)
637 case PLGRP_AFFINITY_GET
:
638 (void) printf(HDR_PLGRP_AFF_GET
);
641 case PLGRP_AFFINITY_SET
:
642 (void) printf(HDR_PLGRP_AFF_SET
);
646 (void) printf(HDR_PLGRP_HOME_GET
);
650 (void) printf(HDR_PLGRP_HOME_SET
);
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
,
665 lgrp_affinity_args_t args
;
674 * Fill in arguments needed for syscall(SYS_lgrpsys,
675 * LGRP_SYS_AFFINITY_GET, 0, &args)
677 syscall
= SYS_lgrpsys
;
679 args
.idtype
= idtype
;
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
;
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;
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;
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
);
712 * Have agent LWP call syscall with appropriate arguments in target
715 Pretval
= Psyscall(Ph
, &retval
, syscall
, Pnargs
, &Pargd
[0]);
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
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
;
741 * Fill in arguments needed for syscall(SYS_lgrpsys,
742 * LGRP_SYS_AFFINITY_SET, 0, &args)
744 syscall
= SYS_lgrpsys
;
746 args
.idtype
= idtype
;
752 * Fill out /proc argument descriptors for syscall(SYS_lgrpsys,
753 * LGRP_SYS_AFFINITY_SET, idtype, id)
755 Pnargs
= LGRPSYS_NARGS
;
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;
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;
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
);
779 * Have agent LWP call syscall with appropriate arguments in
782 Pretval
= Psyscall(Ph
, &retval
, syscall
, Pnargs
, &Pargd
[0]);
784 errno
= (Pretval
< 0) ? ENOSYS
: Pretval
;
788 return (retval
.sys_rval1
);
792 * Use /proc to call lgrp_home() in another process
795 Plgrp_home(struct ps_prochandle
*Ph
, idtype_t idtype
, id_t id
)
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
;
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;
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;
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;
838 * Have agent LWP call syscall with appropriate arguments in
841 Pretval
= Psyscall(Ph
, &retval
, syscall
, Pnargs
, &Pargd
[0]);
843 errno
= (Pretval
< 0) ? ENOSYS
: Pretval
;
847 return (retval
.sys_rval1
);
851 * Use /proc to call lgrp_affinity_set(3LGRP) to set home lgroup of given
855 Plgrp_home_set(struct ps_prochandle
*Ph
, idtype_t idtype
, id_t id
,
858 return (Plgrp_affinity_set(Ph
, idtype
, id
, lgrp
,
864 * Do plgrp(1) operation on specified thread
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
;
874 lgrp_affinity_t
*init_affs
;
876 lgrp_id_t
*lgrps_changed
;
880 struct ps_prochandle
*Ph
;
884 * No args, so nothing to do.
886 if (plgrp_args
== NULL
)
890 * Unpack plgrp(1) arguments and state needed to process this LWP
893 lgrps
= plgrp_args
->lgrps
;
894 affs
= plgrp_args
->affs
;
895 nlgrps
= plgrp_args
->nlgrps
;
897 switch (plgrp_args
->op
) {
901 * Get and display home lgroup for given LWP
903 home
= lwpsinfo
->pr_lgrp
;
904 (void) printf(FMT_HOME
"\n", (int)home
);
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
,
922 if (affs
[i
] == LGRP_AFF_INVALID
) {
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
,
933 * Print affinities for each type.
935 print_affinities(lgrps
, affs
, nlgrps
);
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) {
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
],
959 int width
= strlen(HDR_PLGRP_HOME_CHANGE
);
961 home
= Plgrp_home(Ph
, P_LWPID
, lwpid
);
964 (void) fprintf(stderr
,
965 gettext("%s cannot get home lgroup for"
967 progname
, pid
, lwpid
, strerror(errno
));
971 len
= printf(FMT_NEWHOME
, (int)old_home
, (int)home
);
973 (void) printf("%*c\n", (int)(width
- len
), ' ');
976 plgrp_args
->index
= (i
+ 1) % nlgrps
;
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"),
1009 Prelease(Ph
, PRELEASE_RETAIN
);
1013 return (EXIT_NONFATAL
);
1017 * Initialize current and initial lgroup affinities and
1020 for (lgrpid
= 0; lgrpid
< NLGRPS
; lgrpid
++) {
1022 if (!LGRP_VALID(lgrpid
)) {
1023 init_affs
[lgrpid
] = LGRP_AFF_INVALID
;
1026 Plgrp_affinity_get(Ph
, P_LWPID
,
1029 if (init_affs
[lgrpid
] == LGRP_AFF_INVALID
) {
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
,
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
];
1053 * If the suggested affinity is the same as the current
1054 * one, skip this lgroup.
1056 if (aff
== cur_affs
[lgrpid
])
1060 * Set affinity to the new value
1062 if (Plgrp_affinity_set(Ph
, P_LWPID
, lwpid
, lgrpid
,
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
),
1076 * Get the new value and verify that it changed as
1080 Plgrp_affinity_get(Ph
, P_LWPID
, lwpid
, lgrpid
);
1082 if (cur_affs
[lgrpid
] == LGRP_AFF_INVALID
) {
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
,
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"
1098 progname
, lgrpid
, cur_affs
[lgrpid
], aff
,
1105 * Compare current and initial affinities and mark lgroups with
1106 * changed affinities.
1109 for (lgrpid
= 0; lgrpid
< NLGRPS
; lgrpid
++) {
1110 if (init_affs
[lgrpid
] != cur_affs
[lgrpid
]) {
1111 lgrps_changed
[lgrpid
] = lgrpid
;
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
),
1129 print_affinities(lgrps_changed
, cur_affs
, NLGRPS
);
1130 (void) printf("\n");
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
);
1141 (void) fprintf(stderr
,
1142 gettext("%s: cannot get home"
1143 " for %d/%d: %s\n"),
1144 progname
, pid
, lwpid
, strerror(errno
));
1147 if (old_home
!= home
) {
1151 * Fit string into fixed width
1153 len
= printf(FMT_NEWHOME
,
1154 (int)old_home
, (int)home
);
1156 (void) printf("%*c", width
- len
, ' ');
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
);
1185 * Routine called by Plwp_iter_all() as it iterates through LWPs of another
1190 Plwp_iter_handler(void *arg
, const lwpstatus_t
*lwpstatus
,
1191 const lwpsinfo_t
*lwpsinfo
)
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
)
1205 * Unpack plgrp(1) arguments and state needed to process this LWP
1208 Ph
= plgrp_args
->Ph
;
1211 * Just return if no /proc handle for process
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
))
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
1240 do_process(char *pidstring
, plgrp_args_t
*plgrp_todo
, int force
)
1244 struct ps_prochandle
*Ph
;
1247 * Nothing to do, so return.
1249 if (plgrp_todo
== NULL
|| interrupt
)
1253 * Grab target process or core and return
1254 * /proc handle for process and string of LWP
1257 Ph
= proc_arg_xgrab(pidstring
, NULL
,
1258 PR_ARG_ANY
, force
| PGRAB_RETAIN
| PGRAB_NOSTOP
, &error
, &lwps
);
1260 (void) fprintf(stderr
,
1261 gettext("%s: Unable to grab process %s: %s\n"),
1262 progname
, pidstring
, Pgrab_error(error
));
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
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"),
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
;
1309 lgrp_cookie_t cookie
;
1313 plgrp_args_t plgrp_todo
;
1316 (void) setlocale(LC_ALL
, "");
1317 (void) textdomain(TEXT_DOMAIN
);
1322 * Get name of program
1324 progname
= basename(argv
[0]);
1327 * Not much to do when only name of program given
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
1364 bzero(&plgrp_todo
, sizeof (plgrp_args_t
));
1365 plgrp_todo
.op
= PLGRP_HOME_GET
;
1372 while (!interrupt
&& (c
= getopt(argc
, argv
, "a:A:FhH:")) != -1) {
1374 * Parse option and only allow one option besides -F to be
1379 case 'h': /* Get home lgroup */
1381 * Only allow one option (besides -F) to be specified
1384 usage(EXIT_FAILURE
);
1387 plgrp_todo
.op
= PLGRP_HOME_GET
;
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
);
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"),
1412 usage(EXIT_FAILURE
);
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
);
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"
1439 usage(EXIT_FAILURE
);
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
);
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.
1464 while (affstring
!= NULL
&& strlen(affstring
) > 0) {
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
);
1475 (void) fprintf(stderr
,
1476 gettext("%s: invalid "
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 "
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 "
1501 usage(EXIT_FAILURE
);
1509 * Now parse the list of lgroups
1511 if (parse_lgrps(cookie
, &plgrp_todo
,
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
] ==
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.
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
);
1546 case 'F': /* Force */
1549 * Only allow one occurrence
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
;
1562 case '?': /* Unrecognized option */
1564 usage(EXIT_FAILURE
);
1571 * Should have more arguments left at least for PID or core
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"),
1596 return ((nerrors
||interrupt
) ? EXIT_NONFATAL
: EXIT_SUCCESS
);