1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003-2008, Joseph Koshy
5 * Copyright (c) 2007 The FreeBSD Foundation
6 * All rights reserved.
7 * Copyright (c) 2018, Matthew Macy
8 *
9 * Portions of this software were developed by A. Joseph Koshy under
10 * sponsorship from the FreeBSD Foundation and Google, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/cpuset.h>
36 #include <sys/event.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include <sys/sysctl.h>
41 #include <sys/time.h>
42 #include <sys/ttycom.h>
43 #include <sys/user.h>
44 #include <sys/wait.h>
45
46 #include <assert.h>
47 #include <curses.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <getopt.h>
52 #include <kvm.h>
53 #include <libgen.h>
54 #include <limits.h>
55 #include <locale.h>
56 #include <math.h>
57 #include <pmc.h>
58 #include <pmclog.h>
59 #include <regex.h>
60 #include <signal.h>
61 #include <stdarg.h>
62 #include <stdint.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <sysexits.h>
67 #include <unistd.h>
68
69 #include <libpmcstat.h>
70 #include "cmd_pmc.h"
71
72 static struct pmcstat_stats pmcstat_stats; /* statistics */
73
74 static struct pmc_plugins plugins[] = {
75 {
76 .pl_name = "none",
77 },
78 {
79 .pl_name = NULL
80 }
81 };
82
83 int
pmc_util_get_pid(struct pmcstat_args * args)84 pmc_util_get_pid(struct pmcstat_args *args)
85 {
86 struct pmcstat_target *pt;
87
88 assert(args->pa_flags & FLAG_HAS_COMMANDLINE);
89
90 /*
91 * If a command line was specified, it would be the very first
92 * in the list, before any other processes specified by -t.
93 */
94 pt = SLIST_FIRST(&args->pa_targets);
95 return (pt->pt_pid);
96 }
97
98 void
pmc_util_kill_process(struct pmcstat_args * args)99 pmc_util_kill_process(struct pmcstat_args *args)
100 {
101 struct pmcstat_target *pt;
102
103 assert(args->pa_flags & FLAG_HAS_COMMANDLINE);
104
105 /*
106 * If a command line was specified, it would be the very first
107 * in the list, before any other processes specified by -t.
108 */
109 pt = SLIST_FIRST(&args->pa_targets);
110 assert(pt != NULL);
111
112 if (kill(pt->pt_pid, SIGINT) != 0)
113 err(EX_OSERR, "ERROR: cannot signal child process");
114 }
115
116 void
pmc_util_shutdown_logging(struct pmcstat_args * args)117 pmc_util_shutdown_logging(struct pmcstat_args *args)
118 {
119 pmcstat_shutdown_logging(args, plugins, &pmcstat_stats);
120 }
121
122 void
pmc_util_cleanup(struct pmcstat_args * args)123 pmc_util_cleanup(struct pmcstat_args *args)
124 {
125 struct pmcstat_ev *ev;
126
127 /* release allocated PMCs. */
128 STAILQ_FOREACH(ev, &args->pa_events, ev_next)
129 if (ev->ev_pmcid != PMC_ID_INVALID) {
130 if (pmc_stop(ev->ev_pmcid) < 0)
131 err(EX_OSERR,
132 "ERROR: cannot stop pmc 0x%x \"%s\"",
133 ev->ev_pmcid, ev->ev_name);
134 if (pmc_release(ev->ev_pmcid) < 0)
135 err(EX_OSERR,
136 "ERROR: cannot release pmc 0x%x \"%s\"",
137 ev->ev_pmcid, ev->ev_name);
138 }
139 /* de-configure the log file if present. */
140 if (args->pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
141 (void)pmc_configure_logfile(-1);
142
143 if (args->pa_logparser) {
144 pmclog_close(args->pa_logparser);
145 args->pa_logparser = NULL;
146 }
147 pmc_util_shutdown_logging(args);
148 }
149
150 void
pmc_util_start_pmcs(struct pmcstat_args * args)151 pmc_util_start_pmcs(struct pmcstat_args *args)
152 {
153 struct pmcstat_ev *ev;
154
155 STAILQ_FOREACH(ev, &args->pa_events, ev_next) {
156
157 assert(ev->ev_pmcid != PMC_ID_INVALID);
158
159 if (pmc_start(ev->ev_pmcid) < 0) {
160 warn("ERROR: Cannot start pmc 0x%x \"%s\"",
161 ev->ev_pmcid, ev->ev_name);
162 pmc_util_cleanup(args);
163 exit(EX_OSERR);
164 }
165 }
166 }
167