2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003-2008, Joseph Koshy
5 * Copyright (c) 2007 The FreeBSD Foundation
7 * Copyright (c) 2018, Matthew Macy
9 * Portions of this software were developed by A. Joseph Koshy under
10 * sponsorship from the FreeBSD Foundation and Google, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/cpuset.h>
39 #include <sys/event.h>
40 #include <sys/queue.h>
41 #include <sys/socket.h>
43 #include <sys/sysctl.h>
45 #include <sys/ttycom.h>
72 #include <libpmcstat.h>
75 static struct pmcstat_stats pmcstat_stats; /* statistics */
77 static struct pmc_plugins plugins[] = {
87 pmc_util_get_pid(struct pmcstat_args *args)
89 struct pmcstat_target *pt;
91 assert(args->pa_flags & FLAG_HAS_COMMANDLINE);
94 * If a command line was specified, it would be the very first
95 * in the list, before any other processes specified by -t.
97 pt = SLIST_FIRST(&args->pa_targets);
102 pmc_util_kill_process(struct pmcstat_args *args)
104 struct pmcstat_target *pt;
106 assert(args->pa_flags & FLAG_HAS_COMMANDLINE);
109 * If a command line was specified, it would be the very first
110 * in the list, before any other processes specified by -t.
112 pt = SLIST_FIRST(&args->pa_targets);
115 if (kill(pt->pt_pid, SIGINT) != 0)
116 err(EX_OSERR, "ERROR: cannot signal child process");
120 pmc_util_shutdown_logging(struct pmcstat_args *args)
122 pmcstat_shutdown_logging(args, plugins, &pmcstat_stats);
126 pmc_util_cleanup(struct pmcstat_args *args)
128 struct pmcstat_ev *ev;
130 /* release allocated PMCs. */
131 STAILQ_FOREACH(ev, &args->pa_events, ev_next)
132 if (ev->ev_pmcid != PMC_ID_INVALID) {
133 if (pmc_stop(ev->ev_pmcid) < 0)
135 "ERROR: cannot stop pmc 0x%x \"%s\"",
136 ev->ev_pmcid, ev->ev_name);
137 if (pmc_release(ev->ev_pmcid) < 0)
139 "ERROR: cannot release pmc 0x%x \"%s\"",
140 ev->ev_pmcid, ev->ev_name);
142 /* de-configure the log file if present. */
143 if (args->pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
144 (void)pmc_configure_logfile(-1);
146 if (args->pa_logparser) {
147 pmclog_close(args->pa_logparser);
148 args->pa_logparser = NULL;
150 pmc_util_shutdown_logging(args);
154 pmc_util_start_pmcs(struct pmcstat_args *args)
156 struct pmcstat_ev *ev;
158 STAILQ_FOREACH(ev, &args->pa_events, ev_next) {
160 assert(ev->ev_pmcid != PMC_ID_INVALID);
162 if (pmc_start(ev->ev_pmcid) < 0) {
163 warn("ERROR: Cannot start pmc 0x%x \"%s\"",
164 ev->ev_pmcid, ev->ev_name);
165 pmc_util_cleanup(args);