This repository was archived by the owner on Feb 1, 2026. It is now read-only.
forked from detailyang/ipc_benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuds.c
More file actions
103 lines (85 loc) · 2.43 KB
/
uds.c
File metadata and controls
103 lines (85 loc) · 2.43 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
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
double
getdetlatimeofday(struct timeval *begin, struct timeval *end)
{
return (end->tv_sec + end->tv_usec * 1.0 / 1000000) -
(begin->tv_sec + begin->tv_usec * 1.0 / 1000000);
}
int main(int argc, char *argv[]) {
int fd, nfd;
int i, size, count, sum, n;
char *buf;
size_t len;
struct timeval begin, end;
struct sockaddr_un un;
if (argc != 3) {
printf("usage: ./uds <size> <count>\n");
return 1;
}
size = atoi(argv[1]);
count = atoi(argv[2]);
buf = malloc(size);
if (buf == NULL) {
perror("malloc");
return 1;
}
if (fork() == 0) {
fd = socket(AF_UNIX, SOCK_STREAM, 0);
unlink("./uds-ipc");
un.sun_family = AF_UNIX;
strcpy(un.sun_path, "./uds-ipc");
len = offsetof(struct sockaddr_un, sun_path) + strlen("./uds-ipc");
if (bind(fd, (struct sockaddr *)&un, len) == -1) {
perror("bind");
return 1;
}
listen(fd, 1024);
if ((nfd = accept(fd, NULL, NULL)) == -1) {
perror("accept");
return 1;
}
sum = 0;
for (i = 0; i < count; i++) {
n = read(nfd, buf, size);
if (n == -1) {
return 1;
}
sum += n;
}
if (sum != count * size) {
return 1;
}
} else {
sleep(1);
fd = socket(AF_UNIX, SOCK_STREAM, 0);
un.sun_family = AF_UNIX;
strcpy(un.sun_path, "./uds-ipc");
len = offsetof(struct sockaddr_un, sun_path) + strlen("./uds-ipc");
nfd = connect(fd, (struct sockaddr *)&un, len);
if (fd == -1) {
perror("connect");
return 1;
}
gettimeofday(&begin, NULL);
for (i = 0; i < count; i++) {
if (write(nfd, buf, size) != size) {
perror("wirte");
return 1;
}
}
gettimeofday(&end, NULL);
printf("%.0fMb/s %.0fmsg/s\n",
(count * size * 1.0 / getdetlatimeofday(&begin, &end)) * 8 / 1000000,
(count * 1.0 / getdetlatimeofday(&begin, &end)));
}
return 0;
}