perf record: Split out counter creation into a helper function
authorIngo Molnar <mingo@elte.hu>
Fri, 5 Jun 2009 11:18:41 +0000 (13:18 +0200)
committerIngo Molnar <mingo@elte.hu>
Fri, 5 Jun 2009 11:29:57 +0000 (13:29 +0200)
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>
Documentation/perf_counter/builtin-record.c

index bf59df5bddf3651ff30a2cd69e098c9574cff2ae..7f2d7ce94075e24e7806c2acef14106dfd475892 100644 (file)
@@ -336,65 +336,71 @@ static void synthesize_events(void)
        closedir(proc);
 }
 
-static void open_counters(int cpu, pid_t pid)
+static int group_fd;
+
+static void create_counter(int counter, int cpu, pid_t pid)
 {
        struct perf_counter_attr attr;
-       int counter, group_fd;
        int track = 1;
 
-       if (pid > 0) {
-               pid_synthesize_comm_event(pid, 0);
-               pid_synthesize_mmap_events(pid);
-       }
+       memset(&attr, 0, sizeof(attr));
+       attr.config             = event_id[counter];
+       attr.sample_period      = event_count[counter];
+       attr.sample_type        = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
+       attr.mmap               = track;
+       attr.comm               = track;
+       attr.inherit    = (cpu < 0) && inherit;
 
-       group_fd = -1;
-       for (counter = 0; counter < nr_counters; counter++) {
+       track = 0; /* only the first counter needs these */
 
-               memset(&attr, 0, sizeof(attr));
-               attr.config             = event_id[counter];
-               attr.sample_period      = event_count[counter];
-               attr.sample_type        = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
-               attr.mmap               = track;
-               attr.comm               = track;
-               attr.inherit    = (cpu < 0) && inherit;
+       fd[nr_cpu][counter] = sys_perf_counter_open(&attr, pid, cpu, group_fd, 0);
 
-               track = 0; // only the first counter needs these
+       if (fd[nr_cpu][counter] < 0) {
+               int err = errno;
 
-               fd[nr_cpu][counter] =
-                       sys_perf_counter_open(&attr, pid, cpu, group_fd, 0);
+               error("syscall returned with %d (%s)\n",
+                               fd[nr_cpu][counter], strerror(err));
+               if (err == EPERM)
+                       printf("Are you root?\n");
+               exit(-1);
+       }
+       assert(fd[nr_cpu][counter] >= 0);
+       fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
 
-               if (fd[nr_cpu][counter] < 0) {
-                       int err = errno;
+       /*
+        * First counter acts as the group leader:
+        */
+       if (group && group_fd == -1)
+               group_fd = fd[nr_cpu][counter];
+
+       event_array[nr_poll].fd = fd[nr_cpu][counter];
+       event_array[nr_poll].events = POLLIN;
+       nr_poll++;
+
+       mmap_array[nr_cpu][counter].counter = counter;
+       mmap_array[nr_cpu][counter].prev = 0;
+       mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
+       mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
+                       PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0);
+       if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
+               error("failed to mmap with %d (%s)\n", errno, strerror(errno));
+               exit(-1);
+       }
+}
 
-                       error("syscall returned with %d (%s)\n",
-                                       fd[nr_cpu][counter], strerror(err));
-                       if (err == EPERM)
-                               printf("Are you root?\n");
-                       exit(-1);
-               }
-               assert(fd[nr_cpu][counter] >= 0);
-               fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
+static void open_counters(int cpu, pid_t pid)
+{
+       int counter;
 
-               /*
-                * First counter acts as the group leader:
-                */
-               if (group && group_fd == -1)
-                       group_fd = fd[nr_cpu][counter];
-
-               event_array[nr_poll].fd = fd[nr_cpu][counter];
-               event_array[nr_poll].events = POLLIN;
-               nr_poll++;
-
-               mmap_array[nr_cpu][counter].counter = counter;
-               mmap_array[nr_cpu][counter].prev = 0;
-               mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
-               mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
-                               PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0);
-               if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
-                       error("failed to mmap with %d (%s)\n", errno, strerror(errno));
-                       exit(-1);
-               }
+       if (pid > 0) {
+               pid_synthesize_comm_event(pid, 0);
+               pid_synthesize_mmap_events(pid);
        }
+
+       group_fd = -1;
+       for (counter = 0; counter < nr_counters; counter++)
+               create_counter(counter, cpu, pid);
+
        nr_cpu++;
 }