|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* smpl_pebs.c - PEBS standalone (no libpfm) sampling example for P4/Xeon
|
|
Packit |
577717 |
* (support 32-bit and 64-bit modes)
|
|
Packit |
577717 |
*
|
|
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 |
#ifndef _GNU_SOURCE
|
|
Packit |
577717 |
#define _GNU_SOURCE /* for getline */
|
|
Packit |
577717 |
#endif
|
|
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 <unistd.h>
|
|
Packit |
577717 |
#include <sys/wait.h>
|
|
Packit |
577717 |
#include <sys/ptrace.h>
|
|
Packit |
577717 |
#include <sys/mman.h>
|
|
Packit |
577717 |
#include <perfmon/perfmon.h>
|
|
Packit |
577717 |
#include <perfmon/perfmon_pebs_p4_smpl.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define NUM_PMCS 32
|
|
Packit |
577717 |
#define NUM_PMDS 32
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define SMPL_PERIOD 100000ULL /* must not use more bits than actual HW counter width */
|
|
Packit |
577717 |
|
|
Packit |
577717 |
typedef pfm_pebs_p4_smpl_hdr_t smpl_hdr_t;
|
|
Packit |
577717 |
typedef pfm_pebs_p4_smpl_entry_t smpl_entry_t;
|
|
Packit |
577717 |
typedef pfm_pebs_p4_smpl_arg_t smpl_arg_t;
|
|
Packit |
577717 |
#define FMT_NAME PFM_PEBS_P4_SMPL_NAME
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static uint64_t collected_samples;
|
|
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 |
static void
|
|
Packit |
577717 |
warning(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 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
child(char **arg)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* force the task to stop before executing the first
|
|
Packit |
577717 |
* user level instruction
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
execvp(arg[0], arg);
|
|
Packit |
577717 |
/* not reached */
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
process_smpl_buf(smpl_hdr_t *hdr)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
static uint64_t last_overflow = ~0; /* initialize to biggest value possible */
|
|
Packit |
577717 |
static uint64_t last_count;
|
|
Packit |
577717 |
smpl_entry_t *ent;
|
|
Packit |
577717 |
unsigned long count;
|
|
Packit |
577717 |
uint64_t entry;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
count = (hdr->ds.pebs_index - hdr->ds.pebs_buf_base)/sizeof(*ent);
|
|
Packit |
577717 |
if (hdr->overflows == last_overflow && last_count == count) {
|
|
Packit |
577717 |
warning("skipping identical set of samples %"PRIu64" = %"PRIu64"\n",
|
|
Packit |
577717 |
hdr->overflows, last_overflow);
|
|
Packit |
577717 |
return;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
last_count = count;
|
|
Packit |
577717 |
last_overflow = hdr->overflows;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* the beginning of the buffer does not necessarily follow the header
|
|
Packit |
577717 |
* due to alignement.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ent = (smpl_entry_t *)((unsigned long)(hdr+1)+ hdr->start_offs);
|
|
Packit |
577717 |
entry = collected_samples;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
while(count--) {
|
|
Packit |
577717 |
printf("entry %06"PRIu64" eflags:0x%08lx EAX:0x%08lx ESP:0x%08lx IP:0x%08lx\n",
|
|
Packit |
577717 |
entry,
|
|
Packit |
577717 |
ent->eflags,
|
|
Packit |
577717 |
ent->eax,
|
|
Packit |
577717 |
ent->esp,
|
|
Packit |
577717 |
ent->ip);
|
|
Packit |
577717 |
ent++;
|
|
Packit |
577717 |
entry++;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
collected_samples = entry;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static int
|
|
Packit |
577717 |
get_cpuinfo_attr(char *attr, char *ret_buf, size_t maxlen)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
FILE *fp;
|
|
Packit |
577717 |
int ret = -1;
|
|
Packit |
577717 |
size_t attr_len, buf_len = 0;
|
|
Packit |
577717 |
char *p, *value = NULL;
|
|
Packit |
577717 |
char *buffer = NULL;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (attr == NULL || ret_buf == NULL || maxlen < 1)
|
|
Packit |
577717 |
return -1;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
attr_len = strlen(attr);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fp = fopen("/proc/cpuinfo", "r");
|
|
Packit |
577717 |
if (fp == NULL)
|
|
Packit |
577717 |
return -1;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
while(getline(&buffer, &buf_len, fp) != -1){
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* skip blank lines */
|
|
Packit |
577717 |
if (*buffer == '\n')
|
|
Packit |
577717 |
continue;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
p = strchr(buffer, ':');
|
|
Packit |
577717 |
if (p == NULL)
|
|
Packit |
577717 |
goto error;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* p+2: +1 = space, +2= firt character
|
|
Packit |
577717 |
* strlen()-1 gets rid of \n
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
*p = '\0';
|
|
Packit |
577717 |
value = p+2;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
value[strlen(value)-1] = '\0';
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (!strncmp(attr, buffer, attr_len))
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
strncpy(ret_buf, value, maxlen-1);
|
|
Packit |
577717 |
ret_buf[maxlen-1] = '\0';
|
|
Packit |
577717 |
ret = 0;
|
|
Packit |
577717 |
error:
|
|
Packit |
577717 |
free(buffer);
|
|
Packit |
577717 |
fclose(fp);
|
|
Packit |
577717 |
return ret;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
check_valid_cpu(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
int ret, siblings, family, cores;
|
|
Packit |
577717 |
char buffer[128];
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = get_cpuinfo_attr("vendor_id", buffer, sizeof(buffer));
|
|
Packit |
577717 |
if (ret == -1 || strcmp(buffer, "GenuineIntel"))
|
|
Packit |
577717 |
fatal_error("this programs works only with Intel processors\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = get_cpuinfo_attr("cpu family", buffer, sizeof(buffer));
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
fatal_error("cannot determine processor family\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
family = atoi(buffer);
|
|
Packit |
577717 |
if (family != 15)
|
|
Packit |
577717 |
fatal_error("this program only works for P4/Xeon with PEBS (found family=%d)\n", family);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = get_cpuinfo_attr("siblings", buffer, sizeof(buffer));
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
fatal_error("cannot deterimine number of siblings\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
siblings = atoi(buffer);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = get_cpuinfo_attr("cpu cores", buffer, sizeof(buffer));
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
fatal_error("cannot determine number of cpu cores\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
cores = atoi(buffer);
|
|
Packit |
577717 |
if (siblings > cores)
|
|
Packit |
577717 |
fatal_error("PEBS does not work when HyperThreading is enabled\n");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
main(int argc, char **argv)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
pfarg_pmr_t pd[NUM_PMDS];
|
|
Packit |
577717 |
pfarg_pmr_t pc[NUM_PMCS];
|
|
Packit |
577717 |
smpl_arg_t buf_arg;
|
|
Packit |
577717 |
pfarg_msg_t msg;
|
|
Packit |
577717 |
smpl_hdr_t *hdr;
|
|
Packit |
577717 |
void *buf_addr;
|
|
Packit |
577717 |
pid_t pid;
|
|
Packit |
577717 |
int ret, fd, status, npmcs = 0;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
check_valid_cpu();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (argc < 2)
|
|
Packit |
577717 |
fatal_error("you need to pass a program to sample\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
memset(pd, 0, sizeof(pd));
|
|
Packit |
577717 |
memset(pc, 0, sizeof(pc));
|
|
Packit |
577717 |
memset(&buf_arg, 0, sizeof(buf_arg));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
buf_arg.buf_size = getpagesize();
|
|
Packit |
577717 |
buf_arg.cnt_reset = -SMPL_PERIOD;
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* trigger interrupt when reached 90% of buffer
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
buf_arg.intr_thres = (buf_arg.buf_size/sizeof(smpl_entry_t))*90/100;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fd = pfm_create(PFM_FL_SMPL_FMT, NULL, FMT_NAME, &buf_arg, sizeof(buf_arg));
|
|
Packit |
577717 |
if (fd == -1) {
|
|
Packit |
577717 |
if (errno == ENOSYS) {
|
|
Packit |
577717 |
fatal_error("Your kernel does not have performance monitoring support!\n");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
fatal_error("cannot create session %s, maybe you do not have the P4/Xeon PEBS sampling format in the kernel.\n Check /sys/kernel/perfmon\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 |
buf_addr = mmap(NULL, (size_t)buf_arg.buf_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
Packit |
577717 |
if (buf_addr == MAP_FAILED)
|
|
Packit |
577717 |
fatal_error("cannot mmap sampling buffer errno %d\n", errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("session [%d] buffer mapped @%p\n", fd, buf_addr);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
hdr = (smpl_hdr_t *)buf_addr;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("pebs_base=0x%lx pebs_end=0x%lx index=0x%lx\n"
|
|
Packit |
577717 |
"intr=0x%lx version=%u.%u\n"
|
|
Packit |
577717 |
"entry_size=%zu ds_size=%zu\n",
|
|
Packit |
577717 |
hdr->ds.pebs_buf_base,
|
|
Packit |
577717 |
hdr->ds.pebs_abs_max,
|
|
Packit |
577717 |
hdr->ds.pebs_index,
|
|
Packit |
577717 |
hdr->ds.pebs_intr_thres,
|
|
Packit |
577717 |
PFM_VERSION_MAJOR(hdr->version),
|
|
Packit |
577717 |
PFM_VERSION_MINOR(hdr->version),
|
|
Packit |
577717 |
sizeof(smpl_entry_t),
|
|
Packit |
577717 |
sizeof(hdr->ds));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (PFM_VERSION_MAJOR(hdr->version) < 1)
|
|
Packit |
577717 |
fatal_error("invalid buffer format version\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* using the replay_event event
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* CRU_ESCR2.usr=1
|
|
Packit |
577717 |
* CRU_ESCR2.event_mask=1 (NBOGUS)
|
|
Packit |
577717 |
* CRU_ESCR2.event_select=0x9 (replay_event)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pc[npmcs].reg_num = 21;
|
|
Packit |
577717 |
pc[npmcs].reg_value = (9ULL <<25) | (1ULL<<9) |(1ULL<<2);
|
|
Packit |
577717 |
npmcs++;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* for PEBS, must use IQ_CCCR4 for thread0
|
|
Packit |
577717 |
* IQ_CCCR4.escr_select = 5
|
|
Packit |
577717 |
* IQ_CCCR4.enable= 1
|
|
Packit |
577717 |
* IQ_CCCR4.active_thread= 3
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* We must disable 64-bit emulation by the kernel
|
|
Packit |
577717 |
* on the associated counter when using PEBS. Otherwise
|
|
Packit |
577717 |
* we received a spurious interrupt for every counter overflow.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pc[npmcs].reg_num = 31;
|
|
Packit |
577717 |
pc[npmcs].reg_flags = PFM_REGFL_NO_EMUL64;
|
|
Packit |
577717 |
pc[npmcs].reg_value = (5ULL << 13) | (1ULL<<12) | (3ULL<<16);
|
|
Packit |
577717 |
npmcs++;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* PEBS_MATRIX_VERT.bit0=1 (1st level cache load miss retired)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pc[npmcs].reg_num = 63;
|
|
Packit |
577717 |
pc[npmcs].reg_value = 1;
|
|
Packit |
577717 |
npmcs++;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* PEBS_ENABLE.enable=1 (bit0)
|
|
Packit |
577717 |
* PEBS_ENABLE.uops=1 (bit 24)
|
|
Packit |
577717 |
* PEBS_ENABLE.my_thr=1 (bit 25)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pc[npmcs].reg_num = 64;
|
|
Packit |
577717 |
pc[npmcs].reg_value = (1ULL<<25)|(1ULL<<24) | 1ULL;
|
|
Packit |
577717 |
npmcs++;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Must use IQ_CCCR4/IQ_CTR4 with PEBS for thread0
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* IMPORTANT:
|
|
Packit |
577717 |
* SMPL_PERIOD MUST not exceed width of HW counter
|
|
Packit |
577717 |
* because no 64-bit virtualization is done by the
|
|
Packit |
577717 |
* kernel.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
pd[0].reg_num = 8;
|
|
Packit |
577717 |
pd[0].reg_flags = PFM_REGFL_OVFL_NOTIFY;
|
|
Packit |
577717 |
pd[0].reg_value = -SMPL_PERIOD;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Now program the registers
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_write(fd, 0, PFM_RW_PMC, pc, npmcs * sizeof(*pc)) == -1)
|
|
Packit |
577717 |
fatal_error("pfm_writeerror errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pfm_write(fd, 0, PFM_RW_PMD_ATTR, pd, sizeof(*pd)) == -1)
|
|
Packit |
577717 |
fatal_error("pfm_write(PMD) error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
signal(SIGCHLD, SIG_IGN);
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Create the child task
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if ((pid=fork()) == -1) fatal_error("Cannot fork process\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* In order to get the PFM_END_MSG message, it is important
|
|
Packit |
577717 |
* to ensure that the child task does not inherit the file
|
|
Packit |
577717 |
* descriptor of the session. By default, file descriptor
|
|
Packit |
577717 |
* are inherited during exec(). We explicitely close it
|
|
Packit |
577717 |
* here. We could have set it up through fcntl(FD_CLOEXEC)
|
|
Packit |
577717 |
* to achieve the same thing.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pid == 0) {
|
|
Packit |
577717 |
close(fd);
|
|
Packit |
577717 |
child(argv+1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* wait for the child to exec
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
waitpid(pid, &status, WUNTRACED);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* process is stopped at this point
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (WIFEXITED(status)) {
|
|
Packit |
577717 |
warning("task %s [%d] exited already status %d\n", argv[1], pid, WEXITSTATUS(status));
|
|
Packit |
577717 |
goto terminate_session;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* attach the session
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_attach(fd, 0, pid) == -1)
|
|
Packit |
577717 |
fatal_error("pfm_attach error errno %d\n",errno);
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* start monitoring
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_set_state(fd, 0, PFM_ST_START) == -1)
|
|
Packit |
577717 |
fatal_error("pfm_set_state(start) error errno %d\n",errno);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* detach child. Side effect includes
|
|
Packit |
577717 |
* activation of monitoring.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ptrace(PTRACE_DETACH, pid, NULL, 0);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* core loop
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
for(;;) {
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* wait for overflow/end notification messages
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = read(fd, &msg, sizeof(msg));
|
|
Packit |
577717 |
if (ret == -1) {
|
|
Packit |
577717 |
if(ret == -1 && errno == EINTR) {
|
|
Packit |
577717 |
warning("read interrupted, retrying\n");
|
|
Packit |
577717 |
continue;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
fatal_error("cannot read perfmon msg: %s\n", strerror(errno));
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
switch(msg.type) {
|
|
Packit |
577717 |
case PFM_MSG_OVFL: /* the sampling buffer is full */
|
|
Packit |
577717 |
process_smpl_buf(hdr);
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* reactivate monitoring once we are done with the samples
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Note that this call can fail with EBUSY in non-blocking mode
|
|
Packit |
577717 |
* as the task may have disappeared while we were processing
|
|
Packit |
577717 |
* the samples.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if (pfm_set_state(fd, 0, PFM_ST_RESTART) == -1) {
|
|
Packit |
577717 |
if (errno != EBUSY)
|
|
Packit |
577717 |
fatal_error("pfm_set_state(restart)_ error errno %d\n",errno);
|
|
Packit |
577717 |
else
|
|
Packit |
577717 |
warning("pfm_set_state(restart): task has probably terminated \n");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case PFM_MSG_END: /* monitored task terminated */
|
|
Packit |
577717 |
warning("task terminated\n");
|
|
Packit |
577717 |
goto terminate_session;
|
|
Packit |
577717 |
default: fatal_error("unknown message type %d\n", msg.type);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
terminate_session:
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* cleanup child
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
wait4(pid, &status, 0, NULL);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* check for any leftover samples
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
process_smpl_buf(hdr);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
munmap(buf_addr, (size_t)buf_arg.buf_size);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
close(fd);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
return 0;
|
|
Packit |
577717 |
}
|