2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2003,2004 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/cpuset.h>
33 #include <sys/sysctl.h>
48 /* Compile time defaults */
50 #define PMCC_PRINT_USAGE 0
51 #define PMCC_PRINT_EVENTS 1
52 #define PMCC_LIST_STATE 2
53 #define PMCC_ENABLE_DISABLE 3
54 #define PMCC_SHOW_STATISTICS 4
56 #define PMCC_CPU_ALL -1
57 #define PMCC_CPU_WILDCARD '*'
59 #define PMCC_PMC_ALL -1
60 #define PMCC_PMC_WILDCARD '*'
62 #define PMCC_OP_IGNORE 0
63 #define PMCC_OP_DISABLE 1
64 #define PMCC_OP_ENABLE 2
66 #define PMCC_PROGRAM_NAME "pmccontrol"
68 static STAILQ_HEAD(pmcc_op_list, pmcc_op) head = STAILQ_HEAD_INITIALIZER(head);
74 STAILQ_ENTRY(pmcc_op) op_next;
77 /* Function Prototypes */
79 static void pmcc_init_debug(void);
82 static int pmcc_do_list_state(void);
83 static int pmcc_do_enable_disable(struct pmcc_op_list *);
84 static int pmcc_do_list_events(void);
88 static char usage_message[] =
90 " " PMCC_PROGRAM_NAME " -L\n"
91 " " PMCC_PROGRAM_NAME " -l\n"
92 " " PMCC_PROGRAM_NAME " -s\n"
93 " " PMCC_PROGRAM_NAME " [-e pmc | -d pmc | -c cpu] ...";
96 static FILE *debug_stream = NULL;
100 #define DEBUG_MSG(...) \
101 (void) fprintf(debug_stream, "[pmccontrol] " __VA_ARGS__)
103 #define DEBUG_MSG(m) /* */
107 /* log debug messages to a separate file */
109 pmcc_init_debug(void)
113 fn = getenv("PMCCONTROL_DEBUG");
116 debug_stream = fopen(fn, "w");
117 if (debug_stream == NULL)
118 debug_stream = stderr;
120 debug_stream = stderr;
125 pmcc_do_enable_disable(struct pmcc_op_list *op_list)
127 int c, error, i, j, ncpu, npmc, t;
133 if ((ncpu = pmc_ncpu()) < 0)
134 err(EX_OSERR, "Unable to determine the number of cpus");
136 /* Determine the maximum number of PMCs in any CPU. */
138 for (c = 0; c < ncpu; c++) {
139 if ((t = pmc_npmc(c)) < 0)
141 "Unable to determine the number of PMCs in CPU %d",
147 errx(EX_CONFIG, "No PMCs found");
149 if ((map = calloc(npmc, ncpu)) == NULL)
150 err(EX_SOFTWARE, "Out of memory");
153 STAILQ_FOREACH(np, op_list, op_next) {
160 errx(EX_DATAERR, "CPU id too large: \"%d\"", cpu);
163 errx(EX_DATAERR, "PMC id too large: \"%d\"", pmc);
165 #define MARKMAP(M,C,P,V) do { \
166 *((M) + (C)*npmc + (P)) = (V); \
169 #define SET_PMCS(C,P,V) do { \
170 if ((P) == PMCC_PMC_ALL) { \
171 for (j = 0; j < npmc; j++) \
172 MARKMAP(map, (C), j, (V)); \
174 MARKMAP(map, (C), (P), (V)); \
177 #define MAP(M,C,P) (*((M) + (C)*npmc + (P)))
179 if (cpu == PMCC_CPU_ALL)
180 for (i = 0; i < ncpu; i++) {
181 SET_PMCS(i, pmc, op);
184 SET_PMCS(cpu, pmc, op);
188 for (i = 0; i < ncpu; i++)
189 for (j = 0; j < npmc; j++) {
196 if (b == PMCC_OP_ENABLE)
197 error = pmc_enable(i, j);
198 else if (b == PMCC_OP_DISABLE)
199 error = pmc_disable(i, j);
202 err(EX_OSERR, "%s of PMC %d on CPU %d failed",
203 b == PMCC_OP_ENABLE ? "Enable" : "Disable",
211 pmcc_do_list_state(void)
213 cpuset_t logical_cpus_mask;
216 int c, cpu, n, npmc, ncpu;
218 struct pmc_pmcinfo *pi;
219 const struct pmc_cpuinfo *pc;
221 if (pmc_cpuinfo(&pc) != 0)
222 err(EX_OSERR, "Unable to determine CPU information");
224 printf("%d %s CPUs present, with %d PMCs per CPU\n", pc->pm_ncpu,
225 pmc_name_of_cputype(pc->pm_cputype),
228 /* Determine the set of logical CPUs. */
229 cpusetsize = sysconf(_SC_CPUSET_SIZE);
230 if (cpusetsize == -1 || (u_long)cpusetsize > sizeof(cpuset_t))
231 err(EX_OSERR, "Cannot determine which CPUs are logical");
232 CPU_ZERO(&logical_cpus_mask);
233 setsize = (size_t)cpusetsize;
234 if (sysctlbyname("machdep.logical_cpus_mask", &logical_cpus_mask,
235 &setsize, NULL, 0) < 0)
236 CPU_ZERO(&logical_cpus_mask);
240 for (c = cpu = 0; cpu < ncpu; cpu++) {
241 if (pmc_pmcinfo(cpu, &pi) < 0) {
244 err(EX_OSERR, "Unable to get PMC status for CPU %d",
248 printf("#CPU %d:\n", c++);
249 npmc = pmc_npmc(cpu);
250 printf("#N NAME CLASS STATE ROW-DISP\n");
252 for (n = 0; n < npmc; n++) {
253 pd = &pi->pm_pmcs[n];
255 printf(" %-2d %-16s %-6s %-8s %-10s",
258 pmc_name_of_class(pd->pm_class),
259 pd->pm_enabled ? "ENABLED" : "DISABLED",
260 pmc_name_of_disposition(pd->pm_rowdisp));
262 if (pd->pm_ownerpid != -1) {
263 printf(" (pid %d)", pd->pm_ownerpid);
265 pmc_name_of_event(pd->pm_event));
266 if (PMC_IS_SAMPLING_MODE(pd->pm_mode))
267 printf(" (reload count %jd)",
278 pmcc_do_list_events(void)
281 unsigned int i, j, nevents;
282 const char **eventnamelist;
283 const struct pmc_cpuinfo *ci;
285 /* First, try pmu events. */
286 if (pmc_pmu_enabled()) {
287 pmc_pmu_print_counters(NULL);
291 /* Otherwise, use the legacy pmc(3) interfaces. */
292 if (pmc_cpuinfo(&ci) != 0)
293 err(EX_OSERR, "Unable to determine CPU information");
295 eventnamelist = NULL;
297 for (i = 0; i < ci->pm_nclass; i++) {
298 c = ci->pm_classes[i].pm_class;
300 printf("%s\n", pmc_name_of_class(c));
301 if (pmc_event_names_of_class(c, &eventnamelist, &nevents) < 0)
303 "ERROR: Cannot find information for event class \"%s\"",
304 pmc_name_of_class(c));
306 for (j = 0; j < nevents; j++)
307 printf("\t%s\n", eventnamelist[j]);
315 pmcc_show_statistics(void)
318 struct pmc_driverstats gms;
320 if (pmc_get_driver_stats(&gms) < 0)
321 err(EX_OSERR, "ERROR: cannot retrieve driver statistics");
327 #define PRINT(N,V) (void) printf("%-40s %d\n", (N), gms.pm_##V)
328 PRINT("interrupts processed:", intr_processed);
329 PRINT("non-PMC interrupts:", intr_ignored);
330 PRINT("sampling stalls due to space shortages:", intr_bufferfull);
331 PRINT("system calls:", syscalls);
332 PRINT("system calls with errors:", syscall_errors);
333 PRINT("buffer requests:", buffer_requests);
334 PRINT("buffer requests failed:", buffer_requests_failed);
335 PRINT("sampling log sweeps:", log_sweeps);
345 main(int argc, char **argv)
347 int error, command, currentcpu, option, pmc;
357 currentcpu = PMCC_CPU_ALL;
358 command = PMCC_PRINT_USAGE;
363 while ((option = getopt(argc, argv, ":c:d:e:lLs")) != -1)
366 if (command != PMCC_PRINT_USAGE) {
370 command = PMCC_PRINT_EVENTS;
374 if (command != PMCC_PRINT_USAGE &&
375 command != PMCC_ENABLE_DISABLE) {
379 command = PMCC_ENABLE_DISABLE;
381 if (*optarg == PMCC_CPU_WILDCARD)
382 currentcpu = PMCC_CPU_ALL;
384 currentcpu = strtoul(optarg, &dummy, 0);
385 if (*dummy != '\0' || currentcpu < 0)
387 "\"%s\" is not a valid CPU id",
394 if (command != PMCC_PRINT_USAGE &&
395 command != PMCC_ENABLE_DISABLE) {
399 command = PMCC_ENABLE_DISABLE;
401 if (*optarg == PMCC_PMC_WILDCARD)
404 pmc = strtoul(optarg, &dummy, 0);
405 if (*dummy != '\0' || pmc < 0)
407 "\"%s\" is not a valid PMC id",
411 if ((p = malloc(sizeof(*p))) == NULL)
412 err(EX_SOFTWARE, "Out of memory");
414 p->op_cpu = currentcpu;
416 p->op_op = option == 'd' ? PMCC_OP_DISABLE :
419 STAILQ_INSERT_TAIL(&head, p, op_next);
423 if (command != PMCC_PRINT_USAGE) {
427 command = PMCC_LIST_STATE;
431 if (command != PMCC_PRINT_USAGE) {
435 command = PMCC_SHOW_STATISTICS;
440 "Missing argument to option '-%c'", optopt);
444 warnx("Unrecognized option \"-%c\"", optopt);
445 errx(EX_USAGE, "%s", usage_message);
454 if (command == PMCC_PRINT_USAGE)
455 (void) errx(EX_USAGE, "%s", usage_message);
462 "Initialization of the pmc(3) library failed");
465 case PMCC_LIST_STATE:
466 error = pmcc_do_list_state();
468 case PMCC_PRINT_EVENTS:
469 error = pmcc_do_list_events();
471 case PMCC_SHOW_STATISTICS:
472 error = pmcc_show_statistics();
474 case PMCC_ENABLE_DISABLE:
475 if (STAILQ_EMPTY(&head))
477 "No PMCs specified to enable or disable");
478 error = pmcc_do_enable_disable(&head);
486 err(EX_OSERR, "Command failed");