|
Packit |
577717 |
/* $Id: signal.c,v 1.18.2.3 2009/01/23 18:37:41 mikpe Exp $
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* This test program illustrates how performance counter overflow
|
|
Packit |
577717 |
* can be caught and sent to the process as a user-specified signal.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Limitations:
|
|
Packit |
577717 |
* - x86 requires a kernel with local APIC support.
|
|
Packit |
577717 |
* - x86 requires a CPU with a local APIC.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Copyright (C) 2001-2004, 2009 Mikael Pettersson
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
#define __USE_GNU /* enable symbolic names for gregset_t[] indices */
|
|
Packit |
577717 |
#include <sys/ucontext.h>
|
|
Packit |
577717 |
#include <signal.h>
|
|
Packit |
577717 |
#include <stdio.h>
|
|
Packit |
577717 |
#include <stdlib.h>
|
|
Packit |
577717 |
#include <string.h>
|
|
Packit |
577717 |
#include "libperfctr.h"
|
|
Packit |
577717 |
#include "arch.h"
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static const struct vperfctr *vperfctr;
|
|
Packit |
577717 |
static struct perfctr_info info;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void do_open(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
vperfctr = vperfctr_open();
|
|
Packit |
577717 |
if( !vperfctr ) {
|
|
Packit |
577717 |
perror("vperfctr_open");
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
if( vperfctr_info(vperfctr, &info) < 0 ) {
|
|
Packit |
577717 |
perror("vperfctr_info");
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
if( !(info.cpu_features & PERFCTR_FEATURE_PCINT) )
|
|
Packit |
577717 |
printf("PCINT not supported -- expect failure\n");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#if defined(__powerpc__)
|
|
Packit |
577717 |
/* It seems that the PPC32 Linux kernels do not clear the high
|
|
Packit |
577717 |
bits of the si_code when copying the siginfo_t to user-space.
|
|
Packit |
577717 |
This works around that. */
|
|
Packit |
577717 |
#define get_si_code(SI) ((SI) & 0xFFFF)
|
|
Packit |
577717 |
#else
|
|
Packit |
577717 |
#define get_si_code(SI) ((SI))
|
|
Packit |
577717 |
#endif
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void on_sigio(int sig, siginfo_t *si, void *puc)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
struct ucontext *uc;
|
|
Packit |
577717 |
unsigned long pc;
|
|
Packit |
577717 |
unsigned int pmc_mask;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if( sig != SIGIO ) {
|
|
Packit |
577717 |
printf("%s: unexpected signal %d\n", __FUNCTION__, sig);
|
|
Packit |
577717 |
return;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
if( get_si_code(si->si_code) != get_si_code(SI_PMC_OVF) ) {
|
|
Packit |
577717 |
printf("%s: unexpected si_code #%x\n", __FUNCTION__, si->si_code);
|
|
Packit |
577717 |
return;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
if( (pmc_mask = si->si_pmc_ovf_mask) == 0 ) {
|
|
Packit |
577717 |
printf("%s: overflow PMCs not identified\n", __FUNCTION__);
|
|
Packit |
577717 |
return;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
uc = puc;
|
|
Packit |
577717 |
pc = ucontext_pc(uc);
|
|
Packit |
577717 |
if( !vperfctr_is_running(vperfctr) ) {
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* My theory is that this happens if a perfctr overflowed
|
|
Packit |
577717 |
* at the very instruction for the VPERFCTR_STOP call.
|
|
Packit |
577717 |
* Signal delivery is delayed until the kernel returns to
|
|
Packit |
577717 |
* user-space, at which time VPERFCTR_STOP will already
|
|
Packit |
577717 |
* have marked the vperfctr as stopped. In this case, we
|
|
Packit |
577717 |
* cannot and must not attempt to IRESUME it.
|
|
Packit |
577717 |
* This can be triggered by counting e.g. BRANCHES and setting
|
|
Packit |
577717 |
* the overflow limit ridiculously low.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
printf("%s: unexpected overflow from PMC set %#x at pc %#lx\n",
|
|
Packit |
577717 |
__FUNCTION__, pmc_mask, pc);
|
|
Packit |
577717 |
return;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
printf("%s: PMC overflow set %#x at pc %#lx\n", __FUNCTION__, pmc_mask, pc);
|
|
Packit |
577717 |
if( vperfctr_iresume(vperfctr) < 0 ) {
|
|
Packit |
577717 |
perror("vperfctr_iresume");
|
|
Packit |
577717 |
abort();
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void do_sigaction(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
struct sigaction sa;
|
|
Packit |
577717 |
memset(&sa, 0, sizeof sa);
|
|
Packit |
577717 |
sa.sa_sigaction = on_sigio;
|
|
Packit |
577717 |
sa.sa_flags = SA_SIGINFO;
|
|
Packit |
577717 |
if( sigaction(SIGIO, &sa, NULL) < 0 ) {
|
|
Packit |
577717 |
perror("sigaction");
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void do_control(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
struct vperfctr_control control;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
memset(&control, 0, sizeof control);
|
|
Packit |
577717 |
do_setup(&info, &control.cpu_control);
|
|
Packit |
577717 |
control.si_signo = SIGIO;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("Control used:\n");
|
|
Packit |
577717 |
perfctr_cpu_control_print(&control.cpu_control);
|
|
Packit |
577717 |
printf("\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if( vperfctr_control(vperfctr, &control) < 0 ) {
|
|
Packit |
577717 |
perror("vperfctr_control");
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void do_stop(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
struct sigaction sa;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if( vperfctr_stop(vperfctr) )
|
|
Packit |
577717 |
perror("vperfctr_stop");
|
|
Packit |
577717 |
memset(&sa, 0, sizeof sa);
|
|
Packit |
577717 |
sa.sa_handler = SIG_DFL;
|
|
Packit |
577717 |
if( sigaction(SIGIO, &sa, NULL) < 0 ) {
|
|
Packit |
577717 |
perror("sigaction");
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define N 150
|
|
Packit |
577717 |
static double v[N], w[N];
|
|
Packit |
577717 |
static double it;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void do_dotprod(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
int i;
|
|
Packit |
577717 |
double sum;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
sum = 0.0;
|
|
Packit |
577717 |
for(i = 0; i < N; ++i)
|
|
Packit |
577717 |
sum += v[i] * w[i];
|
|
Packit |
577717 |
it = sum;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int main(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
do_sigaction();
|
|
Packit |
577717 |
do_open();
|
|
Packit |
577717 |
do_control();
|
|
Packit |
577717 |
do_dotprod();
|
|
Packit |
577717 |
do_stop();
|
|
Packit |
577717 |
return 0;
|
|
Packit |
577717 |
}
|