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 52791Srab * Common Development and Distribution License (the "License"). 62791Srab * 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*7120Svk226950 * Copyright 2008 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 { 119*7120Svk226950 char *end_event; 1200Sstevel@tonic-gate found = 0; 1212791Srab 122*7120Svk226950 1230Sstevel@tonic-gate cpc_walk_events_pic(cpc, picnum, event, event_walker); 1242791Srab 1252791Srab if (found) 1262791Srab return (1); 1272791Srab 1282791Srab /* 1292791Srab * Before assuming this is an invalid event, see if we have been given 1302791Srab * a raw event code. An event code of '0' is not recognized, as it 1312791Srab * already has a corresponding event name in existing backends and it 1322791Srab * is the only reasonable way to know if strtol() succeeded. 133*7120Svk226950 * Check the second argument of strtol() to ensure invalid events 134*7120Svk226950 * beginning with number do not go through. 1352791Srab */ 136*7120Svk226950 if ((strtol(event, &end_event, 0) != 0) && (*end_event == '\0')) 1372791Srab /* 1382791Srab * Success - this is a valid raw code in hex, decimal, or octal. 1392791Srab */ 1402791Srab return (1); 1412791Srab 1422791Srab return (0); 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate /* 1460Sstevel@tonic-gate * An unknown token was encountered; check here if it is an implicit event 1470Sstevel@tonic-gate * name. We allow users to omit the "picn=" portion of the event spec, and 1480Sstevel@tonic-gate * assign such events to available pics in order they are returned from 1490Sstevel@tonic-gate * getsubopt(3C). We start our search for an available pic _after_ the highest 1500Sstevel@tonic-gate * picnum to be assigned. This ensures that the event spec can never be out of 1510Sstevel@tonic-gate * order; i.e. if the event string is "eventa,eventb" we must ensure that the 1520Sstevel@tonic-gate * picnum counting eventa is less than the picnum counting eventb. 1530Sstevel@tonic-gate */ 1540Sstevel@tonic-gate static int 1550Sstevel@tonic-gate find_event(char *event) 1560Sstevel@tonic-gate { 1570Sstevel@tonic-gate int i; 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate /* 1602791Srab * Event names cannot have '=' in them. If present here, it means we 1612791Srab * have encountered an unknown token (foo=bar, for example). 1622791Srab */ 1632791Srab if (strchr(event, '=') != NULL) 1642791Srab return (0); 1652791Srab 1662791Srab /* 1670Sstevel@tonic-gate * Find the first unavailable pic, after which we must start our search. 1680Sstevel@tonic-gate */ 1690Sstevel@tonic-gate for (i = ncounters - 1; i >= 0; i--) { 1700Sstevel@tonic-gate if (reqs[i].cr_event[0] != '\0') 1710Sstevel@tonic-gate break; 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * If the last counter has been assigned, we cannot place this event. 1750Sstevel@tonic-gate */ 1760Sstevel@tonic-gate if (i == ncounters - 1) 1770Sstevel@tonic-gate return (0); 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* 1800Sstevel@tonic-gate * If none of the counters have been assigned yet, i is -1 and we will 1810Sstevel@tonic-gate * begin our search at 0. Else we begin our search at the counter after 1820Sstevel@tonic-gate * the last one currently assigned. 1830Sstevel@tonic-gate */ 1840Sstevel@tonic-gate i++; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate for (; i < ncounters; i++) { 1870Sstevel@tonic-gate if (event_valid(i, event) == 0) 1880Sstevel@tonic-gate continue; 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate nreqs++; 1910Sstevel@tonic-gate (void) strncpy(reqs[i].cr_event, event, CPC_MAX_EVENT_LEN); 1920Sstevel@tonic-gate return (1); 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate return (0); 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate static int 1990Sstevel@tonic-gate pic(int tok, char *val) 2000Sstevel@tonic-gate { 2010Sstevel@tonic-gate int picnum = tok_info[tok].picnum; 2020Sstevel@tonic-gate /* 2030Sstevel@tonic-gate * Make sure the each pic only appears in the spec once. 2040Sstevel@tonic-gate */ 2050Sstevel@tonic-gate if (reqs[picnum].cr_event[0] != '\0') { 2060Sstevel@tonic-gate strtoset_err(gettext("repeated 'pic%d' token\n"), picnum); 2070Sstevel@tonic-gate return (-1); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate if (val == NULL || val[0] == '\0') { 2110Sstevel@tonic-gate strtoset_err(gettext("missing 'pic%d' value\n"), picnum); 2120Sstevel@tonic-gate return (-1); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate if (event_valid(picnum, val) == 0) { 2160Sstevel@tonic-gate strtoset_err(gettext("pic%d cannot measure event '%s' on this " 2170Sstevel@tonic-gate "cpu\n"), picnum, val); 2180Sstevel@tonic-gate return (-1); 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate nreqs++; 2220Sstevel@tonic-gate (void) strncpy(reqs[picnum].cr_event, val, CPC_MAX_EVENT_LEN); 2230Sstevel@tonic-gate return (0); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* 2270Sstevel@tonic-gate * We explicitly ignore any value provided for these tokens, as their 2280Sstevel@tonic-gate * mere presence signals us to turn on or off the relevant flags. 2290Sstevel@tonic-gate */ 2300Sstevel@tonic-gate /*ARGSUSED*/ 2310Sstevel@tonic-gate static int 2320Sstevel@tonic-gate flag(int tok, char *val) 2330Sstevel@tonic-gate { 2340Sstevel@tonic-gate int i; 2350Sstevel@tonic-gate int picnum = tok_info[tok].picnum; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate /* 2380Sstevel@tonic-gate * If picnum is -1, this flag should be applied to all reqs. 2390Sstevel@tonic-gate */ 2400Sstevel@tonic-gate for (i = (picnum == -1) ? 0 : picnum; i < ncounters; i++) { 2410Sstevel@tonic-gate if (strcmp(tok_info[tok].name, "nouser") == 0) 2420Sstevel@tonic-gate reqs[i].cr_flags &= ~CPC_COUNT_USER; 2430Sstevel@tonic-gate else if (strcmp(tok_info[tok].name, "sys") == 0) 2440Sstevel@tonic-gate reqs[i].cr_flags |= CPC_COUNT_SYSTEM; 2450Sstevel@tonic-gate else 2460Sstevel@tonic-gate return (-1); 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate if (picnum != -1) 2490Sstevel@tonic-gate break; 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate return (0); 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate static int 2560Sstevel@tonic-gate doattr(int tok, char *val) 2570Sstevel@tonic-gate { 2580Sstevel@tonic-gate int i; 2590Sstevel@tonic-gate int picnum = tok_info[tok].picnum; 2600Sstevel@tonic-gate tmp_attr_t *tmp; 2610Sstevel@tonic-gate char *endptr; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * If picnum is -1, this attribute should be applied to all reqs. 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate for (i = (picnum == -1) ? 0 : picnum; i < ncounters; i++) { 2670Sstevel@tonic-gate tmp = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t)); 2680Sstevel@tonic-gate tmp->name = tok_info[tok].name; 2690Sstevel@tonic-gate if (val != NULL) { 2700Sstevel@tonic-gate tmp->val = strtoll(val, &endptr, 0); 2710Sstevel@tonic-gate if (endptr == val) { 2720Sstevel@tonic-gate strtoset_err(gettext("invalid value '%s' for " 2730Sstevel@tonic-gate "attribute '%s'\n"), val, tmp->name); 2740Sstevel@tonic-gate free(tmp); 2750Sstevel@tonic-gate return (-1); 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate } else 2780Sstevel@tonic-gate /* 2790Sstevel@tonic-gate * No value was provided for this attribute, 2800Sstevel@tonic-gate * so specify a default value of 1. 2810Sstevel@tonic-gate */ 2820Sstevel@tonic-gate tmp->val = 1; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate tmp->next = attrs[i]; 2850Sstevel@tonic-gate attrs[i] = tmp; 2860Sstevel@tonic-gate reqs[i].cr_nattrs++; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate if (picnum != -1) 2890Sstevel@tonic-gate break; 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate return (0); 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate /*ARGSUSED*/ 2960Sstevel@tonic-gate static void 2970Sstevel@tonic-gate attr_count_walker(void *arg, const char *attr) 2980Sstevel@tonic-gate { 2990Sstevel@tonic-gate /* 3000Sstevel@tonic-gate * We don't allow picnum to be specified by the user. 3010Sstevel@tonic-gate */ 3020Sstevel@tonic-gate if (strncmp(attr, "picnum", 7) == 0) 3030Sstevel@tonic-gate return; 3040Sstevel@tonic-gate (*(int *)arg)++; 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate static int 3080Sstevel@tonic-gate cpc_count_attrs(cpc_t *cpc) 3090Sstevel@tonic-gate { 3100Sstevel@tonic-gate int nattrs = 0; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate cpc_walk_attrs(cpc, &nattrs, attr_count_walker); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate return (nattrs); 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate static void 3180Sstevel@tonic-gate attr_walker(void *arg, const char *attr) 3190Sstevel@tonic-gate { 3200Sstevel@tonic-gate int *i = arg; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate if (strncmp(attr, "picnum", 7) == 0) 3230Sstevel@tonic-gate return; 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate if ((attrlist[(*i)++] = strdup(attr)) == NULL) { 3260Sstevel@tonic-gate strtoset_err(gettext("no memory available\n")); 3270Sstevel@tonic-gate exit(0); 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate cpc_set_t * 3320Sstevel@tonic-gate cpc_strtoset(cpc_t *cpcin, const char *spec, int smt) 3330Sstevel@tonic-gate { 3340Sstevel@tonic-gate cpc_set_t *set; 3350Sstevel@tonic-gate cpc_attr_t *req_attrs; 3360Sstevel@tonic-gate tmp_attr_t *tmp; 3370Sstevel@tonic-gate size_t toklen; 3380Sstevel@tonic-gate int i; 3390Sstevel@tonic-gate int j; 3400Sstevel@tonic-gate int x; 3410Sstevel@tonic-gate char *opts; 3420Sstevel@tonic-gate char *val; 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate cpc = cpcin; 3450Sstevel@tonic-gate nattrs = 0; 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate ncounters = cpc_npic(cpc); 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate reqs = (request_t *)emalloc(ncounters * sizeof (request_t)); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate attrs = (tmp_attr_t **)emalloc(ncounters * sizeof (tmp_attr_t *)); 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 3540Sstevel@tonic-gate reqs[i].cr_event[0] = '\0'; 3550Sstevel@tonic-gate reqs[i].cr_flags = CPC_COUNT_USER; 3560Sstevel@tonic-gate /* 3570Sstevel@tonic-gate * Each pic will have at least one attribute: the physical pic 3580Sstevel@tonic-gate * assignment via the "picnum" attribute. Set that up here for 3590Sstevel@tonic-gate * each request. 3600Sstevel@tonic-gate */ 3610Sstevel@tonic-gate reqs[i].cr_nattrs = 1; 3620Sstevel@tonic-gate attrs[i] = emalloc(sizeof (tmp_attr_t)); 3630Sstevel@tonic-gate attrs[i]->name = "picnum"; 3640Sstevel@tonic-gate attrs[i]->val = i; 3650Sstevel@tonic-gate attrs[i]->next = NULL; 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate /* 3690Sstevel@tonic-gate * Build up a list of acceptable tokens. 3700Sstevel@tonic-gate * 3710Sstevel@tonic-gate * Permitted tokens are 3720Sstevel@tonic-gate * picn=event 3730Sstevel@tonic-gate * nousern 3740Sstevel@tonic-gate * sysn 3750Sstevel@tonic-gate * attrn=val 3760Sstevel@tonic-gate * nouser 3770Sstevel@tonic-gate * sys 3780Sstevel@tonic-gate * attr=val 3790Sstevel@tonic-gate * 3800Sstevel@tonic-gate * Where n is a counter number, and attr is any attribute supported by 3810Sstevel@tonic-gate * the current processor. 3820Sstevel@tonic-gate * 3830Sstevel@tonic-gate * If a token appears without a counter number, it applies to all 3840Sstevel@tonic-gate * counters in the request set. 3850Sstevel@tonic-gate * 3860Sstevel@tonic-gate * The number of tokens is: 3870Sstevel@tonic-gate * 3880Sstevel@tonic-gate * picn: ncounters 3890Sstevel@tonic-gate * generic flags: 2 * ncounters (nouser, sys) 3900Sstevel@tonic-gate * attrs: nattrs * ncounters 3910Sstevel@tonic-gate * attrs with no picnum: nattrs 3920Sstevel@tonic-gate * generic flags with no picnum: 2 (nouser, sys) 3930Sstevel@tonic-gate * NULL token to signify end of list to getsubopt(3C). 3940Sstevel@tonic-gate * 3950Sstevel@tonic-gate * Matching each token's index in the token table is a function which 3960Sstevel@tonic-gate * process that token; these are in tok_funcs. 3970Sstevel@tonic-gate */ 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate /* 4000Sstevel@tonic-gate * Count the number of valid attributes. 4010Sstevel@tonic-gate * Set up the attrlist array to point to the attributes in attrlistp. 4020Sstevel@tonic-gate */ 4030Sstevel@tonic-gate nattrs = cpc_count_attrs(cpc); 4040Sstevel@tonic-gate attrlist = (char **)emalloc(nattrs * sizeof (char *)); 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate i = 0; 4070Sstevel@tonic-gate cpc_walk_attrs(cpc, &i, attr_walker); 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate ntoks = ncounters + (2 * ncounters) + (nattrs * ncounters) + nattrs + 3; 4100Sstevel@tonic-gate toks = (char **)emalloc(ntoks * sizeof (char *)); 4110Sstevel@tonic-gate tok_info = (tok_info_t *)emalloc(ntoks * sizeof (tok_info_t)); 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate tok_funcs = (int (**)(int, char *))emalloc(ntoks * 4140Sstevel@tonic-gate sizeof (int (*)(char *))); 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate for (i = 0; i < ntoks; i++) { 4170Sstevel@tonic-gate toks[i] = NULL; 4180Sstevel@tonic-gate tok_funcs[i] = NULL; 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate x = 0; 4220Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 4230Sstevel@tonic-gate toks[x] = (char *)emalloc(TOK_SIZE); 4240Sstevel@tonic-gate (void) snprintf(toks[x], TOK_SIZE, "pic%d", i); 4250Sstevel@tonic-gate tok_info[x].name = "pic"; 4260Sstevel@tonic-gate tok_info[i].picnum = i; 4270Sstevel@tonic-gate tok_funcs[x] = pic; 4280Sstevel@tonic-gate x++; 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 4320Sstevel@tonic-gate toks[x] = (char *)emalloc(TOK_SIZE); 4330Sstevel@tonic-gate (void) snprintf(toks[x], TOK_SIZE, "nouser%d", i); 4340Sstevel@tonic-gate tok_info[x].name = "nouser"; 4350Sstevel@tonic-gate tok_info[x].picnum = i; 4360Sstevel@tonic-gate tok_funcs[x] = flag; 4370Sstevel@tonic-gate x++; 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 4410Sstevel@tonic-gate toks[x] = (char *)emalloc(TOK_SIZE); 4420Sstevel@tonic-gate (void) snprintf(toks[x], TOK_SIZE, "sys%d", i); 4430Sstevel@tonic-gate tok_info[x].name = "sys"; 4440Sstevel@tonic-gate tok_info[x].picnum = i; 4450Sstevel@tonic-gate tok_funcs[x] = flag; 4460Sstevel@tonic-gate x++; 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate for (j = 0; j < nattrs; j++) { 4490Sstevel@tonic-gate toklen = strlen(attrlist[j]) + 3; 4500Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 4510Sstevel@tonic-gate toks[x] = (char *)emalloc(toklen); 4520Sstevel@tonic-gate (void) snprintf(toks[x], toklen, "%s%d", attrlist[j], 4530Sstevel@tonic-gate i); 4540Sstevel@tonic-gate tok_info[x].name = attrlist[j]; 4550Sstevel@tonic-gate tok_info[x].picnum = i; 4560Sstevel@tonic-gate tok_funcs[x] = doattr; 4570Sstevel@tonic-gate x++; 4580Sstevel@tonic-gate } 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate /* 4610Sstevel@tonic-gate * Now create a token for this attribute with no picnum; if used 4620Sstevel@tonic-gate * it will be applied to all reqs. 4630Sstevel@tonic-gate */ 4640Sstevel@tonic-gate toks[x] = (char *)emalloc(toklen); 4650Sstevel@tonic-gate (void) snprintf(toks[x], toklen, "%s", attrlist[j]); 4660Sstevel@tonic-gate tok_info[x].name = attrlist[j]; 4670Sstevel@tonic-gate tok_info[x].picnum = -1; 4680Sstevel@tonic-gate tok_funcs[x] = doattr; 4690Sstevel@tonic-gate x++; 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate toks[x] = "nouser"; 4730Sstevel@tonic-gate tok_info[x].name = "nouser"; 4740Sstevel@tonic-gate tok_info[x].picnum = -1; 4750Sstevel@tonic-gate tok_funcs[x] = flag; 4760Sstevel@tonic-gate x++; 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate toks[x] = "sys"; 4790Sstevel@tonic-gate tok_info[x].name = "sys"; 4800Sstevel@tonic-gate tok_info[x].picnum = -1; 4810Sstevel@tonic-gate tok_funcs[x] = flag; 4820Sstevel@tonic-gate x++; 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate toks[x] = NULL; 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate opts = strcpy(alloca(strlen(spec) + 1), spec); 4870Sstevel@tonic-gate while (*opts != '\0') { 4880Sstevel@tonic-gate int idx = getsubopt(&opts, toks, &val); 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate if (idx == -1) { 4910Sstevel@tonic-gate if (find_event(val) == 0) { 4920Sstevel@tonic-gate strtoset_err(gettext("bad token '%s'\n"), val); 4930Sstevel@tonic-gate goto inval; 4940Sstevel@tonic-gate } else 4950Sstevel@tonic-gate continue; 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate if (tok_funcs[idx](idx, val) == -1) 4990Sstevel@tonic-gate goto inval; 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate /* 5030Sstevel@tonic-gate * The string has been processed. Now count how many PICs were used, 5040Sstevel@tonic-gate * create a request set, and specify each request properly. 5050Sstevel@tonic-gate */ 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate if ((set = cpc_set_create(cpc)) == NULL) { 5080Sstevel@tonic-gate strtoset_err(gettext("no memory available\n")); 5090Sstevel@tonic-gate exit(0); 5100Sstevel@tonic-gate } 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 5130Sstevel@tonic-gate if (reqs[i].cr_event[0] == '\0') 5140Sstevel@tonic-gate continue; 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate /* 5170Sstevel@tonic-gate * If the caller wishes to measure events on the physical CPU, 5180Sstevel@tonic-gate * we need to add SMT attributes to each request. 5190Sstevel@tonic-gate */ 5200Sstevel@tonic-gate if (smt) 5210Sstevel@tonic-gate smt_special(i); 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate req_attrs = (cpc_attr_t *)emalloc(reqs[i].cr_nattrs * 5240Sstevel@tonic-gate sizeof (cpc_attr_t)); 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate j = 0; 5270Sstevel@tonic-gate for (tmp = attrs[i]; tmp != NULL; tmp = tmp->next) { 5280Sstevel@tonic-gate req_attrs[j].ca_name = tmp->name; 5290Sstevel@tonic-gate req_attrs[j].ca_val = tmp->val; 5300Sstevel@tonic-gate j++; 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate if (cpc_set_add_request(cpc, set, reqs[i].cr_event, 0, 5340Sstevel@tonic-gate reqs[i].cr_flags, reqs[i].cr_nattrs, req_attrs) == -1) { 5350Sstevel@tonic-gate free(req_attrs); 5360Sstevel@tonic-gate (void) cpc_set_destroy(cpc, set); 5370Sstevel@tonic-gate strtoset_err( 538*7120Svk226950 gettext("cpc_set_add_request() failed: %s\n"), 539*7120Svk226950 strerror(errno)); 5400Sstevel@tonic-gate goto inval; 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate free(req_attrs); 5440Sstevel@tonic-gate } 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate strtoset_cleanup(); 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate return (set); 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate inval: 5510Sstevel@tonic-gate strtoset_cleanup(); 5520Sstevel@tonic-gate errno = EINVAL; 5530Sstevel@tonic-gate return (NULL); 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate static void 5570Sstevel@tonic-gate strtoset_cleanup(void) 5580Sstevel@tonic-gate { 5590Sstevel@tonic-gate int i; 5600Sstevel@tonic-gate tmp_attr_t *tmp, *p; 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate for (i = 0; i < nattrs; i++) 5630Sstevel@tonic-gate free(attrlist[i]); 5640Sstevel@tonic-gate free(attrlist); 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 5670Sstevel@tonic-gate for (tmp = attrs[i]; tmp != NULL; tmp = p) { 5680Sstevel@tonic-gate p = tmp->next; 5690Sstevel@tonic-gate free(tmp); 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate free(attrs); 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate for (i = 0; i < ntoks - 3; i++) 5750Sstevel@tonic-gate /* 5760Sstevel@tonic-gate * We free all but the last three tokens: "nouser", "sys", NULL 5770Sstevel@tonic-gate */ 5780Sstevel@tonic-gate free(toks[i]); 5790Sstevel@tonic-gate free(toks); 5800Sstevel@tonic-gate free(reqs); 5810Sstevel@tonic-gate free(tok_info); 5820Sstevel@tonic-gate free(tok_funcs); 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate /* 5860Sstevel@tonic-gate * The following is called to modify requests so that they count events on 5870Sstevel@tonic-gate * behalf of a physical processor, instead of a logical processor. It duplicates 5880Sstevel@tonic-gate * the request flags for the sibling processor (i.e. if the request counts user 5890Sstevel@tonic-gate * events, add an attribute to count user events on the sibling processor also). 5900Sstevel@tonic-gate */ 5910Sstevel@tonic-gate static void 5920Sstevel@tonic-gate smt_special(int picnum) 5930Sstevel@tonic-gate { 5940Sstevel@tonic-gate tmp_attr_t *attr; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate if (reqs[picnum].cr_flags & CPC_COUNT_USER) { 5970Sstevel@tonic-gate attr = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t)); 5980Sstevel@tonic-gate attr->name = "count_sibling_usr"; 5990Sstevel@tonic-gate attr->val = 1; 6000Sstevel@tonic-gate attr->next = attrs[picnum]; 6010Sstevel@tonic-gate attrs[picnum] = attr; 6020Sstevel@tonic-gate reqs[picnum].cr_nattrs++; 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate if (reqs[picnum].cr_flags & CPC_COUNT_SYSTEM) { 6060Sstevel@tonic-gate attr = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t)); 6070Sstevel@tonic-gate attr->name = "count_sibling_sys"; 6080Sstevel@tonic-gate attr->val = 1; 6090Sstevel@tonic-gate attr->next = attrs[picnum]; 6100Sstevel@tonic-gate attrs[picnum] = attr; 6110Sstevel@tonic-gate reqs[picnum].cr_nattrs++; 6120Sstevel@tonic-gate } 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate /* 6160Sstevel@tonic-gate * If we ever fail to get memory, we print an error message and exit. 6170Sstevel@tonic-gate */ 6180Sstevel@tonic-gate static void * 6190Sstevel@tonic-gate emalloc(size_t n) 6200Sstevel@tonic-gate { 6210Sstevel@tonic-gate void *p = malloc(n); 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate if (p == NULL) { 6240Sstevel@tonic-gate strtoset_err(gettext("no memory available\n")); 6250Sstevel@tonic-gate exit(0); 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate return (p); 6290Sstevel@tonic-gate } 630