~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/tests/fdarray.c

Version: ~ [ linux-5.4-rc7 ] ~ [ linux-5.3.11 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.84 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.154 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.201 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.201 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.77 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #include <api/fd/array.h>
  2 #include "util/debug.h"
  3 #include "tests/tests.h"
  4 
  5 static void fdarray__init_revents(struct fdarray *fda, short revents)
  6 {
  7         int fd;
  8 
  9         fda->nr = fda->nr_alloc;
 10 
 11         for (fd = 0; fd < fda->nr; ++fd) {
 12                 fda->entries[fd].fd      = fda->nr - fd;
 13                 fda->entries[fd].revents = revents;
 14         }
 15 }
 16 
 17 static int fdarray__fprintf_prefix(struct fdarray *fda, const char *prefix, FILE *fp)
 18 {
 19         int printed = 0;
 20 
 21         if (!verbose)
 22                 return 0;
 23 
 24         printed += fprintf(fp, "\n%s: ", prefix);
 25         return printed + fdarray__fprintf(fda, fp);
 26 }
 27 
 28 int test__fdarray__filter(void)
 29 {
 30         int nr_fds, expected_fd[2], fd, err = TEST_FAIL;
 31         struct fdarray *fda = fdarray__new(5, 5);
 32 
 33         if (fda == NULL) {
 34                 pr_debug("\nfdarray__new() failed!");
 35                 goto out;
 36         }
 37 
 38         fdarray__init_revents(fda, POLLIN);
 39         nr_fds = fdarray__filter(fda, POLLHUP, NULL);
 40         if (nr_fds != fda->nr_alloc) {
 41                 pr_debug("\nfdarray__filter()=%d != %d shouldn't have filtered anything",
 42                          nr_fds, fda->nr_alloc);
 43                 goto out_delete;
 44         }
 45 
 46         fdarray__init_revents(fda, POLLHUP);
 47         nr_fds = fdarray__filter(fda, POLLHUP, NULL);
 48         if (nr_fds != 0) {
 49                 pr_debug("\nfdarray__filter()=%d != %d, should have filtered all fds",
 50                          nr_fds, fda->nr_alloc);
 51                 goto out_delete;
 52         }
 53 
 54         fdarray__init_revents(fda, POLLHUP);
 55         fda->entries[2].revents = POLLIN;
 56         expected_fd[0] = fda->entries[2].fd;
 57 
 58         pr_debug("\nfiltering all but fda->entries[2]:");
 59         fdarray__fprintf_prefix(fda, "before", stderr);
 60         nr_fds = fdarray__filter(fda, POLLHUP, NULL);
 61         fdarray__fprintf_prefix(fda, " after", stderr);
 62         if (nr_fds != 1) {
 63                 pr_debug("\nfdarray__filter()=%d != 1, should have left just one event", nr_fds);
 64                 goto out_delete;
 65         }
 66 
 67         if (fda->entries[0].fd != expected_fd[0]) {
 68                 pr_debug("\nfda->entries[0].fd=%d != %d\n",
 69                          fda->entries[0].fd, expected_fd[0]);
 70                 goto out_delete;
 71         }
 72 
 73         fdarray__init_revents(fda, POLLHUP);
 74         fda->entries[0].revents = POLLIN;
 75         expected_fd[0] = fda->entries[0].fd;
 76         fda->entries[3].revents = POLLIN;
 77         expected_fd[1] = fda->entries[3].fd;
 78 
 79         pr_debug("\nfiltering all but (fda->entries[0], fda->entries[3]):");
 80         fdarray__fprintf_prefix(fda, "before", stderr);
 81         nr_fds = fdarray__filter(fda, POLLHUP, NULL);
 82         fdarray__fprintf_prefix(fda, " after", stderr);
 83         if (nr_fds != 2) {
 84                 pr_debug("\nfdarray__filter()=%d != 2, should have left just two events",
 85                          nr_fds);
 86                 goto out_delete;
 87         }
 88 
 89         for (fd = 0; fd < 2; ++fd) {
 90                 if (fda->entries[fd].fd != expected_fd[fd]) {
 91                         pr_debug("\nfda->entries[%d].fd=%d != %d\n", fd,
 92                                  fda->entries[fd].fd, expected_fd[fd]);
 93                         goto out_delete;
 94                 }
 95         }
 96 
 97         pr_debug("\n");
 98 
 99         err = 0;
100 out_delete:
101         fdarray__delete(fda);
102 out:
103         return err;
104 }
105 
106 int test__fdarray__add(void)
107 {
108         int err = TEST_FAIL;
109         struct fdarray *fda = fdarray__new(2, 2);
110 
111         if (fda == NULL) {
112                 pr_debug("\nfdarray__new() failed!");
113                 goto out;
114         }
115 
116 #define FDA_CHECK(_idx, _fd, _revents)                                     \
117         if (fda->entries[_idx].fd != _fd) {                                \
118                 pr_debug("\n%d: fda->entries[%d](%d) != %d!",              \
119                          __LINE__, _idx, fda->entries[1].fd, _fd);         \
120                 goto out_delete;                                           \
121         }                                                                  \
122         if (fda->entries[_idx].events != (_revents)) {                     \
123                 pr_debug("\n%d: fda->entries[%d].revents(%d) != %d!",      \
124                          __LINE__, _idx, fda->entries[_idx].fd, _revents); \
125                 goto out_delete;                                           \
126         }
127 
128 #define FDA_ADD(_idx, _fd, _revents, _nr)                                  \
129         if (fdarray__add(fda, _fd, _revents) < 0) {                        \
130                 pr_debug("\n%d: fdarray__add(fda, %d, %d) failed!",        \
131                          __LINE__,_fd, _revents);                          \
132                 goto out_delete;                                           \
133         }                                                                  \
134         if (fda->nr != _nr) {                                              \
135                 pr_debug("\n%d: fdarray__add(fda, %d, %d)=%d != %d",       \
136                          __LINE__,_fd, _revents, fda->nr, _nr);            \
137                 goto out_delete;                                           \
138         }                                                                  \
139         FDA_CHECK(_idx, _fd, _revents)
140 
141         FDA_ADD(0, 1, POLLIN, 1);
142         FDA_ADD(1, 2, POLLERR, 2);
143 
144         fdarray__fprintf_prefix(fda, "before growing array", stderr);
145 
146         FDA_ADD(2, 35, POLLHUP, 3);
147 
148         if (fda->entries == NULL) {
149                 pr_debug("\nfdarray__add(fda, 35, POLLHUP) should have allocated fda->pollfd!");
150                 goto out_delete;
151         }
152 
153         fdarray__fprintf_prefix(fda, "after 3rd add", stderr);
154 
155         FDA_ADD(3, 88, POLLIN | POLLOUT, 4);
156 
157         fdarray__fprintf_prefix(fda, "after 4th add", stderr);
158 
159         FDA_CHECK(0, 1, POLLIN);
160         FDA_CHECK(1, 2, POLLERR);
161         FDA_CHECK(2, 35, POLLHUP);
162         FDA_CHECK(3, 88, POLLIN | POLLOUT);
163 
164 #undef FDA_ADD
165 #undef FDA_CHECK
166 
167         pr_debug("\n");
168 
169         err = 0;
170 out_delete:
171         fdarray__delete(fda);
172 out:
173         return err;
174 }
175 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp