huangcm
2025-07-03 5fc6eec0444a62f7a596240b200dd837059dba70
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
240
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/* This program benchmarks a QEMUD pipe to exchange data with a test
 * server.
 *
 * See test_host_1.c for the corresponding server code, which simply
 * sends back anything it receives from the client.
 */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "test_util.h"
 
#define  PIPE_NAME  "pingpong"
 
char* progname;
 
static void usage(int code)
{
    printf("Usage: %s [options]\n\n", progname);
    printf(
      "Valid options are:\n\n"
      "  -? -h --help  Print this message\n"
      "  -pipe <name>  Use pipe name (default: " PIPE_NAME ")\n"
      "  -tcp <port>   Use local tcp port\n"
      "  -size <size>  Specify packet size\n"
      "\n"
    );
    exit(code);
}
 
int main(int argc, char** argv)
{
    Pipe        pipe[1];
    const char* tcpPort = NULL;
    int         localPort = 0;
    const char* pipeName = NULL;
    const char* packetSize = NULL;
    int         port = 8012;
    int         maxCount   = 1000;
    int         bufferSize = 16384;
    uint8_t*    buffer;
    uint8_t*    buffer2;
    int         nn, count;
    double      time0, time1;
 
    /* Extract program name */
    {
        char* p = strrchr(argv[0], '/');
        if (p == NULL)
            progname = argv[0];
        else
            progname = p+1;
    }
 
    /* Parse options */
    while (argc > 1 && argv[1][0] == '-') {
        char* arg = argv[1];
        if (!strcmp(arg, "-?") || !strcmp(arg, "-h") || !strcmp(arg, "--help")) {
            usage(0);
        } else if (!strcmp(arg, "-pipe")) {
            if (argc < 3) {
                fprintf(stderr, "-pipe option needs an argument! See --help for details.\n");
                exit(1);
            }
            argc--;
            argv++;
            pipeName = argv[1];
        } else if (!strcmp(arg, "-tcp")) {
            if (argc < 3) {
                fprintf(stderr, "-tcp option needs an argument! See --help for details.\n");
                exit(1);
            }
            argc--;
            argv++;
            tcpPort = argv[1];
        } else if (!strcmp(arg, "-size")) {
            if (argc < 3) {
                fprintf(stderr, "-tcp option needs an argument! See --help for details.\n");
                exit(1);
            }
            argc--;
            argv++;
            packetSize = argv[1];
        } else {
            fprintf(stderr, "UNKNOWN OPTION: %s\n\n", arg);
            usage(1);
        }
        argc--;
        argv++;
    }
 
    /* Check arguments */
    if (tcpPort && pipeName) {
        fprintf(stderr, "You can't use both -pipe and -tcp at the same time\n");
        exit(2);
    }
 
    if (tcpPort != NULL) {
        localPort = atoi(tcpPort);
        if (localPort <= 0 || localPort > 65535) {
            fprintf(stderr, "Invalid port number: %s\n", tcpPort);
            exit(2);
        }
    } else if (pipeName == NULL) {
        /* Use default pipe name */
        pipeName = PIPE_NAME;
    }
 
    if (packetSize != NULL) {
        int  size = atoi(packetSize);
        if (size <= 0) {
            fprintf(stderr, "Invalid byte size: %s\n", packetSize);
            exit(3);
        }
        bufferSize = size;
    }
 
    /* Open the pipe */
    if (tcpPort != NULL) {
        if (pipe_openSocket(pipe, localPort) < 0) {
            fprintf(stderr, "Could not open tcp socket!\n");
            return 1;
        }
        printf("Connected to tcp:localhost:%d\n", port);
    }
    else {
        if (pipe_openQemuPipe(pipe, pipeName) < 0) {
            fprintf(stderr, "Could not open '%s' pipe: %s\n", pipeName, strerror(errno));
            return 1;
        }
        printf("Connected to '%s' pipe\n", pipeName);
    }
 
    /* Allocate buffers, setup their data */
    buffer  = malloc(bufferSize);
    buffer2 = malloc(bufferSize);
 
    for (nn = 0; nn < bufferSize; nn++) {
        buffer[nn] = (uint8_t)nn;
    }
 
    /* Do the work! */
    time0 = now_secs();
 
    for (count = 0; count < maxCount; count++) {
        int ret = pipe_send(pipe, buffer, bufferSize);
        int pos, len;
 
        if (ret < 0) {
            fprintf(stderr,"%d: Sending %d bytes failed: %s\n", count, bufferSize, strerror(errno));
            return 1;
        }
 
#if 1
        /* The server is supposed to send the message back */
        pos = 0;
        len = bufferSize;
        while (len > 0) {
            ret = pipe_recv(pipe, buffer2 + pos, len);
            if (ret < 0) {
                fprintf(stderr, "Receiving failed (ret=%d): %s\n", ret, strerror(errno));
                return 3;
            }
            if (ret == 0) {
                fprintf(stderr, "Disconnection while receiving!\n");
                return 4;
            }
            pos += ret;
            len -= ret;
        }
 
        if (memcmp(buffer, buffer2, bufferSize) != 0) {
            fprintf(stderr, "Message content mismatch!\n");
            const int maxAvail = 16;
            const int maxLines = 12;
            int numLines = 0;
            for (nn = 0; nn < bufferSize; ) {
                int avail = bufferSize - nn;
                int mm;
                if (avail > maxAvail)
                    avail = maxAvail;
 
                if (memcmp(buffer+nn, buffer2+nn, avail) != 0) {
                    if (++numLines >= maxLines) {
                        printf(".... to be continued ...\n");
                        break;
                    }
                    printf("%04x:", nn);
 
                    for (mm = 0; mm < avail; mm++)
                        printf(" %02x", buffer[nn+mm]);
                    for ( ; mm < maxAvail; mm++ )
                        printf("   ");
 
                    printf( " -- " );
 
                    for (mm = 0; mm < avail; mm++)
                        printf(" %02x", buffer2[nn+mm]);
 
                    printf ("\n");
                }
                nn += avail;
            }
            return 6;
        }
 
#endif
 
        if (count > 0 && (count % 200) == 0) {
            printf("... %d\n", count);
        }
    }
 
    time1 = now_secs();
 
    printf("Closing pipe\n");
    pipe_close(pipe);
 
    printf("Total time: %g seconds\n", time1 - time0);
    printf("Total bytes: %g bytes\n", 1.0*maxCount*bufferSize);
    printf("Bandwidth: %g MB/s\n", (maxCount*bufferSize/(1024.0*1024.0))/(time1 - time0) );
    return 0;
}