1 /* Test groups of 20 processes spraying to 20 receivers */
9 #include <sys/socket.h>
13 #include "interbench.h"
19 static inline void barf(const char *msg
)
24 static void fdpair(int fds
[2])
26 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) == -1)
27 barf("Creating fdpair");
30 /* Block until we're ready to go */
31 static void ready(int ready_out
, int wakefd
)
34 struct pollfd pollfd
= { .fd
= wakefd
, .events
= POLLIN
};
36 /* Tell them we're ready. */
37 if (write(ready_out
, &dummy
, 1) != 1)
38 barf("CLIENT: ready write");
40 /* Wait for "GO" signal */
41 if (poll(&pollfd
, 1, -1) != 1)
45 /* Sender sprays LOOPS messages down each file descriptor */
46 static void sender(int out_fd
[NUM_FDS
],
53 ready(ready_out
, wakefd
);
55 /* Now pump to every receiver. */
56 for (i
= 0; i
< LOOPS
; i
++) {
57 for (j
= 0; j
< NUM_FDS
; j
++) {
59 unsigned long done
= 0;
62 ret
= write(out_fd
[j
], data
+ done
, sizeof(data
)-done
);
64 barf("SENDER: write");
66 if (done
< sizeof(data
))
72 /* One receiver per fd */
73 static void receiver(unsigned int num_packets
,
80 /* Wait for start... */
81 ready(ready_out
, wakefd
);
83 /* Receive them all */
84 for (i
= 0; i
< num_packets
; i
++) {
89 ret
= Read(in_fd
, data
+ done
, DATASIZE
- done
);
96 /* One group of senders and receivers */
97 static unsigned int group(int ready_out
,
101 int out_fds
[NUM_FDS
];
103 for (i
= 0; i
< NUM_FDS
; i
++) {
106 /* Create the pipe between client and server */
109 /* Fork the receiver. */
111 case -1: barf("fork()");
114 receiver(NUM_FDS
*LOOPS
, fds
[0], ready_out
, wakefd
);
122 /* Now we have all the fds, fork the senders */
123 for (i
= 0; i
< NUM_FDS
; i
++) {
125 case -1: barf("fork()");
127 sender(out_fds
, ready_out
, wakefd
);
132 /* Close the fds we have left */
133 for (i
= 0; i
< NUM_FDS
; i
++)
136 /* Return number of children to reap */
140 void *hackbench_thread(void *t
)
142 unsigned int i
, num_groups
, total_children
;
143 int readyfds
[2], wakefds
[2];
154 for (i
= 0; i
< num_groups
; i
++)
155 total_children
+= group(readyfds
[1], wakefds
[0]);
157 /* Wait for everyone to be ready */
158 for (i
= 0; i
< total_children
; i
++)
159 if (Read(readyfds
[0], &dummy
, 1) != 1)
160 barf("Reading for readyfds");
163 if (write(wakefds
[1], &dummy
, 1) != 1)
164 barf("Writing to start them");
167 for (i
= 0; i
< total_children
; i
++) {
170 if (!WIFEXITED(status
))
173 if (!trywait_sem(&hackthread
.sem
.stop
))
177 post_sem(&hackthread
.sem
.complete
);