summaryrefslogtreecommitdiffstats
path: root/share/examples/perfmon/perfmon.c
blob: 274a828af99ce8a870fd94b4afd6d243e87164c4 (plain)
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
/*
 * Copyright 1996 Massachusetts Institute of Technology
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that both the above copyright notice and this
 * permission notice appear in all copies, that both the above
 * copyright notice and this permission notice appear in all
 * supporting documentation, and that the name of M.I.T. not be used
 * in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  M.I.T. makes
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 * 
 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$Id$
 */

#include <sys/types.h>
#include <sys/ioctl.h>

#include <machine/cpu.h>
#include <machine/perfmon.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>

static int getnum(const char *, int, int);
static void __dead usage(const char *) __dead2;

int
main(int argc, char **argv)
{
	int c, fd, num;
	int loops, i, sleeptime;
	struct pmc pmc;
	struct pmc_tstamp then, now;
	struct pmc_data value;
	quad_t *buf;
	double total;

	pmc.pmc_num = 0;
	pmc.pmc_event = 0;
	pmc.pmc_unit = 0;
	pmc.pmc_flags = 0;
	pmc.pmc_mask = 0;
	loops = 50;
	sleeptime = 0;

	while ((c = getopt(argc, argv, "s:l:uoeiU:m:")) != EOF) {
		switch(c) {
		case 'u':
			pmc.pmc_flags |= PMCF_USR;
			break;
		case 'o':
			pmc.pmc_flags |= PMCF_OS;
			break;
		case 'e':
			pmc.pmc_flags |= PMCF_E;
			break;
		case 'i':
			pmc.pmc_flags |= PMCF_INV;
			break;
		case 'U':
			pmc.pmc_unit = getnum(optarg, 0, 256);
			break;
		case 'm':
			pmc.pmc_mask = getnum(optarg, 0, 256);
			break;
		case 'l':
			loops = getnum(optarg, 1, INT_MAX - 1);
			break;
		case 's':
			sleeptime = getnum(optarg, 0, INT_MAX - 1);
			break;
		default:
			usage(argv[0]);
		}
	}

	if (argc - optind != 1)
		usage(argv[0]);

	pmc.pmc_event = getnum(argv[optind], 0, 255);

	buf = malloc((loops + 1) * sizeof *buf);
	if (!buf)
		err(1, "malloc(%lu)", (unsigned long)(loops +1) * sizeof *buf);

	fd = open(_PATH_PERFMON, O_RDWR, 0);
	if (fd < 0)
		err(1, "open: " _PATH_PERFMON);

	if (ioctl(fd, PMIOSETUP, &pmc) < 0)
		err(1, "ioctl(PMIOSETUP)");

	if (ioctl(fd, PMIOTSTAMP, &then) < 0)
		err(1, "ioctl(PMIOTSTAMP)");

	num = 0;
	if (ioctl(fd, PMIOSTART, &num) < 0)
		err(1, "ioctl(PMIOSTART)");

	value.pmcd_num = 0;
	for (i = 0; i < loops; i++) {
		if (sleeptime)
			sleep(sleeptime);
		if (ioctl(fd, PMIOSTOP, &num) < 0)
			err(1, "ioctl(PMIOSTOP)");
		if (ioctl(fd, PMIOREAD, &value) < 0)
			err(1, "ioctl(PMIOREAD)");
		buf[i] = value.pmcd_value;
		if (ioctl(fd, PMIORESET, &value.pmcd_num) < 0)
			err(1, "ioctl(PMIORESET)");
		if (ioctl(fd, PMIOSTART, &num) < 0)
			err(1, "ioctl(PMIOSTART)");
	}

	if (ioctl(fd, PMIOSTOP, &num) < 0)
		err(1, "ioctl(PMIOSTOP)");
	if (ioctl(fd, PMIOREAD, &value) < 0)
		err(1, "ioctl(PMIOREAD)");
	buf[i] = value.pmcd_value;
	if (ioctl(fd, PMIOTSTAMP, &now) < 0)
		err(1, "ioctl(PMIOTSTAMP)");

	total = 0;
	for (i = 1; i <= loops; i++) {
		printf("%d: %qd\n", i, buf[i]);
		total += buf[i];
	}
	printf("total: %f\nmean: %f\n", total, total / loops);

	printf("clocks (at %d-MHz): %qd\n", now.pmct_rate,
	       now.pmct_value - then.pmct_value);

	return 0;
}

static int
getnum(const char *buf, int min, int max)
{
	char *ep;
	long l;

	errno = 0;
	l = strtol(buf, &ep, 0);
	if (*buf && !*ep && !errno) {
		if (l < min || l > max) {
			errx(1, "`%s': must be between %d and %d", 
			     buf, min, max);
		}
		return (int)l;
	} else if(errno) {
		errx(1, "`%s': must be between %ld and %ld", 
		     LONG_MIN, LONG_MAX);
	}
	errx(1, "`%s': parameter must be an integer");
}

static void
usage(const char *pname)
{
	fprintf(stderr, 
		"%s: usage:\n\t%s [-eiou] [-l nloops] [-U unit] [-m mask] "
		"counter\n", pname, pname);
	exit(1);
}
OpenPOWER on IntegriCloud