xref: /freebsd/usr.sbin/pmc/pmc.c (revision 32b68c46fa0d700f02e3ac69f9a89f27d48d7395)
13554f22eSMatt Macy /*-
23554f22eSMatt Macy  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
33554f22eSMatt Macy  *
43554f22eSMatt Macy  * Copyright (c) 2018, Matthew Macy
53554f22eSMatt Macy  *
63554f22eSMatt Macy  * Redistribution and use in source and binary forms, with or without
73554f22eSMatt Macy  * modification, are permitted provided that the following conditions
83554f22eSMatt Macy  * are met:
93554f22eSMatt Macy  * 1. Redistributions of source code must retain the above copyright
103554f22eSMatt Macy  *    notice, this list of conditions and the following disclaimer.
113554f22eSMatt Macy  * 2. Redistributions in binary form must reproduce the above copyright
123554f22eSMatt Macy  *    notice, this list of conditions and the following disclaimer in the
133554f22eSMatt Macy  *    documentation and/or other materials provided with the distribution.
143554f22eSMatt Macy  *
153554f22eSMatt Macy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
163554f22eSMatt Macy  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173554f22eSMatt Macy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183554f22eSMatt Macy  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
193554f22eSMatt Macy  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203554f22eSMatt Macy  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213554f22eSMatt Macy  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223554f22eSMatt Macy  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233554f22eSMatt Macy  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243554f22eSMatt Macy  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253554f22eSMatt Macy  * SUCH DAMAGE.
263554f22eSMatt Macy  *
273554f22eSMatt Macy  * $FreeBSD$
283554f22eSMatt Macy  *
293554f22eSMatt Macy  */
303554f22eSMatt Macy 
313554f22eSMatt Macy #include <sys/types.h>
323554f22eSMatt Macy #include <sys/errno.h>
333554f22eSMatt Macy #include <sys/sysctl.h>
343554f22eSMatt Macy #include <stddef.h>
353554f22eSMatt Macy #include <stdlib.h>
363554f22eSMatt Macy #include <err.h>
373554f22eSMatt Macy #include <limits.h>
383554f22eSMatt Macy #include <string.h>
393554f22eSMatt Macy #include <pmc.h>
403554f22eSMatt Macy #include <pmclog.h>
413554f22eSMatt Macy #include <libpmcstat.h>
423554f22eSMatt Macy #include <sysexits.h>
433554f22eSMatt Macy #include <unistd.h>
443554f22eSMatt Macy 
453554f22eSMatt Macy #include <libpmcstat.h>
463554f22eSMatt Macy #include "cmd_pmc.h"
473554f22eSMatt Macy 
483554f22eSMatt Macy int	pmc_displayheight = DEFAULT_DISPLAY_HEIGHT;
493554f22eSMatt Macy int	pmc_displaywidth = DEFAULT_DISPLAY_WIDTH;
503554f22eSMatt Macy int	pmc_kq;
513554f22eSMatt Macy struct pmcstat_args pmc_args;
523554f22eSMatt Macy 
533554f22eSMatt Macy struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs);
543554f22eSMatt Macy 
553554f22eSMatt Macy struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH];
563554f22eSMatt Macy 
573554f22eSMatt Macy struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH];
583554f22eSMatt Macy 
593554f22eSMatt Macy struct cmd_handler {
603554f22eSMatt Macy 	const char *ch_name;
613554f22eSMatt Macy 	cmd_disp_t ch_fn;
623554f22eSMatt Macy };
633554f22eSMatt Macy 
643554f22eSMatt Macy static struct cmd_handler disp_table[] = {
653554f22eSMatt Macy 	{"stat", cmd_pmc_stat},
663554f22eSMatt Macy 	{"stat-system", cmd_pmc_stat_system},
67c8d23c13SMatt Macy 	{"list-events", cmd_pmc_list_events},
68bfb46e2bSMatt Macy 	{"filter", cmd_pmc_filter},
69b2ca2e50SMatt Macy 	{"summary", cmd_pmc_summary},
703554f22eSMatt Macy 	{NULL, NULL}
713554f22eSMatt Macy };
723554f22eSMatt Macy 
73*32b68c46SEitan Adler static void __dead2
743554f22eSMatt Macy usage(void)
753554f22eSMatt Macy {
763554f22eSMatt Macy 	errx(EX_USAGE,
773554f22eSMatt Macy 	    "\t pmc management utility\n"
783554f22eSMatt Macy 	    "\t stat <program> run program and print stats\n"
793554f22eSMatt Macy 		 "\t stat-system <program> run program and print system wide stats for duration of execution\n"
80c8d23c13SMatt Macy 		 "\t list-events list PMC events available on host\n"
81bfb46e2bSMatt Macy 		 "\t filter filter records by lwp, pid, or event\n"
823554f22eSMatt Macy 	    );
833554f22eSMatt Macy }
843554f22eSMatt Macy 
853554f22eSMatt Macy static cmd_disp_t
863554f22eSMatt Macy disp_lookup(char *name)
873554f22eSMatt Macy {
883554f22eSMatt Macy 	struct cmd_handler *hnd;
893554f22eSMatt Macy 
903554f22eSMatt Macy 	for (hnd = disp_table; hnd->ch_name != NULL; hnd++)
913554f22eSMatt Macy 		if (strcmp(hnd->ch_name, name) == 0)
923554f22eSMatt Macy 			return (hnd->ch_fn);
933554f22eSMatt Macy 	return (NULL);
943554f22eSMatt Macy }
953554f22eSMatt Macy 
963554f22eSMatt Macy int
973554f22eSMatt Macy main(int argc, char **argv)
983554f22eSMatt Macy {
993554f22eSMatt Macy 	cmd_disp_t disp;
1003554f22eSMatt Macy 
1013554f22eSMatt Macy 	pmc_args.pa_printfile = stderr;
1023554f22eSMatt Macy 	STAILQ_INIT(&pmc_args.pa_events);
1033554f22eSMatt Macy 	SLIST_INIT(&pmc_args.pa_targets);
1043554f22eSMatt Macy 	if (argc == 1)
1053554f22eSMatt Macy 		usage();
1063554f22eSMatt Macy 	if ((disp = disp_lookup(argv[1])) == NULL)
1073554f22eSMatt Macy 		usage();
1083554f22eSMatt Macy 	argc--;
1093554f22eSMatt Macy 	argv++;
1103554f22eSMatt Macy 
1113554f22eSMatt Macy 	/* Allocate a kqueue */
1123554f22eSMatt Macy 	if ((pmc_kq = kqueue()) < 0)
1133554f22eSMatt Macy 		err(EX_OSERR, "ERROR: Cannot allocate kqueue");
1143554f22eSMatt Macy 	if (pmc_init() < 0)
1153554f22eSMatt Macy 		err(EX_UNAVAILABLE,
1163554f22eSMatt Macy 		    "ERROR: Initialization of the pmc(3) library failed"
1173554f22eSMatt Macy 		    );
1183554f22eSMatt Macy 	return (disp(argc, argv));
1193554f22eSMatt Macy }
120