Blame src/perfctr-2.6.x/examples/perfex/ppc.c

Packit Service a1973e
/* $Id: ppc.c,v 1.1.2.2 2010/06/08 20:48:56 mikpe Exp $
Packit Service a1973e
 * PPC32-specific code.
Packit Service a1973e
 *
Packit Service a1973e
 * Copyright (C) 2004-2010  Mikael Pettersson
Packit Service a1973e
 */
Packit Service a1973e
#include <stdio.h>
Packit Service a1973e
#include <stdlib.h>
Packit Service a1973e
#include "libperfctr.h"
Packit Service a1973e
#include "arch.h"
Packit Service a1973e
Packit Service a1973e
void do_print(FILE *resfile,
Packit Service a1973e
	      const struct perfctr_info *info,
Packit Service a1973e
	      const struct perfctr_cpu_control *cpu_control,
Packit Service a1973e
	      const struct perfctr_sum_ctrs *sum)
Packit Service a1973e
{
Packit Service a1973e
    unsigned int nrctrs, i;
Packit Service a1973e
Packit Service a1973e
    if( cpu_control->tsc_on )
Packit Service a1973e
	fprintf(resfile, "tsc\t\t\t%19lld\n", sum->tsc);
Packit Service a1973e
    nrctrs = cpu_control->nractrs;
Packit Service a1973e
    for(i = 0; i < nrctrs; ++i) {
Packit Service a1973e
	fprintf(resfile, "event 0x%08X",
Packit Service a1973e
		cpu_control->evntsel[i]);
Packit Service a1973e
	fprintf(resfile, "\t%19lld\n", sum->pmc[i]);
Packit Service a1973e
    }
Packit Service a1973e
    if( cpu_control->ppc.mmcr0 )
Packit Service a1973e
	fprintf(resfile, "mmcr0 0x%08X\n",
Packit Service a1973e
		cpu_control->ppc.mmcr0);
Packit Service a1973e
    if( cpu_control->ppc.mmcr2 )
Packit Service a1973e
	fprintf(resfile, "mmcr2 0x%08X\n",
Packit Service a1973e
		cpu_control->ppc.mmcr2);
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
void do_arch_usage(void)
Packit Service a1973e
{
Packit Service a1973e
    fprintf(stderr, "\t--mmcr0=<value>\t\t\tValue for MMCR0\n");
Packit Service a1973e
    fprintf(stderr, "\t--mmcr2=<value>\t\t\tValue for MMCR2\n");
Packit Service a1973e
    fprintf(stderr, "\n");
Packit Service a1973e
    fprintf(stderr, "Syntax of event specifiers:\n");
Packit Service a1973e
    fprintf(stderr, "\tevent ::= evntsel[@pmc]\n");
Packit Service a1973e
    fprintf(stderr, "\n");
Packit Service a1973e
    fprintf(stderr, "\tevntsel and pmc are decimal or hexadecimal numbers.\n");
Packit Service a1973e
    fprintf(stderr, "\n");
Packit Service a1973e
    fprintf(stderr, "\tevntsel is the primary processor-specific event selection code\n");
Packit Service a1973e
    fprintf(stderr, "\tto use for this counter. This field is mandatory.\n");
Packit Service a1973e
    fprintf(stderr, "\tEvntsel is written to a field in MMCR0 or MMCR1.\n");
Packit Service a1973e
    fprintf(stderr, "\n");
Packit Service a1973e
    fprintf(stderr, "\tpmc describes which CPU counter to use for this event.\n");
Packit Service a1973e
    fprintf(stderr, "\tBy default the events use counters 0 and up in the order listed.\n");
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static int parse_event_spec(const char *arg, unsigned int *evntsel,
Packit Service a1973e
			    unsigned int *pmc)
Packit Service a1973e
{
Packit Service a1973e
    char *endp;
Packit Service a1973e
Packit Service a1973e
    *evntsel = my_strtoul(arg, &endp);
Packit Service a1973e
    if( endp[0] != '@' ) {
Packit Service a1973e
	*pmc = (unsigned int)-1;
Packit Service a1973e
    } else {
Packit Service a1973e
	arg = endp + 1;
Packit Service a1973e
	*pmc = my_strtoul(arg, &endp);
Packit Service a1973e
    }
Packit Service a1973e
    return endp[0] != '\0';
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
unsigned int do_event_spec(unsigned int n,
Packit Service a1973e
			   const char *arg,
Packit Service a1973e
			   struct perfctr_cpu_control *cpu_control)
Packit Service a1973e
{
Packit Service a1973e
    unsigned int spec_evntsel, spec_pmc;
Packit Service a1973e
Packit Service a1973e
    if( parse_event_spec(arg, &spec_evntsel, &spec_pmc) ) {
Packit Service a1973e
	fprintf(stderr, "perfex: invalid event specifier: '%s'\n", arg);
Packit Service a1973e
	exit(1);
Packit Service a1973e
    }
Packit Service a1973e
    if( n >= ARRAY_SIZE(cpu_control->evntsel) ) {
Packit Service a1973e
	fprintf(stderr, "perfex: too many event specifiers\n");
Packit Service a1973e
	exit(1);
Packit Service a1973e
    }
Packit Service a1973e
    if( spec_pmc == (unsigned int)-1 )
Packit Service a1973e
	spec_pmc = n;
Packit Service a1973e
    cpu_control->evntsel[n] = spec_evntsel;
Packit Service a1973e
    cpu_control->pmc_map[n] = spec_pmc;
Packit Service a1973e
    cpu_control->nractrs = ++n;
Packit Service a1973e
    return n;
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static int parse_value(const char *arg, unsigned int *value)
Packit Service a1973e
{
Packit Service a1973e
    char *endp;
Packit Service a1973e
Packit Service a1973e
    *value = my_strtoul(arg, &endp);
Packit Service a1973e
    return endp[0] != '\0';
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
int do_arch_option(int ch,
Packit Service a1973e
		   const char *arg,
Packit Service a1973e
		   struct perfctr_cpu_control *cpu_control)
Packit Service a1973e
{
Packit Service a1973e
    unsigned int spec_value;
Packit Service a1973e
Packit Service a1973e
    switch( ch ) {
Packit Service a1973e
      case 1:
Packit Service a1973e
	if( parse_value(arg, &spec_value) ) {
Packit Service a1973e
	    fprintf(stderr, "perfex: invalid value: '%s'\n", arg);
Packit Service a1973e
	    exit(1);
Packit Service a1973e
	}
Packit Service a1973e
	cpu_control->ppc.mmcr0 = spec_value;
Packit Service a1973e
	return 0;
Packit Service a1973e
      case 2:
Packit Service a1973e
	if( parse_value(arg, &spec_value) ) {
Packit Service a1973e
	    fprintf(stderr, "perfex: invalid value: '%s'\n", arg);
Packit Service a1973e
	    exit(1);
Packit Service a1973e
	}
Packit Service a1973e
	cpu_control->ppc.mmcr2 = spec_value;
Packit Service a1973e
	return 0;
Packit Service a1973e
    }
Packit Service a1973e
    return -1;
Packit Service a1973e
}