[73e1ce]: perf_examples / task_smpl.c  Maximize  Restore  History

Download this file

436 lines (362 with data), 10.2 kB

  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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* task_smpl.c - example of a task sampling another one using a randomized sampling period
*
* Copyright (c) 2009 Google, Inc
* Contributed by Stephane Eranian <eranian@gmail.com>
*
* Based on:
* Copyright (c) 2003-2006 Hewlett-Packard Development Company, L.P.
* Contributed by Stephane Eranian <eranian@hpl.hp.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.
*/
#include <sys/types.h>
#include <linux/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <getopt.h>
#include <setjmp.h>
#include <sys/wait.h>
#include <sys/poll.h>
#include <sys/mman.h>
#include <locale.h>
#include <sys/ioctl.h>
#include <err.h>
#include "perf_util.h"
#define SMPL_PERIOD 240000000ULL
typedef struct {
int opt_no_show;
int opt_inherit;
int cpu;
int mmap_pages;
char *events;
} options_t;
static jmp_buf jbuf;
static uint64_t collected_samples, lost_samples;
static perf_event_desc_t *fds;
static int num_fds;
static options_t options;
static struct option the_options[]={
{ "help", 0, 0, 1},
{ "no-show", 0, &options.opt_no_show, 1},
{ 0, 0, 0, 0}
};
static char *gen_events = "PERF_COUNT_HW_CPU_CYCLES,PERF_COUNT_HW_INSTRUCTIONS";
static void
cld_handler(int n)
{
longjmp(jbuf, 1);
}
int
child(char **arg)
{
execvp(arg[0], arg);
/* not reached */
return -1;
}
struct timeval last_read, this_read;
static void
display_lost(perf_event_desc_t *hw)
{
struct { uint64_t id, lost; } lost;
const char *str;
int e, ret;
ret = perf_read_buffer(hw->buf, hw->pgmsk, &lost, sizeof(lost));
if (ret)
errx(1, "cannot read lost info");
e = perf_id2event(fds, num_fds, lost.id);
if (e == -1)
str = "unknown lost event";
else
str = fds[e].name;
printf("<<<LOST %"PRIu64" SAMPLES FOR EVENT %s>>>\n", lost.lost, str);
lost_samples += lost.lost;
}
static void
display_exit(perf_event_desc_t *hw)
{
struct { pid_t pid, ppid, tid, ptid; } grp;
int ret;
ret = perf_read_buffer(hw->buf, hw->pgmsk, &grp, sizeof(grp));
if (ret)
errx(1, "cannot read exit info");
printf("[%d] exited\n", grp.pid);
}
static void
display_freq(int mode, perf_event_desc_t *hw)
{
struct { uint64_t time, id, stream_id; } thr;
int ret;
ret = perf_read_buffer(hw->buf, hw->pgmsk, &thr, sizeof(thr));
if (ret)
errx(1, "cannot read throttling info");
printf("%s value=%"PRIu64" event ID=%"PRIu64"\n", mode ? "Throttled" : "Unthrottled", thr.id, thr.stream_id);
}
static void
process_smpl_buf(perf_event_desc_t *hw)
{
struct perf_event_header ehdr;
int ret;
for(;;) {
ret = perf_read_buffer(hw->buf, hw->pgmsk, &ehdr, sizeof(ehdr));
if (ret)
return; /* nothing to read */
switch(ehdr.type) {
case PERF_RECORD_SAMPLE:
collected_samples++;
ret = perf_display_sample(fds, num_fds, hw - fds, &ehdr, stdout);
if (ret)
errx(1, "cannot parse sample");
break;
case PERF_RECORD_EXIT:
display_exit(hw);
break;
case PERF_RECORD_LOST:
display_lost(hw);
break;
case PERF_RECORD_THROTTLE:
display_freq(1, hw);
break;
case PERF_RECORD_UNTHROTTLE:
display_freq(0, hw);
break;
default:
printf("unknown sample type %d\n", ehdr.type);
perf_skip_buffer(hw->buf, ehdr.size);
}
}
}
int
mainloop(char **arg)
{
static uint64_t ovfl_count; /* static to avoid setjmp issue */
struct pollfd pollfds[1];
int go[2], ready[2];
uint64_t *val;
size_t sz, pgsz;
size_t map_size = 0;
pid_t pid;
int status, ret;
int i;
char buf;
if (pfm_initialize() != PFM_SUCCESS)
errx(1, "libpfm initialization failed\n");
pgsz = sysconf(_SC_PAGESIZE);
map_size = (options.mmap_pages+1)*pgsz;
/*
* does allocate fds
*/
ret = perf_setup_list_events(options.events, &fds, &num_fds);
if (ret || !num_fds)
errx(1, "cannot setup event list");
memset(pollfds, 0, sizeof(pollfds));
ret = pipe(ready);
if (ret)
err(1, "cannot create pipe ready");
ret = pipe(go);
if (ret)
err(1, "cannot create pipe go");
/*
* Create the child task
*/
if ((pid=fork()) == -1)
err(1, "cannot fork process\n");
if (pid == 0) {
close(ready[0]);
close(go[1]);
/*
* let the parent know we exist
*/
close(ready[1]);
if (read(go[0], &buf, 1) == -1)
err(1, "unable to read go_pipe");
exit(child(arg));
}
close(ready[1]);
close(go[0]);
if (read(ready[0], &buf, 1) == -1)
err(1, "unable to read child_ready_pipe");
close(ready[0]);
fds[0].fd = -1;
if (!fds[0].hw.sample_period)
errx(1, "need to set sampling period or freq on first event, use :period= or :freq=");
for(i=0; i < num_fds; i++) {
if (i == 0) {
fds[i].hw.disabled = 1;
fds[i].hw.enable_on_exec = 1; /* start immediately */
} else
fds[i].hw.disabled = 0;
if (options.opt_inherit)
fds[i].hw.inherit = 1;
if (fds[i].hw.sample_period) {
/*
* set notification threshold to be halfway through the buffer
*/
fds[i].hw.wakeup_watermark = (options.mmap_pages*pgsz) / 2;
fds[i].hw.watermark = 1;
fds[i].hw.sample_type = PERF_SAMPLE_IP|PERF_SAMPLE_TID|PERF_SAMPLE_READ|PERF_SAMPLE_TIME|PERF_SAMPLE_PERIOD|PERF_SAMPLE_STREAM_ID;
printf("%s period=%"PRIu64" freq=%d\n", fds[i].name, fds[i].hw.sample_period, fds[i].hw.freq);
fds[i].hw.read_format = PERF_FORMAT_SCALE;
if (num_fds > 1)
fds[i].hw.read_format |= PERF_FORMAT_GROUP|PERF_FORMAT_ID;
if (fds[i].hw.freq)
fds[i].hw.sample_type |= PERF_SAMPLE_PERIOD;
}
fds[i].fd = perf_event_open(&fds[i].hw, pid, options.cpu, fds[0].fd, 0);
if (fds[i].fd == -1) {
if (fds[i].hw.precise_ip)
err(1, "cannot attach event %s: precise mode may not be supported", fds[i].name);
err(1, "cannot attach event %s", fds[i].name);
}
}
/*
* kernel adds the header page to the size of the mmapped region
*/
fds[0].buf = mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fds[0].fd, 0);
if (fds[0].buf == MAP_FAILED)
err(1, "cannot mmap buffer");
/* does not include header page */
fds[0].pgmsk = (options.mmap_pages*pgsz)-1;
/*
* send samples for all events to first event's buffer
*/
for (i = 1; i < num_fds; i++) {
if (!fds[i].hw.sample_period)
continue;
ret = ioctl(fds[i].fd, PERF_EVENT_IOC_SET_OUTPUT, fds[0].fd);
if (ret)
err(1, "cannot redirect sampling output");
}
/*
* we are using PERF_FORMAT_GROUP, therefore the structure
* of val is as follows:
*
* { u64 nr;
* { u64 time_enabled; } && PERF_FORMAT_ENABLED
* { u64 time_running; } && PERF_FORMAT_RUNNING
* { u64 value;
* { u64 id; } && PERF_FORMAT_ID
* } cntr[nr];
* We are skipping the first 3 values (nr, time_enabled, time_running)
* and then for each event we get a pair of values.
*/
if (num_fds > 1) {
sz = (3+2*num_fds)*sizeof(uint64_t);
val = malloc(sz);
if (!val)
err(1, "cannot allocate memory");
ret = read(fds[0].fd, val, sz);
if (ret == -1)
err(1, "cannot read id %zu", sizeof(val));
for(i=0; i < num_fds; i++) {
fds[i].id = val[2*i+1+3];
printf("%"PRIu64" %s\n", fds[i].id, fds[i].name);
}
free(val);
}
pollfds[0].fd = fds[0].fd;
pollfds[0].events = POLLIN;
for(i=0; i < num_fds; i++) {
ret = ioctl(fds[i].fd, PERF_EVENT_IOC_ENABLE, 0);
if (ret)
err(1, "cannot enable event %s\n", fds[i].name);
}
signal(SIGCHLD, cld_handler);
close(go[1]);
if (setjmp(jbuf) == 1)
goto terminate_session;
/*
* core loop
*/
for(;;) {
ret = poll(pollfds, 1, -1);
if (ret < 0 && errno == EINTR)
break;
ovfl_count++;
process_smpl_buf(&fds[0]);
}
terminate_session:
/*
* cleanup child
*/
wait4(pid, &status, 0, NULL);
for(i=0; i < num_fds; i++)
close(fds[i].fd);
/* check for partial event buffer */
process_smpl_buf(&fds[0]);
munmap(fds[0].buf, map_size);
free(fds);
printf("%"PRIu64" samples collected in %"PRIu64" poll events, %"PRIu64" lost samples\n",
collected_samples,
ovfl_count, lost_samples);
/* free libpfm resources cleanly */
pfm_terminate();
return 0;
}
static void
usage(void)
{
printf("usage: task_smpl [-h] [--help] [-i] [-c cpu] [-m mmap_pages] [-e event1,...,eventn] cmd\n");
}
int
main(int argc, char **argv)
{
int c;
setlocale(LC_ALL, "");
options.cpu = -1;
while ((c=getopt_long(argc, argv,"+he:m:ic:", the_options, 0)) != -1) {
switch(c) {
case 0: continue;
case 'e':
if (options.events)
errx(1, "events specified twice\n");
options.events = optarg;
break;
case 'i':
options.opt_inherit = 1;
break;
case 'm':
if (options.mmap_pages)
errx(1, "mmap pages already set\n");
options.mmap_pages = atoi(optarg);
break;
case 'c':
options.cpu = atoi(optarg);
break;
case 'h':
usage();
exit(0);
default:
errx(1, "unknown option");
}
}
if (argv[optind] == NULL)
errx(1, "you must specify a command to execute\n");
if (!options.events)
options.events = strdup(gen_events);
if (!options.mmap_pages)
options.mmap_pages = 1;
if (options.mmap_pages > 1 && ((options.mmap_pages) & 0x1))
errx(1, "number of pages must be power of 2\n");
return mainloop(argv+optind);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks