~vcs-imports/junkcode/main

« back to all changes in this revision

Viewing changes to messaging/unix_stream.c

  • Committer: tridge
  • Date: 2006-08-13 06:50:46 UTC
  • Revision ID: vcs-imports@canonical.com-20060813065046-faca53adb6ad9f49
messaging tests from Mainz

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   measure latency of unix domain sockets
 
3
   tridge@samba.org July 2006
 
4
*/
 
5
 
 
6
#define _GNU_SOURCE
 
7
#include <stdio.h>
 
8
#include <stdlib.h>
 
9
#include <unistd.h>
 
10
#include <stdarg.h>
 
11
#include <fcntl.h>
 
12
#include <sys/ioctl.h>
 
13
#include <sys/types.h>
 
14
#include <sys/socket.h>
 
15
#include <sys/un.h>
 
16
#include <sys/poll.h>
 
17
#include <sys/time.h>
 
18
#include <time.h>
 
19
#include <errno.h>
 
20
 
 
21
 
 
22
static struct timeval tp1,tp2;
 
23
 
 
24
static void start_timer()
 
25
{
 
26
        gettimeofday(&tp1,NULL);
 
27
}
 
28
 
 
29
static double end_timer()
 
30
{
 
31
        gettimeofday(&tp2,NULL);
 
32
        return (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - 
 
33
                (tp1.tv_sec + (tp1.tv_usec*1.0e-6));
 
34
}
 
35
 
 
36
static void fatal(const char *why)
 
37
{
 
38
        fprintf(stderr, "fatal: %s - %s\n", why, strerror(errno));
 
39
        exit(1);
 
40
}
 
41
 
 
42
/*
 
43
  connect to a unix domain socket
 
44
*/
 
45
int ux_socket_connect(const char *name)
 
46
{
 
47
        int fd;
 
48
        struct sockaddr_un addr;
 
49
 
 
50
        memset(&addr, 0, sizeof(addr));
 
51
        addr.sun_family = AF_UNIX;
 
52
        strncpy(addr.sun_path, name, sizeof(addr.sun_path));
 
53
 
 
54
        fd = socket(AF_UNIX, SOCK_STREAM, 0);
 
55
        if (fd == -1) {
 
56
                return -1;
 
57
        }
 
58
        
 
59
        if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
 
60
                close(fd);
 
61
                return -1;
 
62
        }
 
63
 
 
64
        return fd;
 
65
}
 
66
 
 
67
 
 
68
/*
 
69
  create a unix domain socket and bind it
 
70
  return a file descriptor open on the socket 
 
71
*/
 
72
static int ux_socket_bind(const char *name)
 
73
{
 
74
        int fd;
 
75
        struct sockaddr_un addr;
 
76
 
 
77
        /* get rid of any old socket */
 
78
        unlink(name);
 
79
 
 
80
        fd = socket(AF_UNIX, SOCK_STREAM, 0);
 
81
        if (fd == -1) return -1;
 
82
 
 
83
        memset(&addr, 0, sizeof(addr));
 
84
        addr.sun_family = AF_UNIX;
 
85
        strncpy(addr.sun_path, name, sizeof(addr.sun_path));
 
86
 
 
87
        if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
 
88
                close(fd);
 
89
                return -1;
 
90
        }       
 
91
 
 
92
        listen(fd, 1);
 
93
 
 
94
        return fd;
 
95
}
 
96
 
 
97
/*
 
98
  accept on a unix socket
 
99
*/
 
100
static int ux_socket_accept(int fd)
 
101
{
 
102
        struct sockaddr a;
 
103
        socklen_t len;
 
104
        return accept(fd, &a, &len);
 
105
}
 
106
 
 
107
static void worker(const char *n1, const char *n2, int w)
 
108
{
 
109
        int l = ux_socket_bind(n1);
 
110
        int s2, s1;
 
111
        int count=0;
 
112
 
 
113
        sleep(1);
 
114
 
 
115
        s2 = ux_socket_connect(n2);
 
116
        s1 = ux_socket_accept(l);
 
117
        start_timer();
 
118
 
 
119
        while (1) {
 
120
                char c=0;
 
121
                if (write(s2, &c, 1) != 1) {
 
122
                        fatal("write");
 
123
                }
 
124
                if (read(s1, &c, 1) != 1) {
 
125
                        fatal("read");
 
126
                }
 
127
                if (w == 1 && (end_timer() > 1.0)) {
 
128
                        printf("%8u ops/sec\r", 
 
129
                               (unsigned)(2*count/end_timer()));
 
130
                        fflush(stdout);
 
131
                        start_timer();
 
132
                        count=0;
 
133
                }
 
134
                count++;
 
135
        }
 
136
}
 
137
 
 
138
int main(int argc, char *argv[])
 
139
{
 
140
        if (fork() == 0) {
 
141
                worker("sock2.unx", "sock1.unx", 0);
 
142
        }
 
143
        worker("sock1.unx", "sock2.unx", 1);
 
144
        return 0;
 
145
}