servr_uk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. ** This server simulates a server running in loopback mode.
  8. **
  9. ** The idea is that a single server is created. The server initially creates
  10. ** a number of worker threads. Then, with the server running, a number of
  11. ** clients are created which start requesting service from the server.
  12. **
  13. **
  14. ** Modification History:
  15. ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  16. ** The debug mode will print all of the printfs associated with this test.
  17. ** The regress mode will be the default mode. Since the regress tool limits
  18. ** the output to a one line status:PASS or FAIL,all of the printf statements
  19. ** have been handled with an if (debug_mode) statement.
  20. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  21. ** recognize the return code from tha main program.
  22. ***********************************************************************/
  23. /***********************************************************************
  24. ** Includes
  25. ***********************************************************************/
  26. /* Used to get the command line option */
  27. #include "plgetopt.h"
  28. #include "nspr.h"
  29. #include "pprthred.h"
  30. #include <string.h>
  31. #define PORT 15004
  32. #define THREAD_STACKSIZE 0
  33. static int _iterations = 1000;
  34. static int _clients = 1;
  35. static int _client_data = 250;
  36. static int _server_data = (8*1024);
  37. static PRThreadScope ServerScope, ClientScope;
  38. #define SERVER "Server"
  39. #define MAIN "Main"
  40. #define SERVER_STATE_STARTUP 0
  41. #define SERVER_STATE_READY 1
  42. #define SERVER_STATE_DYING 2
  43. #define SERVER_STATE_DEAD 4
  44. int ServerState;
  45. PRLock *ServerStateCVLock;
  46. PRCondVar *ServerStateCV;
  47. #ifdef DEBUGPRINTS
  48. #define DPRINTF printf
  49. #else
  50. #define DPRINTF
  51. #endif
  52. PRIntn failed_already=0;
  53. PRIntn debug_mode;
  54. static void do_work(void);
  55. /* --- Server state functions --------------------------------------------- */
  56. void
  57. SetServerState(char *waiter, PRInt32 state)
  58. {
  59. PR_Lock(ServerStateCVLock);
  60. ServerState = state;
  61. PR_NotifyCondVar(ServerStateCV);
  62. if (debug_mode) {
  63. DPRINTF("\t%s changed state to %d\n", waiter, state);
  64. }
  65. PR_Unlock(ServerStateCVLock);
  66. }
  67. int
  68. WaitServerState(char *waiter, PRInt32 state)
  69. {
  70. PRInt32 rv;
  71. PR_Lock(ServerStateCVLock);
  72. if (debug_mode) {
  73. DPRINTF("\t%s waiting for state %d\n", waiter, state);
  74. }
  75. while(!(ServerState & state)) {
  76. PR_WaitCondVar(ServerStateCV, PR_INTERVAL_NO_TIMEOUT);
  77. }
  78. rv = ServerState;
  79. if (debug_mode) DPRINTF("\t%s resuming from wait for state %d; state now %d\n",
  80. waiter, state, ServerState);
  81. PR_Unlock(ServerStateCVLock);
  82. return rv;
  83. }
  84. /* --- Server Functions ------------------------------------------- */
  85. PRLock *workerThreadsLock;
  86. PRInt32 workerThreads;
  87. PRInt32 workerThreadsBusy;
  88. void
  89. WorkerThreadFunc(void *_listenSock)
  90. {
  91. PRFileDesc *listenSock = (PRFileDesc *)_listenSock;
  92. PRInt32 bytesRead;
  93. PRInt32 bytesWritten;
  94. char *dataBuf;
  95. char *sendBuf;
  96. if (debug_mode) DPRINTF("\tServer buffer is %d bytes; %d data, %d netaddrs\n",
  97. _client_data+(2*sizeof(PRNetAddr))+32, _client_data, (2*sizeof(PRNetAddr))+32);
  98. dataBuf = (char *)PR_MALLOC(_client_data + 2*sizeof(PRNetAddr) + 32);
  99. if (!dataBuf)
  100. if (debug_mode) {
  101. printf("\tServer could not malloc space!?\n");
  102. }
  103. sendBuf = (char *)PR_MALLOC(_server_data *sizeof(char));
  104. if (!sendBuf)
  105. if (debug_mode) {
  106. printf("\tServer could not malloc space!?\n");
  107. }
  108. if (debug_mode) {
  109. DPRINTF("\tServer worker thread running\n");
  110. }
  111. while(1) {
  112. PRInt32 bytesToRead = _client_data;
  113. PRInt32 bytesToWrite = _server_data;
  114. PRFileDesc *newSock;
  115. PRNetAddr *rAddr;
  116. PRInt32 loops = 0;
  117. loops++;
  118. if (debug_mode) {
  119. DPRINTF("\tServer thread going into accept\n");
  120. }
  121. bytesRead = PR_AcceptRead(listenSock,
  122. &newSock,
  123. &rAddr,
  124. dataBuf,
  125. bytesToRead,
  126. PR_INTERVAL_NO_TIMEOUT);
  127. if (bytesRead < 0) {
  128. if (debug_mode) {
  129. printf("\tServer error in accept (%d)\n", bytesRead);
  130. }
  131. continue;
  132. }
  133. if (debug_mode) {
  134. DPRINTF("\tServer accepted connection (%d bytes)\n", bytesRead);
  135. }
  136. PR_AtomicIncrement(&workerThreadsBusy);
  137. if (workerThreadsBusy == workerThreads) {
  138. PR_Lock(workerThreadsLock);
  139. if (workerThreadsBusy == workerThreads) {
  140. PRThread *WorkerThread;
  141. WorkerThread = PR_CreateThread(
  142. PR_SYSTEM_THREAD,
  143. WorkerThreadFunc,
  144. listenSock,
  145. PR_PRIORITY_NORMAL,
  146. ServerScope,
  147. PR_UNJOINABLE_THREAD,
  148. THREAD_STACKSIZE);
  149. if (!WorkerThread) {
  150. if (debug_mode) {
  151. printf("Error creating client thread %d\n", workerThreads);
  152. }
  153. } else {
  154. PR_AtomicIncrement(&workerThreads);
  155. if (debug_mode) {
  156. DPRINTF("\tServer creates worker (%d)\n", workerThreads);
  157. }
  158. }
  159. }
  160. PR_Unlock(workerThreadsLock);
  161. }
  162. bytesToRead -= bytesRead;
  163. while (bytesToRead) {
  164. bytesRead = PR_Recv(newSock,
  165. dataBuf,
  166. bytesToRead,
  167. 0,
  168. PR_INTERVAL_NO_TIMEOUT);
  169. if (bytesRead < 0) {
  170. if (debug_mode) {
  171. printf("\tServer error receiving data (%d)\n", bytesRead);
  172. }
  173. continue;
  174. }
  175. if (debug_mode) {
  176. DPRINTF("\tServer received %d bytes\n", bytesRead);
  177. }
  178. }
  179. bytesWritten = PR_Send(newSock,
  180. sendBuf,
  181. bytesToWrite,
  182. 0,
  183. PR_INTERVAL_NO_TIMEOUT);
  184. if (bytesWritten != _server_data) {
  185. if (debug_mode) printf("\tError sending data to client (%d, %d)\n",
  186. bytesWritten, PR_GetOSError());
  187. } else {
  188. if (debug_mode) {
  189. DPRINTF("\tServer sent %d bytes\n", bytesWritten);
  190. }
  191. }
  192. PR_Close(newSock);
  193. PR_AtomicDecrement(&workerThreadsBusy);
  194. }
  195. }
  196. PRFileDesc *
  197. ServerSetup(void)
  198. {
  199. PRFileDesc *listenSocket;
  200. PRSocketOptionData sockOpt;
  201. PRNetAddr serverAddr;
  202. PRThread *WorkerThread;
  203. if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
  204. if (debug_mode) {
  205. printf("\tServer error creating listen socket\n");
  206. }
  207. else {
  208. return NULL;
  209. }
  210. }
  211. sockOpt.option = PR_SockOpt_Reuseaddr;
  212. sockOpt.value.reuse_addr = PR_TRUE;
  213. if ( PR_SetSocketOption(listenSocket, &sockOpt) == PR_FAILURE) {
  214. if (debug_mode) printf("\tServer error setting socket option: OS error %d\n",
  215. PR_GetOSError());
  216. else {
  217. failed_already=1;
  218. }
  219. PR_Close(listenSocket);
  220. return NULL;
  221. }
  222. memset(&serverAddr, 0, sizeof(PRNetAddr));
  223. serverAddr.inet.family = PR_AF_INET;
  224. serverAddr.inet.port = PR_htons(PORT);
  225. serverAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
  226. if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
  227. if (debug_mode) printf("\tServer error binding to server address: OS error %d\n",
  228. PR_GetOSError());
  229. else {
  230. failed_already=1;
  231. }
  232. PR_Close(listenSocket);
  233. return NULL;
  234. }
  235. if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
  236. if (debug_mode) {
  237. printf("\tServer error listening to server socket\n");
  238. }
  239. else {
  240. failed_already=1;
  241. }
  242. PR_Close(listenSocket);
  243. return NULL;
  244. }
  245. /* Create Clients */
  246. workerThreads = 0;
  247. workerThreadsBusy = 0;
  248. workerThreadsLock = PR_NewLock();
  249. WorkerThread = PR_CreateThread(
  250. PR_SYSTEM_THREAD,
  251. WorkerThreadFunc,
  252. listenSocket,
  253. PR_PRIORITY_NORMAL,
  254. ServerScope,
  255. PR_UNJOINABLE_THREAD,
  256. THREAD_STACKSIZE);
  257. if (!WorkerThread) {
  258. if (debug_mode) {
  259. printf("error creating working thread\n");
  260. }
  261. PR_Close(listenSocket);
  262. return NULL;
  263. }
  264. PR_AtomicIncrement(&workerThreads);
  265. if (debug_mode) {
  266. DPRINTF("\tServer created primordial worker thread\n");
  267. }
  268. return listenSocket;
  269. }
  270. /* The main server loop */
  271. void
  272. ServerThreadFunc(void *unused)
  273. {
  274. PRFileDesc *listenSocket;
  275. /* Do setup */
  276. listenSocket = ServerSetup();
  277. if (!listenSocket) {
  278. SetServerState(SERVER, SERVER_STATE_DEAD);
  279. } else {
  280. if (debug_mode) {
  281. DPRINTF("\tServer up\n");
  282. }
  283. /* Tell clients they can start now. */
  284. SetServerState(SERVER, SERVER_STATE_READY);
  285. /* Now wait for server death signal */
  286. WaitServerState(SERVER, SERVER_STATE_DYING);
  287. /* Cleanup */
  288. SetServerState(SERVER, SERVER_STATE_DEAD);
  289. }
  290. }
  291. /* --- Client Functions ------------------------------------------- */
  292. PRInt32 numRequests;
  293. PRInt32 numClients;
  294. PRMonitor *clientMonitor;
  295. void
  296. ClientThreadFunc(void *unused)
  297. {
  298. PRNetAddr serverAddr;
  299. PRFileDesc *clientSocket;
  300. char *sendBuf;
  301. char *recvBuf;
  302. PRInt32 rv;
  303. PRInt32 bytesNeeded;
  304. sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char));
  305. if (!sendBuf)
  306. if (debug_mode) {
  307. printf("\tClient could not malloc space!?\n");
  308. }
  309. recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char));
  310. if (!recvBuf)
  311. if (debug_mode) {
  312. printf("\tClient could not malloc space!?\n");
  313. }
  314. memset(&serverAddr, 0, sizeof(PRNetAddr));
  315. serverAddr.inet.family = PR_AF_INET;
  316. serverAddr.inet.port = PR_htons(PORT);
  317. serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
  318. while(numRequests > 0) {
  319. if ( (numRequests % 10) == 0 )
  320. if (debug_mode) {
  321. printf(".");
  322. }
  323. if (debug_mode) {
  324. DPRINTF("\tClient starting request %d\n", numRequests);
  325. }
  326. clientSocket = PR_NewTCPSocket();
  327. if (!clientSocket) {
  328. if (debug_mode) printf("Client error creating socket: OS error %d\n",
  329. PR_GetOSError());
  330. continue;
  331. }
  332. if (debug_mode) {
  333. DPRINTF("\tClient connecting\n");
  334. }
  335. rv = PR_Connect(clientSocket,
  336. &serverAddr,
  337. PR_INTERVAL_NO_TIMEOUT);
  338. if (!clientSocket) {
  339. if (debug_mode) {
  340. printf("\tClient error connecting\n");
  341. }
  342. continue;
  343. }
  344. if (debug_mode) {
  345. DPRINTF("\tClient connected\n");
  346. }
  347. rv = PR_Send(clientSocket,
  348. sendBuf,
  349. _client_data,
  350. 0,
  351. PR_INTERVAL_NO_TIMEOUT);
  352. if (rv != _client_data) {
  353. if (debug_mode) {
  354. printf("Client error sending data (%d)\n", rv);
  355. }
  356. PR_Close(clientSocket);
  357. continue;
  358. }
  359. if (debug_mode) {
  360. DPRINTF("\tClient sent %d bytes\n", rv);
  361. }
  362. bytesNeeded = _server_data;
  363. while(bytesNeeded) {
  364. rv = PR_Recv(clientSocket,
  365. recvBuf,
  366. bytesNeeded,
  367. 0,
  368. PR_INTERVAL_NO_TIMEOUT);
  369. if (rv <= 0) {
  370. if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n",
  371. rv, (_server_data - bytesNeeded), _server_data);
  372. break;
  373. }
  374. if (debug_mode) {
  375. DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv);
  376. }
  377. bytesNeeded -= rv;
  378. }
  379. PR_Close(clientSocket);
  380. PR_AtomicDecrement(&numRequests);
  381. }
  382. PR_EnterMonitor(clientMonitor);
  383. --numClients;
  384. PR_Notify(clientMonitor);
  385. PR_ExitMonitor(clientMonitor);
  386. PR_DELETE(sendBuf);
  387. PR_DELETE(recvBuf);
  388. }
  389. void
  390. RunClients(void)
  391. {
  392. PRInt32 index;
  393. numRequests = _iterations;
  394. numClients = _clients;
  395. clientMonitor = PR_NewMonitor();
  396. for (index=0; index<_clients; index++) {
  397. PRThread *clientThread;
  398. clientThread = PR_CreateThread(
  399. PR_USER_THREAD,
  400. ClientThreadFunc,
  401. NULL,
  402. PR_PRIORITY_NORMAL,
  403. ClientScope,
  404. PR_UNJOINABLE_THREAD,
  405. THREAD_STACKSIZE);
  406. if (!clientThread) {
  407. if (debug_mode) {
  408. printf("\terror creating client thread %d\n", index);
  409. }
  410. } else if (debug_mode) {
  411. DPRINTF("\tMain created client %d/%d\n", index+1, _clients);
  412. }
  413. }
  414. PR_EnterMonitor(clientMonitor);
  415. while(numClients) {
  416. PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT);
  417. }
  418. PR_ExitMonitor(clientMonitor);
  419. }
  420. /* --- Main Function ---------------------------------------------- */
  421. static
  422. void do_work()
  423. {
  424. PRThread *ServerThread;
  425. PRInt32 state;
  426. SetServerState(MAIN, SERVER_STATE_STARTUP);
  427. ServerThread = PR_CreateThread(
  428. PR_USER_THREAD,
  429. ServerThreadFunc,
  430. NULL,
  431. PR_PRIORITY_NORMAL,
  432. ServerScope,
  433. PR_JOINABLE_THREAD,
  434. THREAD_STACKSIZE);
  435. if (!ServerThread) {
  436. if (debug_mode) {
  437. printf("error creating main server thread\n");
  438. }
  439. return;
  440. }
  441. /* Wait for server to be ready */
  442. state = WaitServerState(MAIN, SERVER_STATE_READY|SERVER_STATE_DEAD);
  443. if (!(state & SERVER_STATE_DEAD)) {
  444. /* Run Test Clients */
  445. RunClients();
  446. /* Send death signal to server */
  447. SetServerState(MAIN, SERVER_STATE_DYING);
  448. }
  449. PR_JoinThread(ServerThread);
  450. }
  451. static void do_workUK(void)
  452. {
  453. ServerScope = PR_LOCAL_THREAD;
  454. ClientScope = PR_GLOBAL_THREAD;
  455. do_work();
  456. }
  457. static void Measure(void (*func)(void), const char *msg)
  458. {
  459. PRIntervalTime start, stop;
  460. double d;
  461. start = PR_IntervalNow();
  462. (*func)();
  463. stop = PR_IntervalNow();
  464. d = (double)PR_IntervalToMicroseconds(stop - start);
  465. if (debug_mode) {
  466. printf("\n%40s: %6.2f usec\n", msg, d / _iterations);
  467. }
  468. }
  469. int main(int argc, char **argv)
  470. {
  471. /* The command line argument: -d is used to determine if the test is being run
  472. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  473. All of the printfs associated with this test has been handled with a if (debug_mode)
  474. test.
  475. Usage: test_name -d
  476. */
  477. PLOptStatus os;
  478. PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  479. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  480. {
  481. if (PL_OPT_BAD == os) {
  482. continue;
  483. }
  484. switch (opt->option)
  485. {
  486. case 'd': /* debug mode */
  487. debug_mode = 1;
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. PL_DestroyOptState(opt);
  494. /* main test */
  495. if (debug_mode) {
  496. printf("Enter number of iterations: \n");
  497. scanf("%d", &_iterations);
  498. printf("Enter number of clients : \n");
  499. scanf("%d", &_clients);
  500. printf("Enter size of client data : \n");
  501. scanf("%d", &_client_data);
  502. printf("Enter size of server data : \n");
  503. scanf("%d", &_server_data);
  504. }
  505. else
  506. {
  507. _iterations = 7;
  508. _clients = 7;
  509. _client_data = 100;
  510. _server_data = 100;
  511. }
  512. if (debug_mode) {
  513. printf("\n\n%d iterations with %d client threads.\n",
  514. _iterations, _clients);
  515. printf("Sending %d bytes of client data and %d bytes of server data\n",
  516. _client_data, _server_data);
  517. }
  518. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  519. PR_STDIO_INIT();
  520. PR_SetThreadRecycleMode(64);
  521. ServerStateCVLock = PR_NewLock();
  522. ServerStateCV = PR_NewCondVar(ServerStateCVLock);
  523. Measure(do_workUK, "server loop user/kernel");
  524. PR_Cleanup();
  525. if(failed_already) {
  526. return 1;
  527. }
  528. else {
  529. return 0;
  530. }
  531. }