-
Notifications
You must be signed in to change notification settings - Fork 8
/
hackbench.c
178 lines (148 loc) · 3.48 KB
/
hackbench.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Test groups of 20 processes spraying to 20 receivers */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/poll.h>
#include "interbench.h"
#define DATASIZE 100
#define LOOPS 100
#define NUM_FDS 20
static inline void barf(const char *msg)
{
terminal_error(msg);
}
static void fdpair(int fds[2])
{
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
barf("Creating fdpair");
}
/* Block until we're ready to go */
static void ready(int ready_out, int wakefd)
{
char dummy;
struct pollfd pollfd = { .fd = wakefd, .events = POLLIN };
/* Tell them we're ready. */
if (write(ready_out, &dummy, 1) != 1)
barf("CLIENT: ready write");
/* Wait for "GO" signal */
if (poll(&pollfd, 1, -1) != 1)
barf("poll");
}
/* Sender sprays LOOPS messages down each file descriptor */
static void sender(int out_fd[NUM_FDS],
int ready_out,
int wakefd)
{
char data[DATASIZE];
unsigned int i, j;
ready(ready_out, wakefd);
/* Now pump to every receiver. */
for (i = 0; i < LOOPS; i++) {
for (j = 0; j < NUM_FDS; j++) {
int ret;
unsigned long done = 0;
again:
ret = write(out_fd[j], data + done, sizeof(data)-done);
if (ret < 0)
barf("SENDER: write");
done += ret;
if (done < sizeof(data))
goto again;
}
}
}
/* One receiver per fd */
static void receiver(unsigned int num_packets,
int in_fd,
int ready_out,
int wakefd)
{
unsigned int i;
/* Wait for start... */
ready(ready_out, wakefd);
/* Receive them all */
for (i = 0; i < num_packets; i++) {
char data[DATASIZE];
int ret, done = 0;
again:
ret = Read(in_fd, data + done, DATASIZE - done);
done += ret;
if (done < DATASIZE)
goto again;
}
}
/* One group of senders and receivers */
static unsigned int group(int receivers, int ready_out, int wakefd)
{
unsigned int i;
int *out_fds = calloc(sizeof(int *), receivers + 1);
for (i = 0; i < NUM_FDS; i++) {
int fds[2];
/* Create the pipe between client and server */
fdpair(fds);
/* Fork the receiver. */
switch (fork()) {
case -1: barf("fork()");
case 0:
close(fds[1]);
receiver(NUM_FDS*LOOPS, fds[0], ready_out, wakefd);
exit(0);
}
out_fds[i] = fds[1];
close(fds[0]);
}
/* Now we have all the fds, fork the senders */
for (i = 0; i < NUM_FDS; i++) {
switch (fork()) {
case -1: barf("fork()");
case 0:
sender(out_fds, ready_out, wakefd);
exit(0);
}
}
/* Close the fds we have left */
for (i = 0; i < NUM_FDS; i++)
close(out_fds[i]);
free(out_fds);
/* Return number of children to reap */
return NUM_FDS * 2;
}
void *hackbench_thread(void *t)
{
unsigned int i, *num_groups, total_children;
int readyfds[2], wakefds[2];
char dummy;
num_groups = t;
fdpair(readyfds);
fdpair(wakefds);
while (1) {
total_children = 0;
for (i = 0; i < *num_groups; i++)
total_children += group(*num_groups, readyfds[1], wakefds[0]);
/* Wait for everyone to be ready */
for (i = 0; i < total_children; i++)
if (Read(readyfds[0], &dummy, 1) != 1)
barf("Reading for readyfds");
/* Kick them off */
if (write(wakefds[1], &dummy, 1) != 1)
barf("Writing to start them");
/* Reap them all */
for (i = 0; i < total_children; i++) {
int status;
wait(&status);
if (!WIFEXITED(status))
exit(1);
}
if (!trywait_sem(&hackthread.sem.stop))
break;
}
post_sem(&hackthread.sem.complete);
return NULL;
}