1 /* Copyright (C) 2021 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 /* Hardware counter profiling */
30 #include <sys/syscall.h>
34 #define _STRING_H 1 /* XXX MEZ: temporary workaround */
36 #include "collector_module.h"
37 #include "gp-experiment.h"
38 #include "libcol_util.h"
39 #include "hwprofile.h"
43 /* TprintfT(<level>,...) definitions. Adjust per module as needed */
44 #define DBG_LT0 0 // for high-level configuration, unexpected errors/warnings
45 #define DBG_LT1 1 // for configuration details, warnings
51 #define SD_OFF 0 /* before start or after close she shut down process */
52 #define SD_PENDING 1 /* before running real_detach_experiment() */
53 #define SD_COMPLETE 2 /* after running real_detach_experiment() */
55 static int init_interface (CollectorInterface*);
56 static int open_experiment (const char *);
57 static int start_data_collection (void);
58 static int stop_data_collection (void);
59 static int close_experiment (void);
60 static int detach_experiment (void);
61 static int real_detach_experiment (void);
63 static ModuleInterface module_interface ={
64 SP_HWCNTR_FILE, /* description */
65 init_interface, /* initInterface */
66 open_experiment, /* openExperiment */
67 start_data_collection, /* startDataCollection */
68 stop_data_collection, /* stopDataCollection */
69 close_experiment, /* closeExperiment */
70 detach_experiment /* detachExperiment (fork child) */
73 static CollectorInterface *collector_interface = NULL;
76 /*---------------------------------------------------------------------------*/
77 /* compile options and workarounds */
79 /* Solaris: We set ITIMER_REALPROF to ensure that counters get started on
80 * LWPs that existed before the collector initialization.
82 * In addition, if the appropriate #define's are set, we check for:
83 * lost-hw-overflow -- the HW counters rollover, but the overflow
84 * interrupt is not generated (counters keep running)
85 * lost-sigemt -- the interrupt is received by the kernel,
86 * which stops the counters, but the kernel fails
87 * to deliver the signal.
90 /*---------------------------------------------------------------------------*/
94 HWCMODE_OFF, /* before start or after close */
95 HWCMODE_SUSPEND, /* stop_data_collection called */
96 HWCMODE_ACTIVE, /* counters are defined and after start_data_collection() */
97 HWCMODE_ABORT /* fatal error occured. Log a message, stop recording */
100 /*---------------------------------------------------------------------------*/
102 static void init_ucontexts (void);
103 static int hwc_initialize_handlers (void);
104 static void collector_record_counter (ucontext_t*,
108 static void collector_hwc_ABORT (int errnum, const char *msg);
109 static void hwclogwrite0 ();
110 static void hwclogwrite (Hwcentry *);
111 static void set_hwc_mode (hwc_mode_t);
112 static void collector_sigemt_handler (int sig, siginfo_t *si, void *puc);
114 /*---------------------------------------------------------------------------*/
115 /* static variables */
117 /* --- user counter selections and options */
118 static int hwcdef_has_memspace; /* true to indicate use of extened packets */
119 static unsigned hwcdef_cnt; /* number of *active* hardware counters */
120 static unsigned hwcdef_num_sampling_ctrdefs; /* ctrs that use sampling */
121 static unsigned hwcdef_num_overflow_ctrdefs; /* ctrs that use overflow */
122 static Hwcentry **hwcdef; /* HWC definitions */
123 static int cpcN_cpuver = CPUVER_UNDEFINED;
124 static int hwcdrv_inited; /* Don't call hwcdrv_init() in fork_child */
125 static hwcdrv_api_t *hwc_driver = NULL;
126 static unsigned hwprofile_tsd_key = COLLECTOR_TSD_INVALID_KEY;
127 static int hwprofile_tsd_sz = 0;
128 static volatile hwc_mode_t hwc_mode = HWCMODE_OFF;
129 static volatile unsigned int nthreads_in_sighandler = 0;
130 static volatile unsigned int sd_state = SD_OFF;
132 /* --- experiment logging state */
133 static CollectorModule expr_hndl = COLLECTOR_MODULE_ERR;
134 static ucontext_t expr_dummy_uc; // used for hacked "collector" frames
135 static ucontext_t expr_out_of_range_uc; // used for "out-of-range" frames
136 static ucontext_t expr_frozen_uc; // used for "frozen" frames
137 static ucontext_t expr_nopc_uc; // used for not-program-related frames
138 static ucontext_t expr_lostcounts_uc; // used for lost_counts frames
140 /* --- signal handler state */
141 static struct sigaction old_sigemt_handler; //overwritten in fork-child
143 /*---------------------------------------------------------------------------*/
145 #define COUNTERS_ENABLED() (hwcdef_cnt)
146 #define gethrtime collector_interface->getHiResTime
149 #define Tprintf(...) if (collector_interface) collector_interface->writeDebugInfo( 0, __VA_ARGS__ )
150 #define TprintfT(...) if (collector_interface) collector_interface->writeDebugInfo( 1, __VA_ARGS__ )
153 #define TprintfT(...)
157 /*---------------------------------------------------------------------------*/
159 /* Initialization routines */
160 static hwcdrv_api_t *
163 if (hwc_driver == NULL)
164 hwc_driver = __collector_get_hwcdrv ();
168 static void init_module () __attribute__ ((constructor));
172 __collector_dlsym_guard = 1;
173 RegModuleFunc reg_module = (RegModuleFunc) dlsym (RTLD_DEFAULT, "__collector_register_module");
174 __collector_dlsym_guard = 0;
175 if (reg_module == NULL)
177 TprintfT (0, "hwprofile: init_module FAILED - reg_module = NULL\n");
180 expr_hndl = reg_module (&module_interface);
181 if (expr_hndl == COLLECTOR_MODULE_ERR)
183 TprintfT (0, "hwprofile: ERROR: handle not created.\n");
184 if (collector_interface)
185 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">data handle not created</event>\n",
186 SP_JCMD_CERROR, COL_ERROR_HWCINIT);
191 init_interface (CollectorInterface *_collector_interface)
193 collector_interface = _collector_interface;
194 return COL_ERROR_NONE;
200 return collector_interface->getKey (hwprofile_tsd_key);
204 open_experiment (const char *exp)
206 if (collector_interface == NULL)
208 TprintfT (0, "hwprofile: ERROR: collector_interface is null.\n");
209 return COL_ERROR_HWCINIT;
211 const char *params = collector_interface->getParams ();
214 if (__collector_strStartWith (params, "h:*") == 0)
216 /* HWC counters set by default */
217 collector_interface->writeLog ("<%s %s=\"1\"/>\n",
218 SP_TAG_SETTING, SP_JCMD_HWC_DEFAULT);
222 else if (__collector_strStartWith (params, "h:") == 0)
227 params = CALL_UTIL (strchr)(params, ';');
231 if (params == NULL) /* HWC profiling not specified */
232 return COL_ERROR_HWCINIT;
233 char *s = CALL_UTIL (strchr)(params, (int) ';');
234 int sz = s ? s - params : CALL_UTIL (strlen)(params);
235 char *defstring = (char*) alloca (sz + 1);
236 CALL_UTIL (strlcpy)(defstring, params, sz + 1);
237 TprintfT (0, "hwprofile: open_experiment %s -- %s\n", exp, defstring);
239 int err = COL_ERROR_NONE;
240 /* init counter library */
242 { /* do not call hwcdrv_init() from fork-child */
245 if (hwc_driver->hwcdrv_init (collector_hwc_ABORT, &hwprofile_tsd_sz) == 0)
247 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">%s</event>\n",
248 SP_JCMD_CERROR, COL_ERROR_HWCINIT, defstring);
249 TprintfT (0, "hwprofile: ERROR: hwcfuncs_init() failed\n");
250 return COL_ERROR_HWCINIT;
253 if (hwc_driver->hwcdrv_enable_mt (hwprofile_get_tsd))
255 // It is OK to call hwcdrv_enable_mt() before tsd key is created
256 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">%s</event>\n",
257 SP_JCMD_CERROR, COL_ERROR_HWCINIT, defstring);
258 TprintfT (0, "hwprofile: ERROR: hwcdrv_enable_mt() failed\n");
259 return COL_ERROR_HWCINIT;
262 hwc_driver->hwcdrv_get_info (&cpcN_cpuver, NULL, NULL, NULL, NULL);
265 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">%s</event>\n",
266 SP_JCMD_CERROR, COL_ERROR_HWCINIT, defstring);
267 TprintfT (0, "hwprofile: ERROR: hwcdrv_get_info() failed\n");
268 return COL_ERROR_HWCINIT;
272 if (hwprofile_tsd_sz)
274 hwprofile_tsd_key = collector_interface->createKey (hwprofile_tsd_sz, NULL, NULL);
275 if (hwprofile_tsd_key == COLLECTOR_TSD_INVALID_KEY)
277 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">%s</event>\n",
278 SP_JCMD_CERROR, COL_ERROR_HWCINIT, defstring);
279 TprintfT (0, "hwprofile: ERROR: TSD createKey failed\n");
280 return COL_ERROR_HWCINIT;
284 hwcdef_has_memspace = 0;
286 /* create counters based on hwcdef[] */
287 err = __collector_hwcfuncs_bind_descriptor (defstring);
290 err = err == HWCFUNCS_ERROR_HWCINIT ? COL_ERROR_HWCINIT : COL_ERROR_HWCARGS;
291 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">%s</event>\n",
292 SP_JCMD_CERROR, err, defstring);
293 TprintfT (0, "hwprofile: ERROR: open_experiment() failed, RC=%d \n", err);
297 /* generate an array of counter structures for each requested counter */
298 hwcdef = __collector_hwcfuncs_get_ctrs (&hwcdef_cnt);
299 hwcdef_num_sampling_ctrdefs = hwcdef_num_overflow_ctrdefs = 0;
301 for (idx = 0; idx < hwcdef_cnt; idx++)
303 if (HWCENTRY_USES_SAMPLING (hwcdef[idx]))
305 hwcdef_num_sampling_ctrdefs++;
309 hwcdef_num_overflow_ctrdefs++;
315 /* initialize the SIGEMT handler, and the periodic HWC checker */
316 err = hwc_initialize_handlers ();
317 if (err != COL_ERROR_NONE)
320 TprintfT (0, "hwprofile: ERROR: open_experiment() failed, RC=%d \n", err);
321 /* log written by hwc_initialize_handlers() */
325 for (idx = 0; idx < hwcdef_cnt; idx++)
326 if (ABST_BACKTRACK_ENABLED (hwcdef[idx]->memop))
327 hwcdef_has_memspace = 1;
329 /* record the hwc definitions in the log, based on the counter array */
331 for (idx = 0; idx < hwcdef_cnt; idx++)
332 hwclogwrite (hwcdef[idx]);
333 return COL_ERROR_NONE;
337 __collector_ext_hwc_lwp_init ()
339 return get_hwc_driver ()->hwcdrv_lwp_init ();
343 __collector_ext_hwc_lwp_fini ()
345 get_hwc_driver ()->hwcdrv_lwp_fini ();
349 __collector_ext_hwc_lwp_suspend ()
351 return get_hwc_driver ()->hwcdrv_lwp_suspend ();
355 __collector_ext_hwc_lwp_resume ()
357 return get_hwc_driver ()->hwcdrv_lwp_resume ();
360 /* Dummy routine, used to provide a context for non-program related profiles */
362 __collector_not_program_related () { }
364 /* Dummy routine, used to provide a context for lost counts (perf_events) */
366 __collector_hwc_samples_lost () { }
368 /* Dummy routine, used to provide a context */
370 __collector_hwcs_frozen () { }
372 /* Dummy routine, used to provide a context */
374 __collector_hwcs_out_of_range () { }
375 /* initialize some structures */
377 init_ucontexts (void)
379 /* initialize dummy context for "collector" frames */
380 CALL_UTIL (getcontext) (&expr_dummy_uc);
381 SETFUNCTIONCONTEXT (&expr_dummy_uc, NULL);
383 /* initialize dummy context for "out-of-range" frames */
384 CALL_UTIL (getcontext) (&expr_out_of_range_uc);
385 SETFUNCTIONCONTEXT (&expr_out_of_range_uc, &__collector_hwcs_out_of_range);
387 /* initialize dummy context for "frozen" frames */
388 CALL_UTIL (getcontext) (&expr_frozen_uc);
389 SETFUNCTIONCONTEXT (&expr_frozen_uc, &__collector_hwcs_frozen);
391 /* initialize dummy context for non-program-related frames */
392 CALL_UTIL (getcontext) (&expr_nopc_uc);
393 SETFUNCTIONCONTEXT (&expr_nopc_uc, &__collector_not_program_related);
395 /* initialize dummy context for lost-counts-related frames */
396 CALL_UTIL (getcontext) (&expr_lostcounts_uc);
397 SETFUNCTIONCONTEXT (&expr_lostcounts_uc, &__collector_hwc_samples_lost);
399 /* initialize the signal handler */
401 hwc_initialize_handlers (void)
403 /* install the signal handler for SIGEMT */
404 struct sigaction oact;
405 if (__collector_sigaction (HWCFUNCS_SIGNAL, NULL, &oact) != 0)
407 TprintfT (0, "hwc_initialize_handlers(): ERROR: hwc_initialize_handlers(): __collector_sigaction() failed to get oact\n");
408 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">old handler could not be determined</event>\n", SP_JCMD_CERROR, COL_ERROR_HWCINIT);
409 return COL_ERROR_HWCINIT;
411 if (oact.sa_sigaction == collector_sigemt_handler)
413 /* signal handler is already in place; we are probably in a fork-child */
414 TprintfT (DBG_LT1, "hwc_initialize_handlers(): hwc_initialize_handlers() collector_sigemt_handler already installed\n");
418 /* set our signal handler */
419 struct sigaction c_act;
420 CALL_UTIL (memset)(&c_act, 0, sizeof c_act);
421 sigemptyset (&c_act.sa_mask);
422 sigaddset (&c_act.sa_mask, SIGPROF); /* block SIGPROF delivery in handler */
423 /* XXXX should probably also block sample_sig & pause_sig */
424 c_act.sa_sigaction = collector_sigemt_handler; /* note: used to set sa_handler instead */
425 c_act.sa_flags = SA_RESTART | SA_SIGINFO;
426 if (__collector_sigaction (HWCFUNCS_SIGNAL, &c_act, &old_sigemt_handler) != 0)
428 TprintfT (0, "hwc_initialize_handlers(): ERROR: hwc_initialize_handlers(): __collector_sigaction() failed to set cact\n");
429 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">event handler could not be installed</event>\n", SP_JCMD_CERROR, COL_ERROR_HWCINIT);
430 return COL_ERROR_HWCINIT;
433 return COL_ERROR_NONE;
437 close_experiment (void)
439 /* note: stop_data_collection() should have already been called by
440 * collector_close_experiment()
442 if (!COUNTERS_ENABLED ())
443 return COL_ERROR_NONE;
444 detach_experiment ();
446 /* cpc or libperfctr may still generate sigemts for a while */
447 /* verify that SIGEMT handler is still installed */
448 /* (still required with sigaction interposition and management,
449 since interposition is not done for attach experiments)
451 struct sigaction curr;
452 if (__collector_sigaction (HWCFUNCS_SIGNAL, NULL, &curr) == -1)
454 TprintfT (0, "hwprofile close_experiment: ERROR: hwc sigaction check failed: errno=%d\n", errno);
456 else if (curr.sa_sigaction != collector_sigemt_handler)
458 TprintfT (DBG_LT1, "hwprofile close_experiment: WARNING: collector sigemt handler replaced by 0x%p!\n", curr.sa_handler);
459 (void) collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">0x%p</event>\n",
460 SP_JCMD_CWARN, COL_WARN_SIGEMT, curr.sa_handler);
463 TprintfT (DBG_LT1, "hwprofile close_experiment: collector sigemt handler integrity verified!\n");
464 TprintfT (0, "hwprofile: close_experiment\n");
469 detach_experiment (void)
471 /* fork child. Clean up state but don't write to experiment */
472 /* note: stop_data_collection() has already been called by the fork_prologue */
473 // detach_experiment() can be called asynchronously
474 // from anywhere, even from within a sigemt handler
476 // Important: stop_data_collection() _must_ be called
477 // before detach_experiment() is called.
478 if (!COUNTERS_ENABLED ())
479 return COL_ERROR_NONE;
480 TprintfT (0, "hwprofile: detach_experiment()\n");
481 if (SD_OFF != __collector_cas_32 (&sd_state, SD_OFF, SD_PENDING))
483 // one and only one call should ever make it here here.
484 if (hwc_mode == HWCMODE_ACTIVE)
486 TprintfT (0, "hwprofile: ERROR: stop_data_collection() should have been called before detach_experiment()\n");
487 stop_data_collection ();
490 // Assumption: The only calls to sigemt_handler
491 // we should see at this point
492 // will be those that were already in-flight before
493 // stop_new_sigemts() was called.
494 if (nthreads_in_sighandler > 0)
496 // sigemt handlers should see
497 // SD_PENDING and should call real_detach_experiment()
498 // when the last handler is finished.
499 TprintfT (DBG_LT1, "hwprofile: detach in the middle of signal handler.\n");
503 // If we get here, there should be no remaining
504 // sigemt handlers. However, we don't really know
505 // if there were ever any in flight, so call
506 // real_detach_experiment() here:
507 return real_detach_experiment (); // multiple calls to this OK
511 real_detach_experiment (void)
513 /*multiple calls to this routine are OK.*/
514 if (SD_PENDING != __collector_cas_32 (&sd_state, SD_PENDING, SD_COMPLETE))
516 // only the first caller to this routine should get here.
517 hwcdef_cnt = 0; /* since now deinstalled */
519 set_hwc_mode (HWCMODE_OFF);
520 if (SD_COMPLETE != __collector_cas_32 (&sd_state, SD_COMPLETE, SD_OFF))
522 TprintfT (0, "hwprofile: ERROR: unexpected sd_state in real_detach_experiment()\n");
525 hwprofile_tsd_key = COLLECTOR_TSD_INVALID_KEY;
526 TprintfT (DBG_LT0, "hwprofile: real_detach_experiment() detached from experiment.\n");
530 /*---------------------------------------------------------------------------*/
531 /* Record counter values. */
533 /* <value> should already be adjusted to be "zero-based" (counting up from 0).*/
535 collector_record_counter_internal (ucontext_t *ucp, int timecvt,
536 ABST_type ABS_memop, hrtime_t time,
537 unsigned tag, uint64_t value, uint64_t pc,
538 uint64_t va, uint64_t latency,
539 uint64_t data_source)
542 CALL_UTIL (memset)(&pckt, 0, sizeof ( MHwcntr_packet));
543 pckt.comm.tstamp = time;
547 if (HWCVAL_HAS_ERR (value))
549 value = HWCVAL_CLR_ERR (value);
551 value = HWCVAL_SET_ERR (value);
556 pckt.interval = value;
557 pckt.comm.type = HW_PCKT;
558 pckt.comm.tsize = sizeof (Hwcntr_packet);
559 TprintfT (DBG_LT4, "hwprofile: %llu sample %lld tag %u recorded\n",
560 (unsigned long long) time, (long long) value, tag);
561 if (ABS_memop == ABST_NOPC)
563 pckt.comm.frinfo = collector_interface->getFrameInfo (expr_hndl, pckt.comm.tstamp, FRINFO_FROM_UC, ucp);
564 collector_interface->writeDataRecord (expr_hndl, (Common_packet*) & pckt);
568 collector_record_counter (ucontext_t *ucp, int timecvt, ABST_type ABS_memop,
569 hrtime_t time, unsigned tag, uint64_t value)
571 collector_record_counter_internal (ucp, timecvt, ABS_memop, time, tag, value,
572 HWCFUNCS_INVALID_U64, HWCFUNCS_INVALID_U64,
573 HWCFUNCS_INVALID_U64, HWCFUNCS_INVALID_U64);
577 /*---------------------------------------------------------------------------*/
578 /* Signal handlers */
580 /* SIGEMT -- relayed from libcpc, when the counter overflows */
582 /* Generates the appropriate event or events, and resets the counters */
584 collector_sigemt_handler (int sig, siginfo_t *si, void *puc)
587 hwc_event_t sample, lost_samples;
588 if (sig != HWCFUNCS_SIGNAL)
590 TprintfT (0, "hwprofile: ERROR: %s: unexpected signal %d\n", "collector_sigemt_handler", sig);
593 if (!COUNTERS_ENABLED ())
594 { /* apparently deinstalled */
595 TprintfT (0, "hwprofile: WARNING: SIGEMT detected after close_experiment()\n");
596 /* kills future sigemts since hwcdrv_sighlr_restart() not called */
600 /* Typically, we expect HWC overflow signals to come from the kernel: si_code > 0.
601 * On Linux, however, dbx might be "forwarding" a signal using tkill()/tgkill().
602 * For more information on what si_code values can be expected on Linux, check:
603 * cmn_components/Collector_Interface/hwcdrv_pcl.c hwcdrv_overflow()
604 * cmn_components/Collector_Interface/hwcdrv_perfctr.c hdrv_perfctr_overflow()
606 if (puc == NULL || si == NULL || (si->si_code <= 0 && si->si_code != SI_TKILL))
608 TprintfT (DBG_LT3, "hwprofile: collector_sigemt_handler SIG%02d\n", sig);
609 if (old_sigemt_handler.sa_handler == SIG_DFL)
610 __collector_SIGDFL_handler (HWCFUNCS_SIGNAL);
611 else if (old_sigemt_handler.sa_handler != SIG_IGN &&
612 old_sigemt_handler.sa_sigaction != &collector_sigemt_handler)
614 /* Redirect the signal to the previous signal handler */
615 (old_sigemt_handler.sa_sigaction)(sig, si, puc);
616 TprintfT (DBG_LT1, "hwprofile: collector_sigemt_handler SIG%02d redirected to original handler\n", sig);
620 rc = get_hwc_driver ()->hwcdrv_overflow (si, &sample, &lost_samples);
623 /* hwcdrv_sighlr_restart() should not be called */
624 TprintfT (0, "hwprofile: ERROR: collector_sigemt_handler: hwcdrv_overflow() failed\n");
628 if (hwc_mode == HWCMODE_ACTIVE)
630 /* record the event only if counters are active */
631 /* The following has been copied from dispatcher.c */
633 /* 23340823 signal handler third argument should point to a ucontext_t */
634 /* Convert sigcontext to ucontext_t on sparc-Linux */
636 struct sigcontext *sctx = (struct sigcontext*) puc;
637 ucontext_t *uctx = &uctxmem;
638 uctx->uc_link = NULL;
640 uctx->uc_mcontext.gregs[REG_PC] = sctx->si_regs.pc;
641 __collector_memcpy (&uctx->uc_mcontext.gregs[3],
642 sctx->si_regs.u_regs,
643 sizeof (sctx->si_regs.u_regs));
645 uctx->uc_mcontext.mc_gregs[MC_PC] = sctx->sigc_regs.tpc;
646 __collector_memcpy (&uctx->uc_mcontext.mc_gregs[3],
647 sctx->sigc_regs.u_regs,
648 sizeof (sctx->sigc_regs.u_regs));
651 ucontext_t *uctx = (ucontext_t*) puc;
654 for (int ii = 0; ii < hwcdef_cnt; ii++)
655 if (lost_samples.ce_pic[ii])
656 collector_record_counter (&expr_lostcounts_uc, hwcdef[ii]->timecvt,
657 hwcdef[ii]->memop, lost_samples.ce_hrt,
658 hwcdef[ii]->sort_order, lost_samples.ce_pic[ii]);
659 for (int ii = 0; ii < hwcdef_cnt; ii++)
660 if (sample.ce_pic[ii])
661 collector_record_counter (uctx, hwcdef[ii]->timecvt,
662 hwcdef[ii]->memop, sample.ce_hrt,
663 hwcdef[ii]->sort_order, sample.ce_pic[ii]);
665 rc = get_hwc_driver ()->hwcdrv_sighlr_restart (NULL);
667 /* SIGPROF -- not installed as handler, but
668 * __collector_ext_hwc_check: called by (SIGPROF) dispatcher.
669 * Periodical check of integrity of HWC count/signal mechanism,
670 * as required for various chip/system bugs/workarounds.
673 __collector_ext_hwc_check (siginfo_t *info, ucontext_t *vcontext) { }
675 /*---------------------------------------------------------------------------*/
677 collector_sigemt_sigaction (const struct sigaction *nact,
678 struct sigaction *oact)
680 struct sigaction oact_check;
681 /* Error codes and messages that refer to HWC are tricky.
682 * E.g., HWC profiling might not even be on; we might
683 * encounter an error here simply because the user is
684 * trying to set a handler for a signal that happens to
685 * be HWCFUNCS_SIGNAL, which we aren't even using.
687 if (__collector_sigaction (HWCFUNCS_SIGNAL, NULL, &oact_check) != 0)
689 TprintfT (0, "hwprofile: ERROR: collector_sigemt_sigaction(): request to set handler for signal %d, but check on existing handler failed\n", HWCFUNCS_SIGNAL);
690 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">old handler for signal %d could not be determined</event>\n", SP_JCMD_CERROR, COL_ERROR_HWCINIT, HWCFUNCS_SIGNAL);
691 return COL_ERROR_HWCINIT;
694 if (oact_check.sa_sigaction == collector_sigemt_handler)
696 /* dispatcher is in place, so nact/oact apply to old_sigemt_handler */
699 oact->sa_handler = old_sigemt_handler.sa_handler;
700 oact->sa_mask = old_sigemt_handler.sa_mask;
701 oact->sa_flags = old_sigemt_handler.sa_flags;
705 old_sigemt_handler.sa_handler = nact->sa_handler;
706 old_sigemt_handler.sa_mask = nact->sa_mask;
707 old_sigemt_handler.sa_flags = nact->sa_flags;
709 return COL_ERROR_NONE;
711 else /* no dispatcher in place, so just act like normal sigaction() */
712 return __collector_sigaction (HWCFUNCS_SIGNAL, nact, oact);
716 collector_hwc_ABORT (int errnum, const char *msg)
718 TprintfT (0, "hwprofile: collector_hwc_ABORT: [%d] %s\n", errnum, msg);
719 if (hwc_mode == HWCMODE_ABORT) /* HWC collection already aborted! */
721 set_hwc_mode (HWCMODE_ABORT); /* set global flag to disable handlers and indicate abort */
723 /* Write the error message to the experiment */
724 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">%s: errno=%d</event>\n",
725 SP_JCMD_CERROR, COL_ERROR_HWCFAIL, msg, errnum);
730 TprintfT (0, "hwprofile: Continuing without HWC collection...\n");
735 start_data_collection (void)
737 hwc_mode_t old_mode = hwc_mode;
738 if (!COUNTERS_ENABLED ())
739 return COL_ERROR_NONE;
740 TprintfT (0, "hwprofile: start_data_collection (hwc_mode=%d)\n", old_mode);
744 if (get_hwc_driver ()->hwcdrv_start ())
746 TprintfT (0, "hwprofile: ERROR: start_data_collection() failed in hwcdrv_start()\n");
747 collector_interface->writeLog ("<event kind=\"%s\" id=\"%d\">%s: errno=%d</event>\n",
748 SP_JCMD_CERROR, COL_ERROR_HWCFAIL,
749 "start_data_collection()", errno);
750 return COL_ERROR_HWCINIT;
752 set_hwc_mode (HWCMODE_ACTIVE); /* start handling events on signals */
754 case HWCMODE_SUSPEND:
755 if (get_hwc_driver ()->hwcdrv_lwp_resume ())
757 TprintfT (0, "hwprofile: ERROR: start_data_collection() failed in hwcdrv_lwp_resume()\n");
758 /* ignore errors from lwp_resume() */
760 set_hwc_mode (HWCMODE_ACTIVE); /* start handling events on signals */
763 TprintfT (0, "hwprofile: ERROR: start_data_collection() invalid mode\n");
764 return COL_ERROR_HWCINIT;
766 return COL_ERROR_NONE;
770 stop_data_collection (void)
772 hwc_mode_t old_mode = hwc_mode;
773 if (!COUNTERS_ENABLED ())
774 return COL_ERROR_NONE;
775 TprintfT (0, "hwprofile: stop_data_collection (hwc_mode=%d)\n", old_mode);
778 case HWCMODE_SUSPEND:
779 return COL_ERROR_NONE;
781 set_hwc_mode (HWCMODE_SUSPEND); /* stop handling signals */
784 /* Don't change the mode, but attempt to suspend anyway... */
788 if (get_hwc_driver ()->hwcdrv_lwp_suspend ())
789 /* ignore errors from lwp_suspend() */
790 TprintfT (0, "hwprofile: ERROR: stop_data_collection() failed in hwcdrv_lwp_suspend()\n");
793 * hwcdrv_lwp_suspend() cannot guarantee that all SIGEMTs will stop
794 * but hwc_mode will prevent logging and counters will overflow once
797 /* There may still be pending SIGEMTs so don't reset the SIG_DFL handler.
799 /* see comment in dispatcher.c */
800 /* ret = __collector_sigaction( SIGEMT, &old_sigemt_handler, NULL ); */
801 return COL_ERROR_NONE;
804 /*---------------------------------------------------------------------------*/
808 set_hwc_mode (hwc_mode_t md)
810 TprintfT (DBG_LT1, "hwprofile: set_hwc_mode(%d)\n", md);
815 __collector_ext_hwc_active ()
817 return (hwc_mode == HWCMODE_ACTIVE);
823 collector_interface->writeLog ("<profdata fname=\"%s\"/>\n",
824 module_interface.description);
825 /* Record Hwcntr_packet description */
826 Hwcntr_packet *pp = NULL;
827 collector_interface->writeLog ("<profpckt kind=\"%d\" uname=\"" STXT ("Hardware counter profiling data") "\">\n", HW_PCKT);
828 collector_interface->writeLog (" <field name=\"LWPID\" uname=\"" STXT ("Lightweight process id") "\" offset=\"%d\" type=\"%s\"/>\n",
829 &pp->comm.lwp_id, sizeof (pp->comm.lwp_id) == 4 ? "INT32" : "INT64");
830 collector_interface->writeLog (" <field name=\"THRID\" uname=\"" STXT ("Thread number") "\" offset=\"%d\" type=\"%s\"/>\n",
831 &pp->comm.thr_id, sizeof (pp->comm.thr_id) == 4 ? "INT32" : "INT64");
832 collector_interface->writeLog (" <field name=\"CPUID\" uname=\"" STXT ("CPU id") "\" offset=\"%d\" type=\"%s\"/>\n",
833 &pp->comm.cpu_id, sizeof (pp->comm.cpu_id) == 4 ? "INT32" : "INT64");
834 collector_interface->writeLog (" <field name=\"TSTAMP\" uname=\"" STXT ("High resolution timestamp") "\" offset=\"%d\" type=\"%s\"/>\n",
835 &pp->comm.tstamp, sizeof (pp->comm.tstamp) == 4 ? "INT32" : "INT64");
836 collector_interface->writeLog (" <field name=\"FRINFO\" offset=\"%d\" type=\"%s\"/>\n",
837 &pp->comm.frinfo, sizeof (pp->comm.frinfo) == 4 ? "INT32" : "INT64");
838 collector_interface->writeLog (" <field name=\"HWCTAG\" uname=\"" STXT ("Hardware counter index") "\" offset=\"%d\" type=\"%s\"/>\n",
839 &pp->tag, sizeof (pp->tag) == 4 ? "INT32" : "INT64");
840 collector_interface->writeLog (" <field name=\"HWCINT\" uname=\"" STXT ("Hardware counter interval") "\" offset=\"%d\" type=\"%s\"/>\n",
841 &pp->interval, sizeof (pp->interval) == 4 ? "INT32" : "INT64");
842 collector_interface->writeLog ("</profpckt>\n");
843 if (hwcdef_has_memspace)
845 /* Record MHwcntr_packet description */
846 MHwcntr_packet *xpp = NULL;
847 collector_interface->writeLog ("<profpckt kind=\"%d\" uname=\"" STXT ("Hardware counter profiling data") "\">\n", MHWC_PCKT);
848 collector_interface->writeLog (" <field name=\"LWPID\" uname=\"" STXT ("Lightweight process id") "\" offset=\"%d\" type=\"%s\"/>\n",
849 &xpp->comm.lwp_id, sizeof (xpp->comm.lwp_id) == 4 ? "INT32" : "INT64");
850 collector_interface->writeLog (" <field name=\"THRID\" uname=\"" STXT ("Thread number") "\" offset=\"%d\" type=\"%s\"/>\n",
851 &xpp->comm.thr_id, sizeof (xpp->comm.thr_id) == 4 ? "INT32" : "INT64");
852 collector_interface->writeLog (" <field name=\"CPUID\" uname=\"" STXT ("CPU id") "\" offset=\"%d\" type=\"%s\"/>\n",
853 &xpp->comm.cpu_id, sizeof (xpp->comm.cpu_id) == 4 ? "INT32" : "INT64");
854 collector_interface->writeLog (" <field name=\"TSTAMP\" uname=\"" STXT ("High resolution timestamp") "\" offset=\"%d\" type=\"%s\"/>\n",
855 &xpp->comm.tstamp, sizeof (xpp->comm.tstamp) == 4 ? "INT32" : "INT64");
856 collector_interface->writeLog (" <field name=\"FRINFO\" offset=\"%d\" type=\"%s\"/>\n",
857 &xpp->comm.frinfo, sizeof (xpp->comm.frinfo) == 4 ? "INT32" : "INT64");
858 collector_interface->writeLog (" <field name=\"HWCTAG\" uname=\"" STXT ("Hardware counter index") "\" offset=\"%d\" type=\"%s\"/>\n",
859 &xpp->tag, sizeof (xpp->tag) == 4 ? "INT32" : "INT64");
860 collector_interface->writeLog (" <field name=\"HWCINT\" uname=\"" STXT ("Hardware counter interval") "\" offset=\"%d\" type=\"%s\"/>\n",
861 &xpp->interval, sizeof (xpp->interval) == 4 ? "INT32" : "INT64");
862 collector_interface->writeLog (" <field name=\"VADDR\" uname=\"" STXT ("Virtual address (data)") "\" offset=\"%d\" type=\"%s\"/>\n",
863 &xpp->ea_vaddr, sizeof (xpp->ea_vaddr) == 4 ? "UINT32" : "UINT64");
864 collector_interface->writeLog (" <field name=\"PADDR\" uname=\"" STXT ("Physical address (data)") "\" offset=\"%d\" type=\"%s\"/>\n",
865 &xpp->ea_paddr, sizeof (xpp->ea_paddr) == 4 ? "UINT32" : "UINT64");
866 collector_interface->writeLog (" <field name=\"VIRTPC\" uname=\"" STXT ("Virtual address (instruction)") "\" offset=\"%d\" type=\"%s\"/>\n",
867 &xpp->pc_vaddr, sizeof (xpp->pc_vaddr) == 4 ? "UINT32" : "UINT64");
868 collector_interface->writeLog (" <field name=\"PHYSPC\" uname=\"" STXT ("Physical address (instruction)") "\" offset=\"%d\" type=\"%s\"/>\n",
869 &xpp->pc_paddr, sizeof (xpp->pc_paddr) == 4 ? "UINT32" : "UINT64");
870 collector_interface->writeLog (" <field name=\"EA_PAGESIZE\" uname=\"" STXT ("Page size (data)") "\" offset=\"%d\" type=\"%s\"/>\n",
871 &xpp->ea_pagesz, sizeof (xpp->ea_pagesz) == 4 ? "INT32" : "INT64");
872 collector_interface->writeLog (" <field name=\"PC_PAGESIZE\" uname=\"" STXT ("Page size (instruction)") "\" offset=\"%d\" type=\"%s\"/>\n",
873 &xpp->pc_pagesz, sizeof (xpp->pc_pagesz) == 4 ? "INT32" : "INT64");
874 collector_interface->writeLog (" <field name=\"EA_LGRP\" uname=\"" STXT ("Page locality group (data)") "\" offset=\"%d\" type=\"%s\"/>\n",
875 &xpp->ea_lgrp, sizeof (xpp->ea_lgrp) == 4 ? "INT32" : "INT64");
876 collector_interface->writeLog (" <field name=\"PC_LGRP\" uname=\"" STXT ("Page locality group (instruction)") "\" offset=\"%d\" type=\"%s\"/>\n",
877 &xpp->pc_lgrp, sizeof (xpp->pc_lgrp) == 4 ? "INT32" : "INT64");
878 collector_interface->writeLog (" <field name=\"LWP_LGRP_HOME\" uname=\"" STXT ("LWP home lgroup id") "\" offset=\"%d\" type=\"%s\"/>\n",
879 &xpp->lgrp_lwp, sizeof (xpp->lgrp_lwp) == 4 ? "INT32" : "INT64");
880 collector_interface->writeLog (" <field name=\"PS_LGRP_HOME\" uname=\"" STXT ("Process home lgroup id") "\" offset=\"%d\" type=\"%s\"/>\n",
881 &xpp->lgrp_ps, sizeof (xpp->lgrp_ps) == 4 ? "INT32" : "INT64");
882 collector_interface->writeLog (" <field name=\"MEM_LAT\" uname=\"" STXT ("Memory Latency Cycles") "\" offset=\"%d\" type=\"%s\"/>\n",
883 &xpp->latency, sizeof (xpp->latency) == 4 ? "INT32" : "INT64");
884 collector_interface->writeLog (" <field name=\"MEM_SRC\" uname=\"" STXT ("Memory Data Source") "\" offset=\"%d\" type=\"%s\"/>\n",
885 &xpp->data_source, sizeof (xpp->data_source) == 4 ? "INT32" : "INT64");
886 collector_interface->writeLog ("</profpckt>\n");
891 hwclogwrite (Hwcentry * ctr)
893 TprintfT (DBG_LT1, "hwprofile: writeLog(%s %u %s %d %u %d)\n",
894 SP_JCMD_HW_COUNTER, cpcN_cpuver, ctr->name ? ctr->name : "NULL",
895 ctr->val, ctr->sort_order, ctr->memop);
896 collector_interface->writeLog ("<profile name=\"%s\"", SP_JCMD_HW_COUNTER);
897 collector_interface->writeLog (" cpuver=\"%u\"", cpcN_cpuver);
898 collector_interface->writeLog (" hwcname=\"%s\"", ctr->name);
899 collector_interface->writeLog (" int_name=\"%s\"", ctr->int_name);
900 collector_interface->writeLog (" interval=\"%d\"", ctr->val);
901 collector_interface->writeLog (" tag=\"%u\"", ctr->sort_order);
902 collector_interface->writeLog (" memop=\"%d\"", ctr->memop);
903 collector_interface->writeLog ("/>\n");