diff options
Diffstat (limited to 'tools/perf/tests')
-rw-r--r-- | tools/perf/tests/builtin-test.c | 8 | ||||
-rw-r--r-- | tools/perf/tests/evlist.c | 214 | ||||
-rw-r--r-- | tools/perf/tests/fdarray.c | 174 | ||||
-rw-r--r-- | tools/perf/tests/tests.h | 4 |
4 files changed, 180 insertions, 220 deletions
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 174c3ff..ac655b0 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -158,12 +158,12 @@ static struct test { .func = test__switch_tracking, }, { - .desc = "Filter fds with revents mask in a pollfd array", - .func = test__perf_evlist__filter_pollfd, + .desc = "Filter fds with revents mask in a fdarray", + .func = test__fdarray__filter, }, { - .desc = "Add fd to pollfd array, making it autogrow", - .func = test__perf_evlist__add_pollfd, + .desc = "Add fd to a fdarray, making it autogrow", + .func = test__fdarray__add, }, { .func = NULL, diff --git a/tools/perf/tests/evlist.c b/tools/perf/tests/evlist.c deleted file mode 100644 index 99d7dfd..0000000 --- a/tools/perf/tests/evlist.c +++ /dev/null @@ -1,214 +0,0 @@ -#include "util/evlist.h" -#include "util/debug.h" -#include "util/thread_map.h" -#include "util/cpumap.h" -#include "tests/tests.h" - -static void perf_evlist__init_pollfd(struct perf_evlist *evlist, - int nr_fds_alloc, short revents) -{ - int fd; - - evlist->nr_fds = nr_fds_alloc; - - for (fd = 0; fd < nr_fds_alloc; ++fd) { - evlist->pollfd[fd].fd = nr_fds_alloc - fd; - evlist->pollfd[fd].revents = revents; - } -} - -static int perf_evlist__fprintf_pollfd(struct perf_evlist *evlist, - const char *prefix, FILE *fp) -{ - int printed = 0, fd; - - if (!verbose) - return 0; - - printed += fprintf(fp, "\n%s: %3d [ ", prefix, evlist->nr_fds); - for (fd = 0; fd < evlist->nr_fds; ++fd) - printed += fprintf(fp, "%s%d", fd ? ", " : "", evlist->pollfd[fd].fd); - printed += fprintf(fp, " ]"); - return printed; -} - -int test__perf_evlist__filter_pollfd(void) -{ - const int nr_fds_alloc = 5; - int nr_fds, expected_fd[2], fd; - struct pollfd pollfd[nr_fds_alloc]; - struct perf_evlist evlist_alloc = { - .pollfd = pollfd, - }, *evlist = &evlist_alloc; - - perf_evlist__init_pollfd(evlist, nr_fds_alloc, POLLIN); - nr_fds = perf_evlist__filter_pollfd(evlist, POLLHUP); - if (nr_fds != nr_fds_alloc) { - pr_debug("\nperf_evlist__filter_pollfd()=%d != %d shouldn't have filtered anything", - nr_fds, nr_fds_alloc); - return TEST_FAIL; - } - - perf_evlist__init_pollfd(evlist, nr_fds_alloc, POLLHUP); - nr_fds = perf_evlist__filter_pollfd(evlist, POLLHUP); - if (nr_fds != 0) { - pr_debug("\nperf_evlist__filter_pollfd()=%d != %d, should have filtered all fds", - nr_fds, nr_fds_alloc); - return TEST_FAIL; - } - - perf_evlist__init_pollfd(evlist, nr_fds_alloc, POLLHUP); - pollfd[2].revents = POLLIN; - expected_fd[0] = pollfd[2].fd; - - pr_debug("\nfiltering all but pollfd[2]:"); - perf_evlist__fprintf_pollfd(evlist, "before", stderr); - nr_fds = perf_evlist__filter_pollfd(evlist, POLLHUP); - perf_evlist__fprintf_pollfd(evlist, " after", stderr); - if (nr_fds != 1) { - pr_debug("\nperf_evlist__filter_pollfd()=%d != 1, should have left just one event", - nr_fds); - return TEST_FAIL; - } - - if (pollfd[0].fd != expected_fd[0]) { - pr_debug("\npollfd[0].fd=%d != %d\n", pollfd[0].fd, expected_fd[0]); - return TEST_FAIL; - } - - perf_evlist__init_pollfd(evlist, nr_fds_alloc, POLLHUP); - pollfd[0].revents = POLLIN; - expected_fd[0] = pollfd[0].fd; - pollfd[3].revents = POLLIN; - expected_fd[1] = pollfd[3].fd; - - pr_debug("\nfiltering all but (pollfd[0], pollfd[3]):"); - perf_evlist__fprintf_pollfd(evlist, "before", stderr); - nr_fds = perf_evlist__filter_pollfd(evlist, POLLHUP); - perf_evlist__fprintf_pollfd(evlist, " after", stderr); - if (nr_fds != 2) { - pr_debug("\nperf_evlist__filter_pollfd()=%d != 2, should have left just two events", - nr_fds); - return TEST_FAIL; - } - - for (fd = 0; fd < 2; ++fd) { - if (pollfd[fd].fd != expected_fd[fd]) { - pr_debug("\npollfd[%d].fd=%d != %d\n", fd, pollfd[fd].fd, expected_fd[fd]); - return TEST_FAIL; - } - } - - pr_debug("\n"); - - return 0; -} - -int test__perf_evlist__add_pollfd(void) -{ - struct perf_evsel evsel = { - .system_wide = false, - }; - struct thread_map threads = { - .nr = 2, - }; - struct perf_evlist evlist_alloc = { - .pollfd = NULL, - .threads = &threads, - }, *evlist = &evlist_alloc; - - INIT_LIST_HEAD(&evlist->entries); - list_add(&evsel.node, &evlist->entries); - - if (perf_evlist__alloc_pollfd(evlist) < 0) { - pr_debug("\nperf_evlist__alloc_pollfd(evlist) failed!"); - return TEST_FAIL; - } - - if (evlist->nr_fds_alloc != threads.nr) { - pr_debug("\n_evlist__alloc_pollfd: nr_fds_alloc=%d != (threads->nr(%d) * cpu_map->nr(%d))=%d", - evlist->nr_fds_alloc, thread_map__nr(evlist->threads), cpu_map__nr(evlist->cpus), - thread_map__nr(evlist->threads) * cpu_map__nr(evlist->cpus)); - return TEST_FAIL; - } - - if (perf_evlist__add_pollfd(evlist, 1) < 0) { - pr_debug("\nperf_evlist__add_pollfd(evlist, 1) failed!"); - return TEST_FAIL; - } - - if (evlist->nr_fds != 1) { - pr_debug("\nperf_evlist__add_pollfd(evlist, 1)=%d != 1", evlist->nr_fds); - return TEST_FAIL; - } - - if (perf_evlist__add_pollfd(evlist, 2) < 0) { - pr_debug("\nperf_evlist__add_pollfd(evlist, 2) failed!"); - return TEST_FAIL; - } - - if (evlist->nr_fds != 2) { - pr_debug("\nperf_evlist__add_pollfd(evlist, 2)=%d != 2", evlist->nr_fds); - return TEST_FAIL; - } - - perf_evlist__fprintf_pollfd(evlist, "before growing array", stderr); - - if (perf_evlist__add_pollfd(evlist, 35) < 0) { - pr_debug("\nperf_evlist__add_pollfd(evlist, 35) failed!"); - return TEST_FAIL; - } - - if (evlist->nr_fds != 3) { - pr_debug("\nperf_evlist__add_pollfd(evlist, 35)=%d != 3", evlist->nr_fds); - return TEST_FAIL; - } - - if (evlist->pollfd == NULL) { - pr_debug("\nperf_evlist__add_pollfd(evlist, 35) should have allocated evlist->pollfd!"); - return TEST_FAIL; - } - - perf_evlist__fprintf_pollfd(evlist, "after 3rd add_pollfd", stderr); - - if (evlist->pollfd[2].fd != 35) { - pr_debug("\nevlist->pollfd[2](%d) != 35!", evlist->pollfd[2].fd); - return TEST_FAIL; - } - - if (perf_evlist__add_pollfd(evlist, 88) < 0) { - pr_debug("\nperf_evlist__add_pollfd(evlist, 88) failed!"); - return TEST_FAIL; - } - - if (evlist->nr_fds != 4) { - pr_debug("\nperf_evlist__add_pollfd(evlist, 88)=%d != 2", evlist->nr_fds); - return TEST_FAIL; - } - - perf_evlist__fprintf_pollfd(evlist, "after 4th add_pollfd", stderr); - - if (evlist->pollfd[0].fd != 1) { - pr_debug("\nevlist->pollfd[0](%d) != 1!", evlist->pollfd[0].fd); - return TEST_FAIL; - } - - if (evlist->pollfd[1].fd != 2) { - pr_debug("\nevlist->pollfd[1](%d) != 2!", evlist->pollfd[1].fd); - return TEST_FAIL; - } - - if (evlist->pollfd[2].fd != 35) { - pr_debug("\nevlist->pollfd[2](%d) != 35!", evlist->pollfd[2].fd); - return TEST_FAIL; - } - - if (evlist->pollfd[3].fd != 88) { - pr_debug("\nevlist->pollfd[3](%d) != 88!", evlist->pollfd[3].fd); - return TEST_FAIL; - } - - pr_debug("\n"); - - return 0; -} diff --git a/tools/perf/tests/fdarray.c b/tools/perf/tests/fdarray.c new file mode 100644 index 0000000..a0fea62 --- /dev/null +++ b/tools/perf/tests/fdarray.c @@ -0,0 +1,174 @@ +#include <api/fd/array.h> +#include "util/debug.h" +#include "tests/tests.h" + +static void fdarray__init_revents(struct fdarray *fda, short revents) +{ + int fd; + + fda->nr = fda->nr_alloc; + + for (fd = 0; fd < fda->nr; ++fd) { + fda->entries[fd].fd = fda->nr - fd; + fda->entries[fd].revents = revents; + } +} + +static int fdarray__fprintf_prefix(struct fdarray *fda, const char *prefix, FILE *fp) +{ + int printed = 0; + + if (!verbose) + return 0; + + printed += fprintf(fp, "\n%s: ", prefix); + return printed + fdarray__fprintf(fda, fp); +} + +int test__fdarray__filter(void) +{ + int nr_fds, expected_fd[2], fd, err = TEST_FAIL; + struct fdarray *fda = fdarray__new(5, 5); + + if (fda == NULL) { + pr_debug("\nfdarray__new() failed!"); + goto out; + } + + fdarray__init_revents(fda, POLLIN); + nr_fds = fdarray__filter(fda, POLLHUP); + if (nr_fds != fda->nr_alloc) { + pr_debug("\nfdarray__filter()=%d != %d shouldn't have filtered anything", + nr_fds, fda->nr_alloc); + goto out_delete; + } + + fdarray__init_revents(fda, POLLHUP); + nr_fds = fdarray__filter(fda, POLLHUP); + if (nr_fds != 0) { + pr_debug("\nfdarray__filter()=%d != %d, should have filtered all fds", + nr_fds, fda->nr_alloc); + goto out_delete; + } + + 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); + nr_fds = fdarray__filter(fda, POLLHUP); + fdarray__fprintf_prefix(fda, " after", stderr); + if (nr_fds != 1) { + pr_debug("\nfdarray__filter()=%d != 1, should have left just one event", nr_fds); + 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); + nr_fds = fdarray__filter(fda, POLLHUP); + fdarray__fprintf_prefix(fda, " after", stderr); + if (nr_fds != 2) { + pr_debug("\nfdarray__filter()=%d != 2, should have left just two events", + nr_fds); + 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; +out_delete: + fdarray__delete(fda); +out: + return err; +} + +int test__fdarray__add(void) +{ + int err = TEST_FAIL; + struct fdarray *fda = fdarray__new(2, 2); + + if (fda == NULL) { + pr_debug("\nfdarray__new() failed!"); + goto out; + } + +#define FDA_CHECK(_idx, _fd, _revents) \ + if (fda->entries[_idx].fd != _fd) { \ + pr_debug("\n%d: fda->entries[%d](%d) != %d!", \ + __LINE__, _idx, fda->entries[1].fd, _fd); \ + goto out_delete; \ + } \ + if (fda->entries[_idx].events != (_revents)) { \ + pr_debug("\n%d: fda->entries[%d].revents(%d) != %d!", \ + __LINE__, _idx, fda->entries[_idx].fd, _revents); \ + goto out_delete; \ + } + +#define FDA_ADD(_idx, _fd, _revents, _nr) \ + if (fdarray__add(fda, _fd, _revents) < 0) { \ + pr_debug("\n%d: fdarray__add(fda, %d, %d) failed!", \ + __LINE__,_fd, _revents); \ + goto out_delete; \ + } \ + if (fda->nr != _nr) { \ + pr_debug("\n%d: fdarray__add(fda, %d, %d)=%d != %d", \ + __LINE__,_fd, _revents, fda->nr, _nr); \ + goto out_delete; \ + } \ + FDA_CHECK(_idx, _fd, _revents) + + FDA_ADD(0, 1, POLLIN, 1); + FDA_ADD(1, 2, POLLERR, 2); + + fdarray__fprintf_prefix(fda, "before growing array", stderr); + + FDA_ADD(2, 35, POLLHUP, 3); + + if (fda->entries == NULL) { + pr_debug("\nfdarray__add(fda, 35, POLLHUP) should have allocated fda->pollfd!"); + goto out_delete; + } + + fdarray__fprintf_prefix(fda, "after 3rd add", stderr); + + FDA_ADD(3, 88, POLLIN | POLLOUT, 4); + + fdarray__fprintf_prefix(fda, "after 4th add", stderr); + + FDA_CHECK(0, 1, POLLIN); + FDA_CHECK(1, 2, POLLERR); + FDA_CHECK(2, 35, POLLHUP); + FDA_CHECK(3, 88, POLLIN | POLLOUT); + +#undef FDA_ADD +#undef FDA_CHECK + + pr_debug("\n"); + + err = 0; +out_delete: + fdarray__delete(fda); +out: + return err; +} diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 699d4bb..00e776a 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -49,8 +49,8 @@ int test__thread_mg_share(void); int test__hists_output(void); int test__hists_cumulate(void); int test__switch_tracking(void); -int test__perf_evlist__filter_pollfd(void); -int test__perf_evlist__add_pollfd(void); +int test__fdarray__filter(void); +int test__fdarray__add(void); #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) #ifdef HAVE_DWARF_UNWIND_SUPPORT |