xref: /onnv-gate/usr/src/cmd/cpc/common/strtoset.c (revision 2791:59a687d6d9c0)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2791Srab  * Common Development and Distribution License (the "License").
6*2791Srab  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2791Srab  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <alloca.h>
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include <libintl.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "libcpc.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * Takes a string and converts it to a cpc_set_t.
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * While processing the string using getsubopt(), we will use an array of
400Sstevel@tonic-gate  * requests to hold the data, and a proprietary representation of attributes
410Sstevel@tonic-gate  * which allow us to avoid a realloc()/bcopy() dance every time we come across
420Sstevel@tonic-gate  * a new attribute.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * Not until after the string has been processed in its entirety do we
450Sstevel@tonic-gate  * allocate and specify a request set properly.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * Leave enough room in token strings for picn, nousern, or sysn where n is
500Sstevel@tonic-gate  * picnum.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate #define	TOK_SIZE	10
530Sstevel@tonic-gate 
540Sstevel@tonic-gate typedef struct __tmp_attr {
550Sstevel@tonic-gate 	char			*name;
560Sstevel@tonic-gate 	uint64_t		val;
570Sstevel@tonic-gate 	struct __tmp_attr	*next;
580Sstevel@tonic-gate } tmp_attr_t;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate typedef struct __tok_info {
610Sstevel@tonic-gate 	char			*name;
620Sstevel@tonic-gate 	int			picnum;
630Sstevel@tonic-gate } tok_info_t;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate typedef struct __request_t {
660Sstevel@tonic-gate 	char			cr_event[CPC_MAX_EVENT_LEN];
670Sstevel@tonic-gate 	uint_t			cr_flags;
680Sstevel@tonic-gate 	uint_t			cr_nattrs;	/* # CPU-specific attrs */
690Sstevel@tonic-gate } request_t;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static void strtoset_cleanup(void);
720Sstevel@tonic-gate static void smt_special(int picnum);
730Sstevel@tonic-gate static void *emalloc(size_t n);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * Clients of cpc_strtoset may set this to specify an error handler during
770Sstevel@tonic-gate  * string parsing.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate cpc_errhndlr_t		*strtoset_errfn = NULL;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static request_t	*reqs;
820Sstevel@tonic-gate static int		nreqs;
830Sstevel@tonic-gate static int		ncounters;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static tmp_attr_t	**attrs;
860Sstevel@tonic-gate static int		ntoks;
870Sstevel@tonic-gate static char		**toks;
880Sstevel@tonic-gate static tok_info_t	*tok_info;
890Sstevel@tonic-gate static int		(*(*tok_funcs))(int, char *);
900Sstevel@tonic-gate static char		**attrlist;	/* array of ptrs to toks in attrlistp */
910Sstevel@tonic-gate static int		nattrs;
920Sstevel@tonic-gate static cpc_t		*cpc;
930Sstevel@tonic-gate static int		found;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static void
960Sstevel@tonic-gate strtoset_err(const char *fmt, ...)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	va_list ap;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (strtoset_errfn == NULL)
1010Sstevel@tonic-gate 		return;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	va_start(ap, fmt);
1040Sstevel@tonic-gate 	(*strtoset_errfn)("cpc_strtoset", -1, fmt, ap);
1050Sstevel@tonic-gate 	va_end(ap);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*ARGSUSED*/
1090Sstevel@tonic-gate static void
1100Sstevel@tonic-gate event_walker(void *arg, uint_t picno, const char *event)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	if (strncmp(arg, event, CPC_MAX_EVENT_LEN) == 0)
1130Sstevel@tonic-gate 		found = 1;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static int
1170Sstevel@tonic-gate event_valid(int picnum, char *event)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	found = 0;
120*2791Srab 
1210Sstevel@tonic-gate 	cpc_walk_events_pic(cpc, picnum, event, event_walker);
122*2791Srab 
123*2791Srab 	if (found)
124*2791Srab 		return (1);
125*2791Srab 
126*2791Srab 	/*
127*2791Srab 	 * Before assuming this is an invalid event, see if we have been given
128*2791Srab 	 * a raw event code. An event code of '0' is not recognized, as it
129*2791Srab 	 * already has a corresponding event name in existing backends and it
130*2791Srab 	 * is the only reasonable way to know if strtol() succeeded.
131*2791Srab 	 */
132*2791Srab 	if (strtol(event, NULL, 0) != 0)
133*2791Srab 		/*
134*2791Srab 		 * Success - this is a valid raw code in hex, decimal, or octal.
135*2791Srab 		 */
136*2791Srab 		return (1);
137*2791Srab 
138*2791Srab 	return (0);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * An unknown token was encountered; check here if it is an implicit event
1430Sstevel@tonic-gate  * name. We allow users to omit the "picn=" portion of the event spec, and
1440Sstevel@tonic-gate  * assign such events to available pics in order they are returned from
1450Sstevel@tonic-gate  * getsubopt(3C). We start our search for an available pic _after_ the highest
1460Sstevel@tonic-gate  * picnum to be assigned. This ensures that the event spec can never be out of
1470Sstevel@tonic-gate  * order; i.e. if the event string is "eventa,eventb" we must ensure that the
1480Sstevel@tonic-gate  * picnum counting eventa is less than the picnum counting eventb.
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate static int
1510Sstevel@tonic-gate find_event(char *event)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	int i;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/*
156*2791Srab 	 * Event names cannot have '=' in them. If present here, it means we
157*2791Srab 	 * have encountered an unknown token (foo=bar, for example).
158*2791Srab 	 */
159*2791Srab 	if (strchr(event, '=') != NULL)
160*2791Srab 		return (0);
161*2791Srab 
162*2791Srab 	/*
1630Sstevel@tonic-gate 	 * Find the first unavailable pic, after which we must start our search.
1640Sstevel@tonic-gate 	 */
1650Sstevel@tonic-gate 	for (i = ncounters - 1; i >= 0; i--) {
1660Sstevel@tonic-gate 		if (reqs[i].cr_event[0] != '\0')
1670Sstevel@tonic-gate 			break;
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 	/*
1700Sstevel@tonic-gate 	 * If the last counter has been assigned, we cannot place this event.
1710Sstevel@tonic-gate 	 */
1720Sstevel@tonic-gate 	if (i == ncounters - 1)
1730Sstevel@tonic-gate 		return (0);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * If none of the counters have been assigned yet, i is -1 and we will
1770Sstevel@tonic-gate 	 * begin our search at 0. Else we begin our search at the counter after
1780Sstevel@tonic-gate 	 * the last one currently assigned.
1790Sstevel@tonic-gate 	 */
1800Sstevel@tonic-gate 	i++;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	for (; i < ncounters; i++) {
1830Sstevel@tonic-gate 		if (event_valid(i, event) == 0)
1840Sstevel@tonic-gate 			continue;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		nreqs++;
1870Sstevel@tonic-gate 		(void) strncpy(reqs[i].cr_event, event, CPC_MAX_EVENT_LEN);
1880Sstevel@tonic-gate 		return (1);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	return (0);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static int
1950Sstevel@tonic-gate pic(int tok, char *val)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	int picnum = tok_info[tok].picnum;
1980Sstevel@tonic-gate 	/*
1990Sstevel@tonic-gate 	 * Make sure the each pic only appears in the spec once.
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	if (reqs[picnum].cr_event[0] != '\0') {
2020Sstevel@tonic-gate 		strtoset_err(gettext("repeated 'pic%d' token\n"), picnum);
2030Sstevel@tonic-gate 		return (-1);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (val == NULL || val[0] == '\0') {
2070Sstevel@tonic-gate 		strtoset_err(gettext("missing 'pic%d' value\n"), picnum);
2080Sstevel@tonic-gate 		return (-1);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (event_valid(picnum, val) == 0) {
2120Sstevel@tonic-gate 		strtoset_err(gettext("pic%d cannot measure event '%s' on this "
2130Sstevel@tonic-gate 		    "cpu\n"), picnum, val);
2140Sstevel@tonic-gate 		return (-1);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	nreqs++;
2180Sstevel@tonic-gate 	(void) strncpy(reqs[picnum].cr_event, val, CPC_MAX_EVENT_LEN);
2190Sstevel@tonic-gate 	return (0);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate  * We explicitly ignore any value provided for these tokens, as their
2240Sstevel@tonic-gate  * mere presence signals us to turn on or off the relevant flags.
2250Sstevel@tonic-gate  */
2260Sstevel@tonic-gate /*ARGSUSED*/
2270Sstevel@tonic-gate static int
2280Sstevel@tonic-gate flag(int tok, char *val)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	int i;
2310Sstevel@tonic-gate 	int picnum = tok_info[tok].picnum;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/*
2340Sstevel@tonic-gate 	 * If picnum is -1, this flag should be applied to all reqs.
2350Sstevel@tonic-gate 	 */
2360Sstevel@tonic-gate 	for (i = (picnum == -1) ? 0 : picnum; i < ncounters; i++) {
2370Sstevel@tonic-gate 		if (strcmp(tok_info[tok].name, "nouser") == 0)
2380Sstevel@tonic-gate 			reqs[i].cr_flags &= ~CPC_COUNT_USER;
2390Sstevel@tonic-gate 		else if (strcmp(tok_info[tok].name, "sys") == 0)
2400Sstevel@tonic-gate 			reqs[i].cr_flags |= CPC_COUNT_SYSTEM;
2410Sstevel@tonic-gate 		else
2420Sstevel@tonic-gate 			return (-1);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		if (picnum != -1)
2450Sstevel@tonic-gate 			break;
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	return (0);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate static int
2520Sstevel@tonic-gate doattr(int tok, char *val)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	int		i;
2550Sstevel@tonic-gate 	int		picnum = tok_info[tok].picnum;
2560Sstevel@tonic-gate 	tmp_attr_t	*tmp;
2570Sstevel@tonic-gate 	char		*endptr;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/*
2600Sstevel@tonic-gate 	 * If picnum is -1, this attribute should be applied to all reqs.
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	for (i = (picnum == -1) ? 0 : picnum; i < ncounters; i++) {
2630Sstevel@tonic-gate 		tmp = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t));
2640Sstevel@tonic-gate 		tmp->name = tok_info[tok].name;
2650Sstevel@tonic-gate 		if (val != NULL) {
2660Sstevel@tonic-gate 			tmp->val = strtoll(val, &endptr, 0);
2670Sstevel@tonic-gate 			if (endptr == val) {
2680Sstevel@tonic-gate 				strtoset_err(gettext("invalid value '%s' for "
2690Sstevel@tonic-gate 				    "attribute '%s'\n"), val, tmp->name);
2700Sstevel@tonic-gate 				free(tmp);
2710Sstevel@tonic-gate 				return (-1);
2720Sstevel@tonic-gate 			}
2730Sstevel@tonic-gate 		} else
2740Sstevel@tonic-gate 			/*
2750Sstevel@tonic-gate 			 * No value was provided for this attribute,
2760Sstevel@tonic-gate 			 * so specify a default value of 1.
2770Sstevel@tonic-gate 			 */
2780Sstevel@tonic-gate 			tmp->val = 1;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 		tmp->next = attrs[i];
2810Sstevel@tonic-gate 		attrs[i] = tmp;
2820Sstevel@tonic-gate 		reqs[i].cr_nattrs++;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 		if (picnum != -1)
2850Sstevel@tonic-gate 			break;
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	return (0);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*ARGSUSED*/
2920Sstevel@tonic-gate static void
2930Sstevel@tonic-gate attr_count_walker(void *arg, const char *attr)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	/*
2960Sstevel@tonic-gate 	 * We don't allow picnum to be specified by the user.
2970Sstevel@tonic-gate 	 */
2980Sstevel@tonic-gate 	if (strncmp(attr, "picnum", 7) == 0)
2990Sstevel@tonic-gate 		return;
3000Sstevel@tonic-gate 	(*(int *)arg)++;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate static int
3040Sstevel@tonic-gate cpc_count_attrs(cpc_t *cpc)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	int nattrs = 0;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	cpc_walk_attrs(cpc, &nattrs, attr_count_walker);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	return (nattrs);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate static void
3140Sstevel@tonic-gate attr_walker(void *arg, const char *attr)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate 	int *i = arg;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (strncmp(attr, "picnum", 7) == 0)
3190Sstevel@tonic-gate 		return;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	if ((attrlist[(*i)++] = strdup(attr)) == NULL) {
3220Sstevel@tonic-gate 		strtoset_err(gettext("no memory available\n"));
3230Sstevel@tonic-gate 		exit(0);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate cpc_set_t *
3280Sstevel@tonic-gate cpc_strtoset(cpc_t *cpcin, const char *spec, int smt)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	cpc_set_t		*set;
3310Sstevel@tonic-gate 	cpc_attr_t		*req_attrs;
3320Sstevel@tonic-gate 	tmp_attr_t		*tmp;
3330Sstevel@tonic-gate 	size_t			toklen;
3340Sstevel@tonic-gate 	int			i;
3350Sstevel@tonic-gate 	int			j;
3360Sstevel@tonic-gate 	int			x;
3370Sstevel@tonic-gate 	char			*opts;
3380Sstevel@tonic-gate 	char			*val;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	cpc = cpcin;
3410Sstevel@tonic-gate 	nattrs = 0;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	ncounters = cpc_npic(cpc);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	reqs = (request_t *)emalloc(ncounters * sizeof (request_t));
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	attrs = (tmp_attr_t **)emalloc(ncounters * sizeof (tmp_attr_t *));
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
3500Sstevel@tonic-gate 		reqs[i].cr_event[0] = '\0';
3510Sstevel@tonic-gate 		reqs[i].cr_flags = CPC_COUNT_USER;
3520Sstevel@tonic-gate 		/*
3530Sstevel@tonic-gate 		 * Each pic will have at least one attribute: the physical pic
3540Sstevel@tonic-gate 		 * assignment via the "picnum" attribute. Set that up here for
3550Sstevel@tonic-gate 		 * each request.
3560Sstevel@tonic-gate 		 */
3570Sstevel@tonic-gate 		reqs[i].cr_nattrs = 1;
3580Sstevel@tonic-gate 		attrs[i] = emalloc(sizeof (tmp_attr_t));
3590Sstevel@tonic-gate 		attrs[i]->name = "picnum";
3600Sstevel@tonic-gate 		attrs[i]->val = i;
3610Sstevel@tonic-gate 		attrs[i]->next = NULL;
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/*
3650Sstevel@tonic-gate 	 * Build up a list of acceptable tokens.
3660Sstevel@tonic-gate 	 *
3670Sstevel@tonic-gate 	 * Permitted tokens are
3680Sstevel@tonic-gate 	 * picn=event
3690Sstevel@tonic-gate 	 * nousern
3700Sstevel@tonic-gate 	 * sysn
3710Sstevel@tonic-gate 	 * attrn=val
3720Sstevel@tonic-gate 	 * nouser
3730Sstevel@tonic-gate 	 * sys
3740Sstevel@tonic-gate 	 * attr=val
3750Sstevel@tonic-gate 	 *
3760Sstevel@tonic-gate 	 * Where n is a counter number, and attr is any attribute supported by
3770Sstevel@tonic-gate 	 * the current processor.
3780Sstevel@tonic-gate 	 *
3790Sstevel@tonic-gate 	 * If a token appears without a counter number, it applies to all
3800Sstevel@tonic-gate 	 * counters in the request set.
3810Sstevel@tonic-gate 	 *
3820Sstevel@tonic-gate 	 * The number of tokens is:
3830Sstevel@tonic-gate 	 *
3840Sstevel@tonic-gate 	 * picn: ncounters
3850Sstevel@tonic-gate 	 * generic flags: 2 * ncounters (nouser, sys)
3860Sstevel@tonic-gate 	 * attrs: nattrs * ncounters
3870Sstevel@tonic-gate 	 * attrs with no picnum: nattrs
3880Sstevel@tonic-gate 	 * generic flags with no picnum: 2 (nouser, sys)
3890Sstevel@tonic-gate 	 * NULL token to signify end of list to getsubopt(3C).
3900Sstevel@tonic-gate 	 *
3910Sstevel@tonic-gate 	 * Matching each token's index in the token table is a function which
3920Sstevel@tonic-gate 	 * process that token; these are in tok_funcs.
3930Sstevel@tonic-gate 	 */
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Count the number of valid attributes.
3970Sstevel@tonic-gate 	 * Set up the attrlist array to point to the attributes in attrlistp.
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	nattrs = cpc_count_attrs(cpc);
4000Sstevel@tonic-gate 	attrlist = (char **)emalloc(nattrs * sizeof (char *));
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	i = 0;
4030Sstevel@tonic-gate 	cpc_walk_attrs(cpc, &i, attr_walker);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	ntoks = ncounters + (2 * ncounters) + (nattrs * ncounters) + nattrs + 3;
4060Sstevel@tonic-gate 	toks = (char **)emalloc(ntoks * sizeof (char *));
4070Sstevel@tonic-gate 	tok_info = (tok_info_t *)emalloc(ntoks * sizeof (tok_info_t));
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	tok_funcs = (int (**)(int, char *))emalloc(ntoks *
4100Sstevel@tonic-gate 	    sizeof (int (*)(char *)));
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	for (i = 0; i < ntoks; i++) {
4130Sstevel@tonic-gate 		toks[i] = NULL;
4140Sstevel@tonic-gate 		tok_funcs[i] = NULL;
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	x = 0;
4180Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
4190Sstevel@tonic-gate 		toks[x] = (char *)emalloc(TOK_SIZE);
4200Sstevel@tonic-gate 		(void) snprintf(toks[x], TOK_SIZE, "pic%d", i);
4210Sstevel@tonic-gate 		tok_info[x].name = "pic";
4220Sstevel@tonic-gate 		tok_info[i].picnum = i;
4230Sstevel@tonic-gate 		tok_funcs[x] = pic;
4240Sstevel@tonic-gate 		x++;
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
4280Sstevel@tonic-gate 		toks[x] = (char *)emalloc(TOK_SIZE);
4290Sstevel@tonic-gate 		(void) snprintf(toks[x], TOK_SIZE, "nouser%d", i);
4300Sstevel@tonic-gate 		tok_info[x].name = "nouser";
4310Sstevel@tonic-gate 		tok_info[x].picnum = i;
4320Sstevel@tonic-gate 		tok_funcs[x] = flag;
4330Sstevel@tonic-gate 		x++;
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
4370Sstevel@tonic-gate 		toks[x] = (char *)emalloc(TOK_SIZE);
4380Sstevel@tonic-gate 		(void) snprintf(toks[x], TOK_SIZE, "sys%d", i);
4390Sstevel@tonic-gate 		tok_info[x].name = "sys";
4400Sstevel@tonic-gate 		tok_info[x].picnum = i;
4410Sstevel@tonic-gate 		tok_funcs[x] = flag;
4420Sstevel@tonic-gate 		x++;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 	for (j = 0; j < nattrs; j++) {
4450Sstevel@tonic-gate 		toklen = strlen(attrlist[j]) + 3;
4460Sstevel@tonic-gate 		for (i = 0; i < ncounters; i++) {
4470Sstevel@tonic-gate 			toks[x] = (char *)emalloc(toklen);
4480Sstevel@tonic-gate 			(void) snprintf(toks[x], toklen, "%s%d", attrlist[j],
4490Sstevel@tonic-gate 			    i);
4500Sstevel@tonic-gate 			tok_info[x].name = attrlist[j];
4510Sstevel@tonic-gate 			tok_info[x].picnum = i;
4520Sstevel@tonic-gate 			tok_funcs[x] = doattr;
4530Sstevel@tonic-gate 			x++;
4540Sstevel@tonic-gate 		}
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 		/*
4570Sstevel@tonic-gate 		 * Now create a token for this attribute with no picnum; if used
4580Sstevel@tonic-gate 		 * it will be applied to all reqs.
4590Sstevel@tonic-gate 		 */
4600Sstevel@tonic-gate 		toks[x] = (char *)emalloc(toklen);
4610Sstevel@tonic-gate 		(void) snprintf(toks[x], toklen, "%s", attrlist[j]);
4620Sstevel@tonic-gate 		tok_info[x].name = attrlist[j];
4630Sstevel@tonic-gate 		tok_info[x].picnum = -1;
4640Sstevel@tonic-gate 		tok_funcs[x] = doattr;
4650Sstevel@tonic-gate 		x++;
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	toks[x] = "nouser";
4690Sstevel@tonic-gate 	tok_info[x].name = "nouser";
4700Sstevel@tonic-gate 	tok_info[x].picnum = -1;
4710Sstevel@tonic-gate 	tok_funcs[x] = flag;
4720Sstevel@tonic-gate 	x++;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	toks[x] = "sys";
4750Sstevel@tonic-gate 	tok_info[x].name = "sys";
4760Sstevel@tonic-gate 	tok_info[x].picnum = -1;
4770Sstevel@tonic-gate 	tok_funcs[x] = flag;
4780Sstevel@tonic-gate 	x++;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	toks[x] = NULL;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	opts = strcpy(alloca(strlen(spec) + 1), spec);
4830Sstevel@tonic-gate 	while (*opts != '\0') {
4840Sstevel@tonic-gate 		int idx = getsubopt(&opts, toks, &val);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 		if (idx == -1) {
4870Sstevel@tonic-gate 			if (find_event(val) == 0) {
4880Sstevel@tonic-gate 				strtoset_err(gettext("bad token '%s'\n"), val);
4890Sstevel@tonic-gate 				goto inval;
4900Sstevel@tonic-gate 			} else
4910Sstevel@tonic-gate 				continue;
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 		if (tok_funcs[idx](idx, val) == -1)
4950Sstevel@tonic-gate 			goto inval;
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	/*
4990Sstevel@tonic-gate 	 * The string has been processed. Now count how many PICs were used,
5000Sstevel@tonic-gate 	 * create a request set, and specify each request properly.
5010Sstevel@tonic-gate 	 */
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	if ((set = cpc_set_create(cpc)) == NULL) {
5040Sstevel@tonic-gate 		strtoset_err(gettext("no memory available\n"));
5050Sstevel@tonic-gate 		exit(0);
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
5090Sstevel@tonic-gate 		if (reqs[i].cr_event[0] == '\0')
5100Sstevel@tonic-gate 			continue;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 		/*
5130Sstevel@tonic-gate 		 * If the caller wishes to measure events on the physical CPU,
5140Sstevel@tonic-gate 		 * we need to add SMT attributes to each request.
5150Sstevel@tonic-gate 		 */
5160Sstevel@tonic-gate 		if (smt)
5170Sstevel@tonic-gate 			smt_special(i);
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 		req_attrs = (cpc_attr_t *)emalloc(reqs[i].cr_nattrs *
5200Sstevel@tonic-gate 		    sizeof (cpc_attr_t));
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 		j = 0;
5230Sstevel@tonic-gate 		for (tmp = attrs[i]; tmp != NULL; tmp = tmp->next) {
5240Sstevel@tonic-gate 			req_attrs[j].ca_name = tmp->name;
5250Sstevel@tonic-gate 			req_attrs[j].ca_val = tmp->val;
5260Sstevel@tonic-gate 			j++;
5270Sstevel@tonic-gate 		}
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		if (cpc_set_add_request(cpc, set, reqs[i].cr_event, 0,
5300Sstevel@tonic-gate 		    reqs[i].cr_flags, reqs[i].cr_nattrs, req_attrs) == -1) {
5310Sstevel@tonic-gate 			free(req_attrs);
5320Sstevel@tonic-gate 			(void) cpc_set_destroy(cpc, set);
5330Sstevel@tonic-gate 			strtoset_err(
5340Sstevel@tonic-gate 				gettext("cpc_set_add_request() failed: %s\n"),
5350Sstevel@tonic-gate 				    strerror(errno));
5360Sstevel@tonic-gate 			goto inval;
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		free(req_attrs);
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	strtoset_cleanup();
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	return (set);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate inval:
5470Sstevel@tonic-gate 	strtoset_cleanup();
5480Sstevel@tonic-gate 	errno = EINVAL;
5490Sstevel@tonic-gate 	return (NULL);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate static void
5530Sstevel@tonic-gate strtoset_cleanup(void)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	int		i;
5560Sstevel@tonic-gate 	tmp_attr_t	*tmp, *p;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	for (i = 0; i < nattrs; i++)
5590Sstevel@tonic-gate 		free(attrlist[i]);
5600Sstevel@tonic-gate 	free(attrlist);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
5630Sstevel@tonic-gate 		for (tmp = attrs[i]; tmp != NULL; tmp = p) {
5640Sstevel@tonic-gate 			p = tmp->next;
5650Sstevel@tonic-gate 			free(tmp);
5660Sstevel@tonic-gate 		}
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 	free(attrs);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	for (i = 0; i < ntoks - 3; i++)
5710Sstevel@tonic-gate 		/*
5720Sstevel@tonic-gate 		 * We free all but the last three tokens: "nouser", "sys", NULL
5730Sstevel@tonic-gate 		 */
5740Sstevel@tonic-gate 		free(toks[i]);
5750Sstevel@tonic-gate 	free(toks);
5760Sstevel@tonic-gate 	free(reqs);
5770Sstevel@tonic-gate 	free(tok_info);
5780Sstevel@tonic-gate 	free(tok_funcs);
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate /*
5820Sstevel@tonic-gate  * The following is called to modify requests so that they count events on
5830Sstevel@tonic-gate  * behalf of a physical processor, instead of a logical processor. It duplicates
5840Sstevel@tonic-gate  * the request flags for the sibling processor (i.e. if the request counts user
5850Sstevel@tonic-gate  * events, add an attribute to count user events on the sibling processor also).
5860Sstevel@tonic-gate  */
5870Sstevel@tonic-gate static void
5880Sstevel@tonic-gate smt_special(int picnum)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	tmp_attr_t *attr;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if (reqs[picnum].cr_flags & CPC_COUNT_USER) {
5930Sstevel@tonic-gate 		attr = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t));
5940Sstevel@tonic-gate 		attr->name = "count_sibling_usr";
5950Sstevel@tonic-gate 		attr->val = 1;
5960Sstevel@tonic-gate 		attr->next = attrs[picnum];
5970Sstevel@tonic-gate 		attrs[picnum] = attr;
5980Sstevel@tonic-gate 		reqs[picnum].cr_nattrs++;
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if (reqs[picnum].cr_flags & CPC_COUNT_SYSTEM) {
6020Sstevel@tonic-gate 		attr = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t));
6030Sstevel@tonic-gate 		attr->name = "count_sibling_sys";
6040Sstevel@tonic-gate 		attr->val = 1;
6050Sstevel@tonic-gate 		attr->next = attrs[picnum];
6060Sstevel@tonic-gate 		attrs[picnum] = attr;
6070Sstevel@tonic-gate 		reqs[picnum].cr_nattrs++;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate /*
6120Sstevel@tonic-gate  * If we ever fail to get memory, we print an error message and exit.
6130Sstevel@tonic-gate  */
6140Sstevel@tonic-gate static void *
6150Sstevel@tonic-gate emalloc(size_t n)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate 	void *p = malloc(n);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	if (p == NULL) {
6200Sstevel@tonic-gate 		strtoset_err(gettext("no memory available\n"));
6210Sstevel@tonic-gate 		exit(0);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	return (p);
6250Sstevel@tonic-gate }
626