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 static 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 static FILE *debug_stream = NULL;
101 #define DEBUG_MSG(...) \
102 (void) fprintf(debug_stream, "[pmccontrol] " __VA_ARGS__);
104 #define DEBUG_MSG(m) /* */
108 /* log debug messages to a separate file */
110 pmcc_init_debug(void)
114 fn = getenv("PMCCONTROL_DEBUG");
117 debug_stream = fopen(fn, "w");
118 if (debug_stream == NULL)
119 debug_stream = stderr;
121 debug_stream = stderr;
126 pmcc_do_enable_disable(struct pmcc_op_list *op_list)
128 int c, error, i, j, ncpu, npmc, t;
134 if ((ncpu = pmc_ncpu()) < 0)
135 err(EX_OSERR, "Unable to determine the number of cpus");
137 /* Determine the maximum number of PMCs in any CPU. */
139 for (c = 0; c < ncpu; c++) {
140 if ((t = pmc_npmc(c)) < 0)
142 "Unable to determine the number of PMCs in CPU %d",
144 npmc = t > npmc ? t : npmc;
148 errx(EX_CONFIG, "No PMCs found");
150 if ((map = malloc(npmc * ncpu)) == NULL)
151 err(EX_SOFTWARE, "Out of memory");
153 (void) memset(map, PMCC_OP_IGNORE, npmc*ncpu);
156 STAILQ_FOREACH(np, op_list, op_next) {
163 errx(EX_DATAERR, "CPU id too large: \"%d\"", cpu);
166 errx(EX_DATAERR, "PMC id too large: \"%d\"", pmc);
168 #define MARKMAP(M,C,P,V) do { \
169 *((M) + (C)*npmc + (P)) = (V); \
172 #define SET_PMCS(C,P,V) do { \
173 if ((P) == PMCC_PMC_ALL) { \
174 for (j = 0; j < npmc; j++) \
175 MARKMAP(map, (C), j, (V)); \
177 MARKMAP(map, (C), (P), (V)); \
180 #define MAP(M,C,P) (*((M) + (C)*npmc + (P)))
182 if (cpu == PMCC_CPU_ALL)
183 for (i = 0; i < ncpu; i++) {
184 SET_PMCS(i, pmc, op);
187 SET_PMCS(cpu, pmc, op);
191 for (i = 0; i < ncpu; i++)
192 for (j = 0; j < npmc; j++) {
199 if (b == PMCC_OP_ENABLE)
200 error = pmc_enable(i, j);
201 else if (b == PMCC_OP_DISABLE)
202 error = pmc_disable(i, j);
205 err(EX_OSERR, "%s of PMC %d on CPU %d failed",
206 b == PMCC_OP_ENABLE ? "Enable" : "Disable",
214 pmcc_do_list_state(void)
216 cpuset_t logical_cpus_mask;
219 int c, cpu, n, npmc, ncpu;
221 struct pmc_pmcinfo *pi;
222 const struct pmc_cpuinfo *pc;
224 if (pmc_cpuinfo(&pc) != 0)
225 err(EX_OSERR, "Unable to determine CPU information");
227 printf("%d %s CPUs present, with %d PMCs per CPU\n", pc->pm_ncpu,
228 pmc_name_of_cputype(pc->pm_cputype),
231 /* Determine the set of logical CPUs. */
232 cpusetsize = sysconf(_SC_CPUSET_SIZE);
233 if (cpusetsize == -1 || (u_long)cpusetsize > sizeof(cpuset_t))
234 err(EX_OSERR, "Cannot determine which CPUs are logical");
235 CPU_ZERO(&logical_cpus_mask);
236 setsize = (size_t)cpusetsize;
237 if (sysctlbyname("machdep.logical_cpus_mask", &logical_cpus_mask,
238 &setsize, NULL, 0) < 0)
239 CPU_ZERO(&logical_cpus_mask);
243 for (c = cpu = 0; cpu < ncpu; cpu++) {
244 #if defined(__i386__) || defined(__amd64__)
245 if (pc->pm_cputype == PMC_CPU_INTEL_PIV &&
246 CPU_ISSET(cpu, &logical_cpus_mask))
247 continue; /* skip P4-style 'logical' cpus */
249 if (pmc_pmcinfo(cpu, &pi) < 0) {
252 err(EX_OSERR, "Unable to get PMC status for CPU %d",
256 printf("#CPU %d:\n", c++);
257 npmc = pmc_npmc(cpu);
258 printf("#N NAME CLASS STATE ROW-DISP\n");
260 for (n = 0; n < npmc; n++) {
261 pd = &pi->pm_pmcs[n];
263 printf(" %-2d %-16s %-6s %-8s %-10s",
266 pmc_name_of_class(pd->pm_class),
267 pd->pm_enabled ? "ENABLED" : "DISABLED",
268 pmc_name_of_disposition(pd->pm_rowdisp));
270 if (pd->pm_ownerpid != -1) {
271 printf(" (pid %d)", pd->pm_ownerpid);
273 pmc_name_of_event(pd->pm_event));
274 if (PMC_IS_SAMPLING_MODE(pd->pm_mode))
275 printf(" (reload count %jd)",
286 pmcc_do_list_events(void)
289 unsigned int i, j, nevents;
290 const char **eventnamelist;
291 const struct pmc_cpuinfo *ci;
293 if (pmc_cpuinfo(&ci) != 0)
294 err(EX_OSERR, "Unable to determine CPU information");
296 eventnamelist = NULL;
298 for (i = 0; i < ci->pm_nclass; i++) {
299 c = ci->pm_classes[i].pm_class;
301 printf("%s\n", pmc_name_of_class(c));
302 if (pmc_event_names_of_class(c, &eventnamelist, &nevents) < 0)
304 "ERROR: Cannot find information for event class \"%s\"",
305 pmc_name_of_class(c));
307 for (j = 0; j < nevents; j++)
308 printf("\t%s\n", eventnamelist[j]);
316 pmcc_show_statistics(void)
319 struct pmc_driverstats gms;
321 if (pmc_get_driver_stats(&gms) < 0)
322 err(EX_OSERR, "ERROR: cannot retrieve driver statistics");
328 #define PRINT(N,V) (void) printf("%-40s %d\n", (N), gms.pm_##V)
329 PRINT("interrupts processed:", intr_processed);
330 PRINT("non-PMC interrupts:", intr_ignored);
331 PRINT("sampling stalls due to space shortages:", intr_bufferfull);
332 PRINT("system calls:", syscalls);
333 PRINT("system calls with errors:", syscall_errors);
334 PRINT("buffer requests:", buffer_requests);
335 PRINT("buffer requests failed:", buffer_requests_failed);
336 PRINT("sampling log sweeps:", log_sweeps);
346 main(int argc, char **argv)
348 int error, command, currentcpu, option, pmc;
358 currentcpu = PMCC_CPU_ALL;
359 command = PMCC_PRINT_USAGE;
364 while ((option = getopt(argc, argv, ":c:d:e:lLs")) != -1)
367 if (command != PMCC_PRINT_USAGE) {
371 command = PMCC_PRINT_EVENTS;
375 if (command != PMCC_PRINT_USAGE &&
376 command != PMCC_ENABLE_DISABLE) {
380 command = PMCC_ENABLE_DISABLE;
382 if (*optarg == PMCC_CPU_WILDCARD)
383 currentcpu = PMCC_CPU_ALL;
385 currentcpu = strtoul(optarg, &dummy, 0);
386 if (*dummy != '\0' || currentcpu < 0)
388 "\"%s\" is not a valid CPU id",
395 if (command != PMCC_PRINT_USAGE &&
396 command != PMCC_ENABLE_DISABLE) {
400 command = PMCC_ENABLE_DISABLE;
402 if (*optarg == PMCC_PMC_WILDCARD)
405 pmc = strtoul(optarg, &dummy, 0);
406 if (*dummy != '\0' || pmc < 0)
408 "\"%s\" is not a valid PMC id",
412 if ((p = malloc(sizeof(*p))) == NULL)
413 err(EX_SOFTWARE, "Out of memory");
415 p->op_cpu = currentcpu;
417 p->op_op = option == 'd' ? PMCC_OP_DISABLE :
420 STAILQ_INSERT_TAIL(&head, p, op_next);
424 if (command != PMCC_PRINT_USAGE) {
428 command = PMCC_LIST_STATE;
432 if (command != PMCC_PRINT_USAGE) {
436 command = PMCC_SHOW_STATISTICS;
441 "Missing argument to option '-%c'", optopt);
445 warnx("Unrecognized option \"-%c\"", optopt);
446 errx(EX_USAGE, "%s", usage_message);
455 if (command == PMCC_PRINT_USAGE)
456 (void) errx(EX_USAGE, "%s", usage_message);
463 "Initialization of the pmc(3) library failed");
466 case PMCC_LIST_STATE:
467 error = pmcc_do_list_state();
469 case PMCC_PRINT_EVENTS:
470 error = pmcc_do_list_events();
472 case PMCC_SHOW_STATISTICS:
473 error = pmcc_show_statistics();
475 case PMCC_ENABLE_DISABLE:
476 if (STAILQ_EMPTY(&head))
478 "No PMCs specified to enable or disable");
479 error = pmcc_do_enable_disable(&head);
487 err(EX_OSERR, "Command failed");