-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuds_blocking.c
More file actions
239 lines (209 loc) · 6.89 KB
/
uds_blocking.c
File metadata and controls
239 lines (209 loc) · 6.89 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "uds_blocking.h"
#include "benchmark.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/select.h>
/* Setup Unix Domain Socket server */
UDSBlockingState* setup_uds_blocking_server(const char* socket_path) {
UDSBlockingState* state = malloc(sizeof(UDSBlockingState));
if (!state) {
perror("malloc");
return NULL;
}
// Create socket
state->fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (state->fd == -1) {
perror("socket");
free(state);
return NULL;
}
// Remove existing socket file if it exists
unlink(socket_path);
// Bind socket
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
if (bind(state->fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("bind");
close(state->fd);
free(state);
return NULL;
}
// Listen for connections
if (listen(state->fd, 1) == -1) {
perror("listen");
close(state->fd);
free(state);
return NULL;
}
state->is_server = true;
return state;
}
/* Setup Unix Domain Socket client */
UDSBlockingState* setup_uds_blocking_client(const char* socket_path) {
UDSBlockingState* state = malloc(sizeof(UDSBlockingState));
if (!state) {
perror("malloc");
return NULL;
}
// Create socket
state->fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (state->fd == -1) {
perror("socket");
free(state);
return NULL;
}
// Connect to server
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
if (connect(state->fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("connect");
close(state->fd);
free(state);
return NULL;
}
state->is_server = false;
return state;
}
/* Run the Unix Domain Socket server benchmark */
void run_uds_blocking_server(UDSBlockingState* state, int duration_secs, float work_secs) {
void* buffer = malloc(MAX_MSG_SIZE);
if (!buffer) {
perror("malloc");
return;
}
int client_fd = accept(state->fd, NULL, NULL);
if (client_fd == -1) {
perror("accept");
free(buffer);
return;
}
uint64_t start_time = get_timestamp_us();
uint64_t end_time = start_time + (duration_secs * 1000000ULL);
printf("Server ready to process messages\n");
while (get_timestamp_us() < end_time) {
// First, read the header to get the size
ssize_t header_bytes = recv(client_fd, buffer, sizeof(Message), 0);
if (header_bytes <= 0) break;
Message* msg = (Message*)buffer;
size_t msg_size = msg->size;
if (msg_size < sizeof(Message) + sizeof(uint32_t) || msg_size > MAX_MSG_SIZE) {
fprintf(stderr, "Server: Invalid message size %zu\n", msg_size);
break;
}
// Read the rest of the message
ssize_t body_bytes = recv(client_fd, (char*)buffer + sizeof(Message), msg_size - sizeof(Message), 0);
if (body_bytes < 0) break;
if (!validate_message(msg, msg_size)) {
fprintf(stderr, "Server: Message validation failed\n");
}
// Simulate work if requested
if (work_secs > 0.0f) {
usleep(work_secs * 1000000);
}
if (send(client_fd, buffer, msg_size, 0) != (ssize_t)msg_size) {
perror("send");
break;
}
}
close(client_fd);
free(buffer);
}
/* Run the Unix Domain Socket client benchmark */
void run_uds_blocking_client(UDSBlockingState* state, int duration_secs, BenchmarkStats* stats) {
void* buffer = malloc(MAX_MSG_SIZE);
if (!buffer) {
perror("malloc");
return;
}
uint64_t* latencies = malloc(sizeof(uint64_t) * MAX_LATENCIES);
if (!latencies) {
perror("malloc");
free(buffer);
return;
}
size_t latency_count = 0;
double cpu_start = get_cpu_usage();
Message* msg = (Message*)buffer;
// Benchmark phase
stats->ops = 0;
stats->bytes = 0;
uint64_t start_time = get_timestamp_us();
uint64_t end_time = start_time + (duration_secs * 1000000ULL);
while (get_timestamp_us() < end_time) {
// Send message
random_message(msg, 2048, 4096);
msg->timestamp = get_timestamp_us();
ssize_t sent = send(state->fd, buffer, msg->size, 0);
if (sent != msg->size) {
if (sent < 0 && (errno == EPIPE || errno == ECONNRESET)) {
fprintf(stderr, "Connection closed by server (benchmark)\n");
break;
}
perror("send");
break;
}
// Read header
ssize_t header_bytes = recv(state->fd, buffer, sizeof(Message), 0);
if (header_bytes <= 0) {
if (header_bytes == 0 || (header_bytes < 0 && (errno == EPIPE || errno == ECONNRESET))) {
fprintf(stderr, "Connection closed by server (benchmark)\n");
break;
}
fprintf(stderr, "Failed to read message header\n");
break;
}
Message* response = (Message*)buffer;
size_t msg_size = response->size;
if (msg_size < sizeof(Message) + sizeof(uint32_t) || msg_size > MAX_MSG_SIZE) {
fprintf(stderr, "Client: Invalid message size %zu\n", msg_size);
break;
}
// Read body
ssize_t body_bytes = recv(state->fd, (char*)buffer + sizeof(Message), msg_size - sizeof(Message), 0);
if (body_bytes < 0) {
if (errno == EPIPE || errno == ECONNRESET) {
fprintf(stderr, "Connection closed by server (benchmark)\n");
break;
}
fprintf(stderr, "Failed to read message body\n");
break;
}
if (!validate_message(response, msg_size)) {
fprintf(stderr, "Client: Message validation failed\n");
}
// Calculate latency
uint64_t now = get_timestamp_us();
uint64_t latency = now - msg->timestamp;
if (latency_count < MAX_LATENCIES) {
latencies[latency_count++] = latency;
}
// Update statistics
stats->ops++;
stats->bytes += msg_size;
}
double cpu_end = get_cpu_usage();
stats->cpu_usage = (cpu_end - cpu_start) / (10000.0 * duration_secs);
calculate_stats(latencies, latency_count, stats);
free(buffer);
free(latencies);
}
/* Free Unix Domain Socket resources */
void free_uds_blocking(UDSBlockingState* state) {
if (state) {
close(state->fd);
if (state->is_server) {
unlink(SOCKET_PATH);
}
free(state);
}
}