1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/* $OpenBSD: pctrctl.c,v 1.1 1996/08/08 18:47:03 dm Exp $ */
/*
* Pentium performance counter driver for OpenBSD.
* Author: David Mazieres <dm@lcs.mit.edu>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <machine/pctr.h>
char *progname;
char *pctr_name[] = {
"Data read", /* 0 */
"Data write",
"Data TLB miss",
"Data read miss",
"Data write miss",
"Write (hit) to M or E state lines",
"Data cache lines written back",
"Data cache snoops",
"Data cache snoop hits", /* 8 */
"Memory accesses in both pipes",
"Bank conflicts",
"Misaligned data memory references",
"Code read",
"Code TLB miss",
"Code cache miss",
"Any segment register load",
NULL, /* 0x10 */
NULL,
"Branches",
"BTB hits",
"Taken branch or BTB hit",
"Pipeline flushes",
"Instructions executed",
"Instructions executed in the V-pipe",
"Bus utilization (clocks)", /* 0x18 */
"Pipeline stalled by write backup",
"Pipeline stalled by data memory read",
"Pipeline stalled by write to E or M line",
"Locked bus cycle",
"I/O read or write cycle",
"Noncacheable memory references",
"AGI (Address Generation Interlock)",
NULL, /* 0x20 */
NULL,
"Floating-point operations",
"Breakpoint 0 match",
"Breakpoint 1 match",
"Breakpoint 2 match",
"Breakpoint 3 match",
"Hardware interupts",
"Data read or data write", /* 0x28 */
"Data read miss or data write miss",
};
static const int pctr_name_size = (sizeof (pctr_name) / sizeof (char *));
/* Print all possible counter functions */
static void
list (void)
{
int i;
printf ("Hardware counter event types:\n");
for (i = 0; i < pctr_name_size; i++)
printf (" %02x %s\n", i, pctr_name[i] ? pctr_name[i] : "invalid");
}
/* Print status of counters */
static void
readst (void)
{
int fd, i;
struct pctrst st;
fd = open (_PATH_PCTR, O_RDONLY);
if (fd < 0) {
perror (_PATH_PCTR);
exit (1);
}
if (ioctl (fd, PCIOCRD, &st) < 0) {
perror ("PCIOCRD");
exit (1);
}
close (fd);
for (i = 0; i < PCTR_NUM; i++)
printf (" ctr%d = %16qd [%c%c%c %02x (%s)]\n", i, st.pctr_hwc[i],
(st.pctr_fn[i] & PCTR_C) ? 'c' : 'e',
(st.pctr_fn[i] & PCTR_U) ? 'u' : '-',
(st.pctr_fn[i] & PCTR_K) ? 'k' : '-',
(st.pctr_fn[i] & 0x3f),
(((st.pctr_fn[i] & 0x3f) < pctr_name_size
&& pctr_name[st.pctr_fn[i] & 0x3f])
? pctr_name[st.pctr_fn[i] & 0x3f] : "invalid"));
printf (" tsc = %16qd\n idl = %16qd\n", st.pctr_tsc, st.pctr_idl);
}
static void
setctr (int ctr, u_short val)
{
int fd;
fd = open (_PATH_PCTR, O_WRONLY);
if (fd < 0) {
perror (_PATH_PCTR);
exit (1);
}
if (ioctl (fd, PCIOCS0 + ctr, &val) < 0) {
perror ("PCIOCSn");
exit (1);
}
close (fd);
}
static void
usage (void)
{
fprintf (stderr,
"usage: %s [-l | -s ctr [selstr] evtype]\n"
" -l list event types\n"
" -s set counter <ctr> to monitor events of type <evtype>\n"
" <selstr> = [e|c][u][k] (default euk)\n"
" e - count number of events\n"
" c - count number of cycles\n"
" u - count events in user mode (ring 3)\n"
" k - count events in kernel mode (rings 0-2)\n",
progname);
exit (1);
}
int
main (int argc, char **argv)
{
int fd;
u_int ctr;
char *cp;
u_short fn;
if (progname = strrchr (argv[0], '/'))
progname++;
else
progname = argv[0];
if (argc <= 1)
readst ();
else if (argc == 2 && !strcmp (argv[1], "-l"))
list ();
else if (!strcmp (argv[1], "-s") && argc >= 4) {
if (argc > 5)
usage ();
ctr = atoi (argv[2]);
if (ctr >= PCTR_NUM)
usage ();
if (argc == 5) {
fn = strtoul (argv[4], NULL, 16);
if (fn & ~0x3f)
usage ();
for (cp = argv[3]; *cp; cp++) {
switch (*cp) {
case 'c':
fn |= PCTR_C;
break;
case 'e':
fn &= ~PCTR_C;
break;
case 'k':
fn |= PCTR_K;
break;
case 'u':
fn |= PCTR_U;
break;
default:
usage ();
}
}
}
else {
fn = strtoul (argv[3], NULL, 16);
if (fn & ~0x3f)
usage ();
fn |= PCTR_K | PCTR_U;
}
setctr (ctr, fn);
}
else
usage ();
return 0;
}
|