Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf annotate: Check for fused instructions

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>

authored by

Jin Yao and committed by
Arnaldo Carvalho de Melo
69fb09f6 4b1303d0

+81 -6
+46
tools/perf/arch/x86/annotate/instructions.c
··· 76 76 { .name = "xbeginq", .ops = &jump_ops, }, 77 77 { .name = "retq", .ops = &ret_ops, }, 78 78 }; 79 + 80 + static bool x86__ins_is_fused(struct arch *arch, const char *ins1, 81 + const char *ins2) 82 + { 83 + if (arch->family != 6 || arch->model < 0x1e || strstr(ins2, "jmp")) 84 + return false; 85 + 86 + if (arch->model == 0x1e) { 87 + /* Nehalem */ 88 + if ((strstr(ins1, "cmp") && !strstr(ins1, "xchg")) || 89 + strstr(ins1, "test")) { 90 + return true; 91 + } 92 + } else { 93 + /* Newer platform */ 94 + if ((strstr(ins1, "cmp") && !strstr(ins1, "xchg")) || 95 + strstr(ins1, "test") || 96 + strstr(ins1, "add") || 97 + strstr(ins1, "sub") || 98 + strstr(ins1, "and") || 99 + strstr(ins1, "inc") || 100 + strstr(ins1, "dec")) { 101 + return true; 102 + } 103 + } 104 + 105 + return false; 106 + } 107 + 108 + static int x86__cpuid_parse(struct arch *arch, char *cpuid) 109 + { 110 + unsigned int family, model, stepping; 111 + int ret; 112 + 113 + /* 114 + * cpuid = "GenuineIntel,family,model,stepping" 115 + */ 116 + ret = sscanf(cpuid, "%*[^,],%u,%u,%u", &family, &model, &stepping); 117 + if (ret == 3) { 118 + arch->family = family; 119 + arch->model = model; 120 + return 0; 121 + } 122 + 123 + return -1; 124 + }
+1 -1
tools/perf/builtin-top.c
··· 134 134 return err; 135 135 } 136 136 137 - err = symbol__disassemble(sym, map, NULL, 0, NULL); 137 + err = symbol__disassemble(sym, map, NULL, 0, NULL, NULL); 138 138 if (err == 0) { 139 139 out_assign: 140 140 top->sym_filter_entry = he;
+3 -1
tools/perf/ui/browsers/annotate.c
··· 9 9 #include "../../util/symbol.h" 10 10 #include "../../util/evsel.h" 11 11 #include "../../util/config.h" 12 + #include "../../util/evlist.h" 12 13 #include <inttypes.h> 13 14 #include <pthread.h> 14 15 #include <linux/kernel.h> ··· 1075 1074 } 1076 1075 1077 1076 err = symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 1078 - sizeof_bdl, &browser.arch); 1077 + sizeof_bdl, &browser.arch, 1078 + perf_evsel__env_cpuid(evsel)); 1079 1079 if (err) { 1080 1080 char msg[BUFSIZ]; 1081 1081 symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg));
+1 -1
tools/perf/ui/gtk/annotate.c
··· 169 169 return -1; 170 170 171 171 err = symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 172 - 0, NULL); 172 + 0, NULL, NULL); 173 173 if (err) { 174 174 char msg[BUFSIZ]; 175 175 symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg));
+20 -2
tools/perf/util/annotate.c
··· 47 47 bool sorted_instructions; 48 48 bool initialized; 49 49 void *priv; 50 + unsigned int model; 51 + unsigned int family; 50 52 int (*init)(struct arch *arch); 53 + bool (*ins_is_fused)(struct arch *arch, const char *ins1, 54 + const char *ins2); 55 + int (*cpuid_parse)(struct arch *arch, char *cpuid); 51 56 struct { 52 57 char comment_char; 53 58 char skip_functions_char; ··· 134 129 .name = "x86", 135 130 .instructions = x86__instructions, 136 131 .nr_instructions = ARRAY_SIZE(x86__instructions), 132 + .ins_is_fused = x86__ins_is_fused, 133 + .cpuid_parse = x86__cpuid_parse, 137 134 .objdump = { 138 135 .comment_char = '#', 139 136 }, ··· 176 169 return ins->ops->scnprintf(ins, bf, size, ops); 177 170 178 171 return ins__raw_scnprintf(ins, bf, size, ops); 172 + } 173 + 174 + bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2) 175 + { 176 + if (!arch || !arch->ins_is_fused) 177 + return false; 178 + 179 + return arch->ins_is_fused(arch, ins1, ins2); 179 180 } 180 181 181 182 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map *map) ··· 1396 1381 1397 1382 int symbol__disassemble(struct symbol *sym, struct map *map, 1398 1383 const char *arch_name, size_t privsize, 1399 - struct arch **parch) 1384 + struct arch **parch, char *cpuid) 1400 1385 { 1401 1386 struct dso *dso = map->dso; 1402 1387 char command[PATH_MAX * 2]; ··· 1432 1417 return err; 1433 1418 } 1434 1419 } 1420 + 1421 + if (arch->cpuid_parse && cpuid) 1422 + arch->cpuid_parse(arch, cpuid); 1435 1423 1436 1424 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, 1437 1425 symfs_filename, sym->name, map->unmap_ip(map, sym->start), ··· 1925 1907 u64 len; 1926 1908 1927 1909 if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 1928 - 0, NULL) < 0) 1910 + 0, NULL, NULL) < 0) 1929 1911 return -1; 1930 1912 1931 1913 len = symbol__size(sym);
+2 -1
tools/perf/util/annotate.h
··· 53 53 bool ins__is_call(const struct ins *ins); 54 54 bool ins__is_ret(const struct ins *ins); 55 55 int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops); 56 + bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2); 56 57 57 58 struct annotation; 58 59 ··· 161 160 162 161 int symbol__disassemble(struct symbol *sym, struct map *map, 163 162 const char *arch_name, size_t privsize, 164 - struct arch **parch); 163 + struct arch **parch, char *cpuid); 165 164 166 165 enum symbol_disassemble_errno { 167 166 SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,
+7
tools/perf/util/evsel.c
··· 2610 2610 return evsel->evlist->env->arch; 2611 2611 return NULL; 2612 2612 } 2613 + 2614 + char *perf_evsel__env_cpuid(struct perf_evsel *evsel) 2615 + { 2616 + if (evsel && evsel->evlist && evsel->evlist->env) 2617 + return evsel->evlist->env->cpuid; 2618 + return NULL; 2619 + }
+1
tools/perf/util/evsel.h
··· 436 436 attr__fprintf_f attr__fprintf, void *priv); 437 437 438 438 char *perf_evsel__env_arch(struct perf_evsel *evsel); 439 + char *perf_evsel__env_cpuid(struct perf_evsel *evsel); 439 440 440 441 #endif /* __PERF_EVSEL_H */