aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIngo Molnar <mingo@elte.hu>2009-06-05 07:18:41 -0400
committerIngo Molnar <mingo@elte.hu>2009-06-05 07:29:57 -0400
commitf250c030a87273f8838a2302bee7c2b4d03e9151 (patch)
treeb4c95d6dc7659d21cb5eb305cdc2a85ee9193320
parentee7b31fe5c5da8a038b96e54ae9fbd5dcab3b1da (diff)
perf record: Split out counter creation into a helper function
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Mike Galbraith <efault@gmx.de> Cc: Paul Mackerras <paulus@samba.org> Cc: Arnaldo Carvalho de Melo <acme@redhat.com> LKML-Reference: <new-submission> Signed-off-by: Ingo Molnar <mingo@elte.hu>
-rw-r--r--Documentation/perf_counter/builtin-record.c100
1 files changed, 53 insertions, 47 deletions
diff --git a/Documentation/perf_counter/builtin-record.c b/Documentation/perf_counter/builtin-record.c
index bf59df5bddf3..7f2d7ce94075 100644
--- a/Documentation/perf_counter/builtin-record.c
+++ b/Documentation/perf_counter/builtin-record.c
@@ -336,65 +336,71 @@ static void synthesize_events(void)
336 closedir(proc); 336 closedir(proc);
337} 337}
338 338
339static void open_counters(int cpu, pid_t pid) 339static int group_fd;
340
341static void create_counter(int counter, int cpu, pid_t pid)
340{ 342{
341 struct perf_counter_attr attr; 343 struct perf_counter_attr attr;
342 int counter, group_fd;
343 int track = 1; 344 int track = 1;
344 345
345 if (pid > 0) { 346 memset(&attr, 0, sizeof(attr));
346 pid_synthesize_comm_event(pid, 0); 347 attr.config = event_id[counter];
347 pid_synthesize_mmap_events(pid); 348 attr.sample_period = event_count[counter];
348 } 349 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
350 attr.mmap = track;
351 attr.comm = track;
352 attr.inherit = (cpu < 0) && inherit;
349 353
350 group_fd = -1; 354 track = 0; /* only the first counter needs these */
351 for (counter = 0; counter < nr_counters; counter++) {
352 355
353 memset(&attr, 0, sizeof(attr)); 356 fd[nr_cpu][counter] = sys_perf_counter_open(&attr, pid, cpu, group_fd, 0);
354 attr.config = event_id[counter];
355 attr.sample_period = event_count[counter];
356 attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
357 attr.mmap = track;
358 attr.comm = track;
359 attr.inherit = (cpu < 0) && inherit;
360 357
361 track = 0; // only the first counter needs these 358 if (fd[nr_cpu][counter] < 0) {
359 int err = errno;
362 360
363 fd[nr_cpu][counter] = 361 error("syscall returned with %d (%s)\n",
364 sys_perf_counter_open(&attr, pid, cpu, group_fd, 0); 362 fd[nr_cpu][counter], strerror(err));
363 if (err == EPERM)
364 printf("Are you root?\n");
365 exit(-1);
366 }
367 assert(fd[nr_cpu][counter] >= 0);
368 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
365 369
366 if (fd[nr_cpu][counter] < 0) { 370 /*
367 int err = errno; 371 * First counter acts as the group leader:
372 */
373 if (group && group_fd == -1)
374 group_fd = fd[nr_cpu][counter];
375
376 event_array[nr_poll].fd = fd[nr_cpu][counter];
377 event_array[nr_poll].events = POLLIN;
378 nr_poll++;
379
380 mmap_array[nr_cpu][counter].counter = counter;
381 mmap_array[nr_cpu][counter].prev = 0;
382 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
383 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
384 PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0);
385 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
386 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
387 exit(-1);
388 }
389}
368 390
369 error("syscall returned with %d (%s)\n", 391static void open_counters(int cpu, pid_t pid)
370 fd[nr_cpu][counter], strerror(err)); 392{
371 if (err == EPERM) 393 int counter;
372 printf("Are you root?\n");
373 exit(-1);
374 }
375 assert(fd[nr_cpu][counter] >= 0);
376 fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
377 394
378 /* 395 if (pid > 0) {
379 * First counter acts as the group leader: 396 pid_synthesize_comm_event(pid, 0);
380 */ 397 pid_synthesize_mmap_events(pid);
381 if (group && group_fd == -1)
382 group_fd = fd[nr_cpu][counter];
383
384 event_array[nr_poll].fd = fd[nr_cpu][counter];
385 event_array[nr_poll].events = POLLIN;
386 nr_poll++;
387
388 mmap_array[nr_cpu][counter].counter = counter;
389 mmap_array[nr_cpu][counter].prev = 0;
390 mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
391 mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
392 PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0);
393 if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
394 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
395 exit(-1);
396 }
397 } 398 }
399
400 group_fd = -1;
401 for (counter = 0; counter < nr_counters; counter++)
402 create_counter(counter, cpu, pid);
403
398 nr_cpu++; 404 nr_cpu++;
399} 405}
400 406