Remove some ports specific references.
[dragonfly.git] / share / examples / perfmon / perfmon.c
1 /*
2  * Copyright 1996 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/share/examples/perfmon/perfmon.c,v 1.5 1999/08/28 00:19:26 peter Exp $
30  * $DragonFly: src/share/examples/perfmon/perfmon.c,v 1.2 2003/06/17 04:36:57 dillon Exp $
31  */
32
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35
36 #include <machine/cpu.h>
37 #include <machine/perfmon.h>
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <err.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <limits.h>
46 #include <errno.h>
47
48 static int getnum(const char *, int, int);
49 static void usage(const char *) __dead2;
50
51 int
52 main(int argc, char **argv)
53 {
54         int c, fd, num;
55         int loops, i, sleeptime;
56         char *cmd;
57         struct pmc pmc;
58         struct pmc_tstamp then, now;
59         struct pmc_data value;
60         quad_t *buf;
61         double total;
62
63         pmc.pmc_num = 0;
64         pmc.pmc_event = 0;
65         pmc.pmc_unit = 0;
66         pmc.pmc_flags = 0;
67         pmc.pmc_mask = 0;
68         cmd = NULL;
69         loops = 50;
70         sleeptime = 0;
71
72         while ((c = getopt(argc, argv, "s:l:uoeiU:m:c:")) != -1) {
73                 switch(c) {
74                 case 'u':
75                         pmc.pmc_flags |= PMCF_USR;
76                         break;
77                 case 'o':
78                         pmc.pmc_flags |= PMCF_OS;
79                         break;
80                 case 'e':
81                         pmc.pmc_flags |= PMCF_E;
82                         break;
83                 case 'i':
84                         pmc.pmc_flags |= PMCF_INV;
85                         break;
86                 case 'U':
87                         pmc.pmc_unit = getnum(optarg, 0, 256);
88                         break;
89                 case 'm':
90                         pmc.pmc_mask = getnum(optarg, 0, 256);
91                         break;
92                 case 'l':
93                         loops = getnum(optarg, 1, INT_MAX - 1);
94                         break;
95                 case 's':
96                         sleeptime = getnum(optarg, 0, INT_MAX - 1);
97                         break;
98                 case 'c':
99                         cmd = optarg;
100                         break;
101                 default:
102                         usage(argv[0]);
103                 }
104         }
105
106         if (argc - optind != 1)
107                 usage(argv[0]);
108
109         pmc.pmc_event = getnum(argv[optind], 0, 255);
110
111         buf = malloc((loops + 1) * sizeof *buf);
112         if (!buf)
113                 err(1, "malloc(%lu)", (unsigned long)(loops +1) * sizeof *buf);
114
115         fd = open(_PATH_PERFMON, O_RDWR, 0);
116         if (fd < 0)
117                 err(1, "open: " _PATH_PERFMON);
118
119         if (ioctl(fd, PMIOSETUP, &pmc) < 0)
120                 err(1, "ioctl(PMIOSETUP)");
121
122         if (ioctl(fd, PMIOTSTAMP, &then) < 0)
123                 err(1, "ioctl(PMIOTSTAMP)");
124
125         num = 0;
126         if (ioctl(fd, PMIOSTART, &num) < 0)
127                 err(1, "ioctl(PMIOSTART)");
128
129         value.pmcd_num = 0;
130         for (i = 0; i < loops; i++) {
131                 if (ioctl(fd, PMIOSTOP, &num) < 0)
132                         err(1, "ioctl(PMIOSTOP)");
133                 if (ioctl(fd, PMIOREAD, &value) < 0)
134                         err(1, "ioctl(PMIOREAD)");
135                 buf[i] = value.pmcd_value;
136                 if (ioctl(fd, PMIORESET, &value.pmcd_num) < 0)
137                         err(1, "ioctl(PMIORESET)");
138                 if (ioctl(fd, PMIOSTART, &num) < 0)
139                         err(1, "ioctl(PMIOSTART)");
140                 if (sleeptime)
141                         sleep(sleeptime);
142                 if (cmd)
143                         system(cmd);
144         }
145
146         if (ioctl(fd, PMIOSTOP, &num) < 0)
147                 err(1, "ioctl(PMIOSTOP)");
148         if (ioctl(fd, PMIOREAD, &value) < 0)
149                 err(1, "ioctl(PMIOREAD)");
150         buf[i] = value.pmcd_value;
151         if (ioctl(fd, PMIOTSTAMP, &now) < 0)
152                 err(1, "ioctl(PMIOTSTAMP)");
153
154         total = 0;
155         for (i = 1; i <= loops; i++) {
156                 printf("%d: %qd\n", i, buf[i]);
157                 total += buf[i];
158         }
159         printf("total: %f\nmean: %f\n", total, total / loops);
160
161         printf("clocks (at %d-MHz): %qd\n", now.pmct_rate,
162                now.pmct_value - then.pmct_value);
163
164         return 0;
165 }
166
167 static int
168 getnum(const char *buf, int min, int max)
169 {
170         char *ep;
171         long l;
172
173         errno = 0;
174         l = strtol(buf, &ep, 0);
175         if (*buf && !*ep && !errno) {
176                 if (l < min || l > max) {
177                         errx(1, "`%s': must be between %d and %d", 
178                              buf, min, max);
179                 }
180                 return (int)l;
181         } else if(errno) {
182                 errx(1, "`%s': must be between %ld and %ld", 
183                      LONG_MIN, LONG_MAX);
184         }
185         errx(1, "`%s': parameter must be an integer");
186 }
187
188 static void
189 usage(const char *pname)
190 {
191         fprintf(stderr, 
192         "usage: %s [-eiou] [-c command] [-l nloops] [-m mask] [-s sleeptime]\n"
193         "       [-U unit] counter\n",
194                 pname);
195         exit(1);
196 }