]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pmc/pmc_util.c
libevent: Import libevent 2.1.12
[FreeBSD/FreeBSD.git] / usr.sbin / pmc / pmc_util.c
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/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/cpuset.h>
37 #include <sys/event.h>
38 #include <sys/queue.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 #include <sys/sysctl.h>
42 #include <sys/time.h>
43 #include <sys/ttycom.h>
44 #include <sys/user.h>
45 #include <sys/wait.h>
46
47 #include <assert.h>
48 #include <curses.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <getopt.h>
53 #include <kvm.h>
54 #include <libgen.h>
55 #include <limits.h>
56 #include <locale.h>
57 #include <math.h>
58 #include <pmc.h>
59 #include <pmclog.h>
60 #include <regex.h>
61 #include <signal.h>
62 #include <stdarg.h>
63 #include <stdint.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <sysexits.h>
68 #include <unistd.h>
69
70 #include <libpmcstat.h>
71 #include "cmd_pmc.h"
72
73 static struct pmcstat_stats pmcstat_stats;      /* statistics */
74
75 static struct pmc_plugins plugins[] = {
76         {
77                 .pl_name = "none",
78         },
79         {
80                 .pl_name = NULL
81         }
82 };
83
84 int
85 pmc_util_get_pid(struct pmcstat_args *args)
86 {
87         struct pmcstat_target *pt;
88
89         assert(args->pa_flags & FLAG_HAS_COMMANDLINE);
90
91         /*
92          * If a command line was specified, it would be the very first
93          * in the list, before any other processes specified by -t.
94          */
95         pt = SLIST_FIRST(&args->pa_targets);
96         return (pt->pt_pid);
97 }
98
99 void
100 pmc_util_kill_process(struct pmcstat_args *args)
101 {
102         struct pmcstat_target *pt;
103
104         assert(args->pa_flags & FLAG_HAS_COMMANDLINE);
105
106         /*
107          * If a command line was specified, it would be the very first
108          * in the list, before any other processes specified by -t.
109          */
110         pt = SLIST_FIRST(&args->pa_targets);
111         assert(pt != NULL);
112
113         if (kill(pt->pt_pid, SIGINT) != 0)
114                 err(EX_OSERR, "ERROR: cannot signal child process");
115 }
116
117 void
118 pmc_util_shutdown_logging(struct pmcstat_args *args)
119 {
120         pmcstat_shutdown_logging(args, plugins, &pmcstat_stats);
121 }
122
123 void
124 pmc_util_cleanup(struct pmcstat_args *args)
125 {
126         struct pmcstat_ev *ev;
127
128         /* release allocated PMCs. */
129         STAILQ_FOREACH(ev, &args->pa_events, ev_next)
130             if (ev->ev_pmcid != PMC_ID_INVALID) {
131                 if (pmc_stop(ev->ev_pmcid) < 0)
132                         err(EX_OSERR,
133                             "ERROR: cannot stop pmc 0x%x \"%s\"",
134                             ev->ev_pmcid, ev->ev_name);
135                 if (pmc_release(ev->ev_pmcid) < 0)
136                         err(EX_OSERR,
137                             "ERROR: cannot release pmc 0x%x \"%s\"",
138                             ev->ev_pmcid, ev->ev_name);
139         }
140         /* de-configure the log file if present. */
141         if (args->pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
142                 (void)pmc_configure_logfile(-1);
143
144         if (args->pa_logparser) {
145                 pmclog_close(args->pa_logparser);
146                 args->pa_logparser = NULL;
147         }
148         pmc_util_shutdown_logging(args);
149 }
150
151 void
152 pmc_util_start_pmcs(struct pmcstat_args *args)
153 {
154         struct pmcstat_ev *ev;
155
156         STAILQ_FOREACH(ev, &args->pa_events, ev_next) {
157
158                 assert(ev->ev_pmcid != PMC_ID_INVALID);
159
160                 if (pmc_start(ev->ev_pmcid) < 0) {
161                         warn("ERROR: Cannot start pmc 0x%x \"%s\"",
162                             ev->ev_pmcid, ev->ev_name);
163                         pmc_util_cleanup(args);
164                         exit(EX_OSERR);
165                 }
166         }
167 }