1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
/*
* self_pipe.c - dual process ping-pong example to stress PMU context switch of one process
*
* Copyright (c) 2008 Google, Inc
* Contributed by Stephane Eranian <eranian@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of libpfm, a performance monitoring support library for
* applications on Linux.
*/
#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sched.h>
#include <err.h>
#include <sys/ioctl.h>
#include <sys/prctl.h>
#include <perfmon/pfmlib_perf_event.h>
#include "perf_util.h"
static struct {
const char *events;
int cpu;
int delay;
} options;
int
pin_cpu(pid_t pid, unsigned int cpu)
{
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu, &mask);
return sched_setaffinity(pid, sizeof(mask), &mask);
}
static volatile int quit;
void sig_handler(int n)
{
quit = 1;
}
static void
do_child(int fr, int fw)
{
char c;
ssize_t ret;
for(;;) {
ret = read(fr, &c, 1);
if (ret < 0)
break;
ret = write(fw, "c", 1);
if (ret < 0)
break;
}
printf("child exited\n");
exit(0);
}
static void
measure(void)
{
perf_event_desc_t *fds = NULL;
int num_fds = 0;
uint64_t values[3];
ssize_t n;
int i, ret;
int pr[2], pw[2];
pid_t pid;
char cc = '0';
ret = pfm_initialize();
if (ret != PFM_SUCCESS)
err(1, "cannot initialize libpfm");
if (options.cpu == -1) {
srandom(getpid());
options.cpu = random() % sysconf(_SC_NPROCESSORS_ONLN);
}
ret = pipe(pr);
if (ret)
err(1, "cannot create read pipe");
ret = pipe(pw);
if (ret)
err(1, "cannot create write pipe");
ret = perf_setup_list_events(options.events, &fds, &num_fds);
if (ret || !num_fds)
exit(1);
for(i=0; i < num_fds; i++) {
fds[i].hw.disabled = 1;
fds[i].hw.read_format = PERF_FORMAT_SCALE;
fds[i].fd = perf_event_open(&fds[i].hw, 0, -1, -1, 0);
if (fds[i].fd == -1)
err(1, "cannot open event %d", i);
}
/*
* Pin to CPU0, inherited by child process. That will enforce
* the ping-pionging and thus stress the PMU context switch
* which is what we want
*/
ret = pin_cpu(getpid(), options.cpu);
if (ret)
err(1, "cannot pin to CPU%d", options.cpu);
printf("Both processes pinned to CPU%d, running for %d seconds\n", options.cpu, options.delay);
/*
* create second process which is not monitoring at the moment
*/
switch(pid=fork()) {
case -1:
err(1, "cannot create child\n");
exit(1); /* not reached */
case 0:
/* do not inherit session fd */
for(i=0; i < num_fds; i++)
close(fds[i].fd);
/* pr[]: write master, read child */
/* pw[]: read master, write child */
close(pr[1]); close(pw[0]);
do_child(pr[0], pw[1]);
exit(1);
}
close(pr[0]);
close(pw[1]);
/*
* Let's roll now
*/
prctl(PR_TASK_PERF_EVENTS_ENABLE);
signal(SIGALRM, sig_handler);
alarm(options.delay);
/*
* ping pong loop
*/
while(!quit) {
n = write(pr[1], "c", 1);
if (n < 1)
err(1, "write failed");
n = read(pw[0], &cc, 1);
if (n < 1)
err(1, "read failed");
}
prctl(PR_TASK_PERF_EVENTS_DISABLE);
for(i=0; i < num_fds; i++) {
uint64_t val;
double ratio;
ret = read(fds[i].fd, values, sizeof(values));
if (ret == -1)
err(1,"pfm_read error");
if (ret != sizeof(values))
errx(1, "did not read correct amount %d", ret);
val = perf_scale(values);
ratio = perf_scale_ratio(values);
if (ratio == 1.0)
printf("%20"PRIu64" %s\n", val, fds[i].name);
else
if (ratio == 0.0)
printf("%20"PRIu64" %s (did not run: competing session)\n", val, fds[i].name);
else
printf("%20"PRIu64" %s (scaled from %.2f%% of time)\n", val, fds[i].name, ratio*100.0);
}
/*
* kill child process
*/
kill(SIGKILL, pid);
/*
* close pipes
*/
close(pr[1]);
close(pw[0]);
/*
* and destroy our session
*/
for(i=0; i < num_fds; i++)
close(fds[i].fd);
perf_free_fds(fds, num_fds);
/* free libpfm resources cleanly */
pfm_terminate();
}
static void
usage(void)
{
printf("usage: self_pipe [-h] [-c cpu] [-d delay] [-e event1,event2,...]\n");
}
int
main(int argc, char **argv)
{
int c;
options.cpu = -1;
options.delay = -1;
while ((c=getopt(argc, argv,"he:c:d:")) != -1) {
switch(c) {
case 'e':
options.events = optarg;
break;
case 'c':
options.cpu = atoi(optarg);
break;
case 'd':
options.delay = atoi(optarg);
break;
case 'h':
usage();
exit(0);
default:
errx(1, "unknown error");
}
}
if (!options.events)
options.events = "cycles:u,instructions:u";
if (options.delay == -1)
options.delay = 10;
measure();
return 0;
}
|