perfmon.c revision 253749
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: head/share/examples/perfmon/perfmon.c 253749 2013-07-28 18:35:43Z avg $ 30 */ 31 32#include <sys/types.h> 33#include <sys/ioctl.h> 34 35#include <machine/perfmon.h> 36 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40#include <err.h> 41#include <unistd.h> 42#include <fcntl.h> 43#include <limits.h> 44#include <errno.h> 45 46static int getnum(const char *, int, int); 47static void usage(const char *) __dead2; 48 49int 50main(int argc, char **argv) 51{ 52 int c, fd, num; 53 int loops, i, sleeptime; 54 char *cmd; 55 struct pmc pmc; 56 struct pmc_tstamp then, now; 57 struct pmc_data value; 58 quad_t *buf; 59 double total; 60 61 pmc.pmc_num = 0; 62 pmc.pmc_event = 0; 63 pmc.pmc_unit = 0; 64 pmc.pmc_flags = 0; 65 pmc.pmc_mask = 0; 66 cmd = NULL; 67 loops = 50; 68 sleeptime = 0; 69 70 while ((c = getopt(argc, argv, "s:l:uoeiU:m:c:")) != -1) { 71 switch(c) { 72 case 'u': 73 pmc.pmc_flags |= PMCF_USR; 74 break; 75 case 'o': 76 pmc.pmc_flags |= PMCF_OS; 77 break; 78 case 'e': 79 pmc.pmc_flags |= PMCF_E; 80 break; 81 case 'i': 82 pmc.pmc_flags |= PMCF_INV; 83 break; 84 case 'U': 85 pmc.pmc_unit = getnum(optarg, 0, 256); 86 break; 87 case 'm': 88 pmc.pmc_mask = getnum(optarg, 0, 256); 89 break; 90 case 'l': 91 loops = getnum(optarg, 1, INT_MAX - 1); 92 break; 93 case 's': 94 sleeptime = getnum(optarg, 0, INT_MAX - 1); 95 break; 96 case 'c': 97 cmd = optarg; 98 break; 99 default: 100 usage(argv[0]); 101 } 102 } 103 104 if (argc - optind != 1) 105 usage(argv[0]); 106 107 pmc.pmc_event = getnum(argv[optind], 0, 255); 108 109 buf = malloc((loops + 1) * sizeof *buf); 110 if (!buf) 111 err(1, "malloc(%lu)", (unsigned long)(loops +1) * sizeof *buf); 112 113 fd = open(_PATH_PERFMON, O_RDWR, 0); 114 if (fd < 0) 115 err(1, "open: " _PATH_PERFMON); 116 117 if (ioctl(fd, PMIOSETUP, &pmc) < 0) 118 err(1, "ioctl(PMIOSETUP)"); 119 120 if (ioctl(fd, PMIOTSTAMP, &then) < 0) 121 err(1, "ioctl(PMIOTSTAMP)"); 122 123 num = 0; 124 if (ioctl(fd, PMIOSTART, &num) < 0) 125 err(1, "ioctl(PMIOSTART)"); 126 127 value.pmcd_num = 0; 128 for (i = 0; i < loops; i++) { 129 if (ioctl(fd, PMIOSTOP, &num) < 0) 130 err(1, "ioctl(PMIOSTOP)"); 131 if (ioctl(fd, PMIOREAD, &value) < 0) 132 err(1, "ioctl(PMIOREAD)"); 133 buf[i] = value.pmcd_value; 134 if (ioctl(fd, PMIORESET, &value.pmcd_num) < 0) 135 err(1, "ioctl(PMIORESET)"); 136 if (ioctl(fd, PMIOSTART, &num) < 0) 137 err(1, "ioctl(PMIOSTART)"); 138 if (sleeptime) 139 sleep(sleeptime); 140 if (cmd) 141 system(cmd); 142 } 143 144 if (ioctl(fd, PMIOSTOP, &num) < 0) 145 err(1, "ioctl(PMIOSTOP)"); 146 if (ioctl(fd, PMIOREAD, &value) < 0) 147 err(1, "ioctl(PMIOREAD)"); 148 buf[i] = value.pmcd_value; 149 if (ioctl(fd, PMIOTSTAMP, &now) < 0) 150 err(1, "ioctl(PMIOTSTAMP)"); 151 152 total = 0; 153 for (i = 1; i <= loops; i++) { 154 printf("%d: %qd\n", i, buf[i]); 155 total += buf[i]; 156 } 157 printf("total: %f\nmean: %f\n", total, total / loops); 158 159 printf("clocks (at %d-MHz): %qd\n", now.pmct_rate, 160 now.pmct_value - then.pmct_value); 161 162 return 0; 163} 164 165static int 166getnum(const char *buf, int min, int max) 167{ 168 char *ep; 169 long l; 170 171 errno = 0; 172 l = strtol(buf, &ep, 0); 173 if (*buf && !*ep && !errno) { 174 if (l < min || l > max) { 175 errx(1, "%s: must be between %d and %d", 176 buf, min, max); 177 } 178 return (int)l; 179 } 180 181 errx(1, "%s: parameter must be an integer", buf); 182} 183 184static void 185usage(const char *pname) 186{ 187 fprintf(stderr, 188 "usage: %s [-eiou] [-c command] [-l nloops] [-m mask] [-s sleeptime]\n" 189 " [-U unit] counter\n", 190 pname); 191 exit(1); 192} 193