Skip to content

Commit 7a9418c

Browse files
captain5050acmel
authored andcommitted
perf dsos: Switch hand crafted code to bsearch()
Switch to using the bsearch library function rather than having a hand written binary search. Const-ify some static functions to avoid compiler warnings. Signed-off-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com> Cc: Ben Gainey <ben.gainey@arm.com> Cc: Changbin Du <changbin.du@huawei.com> Cc: Chengen Du <chengen.du@canonical.com> Cc: Colin Ian King <colin.i.king@gmail.com> Cc: Dima Kogan <dima@secretsauce.net> Cc: Ilkka Koskinen <ilkka@os.amperecomputing.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Clark <james.clark@arm.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: K Prateek Nayak <kprateek.nayak@amd.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Leo Yan <leo.yan@linux.dev> Cc: Li Dong <lidong@vivo.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Paran Lee <p4ranlee@gmail.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Song Liu <song@kernel.org> Cc: Sun Haiyong <sunhaiyong@loongson.cn> Cc: Thomas Richter <tmricht@linux.ibm.com> Cc: Tiezhu Yang <yangtiezhu@loongson.cn> Cc: Yanteng Si <siyanteng@loongson.cn> Cc: zhaimingbing <zhaimingbing@cmss.chinamobile.com> Link: https://lore.kernel.org/r/20240504213803.218974-5-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 7410d60 commit 7a9418c

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

tools/perf/util/dsos.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,15 @@ bool dsos__read_build_ids(struct dsos *dsos, bool with_hits)
107107
return args.have_build_id;
108108
}
109109

110-
static int __dso__cmp_long_name(const char *long_name, struct dso_id *id, struct dso *b)
110+
static int __dso__cmp_long_name(const char *long_name, const struct dso_id *id,
111+
const struct dso *b)
111112
{
112113
int rc = strcmp(long_name, b->long_name);
113114
return rc ?: dso_id__cmp(id, &b->id);
114115
}
115116

116-
static int __dso__cmp_short_name(const char *short_name, struct dso_id *id, struct dso *b)
117+
static int __dso__cmp_short_name(const char *short_name, const struct dso_id *id,
118+
const struct dso *b)
117119
{
118120
int rc = strcmp(short_name, b->short_name);
119121
return rc ?: dso_id__cmp(id, &b->id);
@@ -133,6 +135,19 @@ static int dsos__cmp_long_name_id_short_name(const void *va, const void *vb)
133135
return rc;
134136
}
135137

138+
struct dsos__key {
139+
const char *long_name;
140+
const struct dso_id *id;
141+
};
142+
143+
static int dsos__cmp_key_long_name_id(const void *vkey, const void *vdso)
144+
{
145+
const struct dsos__key *key = vkey;
146+
const struct dso *dso = *((const struct dso **)vdso);
147+
148+
return __dso__cmp_long_name(key->long_name, key->id, dso);
149+
}
150+
136151
/*
137152
* Find a matching entry and/or link current entry to RB tree.
138153
* Either one of the dso or name parameter must be non-NULL or the
@@ -143,7 +158,11 @@ static struct dso *__dsos__find_by_longname_id(struct dsos *dsos,
143158
struct dso_id *id,
144159
bool write_locked)
145160
{
146-
int low = 0, high = dsos->cnt - 1;
161+
struct dsos__key key = {
162+
.long_name = name,
163+
.id = id,
164+
};
165+
struct dso **res;
147166

148167
if (!dsos->sorted) {
149168
if (!write_locked) {
@@ -162,23 +181,12 @@ static struct dso *__dsos__find_by_longname_id(struct dsos *dsos,
162181
dsos->sorted = true;
163182
}
164183

165-
/*
166-
* Find node with the matching name
167-
*/
168-
while (low <= high) {
169-
int mid = (low + high) / 2;
170-
struct dso *this = dsos->dsos[mid];
171-
int rc = __dso__cmp_long_name(name, id, this);
184+
res = bsearch(&key, dsos->dsos, dsos->cnt, sizeof(struct dso *),
185+
dsos__cmp_key_long_name_id);
186+
if (!res)
187+
return NULL;
172188

173-
if (rc == 0) {
174-
return dso__get(this); /* Find matching dso */
175-
}
176-
if (rc < 0)
177-
high = mid - 1;
178-
else
179-
low = mid + 1;
180-
}
181-
return NULL;
189+
return dso__get(*res);
182190
}
183191

184192
int __dsos__add(struct dsos *dsos, struct dso *dso)

0 commit comments

Comments
 (0)