1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <stdio.h> 30*0Sstevel@tonic-gate #include <stdlib.h> 31*0Sstevel@tonic-gate #include <alloca.h> 32*0Sstevel@tonic-gate #include <errno.h> 33*0Sstevel@tonic-gate #include <libintl.h> 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include "libcpc.h" 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate /* 38*0Sstevel@tonic-gate * Takes a string and converts it to a cpc_set_t. 39*0Sstevel@tonic-gate * 40*0Sstevel@tonic-gate * While processing the string using getsubopt(), we will use an array of 41*0Sstevel@tonic-gate * requests to hold the data, and a proprietary representation of attributes 42*0Sstevel@tonic-gate * which allow us to avoid a realloc()/bcopy() dance every time we come across 43*0Sstevel@tonic-gate * a new attribute. 44*0Sstevel@tonic-gate * 45*0Sstevel@tonic-gate * Not until after the string has been processed in its entirety do we 46*0Sstevel@tonic-gate * allocate and specify a request set properly. 47*0Sstevel@tonic-gate */ 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate /* 50*0Sstevel@tonic-gate * Leave enough room in token strings for picn, nousern, or sysn where n is 51*0Sstevel@tonic-gate * picnum. 52*0Sstevel@tonic-gate */ 53*0Sstevel@tonic-gate #define TOK_SIZE 10 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate typedef struct __tmp_attr { 56*0Sstevel@tonic-gate char *name; 57*0Sstevel@tonic-gate uint64_t val; 58*0Sstevel@tonic-gate struct __tmp_attr *next; 59*0Sstevel@tonic-gate } tmp_attr_t; 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate typedef struct __tok_info { 62*0Sstevel@tonic-gate char *name; 63*0Sstevel@tonic-gate int picnum; 64*0Sstevel@tonic-gate } tok_info_t; 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate typedef struct __request_t { 67*0Sstevel@tonic-gate char cr_event[CPC_MAX_EVENT_LEN]; 68*0Sstevel@tonic-gate uint_t cr_flags; 69*0Sstevel@tonic-gate uint_t cr_nattrs; /* # CPU-specific attrs */ 70*0Sstevel@tonic-gate } request_t; 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate static void strtoset_cleanup(void); 73*0Sstevel@tonic-gate static void smt_special(int picnum); 74*0Sstevel@tonic-gate static void *emalloc(size_t n); 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate /* 77*0Sstevel@tonic-gate * Clients of cpc_strtoset may set this to specify an error handler during 78*0Sstevel@tonic-gate * string parsing. 79*0Sstevel@tonic-gate */ 80*0Sstevel@tonic-gate cpc_errhndlr_t *strtoset_errfn = NULL; 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate static request_t *reqs; 83*0Sstevel@tonic-gate static int nreqs; 84*0Sstevel@tonic-gate static int ncounters; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate static tmp_attr_t **attrs; 87*0Sstevel@tonic-gate static int ntoks; 88*0Sstevel@tonic-gate static char **toks; 89*0Sstevel@tonic-gate static tok_info_t *tok_info; 90*0Sstevel@tonic-gate static int (*(*tok_funcs))(int, char *); 91*0Sstevel@tonic-gate static char **attrlist; /* array of ptrs to toks in attrlistp */ 92*0Sstevel@tonic-gate static int nattrs; 93*0Sstevel@tonic-gate static cpc_t *cpc; 94*0Sstevel@tonic-gate static int found; 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate static void 97*0Sstevel@tonic-gate strtoset_err(const char *fmt, ...) 98*0Sstevel@tonic-gate { 99*0Sstevel@tonic-gate va_list ap; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate if (strtoset_errfn == NULL) 102*0Sstevel@tonic-gate return; 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate va_start(ap, fmt); 105*0Sstevel@tonic-gate (*strtoset_errfn)("cpc_strtoset", -1, fmt, ap); 106*0Sstevel@tonic-gate va_end(ap); 107*0Sstevel@tonic-gate } 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate /*ARGSUSED*/ 110*0Sstevel@tonic-gate static void 111*0Sstevel@tonic-gate event_walker(void *arg, uint_t picno, const char *event) 112*0Sstevel@tonic-gate { 113*0Sstevel@tonic-gate if (strncmp(arg, event, CPC_MAX_EVENT_LEN) == 0) 114*0Sstevel@tonic-gate found = 1; 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate static int 118*0Sstevel@tonic-gate event_valid(int picnum, char *event) 119*0Sstevel@tonic-gate { 120*0Sstevel@tonic-gate found = 0; 121*0Sstevel@tonic-gate cpc_walk_events_pic(cpc, picnum, event, event_walker); 122*0Sstevel@tonic-gate return (found); 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate /* 126*0Sstevel@tonic-gate * An unknown token was encountered; check here if it is an implicit event 127*0Sstevel@tonic-gate * name. We allow users to omit the "picn=" portion of the event spec, and 128*0Sstevel@tonic-gate * assign such events to available pics in order they are returned from 129*0Sstevel@tonic-gate * getsubopt(3C). We start our search for an available pic _after_ the highest 130*0Sstevel@tonic-gate * picnum to be assigned. This ensures that the event spec can never be out of 131*0Sstevel@tonic-gate * order; i.e. if the event string is "eventa,eventb" we must ensure that the 132*0Sstevel@tonic-gate * picnum counting eventa is less than the picnum counting eventb. 133*0Sstevel@tonic-gate */ 134*0Sstevel@tonic-gate static int 135*0Sstevel@tonic-gate find_event(char *event) 136*0Sstevel@tonic-gate { 137*0Sstevel@tonic-gate int i; 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate /* 140*0Sstevel@tonic-gate * Find the first unavailable pic, after which we must start our search. 141*0Sstevel@tonic-gate */ 142*0Sstevel@tonic-gate for (i = ncounters - 1; i >= 0; i--) { 143*0Sstevel@tonic-gate if (reqs[i].cr_event[0] != '\0') 144*0Sstevel@tonic-gate break; 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate /* 147*0Sstevel@tonic-gate * If the last counter has been assigned, we cannot place this event. 148*0Sstevel@tonic-gate */ 149*0Sstevel@tonic-gate if (i == ncounters - 1) 150*0Sstevel@tonic-gate return (0); 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate /* 153*0Sstevel@tonic-gate * If none of the counters have been assigned yet, i is -1 and we will 154*0Sstevel@tonic-gate * begin our search at 0. Else we begin our search at the counter after 155*0Sstevel@tonic-gate * the last one currently assigned. 156*0Sstevel@tonic-gate */ 157*0Sstevel@tonic-gate i++; 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate for (; i < ncounters; i++) { 160*0Sstevel@tonic-gate if (event_valid(i, event) == 0) 161*0Sstevel@tonic-gate continue; 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate nreqs++; 164*0Sstevel@tonic-gate (void) strncpy(reqs[i].cr_event, event, CPC_MAX_EVENT_LEN); 165*0Sstevel@tonic-gate return (1); 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate return (0); 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate static int 172*0Sstevel@tonic-gate pic(int tok, char *val) 173*0Sstevel@tonic-gate { 174*0Sstevel@tonic-gate int picnum = tok_info[tok].picnum; 175*0Sstevel@tonic-gate /* 176*0Sstevel@tonic-gate * Make sure the each pic only appears in the spec once. 177*0Sstevel@tonic-gate */ 178*0Sstevel@tonic-gate if (reqs[picnum].cr_event[0] != '\0') { 179*0Sstevel@tonic-gate strtoset_err(gettext("repeated 'pic%d' token\n"), picnum); 180*0Sstevel@tonic-gate return (-1); 181*0Sstevel@tonic-gate } 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate if (val == NULL || val[0] == '\0') { 184*0Sstevel@tonic-gate strtoset_err(gettext("missing 'pic%d' value\n"), picnum); 185*0Sstevel@tonic-gate return (-1); 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate if (event_valid(picnum, val) == 0) { 189*0Sstevel@tonic-gate strtoset_err(gettext("pic%d cannot measure event '%s' on this " 190*0Sstevel@tonic-gate "cpu\n"), picnum, val); 191*0Sstevel@tonic-gate return (-1); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate nreqs++; 195*0Sstevel@tonic-gate (void) strncpy(reqs[picnum].cr_event, val, CPC_MAX_EVENT_LEN); 196*0Sstevel@tonic-gate return (0); 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate /* 200*0Sstevel@tonic-gate * We explicitly ignore any value provided for these tokens, as their 201*0Sstevel@tonic-gate * mere presence signals us to turn on or off the relevant flags. 202*0Sstevel@tonic-gate */ 203*0Sstevel@tonic-gate /*ARGSUSED*/ 204*0Sstevel@tonic-gate static int 205*0Sstevel@tonic-gate flag(int tok, char *val) 206*0Sstevel@tonic-gate { 207*0Sstevel@tonic-gate int i; 208*0Sstevel@tonic-gate int picnum = tok_info[tok].picnum; 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate /* 211*0Sstevel@tonic-gate * If picnum is -1, this flag should be applied to all reqs. 212*0Sstevel@tonic-gate */ 213*0Sstevel@tonic-gate for (i = (picnum == -1) ? 0 : picnum; i < ncounters; i++) { 214*0Sstevel@tonic-gate if (strcmp(tok_info[tok].name, "nouser") == 0) 215*0Sstevel@tonic-gate reqs[i].cr_flags &= ~CPC_COUNT_USER; 216*0Sstevel@tonic-gate else if (strcmp(tok_info[tok].name, "sys") == 0) 217*0Sstevel@tonic-gate reqs[i].cr_flags |= CPC_COUNT_SYSTEM; 218*0Sstevel@tonic-gate else 219*0Sstevel@tonic-gate return (-1); 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate if (picnum != -1) 222*0Sstevel@tonic-gate break; 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate return (0); 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate static int 229*0Sstevel@tonic-gate doattr(int tok, char *val) 230*0Sstevel@tonic-gate { 231*0Sstevel@tonic-gate int i; 232*0Sstevel@tonic-gate int picnum = tok_info[tok].picnum; 233*0Sstevel@tonic-gate tmp_attr_t *tmp; 234*0Sstevel@tonic-gate char *endptr; 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate /* 237*0Sstevel@tonic-gate * If picnum is -1, this attribute should be applied to all reqs. 238*0Sstevel@tonic-gate */ 239*0Sstevel@tonic-gate for (i = (picnum == -1) ? 0 : picnum; i < ncounters; i++) { 240*0Sstevel@tonic-gate tmp = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t)); 241*0Sstevel@tonic-gate tmp->name = tok_info[tok].name; 242*0Sstevel@tonic-gate if (val != NULL) { 243*0Sstevel@tonic-gate tmp->val = strtoll(val, &endptr, 0); 244*0Sstevel@tonic-gate if (endptr == val) { 245*0Sstevel@tonic-gate strtoset_err(gettext("invalid value '%s' for " 246*0Sstevel@tonic-gate "attribute '%s'\n"), val, tmp->name); 247*0Sstevel@tonic-gate free(tmp); 248*0Sstevel@tonic-gate return (-1); 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate } else 251*0Sstevel@tonic-gate /* 252*0Sstevel@tonic-gate * No value was provided for this attribute, 253*0Sstevel@tonic-gate * so specify a default value of 1. 254*0Sstevel@tonic-gate */ 255*0Sstevel@tonic-gate tmp->val = 1; 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gate tmp->next = attrs[i]; 258*0Sstevel@tonic-gate attrs[i] = tmp; 259*0Sstevel@tonic-gate reqs[i].cr_nattrs++; 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate if (picnum != -1) 262*0Sstevel@tonic-gate break; 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate return (0); 266*0Sstevel@tonic-gate } 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate /*ARGSUSED*/ 269*0Sstevel@tonic-gate static void 270*0Sstevel@tonic-gate attr_count_walker(void *arg, const char *attr) 271*0Sstevel@tonic-gate { 272*0Sstevel@tonic-gate /* 273*0Sstevel@tonic-gate * We don't allow picnum to be specified by the user. 274*0Sstevel@tonic-gate */ 275*0Sstevel@tonic-gate if (strncmp(attr, "picnum", 7) == 0) 276*0Sstevel@tonic-gate return; 277*0Sstevel@tonic-gate (*(int *)arg)++; 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate static int 281*0Sstevel@tonic-gate cpc_count_attrs(cpc_t *cpc) 282*0Sstevel@tonic-gate { 283*0Sstevel@tonic-gate int nattrs = 0; 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gate cpc_walk_attrs(cpc, &nattrs, attr_count_walker); 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate return (nattrs); 288*0Sstevel@tonic-gate } 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gate static void 291*0Sstevel@tonic-gate attr_walker(void *arg, const char *attr) 292*0Sstevel@tonic-gate { 293*0Sstevel@tonic-gate int *i = arg; 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gate if (strncmp(attr, "picnum", 7) == 0) 296*0Sstevel@tonic-gate return; 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate if ((attrlist[(*i)++] = strdup(attr)) == NULL) { 299*0Sstevel@tonic-gate strtoset_err(gettext("no memory available\n")); 300*0Sstevel@tonic-gate exit(0); 301*0Sstevel@tonic-gate } 302*0Sstevel@tonic-gate } 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate cpc_set_t * 305*0Sstevel@tonic-gate cpc_strtoset(cpc_t *cpcin, const char *spec, int smt) 306*0Sstevel@tonic-gate { 307*0Sstevel@tonic-gate cpc_set_t *set; 308*0Sstevel@tonic-gate cpc_attr_t *req_attrs; 309*0Sstevel@tonic-gate tmp_attr_t *tmp; 310*0Sstevel@tonic-gate size_t toklen; 311*0Sstevel@tonic-gate int i; 312*0Sstevel@tonic-gate int j; 313*0Sstevel@tonic-gate int x; 314*0Sstevel@tonic-gate char *opts; 315*0Sstevel@tonic-gate char *val; 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate cpc = cpcin; 318*0Sstevel@tonic-gate nattrs = 0; 319*0Sstevel@tonic-gate 320*0Sstevel@tonic-gate ncounters = cpc_npic(cpc); 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gate reqs = (request_t *)emalloc(ncounters * sizeof (request_t)); 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate attrs = (tmp_attr_t **)emalloc(ncounters * sizeof (tmp_attr_t *)); 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 327*0Sstevel@tonic-gate reqs[i].cr_event[0] = '\0'; 328*0Sstevel@tonic-gate reqs[i].cr_flags = CPC_COUNT_USER; 329*0Sstevel@tonic-gate /* 330*0Sstevel@tonic-gate * Each pic will have at least one attribute: the physical pic 331*0Sstevel@tonic-gate * assignment via the "picnum" attribute. Set that up here for 332*0Sstevel@tonic-gate * each request. 333*0Sstevel@tonic-gate */ 334*0Sstevel@tonic-gate reqs[i].cr_nattrs = 1; 335*0Sstevel@tonic-gate attrs[i] = emalloc(sizeof (tmp_attr_t)); 336*0Sstevel@tonic-gate attrs[i]->name = "picnum"; 337*0Sstevel@tonic-gate attrs[i]->val = i; 338*0Sstevel@tonic-gate attrs[i]->next = NULL; 339*0Sstevel@tonic-gate } 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate /* 342*0Sstevel@tonic-gate * Build up a list of acceptable tokens. 343*0Sstevel@tonic-gate * 344*0Sstevel@tonic-gate * Permitted tokens are 345*0Sstevel@tonic-gate * picn=event 346*0Sstevel@tonic-gate * nousern 347*0Sstevel@tonic-gate * sysn 348*0Sstevel@tonic-gate * attrn=val 349*0Sstevel@tonic-gate * nouser 350*0Sstevel@tonic-gate * sys 351*0Sstevel@tonic-gate * attr=val 352*0Sstevel@tonic-gate * 353*0Sstevel@tonic-gate * Where n is a counter number, and attr is any attribute supported by 354*0Sstevel@tonic-gate * the current processor. 355*0Sstevel@tonic-gate * 356*0Sstevel@tonic-gate * If a token appears without a counter number, it applies to all 357*0Sstevel@tonic-gate * counters in the request set. 358*0Sstevel@tonic-gate * 359*0Sstevel@tonic-gate * The number of tokens is: 360*0Sstevel@tonic-gate * 361*0Sstevel@tonic-gate * picn: ncounters 362*0Sstevel@tonic-gate * generic flags: 2 * ncounters (nouser, sys) 363*0Sstevel@tonic-gate * attrs: nattrs * ncounters 364*0Sstevel@tonic-gate * attrs with no picnum: nattrs 365*0Sstevel@tonic-gate * generic flags with no picnum: 2 (nouser, sys) 366*0Sstevel@tonic-gate * NULL token to signify end of list to getsubopt(3C). 367*0Sstevel@tonic-gate * 368*0Sstevel@tonic-gate * Matching each token's index in the token table is a function which 369*0Sstevel@tonic-gate * process that token; these are in tok_funcs. 370*0Sstevel@tonic-gate */ 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate /* 373*0Sstevel@tonic-gate * Count the number of valid attributes. 374*0Sstevel@tonic-gate * Set up the attrlist array to point to the attributes in attrlistp. 375*0Sstevel@tonic-gate */ 376*0Sstevel@tonic-gate nattrs = cpc_count_attrs(cpc); 377*0Sstevel@tonic-gate attrlist = (char **)emalloc(nattrs * sizeof (char *)); 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate i = 0; 380*0Sstevel@tonic-gate cpc_walk_attrs(cpc, &i, attr_walker); 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gate ntoks = ncounters + (2 * ncounters) + (nattrs * ncounters) + nattrs + 3; 383*0Sstevel@tonic-gate toks = (char **)emalloc(ntoks * sizeof (char *)); 384*0Sstevel@tonic-gate tok_info = (tok_info_t *)emalloc(ntoks * sizeof (tok_info_t)); 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gate tok_funcs = (int (**)(int, char *))emalloc(ntoks * 387*0Sstevel@tonic-gate sizeof (int (*)(char *))); 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gate for (i = 0; i < ntoks; i++) { 390*0Sstevel@tonic-gate toks[i] = NULL; 391*0Sstevel@tonic-gate tok_funcs[i] = NULL; 392*0Sstevel@tonic-gate } 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate x = 0; 395*0Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 396*0Sstevel@tonic-gate toks[x] = (char *)emalloc(TOK_SIZE); 397*0Sstevel@tonic-gate (void) snprintf(toks[x], TOK_SIZE, "pic%d", i); 398*0Sstevel@tonic-gate tok_info[x].name = "pic"; 399*0Sstevel@tonic-gate tok_info[i].picnum = i; 400*0Sstevel@tonic-gate tok_funcs[x] = pic; 401*0Sstevel@tonic-gate x++; 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 405*0Sstevel@tonic-gate toks[x] = (char *)emalloc(TOK_SIZE); 406*0Sstevel@tonic-gate (void) snprintf(toks[x], TOK_SIZE, "nouser%d", i); 407*0Sstevel@tonic-gate tok_info[x].name = "nouser"; 408*0Sstevel@tonic-gate tok_info[x].picnum = i; 409*0Sstevel@tonic-gate tok_funcs[x] = flag; 410*0Sstevel@tonic-gate x++; 411*0Sstevel@tonic-gate } 412*0Sstevel@tonic-gate 413*0Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 414*0Sstevel@tonic-gate toks[x] = (char *)emalloc(TOK_SIZE); 415*0Sstevel@tonic-gate (void) snprintf(toks[x], TOK_SIZE, "sys%d", i); 416*0Sstevel@tonic-gate tok_info[x].name = "sys"; 417*0Sstevel@tonic-gate tok_info[x].picnum = i; 418*0Sstevel@tonic-gate tok_funcs[x] = flag; 419*0Sstevel@tonic-gate x++; 420*0Sstevel@tonic-gate } 421*0Sstevel@tonic-gate for (j = 0; j < nattrs; j++) { 422*0Sstevel@tonic-gate toklen = strlen(attrlist[j]) + 3; 423*0Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 424*0Sstevel@tonic-gate toks[x] = (char *)emalloc(toklen); 425*0Sstevel@tonic-gate (void) snprintf(toks[x], toklen, "%s%d", attrlist[j], 426*0Sstevel@tonic-gate i); 427*0Sstevel@tonic-gate tok_info[x].name = attrlist[j]; 428*0Sstevel@tonic-gate tok_info[x].picnum = i; 429*0Sstevel@tonic-gate tok_funcs[x] = doattr; 430*0Sstevel@tonic-gate x++; 431*0Sstevel@tonic-gate } 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate /* 434*0Sstevel@tonic-gate * Now create a token for this attribute with no picnum; if used 435*0Sstevel@tonic-gate * it will be applied to all reqs. 436*0Sstevel@tonic-gate */ 437*0Sstevel@tonic-gate toks[x] = (char *)emalloc(toklen); 438*0Sstevel@tonic-gate (void) snprintf(toks[x], toklen, "%s", attrlist[j]); 439*0Sstevel@tonic-gate tok_info[x].name = attrlist[j]; 440*0Sstevel@tonic-gate tok_info[x].picnum = -1; 441*0Sstevel@tonic-gate tok_funcs[x] = doattr; 442*0Sstevel@tonic-gate x++; 443*0Sstevel@tonic-gate } 444*0Sstevel@tonic-gate 445*0Sstevel@tonic-gate toks[x] = "nouser"; 446*0Sstevel@tonic-gate tok_info[x].name = "nouser"; 447*0Sstevel@tonic-gate tok_info[x].picnum = -1; 448*0Sstevel@tonic-gate tok_funcs[x] = flag; 449*0Sstevel@tonic-gate x++; 450*0Sstevel@tonic-gate 451*0Sstevel@tonic-gate toks[x] = "sys"; 452*0Sstevel@tonic-gate tok_info[x].name = "sys"; 453*0Sstevel@tonic-gate tok_info[x].picnum = -1; 454*0Sstevel@tonic-gate tok_funcs[x] = flag; 455*0Sstevel@tonic-gate x++; 456*0Sstevel@tonic-gate 457*0Sstevel@tonic-gate toks[x] = NULL; 458*0Sstevel@tonic-gate 459*0Sstevel@tonic-gate opts = strcpy(alloca(strlen(spec) + 1), spec); 460*0Sstevel@tonic-gate while (*opts != '\0') { 461*0Sstevel@tonic-gate int idx = getsubopt(&opts, toks, &val); 462*0Sstevel@tonic-gate 463*0Sstevel@tonic-gate if (idx == -1) { 464*0Sstevel@tonic-gate if (find_event(val) == 0) { 465*0Sstevel@tonic-gate strtoset_err(gettext("bad token '%s'\n"), val); 466*0Sstevel@tonic-gate goto inval; 467*0Sstevel@tonic-gate } else 468*0Sstevel@tonic-gate continue; 469*0Sstevel@tonic-gate } 470*0Sstevel@tonic-gate 471*0Sstevel@tonic-gate if (tok_funcs[idx](idx, val) == -1) 472*0Sstevel@tonic-gate goto inval; 473*0Sstevel@tonic-gate } 474*0Sstevel@tonic-gate 475*0Sstevel@tonic-gate /* 476*0Sstevel@tonic-gate * The string has been processed. Now count how many PICs were used, 477*0Sstevel@tonic-gate * create a request set, and specify each request properly. 478*0Sstevel@tonic-gate */ 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate if ((set = cpc_set_create(cpc)) == NULL) { 481*0Sstevel@tonic-gate strtoset_err(gettext("no memory available\n")); 482*0Sstevel@tonic-gate exit(0); 483*0Sstevel@tonic-gate } 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 486*0Sstevel@tonic-gate if (reqs[i].cr_event[0] == '\0') 487*0Sstevel@tonic-gate continue; 488*0Sstevel@tonic-gate 489*0Sstevel@tonic-gate /* 490*0Sstevel@tonic-gate * If the caller wishes to measure events on the physical CPU, 491*0Sstevel@tonic-gate * we need to add SMT attributes to each request. 492*0Sstevel@tonic-gate */ 493*0Sstevel@tonic-gate if (smt) 494*0Sstevel@tonic-gate smt_special(i); 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate req_attrs = (cpc_attr_t *)emalloc(reqs[i].cr_nattrs * 497*0Sstevel@tonic-gate sizeof (cpc_attr_t)); 498*0Sstevel@tonic-gate 499*0Sstevel@tonic-gate j = 0; 500*0Sstevel@tonic-gate for (tmp = attrs[i]; tmp != NULL; tmp = tmp->next) { 501*0Sstevel@tonic-gate req_attrs[j].ca_name = tmp->name; 502*0Sstevel@tonic-gate req_attrs[j].ca_val = tmp->val; 503*0Sstevel@tonic-gate j++; 504*0Sstevel@tonic-gate } 505*0Sstevel@tonic-gate 506*0Sstevel@tonic-gate if (cpc_set_add_request(cpc, set, reqs[i].cr_event, 0, 507*0Sstevel@tonic-gate reqs[i].cr_flags, reqs[i].cr_nattrs, req_attrs) == -1) { 508*0Sstevel@tonic-gate free(req_attrs); 509*0Sstevel@tonic-gate (void) cpc_set_destroy(cpc, set); 510*0Sstevel@tonic-gate strtoset_err( 511*0Sstevel@tonic-gate gettext("cpc_set_add_request() failed: %s\n"), 512*0Sstevel@tonic-gate strerror(errno)); 513*0Sstevel@tonic-gate goto inval; 514*0Sstevel@tonic-gate } 515*0Sstevel@tonic-gate 516*0Sstevel@tonic-gate free(req_attrs); 517*0Sstevel@tonic-gate } 518*0Sstevel@tonic-gate 519*0Sstevel@tonic-gate strtoset_cleanup(); 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gate return (set); 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gate inval: 524*0Sstevel@tonic-gate strtoset_cleanup(); 525*0Sstevel@tonic-gate errno = EINVAL; 526*0Sstevel@tonic-gate return (NULL); 527*0Sstevel@tonic-gate } 528*0Sstevel@tonic-gate 529*0Sstevel@tonic-gate static void 530*0Sstevel@tonic-gate strtoset_cleanup(void) 531*0Sstevel@tonic-gate { 532*0Sstevel@tonic-gate int i; 533*0Sstevel@tonic-gate tmp_attr_t *tmp, *p; 534*0Sstevel@tonic-gate 535*0Sstevel@tonic-gate for (i = 0; i < nattrs; i++) 536*0Sstevel@tonic-gate free(attrlist[i]); 537*0Sstevel@tonic-gate free(attrlist); 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate for (i = 0; i < ncounters; i++) { 540*0Sstevel@tonic-gate for (tmp = attrs[i]; tmp != NULL; tmp = p) { 541*0Sstevel@tonic-gate p = tmp->next; 542*0Sstevel@tonic-gate free(tmp); 543*0Sstevel@tonic-gate } 544*0Sstevel@tonic-gate } 545*0Sstevel@tonic-gate free(attrs); 546*0Sstevel@tonic-gate 547*0Sstevel@tonic-gate for (i = 0; i < ntoks - 3; i++) 548*0Sstevel@tonic-gate /* 549*0Sstevel@tonic-gate * We free all but the last three tokens: "nouser", "sys", NULL 550*0Sstevel@tonic-gate */ 551*0Sstevel@tonic-gate free(toks[i]); 552*0Sstevel@tonic-gate free(toks); 553*0Sstevel@tonic-gate free(reqs); 554*0Sstevel@tonic-gate free(tok_info); 555*0Sstevel@tonic-gate free(tok_funcs); 556*0Sstevel@tonic-gate } 557*0Sstevel@tonic-gate 558*0Sstevel@tonic-gate /* 559*0Sstevel@tonic-gate * The following is called to modify requests so that they count events on 560*0Sstevel@tonic-gate * behalf of a physical processor, instead of a logical processor. It duplicates 561*0Sstevel@tonic-gate * the request flags for the sibling processor (i.e. if the request counts user 562*0Sstevel@tonic-gate * events, add an attribute to count user events on the sibling processor also). 563*0Sstevel@tonic-gate */ 564*0Sstevel@tonic-gate static void 565*0Sstevel@tonic-gate smt_special(int picnum) 566*0Sstevel@tonic-gate { 567*0Sstevel@tonic-gate tmp_attr_t *attr; 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate if (reqs[picnum].cr_flags & CPC_COUNT_USER) { 570*0Sstevel@tonic-gate attr = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t)); 571*0Sstevel@tonic-gate attr->name = "count_sibling_usr"; 572*0Sstevel@tonic-gate attr->val = 1; 573*0Sstevel@tonic-gate attr->next = attrs[picnum]; 574*0Sstevel@tonic-gate attrs[picnum] = attr; 575*0Sstevel@tonic-gate reqs[picnum].cr_nattrs++; 576*0Sstevel@tonic-gate } 577*0Sstevel@tonic-gate 578*0Sstevel@tonic-gate if (reqs[picnum].cr_flags & CPC_COUNT_SYSTEM) { 579*0Sstevel@tonic-gate attr = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t)); 580*0Sstevel@tonic-gate attr->name = "count_sibling_sys"; 581*0Sstevel@tonic-gate attr->val = 1; 582*0Sstevel@tonic-gate attr->next = attrs[picnum]; 583*0Sstevel@tonic-gate attrs[picnum] = attr; 584*0Sstevel@tonic-gate reqs[picnum].cr_nattrs++; 585*0Sstevel@tonic-gate } 586*0Sstevel@tonic-gate } 587*0Sstevel@tonic-gate 588*0Sstevel@tonic-gate /* 589*0Sstevel@tonic-gate * If we ever fail to get memory, we print an error message and exit. 590*0Sstevel@tonic-gate */ 591*0Sstevel@tonic-gate static void * 592*0Sstevel@tonic-gate emalloc(size_t n) 593*0Sstevel@tonic-gate { 594*0Sstevel@tonic-gate void *p = malloc(n); 595*0Sstevel@tonic-gate 596*0Sstevel@tonic-gate if (p == NULL) { 597*0Sstevel@tonic-gate strtoset_err(gettext("no memory available\n")); 598*0Sstevel@tonic-gate exit(0); 599*0Sstevel@tonic-gate } 600*0Sstevel@tonic-gate 601*0Sstevel@tonic-gate return (p); 602*0Sstevel@tonic-gate } 603