perf evlist: Fixup brown paper bag on "hint" for --mmap-pages cmdline arg
[deliverable/linux.git] / tools / perf / util / evlist.c
index 3cebc9a8d52e232e9dafe13dd2b8cf2b0dcd613d..de7515dd683abcb8cad0c35c6e7f4659376f6999 100644 (file)
@@ -8,6 +8,7 @@
  */
 #include "util.h"
 #include <api/fs/debugfs.h>
+#include <api/fs/fs.h>
 #include <poll.h>
 #include "cpumap.h"
 #include "thread_map.h"
@@ -413,7 +414,7 @@ int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
        int nfds = 0;
        struct perf_evsel *evsel;
 
-       list_for_each_entry(evsel, &evlist->entries, node) {
+       evlist__for_each(evlist, evsel) {
                if (evsel->system_wide)
                        nfds += nr_cpus;
                else
@@ -527,6 +528,22 @@ static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
        return 0;
 }
 
+static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
+                                    struct perf_evsel *evsel, int idx, int cpu,
+                                    int thread)
+{
+       struct perf_sample_id *sid = SID(evsel, cpu, thread);
+       sid->idx = idx;
+       if (evlist->cpus && cpu >= 0)
+               sid->cpu = evlist->cpus->map[cpu];
+       else
+               sid->cpu = -1;
+       if (!evsel->system_wide && evlist->threads && thread >= 0)
+               sid->tid = evlist->threads->map[thread];
+       else
+               sid->tid = -1;
+}
+
 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
 {
        struct hlist_head *head;
@@ -800,14 +817,26 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
                        perf_evlist__mmap_get(evlist, idx);
                }
 
-               if (__perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
+               /*
+                * The system_wide flag causes a selected event to be opened
+                * always without a pid.  Consequently it will never get a
+                * POLLHUP, but it is used for tracking in combination with
+                * other events, so it should not need to be polled anyway.
+                * Therefore don't add it for polling.
+                */
+               if (!evsel->system_wide &&
+                   __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
                        perf_evlist__mmap_put(evlist, idx);
                        return -1;
                }
 
-               if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
-                   perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
-                       return -1;
+               if (evsel->attr.read_format & PERF_FORMAT_ID) {
+                       if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
+                                                  fd) < 0)
+                               return -1;
+                       perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
+                                                thread);
+               }
        }
 
        return 0;
@@ -1003,6 +1032,7 @@ int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
 
 out_delete_threads:
        thread_map__delete(evlist->threads);
+       evlist->threads = NULL;
        return -1;
 }
 
@@ -1175,11 +1205,51 @@ void perf_evlist__close(struct perf_evlist *evlist)
        }
 }
 
+static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
+{
+       int err = -ENOMEM;
+
+       /*
+        * Try reading /sys/devices/system/cpu/online to get
+        * an all cpus map.
+        *
+        * FIXME: -ENOMEM is the best we can do here, the cpu_map
+        * code needs an overhaul to properly forward the
+        * error, and we may not want to do that fallback to a
+        * default cpu identity map :-\
+        */
+       evlist->cpus = cpu_map__new(NULL);
+       if (evlist->cpus == NULL)
+               goto out;
+
+       evlist->threads = thread_map__new_dummy();
+       if (evlist->threads == NULL)
+               goto out_free_cpus;
+
+       err = 0;
+out:
+       return err;
+out_free_cpus:
+       cpu_map__delete(evlist->cpus);
+       evlist->cpus = NULL;
+       goto out;
+}
+
 int perf_evlist__open(struct perf_evlist *evlist)
 {
        struct perf_evsel *evsel;
        int err;
 
+       /*
+        * Default: one fd per CPU, all threads, aka systemwide
+        * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
+        */
+       if (evlist->threads == NULL && evlist->cpus == NULL) {
+               err = perf_evlist__create_syswide_maps(evlist);
+               if (err < 0)
+                       goto out_err;
+       }
+
        perf_evlist__update_id_pos(evlist);
 
        evlist__for_each(evlist, evsel) {
@@ -1276,8 +1346,14 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *tar
                sigaction(SIGUSR1, &act, NULL);
        }
 
-       if (target__none(target))
+       if (target__none(target)) {
+               if (evlist->threads == NULL) {
+                       fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
+                               __func__, __LINE__);
+                       goto out_close_pipes;
+               }
                evlist->threads->map[0] = evlist->workload.pid;
+       }
 
        close(child_ready_pipe[1]);
        close(go_pipe[0]);
@@ -1408,6 +1484,28 @@ int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
        return 0;
 }
 
+int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
+{
+       char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
+       int value;
+
+       switch (err) {
+       case EPERM:
+               sysctl__read_int("kernel/perf_event_mlock_kb", &value);
+               scnprintf(buf, size, "Error:\t%s.\n"
+                                    "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
+                                    "Hint:\tTried using %zd kB.\n"
+                                    "Hint:\tTry using a smaller -m/--mmap-pages value.",
+                                    emsg, value, evlist->mmap_len / 1024);
+               break;
+       default:
+               scnprintf(buf, size, "%s", emsg);
+               break;
+       }
+
+       return 0;
+}
+
 void perf_evlist__to_front(struct perf_evlist *evlist,
                           struct perf_evsel *move_evsel)
 {
This page took 0.03516 seconds and 5 git commands to generate.