servr_kk.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. failed_already=1;
  209. }
  210. return NULL;
  211. }
  212. sockOpt.option = PR_SockOpt_Reuseaddr;
  213. sockOpt.value.reuse_addr = PR_TRUE;
  214. if ( PR_SetSocketOption(listenSocket, &sockOpt) == PR_FAILURE) {
  215. if (debug_mode) printf("\tServer error setting socket option: OS error %d\n",
  216. PR_GetOSError());
  217. else {
  218. failed_already=1;
  219. }
  220. PR_Close(listenSocket);
  221. return NULL;
  222. }
  223. memset(&serverAddr, 0, sizeof(PRNetAddr));
  224. serverAddr.inet.family = PR_AF_INET;
  225. serverAddr.inet.port = PR_htons(PORT);
  226. serverAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
  227. if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
  228. if (debug_mode) printf("\tServer error binding to server address: OS error %d\n",
  229. PR_GetOSError());
  230. else {
  231. failed_already=1;
  232. }
  233. PR_Close(listenSocket);
  234. return NULL;
  235. }
  236. if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
  237. if (debug_mode) {
  238. printf("\tServer error listening to server socket\n");
  239. }
  240. else {
  241. failed_already=1;
  242. }
  243. PR_Close(listenSocket);
  244. return NULL;
  245. }
  246. /* Create Clients */
  247. workerThreads = 0;
  248. workerThreadsBusy = 0;
  249. workerThreadsLock = PR_NewLock();
  250. WorkerThread = PR_CreateThread(
  251. PR_SYSTEM_THREAD,
  252. WorkerThreadFunc,
  253. listenSocket,
  254. PR_PRIORITY_NORMAL,
  255. ServerScope,
  256. PR_UNJOINABLE_THREAD,
  257. THREAD_STACKSIZE);
  258. if (!WorkerThread) {
  259. if (debug_mode) {
  260. printf("error creating working thread\n");
  261. }
  262. PR_Close(listenSocket);
  263. return NULL;
  264. }
  265. PR_AtomicIncrement(&workerThreads);
  266. if (debug_mode) {
  267. DPRINTF("\tServer created primordial worker thread\n");
  268. }
  269. return listenSocket;
  270. }
  271. /* The main server loop */
  272. void
  273. ServerThreadFunc(void *unused)
  274. {
  275. PRFileDesc *listenSocket;
  276. /* Do setup */
  277. listenSocket = ServerSetup();
  278. if (!listenSocket) {
  279. SetServerState(SERVER, SERVER_STATE_DEAD);
  280. } else {
  281. if (debug_mode) {
  282. DPRINTF("\tServer up\n");
  283. }
  284. /* Tell clients they can start now. */
  285. SetServerState(SERVER, SERVER_STATE_READY);
  286. /* Now wait for server death signal */
  287. WaitServerState(SERVER, SERVER_STATE_DYING);
  288. /* Cleanup */
  289. SetServerState(SERVER, SERVER_STATE_DEAD);
  290. }
  291. }
  292. /* --- Client Functions ------------------------------------------- */
  293. PRInt32 numRequests;
  294. PRInt32 numClients;
  295. PRMonitor *clientMonitor;
  296. void
  297. ClientThreadFunc(void *unused)
  298. {
  299. PRNetAddr serverAddr;
  300. PRFileDesc *clientSocket;
  301. char *sendBuf;
  302. char *recvBuf;
  303. PRInt32 rv;
  304. PRInt32 bytesNeeded;
  305. sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char));
  306. if (!sendBuf)
  307. if (debug_mode) {
  308. printf("\tClient could not malloc space!?\n");
  309. }
  310. recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char));
  311. if (!recvBuf)
  312. if (debug_mode) {
  313. printf("\tClient could not malloc space!?\n");
  314. }
  315. memset(&serverAddr, 0, sizeof(PRNetAddr));
  316. serverAddr.inet.family = PR_AF_INET;
  317. serverAddr.inet.port = PR_htons(PORT);
  318. serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
  319. while(numRequests > 0) {
  320. if ( (numRequests % 10) == 0 )
  321. if (debug_mode) {
  322. printf(".");
  323. }
  324. if (debug_mode) {
  325. DPRINTF("\tClient starting request %d\n", numRequests);
  326. }
  327. clientSocket = PR_NewTCPSocket();
  328. if (!clientSocket) {
  329. if (debug_mode) printf("Client error creating socket: OS error %d\n",
  330. PR_GetOSError());
  331. continue;
  332. }
  333. if (debug_mode) {
  334. DPRINTF("\tClient connecting\n");
  335. }
  336. rv = PR_Connect(clientSocket,
  337. &serverAddr,
  338. PR_INTERVAL_NO_TIMEOUT);
  339. if (!clientSocket) {
  340. if (debug_mode) {
  341. printf("\tClient error connecting\n");
  342. }
  343. continue;
  344. }
  345. if (debug_mode) {
  346. DPRINTF("\tClient connected\n");
  347. }
  348. rv = PR_Send(clientSocket,
  349. sendBuf,
  350. _client_data,
  351. 0,
  352. PR_INTERVAL_NO_TIMEOUT);
  353. if (rv != _client_data) {
  354. if (debug_mode) {
  355. printf("Client error sending data (%d)\n", rv);
  356. }
  357. PR_Close(clientSocket);
  358. continue;
  359. }
  360. if (debug_mode) {
  361. DPRINTF("\tClient sent %d bytes\n", rv);
  362. }
  363. bytesNeeded = _server_data;
  364. while(bytesNeeded) {
  365. rv = PR_Recv(clientSocket,
  366. recvBuf,
  367. bytesNeeded,
  368. 0,
  369. PR_INTERVAL_NO_TIMEOUT);
  370. if (rv <= 0) {
  371. if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n",
  372. rv, (_server_data - bytesNeeded), _server_data);
  373. break;
  374. }
  375. if (debug_mode) {
  376. DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv);
  377. }
  378. bytesNeeded -= rv;
  379. }
  380. PR_Close(clientSocket);
  381. PR_AtomicDecrement(&numRequests);
  382. }
  383. PR_EnterMonitor(clientMonitor);
  384. --numClients;
  385. PR_Notify(clientMonitor);
  386. PR_ExitMonitor(clientMonitor);
  387. PR_DELETE(sendBuf);
  388. PR_DELETE(recvBuf);
  389. }
  390. void
  391. RunClients(void)
  392. {
  393. PRInt32 index;
  394. numRequests = _iterations;
  395. numClients = _clients;
  396. clientMonitor = PR_NewMonitor();
  397. for (index=0; index<_clients; index++) {
  398. PRThread *clientThread;
  399. clientThread = PR_CreateThread(
  400. PR_USER_THREAD,
  401. ClientThreadFunc,
  402. NULL,
  403. PR_PRIORITY_NORMAL,
  404. ClientScope,
  405. PR_UNJOINABLE_THREAD,
  406. THREAD_STACKSIZE);
  407. if (!clientThread) {
  408. if (debug_mode) {
  409. printf("\terror creating client thread %d\n", index);
  410. }
  411. } else if (debug_mode) {
  412. DPRINTF("\tMain created client %d/%d\n", index+1, _clients);
  413. }
  414. }
  415. PR_EnterMonitor(clientMonitor);
  416. while(numClients) {
  417. PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT);
  418. }
  419. PR_ExitMonitor(clientMonitor);
  420. }
  421. /* --- Main Function ---------------------------------------------- */
  422. static
  423. void do_work()
  424. {
  425. PRThread *ServerThread;
  426. PRInt32 state;
  427. SetServerState(MAIN, SERVER_STATE_STARTUP);
  428. ServerThread = PR_CreateThread(
  429. PR_USER_THREAD,
  430. ServerThreadFunc,
  431. NULL,
  432. PR_PRIORITY_NORMAL,
  433. ServerScope,
  434. PR_JOINABLE_THREAD,
  435. THREAD_STACKSIZE);
  436. if (!ServerThread) {
  437. if (debug_mode) {
  438. printf("error creating main server thread\n");
  439. }
  440. return;
  441. }
  442. /* Wait for server to be ready */
  443. state = WaitServerState(MAIN, SERVER_STATE_READY|SERVER_STATE_DEAD);
  444. if (!(state & SERVER_STATE_DEAD)) {
  445. /* Run Test Clients */
  446. RunClients();
  447. /* Send death signal to server */
  448. SetServerState(MAIN, SERVER_STATE_DYING);
  449. }
  450. PR_JoinThread(ServerThread);
  451. }
  452. static void do_workUU(void)
  453. {
  454. ServerScope = PR_LOCAL_THREAD;
  455. ClientScope = PR_LOCAL_THREAD;
  456. do_work();
  457. }
  458. static void do_workUK(void)
  459. {
  460. ServerScope = PR_LOCAL_THREAD;
  461. ClientScope = PR_GLOBAL_THREAD;
  462. do_work();
  463. }
  464. static void do_workKU(void)
  465. {
  466. ServerScope = PR_GLOBAL_THREAD;
  467. ClientScope = PR_LOCAL_THREAD;
  468. do_work();
  469. }
  470. static void do_workKK(void)
  471. {
  472. ServerScope = PR_GLOBAL_THREAD;
  473. ClientScope = PR_GLOBAL_THREAD;
  474. do_work();
  475. }
  476. static void Measure(void (*func)(void), const char *msg)
  477. {
  478. PRIntervalTime start, stop;
  479. double d;
  480. start = PR_IntervalNow();
  481. (*func)();
  482. stop = PR_IntervalNow();
  483. d = (double)PR_IntervalToMicroseconds(stop - start);
  484. if (debug_mode) {
  485. printf("\n%40s: %6.2f usec\n", msg, d / _iterations);
  486. }
  487. }
  488. int main(int argc, char **argv)
  489. {
  490. /* The command line argument: -d is used to determine if the test is being run
  491. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  492. All of the printfs associated with this test has been handled with a if (debug_mode)
  493. test.
  494. Usage: test_name -d
  495. */
  496. PLOptStatus os;
  497. PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  498. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  499. {
  500. if (PL_OPT_BAD == os) {
  501. continue;
  502. }
  503. switch (opt->option)
  504. {
  505. case 'd': /* debug mode */
  506. debug_mode = 1;
  507. break;
  508. default:
  509. break;
  510. }
  511. }
  512. PL_DestroyOptState(opt);
  513. /* main test */
  514. if (debug_mode) {
  515. printf("Enter number of iterations: \n");
  516. scanf("%d", &_iterations);
  517. printf("Enter number of clients : \n");
  518. scanf("%d", &_clients);
  519. printf("Enter size of client data : \n");
  520. scanf("%d", &_client_data);
  521. printf("Enter size of server data : \n");
  522. scanf("%d", &_server_data);
  523. }
  524. else
  525. {
  526. _iterations = 7;
  527. _clients = 7;
  528. _client_data = 100;
  529. _server_data = 100;
  530. }
  531. if (debug_mode) {
  532. printf("\n\n%d iterations with %d client threads.\n",
  533. _iterations, _clients);
  534. printf("Sending %d bytes of client data and %d bytes of server data\n",
  535. _client_data, _server_data);
  536. }
  537. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  538. PR_STDIO_INIT();
  539. PR_SetThreadRecycleMode(64);
  540. ServerStateCVLock = PR_NewLock();
  541. ServerStateCV = PR_NewCondVar(ServerStateCVLock);
  542. Measure(do_workKK, "server loop kernel/kernel");
  543. PR_Cleanup();
  544. if(failed_already) {
  545. return 1;
  546. }
  547. else {
  548. return 0;
  549. }
  550. }