]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
perf annotate: Check for fused instructions
authorJin Yao <yao.jin@linux.intel.com>
Fri, 7 Jul 2017 05:06:34 +0000 (13:06 +0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 19 Jul 2017 02:11:25 +0000 (23:11 -0300)
Macro fusion merges two instructions to a single micro-op. Intel core
platform performs this hardware optimization under limited
circumstances.

For example, CMP + JCC can be "fused" and executed /retired together.
While with sampling this can result in the sample sometimes being on the
JCC and sometimes on the CMP.  So for the fused instruction pair, they
could be considered together.

On Nehalem, fused instruction pairs:

  cmp/test + jcc.

On other new CPU:

  cmp/test/add/sub/and/inc/dec + jcc.

This patch adds an x86-specific function which checks if 2 instructions
are in a "fused" pair. For non-x86 arch, the function is just NULL.

Changelog:

v4: Move the CPU model checking to symbol__disassemble and save the CPU
    family/model in arch structure.

    It avoids checking every time when jump arrow printed.

v3: Add checking for Nehalem (CMP, TEST). For other newer Intel CPUs
    just check it by default (CMP, TEST, ADD, SUB, AND, INC, DEC).

v2: Remove the original weak function. Arnaldo points out that doing it
    as a weak function that will be overridden by the host arch doesn't
    work. So now it's implemented as an arch-specific function.

Committer fix:

Do not access evsel->evlist->env->cpuid, ->env can be null, introduce
perf_evsel__env_cpuid(), just like perf_evsel__env_arch(), also used in
this function call.

The original patch was segfaulting 'perf top' + annotation.

But this essentially disables this fused instructions augmentation in
'perf top', the right thing is to get the cpuid from the running kernel,
left for a later patch tho.

Signed-off-by: Yao Jin <yao.jin@linux.intel.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/1499403995-19857-2-git-send-email-yao.jin@linux.intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/arch/x86/annotate/instructions.c
tools/perf/builtin-top.c
tools/perf/ui/browsers/annotate.c
tools/perf/ui/gtk/annotate.c
tools/perf/util/annotate.c
tools/perf/util/annotate.h
tools/perf/util/evsel.c
tools/perf/util/evsel.h

index c1625f256df362ab06ab21cc35972bdcca227381..d84b72063a30e33847e28891c16b9158bb0b8406 100644 (file)
@@ -76,3 +76,49 @@ static struct ins x86__instructions[] = {
        { .name = "xbeginq",    .ops = &jump_ops, },
        { .name = "retq",       .ops = &ret_ops,  },
 };
+
+static bool x86__ins_is_fused(struct arch *arch, const char *ins1,
+                             const char *ins2)
+{
+       if (arch->family != 6 || arch->model < 0x1e || strstr(ins2, "jmp"))
+               return false;
+
+       if (arch->model == 0x1e) {
+               /* Nehalem */
+               if ((strstr(ins1, "cmp") && !strstr(ins1, "xchg")) ||
+                    strstr(ins1, "test")) {
+                       return true;
+               }
+       } else {
+               /* Newer platform */
+               if ((strstr(ins1, "cmp") && !strstr(ins1, "xchg")) ||
+                    strstr(ins1, "test") ||
+                    strstr(ins1, "add") ||
+                    strstr(ins1, "sub") ||
+                    strstr(ins1, "and") ||
+                    strstr(ins1, "inc") ||
+                    strstr(ins1, "dec")) {
+                       return true;
+               }
+       }
+
+       return false;
+}
+
+static int x86__cpuid_parse(struct arch *arch, char *cpuid)
+{
+       unsigned int family, model, stepping;
+       int ret;
+
+       /*
+        * cpuid = "GenuineIntel,family,model,stepping"
+        */
+       ret = sscanf(cpuid, "%*[^,],%u,%u,%u", &family, &model, &stepping);
+       if (ret == 3) {
+               arch->family = family;
+               arch->model = model;
+               return 0;
+       }
+
+       return -1;
+}
index 6052376634c058e735de5c398dd716b1317f48af..022486dc67f537438182e00be243c59dc17cbee6 100644 (file)
@@ -134,7 +134,7 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
                return err;
        }
 
-       err = symbol__disassemble(sym, map, NULL, 0, NULL);
+       err = symbol__disassemble(sym, map, NULL, 0, NULL, NULL);
        if (err == 0) {
 out_assign:
                top->sym_filter_entry = he;
index 27f41f28dcb49a10a1bcb2319f012d9953e552f2..c4336138b6737a7bd0f1f08a86aabc4c71dc5c4a 100644 (file)
@@ -9,6 +9,7 @@
 #include "../../util/symbol.h"
 #include "../../util/evsel.h"
 #include "../../util/config.h"
+#include "../../util/evlist.h"
 #include <inttypes.h>
 #include <pthread.h>
 #include <linux/kernel.h>
@@ -1074,7 +1075,8 @@ int symbol__tui_annotate(struct symbol *sym, struct map *map,
        }
 
        err = symbol__disassemble(sym, map, perf_evsel__env_arch(evsel),
-                                 sizeof_bdl, &browser.arch);
+                                 sizeof_bdl, &browser.arch,
+                                 perf_evsel__env_cpuid(evsel));
        if (err) {
                char msg[BUFSIZ];
                symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg));
index d903fd493416bf557c23e9d5d4f59d5fcd83efcc..87e3760624f2e368bae3e2e7d68a7f6ab441eafa 100644 (file)
@@ -169,7 +169,7 @@ static int symbol__gtk_annotate(struct symbol *sym, struct map *map,
                return -1;
 
        err = symbol__disassemble(sym, map, perf_evsel__env_arch(evsel),
-                                 0, NULL);
+                                 0, NULL, NULL);
        if (err) {
                char msg[BUFSIZ];
                symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg));
index be1caabb92906befc755d1b99e22cb6a0439dc34..8748ebb3f9328217849ca9f02678f2ea344472d6 100644 (file)
@@ -47,7 +47,12 @@ struct arch {
        bool            sorted_instructions;
        bool            initialized;
        void            *priv;
+       unsigned int    model;
+       unsigned int    family;
        int             (*init)(struct arch *arch);
+       bool            (*ins_is_fused)(struct arch *arch, const char *ins1,
+                                       const char *ins2);
+       int             (*cpuid_parse)(struct arch *arch, char *cpuid);
        struct          {
                char comment_char;
                char skip_functions_char;
@@ -129,6 +134,8 @@ static struct arch architectures[] = {
                .name = "x86",
                .instructions = x86__instructions,
                .nr_instructions = ARRAY_SIZE(x86__instructions),
+               .ins_is_fused = x86__ins_is_fused,
+               .cpuid_parse = x86__cpuid_parse,
                .objdump =  {
                        .comment_char = '#',
                },
@@ -171,6 +178,14 @@ int ins__scnprintf(struct ins *ins, char *bf, size_t size,
        return ins__raw_scnprintf(ins, bf, size, ops);
 }
 
+bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
+{
+       if (!arch || !arch->ins_is_fused)
+               return false;
+
+       return arch->ins_is_fused(arch, ins1, ins2);
+}
+
 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map)
 {
        char *endptr, *tok, *name;
@@ -1381,7 +1396,7 @@ static const char *annotate__norm_arch(const char *arch_name)
 
 int symbol__disassemble(struct symbol *sym, struct map *map,
                        const char *arch_name, size_t privsize,
-                       struct arch **parch)
+                       struct arch **parch, char *cpuid)
 {
        struct dso *dso = map->dso;
        char command[PATH_MAX * 2];
@@ -1418,6 +1433,9 @@ int symbol__disassemble(struct symbol *sym, struct map *map,
                }
        }
 
+       if (arch->cpuid_parse && cpuid)
+               arch->cpuid_parse(arch, cpuid);
+
        pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
                 symfs_filename, sym->name, map->unmap_ip(map, sym->start),
                 map->unmap_ip(map, sym->end));
@@ -1907,7 +1925,7 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map,
        u64 len;
 
        if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel),
-                               0, NULL) < 0)
+                               0, NULL, NULL) < 0)
                return -1;
 
        len = symbol__size(sym);
index 21055034aedd6a07177d0954d629a48862c514a1..72d72728a0fc899b58a49eb0bba751279a787b26 100644 (file)
@@ -53,6 +53,7 @@ bool ins__is_jump(const struct ins *ins);
 bool ins__is_call(const struct ins *ins);
 bool ins__is_ret(const struct ins *ins);
 int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
+bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
 
 struct annotation;
 
@@ -160,7 +161,7 @@ void symbol__annotate_zero_histograms(struct symbol *sym);
 
 int symbol__disassemble(struct symbol *sym, struct map *map,
                        const char *arch_name, size_t privsize,
-                       struct arch **parch);
+                       struct arch **parch, char *cpuid);
 
 enum symbol_disassemble_errno {
        SYMBOL_ANNOTATE_ERRNO__SUCCESS          = 0,
index 413f74df08de7cae06b1313ef0ed4c1339ee2093..0e4cd60925646c820aacf32d6366645dc9bb3f01 100644 (file)
@@ -2610,3 +2610,10 @@ char *perf_evsel__env_arch(struct perf_evsel *evsel)
                return evsel->evlist->env->arch;
        return NULL;
 }
+
+char *perf_evsel__env_cpuid(struct perf_evsel *evsel)
+{
+       if (evsel && evsel->evlist && evsel->evlist->env)
+               return evsel->evlist->env->cpuid;
+       return NULL;
+}
index d101695c482cfb288db70b1f268372d4f4323f65..219ad0cdb9f49d9f36f2a980611edc001d4c8fb8 100644 (file)
@@ -436,5 +436,6 @@ int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
                             attr__fprintf_f attr__fprintf, void *priv);
 
 char *perf_evsel__env_arch(struct perf_evsel *evsel);
+char *perf_evsel__env_cpuid(struct perf_evsel *evsel);
 
 #endif /* __PERF_EVSEL_H */