libperf: Avoid internal moving of fdarray fds
authorAlexey Budankov <alexey.budankov@linux.intel.com>
Fri, 17 Jul 2020 06:59:12 +0000 (09:59 +0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 21 Jul 2020 11:49:30 +0000 (08:49 -0300)
Avoid moving of fds by fdarray__filter() so fds indices returned by
fdarray__add() can be used for access and processing of objects at
struct pollfd *entries.

Signed-off-by: Alexey Budankov <alexey.budankov@linux.intel.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/676844f8-55d3-c628-23db-aa163a81519e@linux.intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/api/fd/array.c
tools/perf/tests/fdarray.c

index 58d44d5eee31d9792c69f879548ff57298116c5c..89f9a2193c2dd16a7f1772265098e4116354ff6c 100644 (file)
@@ -93,22 +93,21 @@ int fdarray__filter(struct fdarray *fda, short revents,
                return 0;
 
        for (fd = 0; fd < fda->nr; ++fd) {
+               if (!fda->entries[fd].events)
+                       continue;
+
                if (fda->entries[fd].revents & revents) {
                        if (entry_destructor)
                                entry_destructor(fda, fd, arg);
 
+                       fda->entries[fd].revents = fda->entries[fd].events = 0;
                        continue;
                }
 
-               if (fd != nr) {
-                       fda->entries[nr] = fda->entries[fd];
-                       fda->priv[nr]    = fda->priv[fd];
-               }
-
                ++nr;
        }
 
-       return fda->nr = nr;
+       return nr;
 }
 
 int fdarray__poll(struct fdarray *fda, int timeout)
index c7c81c4a5b2b59a11b5fc17c2d0496560ece2c0b..d0c8a05aab2f5c8c3f96d1d50b3ba61238374cd2 100644 (file)
@@ -12,6 +12,7 @@ static void fdarray__init_revents(struct fdarray *fda, short revents)
 
        for (fd = 0; fd < fda->nr; ++fd) {
                fda->entries[fd].fd      = fda->nr - fd;
+               fda->entries[fd].events  = revents;
                fda->entries[fd].revents = revents;
        }
 }
@@ -29,7 +30,7 @@ static int fdarray__fprintf_prefix(struct fdarray *fda, const char *prefix, FILE
 
 int test__fdarray__filter(struct test *test __maybe_unused, int subtest __maybe_unused)
 {
-       int nr_fds, expected_fd[2], fd, err = TEST_FAIL;
+       int nr_fds, err = TEST_FAIL;
        struct fdarray *fda = fdarray__new(5, 5);
 
        if (fda == NULL) {
@@ -55,7 +56,6 @@ int test__fdarray__filter(struct test *test __maybe_unused, int subtest __maybe_
 
        fdarray__init_revents(fda, POLLHUP);
        fda->entries[2].revents = POLLIN;
-       expected_fd[0] = fda->entries[2].fd;
 
        pr_debug("\nfiltering all but fda->entries[2]:");
        fdarray__fprintf_prefix(fda, "before", stderr);
@@ -66,17 +66,9 @@ int test__fdarray__filter(struct test *test __maybe_unused, int subtest __maybe_
                goto out_delete;
        }
 
-       if (fda->entries[0].fd != expected_fd[0]) {
-               pr_debug("\nfda->entries[0].fd=%d != %d\n",
-                        fda->entries[0].fd, expected_fd[0]);
-               goto out_delete;
-       }
-
        fdarray__init_revents(fda, POLLHUP);
        fda->entries[0].revents = POLLIN;
-       expected_fd[0] = fda->entries[0].fd;
        fda->entries[3].revents = POLLIN;
-       expected_fd[1] = fda->entries[3].fd;
 
        pr_debug("\nfiltering all but (fda->entries[0], fda->entries[3]):");
        fdarray__fprintf_prefix(fda, "before", stderr);
@@ -88,14 +80,6 @@ int test__fdarray__filter(struct test *test __maybe_unused, int subtest __maybe_
                goto out_delete;
        }
 
-       for (fd = 0; fd < 2; ++fd) {
-               if (fda->entries[fd].fd != expected_fd[fd]) {
-                       pr_debug("\nfda->entries[%d].fd=%d != %d\n", fd,
-                                fda->entries[fd].fd, expected_fd[fd]);
-                       goto out_delete;
-               }
-       }
-
        pr_debug("\n");
 
        err = 0;