profile.c revision 1.1 1 1.1 christos /* Copyright (C) 2021 Free Software Foundation, Inc.
2 1.1 christos Contributed by Oracle.
3 1.1 christos
4 1.1 christos This file is part of GNU Binutils.
5 1.1 christos
6 1.1 christos This program is free software; you can redistribute it and/or modify
7 1.1 christos it under the terms of the GNU General Public License as published by
8 1.1 christos the Free Software Foundation; either version 3, or (at your option)
9 1.1 christos any later version.
10 1.1 christos
11 1.1 christos This program is distributed in the hope that it will be useful,
12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 christos GNU General Public License for more details.
15 1.1 christos
16 1.1 christos You should have received a copy of the GNU General Public License
17 1.1 christos along with this program; if not, write to the Free Software
18 1.1 christos Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 1.1 christos MA 02110-1301, USA. */
20 1.1 christos
21 1.1 christos /*
22 1.1 christos * Profile handling
23 1.1 christos *
24 1.1 christos * Note: SIGPROF signal-handling and interval timer (once exclusive to
25 1.1 christos * profile handling) are now common services provided by the dispatcher.
26 1.1 christos */
27 1.1 christos
28 1.1 christos #include "config.h"
29 1.1 christos #include <dlfcn.h>
30 1.1 christos #include <stdlib.h>
31 1.1 christos #include <string.h>
32 1.1 christos #include <ucontext.h>
33 1.1 christos #include <unistd.h>
34 1.1 christos
35 1.1 christos #include "gp-defs.h"
36 1.1 christos #include "collector_module.h"
37 1.1 christos #include "gp-experiment.h"
38 1.1 christos #include "data_pckts.h"
39 1.1 christos #include "libcol_util.h"
40 1.1 christos #include "hwprofile.h"
41 1.1 christos #include "tsd.h"
42 1.1 christos
43 1.1 christos /* TprintfT(<level>,...) definitions. Adjust per module as needed */
44 1.1 christos #define DBG_LT0 0 // for high-level configuration, unexpected errors/warnings
45 1.1 christos #define DBG_LT1 1 // for configuration details, warnings
46 1.1 christos #define DBG_LT2 2
47 1.1 christos #define DBG_LT3 3
48 1.1 christos
49 1.1 christos static int init_interface (CollectorInterface*);
50 1.1 christos static int open_experiment (const char *);
51 1.1 christos static int start_data_collection (void);
52 1.1 christos static int stop_data_collection (void);
53 1.1 christos static int close_experiment (void);
54 1.1 christos static int detach_experiment (void);
55 1.1 christos
56 1.1 christos static ModuleInterface module_interface ={
57 1.1 christos SP_PROFILE_FILE, /* description */
58 1.1 christos init_interface, /* initInterface */
59 1.1 christos open_experiment, /* openExperiment */
60 1.1 christos start_data_collection, /* startDataCollection */
61 1.1 christos stop_data_collection, /* stopDataCollection */
62 1.1 christos close_experiment, /* closeExperiment */
63 1.1 christos detach_experiment /* detachExperiment (fork child) */
64 1.1 christos };
65 1.1 christos
66 1.1 christos static CollectorInterface *collector_interface = NULL;
67 1.1 christos static int prof_mode = 0;
68 1.1 christos static CollectorModule prof_hndl = COLLECTOR_MODULE_ERR;
69 1.1 christos static unsigned prof_key = COLLECTOR_TSD_INVALID_KEY;
70 1.1 christos
71 1.1 christos typedef struct ClockPacket
72 1.1 christos { /* clock profiling packet */
73 1.1 christos CM_Packet comm;
74 1.1 christos pthread_t lwp_id;
75 1.1 christos pthread_t thr_id;
76 1.1 christos uint32_t cpu_id;
77 1.1 christos hrtime_t tstamp __attribute__ ((packed));
78 1.1 christos uint64_t frinfo __attribute__ ((packed));
79 1.1 christos int mstate; /* kernel microstate */
80 1.1 christos int nticks; /* number of ticks in that state */
81 1.1 christos } ClockPacket;
82 1.1 christos
83 1.1 christos /* XXX should be able to use local types */
84 1.1 christos #define CLOCK_TYPE OPROF_PCKT
85 1.1 christos
86 1.1 christos #define CHCK_REENTRANCE(x) ( !prof_mode || ((x) = collector_interface->getKey( prof_key )) == NULL || (*(x) != 0) )
87 1.1 christos #define PUSH_REENTRANCE(x) ((*(x))++)
88 1.1 christos #define POP_REENTRANCE(x) ((*(x))--)
89 1.1 christos
90 1.1 christos #ifdef DEBUG
91 1.1 christos #define Tprintf(...) if (collector_interface) collector_interface->writeDebugInfo( 0, __VA_ARGS__ )
92 1.1 christos #define TprintfT(...) if (collector_interface) collector_interface->writeDebugInfo( 1, __VA_ARGS__ )
93 1.1 christos #else
94 1.1 christos #define Tprintf(...)
95 1.1 christos #define TprintfT(...)
96 1.1 christos #endif
97 1.1 christos
98 1.1 christos static void init_module () __attribute__ ((constructor));
99 1.1 christos
100 1.1 christos static void
101 1.1 christos init_module ()
102 1.1 christos {
103 1.1 christos __collector_dlsym_guard = 1;
104 1.1 christos RegModuleFunc reg_module = (RegModuleFunc) dlsym (RTLD_DEFAULT, "__collector_register_module");
105 1.1 christos __collector_dlsym_guard = 0;
106 1.1 christos if (reg_module == NULL)
107 1.1 christos {
108 1.1 christos TprintfT (0, "clockprof: init_module FAILED -- reg_module = NULL\n");
109 1.1 christos return;
110 1.1 christos }
111 1.1 christos prof_hndl = reg_module (&module_interface);
112 1.1 christos if (prof_hndl == COLLECTOR_MODULE_ERR && collector_interface != NULL)
113 1.1 christos {
114 1.1 christos Tprintf (0, "clockprof: ERROR: handle not created.\n");
115 1.1 christos collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">data handle not created</event>\n", SP_JCMD_CERROR, COL_ERROR_PROFINIT);
116 1.1 christos }
117 1.1 christos TprintfT (0, "clockprof: init_module, prof_hndl = %d\n", prof_hndl);
118 1.1 christos return;
119 1.1 christos }
120 1.1 christos
121 1.1 christos static int
122 1.1 christos init_interface (CollectorInterface *_collector_interface)
123 1.1 christos {
124 1.1 christos collector_interface = _collector_interface;
125 1.1 christos return COL_ERROR_NONE;
126 1.1 christos }
127 1.1 christos
128 1.1 christos static int
129 1.1 christos open_experiment (const char *exp)
130 1.1 christos {
131 1.1 christos if (collector_interface == NULL)
132 1.1 christos {
133 1.1 christos Tprintf (0, "clockprof: ERROR: collector_interface is null.\n");
134 1.1 christos return COL_ERROR_PROFINIT;
135 1.1 christos }
136 1.1 christos const char *params = collector_interface->getParams ();
137 1.1 christos while (params)
138 1.1 christos {
139 1.1 christos if (__collector_strStartWith (params, "p:") == 0)
140 1.1 christos {
141 1.1 christos params += 2;
142 1.1 christos break;
143 1.1 christos }
144 1.1 christos while (*params != 0 && *params != ';')
145 1.1 christos params++;
146 1.1 christos if (*params == 0)
147 1.1 christos params = NULL;
148 1.1 christos else
149 1.1 christos params++;
150 1.1 christos }
151 1.1 christos if (params == NULL) /* Clock profiling not specified */
152 1.1 christos return COL_ERROR_PROFINIT;
153 1.1 christos TprintfT (0, "clockprof: open_experiment %s -- %s\n", exp, params);
154 1.1 christos int prof_interval = CALL_UTIL (strtol)(params, NULL, 0);
155 1.1 christos prof_key = collector_interface->createKey (sizeof ( int), NULL, NULL);
156 1.1 christos if (prof_key == (unsigned) - 1)
157 1.1 christos {
158 1.1 christos Tprintf (0, "clockprof: ERROR: TSD key create failed.\n");
159 1.1 christos collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">TSD key not created</event>\n", SP_JCMD_CERROR, COL_ERROR_PROFINIT);
160 1.1 christos return COL_ERROR_PROFINIT;
161 1.1 christos }
162 1.1 christos
163 1.1 christos /* set dispatcher interval timer period used for all timed activities */
164 1.1 christos int prof_interval_actual = __collector_ext_itimer_set (prof_interval);
165 1.1 christos TprintfT (0, "clockprof: open_experiment(): __collector_ext_itimer_set (actual period=%d, req_period=%d)\n",
166 1.1 christos prof_interval_actual, prof_interval);
167 1.1 christos if (prof_interval_actual <= 0)
168 1.1 christos {
169 1.1 christos collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">itimer could not be set</event>\n", SP_JCMD_CERROR, COL_ERROR_PROFINIT);
170 1.1 christos return COL_ERROR_PROFINIT;
171 1.1 christos }
172 1.1 christos if ((prof_interval_actual >= (prof_interval + prof_interval / 10)) ||
173 1.1 christos (prof_interval_actual <= (prof_interval - prof_interval / 10)))
174 1.1 christos collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">%d -> %d</event>\n", SP_JCMD_CWARN, COL_WARN_PROFRND, prof_interval, prof_interval_actual);
175 1.1 christos else if (prof_interval_actual != prof_interval)
176 1.1 christos collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">%d -> %d</event>\n", SP_JCMD_COMMENT, COL_WARN_PROFRND, prof_interval, prof_interval_actual);
177 1.1 christos prof_interval = prof_interval_actual;
178 1.1 christos collector_interface->writeLog ("<profile name=\"%s\" ptimer=\"%d\" numstates=\"%d\">\n",
179 1.1 christos SP_JCMD_PROFILE, prof_interval, LMS_MAGIC_ID_LINUX);
180 1.1 christos collector_interface->writeLog (" <profdata fname=\"%s\"/>\n",
181 1.1 christos module_interface.description);
182 1.1 christos
183 1.1 christos /* Record Profile packet description */
184 1.1 christos ClockPacket *cp = NULL;
185 1.1 christos collector_interface->writeLog (" <profpckt kind=\"%d\" uname=\"" STXT ("Clock profiling data") "\">\n", CLOCK_TYPE);
186 1.1 christos collector_interface->writeLog (" <field name=\"LWPID\" uname=\"" STXT ("Lightweight process id") "\" offset=\"%d\" type=\"%s\"/>\n",
187 1.1 christos &cp->lwp_id, sizeof (cp->lwp_id) == 4 ? "INT32" : "INT64");
188 1.1 christos collector_interface->writeLog (" <field name=\"THRID\" uname=\"" STXT ("Thread number") "\" offset=\"%d\" type=\"%s\"/>\n",
189 1.1 christos &cp->thr_id, sizeof (cp->thr_id) == 4 ? "INT32" : "INT64");
190 1.1 christos collector_interface->writeLog (" <field name=\"CPUID\" uname=\"" STXT ("CPU id") "\" offset=\"%d\" type=\"%s\"/>\n",
191 1.1 christos &cp->cpu_id, sizeof (cp->cpu_id) == 4 ? "INT32" : "INT64");
192 1.1 christos collector_interface->writeLog (" <field name=\"TSTAMP\" uname=\"" STXT ("High resolution timestamp") "\" offset=\"%d\" type=\"%s\"/>\n",
193 1.1 christos &cp->tstamp, sizeof (cp->tstamp) == 4 ? "INT32" : "INT64");
194 1.1 christos collector_interface->writeLog (" <field name=\"FRINFO\" offset=\"%d\" type=\"%s\"/>\n",
195 1.1 christos &cp->frinfo, sizeof (cp->frinfo) == 4 ? "INT32" : "INT64");
196 1.1 christos collector_interface->writeLog (" <field name=\"MSTATE\" uname=\"" STXT ("Thread state") "\" offset=\"%d\" type=\"%s\"/>\n",
197 1.1 christos &cp->mstate, sizeof (cp->mstate) == 4 ? "INT32" : "INT64");
198 1.1 christos collector_interface->writeLog (" <field name=\"NTICK\" uname=\"" STXT ("Duration") "\" offset=\"%d\" type=\"%s\"/>\n",
199 1.1 christos &cp->nticks, sizeof (cp->nticks) == 4 ? "INT32" : "INT64");
200 1.1 christos collector_interface->writeLog (" </profpckt>\n");
201 1.1 christos collector_interface->writeLog ("</profile>\n");
202 1.1 christos return COL_ERROR_NONE;
203 1.1 christos }
204 1.1 christos
205 1.1 christos static int
206 1.1 christos start_data_collection (void)
207 1.1 christos {
208 1.1 christos TprintfT (0, "clockprof: start_data_collection\n");
209 1.1 christos prof_mode = 1;
210 1.1 christos return 0;
211 1.1 christos }
212 1.1 christos
213 1.1 christos static int
214 1.1 christos stop_data_collection (void)
215 1.1 christos {
216 1.1 christos prof_mode = 0;
217 1.1 christos TprintfT (0, "clockprof: stop_data_collection\n");
218 1.1 christos return 0;
219 1.1 christos }
220 1.1 christos
221 1.1 christos static int
222 1.1 christos close_experiment (void)
223 1.1 christos {
224 1.1 christos prof_mode = 0;
225 1.1 christos prof_key = COLLECTOR_TSD_INVALID_KEY;
226 1.1 christos TprintfT (0, "clockprof: close_experiment\n");
227 1.1 christos return 0;
228 1.1 christos }
229 1.1 christos
230 1.1 christos /* fork child. Clean up state but don't write to experiment */
231 1.1 christos static int
232 1.1 christos detach_experiment (void)
233 1.1 christos {
234 1.1 christos prof_mode = 0;
235 1.1 christos prof_key = COLLECTOR_TSD_INVALID_KEY;
236 1.1 christos TprintfT (0, "clockprof: detach_experiment\n");
237 1.1 christos return 0;
238 1.1 christos }
239 1.1 christos
240 1.1 christos /*
241 1.1 christos * void collector_lost_profile_context
242 1.1 christos * Placeholder/marker function used when profiling given NULL context.
243 1.1 christos */
244 1.1 christos void
245 1.1 christos __collector_lost_profile_context (void) { }
246 1.1 christos
247 1.1 christos /*
248 1.1 christos * void __collector_ext_profile_handler( siginfo_t *info, ucontext_t *context )
249 1.1 christos * Handle real profile events to collect profile data.
250 1.1 christos */
251 1.1 christos void
252 1.1 christos __collector_ext_profile_handler (siginfo_t *info, ucontext_t *context)
253 1.1 christos {
254 1.1 christos int *guard;
255 1.1 christos if (!prof_mode) /* sigprof timer running only because hwprofile.c needs it */
256 1.1 christos return;
257 1.1 christos if (CHCK_REENTRANCE (guard))
258 1.1 christos {
259 1.1 christos TprintfT (0, "__collector_ext_profile_handler: ERROR: prof_mode=%d guard=%d!\n",
260 1.1 christos prof_mode, guard ? *guard : -2);
261 1.1 christos return;
262 1.1 christos }
263 1.1 christos PUSH_REENTRANCE (guard);
264 1.1 christos TprintfT (DBG_LT3, "__collector_ext_profile_handler\n");
265 1.1 christos ucontext_t uctxmem;
266 1.1 christos if (context == NULL)
267 1.1 christos {
268 1.1 christos /* assume this case is rare, and accept overhead of creating dummy_uc */
269 1.1 christos TprintfT (0, "collector_profile_handler: ERROR: got NULL context!\n");
270 1.1 christos context = &uctxmem;
271 1.1 christos getcontext (context); /* initialize dummy context */
272 1.1 christos SETFUNCTIONCONTEXT (context, &__collector_lost_profile_context);
273 1.1 christos }
274 1.1 christos ClockPacket pckt;
275 1.1 christos CALL_UTIL (memset)(&pckt, 0, sizeof ( pckt));
276 1.1 christos pckt.comm.tsize = sizeof ( pckt);
277 1.1 christos pckt.comm.type = CLOCK_TYPE;
278 1.1 christos pckt.lwp_id = __collector_lwp_self ();
279 1.1 christos pckt.thr_id = __collector_thr_self ();
280 1.1 christos pckt.cpu_id = CALL_UTIL (getcpuid)();
281 1.1 christos pckt.tstamp = collector_interface->getHiResTime ();
282 1.1 christos pckt.frinfo = collector_interface->getFrameInfo (COLLECTOR_MODULE_ERR, pckt.tstamp, FRINFO_FROM_UC, context);
283 1.1 christos pckt.mstate = LMS_LINUX_CPU;
284 1.1 christos pckt.nticks = 1;
285 1.1 christos collector_interface->writeDataPacket (prof_hndl, (CM_Packet*) & pckt);
286 1.1 christos POP_REENTRANCE (guard);
287 1.1 christos }
288