perf tools: Fix the performance issue for v6.10

These addresses the performance issues reported by Matt, Namhyung and
 Linus.  Recently it changed processing comm string and DSO with sorted
 arrays but it required to sort the array whenever it adds a new entry.
 This caused a performance issue and fix is to enhance the sorting by
 finding the insertion point in the sorted array and to shift righthand
 side using memmove().
 
 Signed-off-by: Namhyung Kim <namhyung@kernel.org>
 -----BEGIN PGP SIGNATURE-----
 
 iHUEABYIAB0WIQSo2x5BnqMqsoHtzsmMstVUGiXMgwUCZov/wgAKCRCMstVUGiXM
 g3olAQCFzp/BnopE7VgUSK5j0EOnMjSsvkQGkocqCVN1Km3y8AEAlV3EKb1rUN8s
 SQ+QcEx7F4V38s+Aoo2SqU1yAwYsXAc=
 =Ao/v
 -----END PGP SIGNATURE-----

Merge tag 'perf-tools-fixes-for-v6.10-2024-07-08' of git://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools

Pull perf tools fixes from Namhyung Kim:
 "Fix performance issue for v6.10

  These address the performance issues reported by Matt, Namhyung and
  Linus. Recently perf changed the processing of the comm string and DSO
  using sorted arrays but this caused it to sort the array whenever
  adding a new entry.

  This caused a performance issue and the fix is to enhance the sorting
  by finding the insertion point in the sorted array and to shift
  righthand side using memmove()"

* tag 'perf-tools-fixes-for-v6.10-2024-07-08' of git://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools:
  perf dsos: When adding a dso into sorted dsos maintain the sort order
  perf comm str: Avoid sort during insert
This commit is contained in:
Linus Torvalds 2024-07-08 14:08:43 -07:00
commit 4376e966ec
2 changed files with 39 additions and 16 deletions

View File

@ -86,14 +86,6 @@ static struct comm_str *comm_str__new(const char *str)
return result;
}
static int comm_str__cmp(const void *_lhs, const void *_rhs)
{
const struct comm_str *lhs = *(const struct comm_str * const *)_lhs;
const struct comm_str *rhs = *(const struct comm_str * const *)_rhs;
return strcmp(comm_str__str(lhs), comm_str__str(rhs));
}
static int comm_str__search(const void *_key, const void *_member)
{
const char *key = _key;
@ -169,9 +161,24 @@ static struct comm_str *comm_strs__findnew(const char *str)
}
result = comm_str__new(str);
if (result) {
comm_strs->strs[comm_strs->num_strs++] = result;
qsort(comm_strs->strs, comm_strs->num_strs, sizeof(struct comm_str *),
comm_str__cmp);
int low = 0, high = comm_strs->num_strs - 1;
int insert = comm_strs->num_strs; /* Default to inserting at the end. */
while (low <= high) {
int mid = low + (high - low) / 2;
int cmp = strcmp(comm_str__str(comm_strs->strs[mid]), str);
if (cmp < 0) {
low = mid + 1;
} else {
high = mid - 1;
insert = mid;
}
}
memmove(&comm_strs->strs[insert + 1], &comm_strs->strs[insert],
(comm_strs->num_strs - insert) * sizeof(struct comm_str *));
comm_strs->num_strs++;
comm_strs->strs[insert] = result;
}
}
up_write(&comm_strs->lock);

View File

@ -203,11 +203,27 @@ int __dsos__add(struct dsos *dsos, struct dso *dso)
dsos->dsos = temp;
dsos->allocated = to_allocate;
}
dsos->dsos[dsos->cnt++] = dso__get(dso);
if (dsos->cnt >= 2 && dsos->sorted) {
dsos->sorted = dsos__cmp_long_name_id_short_name(&dsos->dsos[dsos->cnt - 2],
&dsos->dsos[dsos->cnt - 1])
<= 0;
if (!dsos->sorted) {
dsos->dsos[dsos->cnt++] = dso__get(dso);
} else {
int low = 0, high = dsos->cnt - 1;
int insert = dsos->cnt; /* Default to inserting at the end. */
while (low <= high) {
int mid = low + (high - low) / 2;
int cmp = dsos__cmp_long_name_id_short_name(&dsos->dsos[mid], &dso);
if (cmp < 0) {
low = mid + 1;
} else {
high = mid - 1;
insert = mid;
}
}
memmove(&dsos->dsos[insert + 1], &dsos->dsos[insert],
(dsos->cnt - insert) * sizeof(struct dso *));
dsos->cnt++;
dsos->dsos[insert] = dso__get(dso);
}
dso__set_dsos(dso, dsos);
return 0;