|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* self.c - example of a simple self monitoring task
|
|
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-2007 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 |
|
|
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 <locale.h>
|
|
Packit |
577717 |
#include <sys/prctl.h>
|
|
Packit |
577717 |
#include <err.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#include <perfmon/pfmlib_perf_event.h>
|
|
Packit |
577717 |
#include "perf_util.h"
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static const char *gen_events[]={
|
|
Packit |
577717 |
"cycles",
|
|
Packit |
577717 |
"instructions",
|
|
Packit |
577717 |
NULL
|
|
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 |
void
|
|
Packit |
577717 |
noploop(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
for(;quit == 0;);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
print_counts(perf_event_desc_t *fds, int num_fds, const char *msg)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
uint64_t val;
|
|
Packit |
577717 |
uint64_t values[3];
|
|
Packit |
577717 |
double ratio;
|
|
Packit |
577717 |
int i;
|
|
Packit |
577717 |
ssize_t ret;
|
|
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 |
memset(values, 0, sizeof(values));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for (i = 0; i < num_fds; i++) {
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = read(fds[i].fd, values, sizeof(values));
|
|
Packit |
577717 |
if (ret < (ssize_t)sizeof(values)) {
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
err(1, "cannot read results: %s", strerror(errno));
|
|
Packit |
577717 |
else
|
|
Packit |
577717 |
warnx("could not read event%d", i);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* scaling is systematic because we may be sharing the PMU and
|
|
Packit |
577717 |
* thus may be multiplexed
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
val = perf_scale(values);
|
|
Packit |
577717 |
ratio = perf_scale_ratio(values);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("%s %'20"PRIu64" %s (%.2f%% scaling, raw=%'"PRIu64", ena=%'"PRIu64", run=%'"PRIu64")\n",
|
|
Packit |
577717 |
msg,
|
|
Packit |
577717 |
val,
|
|
Packit |
577717 |
fds[i].name,
|
|
Packit |
577717 |
(1.0-ratio)*100.0,
|
|
Packit |
577717 |
values[0],
|
|
Packit |
577717 |
values[1],
|
|
Packit |
577717 |
values[2]);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
main(int argc, char **argv)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
perf_event_desc_t *fds = NULL;
|
|
Packit |
577717 |
int i, ret, num_fds = 0;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
setlocale(LC_ALL, "");
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* Initialize pfm library (required before we can use it)
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = pfm_initialize();
|
|
Packit |
577717 |
if (ret != PFM_SUCCESS)
|
|
Packit |
577717 |
errx(1, "Cannot initialize library: %s", pfm_strerror(ret));
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = perf_setup_argv_events(argc > 1 ? (const char **)argv+1 : gen_events, &fds, &num_fds);
|
|
Packit |
577717 |
if (ret || !num_fds)
|
|
Packit |
577717 |
errx(1, "cannot setup events");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[0].fd = -1;
|
|
Packit |
577717 |
for(i=0; i < num_fds; i++) {
|
|
Packit |
577717 |
/* request timing information necessary for scaling */
|
|
Packit |
577717 |
fds[i].hw.read_format = PERF_FORMAT_SCALE;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[i].hw.disabled = 1; /* do not start now */
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* each event is in an independent group (multiplexing likely) */
|
|
Packit |
577717 |
fds[i].fd = perf_event_open(&fds[i].hw, 0, -1, -1, 0);
|
|
Packit |
577717 |
if (fds[i].fd == -1)
|
|
Packit |
577717 |
err(1, "cannot open event %d", i);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
signal(SIGALRM, sig_handler);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* enable all counters attached to this thread and created by it
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = prctl(PR_TASK_PERF_EVENTS_ENABLE);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
err(1, "prctl(enable) failed");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
print_counts(fds, num_fds, "INITIAL: ");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
alarm(10);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
noploop();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* disable all counters attached to this thread
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
ret = prctl(PR_TASK_PERF_EVENTS_DISABLE);
|
|
Packit |
577717 |
if (ret)
|
|
Packit |
577717 |
err(1, "prctl(disable) failed");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("Final counts:\n");
|
|
Packit |
577717 |
print_counts(fds, num_fds, "FINAL: ");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for (i = 0; i < num_fds; i++)
|
|
Packit |
577717 |
close(fds[i].fd);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
perf_free_fds(fds, num_fds);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* free libpfm resources cleanly */
|
|
Packit |
577717 |
pfm_terminate();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
return 0;
|
|
Packit |
577717 |
}
|