|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* syst_multi_np.c - simple system-wide counting no thread
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Copyright (c) 2009 Google, Inc
|
|
Packit |
577717 |
* Contributed by Stephane Eranian <eranian@gmail.com>
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Based on:
|
|
Packit |
577717 |
* Copyright (c) 2002-2006 Hewlett-Packard Development Company, L.P.
|
|
Packit |
577717 |
* Contributed by Stephane Eranian <eranian@hpl.hp.com>
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
Packit |
577717 |
* of this software and associated documentation files (the "Software"), to deal
|
|
Packit |
577717 |
* in the Software without restriction, including without limitation the rights
|
|
Packit |
577717 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
Packit |
577717 |
* of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
Packit |
577717 |
* subject to the following conditions:
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* The above copyright notice and this permission notice shall be included in all
|
|
Packit |
577717 |
* copies or substantial portions of the Software.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
Packit |
577717 |
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
Packit |
577717 |
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
Packit |
577717 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
Packit |
577717 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
Packit |
577717 |
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
#include <sys/types.h>
|
|
Packit |
577717 |
#include <inttypes.h>
|
|
Packit |
577717 |
#include <stdio.h>
|
|
Packit |
577717 |
#include <stdlib.h>
|
|
Packit |
577717 |
#include <stdarg.h>
|
|
Packit |
577717 |
#include <errno.h>
|
|
Packit |
577717 |
#include <unistd.h>
|
|
Packit |
577717 |
#include <string.h>
|
|
Packit |
577717 |
#include <err.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#include <perfmon/pfmlib.h>
|
|
Packit |
577717 |
#include <perfmon/perfmon.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#include "detect_pmcs.h"
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define NUM_PMCS PFMLIB_MAX_PMCS
|
|
Packit |
577717 |
#define NUM_PMDS PFMLIB_MAX_PMDS
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define MAX_EVT_NAME_LEN 128
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
main(int argc, char **argv)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
char **p;
|
|
Packit |
577717 |
pfarg_pmc_t pc[NUM_PMCS];
|
|
Packit |
577717 |
pfarg_pmd_t pd[NUM_PMDS];
|
|
Packit |
577717 |
pfarg_ctx_t ctx[1];
|
|
Packit |
577717 |
pfarg_load_t load_args;
|
|
Packit |
577717 |
pfmlib_input_param_t inp;
|
|
Packit |
577717 |
pfmlib_output_param_t outp;
|
|
Packit |
577717 |
pfmlib_options_t pfmlib_options;
|
|
Packit |
577717 |
uint64_t *pdo;
|
|
Packit |
577717 |
int *fds;
|
|
Packit |
577717 |
int ret, i;
|
|
Packit |
577717 |
unsigned int l;
|
|
Packit |
577717 |
long n, ncpus;
|
|
Packit |
577717 |
unsigned int num_counters;
|
|
Packit |
577717 |
char name[MAX_EVT_NAME_LEN];
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* pass options to library (optional)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
memset(&pfmlib_options, 0, sizeof(pfmlib_options));
|
|
Packit |
577717 |
pfmlib_options.pfm_debug = 0; /* set to 1 for debug */
|
|
Packit |
577717 |
pfm_set_options(&pfmlib_options);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Initialize pfm library (required before we can use it)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = pfm_initialize();
|
|
Packit |
577717 |
if (ret != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
errx(1, "Cannot initialize library: %s", pfm_strerror(ret));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pfm_get_num_counters(&num_counters);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
memset(pc, 0, sizeof(pc));
|
|
Packit |
577717 |
memset(pd, 0, sizeof(pd));
|
|
Packit |
577717 |
memset(ctx, 0, sizeof(ctx));
|
|
Packit |
577717 |
memset(&inp,0, sizeof(inp));
|
|
Packit |
577717 |
memset(&outp,0, sizeof(outp));
|
|
Packit |
577717 |
memset(&load_args,0, sizeof(load_args));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* be nice to user!
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (argc > 1) {
|
|
Packit |
577717 |
p = argv+1;
|
|
Packit |
577717 |
for (i=0; *p ; i++, p++) {
|
|
Packit |
577717 |
if (pfm_find_full_event(*p, &inp.pfp_events[i]) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
errx(1, "cannot find %s event", *p);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
} else {
|
|
Packit |
577717 |
if (pfm_get_cycle_event(&inp.pfp_events[0]) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
errx(1, "cannot find cycle event");
|
|
Packit |
577717 |
if (pfm_get_inst_retired_event(&inp.pfp_events[1]) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
errx(1, "cannot find inst retired event");
|
|
Packit |
577717 |
i = 2;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* set the privilege mode:
|
|
Packit |
577717 |
* PFM_PLM3 : user level
|
|
Packit |
577717 |
* PFM_PLM0 : kernel level
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
inp.pfp_dfl_plm = PFM_PLM3|PFM_PLM0;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (i > num_counters) {
|
|
Packit |
577717 |
i = num_counters;
|
|
Packit |
577717 |
warnx("too many events provided (max=%d events), using first %d event(s)", num_counters, i);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* how many counters we use
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
inp.pfp_event_count = i;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* indicate we are using the monitors for a system-wide session.
|
|
Packit |
577717 |
* This may impact the way the library sets up the PMC values.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
inp.pfp_flags = PFMLIB_PFP_SYSTEMWIDE;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* In system wide mode, the perfmon context cannot be inherited.
|
|
Packit |
577717 |
* Also in this mode, we cannot use the blocking form of user level notification.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ctx[0].ctx_flags = PFM_FL_SYSTEM_WIDE;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* build the pfp_unavail_pmcs bitmask by looking
|
|
Packit |
577717 |
* at what perfmon has available. It is not always
|
|
Packit |
577717 |
* the case that all PMU registers are actually available
|
|
Packit |
577717 |
* to applications. For instance, on IA-32 platforms, some
|
|
Packit |
577717 |
* registers may be reserved for the NMI watchdog timer.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* With this bitmap, the library knows which registers NOT to
|
|
Packit |
577717 |
* use. Of source, it is possible that no valid assignement may
|
|
Packit |
577717 |
* be possible if certina PMU registers are not available.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
detect_unavail_pmcs(-1, &inp.pfp_unavail_pmcs);
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* let the library figure out the values for the PMCS
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if ((ret=pfm_dispatch_events(&inp, NULL, &outp, NULL)) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
errx(1, "cannot configure events: %s", pfm_strerror(ret));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Now prepare the argument to initialize the PMDs and PMCS.
|
|
Packit |
577717 |
* We use pfp_pmc_count to determine the number of PMC to intialize.
|
|
Packit |
577717 |
* We use pfp_pmd_count to determine the number of PMD to initialize.
|
|
Packit |
577717 |
* Some events/features may cause extra PMCs to be used, leading to:
|
|
Packit |
577717 |
* - pfp_pmc_count may be >= pfp_event_count
|
|
Packit |
577717 |
* - pfp_pmd_count may be >= pfp_event_count
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
for (i=0; i < outp.pfp_pmc_count; i++) {
|
|
Packit |
577717 |
pc[i].reg_num = outp.pfp_pmcs[i].reg_num;
|
|
Packit |
577717 |
pc[i].reg_value = outp.pfp_pmcs[i].reg_value;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for (i=0; i < outp.pfp_pmd_count; i++)
|
|
Packit |
577717 |
pd[i].reg_num = outp.pfp_pmds[i].reg_num;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ncpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
Packit |
577717 |
if (ncpus < 1)
|
|
Packit |
577717 |
ncpus = 1;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds = malloc(ncpus * sizeof(int));
|
|
Packit |
577717 |
if (!fds)
|
|
Packit |
577717 |
err(1, "cannot allocate fds");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pdo = calloc(ncpus * outp.pfp_pmd_count, sizeof(uint64_t));
|
|
Packit |
577717 |
if (!pdo)
|
|
Packit |
577717 |
err(1, "cannot allocate pdo");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("ncpus=%ld pmd=%d\n", ncpus, outp.pfp_pmd_count);
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* create and load contexts
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
for(n=0; n < ncpus; n++) {
|
|
Packit |
577717 |
fds[n] = pfm_create_context(ctx, NULL, NULL, 0);
|
|
Packit |
577717 |
if (fds[n] == -1) {
|
|
Packit |
577717 |
if (errno == ENOSYS) {
|
|
Packit |
577717 |
errx(1, "your kernel does not have performance monitoring support!\n");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
err(1, "cannot create context");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
if (pfm_write_pmcs(fds[n], pc, outp.pfp_pmc_count))
|
|
Packit |
577717 |
err(1, "pfm_write_pmcs error");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pfm_write_pmds(fds[n], pd, outp.pfp_pmd_count))
|
|
Packit |
577717 |
err(1, "pfm_write_pmds error");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
load_args.load_pid = n;
|
|
Packit |
577717 |
if (pfm_load_context(fds[n], &load_args))
|
|
Packit |
577717 |
err(1, "pfm_load_context error");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* start all
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
for(n=0; n < ncpus; n++) {
|
|
Packit |
577717 |
if (pfm_start(fds[n], NULL) == -1)
|
|
Packit |
577717 |
err(1, "pfm_start error");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for(l=0; l < 20; l++) {
|
|
Packit |
577717 |
sleep(1);
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* read the results
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
puts("------------------------");
|
|
Packit |
577717 |
for(n=0; n < ncpus; n++) {
|
|
Packit |
577717 |
if (pfm_read_pmds(fds[n], pd, outp.pfp_pmd_count))
|
|
Packit |
577717 |
err(1, "pfm_read_pmds error");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for (i=0; i < outp.pfp_pmd_count; i++) {
|
|
Packit |
577717 |
pfm_get_full_event_name(&inp.pfp_events[i], name, MAX_EVT_NAME_LEN);
|
|
Packit |
577717 |
printf("CPU%-2ld PMD%-3u %-20"PRIu64" %s\n",
|
|
Packit |
577717 |
n,
|
|
Packit |
577717 |
pd[i].reg_num,
|
|
Packit |
577717 |
pd[i].reg_value - pdo[n*outp.pfp_pmd_count+i],
|
|
Packit |
577717 |
name);
|
|
Packit |
577717 |
pdo[n*outp.pfp_pmd_count+i] = pd[i].reg_value;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
for(n=0; n < ncpus; n++)
|
|
Packit |
577717 |
close(fds[n]);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
free(fds);
|
|
Packit |
577717 |
free(pdo);
|
|
Packit |
577717 |
return 0;
|
|
Packit |
577717 |
}
|