2 * Copyright (c) 2003,2004 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/cpuset.h>
34 #include <sys/sysctl.h>
49 /* Compile time defaults */
51 #define PMCC_PRINT_USAGE 0
52 #define PMCC_PRINT_EVENTS 1
53 #define PMCC_LIST_STATE 2
54 #define PMCC_ENABLE_DISABLE 3
55 #define PMCC_SHOW_STATISTICS 4
57 #define PMCC_CPU_ALL -1
58 #define PMCC_CPU_WILDCARD '*'
60 #define PMCC_PMC_ALL -1
61 #define PMCC_PMC_WILDCARD '*'
63 #define PMCC_OP_IGNORE 0
64 #define PMCC_OP_DISABLE 1
65 #define PMCC_OP_ENABLE 2
67 #define PMCC_PROGRAM_NAME "pmccontrol"
69 STAILQ_HEAD(pmcc_op_list, pmcc_op) head = STAILQ_HEAD_INITIALIZER(head);
75 STAILQ_ENTRY(pmcc_op) op_next;
78 /* Function Prototypes */
80 static void pmcc_init_debug(void);
83 static int pmcc_do_list_state(void);
84 static int pmcc_do_enable_disable(struct pmcc_op_list *);
85 static int pmcc_do_list_events(void);
89 static char usage_message[] =
91 " " PMCC_PROGRAM_NAME " -L\n"
92 " " PMCC_PROGRAM_NAME " -l\n"
93 " " PMCC_PROGRAM_NAME " -s\n"
94 " " PMCC_PROGRAM_NAME " [-e pmc | -d pmc | -c cpu] ...";
97 FILE *debug_stream = NULL;
101 #define DEBUG_MSG(...) \
102 (void) fprintf(debug_stream, "[pmccontrol] " __VA_ARGS__);
104 #define DEBUG_MSG(m) /* */
107 int pmc_syscall = -1;
109 #define PMC_CALL(cmd, params) \
110 if ((error = syscall(pmc_syscall, PMC_OP_##cmd, (params))) != 0) \
112 DEBUG_MSG("ERROR: syscall [" #cmd "]"); \
117 /* log debug messages to a separate file */
119 pmcc_init_debug(void)
123 fn = getenv("PMCCONTROL_DEBUG");
126 debug_stream = fopen(fn, "w");
127 if (debug_stream == NULL)
128 debug_stream = stderr;
130 debug_stream = stderr;
135 pmcc_do_enable_disable(struct pmcc_op_list *op_list)
137 int c, error, i, j, ncpu, npmc, t;
143 if ((ncpu = pmc_ncpu()) < 0)
144 err(EX_OSERR, "Unable to determine the number of cpus");
146 /* Determine the maximum number of PMCs in any CPU. */
148 for (c = 0; c < ncpu; c++) {
149 if ((t = pmc_npmc(c)) < 0)
150 err(EX_OSERR, "Unable to determine the number of "
151 "PMCs in CPU %d", c);
152 npmc = t > npmc ? t : npmc;
156 errx(EX_CONFIG, "No PMCs found");
158 if ((map = malloc(npmc * ncpu)) == NULL)
159 err(EX_SOFTWARE, "Out of memory");
161 (void) memset(map, PMCC_OP_IGNORE, npmc*ncpu);
164 STAILQ_FOREACH(np, op_list, op_next) {
171 errx(EX_DATAERR, "CPU id too large: \"%d\"", cpu);
174 errx(EX_DATAERR, "PMC id too large: \"%d\"", pmc);
176 #define MARKMAP(M,C,P,V) do { \
177 *((M) + (C)*npmc + (P)) = (V); \
180 #define SET_PMCS(C,P,V) do { \
181 if ((P) == PMCC_PMC_ALL) { \
182 for (j = 0; j < npmc; j++) \
183 MARKMAP(map, (C), j, (V)); \
185 MARKMAP(map, (C), (P), (V)); \
188 #define MAP(M,C,P) (*((M) + (C)*npmc + (P)))
190 if (cpu == PMCC_CPU_ALL)
191 for (i = 0; i < ncpu; i++) {
192 SET_PMCS(i, pmc, op);
195 SET_PMCS(cpu, pmc, op);
199 for (i = 0; i < ncpu; i++)
200 for (j = 0; j < npmc; j++) {
207 if (b == PMCC_OP_ENABLE)
208 error = pmc_enable(i, j);
209 else if (b == PMCC_OP_DISABLE)
210 error = pmc_disable(i, j);
213 err(EX_OSERR, "%s of PMC %d on CPU %d failed",
214 b == PMCC_OP_ENABLE ? "Enable" :
222 pmcc_do_list_state(void)
224 cpuset_t logical_cpus_mask;
227 int c, cpu, n, npmc, ncpu;
229 struct pmc_pmcinfo *pi;
230 const struct pmc_cpuinfo *pc;
232 if (pmc_cpuinfo(&pc) != 0)
233 err(EX_OSERR, "Unable to determine CPU information");
235 printf("%d %s CPUs present, with %d PMCs per CPU\n", pc->pm_ncpu,
236 pmc_name_of_cputype(pc->pm_cputype),
239 /* Determine the set of logical CPUs. */
240 cpusetsize = sysconf(_SC_CPUSET_SIZE);
241 if (cpusetsize == -1 || (u_long)cpusetsize > sizeof(cpuset_t))
242 err(EX_OSERR, "Cannot determine which CPUs are logical");
243 CPU_ZERO(&logical_cpus_mask);
244 setsize = (size_t)cpusetsize;
245 if (sysctlbyname("machdep.logical_cpus_mask", &logical_cpus_mask,
246 &setsize, NULL, 0) < 0)
247 CPU_ZERO(&logical_cpus_mask);
251 for (c = cpu = 0; cpu < ncpu; cpu++) {
252 #if defined(__i386__) || defined(__amd64__)
253 if (pc->pm_cputype == PMC_CPU_INTEL_PIV &&
254 CPU_ISSET(cpu, &logical_cpus_mask))
255 continue; /* skip P4-style 'logical' cpus */
257 if (pmc_pmcinfo(cpu, &pi) < 0) {
260 err(EX_OSERR, "Unable to get PMC status for CPU %d",
264 printf("#CPU %d:\n", c++);
265 npmc = pmc_npmc(cpu);
266 printf("#N NAME CLASS STATE ROW-DISP\n");
268 for (n = 0; n < npmc; n++) {
269 pd = &pi->pm_pmcs[n];
271 printf(" %-2d %-16s %-6s %-8s %-10s",
274 pmc_name_of_class(pd->pm_class),
275 pd->pm_enabled ? "ENABLED" : "DISABLED",
276 pmc_name_of_disposition(pd->pm_rowdisp));
278 if (pd->pm_ownerpid != -1) {
279 printf(" (pid %d)", pd->pm_ownerpid);
281 pmc_name_of_event(pd->pm_event));
282 if (PMC_IS_SAMPLING_MODE(pd->pm_mode))
283 printf(" (reload count %jd)",
294 pmcc_do_list_events(void)
297 unsigned int i, j, nevents;
298 const char **eventnamelist;
299 const struct pmc_cpuinfo *ci;
301 if (pmc_cpuinfo(&ci) != 0)
302 err(EX_OSERR, "Unable to determine CPU information");
304 eventnamelist = NULL;
306 for (i = 0; i < ci->pm_nclass; i++) {
307 c = ci->pm_classes[i].pm_class;
309 printf("%s\n", pmc_name_of_class(c));
310 if (pmc_event_names_of_class(c, &eventnamelist, &nevents) < 0)
311 err(EX_OSERR, "ERROR: Cannot find information for "
312 "event class \"%s\"", pmc_name_of_class(c));
314 for (j = 0; j < nevents; j++)
315 printf("\t%s\n", eventnamelist[j]);
323 pmcc_show_statistics(void)
326 struct pmc_driverstats gms;
328 if (pmc_get_driver_stats(&gms) < 0)
329 err(EX_OSERR, "ERROR: cannot retrieve driver statistics");
335 #define PRINT(N,V) (void) printf("%-40s %d\n", (N), gms.pm_##V)
336 PRINT("interrupts processed:", intr_processed);
337 PRINT("non-PMC interrupts:", intr_ignored);
338 PRINT("sampling stalls due to space shortages:", intr_bufferfull);
339 PRINT("system calls:", syscalls);
340 PRINT("system calls with errors:", syscall_errors);
341 PRINT("buffer requests:", buffer_requests);
342 PRINT("buffer requests failed:", buffer_requests_failed);
343 PRINT("sampling log sweeps:", log_sweeps);
353 main(int argc, char **argv)
355 int error, command, currentcpu, option, pmc;
365 currentcpu = PMCC_CPU_ALL;
366 command = PMCC_PRINT_USAGE;
371 while ((option = getopt(argc, argv, ":c:d:e:lLs")) != -1)
374 if (command != PMCC_PRINT_USAGE) {
378 command = PMCC_PRINT_EVENTS;
382 if (command != PMCC_PRINT_USAGE &&
383 command != PMCC_ENABLE_DISABLE) {
387 command = PMCC_ENABLE_DISABLE;
389 if (*optarg == PMCC_CPU_WILDCARD)
390 currentcpu = PMCC_CPU_ALL;
392 currentcpu = strtoul(optarg, &dummy, 0);
393 if (*dummy != '\0' || currentcpu < 0)
395 "\"%s\" is not a valid CPU id",
402 if (command != PMCC_PRINT_USAGE &&
403 command != PMCC_ENABLE_DISABLE) {
407 command = PMCC_ENABLE_DISABLE;
409 if (*optarg == PMCC_PMC_WILDCARD)
412 pmc = strtoul(optarg, &dummy, 0);
413 if (*dummy != '\0' || pmc < 0)
415 "\"%s\" is not a valid PMC id",
419 if ((p = malloc(sizeof(*p))) == NULL)
420 err(EX_SOFTWARE, "Out of memory");
422 p->op_cpu = currentcpu;
424 p->op_op = option == 'd' ? PMCC_OP_DISABLE :
427 STAILQ_INSERT_TAIL(&head, p, op_next);
431 if (command != PMCC_PRINT_USAGE) {
435 command = PMCC_LIST_STATE;
439 if (command != PMCC_PRINT_USAGE) {
443 command = PMCC_SHOW_STATISTICS;
448 "Missing argument to option '-%c'", optopt);
452 warnx("Unrecognized option \"-%c\"", optopt);
453 errx(EX_USAGE, usage_message);
462 if (command == PMCC_PRINT_USAGE)
463 (void) errx(EX_USAGE, usage_message);
470 "Initialization of the pmc(3) library failed");
473 case PMCC_LIST_STATE:
474 error = pmcc_do_list_state();
476 case PMCC_PRINT_EVENTS:
477 error = pmcc_do_list_events();
479 case PMCC_SHOW_STATISTICS:
480 error = pmcc_show_statistics();
482 case PMCC_ENABLE_DISABLE:
483 if (STAILQ_EMPTY(&head))
484 errx(EX_USAGE, "No PMCs specified to enable or disable");
485 error = pmcc_do_enable_disable(&head);
493 err(EX_OSERR, "Command failed");