pmccontrol.c revision 213691
180708Sjake/*-
280708Sjake * Copyright (c) 2003,2004 Joseph Koshy
380708Sjake * All rights reserved.
480708Sjake *
580708Sjake * Redistribution and use in source and binary forms, with or without
680708Sjake * modification, are permitted provided that the following conditions
780708Sjake * are met:
880708Sjake * 1. Redistributions of source code must retain the above copyright
980708Sjake *    notice, this list of conditions and the following disclaimer.
1080708Sjake * 2. Redistributions in binary form must reproduce the above copyright
1180708Sjake *    notice, this list of conditions and the following disclaimer in the
1280708Sjake *    documentation and/or other materials provided with the distribution.
1380708Sjake *
1481337Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1580708Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1680708Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1781337Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1880708Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1980708Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2080708Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2180708Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2280708Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2380708Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2480708Sjake * SUCH DAMAGE.
2580708Sjake *
2680708Sjake */
27180664Smarius
28180664Smarius#include <sys/cdefs.h>
29180664Smarius__FBSDID("$FreeBSD: head/usr.sbin/pmccontrol/pmccontrol.c 213691 2010-10-11 14:31:24Z gnn $");
3086227Stmm
3186227Stmm#include <sys/types.h>
3280708Sjake#include <sys/queue.h>
3380709Sjake#include <sys/sysctl.h>
3480709Sjake
3580709Sjake#include <assert.h>
3680709Sjake#include <err.h>
3780708Sjake#include <errno.h>
3886227Stmm#include <fcntl.h>
3986227Stmm#include <limits.h>
4086227Stmm#include <pmc.h>
4186227Stmm#include <stdarg.h>
4286227Stmm#include <stdio.h>
4388368Stmm#include <stdlib.h>
4480709Sjake#include <string.h>
45143765Siedowse#include <sysexits.h>
46143765Siedowse#include <unistd.h>
47143765Siedowse
4880709Sjake/* Compile time defaults */
49143765Siedowse
50143765Siedowse#define	PMCC_PRINT_USAGE	0
51143765Siedowse#define	PMCC_PRINT_EVENTS	1
52143765Siedowse#define	PMCC_LIST_STATE 	2
53143765Siedowse#define	PMCC_ENABLE_DISABLE	3
5480709Sjake#define	PMCC_SHOW_STATISTICS	4
55143765Siedowse
56143765Siedowse#define	PMCC_CPU_ALL		-1
57143765Siedowse#define	PMCC_CPU_WILDCARD	'*'
5880709Sjake
59143765Siedowse#define	PMCC_PMC_ALL		-1
6080709Sjake#define	PMCC_PMC_WILDCARD	'*'
61146794Smarcel
62146794Smarcel#define	PMCC_OP_IGNORE		0
63143765Siedowse#define	PMCC_OP_DISABLE		1
6486227Stmm#define	PMCC_OP_ENABLE		2
65143765Siedowse
66143765Siedowse#define	PMCC_PROGRAM_NAME	"pmccontrol"
67143765Siedowse
68143765SiedowseSTAILQ_HEAD(pmcc_op_list, pmcc_op) head = STAILQ_HEAD_INITIALIZER(head);
6980709Sjake
7086227Stmmstruct pmcc_op {
71182688Smarius	char	op_cpu;
72182688Smarius	char	op_pmc;
73182688Smarius	char	op_op;
74182688Smarius	STAILQ_ENTRY(pmcc_op) op_next;
75182688Smarius};
76182688Smarius
7786227Stmm/* Function Prototypes */
78182688Smarius#if	DEBUG
7986227Stmmstatic void	pmcc_init_debug(void);
80143765Siedowse#endif
81143765Siedowse
82143765Siedowsestatic int	pmcc_do_list_state(void);
83143765Siedowsestatic int	pmcc_do_enable_disable(struct pmcc_op_list *);
84143765Siedowsestatic int	pmcc_do_list_events(void);
85143765Siedowse
86146792Smarcel/* Globals */
8780709Sjake
8880709Sjakestatic char usage_message[] =
89	"Usage:\n"
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] ...";
94
95#if DEBUG
96FILE *debug_stream = NULL;
97#endif
98
99#if DEBUG
100#define DEBUG_MSG(...)					                \
101	(void) fprintf(debug_stream, "[pmccontrol] " __VA_ARGS__);
102#else
103#define DEBUG_MSG(m)		/*  */
104#endif /* !DEBUG */
105
106int pmc_syscall = -1;
107
108#define PMC_CALL(cmd, params)						\
109if ((error = syscall(pmc_syscall, PMC_OP_##cmd, (params))) != 0)	\
110{									\
111	DEBUG_MSG("ERROR: syscall [" #cmd "]");				\
112	exit(EX_OSERR);							\
113}
114
115#if DEBUG
116/* log debug messages to a separate file */
117static void
118pmcc_init_debug(void)
119{
120	char *fn;
121
122	fn = getenv("PMCCONTROL_DEBUG");
123	if (fn != NULL)
124	{
125		debug_stream = fopen(fn, "w");
126		if (debug_stream == NULL)
127			debug_stream = stderr;
128	} else
129		debug_stream = stderr;
130}
131#endif
132
133static int
134pmcc_do_enable_disable(struct pmcc_op_list *op_list)
135{
136	int c, error, i, j, ncpu, npmc, t;
137	cpumask_t haltedcpus, cpumask;
138	struct pmcc_op *np;
139	unsigned char *map;
140	unsigned char op;
141	int cpu, pmc;
142	size_t dummy;
143
144	if ((ncpu = pmc_ncpu()) < 0)
145		err(EX_OSERR, "Unable to determine the number of cpus");
146
147	/* Determine the set of active CPUs. */
148	cpumask = (1 << ncpu) - 1;
149	dummy = sizeof(int);
150	haltedcpus = (cpumask_t) 0;
151	if (ncpu > 1 && sysctlbyname("machdep.hlt_cpus", &haltedcpus,
152	    &dummy, NULL, 0) < 0)
153		err(EX_OSERR, "ERROR: Cannot determine which CPUs are "
154		    "halted");
155	cpumask &= ~haltedcpus;
156
157	/* Determine the maximum number of PMCs in any CPU. */
158	npmc = 0;
159	for (c = 0; c < ncpu; c++) {
160		if ((t = pmc_npmc(c)) < 0)
161			err(EX_OSERR, "Unable to determine the number of "
162			    "PMCs in CPU %d", c);
163		npmc = t > npmc ? t : npmc;
164	}
165
166	if (npmc == 0)
167		errx(EX_CONFIG, "No PMCs found");
168
169	if ((map = malloc(npmc * ncpu)) == NULL)
170		err(EX_SOFTWARE, "Out of memory");
171
172	(void) memset(map, PMCC_OP_IGNORE, npmc*ncpu);
173
174	error = 0;
175	STAILQ_FOREACH(np, op_list, op_next) {
176
177		cpu = np->op_cpu;
178		pmc = np->op_pmc;
179		op  = np->op_op;
180
181		if (cpu >= ncpu)
182			errx(EX_DATAERR, "CPU id too large: \"%d\"", cpu);
183
184		if (pmc >= npmc)
185			errx(EX_DATAERR, "PMC id too large: \"%d\"", pmc);
186
187#define MARKMAP(M,C,P,V)	do {				\
188		*((M) + (C)*npmc + (P)) = (V);			\
189} while (0)
190
191#define	SET_PMCS(C,P,V)		do {				\
192		if ((P) == PMCC_PMC_ALL) {			\
193			for (j = 0; j < npmc; j++)		\
194				MARKMAP(map, (C), j, (V));	\
195		} else						\
196			MARKMAP(map, (C), (P), (V));		\
197} while (0)
198
199#define MAP(M,C,P)	(*((M) + (C)*npmc + (P)))
200
201		if (cpu == PMCC_CPU_ALL)
202			for (i = 0; i < ncpu; i++) {
203				if ((1 << i) & cpumask)
204					SET_PMCS(i, pmc, op);
205			}
206		else
207			SET_PMCS(cpu, pmc, op);
208	}
209
210	/* Configure PMCS */
211	for (i = 0; i < ncpu; i++)
212		for (j = 0; j < npmc; j++) {
213			unsigned char b;
214
215			b = MAP(map, i, j);
216
217			error = 0;
218
219			if (b == PMCC_OP_ENABLE)
220				error = pmc_enable(i, j);
221			else if (b == PMCC_OP_DISABLE)
222				error = pmc_disable(i, j);
223
224			if (error < 0)
225				err(EX_OSERR, "%s of PMC %d on CPU %d failed",
226				    b == PMCC_OP_ENABLE ? "Enable" :
227				    "Disable", j, i);
228		}
229
230	return error;
231}
232
233static int
234pmcc_do_list_state(void)
235{
236	size_t dummy;
237	int c, cpu, n, npmc, ncpu;
238	unsigned int logical_cpus_mask;
239	struct pmc_info *pd;
240	struct pmc_pmcinfo *pi;
241	const struct pmc_cpuinfo *pc;
242
243	if (pmc_cpuinfo(&pc) != 0)
244		err(EX_OSERR, "Unable to determine CPU information");
245
246	printf("%d %s CPUs present, with %d PMCs per CPU\n", pc->pm_ncpu,
247	       pmc_name_of_cputype(pc->pm_cputype),
248		pc->pm_npmc);
249
250	dummy = sizeof(logical_cpus_mask);
251	if (sysctlbyname("machdep.logical_cpus_mask", &logical_cpus_mask,
252		&dummy, NULL, 0) < 0)
253		logical_cpus_mask = 0;
254
255	ncpu = pc->pm_ncpu;
256
257	for (c = cpu = 0; cpu < ncpu; cpu++) {
258#if	defined(__i386__) || defined(__amd64__)
259		if (pc->pm_cputype == PMC_CPU_INTEL_PIV &&
260		    (logical_cpus_mask & (1 << cpu)))
261			continue; /* skip P4-style 'logical' cpus */
262#endif
263		if (pmc_pmcinfo(cpu, &pi) < 0) {
264			if (errno == ENXIO)
265				continue;
266			err(EX_OSERR, "Unable to get PMC status for CPU %d",
267			    cpu);
268		}
269
270		printf("#CPU %d:\n", c++);
271		npmc = pmc_npmc(cpu);
272		printf("#N  NAME             CLASS  STATE    ROW-DISP\n");
273
274		for (n = 0; n < npmc; n++) {
275			pd = &pi->pm_pmcs[n];
276
277			printf(" %-2d %-16s %-6s %-8s %-10s",
278			    n,
279			    pd->pm_name,
280			    pmc_name_of_class(pd->pm_class),
281			    pd->pm_enabled ? "ENABLED" : "DISABLED",
282			    pmc_name_of_disposition(pd->pm_rowdisp));
283
284			if (pd->pm_ownerpid != -1) {
285			        printf(" (pid %d)", pd->pm_ownerpid);
286				printf(" %-32s",
287				    pmc_name_of_event(pd->pm_event));
288				if (PMC_IS_SAMPLING_MODE(pd->pm_mode))
289					printf(" (reload count %jd)",
290					    pd->pm_reloadcount);
291			}
292			printf("\n");
293		}
294		free(pi);
295	}
296	return 0;
297}
298
299static int
300pmcc_do_list_events(void)
301{
302	enum pmc_class c;
303	unsigned int i, j, nevents;
304	const char **eventnamelist;
305	const struct pmc_cpuinfo *ci;
306
307	if (pmc_cpuinfo(&ci) != 0)
308		err(EX_OSERR, "Unable to determine CPU information");
309
310	eventnamelist = NULL;
311
312	for (i = 0; i < ci->pm_nclass; i++) {
313		c = ci->pm_classes[i].pm_class;
314
315		printf("%s\n", pmc_name_of_class(c));
316		if (pmc_event_names_of_class(c, &eventnamelist, &nevents) < 0)
317			err(EX_OSERR, "ERROR: Cannot find information for "
318			    "event class \"%s\"", pmc_name_of_class(c));
319
320		for (j = 0; j < nevents; j++)
321			printf("\t%s\n", eventnamelist[j]);
322
323		free(eventnamelist);
324	}
325	return 0;
326}
327
328static int
329pmcc_show_statistics(void)
330{
331
332	struct pmc_driverstats gms;
333
334	if (pmc_get_driver_stats(&gms) < 0)
335		err(EX_OSERR, "ERROR: cannot retrieve driver statistics");
336
337	/*
338	 * Print statistics.
339	 */
340
341#define	PRINT(N,V)	(void) printf("%-40s %d\n", (N), gms.pm_##V)
342	PRINT("interrupts processed:", intr_processed);
343	PRINT("non-PMC interrupts:", intr_ignored);
344	PRINT("sampling stalls due to space shortages:", intr_bufferfull);
345	PRINT("system calls:", syscalls);
346	PRINT("system calls with errors:", syscall_errors);
347	PRINT("buffer requests:", buffer_requests);
348	PRINT("buffer requests failed:", buffer_requests_failed);
349	PRINT("sampling log sweeps:", log_sweeps);
350
351	return 0;
352}
353
354/*
355 * Main
356 */
357
358int
359main(int argc, char **argv)
360{
361	int error, command, currentcpu, option, pmc;
362	char *dummy;
363	struct pmcc_op *p;
364
365#if DEBUG
366	pmcc_init_debug();
367#endif
368
369	/* parse args */
370
371	currentcpu = PMCC_CPU_ALL;
372	command    = PMCC_PRINT_USAGE;
373	error      = 0;
374
375	STAILQ_INIT(&head);
376
377	while ((option = getopt(argc, argv, ":c:d:e:lLs")) != -1)
378		switch (option) {
379		case 'L':
380			if (command != PMCC_PRINT_USAGE) {
381				error = 1;
382				break;
383			}
384			command = PMCC_PRINT_EVENTS;
385			break;
386
387		case 'c':
388			if (command != PMCC_PRINT_USAGE &&
389			    command != PMCC_ENABLE_DISABLE) {
390				error = 1;
391				break;
392			}
393			command = PMCC_ENABLE_DISABLE;
394
395			if (*optarg == PMCC_CPU_WILDCARD)
396				currentcpu = PMCC_CPU_ALL;
397			else {
398				currentcpu = strtoul(optarg, &dummy, 0);
399				if (*dummy != '\0' || currentcpu < 0)
400					errx(EX_DATAERR,
401					    "\"%s\" is not a valid CPU id",
402					    optarg);
403			}
404			break;
405
406		case 'd':
407		case 'e':
408			if (command != PMCC_PRINT_USAGE &&
409			    command != PMCC_ENABLE_DISABLE) {
410				error = 1;
411				break;
412			}
413			command = PMCC_ENABLE_DISABLE;
414
415			if (*optarg == PMCC_PMC_WILDCARD)
416				pmc = PMCC_PMC_ALL;
417			else {
418				pmc = strtoul(optarg, &dummy, 0);
419				if (*dummy != '\0' || pmc < 0)
420					errx(EX_DATAERR,
421					    "\"%s\" is not a valid PMC id",
422					    optarg);
423			}
424
425			if ((p = malloc(sizeof(*p))) == NULL)
426				err(EX_SOFTWARE, "Out of memory");
427
428			p->op_cpu = currentcpu;
429			p->op_pmc = pmc;
430			p->op_op  = option == 'd' ? PMCC_OP_DISABLE :
431			    PMCC_OP_ENABLE;
432
433			STAILQ_INSERT_TAIL(&head, p, op_next);
434			break;
435
436		case 'l':
437			if (command != PMCC_PRINT_USAGE) {
438				error = 1;
439				break;
440			}
441			command = PMCC_LIST_STATE;
442			break;
443
444		case 's':
445			if (command != PMCC_PRINT_USAGE) {
446				error = 1;
447				break;
448			}
449			command = PMCC_SHOW_STATISTICS;
450			break;
451
452		case ':':
453			errx(EX_USAGE,
454			    "Missing argument to option '-%c'", optopt);
455			break;
456
457		case '?':
458			warnx("Unrecognized option \"-%c\"", optopt);
459			errx(EX_USAGE, usage_message);
460			break;
461
462		default:
463			error = 1;
464			break;
465
466		}
467
468	if (command == PMCC_PRINT_USAGE)
469		(void) errx(EX_USAGE, usage_message);
470
471	if (error)
472		exit(EX_USAGE);
473
474	if (pmc_init() < 0)
475		err(EX_UNAVAILABLE,
476		    "Initialization of the pmc(3) library failed");
477
478	switch (command) {
479	case PMCC_LIST_STATE:
480		error = pmcc_do_list_state();
481		break;
482	case PMCC_PRINT_EVENTS:
483		error = pmcc_do_list_events();
484		break;
485	case PMCC_SHOW_STATISTICS:
486		error = pmcc_show_statistics();
487		break;
488	case PMCC_ENABLE_DISABLE:
489		if (STAILQ_EMPTY(&head))
490			errx(EX_USAGE, "No PMCs specified to enable or disable");
491		error = pmcc_do_enable_disable(&head);
492		break;
493	default:
494		assert(0);
495
496	}
497
498	if (error != 0)
499		err(EX_OSERR, "Command failed");
500	exit(0);
501}
502