Hi guys,

It seems that an older problem has crept back in to the kernel patch. I
seem to remember this being fixed once before...but at the moment, it is
still there. 

The normal test cases inside of perfmon don't catch this case, so I have
included one here. Basically it's self that does multiple start and
stops. Manoj, Mark, can you verify this? Test case has been attached.

One good run looks like this:

PMD0               67073650 CYCLES
PMD1               53000183 INSNS_COMPLETE
PMD0               66098650 CYCLES
PMD1               53000181 INSNS_COMPLETE
PMD0               66094923 CYCLES
PMD1               53000181 INSNS_COMPLETE

Occasionally it looks like this. Sometimes it's easier to see if you run
multiple copies.

PMD0               66066007 CYCLES
PMD1               53000182 INSNS_COMPLETE
PMD0               19935908 CYCLES
PMD1               15730260 INSNS_COMPLETE
PMD0               58149463 CYCLES
PMD1               45930517 INSNS_COMPLETE

I have verified this on both 64 and 32 bit builds on a MIPS 25K with an
unmodified (except for syscalls) patch.

Stefane, do you have any obvious ideas about where I should look? The
code currently saves and restores all counters (unlike my previous
mails).

---

BTW, the MIPS LIBPFM code I have needed some fixes for 64 bit builds as
well as some other minor things.

1) Change uint32_t to unsigned int in lib/pfmlib_gen_mips64_priv.h
2) Add perfmon/pfmlib_gen_mips64.h to include/Makefile at line 92
3) Code for get_event_counters should look like this:
>             *code = 0xff & (gen_mips64_pe[i].pme_code >> (cnt*8));
Instead of:
<             *code = 0xf & (gen_mips64_pe[i].pme_code >> (cnt*4));
4) Fix up domain bits to be 'standardized'
diff -r1.5 pfmlib_gen_mips64.h
35,37c35,37
<  * PFM_PLM0 = SUPERVISOR
<  * PFM_PLM1 = INTERRUPT
<  * PFM_PLM2 = KERNEL
---
>  * PFM_PLM0 = KERNEL
>  * PFM_PLM2 = INTERRUPT
>  * PFM_PLM1 = SUPERVISOR
In stuff_regs:
<   reg.sel_os  = plm & PFM_PLM2 ? 1 : 0;
---
>   reg.sel_os  = plm & PFM_PLM0 ? 1 : 0;
>   reg.sel_exl = plm & PFM_PLM2 ? 1 : 0;
202d227
<   reg.sel_exl = plm & PFM_PLM0 ? 1 : 0;


/*
 * self.c - example of a simple self monitoring task
 *
 * Copyright (c) 2002-2006 Hewlett-Packard Development Company, L.P.
 * Contributed by Stephane Eranian <[EMAIL PROTECTED]>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * This file is part of libpfm, a performance monitoring support library for
 * applications on Linux.
 */

#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

#include <perfmon/pfmlib.h>
#include <perfmon/perfmon.h>

#include "detect_pmcs.h"

#define NUM_PMCS PFMLIB_MAX_PMCS
#define NUM_PMDS PFMLIB_MAX_PMDS
#define ITERS 1000000ULL

#define TABSIZE 1024
static int a[TABSIZE], b[TABSIZE];

/*
 * our test code (function cannot be made static otherwise it is optimized away)
 */
uint64_t
noploop(uint64_t loop)
{
	unsigned int j = 0;

	while (loop--) { a[j] += a[j]*loop + b[j];j = (j+1)%TABSIZE; }
	return a[0];
}

static void fatal_error(char *fmt,...) __attribute__((noreturn));

static void
fatal_error(char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	exit(1);
}

int
main(int argc, char **argv)
{
	char **p;
	unsigned int i;
	int ret, ctx_fd;
	pfmlib_input_param_t inp;
	pfmlib_output_param_t outp;
	pfarg_pmd_t pd[NUM_PMDS];
	pfarg_pmc_t pc[NUM_PMCS];
	pfarg_ctx_t ctx;
	pfarg_load_t load_args;
	pfmlib_options_t pfmlib_options;
	unsigned int num_counters;
	size_t len;
	char *name;

	/*
	 * Initialize pfm library (required before we can use it)
	 */
	ret = pfm_initialize();
	if (ret != PFMLIB_SUCCESS)
		fatal_error("Cannot initialize library: %s\n", pfm_strerror(ret));

	pfm_get_max_event_name_len(&len);
	name = malloc(len+1);
	if (!name)
		fatal_error("cannot allocate event name buffer\n");

	pfm_get_num_counters(&num_counters);

	/*
	 * pass options to library (optional)
	 */
	memset(&pfmlib_options, 0, sizeof(pfmlib_options));
	pfmlib_options.pfm_debug   = 1; /* set to 1 for debug */
	pfmlib_options.pfm_verbose = 1; /* set to 1 for verbose */
	pfm_set_options(&pfmlib_options);

	memset(pd, 0, sizeof(pd));
	memset(pc, 0, sizeof(pc));
	memset(&ctx, 0, sizeof(ctx));
	memset(&load_args, 0, sizeof(load_args));

	/*
	 * prepare parameters to library.
	 */
	memset(&inp,0, sizeof(inp));
	memset(&outp,0, sizeof(outp));

	/*
	 * be nice to user!
	 */
	if (argc > 1) {
		p = argv+1;
		for (i=0; *p ; i++, p++) {
			ret = pfm_find_full_event(*p, &inp.pfp_events[i]);
			if (ret != PFMLIB_SUCCESS)
				fatal_error("event %s: %s\n", *p, pfm_strerror(ret));
		}
	} else {
		if (pfm_get_cycle_event(&inp.pfp_events[0]) != PFMLIB_SUCCESS)
			fatal_error("cannot find cycle event\n");

		if (pfm_get_inst_retired_event(&inp.pfp_events[1]) != PFMLIB_SUCCESS)
			fatal_error("cannot find inst retired event\n");
		i = 2;
	}

	/*
	 * set the default privilege mode for all counters:
	 * 	PFM_PLM3 : user level only
	 */
	inp.pfp_dfl_plm   = PFM_PLM3;

	if (i > num_counters) {
		i = num_counters;
		printf("too many events provided (max=%d events), using first %d event(s)\n", num_counters, i);
	}

	/*
	 * how many counters we use
	 */
	inp.pfp_event_count = i;

	/*
	 * now create a new context, per process context.
	 * This just creates a new context with some initial state, it is not
	 * active nor attached to any process.
	 */
	ctx_fd = pfm_create_context(&ctx, NULL, NULL, 0);
	if (ctx_fd == -1)  {
		if (errno == ENOSYS)
			fatal_error("Your kernel does not have performance monitoring support!\n");
		fatal_error("Can't create PFM context %s\n", strerror(errno));
	}
	/*
	 * build the pfp_unavail_pmcs bitmask by looking
	 * at what perfmon has available. It is not always
	 * the case that all PMU registers are actually available
	 * to applications. For instance, on IA-32 platforms, some
	 * registers may be reserved for the NMI watchdog timer.
	 *
	 * With this bitmap, the library knows which registers NOT to
	 * use. Of source, it is possible that no valid assignement may
	 * be possible if certina PMU registers  are not available.
	 */
	detect_unavail_pmcs(ctx_fd, &inp.pfp_unavail_pmcs);

	/*
	 * let the library figure out the values for the PMCS
	 */
	if ((ret=pfm_dispatch_events(&inp, NULL, &outp, NULL)) != PFMLIB_SUCCESS)
		fatal_error("cannot configure events: %s\n", pfm_strerror(ret));

	/*
	 * Now prepare the argument to initialize the PMDs and PMCS.
	 * We use pfp_pmc_count to determine the number of PMC to intialize.
	 * We use pfp_pmd_count to determine the number of PMD to initialize.
	 * Some events/features may cause extra PMCs to be used, leading to:
	 * 	- pfp_pmc_count may be >= pfp_event_count
	 * 	- pfp_pmd_count may be >= pfp_event_count
	 */
	for (i=0; i < outp.pfp_pmc_count; i++) {
		pc[i].reg_num   = outp.pfp_pmcs[i].reg_num;
		pc[i].reg_value = outp.pfp_pmcs[i].reg_value;
	}

	for (i=0; i < outp.pfp_pmd_count; i++) {
		pd[i].reg_num   = outp.pfp_pmds[i].reg_num;
	}

	/*
	 * Now program the registers
	 */
	if (pfm_write_pmcs(ctx_fd, pc, outp.pfp_pmc_count))
		fatal_error("pfm_write_pmcs error errno %d\n",errno);

	if (pfm_write_pmds(ctx_fd, pd, outp.pfp_pmd_count))
		fatal_error("pfm_write_pmds error errno %d\n",errno);

	/*
	 * now we load (i.e., attach) the context to ourself
	 */
	load_args.load_pid = getpid();
	if (pfm_load_context(ctx_fd, &load_args))
		fatal_error("pfm_load_context error errno %d\n",errno);

	noploop(ITERS);

	/*
	 * Let's roll now
	 */
	if (pfm_start(ctx_fd, NULL))
		fatal_error("pfm_start error errno %d\n",errno);

	noploop(ITERS);

	if (pfm_stop(ctx_fd))
		fatal_error("pfm_stop error errno %d\n",errno);

	/*
	 * now read the results. We use pfp_event_count because
	 * libpfm guarantees that counters for the events always
	 * come first.
	 */
	if (pfm_read_pmds(ctx_fd, pd, inp.pfp_event_count))
		fatal_error( "pfm_read_pmds error errno %d\n",errno);

	/*
	 * print the results
	 */
	for (i=0; i < inp.pfp_event_count; i++) {
		pfm_get_full_event_name(&inp.pfp_events[i], name, len+1);
		printf("PMD%-3u %20"PRIu64" %s\n",
			pd[i].reg_num,
			pd[i].reg_value,
			name);
		pd[i].reg_value = 0ULL;
	}

	if (pfm_write_pmds(ctx_fd, pd, outp.pfp_pmd_count))
		fatal_error("pfm_write_pmds error errno %d\n",errno);
	/*
	 * Let's roll now
	 */
	if (pfm_start(ctx_fd, NULL))
		fatal_error("pfm_start error errno %d\n",errno);

	noploop(ITERS);

	if (pfm_stop(ctx_fd))
		fatal_error("pfm_stop error errno %d\n",errno);

	/*
	 * now read the results. We use pfp_event_count because
	 * libpfm guarantees that counters for the events always
	 * come first.
	 */
	if (pfm_read_pmds(ctx_fd, pd, inp.pfp_event_count))
		fatal_error( "pfm_read_pmds error errno %d\n",errno);

	/*
	 * print the results
	 */
	for (i=0; i < inp.pfp_event_count; i++) {
		pfm_get_full_event_name(&inp.pfp_events[i], name, len+1);
		printf("PMD%-3u %20"PRIu64" %s\n",
			pd[i].reg_num,
			pd[i].reg_value,
			name);
		pd[i].reg_value = 0ULL;
	}

	if (pfm_write_pmds(ctx_fd, pd, outp.pfp_pmd_count))
		fatal_error("pfm_write_pmds error errno %d\n",errno);
	/*
	 * Let's roll now
	 */
	if (pfm_start(ctx_fd, NULL))
		fatal_error("pfm_start error errno %d\n",errno);

	noploop(ITERS);

	if (pfm_stop(ctx_fd))
		fatal_error("pfm_stop error errno %d\n",errno);

	/*
	 * now read the results. We use pfp_event_count because
	 * libpfm guarantees that counters for the events always
	 * come first.
	 */
	if (pfm_read_pmds(ctx_fd, pd, inp.pfp_event_count))
		fatal_error( "pfm_read_pmds error errno %d\n",errno);

	/*
	 * print the results
	 */
	for (i=0; i < inp.pfp_event_count; i++) {
		pfm_get_full_event_name(&inp.pfp_events[i], name, len+1);
		printf("PMD%-3u %20"PRIu64" %s\n",
			pd[i].reg_num,
			pd[i].reg_value,
			name);
		pd[i].reg_value = 0ULL;
	}
	free(name);
	/*
	 * and destroy our context
	 */
	close(ctx_fd);

	return 0;
}
_______________________________________________
perfmon mailing list
[email protected]
http://www.hpl.hp.com/hosted/linux/mail-archives/perfmon/

Reply via email to