1 /* Test groups of 20 processes spraying to 20 receivers */
6 #include <sys/socket.h>
12 static unsigned int loops = 100;
13 static int use_pipes = 0;
15 static void barf(const char *msg)
17 fprintf(stderr, "%s (error: %s)\n", msg, strerror(errno));
21 static void fdpair(int fds[2])
27 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0)
30 barf("Creating fdpair");
33 /* Block until we're ready to go */
34 static void ready(int ready_out, int wakefd)
37 struct pollfd pollfd = { .fd = wakefd, .events = POLLIN };
39 /* Tell them we're ready. */
40 if (write(ready_out, &dummy, 1) != 1)
41 barf("CLIENT: ready write");
43 /* Wait for "GO" signal */
44 if (poll(&pollfd, 1, -1) != 1)
48 /* Sender sprays loops messages down each file descriptor */
49 static void sender(unsigned int num_fds,
57 ready(ready_out, wakefd);
59 /* Now pump to every receiver. */
60 for (i = 0; i < loops; i++) {
61 for (j = 0; j < num_fds; j++) {
65 ret = write(out_fd[j], data + done, sizeof(data)-done);
67 barf("SENDER: write");
69 if (done < sizeof(data))
75 /* One receiver per fd */
76 static void receiver(unsigned int num_packets,
83 /* Wait for start... */
84 ready(ready_out, wakefd);
86 /* Receive them all */
87 for (i = 0; i < num_packets; i++) {
92 ret = read(in_fd, data + done, DATASIZE - done);
101 /* One group of senders and receivers */
102 static unsigned int group(unsigned int num_fds,
107 unsigned int out_fds[num_fds];
109 for (i = 0; i < num_fds; i++) {
112 /* Create the pipe between client and server */
115 /* Fork the receiver. */
117 case -1: barf("fork()");
120 receiver(num_fds*loops, fds[0], ready_out, wakefd);
128 /* Now we have all the fds, fork the senders */
129 for (i = 0; i < num_fds; i++) {
131 case -1: barf("fork()");
133 sender(num_fds, out_fds, ready_out, wakefd);
138 /* Close the fds we have left */
139 for (i = 0; i < num_fds; i++)
142 /* Return number of children to reap */
146 int main(int argc, char *argv[])
148 unsigned int i, num_groups, total_children;
149 struct timeval start, stop, diff;
150 unsigned int num_fds = 20;
151 int readyfds[2], wakefds[2];
154 if (argv[1] && strcmp(argv[1], "-pipe") == 0) {
160 if (argc != 2 || (num_groups = atoi(argv[1])) == 0)
161 barf("Usage: hackbench [-pipe] <num groups>\n");
167 for (i = 0; i < num_groups; i++)
168 total_children += group(num_fds, readyfds[1], wakefds[0]);
170 /* Wait for everyone to be ready */
171 for (i = 0; i < total_children; i++)
172 if (read(readyfds[0], &dummy, 1) != 1)
173 barf("Reading for readyfds");
175 gettimeofday(&start, NULL);
178 if (write(wakefds[1], &dummy, 1) != 1)
179 barf("Writing to start them");
182 for (i = 0; i < total_children; i++) {
185 if (!WIFEXITED(status))
189 gettimeofday(&stop, NULL);
192 timersub(&stop, &start, &diff);
193 printf("Time: %lu.%03lu\n", diff.tv_sec, diff.tv_usec/1000);