13554f22eSMatt Macy /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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 */
283554f22eSMatt Macy
293554f22eSMatt Macy #include <sys/types.h>
303554f22eSMatt Macy #include <sys/errno.h>
313554f22eSMatt Macy #include <sys/sysctl.h>
323554f22eSMatt Macy #include <stddef.h>
333554f22eSMatt Macy #include <stdlib.h>
343554f22eSMatt Macy #include <err.h>
353554f22eSMatt Macy #include <limits.h>
363554f22eSMatt Macy #include <string.h>
373554f22eSMatt Macy #include <pmc.h>
383554f22eSMatt Macy #include <pmclog.h>
393554f22eSMatt Macy #include <libpmcstat.h>
403554f22eSMatt Macy #include <sysexits.h>
413554f22eSMatt Macy #include <unistd.h>
423554f22eSMatt Macy
433554f22eSMatt Macy #include <libpmcstat.h>
443554f22eSMatt Macy #include "cmd_pmc.h"
453554f22eSMatt Macy
463554f22eSMatt Macy int pmc_displayheight = DEFAULT_DISPLAY_HEIGHT;
473554f22eSMatt Macy int pmc_displaywidth = DEFAULT_DISPLAY_WIDTH;
483554f22eSMatt Macy int pmc_kq;
493554f22eSMatt Macy struct pmcstat_args pmc_args;
503554f22eSMatt Macy
513554f22eSMatt Macy struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs);
523554f22eSMatt Macy
533554f22eSMatt Macy struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH];
543554f22eSMatt Macy
553554f22eSMatt Macy struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH];
563554f22eSMatt Macy
573554f22eSMatt Macy struct cmd_handler {
583554f22eSMatt Macy const char *ch_name;
593554f22eSMatt Macy cmd_disp_t ch_fn;
603554f22eSMatt Macy };
613554f22eSMatt Macy
623554f22eSMatt Macy static struct cmd_handler disp_table[] = {
633554f22eSMatt Macy {"stat", cmd_pmc_stat},
643554f22eSMatt Macy {"stat-system", cmd_pmc_stat_system},
65c8d23c13SMatt Macy {"list-events", cmd_pmc_list_events},
66bfb46e2bSMatt Macy {"filter", cmd_pmc_filter},
67b2ca2e50SMatt Macy {"summary", cmd_pmc_summary},
683554f22eSMatt Macy {NULL, NULL}
693554f22eSMatt Macy };
703554f22eSMatt Macy
7132b68c46SEitan Adler static void __dead2
usage(void)723554f22eSMatt Macy usage(void)
733554f22eSMatt Macy {
743554f22eSMatt Macy errx(EX_USAGE,
753554f22eSMatt Macy "\t pmc management utility\n"
763554f22eSMatt Macy "\t stat <program> run program and print stats\n"
773554f22eSMatt Macy "\t stat-system <program> run program and print system wide stats for duration of execution\n"
78c8d23c13SMatt Macy "\t list-events list PMC events available on host\n"
79bfb46e2bSMatt Macy "\t filter filter records by lwp, pid, or event\n"
803554f22eSMatt Macy );
813554f22eSMatt Macy }
823554f22eSMatt Macy
833554f22eSMatt Macy static cmd_disp_t
disp_lookup(char * name)843554f22eSMatt Macy disp_lookup(char *name)
853554f22eSMatt Macy {
863554f22eSMatt Macy struct cmd_handler *hnd;
873554f22eSMatt Macy
883554f22eSMatt Macy for (hnd = disp_table; hnd->ch_name != NULL; hnd++)
893554f22eSMatt Macy if (strcmp(hnd->ch_name, name) == 0)
903554f22eSMatt Macy return (hnd->ch_fn);
913554f22eSMatt Macy return (NULL);
923554f22eSMatt Macy }
933554f22eSMatt Macy
943554f22eSMatt Macy int
main(int argc,char ** argv)953554f22eSMatt Macy main(int argc, char **argv)
963554f22eSMatt Macy {
973554f22eSMatt Macy cmd_disp_t disp;
983554f22eSMatt Macy
993554f22eSMatt Macy pmc_args.pa_printfile = stderr;
1003554f22eSMatt Macy STAILQ_INIT(&pmc_args.pa_events);
1013554f22eSMatt Macy SLIST_INIT(&pmc_args.pa_targets);
1023554f22eSMatt Macy if (argc == 1)
1033554f22eSMatt Macy usage();
1043554f22eSMatt Macy if ((disp = disp_lookup(argv[1])) == NULL)
1053554f22eSMatt Macy usage();
1063554f22eSMatt Macy argc--;
1073554f22eSMatt Macy argv++;
1083554f22eSMatt Macy
1093554f22eSMatt Macy /* Allocate a kqueue */
1103554f22eSMatt Macy if ((pmc_kq = kqueue()) < 0)
1113554f22eSMatt Macy err(EX_OSERR, "ERROR: Cannot allocate kqueue");
1123554f22eSMatt Macy if (pmc_init() < 0)
1133554f22eSMatt Macy err(EX_UNAVAILABLE,
1143554f22eSMatt Macy "ERROR: Initialization of the pmc(3) library failed"
1153554f22eSMatt Macy );
1163554f22eSMatt Macy return (disp(argc, argv));
1173554f22eSMatt Macy }
118