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 #define __EXTENSIONS__ /* header bug! strtok_r is overly hidden */ 30*0Sstevel@tonic-gate #include <string.h> 31*0Sstevel@tonic-gate #include <strings.h> 32*0Sstevel@tonic-gate #include <stdio.h> 33*0Sstevel@tonic-gate #include <stdlib.h> 34*0Sstevel@tonic-gate #include <unistd.h> 35*0Sstevel@tonic-gate #include <libintl.h> 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate #include <libcpc.h> 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate #include "cpucmds.h" 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate struct args { 42*0Sstevel@tonic-gate FILE *fp; 43*0Sstevel@tonic-gate int colnum; 44*0Sstevel@tonic-gate int margin; 45*0Sstevel@tonic-gate }; 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate struct evlist { 48*0Sstevel@tonic-gate char *list; 49*0Sstevel@tonic-gate int size; 50*0Sstevel@tonic-gate }; 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate #define MAX_RHS_COLUMN 76 53*0Sstevel@tonic-gate #define EVENT_MARGIN 17 54*0Sstevel@tonic-gate #define ATTR_MARGIN 20 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate /*ARGSUSED*/ 57*0Sstevel@tonic-gate static void 58*0Sstevel@tonic-gate list_cap(void *arg, uint_t regno, const char *name) 59*0Sstevel@tonic-gate { 60*0Sstevel@tonic-gate struct args *args = arg; 61*0Sstevel@tonic-gate int i; 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate if ((args->colnum + strlen(name) + 1) > MAX_RHS_COLUMN) { 64*0Sstevel@tonic-gate (void) fprintf(args->fp, "\n"); 65*0Sstevel@tonic-gate for (i = 0; i < args->margin; i++) 66*0Sstevel@tonic-gate (void) fprintf(args->fp, " "); 67*0Sstevel@tonic-gate args->colnum = args->margin; 68*0Sstevel@tonic-gate } 69*0Sstevel@tonic-gate args->colnum += fprintf(args->fp, "%s ", name); 70*0Sstevel@tonic-gate } 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate static void 73*0Sstevel@tonic-gate list_attr(void *arg, const char *name) 74*0Sstevel@tonic-gate { 75*0Sstevel@tonic-gate /* 76*0Sstevel@tonic-gate * The following attributes are used by the commands but should not be 77*0Sstevel@tonic-gate * reported to the user, since they may not be specified directly. 78*0Sstevel@tonic-gate */ 79*0Sstevel@tonic-gate if (strncmp(name, "picnum", 7) == 0 || 80*0Sstevel@tonic-gate strncmp(name, "count_sibling_usr", 18) == 0 || 81*0Sstevel@tonic-gate strncmp(name, "count_sibling_sys", 18) == 0) 82*0Sstevel@tonic-gate return; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate list_cap(arg, 0, name); 85*0Sstevel@tonic-gate } 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate static void * 88*0Sstevel@tonic-gate emalloc(size_t size) 89*0Sstevel@tonic-gate { 90*0Sstevel@tonic-gate void *ptr; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate if ((ptr = malloc(size)) == NULL) { 93*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("no memory available\n")); 94*0Sstevel@tonic-gate exit(1); 95*0Sstevel@tonic-gate } 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate return (ptr); 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate /* 101*0Sstevel@tonic-gate * Used by allpics_equal(). 102*0Sstevel@tonic-gate */ 103*0Sstevel@tonic-gate /*ARGSUSED*/ 104*0Sstevel@tonic-gate static void 105*0Sstevel@tonic-gate cap_walker(void *arg, uint_t regno, const char *name) 106*0Sstevel@tonic-gate { 107*0Sstevel@tonic-gate struct evlist *list = arg; 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate list->size += strlen(name); 110*0Sstevel@tonic-gate if ((list->list = realloc(list->list, list->size + 1)) == NULL) { 111*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("no memory available\n")); 112*0Sstevel@tonic-gate exit(1); 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate (void) strcat(list->list, name); 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate /* 119*0Sstevel@tonic-gate * Returns 1 if all counters on this chip can count all possible events. 120*0Sstevel@tonic-gate */ 121*0Sstevel@tonic-gate static int 122*0Sstevel@tonic-gate allpics_equal(cpc_t *cpc) 123*0Sstevel@tonic-gate { 124*0Sstevel@tonic-gate int npics = cpc_npic(cpc); 125*0Sstevel@tonic-gate int i; 126*0Sstevel@tonic-gate struct evlist **lists; 127*0Sstevel@tonic-gate int ret = 1; 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate lists = emalloc(npics * sizeof (struct evlist *)); 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate for (i = 0; i < npics; i++) { 132*0Sstevel@tonic-gate lists[i] = emalloc(sizeof (struct evlist)); 133*0Sstevel@tonic-gate lists[i]->size = 0; 134*0Sstevel@tonic-gate lists[i]->list = emalloc(1); 135*0Sstevel@tonic-gate lists[i]->list[0] = '\0'; 136*0Sstevel@tonic-gate cpc_walk_events_pic(cpc, i, lists[i], cap_walker); 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate for (i = 1; i < npics; i++) 140*0Sstevel@tonic-gate if (lists[i]->size != lists[0]->size || 141*0Sstevel@tonic-gate strncmp(lists[i]->list, lists[0]->list, 142*0Sstevel@tonic-gate lists[0]->size) != 0) { 143*0Sstevel@tonic-gate ret = 0; 144*0Sstevel@tonic-gate break; 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate for (i = 0; i < npics; i++) { 148*0Sstevel@tonic-gate free(lists[i]->list); 149*0Sstevel@tonic-gate free(lists[i]); 150*0Sstevel@tonic-gate } 151*0Sstevel@tonic-gate free(lists); 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate return (ret); 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate int 157*0Sstevel@tonic-gate capabilities(cpc_t *cpc, FILE *fp) 158*0Sstevel@tonic-gate { 159*0Sstevel@tonic-gate struct args _args, *args = &_args; 160*0Sstevel@tonic-gate char *text, *tok, *cp; 161*0Sstevel@tonic-gate const char *ccp; 162*0Sstevel@tonic-gate int npic = cpc_npic(cpc); 163*0Sstevel@tonic-gate int i; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate args->fp = fp; 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate if ((ccp = cpc_cciname(cpc)) == NULL) 168*0Sstevel@tonic-gate ccp = "No information available"; 169*0Sstevel@tonic-gate (void) fprintf(args->fp, "\t%s: %s\n\n", 170*0Sstevel@tonic-gate gettext("CPU performance counter interface"), ccp); 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate (void) fprintf(args->fp, gettext("\tevent specification syntax:\n")); 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate (void) fprintf(args->fp, "\t[picn=]<eventn>[,attr[n][=<val>]]" 175*0Sstevel@tonic-gate "[,[picn=]<eventn>[,attr[n][=<val>]],...]\n"); 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate if (allpics_equal(cpc)) { 178*0Sstevel@tonic-gate args->margin = args->colnum = EVENT_MARGIN; 179*0Sstevel@tonic-gate (void) fprintf(args->fp, "\n\tevent[0-%d]: ", npic - 1); 180*0Sstevel@tonic-gate cpc_walk_events_pic(cpc, 0, args, list_cap); 181*0Sstevel@tonic-gate (void) fprintf(args->fp, "\n"); 182*0Sstevel@tonic-gate } else { 183*0Sstevel@tonic-gate args->margin = EVENT_MARGIN; 184*0Sstevel@tonic-gate for (i = 0; i < npic; i++) { 185*0Sstevel@tonic-gate (void) fprintf(args->fp, "\n\tevent%d: ", i); 186*0Sstevel@tonic-gate if (i < 10) (void) fprintf(args->fp, " "); 187*0Sstevel@tonic-gate args->colnum = EVENT_MARGIN; 188*0Sstevel@tonic-gate cpc_walk_events_pic(cpc, i, args, list_cap); 189*0Sstevel@tonic-gate (void) fprintf(args->fp, "\n"); 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate (void) fprintf(args->fp, "\n\tattributes: "); 194*0Sstevel@tonic-gate args->colnum = args->margin = ATTR_MARGIN; 195*0Sstevel@tonic-gate cpc_walk_attrs(cpc, args, list_attr); 196*0Sstevel@tonic-gate /* 197*0Sstevel@tonic-gate * In addition to the attributes published by the kernel, we allow the 198*0Sstevel@tonic-gate * user to specify two additional tokens on all platforms. List them 199*0Sstevel@tonic-gate * here. 200*0Sstevel@tonic-gate */ 201*0Sstevel@tonic-gate list_cap(args, 0, "nouser"); 202*0Sstevel@tonic-gate list_cap(args, 0, "sys"); 203*0Sstevel@tonic-gate (void) fprintf(args->fp, "\n\n\t"); 204*0Sstevel@tonic-gate args->colnum = 8; 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate if ((ccp = cpc_cpuref(cpc)) == NULL) 207*0Sstevel@tonic-gate ccp = "No information available"; 208*0Sstevel@tonic-gate if ((text = strdup(ccp)) == NULL) { 209*0Sstevel@tonic-gate (void) fprintf(stderr, gettext("no memory available.\n")); 210*0Sstevel@tonic-gate exit(1); 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate for (cp = strtok_r(text, " ", &tok); 213*0Sstevel@tonic-gate cp != NULL; cp = strtok_r(NULL, " ", &tok)) { 214*0Sstevel@tonic-gate if ((args->colnum + strlen(cp) + 1) > MAX_RHS_COLUMN) { 215*0Sstevel@tonic-gate (void) fprintf(args->fp, "\n\t"); 216*0Sstevel@tonic-gate args->colnum = 8; 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate args->colnum += fprintf(args->fp, "%s ", cp); 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate (void) fprintf(args->fp, "\n"); 221*0Sstevel@tonic-gate free(text); 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate return (0); 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate /* 227*0Sstevel@tonic-gate * Returns 1 on SMT processors which do not have full CPC hardware for each 228*0Sstevel@tonic-gate * logical processor. 229*0Sstevel@tonic-gate */ 230*0Sstevel@tonic-gate int 231*0Sstevel@tonic-gate smt_limited_cpc_hw(cpc_t *cpc) 232*0Sstevel@tonic-gate { 233*0Sstevel@tonic-gate if (strcmp(cpc_cciname(cpc), "Pentium 4 with HyperThreading") == 0) 234*0Sstevel@tonic-gate return (1); 235*0Sstevel@tonic-gate return (0); 236*0Sstevel@tonic-gate } 237