Annotation of src/usr.bin/pctr/pctr.c, Revision 1.15
1.15 ! deraadt 1: /* $OpenBSD: pctr.c,v 1.14 2007/10/17 11:33:55 deraadt Exp $ */
1.13 deraadt 2:
3: /*
4: * Copyright (c) 2007 Mike Belopuhov, Aleksey Lomovtsev
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
1.1 dm 18:
19: /*
20: * Pentium performance counter control program for OpenBSD.
21: * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
22: *
23: * Modification and redistribution in source and binary forms is
24: * permitted provided that due credit is given to the author and the
1.5 pvalchev 25: * OpenBSD project by leaving this copyright notice intact.
1.1 dm 26: */
27:
1.4 downsj 28: #include <sys/param.h>
1.1 dm 29: #include <sys/types.h>
30: #include <sys/stat.h>
1.4 downsj 31: #include <sys/sysctl.h>
1.1 dm 32: #include <sys/ioctl.h>
1.13 deraadt 33:
1.1 dm 34: #include <machine/cpu.h>
35: #include <machine/pctr.h>
1.4 downsj 36: #include <machine/specialreg.h>
1.1 dm 37:
1.13 deraadt 38: #include <errno.h>
39: #include <err.h>
40: #include <fcntl.h>
41: #include <stdio.h>
42: #include <stdlib.h>
43: #include <string.h>
44: #include <sysexits.h>
45: #include <unistd.h>
46:
47: #include "pctrvar.h"
48:
49: static int cpu_type;
50: static int tsc_avail;
51:
52: static int ctr, func, masku, thold;
53: static int cflag, eflag, iflag, kflag, uflag;
54: static int Mflag, Eflag, Sflag, Iflag, Aflag;
55:
56: static int pctr_cpu_creds(void);
57: static char *pctr_fn2str(u_int32_t);
58: static void pctr_printvals(struct pctrst *);
59: static int pctr_read(struct pctrst *);
60: static int pctr_write(int, u_int32_t);
61: static void pctr_list_fnct(void);
62: static int pctr_set_cntr(void);
63: static void usage(void);
64:
65: int
66: main(int argc, char **argv)
67: {
68: const char *errstr;
69: struct pctrst st;
70: int ch = -1;
71: int list_mode = 0, set_mode = 0;
72:
73: if (pctr_cpu_creds())
74: errx(1, "pctr is only supported on i386 and amd64 "
75: "architectures by now");
76:
77: while ((ch = getopt(argc, argv, "cef:iklm:s:t:uMESIA")) != -1)
78: switch (ch) {
79: case 'l':
80: list_mode++;
81: break;
82: case 's':
83: set_mode++;
84: ctr = strtonum(optarg, 0, PCTR_NUM-1, &errstr);
85: if (errstr)
86: errx(1, "counter number is %s: %s", errstr,
87: optarg);
88: break;
89: case 'f':
90: if (sscanf(optarg, "%x", &func) <= 0 || func < 0 ||
91: func > PCTR_MAX_FUNCT)
92: errx(1, "invalid function number");
93: break;
94: case 'm':
95: if (sscanf(optarg, "%x", &masku) <= 0 || masku < 0 ||
96: masku > PCTR_MAX_UMASK)
97: errx(1, "invalid unit mask number");
98: break;
99: case 't':
100: thold = strtonum(optarg, 0, 0xff, &errstr);
101: if (errstr)
102: errx(1, "threshold is %s: %s", errstr, optarg);
103: break;
104: /* flags */
105: case 'c':
106: cflag++;
107: break;
108: case 'e':
109: eflag++;
110: break;
111: case 'i':
112: iflag++;
113: break;
114: case 'k':
115: kflag++;
116: break;
117: case 'u':
118: uflag++;
119: break;
120: /* MESI/A flags */
121: case 'M':
122: if (Aflag)
123: errx(1, "M, E, S, I and A are mutually "
124: "exclusive");
125: Mflag++;
126: break;
127: case 'E':
128: if (Aflag)
129: errx(1, "M, E, S, I and A are mutually "
130: "exclusive");
131: Eflag++;
132: break;
133: case 'S':
134: if (Aflag)
135: errx(1, "M, E, S, I and A are mutually "
136: "exclusive");
137: Sflag++;
138: break;
139: case 'I':
140: if (Aflag)
141: errx(1, "M, E, S, I and A are mutually "
142: "exclusive");
143: Iflag++;
144: break;
145: case 'A':
146: if (Mflag || Eflag || Sflag || Iflag)
147: errx(1, "M, E, S, I and A are mutually "
148: "exclusive");
149: Aflag++;
150: break;
151: default:
152: usage();
153: /* NOTREACHED */
154: }
155: argc -= optind;
156: argv += optind;
157:
158: if (list_mode)
159: pctr_list_fnct();
160: else if (set_mode) {
161: if (pctr_set_cntr() < 0)
162: err(1, "pctr_set_cntr");
163: } else {
164: bzero(&st, sizeof(st));
165: if (pctr_read(&st) < 0)
166: err(1, "pctr_read");
167: pctr_printvals(&st);
168: }
169: return (0);
170: }
171:
172: static int
173: pctr_cpu_creds(void)
174: {
175: int atype;
176: char arch[16], vendor[64];
177: int mib[2], cpu_id, cpu_feature;
178: size_t len;
179:
180: /* Get the architecture */
181: mib[0] = CTL_HW;
182: mib[1] = HW_MACHINE;
183: len = sizeof(arch) - 1;
184: bzero(arch, sizeof(arch));
185: if (sysctl(mib, 2, arch, &len, NULL, 0) == -1)
186: err(1, "HW_MACHINE");
187: arch[len] = '\0';
188:
189: if (strcmp(arch, "i386") == 0)
190: atype = ARCH_I386;
191: else if (strcmp(arch, "amd64") == 0)
192: atype = ARCH_AMD64;
193: else
194: return (EX_UNAVAILABLE); /* unsupported arch */
195:
196: /* Get the CPU id */
197: mib[0] = CTL_MACHDEP;
198: mib[1] = CPU_CPUID;
199: len = sizeof(cpu_id);
200: if (sysctl(mib, 2, &cpu_id, &len, NULL, 0) == -1)
201: err(1, "CPU_CPUID");
202:
203: /* Get the CPU features */
204: mib[1] = CPU_CPUFEATURE;
205: len = sizeof(cpu_feature);
206: if (sysctl(mib, 2, &cpu_feature, &len, NULL, 0) == -1)
207: err(1, "CPU_CPUFEATURE");
208:
209: /* Get the processor vendor */
210: mib[0] = CTL_MACHDEP;
211: mib[1] = CPU_CPUVENDOR;
212: len = sizeof(vendor) - 1;
213: bzero(vendor, sizeof(vendor));
214: if (sysctl(mib, 2, vendor, &len, NULL, 0) == -1)
215: err(1, "CPU_CPUVENDOR");
216: vendor[len] = '\0';
217:
218: switch (atype) {
219: case ARCH_I386:
220: if (strcmp(vendor, "AuthenticAMD") == 0) {
221: if (((cpu_id >> 8) & 15) >= 6)
222: cpu_type = CPU_AMD;
223: else
224: cpu_type = CPU_UNDEF; /* old AMD cpu */
225:
226: } else if (strcmp(vendor, "GenuineIntel") == 0) {
227: if (((cpu_id >> 8) & 15) == 6 &&
228: ((cpu_id >> 4) & 15) > 14)
229: cpu_type = CPU_CORE;
230: else if (((cpu_id >> 8) & 15) >= 6)
231: cpu_type = CPU_P6;
232: else if (((cpu_id >> 4) & 15) > 0)
233: cpu_type = CPU_P5;
234: else
235: cpu_type = CPU_UNDEF; /* old Intel cpu */
236: }
237: if (cpu_feature & CPUID_TSC)
238: tsc_avail = 1;
239: break;
240: case ARCH_AMD64:
241: if (strcmp(vendor, "AuthenticAMD") == 0)
242: cpu_type = CPU_AMD;
243: else if (strcmp(vendor, "GenuineIntel") == 0)
244: cpu_type = CPU_CORE;
245: if (cpu_feature & CPUID_TSC)
246: tsc_avail = 1;
247: break;
248: }
249: return (0);
250: }
251:
252: static __inline int
253: pctr_ctrfn_index(struct ctrfn *cfnp, u_int32_t func)
254: {
255: int i;
256:
257: for (i = 0; cfnp[i].name != NULL; i++)
258: if (cfnp[i].fn == func)
259: return (i);
260: return (-1);
261: }
262:
263: static char *
264: pctr_fn2str(u_int32_t sel)
265: {
266: static char buf[128];
267: struct ctrfn *cfnp = NULL;
268: char th[6], um[5], *msg;
269: u_int32_t fn;
270: int ind;
271:
272: bzero(buf, sizeof(buf));
273: bzero(th, sizeof(th));
274: bzero(um, sizeof(um));
275: switch (cpu_type) {
276: case CPU_P5:
277: fn = sel & 0x3f;
278: if ((ind = pctr_ctrfn_index(p5fn, fn)) < 0)
279: msg = "unknown function";
280: else
281: msg = p5fn[ind].name;
282: snprintf(buf, sizeof(buf), "%c%c%c %02x %s",
1.15 ! deraadt 283: sel & P5CTR_C ? 'c' : '-',
! 284: sel & P5CTR_U ? 'u' : '-',
! 285: sel & P5CTR_K ? 'k' : '-',
1.13 deraadt 286: fn, msg);
287: break;
288: case CPU_P6:
289: cfnp = p6fn;
290: case CPU_CORE:
1.14 deraadt 291: if (cpu_type == CPU_CORE)
292: cfnp = corefn;
1.13 deraadt 293: fn = sel & 0xff;
294: if ((ind = pctr_ctrfn_index(cfnp, fn)) < 0)
295: msg = "unknown function";
296: else
297: msg = cfnp[ind].name;
298: if (cfnp[ind].name && cfnp[ind].flags & CFL_MESI)
299: snprintf(um, sizeof (um), "%c%c%c%c",
1.15 ! deraadt 300: sel & PCTR_UM_M ? 'M' : '-',
! 301: sel & PCTR_UM_E ? 'E' : '-',
! 302: sel & PCTR_UM_S ? 'S' : '-',
! 303: sel & PCTR_UM_I ? 'I' : '-');
1.13 deraadt 304: else if (cfnp[ind].name && cfnp[ind].flags & CFL_SA)
305: snprintf(um, sizeof(um), "%c",
1.15 ! deraadt 306: sel & PCTR_UM_A ? 'A' : '-');
! 307: if (sel >> PCTR_CM_SHIFT)
1.13 deraadt 308: snprintf(th, sizeof(th), "+%d",
1.15 ! deraadt 309: sel >> PCTR_CM_SHIFT);
1.13 deraadt 310: snprintf(buf, sizeof(buf), "%c%c%c%c %02x %02x %s %s %s",
1.15 ! deraadt 311: sel & PCTR_I ? 'i' : '-',
! 312: sel & PCTR_E ? 'e' : '-',
! 313: sel & PCTR_K ? 'k' : '-',
! 314: sel & PCTR_U ? 'u' : '-',
! 315: fn, (sel >> PCTR_UM_SHIFT) & 0xff, th, um, msg);
1.13 deraadt 316: break;
317: case CPU_AMD:
318: fn = sel & 0xff;
1.15 ! deraadt 319: if (sel >> PCTR_CM_SHIFT)
1.13 deraadt 320: snprintf(th, sizeof(th), "+%d",
1.15 ! deraadt 321: sel >> PCTR_CM_SHIFT);
1.13 deraadt 322: snprintf(buf, sizeof(buf), "%c%c%c%c %02x %02x %s",
1.15 ! deraadt 323: sel & PCTR_I ? 'i' : '-',
! 324: sel & PCTR_E ? 'e' : '-',
! 325: sel & PCTR_K ? 'k' : '-',
! 326: sel & PCTR_U ? 'u' : '-',
! 327: fn, (sel >> PCTR_UM_SHIFT) & 0xff, th);
1.13 deraadt 328: break;
329: }
330: return (buf);
331: }
1.3 downsj 332:
1.1 dm 333: static void
1.13 deraadt 334: pctr_printvals(struct pctrst *st)
335: {
336: int i, n;
337:
338: switch (cpu_type) {
339: case CPU_P5:
340: case CPU_P6:
341: case CPU_CORE:
342: n = PCTR_INTEL_NUM;
343: case CPU_AMD:
344: if (cpu_type == CPU_AMD)
345: n = PCTR_AMD_NUM;
346: for (i = 0; i < n; i++)
347: printf(" ctr%d = %16llu [%s]\n", i, st->pctr_hwc[i],
348: pctr_fn2str(st->pctr_fn[i]));
349: if (tsc_avail)
350: printf(" tsc = %16llu\n", st->pctr_tsc);
351: printf(" idl = %16llu\n", st->pctr_idl);
352: break;
353: }
354: }
355:
356: static int
357: pctr_read(struct pctrst *st)
358: {
359: int fd, se;
360:
361: fd = open(_PATH_PCTR, O_RDONLY);
362: if (fd < 0)
363: return (-1);
364: if (ioctl(fd, PCIOCRD, st) < 0) {
365: se = errno;
366: close(fd);
367: errno = se;
368: return (-1);
369: }
370: return (close(fd));
371: }
372:
373: static int
374: pctr_write(int ctr, u_int32_t val)
375: {
376: int fd, se;
377:
378: fd = open(_PATH_PCTR, O_WRONLY);
379: if (fd < 0)
380: return (-1);
381: if (ioctl(fd, PCIOCS0 + ctr, &val) < 0) {
382: se = errno;
383: close(fd);
384: errno = se;
385: return (-1);
386: }
387: return (close(fd));
388: }
389:
390: static __inline void
391: pctr_printdesc(char *desc)
1.1 dm 392: {
1.7 mickey 393: char *p;
1.1 dm 394:
1.7 mickey 395: for (;;) {
396: while (*desc == ' ')
397: desc++;
398: if (strlen(desc) < 70) {
399: if (*desc)
400: printf(" %s\n", desc);
401: return;
402: }
403: p = desc + 72;
404: while (*--p != ' ')
405: ;
406: while (*--p == ' ')
407: ;
408: p++;
1.13 deraadt 409: printf(" %.*s\n", (int)(p-desc), desc);
1.7 mickey 410: desc = p;
411: }
1.1 dm 412: }
413:
414: static void
1.13 deraadt 415: pctr_list_fnct(void)
1.1 dm 416: {
1.13 deraadt 417: struct ctrfn *cfnp = NULL;
1.1 dm 418:
1.13 deraadt 419: if (cpu_type == CPU_P5)
1.7 mickey 420: cfnp = p5fn;
1.13 deraadt 421: else if (cpu_type == CPU_P6)
1.7 mickey 422: cfnp = p6fn;
1.13 deraadt 423: else if (cpu_type == CPU_CORE)
424: cfnp = corefn;
425: else if (cpu_type == CPU_AMD)
426: cfnp = amdfn;
427: else
428: return;
429:
1.7 mickey 430: for (; cfnp->name; cfnp++) {
431: printf("%02x %s", cfnp->fn, cfnp->name);
432: if (cfnp->flags & CFL_MESI)
1.13 deraadt 433: printf(" (MESI)");
1.7 mickey 434: else if (cfnp->flags & CFL_SA)
1.13 deraadt 435: printf(" (A)");
1.7 mickey 436: if (cfnp->flags & CFL_C0)
437: printf(" (ctr0 only)");
1.13 deraadt 438: else if (cfnp->flags & CFL_C1)
1.7 mickey 439: printf(" (ctr1 only)");
440: printf("\n");
441: if (cfnp->desc)
1.13 deraadt 442: pctr_printdesc(cfnp->desc);
1.7 mickey 443: }
1.1 dm 444: }
445:
1.13 deraadt 446: static int
447: pctr_set_cntr(void)
1.1 dm 448: {
1.13 deraadt 449: struct ctrfn *cfnp = NULL;
450: u_int32_t val = func;
451: int ind = 0;
452:
453: switch (cpu_type) {
454: case CPU_P5:
455: if (ctr >= PCTR_INTEL_NUM)
456: return (EX_DATAERR);
457: if (cflag)
1.15 ! deraadt 458: val |= P5CTR_C;
1.13 deraadt 459: if (kflag)
1.15 ! deraadt 460: val |= P5CTR_K;
1.13 deraadt 461: if (uflag)
1.15 ! deraadt 462: val |= P5CTR_U;
1.13 deraadt 463: if (func && (!kflag && !uflag))
1.15 ! deraadt 464: val |= P5CTR_K | P5CTR_U;
1.13 deraadt 465: break;
466: case CPU_P6:
1.7 mickey 467: cfnp = p6fn;
1.13 deraadt 468: case CPU_CORE:
1.14 deraadt 469: if (cpu_type == CPU_CORE)
470: cfnp = corefn;
1.13 deraadt 471: if (ctr >= PCTR_INTEL_NUM)
472: return (EX_DATAERR);
473: if (func && (ind = pctr_ctrfn_index(cfnp, func)) < 0)
474: return (EX_DATAERR);
475: if (func && cfnp[ind].flags & CFL_SA)
1.15 ! deraadt 476: val |= PCTR_UM_A;
1.13 deraadt 477: if (Mflag && cfnp[ind].flags & CFL_MESI)
1.15 ! deraadt 478: val |= PCTR_UM_M;
1.13 deraadt 479: if (Eflag && cfnp[ind].flags & CFL_MESI)
1.15 ! deraadt 480: val |= PCTR_UM_E;
1.13 deraadt 481: if (Sflag && cfnp[ind].flags & CFL_MESI)
1.15 ! deraadt 482: val |= PCTR_UM_S;
1.13 deraadt 483: if (Iflag && cfnp[ind].flags & CFL_MESI)
1.15 ! deraadt 484: val |= PCTR_UM_I;
1.13 deraadt 485: if (func && (cfnp[ind].flags & CFL_MESI) &&
486: (!Mflag || !Eflag || !Sflag || !Iflag))
1.15 ! deraadt 487: val |= PCTR_UM_MESI;
1.13 deraadt 488: if (func && (cfnp[ind].flags & CFL_ED))
1.15 ! deraadt 489: val |= PCTR_E;
1.13 deraadt 490: case CPU_AMD:
491: if (cpu_type == CPU_AMD && func &&
492: ((ind = pctr_ctrfn_index(amdfn, func)) < 0))
493: return (EX_DATAERR);
494: if (ctr >= PCTR_AMD_NUM)
495: return (EX_DATAERR);
496: if (eflag)
1.15 ! deraadt 497: val |= PCTR_E;
1.13 deraadt 498: if (iflag)
1.15 ! deraadt 499: val |= PCTR_I;
1.13 deraadt 500: if (kflag)
1.15 ! deraadt 501: val |= PCTR_K;
1.13 deraadt 502: if (uflag)
1.15 ! deraadt 503: val |= PCTR_U;
1.13 deraadt 504: if (func && (!kflag && !uflag))
1.15 ! deraadt 505: val |= PCTR_K | PCTR_U;
! 506: val |= masku << PCTR_UM_SHIFT;
! 507: val |= thold << PCTR_CM_SHIFT;
1.13 deraadt 508: if (func)
1.15 ! deraadt 509: val |= PCTR_EN;
1.13 deraadt 510: break;
511: default:
512: return (EX_UNAVAILABLE);
1.7 mickey 513: }
514:
1.13 deraadt 515: return (pctr_write(ctr, val));
1.1 dm 516: }
517:
518: static void
1.13 deraadt 519: usage(void)
1.1 dm 520: {
1.13 deraadt 521: extern char *__progname;
522: char *usg = NULL;
1.1 dm 523:
1.13 deraadt 524: switch (cpu_type) {
525: case CPU_P5:
526: usg = "[-l] [-s ctr] [-cuk] [-f funct]";
527: break;
528: case CPU_P6:
529: case CPU_CORE:
530: usg = "[-l] [-s ctr] [-eikuMESIA] [-f funct] [-m umask] "
531: "[-t thold]";
532: break;
533: case CPU_AMD:
534: usg = "[-l] [-s ctr] [-eiku] [-f funct] [-m umask] "
535: "[-t thold]";
536: break;
1.7 mickey 537: }
1.1 dm 538:
1.13 deraadt 539: fprintf(stderr, "%s: %s\n", __progname, usg);
540: exit(EX_USAGE);
1.1 dm 541: }