[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20231207011722.1220634-48-irogers@google.com>
Date: Wed, 6 Dec 2023 17:17:21 -0800
From: Ian Rogers <irogers@...gle.com>
To: Peter Zijlstra <peterz@...radead.org>,
Ingo Molnar <mingo@...hat.com>,
Arnaldo Carvalho de Melo <acme@...nel.org>,
Mark Rutland <mark.rutland@....com>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
Jiri Olsa <jolsa@...nel.org>,
Namhyung Kim <namhyung@...nel.org>,
Ian Rogers <irogers@...gle.com>,
Adrian Hunter <adrian.hunter@...el.com>,
Nick Terrell <terrelln@...com>,
Kan Liang <kan.liang@...ux.intel.com>,
Andi Kleen <ak@...ux.intel.com>,
Kajol Jain <kjain@...ux.ibm.com>,
Athira Rajeev <atrajeev@...ux.vnet.ibm.com>,
Huacai Chen <chenhuacai@...nel.org>,
Masami Hiramatsu <mhiramat@...nel.org>,
Vincent Whitchurch <vincent.whitchurch@...s.com>,
"Steinar H. Gunderson" <sesse@...gle.com>,
Liam Howlett <liam.howlett@...cle.com>,
Miguel Ojeda <ojeda@...nel.org>,
Colin Ian King <colin.i.king@...il.com>,
Dmitrii Dolgov <9erthalion6@...il.com>,
Yang Jihong <yangjihong1@...wei.com>,
Ming Wang <wangming01@...ngson.cn>,
James Clark <james.clark@....com>,
K Prateek Nayak <kprateek.nayak@....com>,
Sean Christopherson <seanjc@...gle.com>,
Leo Yan <leo.yan@...aro.org>,
Ravi Bangoria <ravi.bangoria@....com>,
German Gomez <german.gomez@....com>,
Changbin Du <changbin.du@...wei.com>,
Paolo Bonzini <pbonzini@...hat.com>, Li Dong <lidong@...o.com>,
Sandipan Das <sandipan.das@....com>,
liuwenyu <liuwenyu7@...wei.com>, linux-kernel@...r.kernel.org,
linux-perf-users@...r.kernel.org,
Guilherme Amadio <amadio@...too.org>
Subject: [PATCH v6 47/47] perf dso: Use container_of to avoid a pointer in dso_data
The dso pointer in dso_data is necessary for reference count checking
to account for the dso_data forming a global list of open dso's with
references to the dso. The dso pointer also allows for the indirection
that reference count checking needs. Outside of reference count
checking the indirection isn't needed and container_of is more
efficient and saves space.
The reference count won't be increased by placing items onto the
global list, matching how things were before the reference count
checking change, but we assert the dso is in dsos holding it live (and
that the set of open dsos is a subset of all dsos for the
machine). Update the DSO data tests so that they use a dsos struct to
make the invariant true.
Signed-off-by: Ian Rogers <irogers@...gle.com>
---
tools/perf/tests/dso-data.c | 60 ++++++++++++++++++-------------------
tools/perf/util/dso.c | 16 +++++++++-
tools/perf/util/dso.h | 2 ++
3 files changed, 46 insertions(+), 32 deletions(-)
diff --git a/tools/perf/tests/dso-data.c b/tools/perf/tests/dso-data.c
index 625dbb2ffe8a..deaefcdd8f09 100644
--- a/tools/perf/tests/dso-data.c
+++ b/tools/perf/tests/dso-data.c
@@ -10,6 +10,7 @@
#include <sys/resource.h>
#include <api/fs/fs.h>
#include "dso.h"
+#include "dsos.h"
#include "machine.h"
#include "symbol.h"
#include "tests.h"
@@ -123,9 +124,10 @@ static int test__dso_data(struct test_suite *test __maybe_unused, int subtest __
TEST_ASSERT_VAL("No test file", file);
memset(&machine, 0, sizeof(machine));
+ dsos__init(&machine.dsos);
- dso = dso__new((const char *)file);
-
+ dso = dso__new(file);
+ TEST_ASSERT_VAL("Failed to add dso", !dsos__add(&machine.dsos, dso));
TEST_ASSERT_VAL("Failed to access to dso",
dso__data_fd(dso, &machine) >= 0);
@@ -170,6 +172,7 @@ static int test__dso_data(struct test_suite *test __maybe_unused, int subtest __
}
dso__put(dso);
+ dsos__exit(&machine.dsos);
unlink(file);
return 0;
}
@@ -199,41 +202,35 @@ static long open_files_cnt(void)
return nr - 1;
}
-static struct dso **dsos;
-
-static int dsos__create(int cnt, int size)
+static int dsos__create(int cnt, int size, struct dsos *dsos)
{
int i;
- dsos = malloc(sizeof(*dsos) * cnt);
- TEST_ASSERT_VAL("failed to alloc dsos array", dsos);
+ dsos__init(dsos);
for (i = 0; i < cnt; i++) {
- char *file;
+ struct dso *dso;
+ char *file = test_file(size);
- file = test_file(size);
TEST_ASSERT_VAL("failed to get dso file", file);
-
- dsos[i] = dso__new(file);
- TEST_ASSERT_VAL("failed to get dso", dsos[i]);
+ dso = dso__new(file);
+ TEST_ASSERT_VAL("failed to get dso", dso);
+ TEST_ASSERT_VAL("failed to add dso", !dsos__add(dsos, dso));
+ dso__put(dso);
}
return 0;
}
-static void dsos__delete(int cnt)
+static void dsos__delete(struct dsos *dsos)
{
- int i;
-
- for (i = 0; i < cnt; i++) {
- struct dso *dso = dsos[i];
+ for (unsigned int i = 0; i < dsos->cnt; i++) {
+ struct dso *dso = dsos->dsos[i];
dso__data_close(dso);
unlink(dso__name(dso));
- dso__put(dso);
}
-
- free(dsos);
+ dsos__exit(dsos);
}
static int set_fd_limit(int n)
@@ -267,10 +264,10 @@ static int test__dso_data_cache(struct test_suite *test __maybe_unused, int subt
/* and this is now our dso open FDs limit */
dso_cnt = limit / 2;
TEST_ASSERT_VAL("failed to create dsos\n",
- !dsos__create(dso_cnt, TEST_FILE_SIZE));
+ !dsos__create(dso_cnt, TEST_FILE_SIZE, &machine.dsos));
for (i = 0; i < (dso_cnt - 1); i++) {
- struct dso *dso = dsos[i];
+ struct dso *dso = machine.dsos.dsos[i];
/*
* Open dsos via dso__data_fd(), it opens the data
@@ -290,17 +287,17 @@ static int test__dso_data_cache(struct test_suite *test __maybe_unused, int subt
}
/* verify the first one is already open */
- TEST_ASSERT_VAL("dsos[0] is not open", dso__data(dsos[0])->fd != -1);
+ TEST_ASSERT_VAL("dsos[0] is not open", dso__data(machine.dsos.dsos[0])->fd != -1);
/* open +1 dso to reach the allowed limit */
- fd = dso__data_fd(dsos[i], &machine);
+ fd = dso__data_fd(machine.dsos.dsos[i], &machine);
TEST_ASSERT_VAL("failed to get fd", fd > 0);
/* should force the first one to be closed */
- TEST_ASSERT_VAL("failed to close dsos[0]", dso__data(dsos[0])->fd == -1);
+ TEST_ASSERT_VAL("failed to close dsos[0]", dso__data(machine.dsos.dsos[0])->fd == -1);
/* cleanup everything */
- dsos__delete(dso_cnt);
+ dsos__delete(&machine.dsos);
/* Make sure we did not leak any file descriptor. */
nr_end = open_files_cnt();
@@ -325,9 +322,9 @@ static int test__dso_data_reopen(struct test_suite *test __maybe_unused, int sub
long nr_end, nr = open_files_cnt(), lim = new_limit(3);
int fd, fd_extra;
-#define dso_0 (dsos[0])
-#define dso_1 (dsos[1])
-#define dso_2 (dsos[2])
+#define dso_0 (machine.dsos.dsos[0])
+#define dso_1 (machine.dsos.dsos[1])
+#define dso_2 (machine.dsos.dsos[2])
/* Rest the internal dso open counter limit. */
reset_fd_limit();
@@ -347,7 +344,8 @@ static int test__dso_data_reopen(struct test_suite *test __maybe_unused, int sub
TEST_ASSERT_VAL("failed to set file limit",
!set_fd_limit((lim)));
- TEST_ASSERT_VAL("failed to create dsos\n", !dsos__create(3, TEST_FILE_SIZE));
+ TEST_ASSERT_VAL("failed to create dsos\n",
+ !dsos__create(3, TEST_FILE_SIZE, &machine.dsos));
/* open dso_0 */
fd = dso__data_fd(dso_0, &machine);
@@ -386,7 +384,7 @@ static int test__dso_data_reopen(struct test_suite *test __maybe_unused, int sub
/* cleanup everything */
close(fd_extra);
- dsos__delete(3);
+ dsos__delete(&machine.dsos);
/* Make sure we did not leak any file descriptor. */
nr_end = open_files_cnt();
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 0fef597725c7..4f20dac89b77 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -496,14 +496,20 @@ static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
static void dso__list_add(struct dso *dso)
{
list_add_tail(&dso__data(dso)->open_entry, &dso__data_open);
+#ifdef REFCNT_CHECKING
dso__data(dso)->dso = dso__get(dso);
+#endif
+ /* Assume the dso is part of dsos, hence the optional reference count above. */
+ assert(dso__dsos(dso));
dso__data_open_cnt++;
}
static void dso__list_del(struct dso *dso)
{
list_del_init(&dso__data(dso)->open_entry);
+#ifdef REFCNT_CHECKING
dso__put(dso__data(dso)->dso);
+#endif
WARN_ONCE(dso__data_open_cnt <= 0,
"DSO data fd counter out of bounds.");
dso__data_open_cnt--;
@@ -653,9 +659,15 @@ static void close_dso(struct dso *dso)
static void close_first_dso(void)
{
struct dso_data *dso_data;
+ struct dso *dso;
dso_data = list_first_entry(&dso__data_open, struct dso_data, open_entry);
- close_dso(dso_data->dso);
+#ifdef REFCNT_CHECKING
+ dso = dso_data->dso;
+#else
+ dso = container_of(dso_data, struct dso, data);
+#endif
+ close_dso(dso);
}
static rlim_t get_fd_limit(void)
@@ -1444,7 +1456,9 @@ struct dso *dso__new_id(const char *name, struct dso_id *id)
data->fd = -1;
data->status = DSO_DATA_STATUS_UNKNOWN;
INIT_LIST_HEAD(&data->open_entry);
+#ifdef REFCNT_CHECKING
data->dso = NULL; /* Set when on the open_entry list. */
+#endif
}
return res;
}
diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h
index fa311ffd2538..e02a4718f1f8 100644
--- a/tools/perf/util/dso.h
+++ b/tools/perf/util/dso.h
@@ -147,7 +147,9 @@ struct dso_cache {
struct dso_data {
struct rb_root cache;
struct list_head open_entry;
+#ifdef REFCNT_CHECKING
struct dso *dso;
+#endif
int fd;
int status;
u32 status_seen;
--
2.43.0.rc2.451.g8631bc7472-goog
Powered by blists - more mailing lists