nsprpub/pr/tests/thrpool_client.c

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /***********************************************************************
michael@0 7 **
michael@0 8 ** Name: thrpool_client.c
michael@0 9 **
michael@0 10 ** Description: Test threadpool functionality.
michael@0 11 **
michael@0 12 ** Modification History:
michael@0 13 */
michael@0 14 #include "primpl.h"
michael@0 15
michael@0 16 #include "plgetopt.h"
michael@0 17
michael@0 18 #include <stdio.h>
michael@0 19 #include <string.h>
michael@0 20 #include <errno.h>
michael@0 21 #ifdef XP_UNIX
michael@0 22 #include <sys/mman.h>
michael@0 23 #endif
michael@0 24 #if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)
michael@0 25 #include <pthread.h>
michael@0 26 #endif
michael@0 27
michael@0 28 #ifdef WIN32
michael@0 29 #include <process.h>
michael@0 30 #endif
michael@0 31
michael@0 32 static int _debug_on = 0;
michael@0 33 static int server_port = -1;
michael@0 34 static char *program_name = NULL;
michael@0 35
michael@0 36 #include "obsolete/prsem.h"
michael@0 37
michael@0 38 #ifdef XP_PC
michael@0 39 #define mode_t int
michael@0 40 #endif
michael@0 41
michael@0 42 #define DPRINTF(arg) if (_debug_on) printf arg
michael@0 43
michael@0 44 #define BUF_DATA_SIZE (2 * 1024)
michael@0 45 #define TCP_MESG_SIZE 1024
michael@0 46 #define NUM_TCP_CLIENTS 10 /* for a listen queue depth of 5 */
michael@0 47
michael@0 48 #define NUM_TCP_CONNECTIONS_PER_CLIENT 10
michael@0 49 #define NUM_TCP_MESGS_PER_CONNECTION 10
michael@0 50 #define TCP_SERVER_PORT 10000
michael@0 51
michael@0 52 static PRInt32 num_tcp_clients = NUM_TCP_CLIENTS;
michael@0 53 static PRInt32 num_tcp_connections_per_client = NUM_TCP_CONNECTIONS_PER_CLIENT;
michael@0 54 static PRInt32 tcp_mesg_size = TCP_MESG_SIZE;
michael@0 55 static PRInt32 num_tcp_mesgs_per_connection = NUM_TCP_MESGS_PER_CONNECTION;
michael@0 56
michael@0 57 int failed_already=0;
michael@0 58
michael@0 59 typedef struct buffer {
michael@0 60 char data[BUF_DATA_SIZE];
michael@0 61 } buffer;
michael@0 62
michael@0 63 PRNetAddr tcp_server_addr, udp_server_addr;
michael@0 64
michael@0 65 typedef struct Client_Param {
michael@0 66 PRNetAddr server_addr;
michael@0 67 PRMonitor *exit_mon; /* monitor to signal on exit */
michael@0 68 PRInt32 *exit_counter; /* counter to decrement, before exit */
michael@0 69 PRInt32 datalen;
michael@0 70 } Client_Param;
michael@0 71
michael@0 72 /*
michael@0 73 * readn
michael@0 74 * read data from sockfd into buf
michael@0 75 */
michael@0 76 static PRInt32
michael@0 77 readn(PRFileDesc *sockfd, char *buf, int len)
michael@0 78 {
michael@0 79 int rem;
michael@0 80 int bytes;
michael@0 81 int offset = 0;
michael@0 82 PRIntervalTime timeout = PR_INTERVAL_NO_TIMEOUT;
michael@0 83
michael@0 84 for (rem=len; rem; offset += bytes, rem -= bytes) {
michael@0 85 DPRINTF(("thread = 0x%lx: calling PR_Recv, bytes = %d\n",
michael@0 86 PR_GetCurrentThread(), rem));
michael@0 87 bytes = PR_Recv(sockfd, buf + offset, rem, 0,
michael@0 88 timeout);
michael@0 89 DPRINTF(("thread = 0x%lx: returning from PR_Recv, bytes = %d\n",
michael@0 90 PR_GetCurrentThread(), bytes));
michael@0 91 if (bytes < 0) {
michael@0 92 return -1;
michael@0 93 }
michael@0 94 }
michael@0 95 return len;
michael@0 96 }
michael@0 97
michael@0 98 /*
michael@0 99 * writen
michael@0 100 * write data from buf to sockfd
michael@0 101 */
michael@0 102 static PRInt32
michael@0 103 writen(PRFileDesc *sockfd, char *buf, int len)
michael@0 104 {
michael@0 105 int rem;
michael@0 106 int bytes;
michael@0 107 int offset = 0;
michael@0 108
michael@0 109 for (rem=len; rem; offset += bytes, rem -= bytes) {
michael@0 110 DPRINTF(("thread = 0x%lx: calling PR_Send, bytes = %d\n",
michael@0 111 PR_GetCurrentThread(), rem));
michael@0 112 bytes = PR_Send(sockfd, buf + offset, rem, 0,
michael@0 113 PR_INTERVAL_NO_TIMEOUT);
michael@0 114 DPRINTF(("thread = 0x%lx: returning from PR_Send, bytes = %d\n",
michael@0 115 PR_GetCurrentThread(), bytes));
michael@0 116 if (bytes <= 0)
michael@0 117 return -1;
michael@0 118 }
michael@0 119 return len;
michael@0 120 }
michael@0 121
michael@0 122 /*
michael@0 123 * TCP_Client
michael@0 124 * Client job
michael@0 125 * Connect to the server at the address specified in the argument.
michael@0 126 * Fill in a buffer, write data to server, read it back and check
michael@0 127 * for data corruption.
michael@0 128 * Close the socket for server connection
michael@0 129 */
michael@0 130 static void PR_CALLBACK
michael@0 131 TCP_Client(void *arg)
michael@0 132 {
michael@0 133 Client_Param *cp = (Client_Param *) arg;
michael@0 134 PRFileDesc *sockfd;
michael@0 135 buffer *in_buf, *out_buf;
michael@0 136 union PRNetAddr netaddr;
michael@0 137 PRInt32 bytes, i, j;
michael@0 138
michael@0 139
michael@0 140 DPRINTF(("TCP client started\n"));
michael@0 141 bytes = cp->datalen;
michael@0 142 out_buf = PR_NEW(buffer);
michael@0 143 if (out_buf == NULL) {
michael@0 144 fprintf(stderr,"%s: failed to alloc buffer struct\n", program_name);
michael@0 145 failed_already=1;
michael@0 146 return;
michael@0 147 }
michael@0 148 in_buf = PR_NEW(buffer);
michael@0 149 if (in_buf == NULL) {
michael@0 150 fprintf(stderr,"%s: failed to alloc buffer struct\n", program_name);
michael@0 151 failed_already=1;
michael@0 152 return;
michael@0 153 }
michael@0 154 netaddr.inet.family = cp->server_addr.inet.family;
michael@0 155 netaddr.inet.port = cp->server_addr.inet.port;
michael@0 156 netaddr.inet.ip = cp->server_addr.inet.ip;
michael@0 157
michael@0 158 for (i = 0; i < num_tcp_connections_per_client; i++) {
michael@0 159 if ((sockfd = PR_OpenTCPSocket(PR_AF_INET)) == NULL) {
michael@0 160 fprintf(stderr,"%s: PR_OpenTCPSocket failed\n", program_name);
michael@0 161 failed_already=1;
michael@0 162 return;
michael@0 163 }
michael@0 164
michael@0 165 DPRINTF(("TCP client connecting to server:%d\n", server_port));
michael@0 166 if (PR_Connect(sockfd, &netaddr,PR_INTERVAL_NO_TIMEOUT) < 0){
michael@0 167 fprintf(stderr, "PR_Connect failed: (%ld, %ld)\n",
michael@0 168 PR_GetError(), PR_GetOSError());
michael@0 169 failed_already=1;
michael@0 170 return;
michael@0 171 }
michael@0 172 for (j = 0; j < num_tcp_mesgs_per_connection; j++) {
michael@0 173 /*
michael@0 174 * fill in random data
michael@0 175 */
michael@0 176 memset(out_buf->data, ((PRInt32) (&netaddr)) + i + j, bytes);
michael@0 177 /*
michael@0 178 * write to server
michael@0 179 */
michael@0 180 if (writen(sockfd, out_buf->data, bytes) < bytes) {
michael@0 181 fprintf(stderr,"%s: ERROR - TCP_Client:writen\n", program_name);
michael@0 182 failed_already=1;
michael@0 183 return;
michael@0 184 }
michael@0 185 /*
michael@0 186 DPRINTF(("TCP Client [0x%lx]: out_buf = 0x%lx out_buf[0] = 0x%lx\n",
michael@0 187 PR_GetCurrentThread(), out_buf, (*((int *) out_buf->data))));
michael@0 188 */
michael@0 189 if (readn(sockfd, in_buf->data, bytes) < bytes) {
michael@0 190 fprintf(stderr,"%s: ERROR - TCP_Client:readn\n", program_name);
michael@0 191 failed_already=1;
michael@0 192 return;
michael@0 193 }
michael@0 194 /*
michael@0 195 * verify the data read
michael@0 196 */
michael@0 197 if (memcmp(in_buf->data, out_buf->data, bytes) != 0) {
michael@0 198 fprintf(stderr,"%s: ERROR - data corruption\n", program_name);
michael@0 199 failed_already=1;
michael@0 200 return;
michael@0 201 }
michael@0 202 }
michael@0 203 /*
michael@0 204 * shutdown reads and writes
michael@0 205 */
michael@0 206 if (PR_Shutdown(sockfd, PR_SHUTDOWN_BOTH) < 0) {
michael@0 207 fprintf(stderr,"%s: ERROR - PR_Shutdown\n", program_name);
michael@0 208 failed_already=1;
michael@0 209 }
michael@0 210 PR_Close(sockfd);
michael@0 211 }
michael@0 212
michael@0 213 PR_DELETE(out_buf);
michael@0 214 PR_DELETE(in_buf);
michael@0 215
michael@0 216 /*
michael@0 217 * Decrement exit_counter and notify parent thread
michael@0 218 */
michael@0 219
michael@0 220 PR_EnterMonitor(cp->exit_mon);
michael@0 221 --(*cp->exit_counter);
michael@0 222 PR_Notify(cp->exit_mon);
michael@0 223 PR_ExitMonitor(cp->exit_mon);
michael@0 224 DPRINTF(("TCP_Client exiting\n"));
michael@0 225 }
michael@0 226
michael@0 227 /*
michael@0 228 * TCP_Socket_Client_Server_Test - concurrent server test
michael@0 229 *
michael@0 230 * Each client connects to the server and sends a chunk of data
michael@0 231 * For each connection, server reads the data
michael@0 232 * from the client and sends it back to the client, unmodified.
michael@0 233 * Each client checks that data received from server is same as the
michael@0 234 * data it sent to the server.
michael@0 235 *
michael@0 236 */
michael@0 237
michael@0 238 static PRInt32
michael@0 239 TCP_Socket_Client_Server_Test(void)
michael@0 240 {
michael@0 241 int i;
michael@0 242 Client_Param *cparamp;
michael@0 243 PRMonitor *mon2;
michael@0 244 PRInt32 datalen;
michael@0 245 PRInt32 connections = 0;
michael@0 246 PRThread *thr;
michael@0 247
michael@0 248 datalen = tcp_mesg_size;
michael@0 249 connections = 0;
michael@0 250
michael@0 251 mon2 = PR_NewMonitor();
michael@0 252 if (mon2 == NULL) {
michael@0 253 fprintf(stderr,"%s: PR_NewMonitor failed\n", program_name);
michael@0 254 failed_already=1;
michael@0 255 return -1;
michael@0 256 }
michael@0 257
michael@0 258 /*
michael@0 259 * Start client jobs
michael@0 260 */
michael@0 261 cparamp = PR_NEW(Client_Param);
michael@0 262 if (cparamp == NULL) {
michael@0 263 fprintf(stderr,"%s: PR_NEW failed\n", program_name);
michael@0 264 failed_already=1;
michael@0 265 return -1;
michael@0 266 }
michael@0 267 cparamp->server_addr.inet.family = PR_AF_INET;
michael@0 268 cparamp->server_addr.inet.port = PR_htons(server_port);
michael@0 269 cparamp->server_addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
michael@0 270 cparamp->exit_mon = mon2;
michael@0 271 cparamp->exit_counter = &connections;
michael@0 272 cparamp->datalen = datalen;
michael@0 273 for (i = 0; i < num_tcp_clients; i++) {
michael@0 274 thr = PR_CreateThread(PR_USER_THREAD, TCP_Client, (void *)cparamp,
michael@0 275 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0);
michael@0 276 if (NULL == thr) {
michael@0 277 fprintf(stderr,"%s: PR_CreateThread failed\n", program_name);
michael@0 278 failed_already=1;
michael@0 279 return -1;
michael@0 280 }
michael@0 281 PR_EnterMonitor(mon2);
michael@0 282 connections++;
michael@0 283 PR_ExitMonitor(mon2);
michael@0 284 DPRINTF(("Created TCP client = 0x%lx\n", thr));
michael@0 285 }
michael@0 286 /* Wait for client jobs to exit */
michael@0 287 PR_EnterMonitor(mon2);
michael@0 288 while (0 != connections) {
michael@0 289 PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
michael@0 290 DPRINTF(("Client job count = %d\n", connections));
michael@0 291 }
michael@0 292 PR_ExitMonitor(mon2);
michael@0 293 printf("%30s","TCP_Socket_Client_Server_Test:");
michael@0 294 printf("%2ld Server %2ld Clients %2ld connections_per_client\n",1l,
michael@0 295 num_tcp_clients, num_tcp_connections_per_client);
michael@0 296 printf("%30s %2ld messages_per_connection %4ld bytes_per_message\n",":",
michael@0 297 num_tcp_mesgs_per_connection, tcp_mesg_size);
michael@0 298
michael@0 299 PR_DELETE(cparamp);
michael@0 300 return 0;
michael@0 301 }
michael@0 302
michael@0 303 /************************************************************************/
michael@0 304
michael@0 305 int main(int argc, char **argv)
michael@0 306 {
michael@0 307 /*
michael@0 308 * -d debug mode
michael@0 309 */
michael@0 310 PLOptStatus os;
michael@0 311 PLOptState *opt;
michael@0 312 program_name = argv[0];
michael@0 313
michael@0 314 opt = PL_CreateOptState(argc, argv, "dp:");
michael@0 315 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 316 {
michael@0 317 if (PL_OPT_BAD == os) continue;
michael@0 318 switch (opt->option)
michael@0 319 {
michael@0 320 case 'd': /* debug mode */
michael@0 321 _debug_on = 1;
michael@0 322 break;
michael@0 323 case 'p':
michael@0 324 server_port = atoi(opt->value);
michael@0 325 break;
michael@0 326 default:
michael@0 327 break;
michael@0 328 }
michael@0 329 }
michael@0 330 PL_DestroyOptState(opt);
michael@0 331
michael@0 332 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
michael@0 333 PR_STDIO_INIT();
michael@0 334
michael@0 335 PR_SetConcurrency(4);
michael@0 336
michael@0 337 TCP_Socket_Client_Server_Test();
michael@0 338
michael@0 339 PR_Cleanup();
michael@0 340 if (failed_already)
michael@0 341 return 1;
michael@0 342 else
michael@0 343 return 0;
michael@0 344 }

mercurial