thrpool_client.c 9.6 KB

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