|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* self_pipe.c - dual process ping-pong example to stress PMU context switch of one process
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Copyright (c) 2008 Stephane Eranian
|
|
Packit |
577717 |
* Contributed by Stephane Eranian <eranian@gmail.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 |
* This file is part of libpfm, a performance monitoring support library for
|
|
Packit |
577717 |
* applications on Linux.
|
|
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 <signal.h>
|
|
Packit |
577717 |
#include <syscall.h>
|
|
Packit |
577717 |
#include <perfmon/pfmlib.h>
|
|
Packit |
577717 |
#include <perfmon/perfmon.h>
|
|
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 |
static void fatal_error(char *fmt,...) __attribute__((noreturn));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
fatal_error(char *fmt, ...)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
va_list ap;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
va_start(ap, fmt);
|
|
Packit |
577717 |
vfprintf(stderr, fmt, ap);
|
|
Packit |
577717 |
va_end(ap);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* pin task to CPU
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
#ifndef __NR_sched_setaffinity
|
|
Packit |
577717 |
#error "you need to define __NR_sched_setaffinity"
|
|
Packit |
577717 |
#endif
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define MAX_CPUS 2048
|
|
Packit |
577717 |
#define NR_CPU_BITS (MAX_CPUS>>3)
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
pin_cpu(pid_t pid, unsigned int cpu)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
uint64_t my_mask[NR_CPU_BITS];
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (cpu >= MAX_CPUS)
|
|
Packit |
577717 |
fatal_error("this program supports only up to %d CPUs\n", MAX_CPUS);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
my_mask[cpu>>6] = 1ULL << (cpu&63);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
return syscall(__NR_sched_setaffinity, pid, sizeof(my_mask), &my_mask);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static volatile int quit;
|
|
Packit |
577717 |
void sig_handler(int n)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
quit = 1;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
do_child(int fr, int fw)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
char c;
|
|
Packit |
577717 |
ssize_t ret;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for(;;) {
|
|
Packit |
577717 |
ret = read(fr, &c, 1);
|
|
Packit |
577717 |
if (ret < 0)
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
ret = write(fw, "c", 1);
|
|
Packit |
577717 |
if (ret < 0)
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
printf("child exited\n");
|
|
Packit |
577717 |
exit(0);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
main(int argc, char **argv)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
char **p;
|
|
Packit |
577717 |
unsigned int i;
|
|
Packit |
577717 |
int ret, ctx_fd;
|
|
Packit |
577717 |
pfmlib_input_param_t inp;
|
|
Packit |
577717 |
pfmlib_output_param_t outp;
|
|
Packit |
577717 |
pfarg_pmd_t pd[NUM_PMDS];
|
|
Packit |
577717 |
pfarg_pmc_t pc[NUM_PMCS];
|
|
Packit |
577717 |
pfarg_ctx_t ctx;
|
|
Packit |
577717 |
pfarg_load_t load_args;
|
|
Packit |
577717 |
pfmlib_options_t pfmlib_options;
|
|
Packit |
577717 |
unsigned int num_counters;
|
|
Packit |
577717 |
int pr[2], pw[2];
|
|
Packit |
577717 |
int which_cpu;
|
|
Packit |
577717 |
pid_t pid;
|
|
Packit |
577717 |
size_t len;
|
|
Packit |
577717 |
ssize_t nbytes;
|
|
Packit |
577717 |
char *name;
|
|
Packit |
577717 |
char c = '0';
|
|
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 |
pfmlib_options.pfm_verbose = 1; /* set to 1 for verbose */
|
|
Packit |
577717 |
pfm_set_options(&pfmlib_options);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
srandom(getpid());
|
|
Packit |
577717 |
which_cpu = random() % sysconf(_SC_NPROCESSORS_ONLN);
|
|
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 |
fatal_error("Cannot initialize library: %s\n", pfm_strerror(ret));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = pipe(pr);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
fatal_error("cannot create read pipe: %s\n", strerror(errno));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = pipe(pw);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
fatal_error("cannot create write pipe: %s\n", strerror(errno));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pfm_get_max_event_name_len(&len;;
|
|
Packit |
577717 |
name = malloc(len+1);
|
|
Packit |
577717 |
if (!name)
|
|
Packit |
577717 |
fatal_error("cannot allocate event name buffer\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Pin to CPU0, inherited by child process. That will enforce
|
|
Packit |
577717 |
* the ping-pionging and thus stress the PMU context switch
|
|
Packit |
577717 |
* which is what we want
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = pin_cpu(getpid(), which_cpu);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
fatal_error("cannot pin to CPU%d: %s\n", which_cpu, strerror(errno));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("Both processes pinned to CPU%d\n", which_cpu);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pfm_get_num_counters(&num_counters);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
memset(pd, 0, sizeof(pd));
|
|
Packit |
577717 |
memset(pc, 0, sizeof(pc));
|
|
Packit |
577717 |
memset(&ctx, 0, sizeof(ctx));
|
|
Packit |
577717 |
memset(&load_args, 0, sizeof(load_args));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* prepare parameters to library.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
memset(&inp,0, sizeof(inp));
|
|
Packit |
577717 |
memset(&outp,0, sizeof(outp));
|
|
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 |
ret = pfm_find_full_event(*p, &inp.pfp_events[i]);
|
|
Packit |
577717 |
if (ret != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
fatal_error("event %s: %s\n", *p, pfm_strerror(ret));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
} else {
|
|
Packit |
577717 |
if (pfm_get_cycle_event(&inp.pfp_events[0]) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
fatal_error("cannot find cycle event\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pfm_get_inst_retired_event(&inp.pfp_events[1]) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
fatal_error("cannot find inst retired event\n");
|
|
Packit |
577717 |
i = 2;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* set the default privilege mode for all counters:
|
|
Packit |
577717 |
* PFM_PLM3 : user level only
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
inp.pfp_dfl_plm = PFM_PLM3;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (i > num_counters) {
|
|
Packit |
577717 |
i = num_counters;
|
|
Packit |
577717 |
printf("too many events provided (max=%d events), using first %d event(s)\n", num_counters, i);
|
|
Packit |
577717 |
}
|
|
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 |
* now create a new context, per process context.
|
|
Packit |
577717 |
* This just creates a new context with some initial state, it is not
|
|
Packit |
577717 |
* active nor attached to any process.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ctx_fd = pfm_create_context(&ctx, NULL, NULL, 0);
|
|
Packit |
577717 |
if (ctx_fd == -1) {
|
|
Packit |
577717 |
if (errno == ENOSYS)
|
|
Packit |
577717 |
fatal_error("Your kernel does not have performance monitoring support!\n");
|
|
Packit |
577717 |
fatal_error("Can't create PFM context %s\n", strerror(errno));
|
|
Packit |
577717 |
}
|
|
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 certain PMU registers are not available.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
detect_unavail_pmcs(ctx_fd, &inp.pfp_unavail_pmcs);
|
|
Packit |
577717 |
|
|
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 |
fatal_error("cannot configure events: %s\n", 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 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Now program the registers
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_write_pmcs(ctx_fd, pc, outp.pfp_pmc_count))
|
|
Packit |
577717 |
fatal_error("pfm_write_pmcs error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pfm_write_pmds(ctx_fd, pd, outp.pfp_pmd_count))
|
|
Packit |
577717 |
fatal_error("pfm_write_pmds error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* now we load (i.e., attach) the context to ourself
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
load_args.load_pid = getpid();
|
|
Packit |
577717 |
if (pfm_load_context(ctx_fd, &load_args))
|
|
Packit |
577717 |
fatal_error("pfm_load_context error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* create second process which is not monitoring at the moment
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
switch(pid=fork()) {
|
|
Packit |
577717 |
case -1:
|
|
Packit |
577717 |
fatal_error("cannot create child\n");
|
|
Packit |
577717 |
case 0:
|
|
Packit |
577717 |
/* do not inherit context fd */
|
|
Packit |
577717 |
close(ctx_fd);
|
|
Packit |
577717 |
/* pr[]: write master, read child */
|
|
Packit |
577717 |
/* pw[]: read master, write child */
|
|
Packit |
577717 |
close(pr[1]); close(pw[0]);
|
|
Packit |
577717 |
do_child(pr[0], pw[1]);
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
close(pr[0]);
|
|
Packit |
577717 |
close(pw[1]);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Let's roll now
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_start(ctx_fd, NULL))
|
|
Packit |
577717 |
fatal_error("pfm_start error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
signal(SIGALRM, sig_handler);
|
|
Packit |
577717 |
alarm(10);
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* ping pong loop
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
while(!quit) {
|
|
Packit |
577717 |
nbytes = write(pr[1], "c", 1);
|
|
Packit |
577717 |
nbytes = read(pw[0], &c, 1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
(void)nbytes;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pfm_stop(ctx_fd))
|
|
Packit |
577717 |
fatal_error("pfm_stop error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* now read the results. We use pfp_event_count because
|
|
Packit |
577717 |
* libpfm guarantees that counters for the events always
|
|
Packit |
577717 |
* come first.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_read_pmds(ctx_fd, pd, inp.pfp_event_count))
|
|
Packit |
577717 |
fatal_error( "pfm_read_pmds error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* print the results
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
for (i=0; i < inp.pfp_event_count; i++) {
|
|
Packit |
577717 |
pfm_get_full_event_name(&inp.pfp_events[i], name, len+1);
|
|
Packit |
577717 |
printf("PMD%-3u %20"PRIu64" %s\n",
|
|
Packit |
577717 |
pd[i].reg_num,
|
|
Packit |
577717 |
pd[i].reg_value,
|
|
Packit |
577717 |
name);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
free(name);
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* kill child process
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
kill(SIGKILL, pid);
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* close pipes
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
close(pr[1]);
|
|
Packit |
577717 |
close(pw[0]);
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* and destroy our context
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
close(ctx_fd);
|
|
Packit |
577717 |
return 0;
|
|
Packit |
577717 |
}
|