perf srcline: Optimize comparision against SRCLINE_UNKNOWN
authorArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 12 Jun 2023 14:10:46 +0000 (11:10 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 12 Jun 2023 21:17:00 +0000 (18:17 -0300)
This is a string constant that gets returned and then strcmp() around,
we can instead just do a pointer comparision.

That requires a new global variable to comply with these warnings from
some versions of clang and gcc:

  41    68.95 fedora:rawhide                : FAIL clang version 16.0.4 (Fedora 16.0.4-1.fc39)
    result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]
            if (start_line != SRCLINE_UNKNOWN &&
                           ^  ~~~~~~~~~~~~~~~  41

Ack comments:

Agreed, the strcmps make me nervous as they won't distinguish heap from
a global meaning we could end up with things like pointers to freed
memory. The comparison with the global is always going to be same imo.

Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ali Saidi <alisaidi@amazon.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Brian Robbins <brianrob@linux.microsoft.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Dmitrii Dolgov <9erthalion6@gmail.com>
Cc: Fangrui Song <maskray@google.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Ivan Babrou <ivan@cloudflare.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jing Zhang <renyu.zj@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Steinar H. Gunderson <sesse@google.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Wenyu Liu <liuwenyu7@huawei.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Ye Xingchen <ye.xingchen@zte.com.cn>
Cc: Yuan Can <yuancan@huawei.com>
Link: https://lore.kernel.org/lkml/ZIcoJytUEz4UgQYR@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-diff.c
tools/perf/util/block-info.c
tools/perf/util/hist.c
tools/perf/util/map.c
tools/perf/util/sort.c
tools/perf/util/srcline.c
tools/perf/util/srcline.h

index eec89567ae483604020bf1b8b67952376cd64627..e8a1b16aa5f83f4f3423516f064613eee6fbe9e9 100644 (file)
@@ -1378,8 +1378,8 @@ static int cycles_printf(struct hist_entry *he, struct hist_entry *pair,
        end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
                                he->ms.sym);
 
-       if ((strncmp(start_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0) &&
-           (strncmp(end_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)) {
+       if (start_line != SRCLINE_UNKNOWN &&
+           end_line != SRCLINE_UNKNOWN) {
                scnprintf(buf, sizeof(buf), "[%s -> %s] %4ld",
                          start_line, end_line, block_he->diff.cycles);
        } else {
index 08279b1b65e5a4b01892aae662fef3325d0565ff..591fc1edd385caee7be9ff7834b18b255994747c 100644 (file)
@@ -296,8 +296,8 @@ static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
        end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
                                he->ms.sym);
 
-       if ((strncmp(start_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0) &&
-           (strncmp(end_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)) {
+       if (start_line != SRCLINE_UNKNOWN &&
+           end_line != SRCLINE_UNKNOWN) {
                scnprintf(buf, sizeof(buf), "[%s -> %s]",
                          start_line, end_line);
        } else {
index 0a10bcc6ec95b5e0913b740bc61141d2d0d5928c..3dc8a4968beb9c016947e0fb86c5137e88d3c37c 100644 (file)
@@ -484,7 +484,7 @@ static int hist_entry__init(struct hist_entry *he,
                        goto err_infos;
        }
 
-       if (he->srcline && strcmp(he->srcline, SRCLINE_UNKNOWN)) {
+       if (he->srcline && he->srcline != SRCLINE_UNKNOWN) {
                he->srcline = strdup(he->srcline);
                if (he->srcline == NULL)
                        goto err_rawdata;
index c77e2fce6a379e7f1b7410b8e6b6800e3a50d506..f30d34903aa4eabe77f20a94eee9a24e2b896f2f 100644 (file)
@@ -496,7 +496,7 @@ int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
 
        if (dso) {
                char *srcline = map__srcline(map, addr, NULL);
-               if (strncmp(srcline, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)
+               if (srcline != SRCLINE_UNKNOWN)
                        ret = fprintf(fp, "%s%s", prefix, srcline);
                zfree_srcline(&srcline);
        }
index 047c3606802f5b7fadce826a58ce0942e653670f..6aa1c7f2b4448b30b403b4cdcfe63b65647b0e6f 100644 (file)
@@ -643,7 +643,7 @@ static char *hist_entry__get_srcfile(struct hist_entry *e)
 
        sf = __get_srcline(map__dso(map), map__rip_2objdump(map, e->ip),
                         e->ms.sym, false, true, true, e->ip);
-       if (!strcmp(sf, SRCLINE_UNKNOWN))
+       if (sf == SRCLINE_UNKNOWN)
                return no_srcfile;
        p = strchr(sf, ':');
        if (p && *sf) {
index b8e596528d7e7e5e464888ebeb93a95b62260b8f..aec596a0b0bbec0f9b23614e766553349f7a8b81 100644 (file)
@@ -23,6 +23,8 @@
 
 bool srcline_full_filename;
 
+char *srcline__unknown = (char *)"??:0";
+
 static const char *dso__name(struct dso *dso)
 {
        const char *dso_name;
@@ -809,7 +811,7 @@ void zfree_srcline(char **srcline)
        if (*srcline == NULL)
                return;
 
-       if (strcmp(*srcline, SRCLINE_UNKNOWN))
+       if (*srcline != SRCLINE_UNKNOWN)
                free(*srcline);
 
        *srcline = NULL;
index a15c7db9058ece964c58e746d4fb584c201993a6..167645bcff0755e2d41eff3091c07fc7a7e69da5 100644 (file)
@@ -25,7 +25,8 @@ char *srcline__tree_find(struct rb_root_cached *tree, u64 addr);
 /* delete all srclines within the tree */
 void srcline__tree_delete(struct rb_root_cached *tree);
 
-#define SRCLINE_UNKNOWN  ((char *) "??:0")
+extern char *srcline__unknown;
+#define SRCLINE_UNKNOWN srcline__unknown
 
 struct inline_list {
        struct symbol           *symbol;