|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* self_smpl.c - example of self sampling using a kernel samplig buffer
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Copyright (c) 2009 Google, Inc
|
|
Packit |
577717 |
* Contributed by Stephane Eranian <eranian@gmail.com>
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Based on mont_dear.c from:
|
|
Packit |
577717 |
* Copyright (c) 2005-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 |
* 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 <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 <fcntl.h>
|
|
Packit |
577717 |
#include <sys/mman.h>
|
|
Packit |
577717 |
#include <inttypes.h>
|
|
Packit |
577717 |
#include <err.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#include <perfmon/pfmlib.h>
|
|
Packit |
577717 |
#include <perfmon/perfmon.h>
|
|
Packit |
577717 |
#include <perfmon/perfmon_dfl_smpl.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define SMPL_PERIOD (2400000)
|
|
Packit |
577717 |
|
|
Packit |
577717 |
typedef pfm_dfl_smpl_hdr_t smpl_hdr_t;
|
|
Packit |
577717 |
typedef pfm_dfl_smpl_entry_t smpl_entry_t;
|
|
Packit |
577717 |
typedef pfm_dfl_smpl_arg_t smpl_arg_t;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static int fd;
|
|
Packit |
577717 |
static void *smpl_vaddr;
|
|
Packit |
577717 |
static size_t entry_size;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
long
|
|
Packit |
577717 |
do_test(unsigned long size)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
unsigned long i, sum = 0;
|
|
Packit |
577717 |
int *array;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
array = (int *)malloc(size * sizeof(int));
|
|
Packit |
577717 |
if (array == NULL ) {
|
|
Packit |
577717 |
printf("line = %d No memory available!\n", __LINE__);
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
for(i=0; i
|
|
Packit |
577717 |
array[i]=1;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
free(array);
|
|
Packit |
577717 |
return sum;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
process_smpl_buffer(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
static uint64_t last_ovfl = ~0UL;
|
|
Packit |
577717 |
static uint64_t smpl_entry = 0;
|
|
Packit |
577717 |
smpl_hdr_t *hdr;
|
|
Packit |
577717 |
smpl_entry_t *ent;
|
|
Packit |
577717 |
unsigned long pos;
|
|
Packit |
577717 |
uint64_t count;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
hdr = (smpl_hdr_t *)smpl_vaddr;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* check that we are not diplaying the previous set of samples again.
|
|
Packit |
577717 |
* Required to take care of the last batch of samples.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (hdr->hdr_overflows <= last_ovfl && last_ovfl != ~0UL) {
|
|
Packit |
577717 |
printf("skipping identical set of samples %"PRIu64" <= %"PRIu64"\n", hdr->hdr_overflows, last_ovfl);
|
|
Packit |
577717 |
return;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
pos = (unsigned long)(hdr+1);
|
|
Packit |
577717 |
count = hdr->hdr_count;
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* walk through all the entries recored in the buffer
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
while(count--) {
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ent = (smpl_entry_t *)pos;
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* print entry header
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
printf("Entry %"PRIu64" PID:%d TID:%d CPU:%d STAMP:0x%"PRIx64" IIP:0x%016"PRIx64"\n",
|
|
Packit |
577717 |
smpl_entry++,
|
|
Packit |
577717 |
ent->tgid,
|
|
Packit |
577717 |
ent->pid,
|
|
Packit |
577717 |
ent->cpu,
|
|
Packit |
577717 |
ent->tstamp,
|
|
Packit |
577717 |
ent->ip);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* move to next entry
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pos += entry_size;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
overflow_handler(int n, struct siginfo *info, struct sigcontext *sc)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
process_smpl_buffer();
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* And resume monitoring
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_restart(fd))
|
|
Packit |
577717 |
errx(1, "pfm_restart");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
main(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
pfarg_pmd_t pd[8];
|
|
Packit |
577717 |
pfarg_pmc_t pc[8];
|
|
Packit |
577717 |
pfmlib_input_param_t inp;
|
|
Packit |
577717 |
pfmlib_output_param_t outp;
|
|
Packit |
577717 |
pfarg_ctx_t ctx;
|
|
Packit |
577717 |
pfarg_load_t load_args;
|
|
Packit |
577717 |
smpl_arg_t buf_arg;
|
|
Packit |
577717 |
pfmlib_options_t pfmlib_options;
|
|
Packit |
577717 |
unsigned long nloop = 10000;
|
|
Packit |
577717 |
struct sigaction act;
|
|
Packit |
577717 |
unsigned int i;
|
|
Packit |
577717 |
int ret;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Initialize pfm library (required before we can use it)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_initialize() != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
errx(1, "cannot initialize library\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Install the overflow handler (SIGIO)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
memset(&act, 0, sizeof(act));
|
|
Packit |
577717 |
act.sa_handler = (sig_t)overflow_handler;
|
|
Packit |
577717 |
sigaction (SIGIO, &act, 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 debug */
|
|
Packit |
577717 |
pfm_set_options(&pfmlib_options);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
memset(pd, 0, sizeof(pd));
|
|
Packit |
577717 |
memset(pc, 0, sizeof(pc));
|
|
Packit |
577717 |
memset(pc, 0, sizeof(pc));
|
|
Packit |
577717 |
memset(&ctx, 0, sizeof(ctx));
|
|
Packit |
577717 |
memset(&buf_arg, 0, sizeof(buf_arg));
|
|
Packit |
577717 |
memset(&load_args, 0, sizeof(load_args));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* prepare parameters to library. we don't use any Itanium
|
|
Packit |
577717 |
* specific features here. so the pfp_model is NULL.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
memset(&inp,0, sizeof(inp));
|
|
Packit |
577717 |
memset(&outp,0, sizeof(outp));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* To count the number of occurence of this instruction, we must
|
|
Packit |
577717 |
* program a counting monitor with the IA64_TAGGED_INST_RETIRED_PMC8
|
|
Packit |
577717 |
* event.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_get_cycle_event(&inp.pfp_events[0]) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
errx(1, "cannot find cycle event\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* set the (global) privilege mode:
|
|
Packit |
577717 |
* PFM_PLM0 : kernel level only
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
inp.pfp_dfl_plm = PFM_PLM3|PFM_PLM0;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* how many counters we use
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
inp.pfp_event_count = 1;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* let the library figure out the values for the PMCS
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* We use all global settings for this EAR.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if ((ret=pfm_dispatch_events(&inp, NULL, &outp, NULL)) != PFMLIB_SUCCESS)
|
|
Packit |
577717 |
errx(1, "cannot configure events: %s\n", pfm_strerror(ret));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* the size of the buffer is indicated in bytes (not entries).
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* The kernel will record into the buffer up to a certain point.
|
|
Packit |
577717 |
* No partial samples are ever recorded.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
buf_arg.buf_size = getpagesize();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* do not generate overflow notification messages
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ctx.ctx_flags = PFM_FL_OVFL_NO_MSG;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* now create the context for self monitoring/per-task
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
fd = pfm_create_context(&ctx, PFM_DFL_SMPL_NAME, &buf_arg, sizeof(buf_arg));
|
|
Packit |
577717 |
if (fd == -1) {
|
|
Packit |
577717 |
if (errno == ENOSYS)
|
|
Packit |
577717 |
errx(1, "kernel does not have performance monitoring support!\n");
|
|
Packit |
577717 |
errx(1, "cannot create PFM context %s\n", strerror(errno));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* retrieve the virtual address at which the sampling
|
|
Packit |
577717 |
* buffer has been mapped
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
smpl_vaddr = mmap(NULL, (size_t)buf_arg.buf_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
Packit |
577717 |
if (smpl_vaddr == MAP_FAILED)
|
|
Packit |
577717 |
errx(1, "cannot mmap sampling buffer errno %d\n", errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("Sampling buffer mapped at %p\n", smpl_vaddr);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Now prepare the argument to initialize the PMDs and PMCS.
|
|
Packit |
577717 |
* We must pfp_pmc_count to determine the number of PMC to intialize.
|
|
Packit |
577717 |
* We must use pfp_event_count to determine the number of PMD to initialize.
|
|
Packit |
577717 |
* Some events causes extra PMCs to be used, so pfp_pmc_count may be >= pfp_event_count.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* This step is new compared to libpfm-2.x. It is necessary because the library no
|
|
Packit |
577717 |
* longer knows about the kernel data structures.
|
|
Packit |
577717 |
*/
|
|
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 |
/*
|
|
Packit |
577717 |
* figure out pmd mapping from output pmc
|
|
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 |
* indicate we want notification when buffer is full
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pd[0].reg_flags |= PFM_REGFL_OVFL_NOTIFY;
|
|
Packit |
577717 |
entry_size = sizeof(smpl_entry_t);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* initialize the PMD and the sampling period
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pd[0].reg_value = - SMPL_PERIOD;
|
|
Packit |
577717 |
pd[0].reg_long_reset = - SMPL_PERIOD;
|
|
Packit |
577717 |
pd[0].reg_short_reset = - SMPL_PERIOD;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Now program the registers
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* We don't use the save variable to indicate the number of elements passed to
|
|
Packit |
577717 |
* the kernel because, as we said earlier, pc may contain more elements than
|
|
Packit |
577717 |
* the number of events we specified, i.e., contains more thann coutning monitors.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_write_pmcs(fd, pc, outp.pfp_pmc_count))
|
|
Packit |
577717 |
errx(1, "pfm_write_pmcs error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pfm_write_pmds(fd, pd, outp.pfp_pmd_count))
|
|
Packit |
577717 |
errx(1, "pfm_write_pmds error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* attach context to stopped task
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
load_args.load_pid = getpid();
|
|
Packit |
577717 |
if (pfm_load_context(fd, &load_args))
|
|
Packit |
577717 |
errx(1, "pfm_load_context error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* setup asynchronous notification on the file descriptor
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_ASYNC);
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
errx(1, "cannot set ASYNC: %s\n", strerror(errno));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* get ownership of the descriptor
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = fcntl(fd, F_SETOWN, getpid());
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
errx(1, "cannot setown: %s\n", strerror(errno));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Let's roll now.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = pfm_start(fd, NULL);
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
errx(1, "cannot pfm_start: %s\n", strerror(errno));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
while(nloop--)
|
|
Packit |
577717 |
do_test(100000);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = pfm_stop(fd);
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
errx(1, "cannot pfm_stop: %s\n", strerror(errno));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* We must call the processing routine to cover the last entries recorded
|
|
Packit |
577717 |
* in the sampling buffer, i.e. which may not be full
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
process_smpl_buffer();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* let's stop this now
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
munmap(smpl_vaddr, (size_t)buf_arg.buf_size);
|
|
Packit |
577717 |
close(fd);
|
|
Packit |
577717 |
return 0;
|
|
Packit |
577717 |
}
|