xref: /titanic_53/usr/src/cmd/cpc/common/caps.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #define	 __EXTENSIONS__	/* header bug! strtok_r is overly hidden */
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <strings.h>
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
34*7c478bd9Sstevel@tonic-gate #include <unistd.h>
35*7c478bd9Sstevel@tonic-gate #include <libintl.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <libcpc.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "cpucmds.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate struct args {
42*7c478bd9Sstevel@tonic-gate 	FILE *fp;
43*7c478bd9Sstevel@tonic-gate 	int colnum;
44*7c478bd9Sstevel@tonic-gate 	int margin;
45*7c478bd9Sstevel@tonic-gate };
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate struct evlist {
48*7c478bd9Sstevel@tonic-gate 	char *list;
49*7c478bd9Sstevel@tonic-gate 	int size;
50*7c478bd9Sstevel@tonic-gate };
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #define	MAX_RHS_COLUMN	76
53*7c478bd9Sstevel@tonic-gate #define	EVENT_MARGIN	17
54*7c478bd9Sstevel@tonic-gate #define	ATTR_MARGIN	20
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
57*7c478bd9Sstevel@tonic-gate static void
58*7c478bd9Sstevel@tonic-gate list_cap(void *arg, uint_t regno, const char *name)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	struct args *args = arg;
61*7c478bd9Sstevel@tonic-gate 	int i;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	if ((args->colnum + strlen(name) + 1) > MAX_RHS_COLUMN) {
64*7c478bd9Sstevel@tonic-gate 		(void) fprintf(args->fp, "\n");
65*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < args->margin; i++)
66*7c478bd9Sstevel@tonic-gate 			(void) fprintf(args->fp, " ");
67*7c478bd9Sstevel@tonic-gate 		args->colnum = args->margin;
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 	args->colnum += fprintf(args->fp, "%s ", name);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate static void
73*7c478bd9Sstevel@tonic-gate list_attr(void *arg, const char *name)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	/*
76*7c478bd9Sstevel@tonic-gate 	 * The following attributes are used by the commands but should not be
77*7c478bd9Sstevel@tonic-gate 	 * reported to the user, since they may not be specified directly.
78*7c478bd9Sstevel@tonic-gate 	 */
79*7c478bd9Sstevel@tonic-gate 	if (strncmp(name, "picnum", 7) == 0 ||
80*7c478bd9Sstevel@tonic-gate 	    strncmp(name, "count_sibling_usr", 18) == 0 ||
81*7c478bd9Sstevel@tonic-gate 	    strncmp(name, "count_sibling_sys", 18) == 0)
82*7c478bd9Sstevel@tonic-gate 		return;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	list_cap(arg, 0, name);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate static void *
88*7c478bd9Sstevel@tonic-gate emalloc(size_t size)
89*7c478bd9Sstevel@tonic-gate {
90*7c478bd9Sstevel@tonic-gate 	void *ptr;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if ((ptr = malloc(size)) == NULL) {
93*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("no memory available\n"));
94*7c478bd9Sstevel@tonic-gate 		exit(1);
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	return (ptr);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate  * Used by allpics_equal().
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
104*7c478bd9Sstevel@tonic-gate static void
105*7c478bd9Sstevel@tonic-gate cap_walker(void *arg, uint_t regno, const char *name)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	struct evlist *list = arg;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	list->size += strlen(name);
110*7c478bd9Sstevel@tonic-gate 	if ((list->list = realloc(list->list, list->size + 1)) == NULL) {
111*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("no memory available\n"));
112*7c478bd9Sstevel@tonic-gate 		exit(1);
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	(void) strcat(list->list, name);
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate /*
119*7c478bd9Sstevel@tonic-gate  * Returns 1 if all counters on this chip can count all possible events.
120*7c478bd9Sstevel@tonic-gate  */
121*7c478bd9Sstevel@tonic-gate static int
122*7c478bd9Sstevel@tonic-gate allpics_equal(cpc_t *cpc)
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate 	int	npics = cpc_npic(cpc);
125*7c478bd9Sstevel@tonic-gate 	int	i;
126*7c478bd9Sstevel@tonic-gate 	struct	evlist **lists;
127*7c478bd9Sstevel@tonic-gate 	int	ret = 1;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	lists = emalloc(npics * sizeof (struct evlist *));
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < npics; i++) {
132*7c478bd9Sstevel@tonic-gate 		lists[i] = emalloc(sizeof (struct evlist));
133*7c478bd9Sstevel@tonic-gate 		lists[i]->size = 0;
134*7c478bd9Sstevel@tonic-gate 		lists[i]->list = emalloc(1);
135*7c478bd9Sstevel@tonic-gate 		lists[i]->list[0] = '\0';
136*7c478bd9Sstevel@tonic-gate 		cpc_walk_events_pic(cpc, i, lists[i], cap_walker);
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < npics; i++)
140*7c478bd9Sstevel@tonic-gate 		if (lists[i]->size != lists[0]->size ||
141*7c478bd9Sstevel@tonic-gate 		    strncmp(lists[i]->list, lists[0]->list,
142*7c478bd9Sstevel@tonic-gate 			lists[0]->size) != 0) {
143*7c478bd9Sstevel@tonic-gate 			ret = 0;
144*7c478bd9Sstevel@tonic-gate 			break;
145*7c478bd9Sstevel@tonic-gate 		}
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < npics; i++) {
148*7c478bd9Sstevel@tonic-gate 		free(lists[i]->list);
149*7c478bd9Sstevel@tonic-gate 		free(lists[i]);
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 	free(lists);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	return (ret);
154*7c478bd9Sstevel@tonic-gate }
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate int
157*7c478bd9Sstevel@tonic-gate capabilities(cpc_t *cpc, FILE *fp)
158*7c478bd9Sstevel@tonic-gate {
159*7c478bd9Sstevel@tonic-gate 	struct args _args, *args = &_args;
160*7c478bd9Sstevel@tonic-gate 	char *text, *tok, *cp;
161*7c478bd9Sstevel@tonic-gate 	const char *ccp;
162*7c478bd9Sstevel@tonic-gate 	int npic = cpc_npic(cpc);
163*7c478bd9Sstevel@tonic-gate 	int i;
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	args->fp = fp;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	if ((ccp = cpc_cciname(cpc)) == NULL)
168*7c478bd9Sstevel@tonic-gate 		ccp = "No information available";
169*7c478bd9Sstevel@tonic-gate 	(void) fprintf(args->fp, "\t%s: %s\n\n",
170*7c478bd9Sstevel@tonic-gate 	    gettext("CPU performance counter interface"), ccp);
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	(void) fprintf(args->fp, gettext("\tevent specification syntax:\n"));
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	(void) fprintf(args->fp, "\t[picn=]<eventn>[,attr[n][=<val>]]"
175*7c478bd9Sstevel@tonic-gate 	    "[,[picn=]<eventn>[,attr[n][=<val>]],...]\n");
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	if (allpics_equal(cpc)) {
178*7c478bd9Sstevel@tonic-gate 		args->margin = args->colnum = EVENT_MARGIN;
179*7c478bd9Sstevel@tonic-gate 		(void) fprintf(args->fp, "\n\tevent[0-%d]: ", npic - 1);
180*7c478bd9Sstevel@tonic-gate 		cpc_walk_events_pic(cpc, 0, args, list_cap);
181*7c478bd9Sstevel@tonic-gate 		(void) fprintf(args->fp, "\n");
182*7c478bd9Sstevel@tonic-gate 	} else {
183*7c478bd9Sstevel@tonic-gate 		args->margin = EVENT_MARGIN;
184*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < npic; i++) {
185*7c478bd9Sstevel@tonic-gate 			(void) fprintf(args->fp, "\n\tevent%d: ", i);
186*7c478bd9Sstevel@tonic-gate 			if (i < 10) (void) fprintf(args->fp, " ");
187*7c478bd9Sstevel@tonic-gate 			args->colnum = EVENT_MARGIN;
188*7c478bd9Sstevel@tonic-gate 			cpc_walk_events_pic(cpc, i, args, list_cap);
189*7c478bd9Sstevel@tonic-gate 			(void) fprintf(args->fp, "\n");
190*7c478bd9Sstevel@tonic-gate 		}
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	(void) fprintf(args->fp, "\n\tattributes: ");
194*7c478bd9Sstevel@tonic-gate 	args->colnum = args->margin = ATTR_MARGIN;
195*7c478bd9Sstevel@tonic-gate 	cpc_walk_attrs(cpc, args, list_attr);
196*7c478bd9Sstevel@tonic-gate 	/*
197*7c478bd9Sstevel@tonic-gate 	 * In addition to the attributes published by the kernel, we allow the
198*7c478bd9Sstevel@tonic-gate 	 * user to specify two additional tokens on all platforms. List them
199*7c478bd9Sstevel@tonic-gate 	 * here.
200*7c478bd9Sstevel@tonic-gate 	 */
201*7c478bd9Sstevel@tonic-gate 	list_cap(args, 0, "nouser");
202*7c478bd9Sstevel@tonic-gate 	list_cap(args, 0, "sys");
203*7c478bd9Sstevel@tonic-gate 	(void) fprintf(args->fp, "\n\n\t");
204*7c478bd9Sstevel@tonic-gate 	args->colnum = 8;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	if ((ccp = cpc_cpuref(cpc)) == NULL)
207*7c478bd9Sstevel@tonic-gate 		ccp = "No information available";
208*7c478bd9Sstevel@tonic-gate 	if ((text = strdup(ccp)) == NULL) {
209*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("no memory available.\n"));
210*7c478bd9Sstevel@tonic-gate 		exit(1);
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate 	for (cp = strtok_r(text, " ", &tok);
213*7c478bd9Sstevel@tonic-gate 	    cp != NULL; cp = strtok_r(NULL, " ", &tok)) {
214*7c478bd9Sstevel@tonic-gate 		if ((args->colnum + strlen(cp) + 1) > MAX_RHS_COLUMN) {
215*7c478bd9Sstevel@tonic-gate 			(void) fprintf(args->fp, "\n\t");
216*7c478bd9Sstevel@tonic-gate 			args->colnum = 8;
217*7c478bd9Sstevel@tonic-gate 		}
218*7c478bd9Sstevel@tonic-gate 		args->colnum += fprintf(args->fp, "%s ", cp);
219*7c478bd9Sstevel@tonic-gate 	}
220*7c478bd9Sstevel@tonic-gate 	(void) fprintf(args->fp, "\n");
221*7c478bd9Sstevel@tonic-gate 	free(text);
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	return (0);
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate /*
227*7c478bd9Sstevel@tonic-gate  * Returns 1 on SMT processors which do not have full CPC hardware for each
228*7c478bd9Sstevel@tonic-gate  * logical processor.
229*7c478bd9Sstevel@tonic-gate  */
230*7c478bd9Sstevel@tonic-gate int
231*7c478bd9Sstevel@tonic-gate smt_limited_cpc_hw(cpc_t *cpc)
232*7c478bd9Sstevel@tonic-gate {
233*7c478bd9Sstevel@tonic-gate 	if (strcmp(cpc_cciname(cpc), "Pentium 4 with HyperThreading") == 0)
234*7c478bd9Sstevel@tonic-gate 		return (1);
235*7c478bd9Sstevel@tonic-gate 	return (0);
236*7c478bd9Sstevel@tonic-gate }
237