static int perf_report__setup_sample_type(struct perf_report *rep)
 {
        struct perf_session *self = rep->session;
-       u64 sample_type = perf_evlist__sample_type(self->evlist);
+       u64 sample_type = perf_evlist__combined_sample_type(self->evlist);
 
        if (!self->fd_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
                if (sort__has_parent) {
 
                }
 
                evsels[i]->attr.wakeup_events = 1;
-               perf_evsel__set_sample_id(evsels[i]);
+               perf_evsel__set_sample_id(evsels[i], false);
 
                perf_evlist__add(evlist, evsels[i]);
 
 
        (PERF_SAMPLE_IP | PERF_SAMPLE_TID |             \
         PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR |          \
        PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID |        \
-        PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD)
+        PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD |         \
+        PERF_SAMPLE_IDENTIFIER)
 
 struct sample_event {
        struct perf_event_header        header;
 
        return evlist;
 }
 
+/**
+ * perf_evlist__set_id_pos - set the positions of event ids.
+ * @evlist: selected event list
+ *
+ * Events with compatible sample types all have the same id_pos
+ * and is_pos.  For convenience, put a copy on evlist.
+ */
+void perf_evlist__set_id_pos(struct perf_evlist *evlist)
+{
+       struct perf_evsel *first = perf_evlist__first(evlist);
+
+       evlist->id_pos = first->id_pos;
+       evlist->is_pos = first->is_pos;
+}
+
 static void perf_evlist__purge(struct perf_evlist *evlist)
 {
        struct perf_evsel *pos, *n;
 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
 {
        list_add_tail(&entry->node, &evlist->entries);
-       ++evlist->nr_entries;
+       if (!evlist->nr_entries++)
+               perf_evlist__set_id_pos(evlist);
 }
 
 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
                                   struct list_head *list,
                                   int nr_entries)
 {
+       bool set_id_pos = !evlist->nr_entries;
+
        list_splice_tail(list, &evlist->entries);
        evlist->nr_entries += nr_entries;
+       if (set_id_pos)
+               perf_evlist__set_id_pos(evlist);
 }
 
 void __perf_evlist__set_leader(struct list_head *list)
        return NULL;
 }
 
+static int perf_evlist__event2id(struct perf_evlist *evlist,
+                                union perf_event *event, u64 *id)
+{
+       const u64 *array = event->sample.array;
+       ssize_t n;
+
+       n = (event->header.size - sizeof(event->header)) >> 3;
+
+       if (event->header.type == PERF_RECORD_SAMPLE) {
+               if (evlist->id_pos >= n)
+                       return -1;
+               *id = array[evlist->id_pos];
+       } else {
+               if (evlist->is_pos > n)
+                       return -1;
+               n -= evlist->is_pos;
+               *id = array[n];
+       }
+       return 0;
+}
+
+static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
+                                                  union perf_event *event)
+{
+       struct hlist_head *head;
+       struct perf_sample_id *sid;
+       int hash;
+       u64 id;
+
+       if (evlist->nr_entries == 1)
+               return perf_evlist__first(evlist);
+
+       if (perf_evlist__event2id(evlist, event, &id))
+               return NULL;
+
+       /* Synthesized events have an id of zero */
+       if (!id)
+               return perf_evlist__first(evlist);
+
+       hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
+       head = &evlist->heads[hash];
+
+       hlist_for_each_entry(sid, head, node) {
+               if (sid->id == id)
+                       return sid->evsel;
+       }
+       return NULL;
+}
+
 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
 {
        struct perf_mmap *md = &evlist->mmap[idx];
 
 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
 {
-       struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
+       struct perf_evsel *pos;
 
-       list_for_each_entry_continue(pos, &evlist->entries, node) {
-               if (first->attr.sample_type != pos->attr.sample_type)
+       if (evlist->nr_entries == 1)
+               return true;
+
+       if (evlist->id_pos < 0 || evlist->is_pos < 0)
+               return false;
+
+       list_for_each_entry(pos, &evlist->entries, node) {
+               if (pos->id_pos != evlist->id_pos ||
+                   pos->is_pos != evlist->is_pos)
                        return false;
        }
 
        return true;
 }
 
-u64 perf_evlist__sample_type(struct perf_evlist *evlist)
+u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
 {
-       struct perf_evsel *first = perf_evlist__first(evlist);
-       return first->attr.sample_type;
+       struct perf_evsel *evsel;
+
+       if (evlist->combined_sample_type)
+               return evlist->combined_sample_type;
+
+       list_for_each_entry(evsel, &evlist->entries, node)
+               evlist->combined_sample_type |= evsel->attr.sample_type;
+
+       return evlist->combined_sample_type;
+}
+
+u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
+{
+       evlist->combined_sample_type = 0;
+       return __perf_evlist__combined_sample_type(evlist);
 }
 
 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
 
        if (sample_type & PERF_SAMPLE_CPU)
                size += sizeof(data->cpu) * 2;
+
+       if (sample_type & PERF_SAMPLE_IDENTIFIER)
+               size += sizeof(data->id);
 out:
        return size;
 }
 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
                              struct perf_sample *sample)
 {
-       struct perf_evsel *evsel = perf_evlist__first(evlist);
+       struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
+
+       if (!evsel)
+               return -EFAULT;
        return perf_evsel__parse_sample(evsel, event, sample);
 }
 
 
        int              nr_fds;
        int              nr_mmaps;
        int              mmap_len;
+       int              id_pos;
+       int              is_pos;
+       u64              combined_sample_type;
        struct {
                int     cork_fd;
                pid_t   pid;
 int perf_evlist__open(struct perf_evlist *evlist);
 void perf_evlist__close(struct perf_evlist *evlist);
 
+void perf_evlist__set_id_pos(struct perf_evlist *evlist);
+bool perf_can_sample_identifier(void);
 void perf_evlist__config(struct perf_evlist *evlist,
                         struct perf_record_opts *opts);
 
 void perf_evlist__set_leader(struct perf_evlist *evlist);
 
 u64 perf_evlist__read_format(struct perf_evlist *evlist);
-u64 perf_evlist__sample_type(struct perf_evlist *evlist);
+u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist);
+u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist);
 bool perf_evlist__sample_id_all(struct perf_evlist *evlist);
 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist);
 
 
 
 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
 
-static int __perf_evsel__sample_size(u64 sample_type)
+int __perf_evsel__sample_size(u64 sample_type)
 {
        u64 mask = sample_type & PERF_SAMPLE_MASK;
        int size = 0;
        return size;
 }
 
+/**
+ * __perf_evsel__calc_id_pos - calculate id_pos.
+ * @sample_type: sample type
+ *
+ * This function returns the position of the event id (PERF_SAMPLE_ID or
+ * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
+ * sample_event.
+ */
+static int __perf_evsel__calc_id_pos(u64 sample_type)
+{
+       int idx = 0;
+
+       if (sample_type & PERF_SAMPLE_IDENTIFIER)
+               return 0;
+
+       if (!(sample_type & PERF_SAMPLE_ID))
+               return -1;
+
+       if (sample_type & PERF_SAMPLE_IP)
+               idx += 1;
+
+       if (sample_type & PERF_SAMPLE_TID)
+               idx += 1;
+
+       if (sample_type & PERF_SAMPLE_TIME)
+               idx += 1;
+
+       if (sample_type & PERF_SAMPLE_ADDR)
+               idx += 1;
+
+       return idx;
+}
+
+/**
+ * __perf_evsel__calc_is_pos - calculate is_pos.
+ * @sample_type: sample type
+ *
+ * This function returns the position (counting backwards) of the event id
+ * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
+ * sample_id_all is used there is an id sample appended to non-sample events.
+ */
+static int __perf_evsel__calc_is_pos(u64 sample_type)
+{
+       int idx = 1;
+
+       if (sample_type & PERF_SAMPLE_IDENTIFIER)
+               return 1;
+
+       if (!(sample_type & PERF_SAMPLE_ID))
+               return -1;
+
+       if (sample_type & PERF_SAMPLE_CPU)
+               idx += 1;
+
+       if (sample_type & PERF_SAMPLE_STREAM_ID)
+               idx += 1;
+
+       return idx;
+}
+
+void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
+{
+       evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
+       evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
+}
+
 void hists__init(struct hists *hists)
 {
        memset(hists, 0, sizeof(*hists));
        if (!(evsel->attr.sample_type & bit)) {
                evsel->attr.sample_type |= bit;
                evsel->sample_size += sizeof(u64);
+               perf_evsel__calc_id_pos(evsel);
        }
 }
 
        if (evsel->attr.sample_type & bit) {
                evsel->attr.sample_type &= ~bit;
                evsel->sample_size -= sizeof(u64);
+               perf_evsel__calc_id_pos(evsel);
        }
 }
 
-void perf_evsel__set_sample_id(struct perf_evsel *evsel)
+void perf_evsel__set_sample_id(struct perf_evsel *evsel,
+                              bool can_sample_identifier)
 {
-       perf_evsel__set_sample_bit(evsel, ID);
+       if (can_sample_identifier) {
+               perf_evsel__reset_sample_bit(evsel, ID);
+               perf_evsel__set_sample_bit(evsel, IDENTIFIER);
+       } else {
+               perf_evsel__set_sample_bit(evsel, ID);
+       }
        evsel->attr.read_format |= PERF_FORMAT_ID;
 }
 
        INIT_LIST_HEAD(&evsel->node);
        hists__init(&evsel->hists);
        evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
+       perf_evsel__calc_id_pos(evsel);
 }
 
 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
                 * We need ID even in case of single event, because
                 * PERF_SAMPLE_READ process ID specific data.
                 */
-               perf_evsel__set_sample_id(evsel);
+               perf_evsel__set_sample_id(evsel, false);
 
                /*
                 * Apply group format only if we belong to group
        array += ((event->header.size -
                   sizeof(event->header)) / sizeof(u64)) - 1;
 
+       if (type & PERF_SAMPLE_IDENTIFIER) {
+               sample->id = *array;
+               array--;
+       }
+
        if (type & PERF_SAMPLE_CPU) {
                u.val64 = *array;
                if (swapped) {
        if (evsel->sample_size + sizeof(event->header) > event->header.size)
                return -EFAULT;
 
+       data->id = -1ULL;
+       if (type & PERF_SAMPLE_IDENTIFIER) {
+               data->id = *array;
+               array++;
+       }
+
        if (type & PERF_SAMPLE_IP) {
                data->ip = *array;
                array++;
                array++;
        }
 
-       data->id = -1ULL;
        if (type & PERF_SAMPLE_ID) {
                data->id = *array;
                array++;
 
        array = event->sample.array;
 
+       if (type & PERF_SAMPLE_IDENTIFIER) {
+               *array = sample->id;
+               array++;
+       }
+
        if (type & PERF_SAMPLE_IP) {
                *array = sample->ip;
                array++;
                bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
                bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
                bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
+               bit_name(IDENTIFIER),
                { .name = NULL, }
        };
 #undef bit_name
 
  * @name - Can be set to retain the original event name passed by the user,
  *         so that when showing results in tools such as 'perf stat', we
  *         show the name used, not some alias.
+ * @id_pos: the position of the event id (PERF_SAMPLE_ID or
+ *          PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of
+ *          struct sample_event
+ * @is_pos: the position (counting backwards) of the event id (PERF_SAMPLE_ID or
+ *          PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if sample_id_all
+ *          is used there is an id sample appended to non-sample events
  */
 struct perf_evsel {
        struct list_head        node;
        } handler;
        struct cpu_map          *cpus;
        unsigned int            sample_size;
+       int                     id_pos;
+       int                     is_pos;
        bool                    supported;
        bool                    needs_swap;
        /* parse modifier helper */
 void perf_evsel__config(struct perf_evsel *evsel,
                        struct perf_record_opts *opts);
 
+int __perf_evsel__sample_size(u64 sample_type);
+void perf_evsel__calc_id_pos(struct perf_evsel *evsel);
+
 bool perf_evsel__is_cache_op_valid(u8 type, u8 op);
 
 #define PERF_EVSEL__MAX_ALIASES 8
 #define perf_evsel__reset_sample_bit(evsel, bit) \
        __perf_evsel__reset_sample_bit(evsel, PERF_SAMPLE_##bit)
 
-void perf_evsel__set_sample_id(struct perf_evsel *evsel);
+void perf_evsel__set_sample_id(struct perf_evsel *evsel,
+                              bool use_sample_identifier);
 
 int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
                           const char *filter);
 
 #include "evlist.h"
 #include "evsel.h"
 #include "cpumap.h"
+#include "parse-events.h"
+
+typedef void (*setup_probe_fn_t)(struct perf_evsel *evsel);
+
+static int perf_do_probe_api(setup_probe_fn_t fn, int cpu, const char *str)
+{
+       struct perf_evlist *evlist;
+       struct perf_evsel *evsel;
+       int err = -EAGAIN, fd;
+
+       evlist = perf_evlist__new();
+       if (!evlist)
+               return -ENOMEM;
+
+       if (parse_events(evlist, str))
+               goto out_delete;
+
+       evsel = perf_evlist__first(evlist);
+
+       fd = sys_perf_event_open(&evsel->attr, -1, cpu, -1, 0);
+       if (fd < 0)
+               goto out_delete;
+       close(fd);
+
+       fn(evsel);
+
+       fd = sys_perf_event_open(&evsel->attr, -1, cpu, -1, 0);
+       if (fd < 0) {
+               if (errno == EINVAL)
+                       err = -EINVAL;
+               goto out_delete;
+       }
+       close(fd);
+       err = 0;
+
+out_delete:
+       perf_evlist__delete(evlist);
+       return err;
+}
+
+static bool perf_probe_api(setup_probe_fn_t fn)
+{
+       const char *try[] = {"cycles:u", "instructions:u", "cpu-clock", NULL};
+       struct cpu_map *cpus;
+       int cpu, ret, i = 0;
+
+       cpus = cpu_map__new(NULL);
+       if (!cpus)
+               return false;
+       cpu = cpus->map[0];
+       cpu_map__delete(cpus);
+
+       do {
+               ret = perf_do_probe_api(fn, cpu, try[i++]);
+               if (!ret)
+                       return true;
+       } while (ret == -EAGAIN && try[i]);
+
+       return false;
+}
+
+static void perf_probe_sample_identifier(struct perf_evsel *evsel)
+{
+       evsel->attr.sample_type |= PERF_SAMPLE_IDENTIFIER;
+}
+
+bool perf_can_sample_identifier(void)
+{
+       return perf_probe_api(perf_probe_sample_identifier);
+}
 
 void perf_evlist__config(struct perf_evlist *evlist,
                        struct perf_record_opts *opts)
 {
        struct perf_evsel *evsel;
+       bool use_sample_identifier = false;
+
        /*
         * Set the evsel leader links before we configure attributes,
         * since some might depend on this info.
        if (evlist->cpus->map[0] < 0)
                opts->no_inherit = true;
 
-       list_for_each_entry(evsel, &evlist->entries, node) {
+       list_for_each_entry(evsel, &evlist->entries, node)
                perf_evsel__config(evsel, opts);
 
-               if (evlist->nr_entries > 1)
-                       perf_evsel__set_sample_id(evsel);
+       if (evlist->nr_entries > 1) {
+               struct perf_evsel *first = perf_evlist__first(evlist);
+
+               list_for_each_entry(evsel, &evlist->entries, node) {
+                       if (evsel->attr.sample_type == first->attr.sample_type)
+                               continue;
+                       use_sample_identifier = perf_can_sample_identifier();
+                       break;
+               }
+               list_for_each_entry(evsel, &evlist->entries, node)
+                       perf_evsel__set_sample_id(evsel, use_sample_identifier);
        }
+
+       perf_evlist__set_id_pos(evlist);
 }
 
                                       union perf_event *event,
                                       struct perf_sample *sample)
 {
-       u64 sample_type = perf_evlist__sample_type(session->evlist);
+       u64 sample_type = __perf_evlist__combined_sample_type(session->evlist);
 
        if (event->header.type != PERF_RECORD_SAMPLE &&
            !perf_evlist__sample_id_all(session->evlist)) {