diff options
| author | Paton J. Lewis <palewis@adobe.com> | 2012-10-04 20:13:39 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-05 14:05:00 -0400 |
| commit | 03a7beb55b9fad363f0dd33e72ccf2d3e1c2a406 (patch) | |
| tree | e89cb2a2db5645600f28699ebf3b4a98195a3fb3 /tools/testing | |
| parent | a0a0a7a94c765f7219b57fa3b79389901bb0bc99 (diff) | |
epoll: support for disabling items, and a self-test app
Enhanced epoll_ctl to support EPOLL_CTL_DISABLE, which disables an epoll
item. If epoll_ctl doesn't return -EBUSY in this case, it is then safe to
delete the epoll item in a multi-threaded environment. Also added a new
test_epoll self- test app to both demonstrate the need for this feature
and test it.
Signed-off-by: Paton J. Lewis <palewis@adobe.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Jason Baron <jbaron@redhat.com>
Cc: Paul Holland <pholland@adobe.com>
Cc: Davide Libenzi <davidel@xmailserver.org>
Cc: Michael Kerrisk <mtk.manpages@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'tools/testing')
| -rw-r--r-- | tools/testing/selftests/Makefile | 2 | ||||
| -rw-r--r-- | tools/testing/selftests/epoll/Makefile | 11 | ||||
| -rw-r--r-- | tools/testing/selftests/epoll/test_epoll.c | 344 |
3 files changed, 356 insertions, 1 deletions
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 85baf11e2acd..43480149119e 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug | 1 | TARGETS = breakpoints kcmp mqueue vm cpu-hotplug memory-hotplug epoll |
| 2 | 2 | ||
| 3 | all: | 3 | all: |
| 4 | for TARGET in $(TARGETS); do \ | 4 | for TARGET in $(TARGETS); do \ |
diff --git a/tools/testing/selftests/epoll/Makefile b/tools/testing/selftests/epoll/Makefile new file mode 100644 index 000000000000..19806ed62f50 --- /dev/null +++ b/tools/testing/selftests/epoll/Makefile | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | # Makefile for epoll selftests | ||
| 2 | |||
| 3 | all: test_epoll | ||
| 4 | %: %.c | ||
| 5 | gcc -pthread -g -o $@ $^ | ||
| 6 | |||
| 7 | run_tests: all | ||
| 8 | ./test_epoll | ||
| 9 | |||
| 10 | clean: | ||
| 11 | $(RM) test_epoll | ||
diff --git a/tools/testing/selftests/epoll/test_epoll.c b/tools/testing/selftests/epoll/test_epoll.c new file mode 100644 index 000000000000..e0fcff1e8331 --- /dev/null +++ b/tools/testing/selftests/epoll/test_epoll.c | |||
| @@ -0,0 +1,344 @@ | |||
| 1 | /* | ||
| 2 | * tools/testing/selftests/epoll/test_epoll.c | ||
| 3 | * | ||
| 4 | * Copyright 2012 Adobe Systems Incorporated | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * Paton J. Lewis <palewis@adobe.com> | ||
| 12 | * | ||
| 13 | */ | ||
| 14 | |||
| 15 | #include <errno.h> | ||
| 16 | #include <fcntl.h> | ||
| 17 | #include <pthread.h> | ||
| 18 | #include <stdio.h> | ||
| 19 | #include <stdlib.h> | ||
| 20 | #include <unistd.h> | ||
| 21 | #include <sys/epoll.h> | ||
| 22 | #include <sys/socket.h> | ||
| 23 | |||
| 24 | /* | ||
| 25 | * A pointer to an epoll_item_private structure will be stored in the epoll | ||
| 26 | * item's event structure so that we can get access to the epoll_item_private | ||
| 27 | * data after calling epoll_wait: | ||
| 28 | */ | ||
| 29 | struct epoll_item_private { | ||
| 30 | int index; /* Position of this struct within the epoll_items array. */ | ||
| 31 | int fd; | ||
| 32 | uint32_t events; | ||
| 33 | pthread_mutex_t mutex; /* Guards the following variables... */ | ||
| 34 | int stop; | ||
| 35 | int status; /* Stores any error encountered while handling item. */ | ||
| 36 | /* The following variable allows us to test whether we have encountered | ||
| 37 | a problem while attempting to cancel and delete the associated | ||
| 38 | event. When the test program exits, 'deleted' should be exactly | ||
| 39 | one. If it is greater than one, then the failed test reflects a real | ||
| 40 | world situation where we would have tried to access the epoll item's | ||
| 41 | private data after deleting it: */ | ||
| 42 | int deleted; | ||
| 43 | }; | ||
| 44 | |||
| 45 | struct epoll_item_private *epoll_items; | ||
| 46 | |||
| 47 | /* | ||
| 48 | * Delete the specified item from the epoll set. In a real-world secneario this | ||
| 49 | * is where we would free the associated data structure, but in this testing | ||
| 50 | * environment we retain the structure so that we can test for double-deletion: | ||
| 51 | */ | ||
| 52 | void delete_item(int index) | ||
| 53 | { | ||
| 54 | __sync_fetch_and_add(&epoll_items[index].deleted, 1); | ||
| 55 | } | ||
| 56 | |||
| 57 | /* | ||
| 58 | * A pointer to a read_thread_data structure will be passed as the argument to | ||
| 59 | * each read thread: | ||
| 60 | */ | ||
| 61 | struct read_thread_data { | ||
| 62 | int stop; | ||
| 63 | int status; /* Indicates any error encountered by the read thread. */ | ||
| 64 | int epoll_set; | ||
| 65 | }; | ||
| 66 | |||
| 67 | /* | ||
| 68 | * The function executed by the read threads: | ||
| 69 | */ | ||
| 70 | void *read_thread_function(void *function_data) | ||
| 71 | { | ||
| 72 | struct read_thread_data *thread_data = | ||
| 73 | (struct read_thread_data *)function_data; | ||
| 74 | struct epoll_event event_data; | ||
| 75 | struct epoll_item_private *item_data; | ||
| 76 | char socket_data; | ||
| 77 | |||
| 78 | /* Handle events until we encounter an error or this thread's 'stop' | ||
| 79 | condition is set: */ | ||
| 80 | while (1) { | ||
| 81 | int result = epoll_wait(thread_data->epoll_set, | ||
| 82 | &event_data, | ||
| 83 | 1, /* Number of desired events */ | ||
| 84 | 1000); /* Timeout in ms */ | ||
| 85 | if (result < 0) { | ||
| 86 | /* Breakpoints signal all threads. Ignore that while | ||
| 87 | debugging: */ | ||
| 88 | if (errno == EINTR) | ||
| 89 | continue; | ||
| 90 | thread_data->status = errno; | ||
| 91 | return 0; | ||
| 92 | } else if (thread_data->stop) | ||
| 93 | return 0; | ||
| 94 | else if (result == 0) /* Timeout */ | ||
| 95 | continue; | ||
| 96 | |||
| 97 | /* We need the mutex here because checking for the stop | ||
| 98 | condition and re-enabling the epoll item need to be done | ||
| 99 | together as one atomic operation when EPOLL_CTL_DISABLE is | ||
| 100 | available: */ | ||
| 101 | item_data = (struct epoll_item_private *)event_data.data.ptr; | ||
| 102 | pthread_mutex_lock(&item_data->mutex); | ||
| 103 | |||
| 104 | /* Remove the item from the epoll set if we want to stop | ||
| 105 | handling that event: */ | ||
| 106 | if (item_data->stop) | ||
| 107 | delete_item(item_data->index); | ||
| 108 | else { | ||
| 109 | /* Clear the data that was written to the other end of | ||
| 110 | our non-blocking socket: */ | ||
| 111 | do { | ||
| 112 | if (read(item_data->fd, &socket_data, 1) < 1) { | ||
| 113 | if ((errno == EAGAIN) || | ||
| 114 | (errno == EWOULDBLOCK)) | ||
| 115 | break; | ||
| 116 | else | ||
| 117 | goto error_unlock; | ||
| 118 | } | ||
| 119 | } while (item_data->events & EPOLLET); | ||
| 120 | |||
| 121 | /* The item was one-shot, so re-enable it: */ | ||
| 122 | event_data.events = item_data->events; | ||
| 123 | if (epoll_ctl(thread_data->epoll_set, | ||
| 124 | EPOLL_CTL_MOD, | ||
| 125 | item_data->fd, | ||
| 126 | &event_data) < 0) | ||
| 127 | goto error_unlock; | ||
| 128 | } | ||
| 129 | |||
| 130 | pthread_mutex_unlock(&item_data->mutex); | ||
| 131 | } | ||
| 132 | |||
| 133 | error_unlock: | ||
| 134 | thread_data->status = item_data->status = errno; | ||
| 135 | pthread_mutex_unlock(&item_data->mutex); | ||
| 136 | return 0; | ||
| 137 | } | ||
| 138 | |||
| 139 | /* | ||
| 140 | * A pointer to a write_thread_data structure will be passed as the argument to | ||
| 141 | * the write thread: | ||
| 142 | */ | ||
| 143 | struct write_thread_data { | ||
| 144 | int stop; | ||
| 145 | int status; /* Indicates any error encountered by the write thread. */ | ||
| 146 | int n_fds; | ||
| 147 | int *fds; | ||
| 148 | }; | ||
| 149 | |||
| 150 | /* | ||
| 151 | * The function executed by the write thread. It writes a single byte to each | ||
| 152 | * socket in turn until the stop condition for this thread is set. If writing to | ||
| 153 | * a socket would block (i.e. errno was EAGAIN), we leave that socket alone for | ||
| 154 | * the moment and just move on to the next socket in the list. We don't care | ||
| 155 | * about the order in which we deliver events to the epoll set. In fact we don't | ||
| 156 | * care about the data we're writing to the pipes at all; we just want to | ||
