xref: /titanic_53/usr/src/cmd/cpc/common/strtoset.c (revision d0ecda70e958c51a970bcec3ea2d03e2d177ff73)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55d3a5ad8Srab  * Common Development and Distribution License (the "License").
65d3a5ad8Srab  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d0ecda70Svk226950  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <alloca.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <libintl.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "libcpc.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Takes a string and converts it to a cpc_set_t.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * While processing the string using getsubopt(), we will use an array of
407c478bd9Sstevel@tonic-gate  * requests to hold the data, and a proprietary representation of attributes
417c478bd9Sstevel@tonic-gate  * which allow us to avoid a realloc()/bcopy() dance every time we come across
427c478bd9Sstevel@tonic-gate  * a new attribute.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * Not until after the string has been processed in its entirety do we
457c478bd9Sstevel@tonic-gate  * allocate and specify a request set properly.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * Leave enough room in token strings for picn, nousern, or sysn where n is
507c478bd9Sstevel@tonic-gate  * picnum.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate #define	TOK_SIZE	10
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate typedef struct __tmp_attr {
557c478bd9Sstevel@tonic-gate 	char			*name;
567c478bd9Sstevel@tonic-gate 	uint64_t		val;
577c478bd9Sstevel@tonic-gate 	struct __tmp_attr	*next;
587c478bd9Sstevel@tonic-gate } tmp_attr_t;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate typedef struct __tok_info {
617c478bd9Sstevel@tonic-gate 	char			*name;
627c478bd9Sstevel@tonic-gate 	int			picnum;
637c478bd9Sstevel@tonic-gate } tok_info_t;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate typedef struct __request_t {
667c478bd9Sstevel@tonic-gate 	char			cr_event[CPC_MAX_EVENT_LEN];
677c478bd9Sstevel@tonic-gate 	uint_t			cr_flags;
687c478bd9Sstevel@tonic-gate 	uint_t			cr_nattrs;	/* # CPU-specific attrs */
697c478bd9Sstevel@tonic-gate } request_t;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static void strtoset_cleanup(void);
727c478bd9Sstevel@tonic-gate static void smt_special(int picnum);
737c478bd9Sstevel@tonic-gate static void *emalloc(size_t n);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Clients of cpc_strtoset may set this to specify an error handler during
777c478bd9Sstevel@tonic-gate  * string parsing.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate cpc_errhndlr_t		*strtoset_errfn = NULL;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static request_t	*reqs;
827c478bd9Sstevel@tonic-gate static int		nreqs;
837c478bd9Sstevel@tonic-gate static int		ncounters;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static tmp_attr_t	**attrs;
867c478bd9Sstevel@tonic-gate static int		ntoks;
877c478bd9Sstevel@tonic-gate static char		**toks;
887c478bd9Sstevel@tonic-gate static tok_info_t	*tok_info;
897c478bd9Sstevel@tonic-gate static int		(*(*tok_funcs))(int, char *);
907c478bd9Sstevel@tonic-gate static char		**attrlist;	/* array of ptrs to toks in attrlistp */
917c478bd9Sstevel@tonic-gate static int		nattrs;
927c478bd9Sstevel@tonic-gate static cpc_t		*cpc;
937c478bd9Sstevel@tonic-gate static int		found;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static void
967c478bd9Sstevel@tonic-gate strtoset_err(const char *fmt, ...)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	va_list ap;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (strtoset_errfn == NULL)
1017c478bd9Sstevel@tonic-gate 		return;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1047c478bd9Sstevel@tonic-gate 	(*strtoset_errfn)("cpc_strtoset", -1, fmt, ap);
1057c478bd9Sstevel@tonic-gate 	va_end(ap);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1097c478bd9Sstevel@tonic-gate static void
1107c478bd9Sstevel@tonic-gate event_walker(void *arg, uint_t picno, const char *event)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	if (strncmp(arg, event, CPC_MAX_EVENT_LEN) == 0)
1137c478bd9Sstevel@tonic-gate 		found = 1;
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static int
1177c478bd9Sstevel@tonic-gate event_valid(int picnum, char *event)
1187c478bd9Sstevel@tonic-gate {
119*d0ecda70Svk226950 	char *end_event;
1207c478bd9Sstevel@tonic-gate 	found = 0;
1215d3a5ad8Srab 
122*d0ecda70Svk226950 
1237c478bd9Sstevel@tonic-gate 	cpc_walk_events_pic(cpc, picnum, event, event_walker);
1245d3a5ad8Srab 
1255d3a5ad8Srab 	if (found)
1265d3a5ad8Srab 		return (1);
1275d3a5ad8Srab 
1285d3a5ad8Srab 	/*
1295d3a5ad8Srab 	 * Before assuming this is an invalid event, see if we have been given
1305d3a5ad8Srab 	 * a raw event code. An event code of '0' is not recognized, as it
1315d3a5ad8Srab 	 * already has a corresponding event name in existing backends and it
1325d3a5ad8Srab 	 * is the only reasonable way to know if strtol() succeeded.
133*d0ecda70Svk226950 	 * Check the second argument of strtol() to ensure invalid events
134*d0ecda70Svk226950 	 * beginning with number do not go through.
1355d3a5ad8Srab 	 */
136*d0ecda70Svk226950 	if ((strtol(event, &end_event, 0) != 0) && (*end_event == '\0'))
1375d3a5ad8Srab 		/*
1385d3a5ad8Srab 		 * Success - this is a valid raw code in hex, decimal, or octal.
1395d3a5ad8Srab 		 */
1405d3a5ad8Srab 		return (1);
1415d3a5ad8Srab 
1425d3a5ad8Srab 	return (0);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * An unknown token was encountered; check here if it is an implicit event
1477c478bd9Sstevel@tonic-gate  * name. We allow users to omit the "picn=" portion of the event spec, and
1487c478bd9Sstevel@tonic-gate  * assign such events to available pics in order they are returned from
1497c478bd9Sstevel@tonic-gate  * getsubopt(3C). We start our search for an available pic _after_ the highest
1507c478bd9Sstevel@tonic-gate  * picnum to be assigned. This ensures that the event spec can never be out of
1517c478bd9Sstevel@tonic-gate  * order; i.e. if the event string is "eventa,eventb" we must ensure that the
1527c478bd9Sstevel@tonic-gate  * picnum counting eventa is less than the picnum counting eventb.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate static int
1557c478bd9Sstevel@tonic-gate find_event(char *event)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	int i;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/*
1605d3a5ad8Srab 	 * Event names cannot have '=' in them. If present here, it means we
1615d3a5ad8Srab 	 * have encountered an unknown token (foo=bar, for example).
1625d3a5ad8Srab 	 */
1635d3a5ad8Srab 	if (strchr(event, '=') != NULL)
1645d3a5ad8Srab 		return (0);
1655d3a5ad8Srab 
1665d3a5ad8Srab 	/*
1677c478bd9Sstevel@tonic-gate 	 * Find the first unavailable pic, after which we must start our search.
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	for (i = ncounters - 1; i >= 0; i--) {
1707c478bd9Sstevel@tonic-gate 		if (reqs[i].cr_event[0] != '\0')
1717c478bd9Sstevel@tonic-gate 			break;
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * If the last counter has been assigned, we cannot place this event.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	if (i == ncounters - 1)
1777c478bd9Sstevel@tonic-gate 		return (0);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/*
1807c478bd9Sstevel@tonic-gate 	 * If none of the counters have been assigned yet, i is -1 and we will
1817c478bd9Sstevel@tonic-gate 	 * begin our search at 0. Else we begin our search at the counter after
1827c478bd9Sstevel@tonic-gate 	 * the last one currently assigned.
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	i++;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	for (; i < ncounters; i++) {
1877c478bd9Sstevel@tonic-gate 		if (event_valid(i, event) == 0)
1887c478bd9Sstevel@tonic-gate 			continue;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		nreqs++;
1917c478bd9Sstevel@tonic-gate 		(void) strncpy(reqs[i].cr_event, event, CPC_MAX_EVENT_LEN);
1927c478bd9Sstevel@tonic-gate 		return (1);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	return (0);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate static int
1997c478bd9Sstevel@tonic-gate pic(int tok, char *val)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	int picnum = tok_info[tok].picnum;
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * Make sure the each pic only appears in the spec once.
2047c478bd9Sstevel@tonic-gate 	 */
2057c478bd9Sstevel@tonic-gate 	if (reqs[picnum].cr_event[0] != '\0') {
2067c478bd9Sstevel@tonic-gate 		strtoset_err(gettext("repeated 'pic%d' token\n"), picnum);
2077c478bd9Sstevel@tonic-gate 		return (-1);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (val == NULL || val[0] == '\0') {
2117c478bd9Sstevel@tonic-gate 		strtoset_err(gettext("missing 'pic%d' value\n"), picnum);
2127c478bd9Sstevel@tonic-gate 		return (-1);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (event_valid(picnum, val) == 0) {
2167c478bd9Sstevel@tonic-gate 		strtoset_err(gettext("pic%d cannot measure event '%s' on this "
2177c478bd9Sstevel@tonic-gate 		    "cpu\n"), picnum, val);
2187c478bd9Sstevel@tonic-gate 		return (-1);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	nreqs++;
2227c478bd9Sstevel@tonic-gate 	(void) strncpy(reqs[picnum].cr_event, val, CPC_MAX_EVENT_LEN);
2237c478bd9Sstevel@tonic-gate 	return (0);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * We explicitly ignore any value provided for these tokens, as their
2287c478bd9Sstevel@tonic-gate  * mere presence signals us to turn on or off the relevant flags.
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2317c478bd9Sstevel@tonic-gate static int
2327c478bd9Sstevel@tonic-gate flag(int tok, char *val)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	int i;
2357c478bd9Sstevel@tonic-gate 	int picnum = tok_info[tok].picnum;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * If picnum is -1, this flag should be applied to all reqs.
2397c478bd9Sstevel@tonic-gate 	 */
2407c478bd9Sstevel@tonic-gate 	for (i = (picnum == -1) ? 0 : picnum; i < ncounters; i++) {
2417c478bd9Sstevel@tonic-gate 		if (strcmp(tok_info[tok].name, "nouser") == 0)
2427c478bd9Sstevel@tonic-gate 			reqs[i].cr_flags &= ~CPC_COUNT_USER;
2437c478bd9Sstevel@tonic-gate 		else if (strcmp(tok_info[tok].name, "sys") == 0)
2447c478bd9Sstevel@tonic-gate 			reqs[i].cr_flags |= CPC_COUNT_SYSTEM;
2457c478bd9Sstevel@tonic-gate 		else
2467c478bd9Sstevel@tonic-gate 			return (-1);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		if (picnum != -1)
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	return (0);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate static int
2567c478bd9Sstevel@tonic-gate doattr(int tok, char *val)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	int		i;
2597c478bd9Sstevel@tonic-gate 	int		picnum = tok_info[tok].picnum;
2607c478bd9Sstevel@tonic-gate 	tmp_attr_t	*tmp;
2617c478bd9Sstevel@tonic-gate 	char		*endptr;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * If picnum is -1, this attribute should be applied to all reqs.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	for (i = (picnum == -1) ? 0 : picnum; i < ncounters; i++) {
2677c478bd9Sstevel@tonic-gate 		tmp = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t));
2687c478bd9Sstevel@tonic-gate 		tmp->name = tok_info[tok].name;
2697c478bd9Sstevel@tonic-gate 		if (val != NULL) {
2707c478bd9Sstevel@tonic-gate 			tmp->val = strtoll(val, &endptr, 0);
2717c478bd9Sstevel@tonic-gate 			if (endptr == val) {
2727c478bd9Sstevel@tonic-gate 				strtoset_err(gettext("invalid value '%s' for "
2737c478bd9Sstevel@tonic-gate 				    "attribute '%s'\n"), val, tmp->name);
2747c478bd9Sstevel@tonic-gate 				free(tmp);
2757c478bd9Sstevel@tonic-gate 				return (-1);
2767c478bd9Sstevel@tonic-gate 			}
2777c478bd9Sstevel@tonic-gate 		} else
2787c478bd9Sstevel@tonic-gate 			/*
2797c478bd9Sstevel@tonic-gate 			 * No value was provided for this attribute,
2807c478bd9Sstevel@tonic-gate 			 * so specify a default value of 1.
2817c478bd9Sstevel@tonic-gate 			 */
2827c478bd9Sstevel@tonic-gate 			tmp->val = 1;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		tmp->next = attrs[i];
2857c478bd9Sstevel@tonic-gate 		attrs[i] = tmp;
2867c478bd9Sstevel@tonic-gate 		reqs[i].cr_nattrs++;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		if (picnum != -1)
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	return (0);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2967c478bd9Sstevel@tonic-gate static void
2977c478bd9Sstevel@tonic-gate attr_count_walker(void *arg, const char *attr)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * We don't allow picnum to be specified by the user.
3017c478bd9Sstevel@tonic-gate 	 */
3027c478bd9Sstevel@tonic-gate 	if (strncmp(attr, "picnum", 7) == 0)
3037c478bd9Sstevel@tonic-gate 		return;
3047c478bd9Sstevel@tonic-gate 	(*(int *)arg)++;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate static int
3087c478bd9Sstevel@tonic-gate cpc_count_attrs(cpc_t *cpc)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	int nattrs = 0;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	cpc_walk_attrs(cpc, &nattrs, attr_count_walker);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	return (nattrs);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate static void
3187c478bd9Sstevel@tonic-gate attr_walker(void *arg, const char *attr)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	int *i = arg;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (strncmp(attr, "picnum", 7) == 0)
3237c478bd9Sstevel@tonic-gate 		return;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if ((attrlist[(*i)++] = strdup(attr)) == NULL) {
3267c478bd9Sstevel@tonic-gate 		strtoset_err(gettext("no memory available\n"));
3277c478bd9Sstevel@tonic-gate 		exit(0);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate cpc_set_t *
3327c478bd9Sstevel@tonic-gate cpc_strtoset(cpc_t *cpcin, const char *spec, int smt)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	cpc_set_t		*set;
3357c478bd9Sstevel@tonic-gate 	cpc_attr_t		*req_attrs;
3367c478bd9Sstevel@tonic-gate 	tmp_attr_t		*tmp;
3377c478bd9Sstevel@tonic-gate 	size_t			toklen;
3387c478bd9Sstevel@tonic-gate 	int			i;
3397c478bd9Sstevel@tonic-gate 	int			j;
3407c478bd9Sstevel@tonic-gate 	int			x;
3417c478bd9Sstevel@tonic-gate 	char			*opts;
3427c478bd9Sstevel@tonic-gate 	char			*val;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	cpc = cpcin;
3457c478bd9Sstevel@tonic-gate 	nattrs = 0;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	ncounters = cpc_npic(cpc);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	reqs = (request_t *)emalloc(ncounters * sizeof (request_t));
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	attrs = (tmp_attr_t **)emalloc(ncounters * sizeof (tmp_attr_t *));
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
3547c478bd9Sstevel@tonic-gate 		reqs[i].cr_event[0] = '\0';
3557c478bd9Sstevel@tonic-gate 		reqs[i].cr_flags = CPC_COUNT_USER;
3567c478bd9Sstevel@tonic-gate 		/*
3577c478bd9Sstevel@tonic-gate 		 * Each pic will have at least one attribute: the physical pic
3587c478bd9Sstevel@tonic-gate 		 * assignment via the "picnum" attribute. Set that up here for
3597c478bd9Sstevel@tonic-gate 		 * each request.
3607c478bd9Sstevel@tonic-gate 		 */
3617c478bd9Sstevel@tonic-gate 		reqs[i].cr_nattrs = 1;
3627c478bd9Sstevel@tonic-gate 		attrs[i] = emalloc(sizeof (tmp_attr_t));
3637c478bd9Sstevel@tonic-gate 		attrs[i]->name = "picnum";
3647c478bd9Sstevel@tonic-gate 		attrs[i]->val = i;
3657c478bd9Sstevel@tonic-gate 		attrs[i]->next = NULL;
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * Build up a list of acceptable tokens.
3707c478bd9Sstevel@tonic-gate 	 *
3717c478bd9Sstevel@tonic-gate 	 * Permitted tokens are
3727c478bd9Sstevel@tonic-gate 	 * picn=event
3737c478bd9Sstevel@tonic-gate 	 * nousern
3747c478bd9Sstevel@tonic-gate 	 * sysn
3757c478bd9Sstevel@tonic-gate 	 * attrn=val
3767c478bd9Sstevel@tonic-gate 	 * nouser
3777c478bd9Sstevel@tonic-gate 	 * sys
3787c478bd9Sstevel@tonic-gate 	 * attr=val
3797c478bd9Sstevel@tonic-gate 	 *
3807c478bd9Sstevel@tonic-gate 	 * Where n is a counter number, and attr is any attribute supported by
3817c478bd9Sstevel@tonic-gate 	 * the current processor.
3827c478bd9Sstevel@tonic-gate 	 *
3837c478bd9Sstevel@tonic-gate 	 * If a token appears without a counter number, it applies to all
3847c478bd9Sstevel@tonic-gate 	 * counters in the request set.
3857c478bd9Sstevel@tonic-gate 	 *
3867c478bd9Sstevel@tonic-gate 	 * The number of tokens is:
3877c478bd9Sstevel@tonic-gate 	 *
3887c478bd9Sstevel@tonic-gate 	 * picn: ncounters
3897c478bd9Sstevel@tonic-gate 	 * generic flags: 2 * ncounters (nouser, sys)
3907c478bd9Sstevel@tonic-gate 	 * attrs: nattrs * ncounters
3917c478bd9Sstevel@tonic-gate 	 * attrs with no picnum: nattrs
3927c478bd9Sstevel@tonic-gate 	 * generic flags with no picnum: 2 (nouser, sys)
3937c478bd9Sstevel@tonic-gate 	 * NULL token to signify end of list to getsubopt(3C).
3947c478bd9Sstevel@tonic-gate 	 *
3957c478bd9Sstevel@tonic-gate 	 * Matching each token's index in the token table is a function which
3967c478bd9Sstevel@tonic-gate 	 * process that token; these are in tok_funcs.
3977c478bd9Sstevel@tonic-gate 	 */
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/*
4007c478bd9Sstevel@tonic-gate 	 * Count the number of valid attributes.
4017c478bd9Sstevel@tonic-gate 	 * Set up the attrlist array to point to the attributes in attrlistp.
4027c478bd9Sstevel@tonic-gate 	 */
4037c478bd9Sstevel@tonic-gate 	nattrs = cpc_count_attrs(cpc);
4047c478bd9Sstevel@tonic-gate 	attrlist = (char **)emalloc(nattrs * sizeof (char *));
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	i = 0;
4077c478bd9Sstevel@tonic-gate 	cpc_walk_attrs(cpc, &i, attr_walker);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	ntoks = ncounters + (2 * ncounters) + (nattrs * ncounters) + nattrs + 3;
4107c478bd9Sstevel@tonic-gate 	toks = (char **)emalloc(ntoks * sizeof (char *));
4117c478bd9Sstevel@tonic-gate 	tok_info = (tok_info_t *)emalloc(ntoks * sizeof (tok_info_t));
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	tok_funcs = (int (**)(int, char *))emalloc(ntoks *
4147c478bd9Sstevel@tonic-gate 	    sizeof (int (*)(char *)));
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	for (i = 0; i < ntoks; i++) {
4177c478bd9Sstevel@tonic-gate 		toks[i] = NULL;
4187c478bd9Sstevel@tonic-gate 		tok_funcs[i] = NULL;
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	x = 0;
4227c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
4237c478bd9Sstevel@tonic-gate 		toks[x] = (char *)emalloc(TOK_SIZE);
4247c478bd9Sstevel@tonic-gate 		(void) snprintf(toks[x], TOK_SIZE, "pic%d", i);
4257c478bd9Sstevel@tonic-gate 		tok_info[x].name = "pic";
4267c478bd9Sstevel@tonic-gate 		tok_info[i].picnum = i;
4277c478bd9Sstevel@tonic-gate 		tok_funcs[x] = pic;
4287c478bd9Sstevel@tonic-gate 		x++;
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
4327c478bd9Sstevel@tonic-gate 		toks[x] = (char *)emalloc(TOK_SIZE);
4337c478bd9Sstevel@tonic-gate 		(void) snprintf(toks[x], TOK_SIZE, "nouser%d", i);
4347c478bd9Sstevel@tonic-gate 		tok_info[x].name = "nouser";
4357c478bd9Sstevel@tonic-gate 		tok_info[x].picnum = i;
4367c478bd9Sstevel@tonic-gate 		tok_funcs[x] = flag;
4377c478bd9Sstevel@tonic-gate 		x++;
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
4417c478bd9Sstevel@tonic-gate 		toks[x] = (char *)emalloc(TOK_SIZE);
4427c478bd9Sstevel@tonic-gate 		(void) snprintf(toks[x], TOK_SIZE, "sys%d", i);
4437c478bd9Sstevel@tonic-gate 		tok_info[x].name = "sys";
4447c478bd9Sstevel@tonic-gate 		tok_info[x].picnum = i;
4457c478bd9Sstevel@tonic-gate 		tok_funcs[x] = flag;
4467c478bd9Sstevel@tonic-gate 		x++;
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	for (j = 0; j < nattrs; j++) {
4497c478bd9Sstevel@tonic-gate 		toklen = strlen(attrlist[j]) + 3;
4507c478bd9Sstevel@tonic-gate 		for (i = 0; i < ncounters; i++) {
4517c478bd9Sstevel@tonic-gate 			toks[x] = (char *)emalloc(toklen);
4527c478bd9Sstevel@tonic-gate 			(void) snprintf(toks[x], toklen, "%s%d", attrlist[j],
4537c478bd9Sstevel@tonic-gate 			    i);
4547c478bd9Sstevel@tonic-gate 			tok_info[x].name = attrlist[j];
4557c478bd9Sstevel@tonic-gate 			tok_info[x].picnum = i;
4567c478bd9Sstevel@tonic-gate 			tok_funcs[x] = doattr;
4577c478bd9Sstevel@tonic-gate 			x++;
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		/*
4617c478bd9Sstevel@tonic-gate 		 * Now create a token for this attribute with no picnum; if used
4627c478bd9Sstevel@tonic-gate 		 * it will be applied to all reqs.
4637c478bd9Sstevel@tonic-gate 		 */
4647c478bd9Sstevel@tonic-gate 		toks[x] = (char *)emalloc(toklen);
4657c478bd9Sstevel@tonic-gate 		(void) snprintf(toks[x], toklen, "%s", attrlist[j]);
4667c478bd9Sstevel@tonic-gate 		tok_info[x].name = attrlist[j];
4677c478bd9Sstevel@tonic-gate 		tok_info[x].picnum = -1;
4687c478bd9Sstevel@tonic-gate 		tok_funcs[x] = doattr;
4697c478bd9Sstevel@tonic-gate 		x++;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	toks[x] = "nouser";
4737c478bd9Sstevel@tonic-gate 	tok_info[x].name = "nouser";
4747c478bd9Sstevel@tonic-gate 	tok_info[x].picnum = -1;
4757c478bd9Sstevel@tonic-gate 	tok_funcs[x] = flag;
4767c478bd9Sstevel@tonic-gate 	x++;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	toks[x] = "sys";
4797c478bd9Sstevel@tonic-gate 	tok_info[x].name = "sys";
4807c478bd9Sstevel@tonic-gate 	tok_info[x].picnum = -1;
4817c478bd9Sstevel@tonic-gate 	tok_funcs[x] = flag;
4827c478bd9Sstevel@tonic-gate 	x++;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	toks[x] = NULL;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	opts = strcpy(alloca(strlen(spec) + 1), spec);
4877c478bd9Sstevel@tonic-gate 	while (*opts != '\0') {
4887c478bd9Sstevel@tonic-gate 		int idx = getsubopt(&opts, toks, &val);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 		if (idx == -1) {
4917c478bd9Sstevel@tonic-gate 			if (find_event(val) == 0) {
4927c478bd9Sstevel@tonic-gate 				strtoset_err(gettext("bad token '%s'\n"), val);
4937c478bd9Sstevel@tonic-gate 				goto inval;
4947c478bd9Sstevel@tonic-gate 			} else
4957c478bd9Sstevel@tonic-gate 				continue;
4967c478bd9Sstevel@tonic-gate 		}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		if (tok_funcs[idx](idx, val) == -1)
4997c478bd9Sstevel@tonic-gate 			goto inval;
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	/*
5037c478bd9Sstevel@tonic-gate 	 * The string has been processed. Now count how many PICs were used,
5047c478bd9Sstevel@tonic-gate 	 * create a request set, and specify each request properly.
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if ((set = cpc_set_create(cpc)) == NULL) {
5087c478bd9Sstevel@tonic-gate 		strtoset_err(gettext("no memory available\n"));
5097c478bd9Sstevel@tonic-gate 		exit(0);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
5137c478bd9Sstevel@tonic-gate 		if (reqs[i].cr_event[0] == '\0')
5147c478bd9Sstevel@tonic-gate 			continue;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		/*
5177c478bd9Sstevel@tonic-gate 		 * If the caller wishes to measure events on the physical CPU,
5187c478bd9Sstevel@tonic-gate 		 * we need to add SMT attributes to each request.
5197c478bd9Sstevel@tonic-gate 		 */
5207c478bd9Sstevel@tonic-gate 		if (smt)
5217c478bd9Sstevel@tonic-gate 			smt_special(i);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		req_attrs = (cpc_attr_t *)emalloc(reqs[i].cr_nattrs *
5247c478bd9Sstevel@tonic-gate 		    sizeof (cpc_attr_t));
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 		j = 0;
5277c478bd9Sstevel@tonic-gate 		for (tmp = attrs[i]; tmp != NULL; tmp = tmp->next) {
5287c478bd9Sstevel@tonic-gate 			req_attrs[j].ca_name = tmp->name;
5297c478bd9Sstevel@tonic-gate 			req_attrs[j].ca_val = tmp->val;
5307c478bd9Sstevel@tonic-gate 			j++;
5317c478bd9Sstevel@tonic-gate 		}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		if (cpc_set_add_request(cpc, set, reqs[i].cr_event, 0,
5347c478bd9Sstevel@tonic-gate 		    reqs[i].cr_flags, reqs[i].cr_nattrs, req_attrs) == -1) {
5357c478bd9Sstevel@tonic-gate 			free(req_attrs);
5367c478bd9Sstevel@tonic-gate 			(void) cpc_set_destroy(cpc, set);
5377c478bd9Sstevel@tonic-gate 			strtoset_err(
5387c478bd9Sstevel@tonic-gate 			    gettext("cpc_set_add_request() failed: %s\n"),
5397c478bd9Sstevel@tonic-gate 			    strerror(errno));
5407c478bd9Sstevel@tonic-gate 			goto inval;
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 		free(req_attrs);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	strtoset_cleanup();
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	return (set);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate inval:
5517c478bd9Sstevel@tonic-gate 	strtoset_cleanup();
5527c478bd9Sstevel@tonic-gate 	errno = EINVAL;
5537c478bd9Sstevel@tonic-gate 	return (NULL);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate static void
5577c478bd9Sstevel@tonic-gate strtoset_cleanup(void)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	int		i;
5607c478bd9Sstevel@tonic-gate 	tmp_attr_t	*tmp, *p;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	for (i = 0; i < nattrs; i++)
5637c478bd9Sstevel@tonic-gate 		free(attrlist[i]);
5647c478bd9Sstevel@tonic-gate 	free(attrlist);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncounters; i++) {
5677c478bd9Sstevel@tonic-gate 		for (tmp = attrs[i]; tmp != NULL; tmp = p) {
5687c478bd9Sstevel@tonic-gate 			p = tmp->next;
5697c478bd9Sstevel@tonic-gate 			free(tmp);
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 	free(attrs);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	for (i = 0; i < ntoks - 3; i++)
5757c478bd9Sstevel@tonic-gate 		/*
5767c478bd9Sstevel@tonic-gate 		 * We free all but the last three tokens: "nouser", "sys", NULL
5777c478bd9Sstevel@tonic-gate 		 */
5787c478bd9Sstevel@tonic-gate 		free(toks[i]);
5797c478bd9Sstevel@tonic-gate 	free(toks);
5807c478bd9Sstevel@tonic-gate 	free(reqs);
5817c478bd9Sstevel@tonic-gate 	free(tok_info);
5827c478bd9Sstevel@tonic-gate 	free(tok_funcs);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate  * The following is called to modify requests so that they count events on
5877c478bd9Sstevel@tonic-gate  * behalf of a physical processor, instead of a logical processor. It duplicates
5887c478bd9Sstevel@tonic-gate  * the request flags for the sibling processor (i.e. if the request counts user
5897c478bd9Sstevel@tonic-gate  * events, add an attribute to count user events on the sibling processor also).
5907c478bd9Sstevel@tonic-gate  */
5917c478bd9Sstevel@tonic-gate static void
5927c478bd9Sstevel@tonic-gate smt_special(int picnum)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	tmp_attr_t *attr;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (reqs[picnum].cr_flags & CPC_COUNT_USER) {
5977c478bd9Sstevel@tonic-gate 		attr = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t));
5987c478bd9Sstevel@tonic-gate 		attr->name = "count_sibling_usr";
5997c478bd9Sstevel@tonic-gate 		attr->val = 1;
6007c478bd9Sstevel@tonic-gate 		attr->next = attrs[picnum];
6017c478bd9Sstevel@tonic-gate 		attrs[picnum] = attr;
6027c478bd9Sstevel@tonic-gate 		reqs[picnum].cr_nattrs++;
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	if (reqs[picnum].cr_flags & CPC_COUNT_SYSTEM) {
6067c478bd9Sstevel@tonic-gate 		attr = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t));
6077c478bd9Sstevel@tonic-gate 		attr->name = "count_sibling_sys";
6087c478bd9Sstevel@tonic-gate 		attr->val = 1;
6097c478bd9Sstevel@tonic-gate 		attr->next = attrs[picnum];
6107c478bd9Sstevel@tonic-gate 		attrs[picnum] = attr;
6117c478bd9Sstevel@tonic-gate 		reqs[picnum].cr_nattrs++;
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate  * If we ever fail to get memory, we print an error message and exit.
6177c478bd9Sstevel@tonic-gate  */
6187c478bd9Sstevel@tonic-gate static void *
6197c478bd9Sstevel@tonic-gate emalloc(size_t n)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	void *p = malloc(n);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (p == NULL) {
6247c478bd9Sstevel@tonic-gate 		strtoset_err(gettext("no memory available\n"));
6257c478bd9Sstevel@tonic-gate 		exit(0);
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	return (p);
6297c478bd9Sstevel@tonic-gate }
630