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