1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2018, Matthew Macy 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 * 29 */ 30 31 #include <sys/types.h> 32 #include <sys/errno.h> 33 #include <sys/sysctl.h> 34 #include <stddef.h> 35 #include <stdlib.h> 36 #include <err.h> 37 #include <limits.h> 38 #include <string.h> 39 #include <pmc.h> 40 #include <pmclog.h> 41 #include <libpmcstat.h> 42 #include <sysexits.h> 43 #include <unistd.h> 44 45 #include <libpmcstat.h> 46 #include "cmd_pmc.h" 47 48 int pmc_displayheight = DEFAULT_DISPLAY_HEIGHT; 49 int pmc_displaywidth = DEFAULT_DISPLAY_WIDTH; 50 int pmc_kq; 51 struct pmcstat_args pmc_args; 52 53 struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs); 54 55 struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH]; 56 57 struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH]; 58 59 struct cmd_handler { 60 const char *ch_name; 61 cmd_disp_t ch_fn; 62 }; 63 64 static struct cmd_handler disp_table[] = { 65 {"stat", cmd_pmc_stat}, 66 {"stat-system", cmd_pmc_stat_system}, 67 {"list-events", cmd_pmc_list_events}, 68 {NULL, NULL} 69 }; 70 71 static void 72 usage(void) 73 { 74 errx(EX_USAGE, 75 "\t pmc management utility\n" 76 "\t stat <program> run program and print stats\n" 77 "\t stat-system <program> run program and print system wide stats for duration of execution\n" 78 "\t list-events list PMC events available on host\n" 79 ); 80 } 81 82 static cmd_disp_t 83 disp_lookup(char *name) 84 { 85 struct cmd_handler *hnd; 86 87 for (hnd = disp_table; hnd->ch_name != NULL; hnd++) 88 if (strcmp(hnd->ch_name, name) == 0) 89 return (hnd->ch_fn); 90 return (NULL); 91 } 92 93 int 94 main(int argc, char **argv) 95 { 96 cmd_disp_t disp; 97 98 pmc_args.pa_printfile = stderr; 99 STAILQ_INIT(&pmc_args.pa_events); 100 SLIST_INIT(&pmc_args.pa_targets); 101 if (argc == 1) 102 usage(); 103 if ((disp = disp_lookup(argv[1])) == NULL) 104 usage(); 105 argc--; 106 argv++; 107 108 /* Allocate a kqueue */ 109 if ((pmc_kq = kqueue()) < 0) 110 err(EX_OSERR, "ERROR: Cannot allocate kqueue"); 111 if (pmc_init() < 0) 112 err(EX_UNAVAILABLE, 113 "ERROR: Initialization of the pmc(3) library failed" 114 ); 115 return (disp(argc, argv)); 116 } 117