sel_spd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. * Test the speed of select within NSPR
  7. *
  8. */
  9. #include "nspr.h"
  10. #include "prpriv.h"
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #ifdef DEBUG
  16. #define PORT_INC_DO +100
  17. #else
  18. #define PORT_INC_DO
  19. #endif
  20. #ifdef IS_64
  21. #define PORT_INC_3264 +200
  22. #else
  23. #define PORT_INC_3264
  24. #endif
  25. #define PORT_BASE 19000 PORT_INC_DO PORT_INC_3264
  26. typedef struct timer_slot_t {
  27. unsigned long d_connect;
  28. unsigned long d_cl_data;
  29. unsigned long d_sv_data;
  30. unsigned long d_close;
  31. unsigned long d_total;
  32. unsigned long requests;
  33. } timer_slot_t;
  34. static long _iterations = 5;
  35. static long _client_data = 8192;
  36. static long _server_data = (128*1024);
  37. static long _threads_max = 10, _threads = 10;
  38. static int verbose=0;
  39. static PRMonitor *exit_cv;
  40. static long _thread_exit_count;
  41. static timer_slot_t *timer_data;
  42. static PRThreadScope scope1, scope2;
  43. void tally_results(int);
  44. /* return the diff in microseconds */
  45. unsigned long _delta(PRIntervalTime *start, PRIntervalTime *stop)
  46. {
  47. /*
  48. * Will C do the right thing with unsigned arithemtic?
  49. */
  50. return PR_IntervalToMicroseconds(*stop - *start);
  51. }
  52. int _readn(PRFileDesc *sock, char *buf, int len)
  53. {
  54. int rem;
  55. int bytes;
  56. for (rem=len; rem; rem -= bytes) {
  57. bytes = PR_Recv(sock, buf+len-rem, rem, 0, PR_INTERVAL_NO_TIMEOUT);
  58. if (bytes <= 0) {
  59. return -1;
  60. }
  61. }
  62. return len;
  63. }
  64. void
  65. _thread_exit(int id)
  66. {
  67. PR_EnterMonitor(exit_cv);
  68. #ifdef DEBUG
  69. fprintf(stdout, "Thread %d EXIT\n", id);
  70. #endif
  71. _thread_exit_count--;
  72. if (_thread_exit_count == 0) {
  73. #ifdef DEBUG
  74. fprintf(stdout, "Thread %d EXIT triggered notify\n", id);
  75. #endif
  76. PR_Notify(exit_cv);
  77. }
  78. PR_ExitMonitor(exit_cv);
  79. }
  80. void
  81. _server_thread(void *arg_id)
  82. {
  83. void _client_thread(void *);
  84. int *id = (int *)arg_id;
  85. PRFileDesc *sock;
  86. PRSocketOptionData sockopt;
  87. PRNetAddr sa;
  88. PRFileDesc * newsock;
  89. char *data_buffer = NULL;
  90. int data_buffer_size;
  91. int index;
  92. PRIntervalTime start,
  93. connect_done,
  94. read_done,
  95. write_done,
  96. close_done;
  97. #ifdef DEBUG
  98. fprintf(stdout, "server thread %d alive\n", *id);
  99. #endif
  100. data_buffer_size = (_client_data>_server_data?_client_data:_server_data);
  101. if ( (data_buffer = (char *)PR_Malloc(data_buffer_size * sizeof(char))) == NULL ) {
  102. fprintf(stderr, "Error creating buffer in server thread %d\n", *id);
  103. goto done;
  104. }
  105. if ( (sock = PR_NewTCPSocket()) == NULL) {
  106. fprintf(stderr, "Error creating socket in server thread %d\n", *id);
  107. goto done;
  108. }
  109. sockopt.option = PR_SockOpt_Reuseaddr;
  110. sockopt.value.reuse_addr = PR_TRUE;
  111. if ( PR_SetSocketOption(sock, &sockopt) == PR_FAILURE) {
  112. fprintf(stderr, "Error setting socket option in server thread %d\n", *id);
  113. goto done;
  114. }
  115. memset(&sa, 0, sizeof(sa));
  116. sa.inet.family = PR_AF_INET;
  117. sa.inet.port = PR_htons(PORT_BASE + *id);
  118. sa.inet.ip = PR_htonl(PR_INADDR_ANY);
  119. if ( PR_Bind(sock, &sa) < 0) {
  120. fprintf(stderr, "Error binding socket in server thread %d errno = %d\n", *id, errno);
  121. goto done;
  122. }
  123. if ( PR_Listen(sock, 32) < 0 ) {
  124. fprintf(stderr, "Error listening to socket in server thread %d\n", *id);
  125. goto done;
  126. }
  127. /* Tell the client to start */
  128. if ( PR_CreateThread(PR_USER_THREAD,
  129. _client_thread,
  130. id,
  131. PR_PRIORITY_NORMAL,
  132. scope2,
  133. PR_UNJOINABLE_THREAD,
  134. 0) == NULL) {
  135. fprintf(stderr, "Error creating client thread %d\n", *id);
  136. }
  137. for (index = 0; index< _iterations; index++) {
  138. #ifdef DEBUG
  139. fprintf(stdout, "server thread %d loop %d\n", *id, index);
  140. #endif
  141. start = PR_IntervalNow();
  142. if ( (newsock = PR_Accept(sock, &sa,
  143. PR_INTERVAL_NO_TIMEOUT)) == NULL) {
  144. fprintf(stderr, "Error accepting connection %d in server thread %d\n",
  145. index, *id);
  146. goto done;
  147. }
  148. #ifdef DEBUG
  149. fprintf(stdout, "server thread %d got connection %d\n", *id, newsock);
  150. #endif
  151. connect_done = PR_IntervalNow();
  152. if ( _readn(newsock, data_buffer, _client_data) < _client_data) {
  153. fprintf(stderr, "Error reading client data for iteration %d in server thread %d\n", index, *id );
  154. goto done;
  155. }
  156. #ifdef DEBUG
  157. fprintf(stdout, "server thread %d read %d bytes\n", *id, _client_data);
  158. #endif
  159. read_done = PR_IntervalNow();
  160. if ( PR_Send(newsock, data_buffer, _server_data, 0,
  161. PR_INTERVAL_NO_TIMEOUT) < _server_data) {
  162. fprintf(stderr, "Error sending client data for iteration %d in server thread %d\n", index, *id );
  163. goto done;
  164. }
  165. #ifdef DEBUG
  166. fprintf(stdout, "server thread %d write %d bytes\n", *id, _server_data);
  167. #endif
  168. write_done = PR_IntervalNow();
  169. PR_Close(newsock);
  170. close_done = PR_IntervalNow();
  171. timer_data[2*(*id)].d_connect += _delta(&start, &connect_done);
  172. timer_data[2*(*id)].d_cl_data += _delta(&connect_done, &read_done);
  173. timer_data[2*(*id)].d_sv_data += _delta(&read_done, &write_done);
  174. timer_data[2*(*id)].d_close += _delta(&write_done, &close_done);
  175. timer_data[2*(*id)].d_total += _delta(&start, &close_done);
  176. timer_data[2*(*id)].requests++;
  177. #ifdef DEBUG
  178. fprintf(stdout, "server: %d %d %d %d %d\n",
  179. _delta(&start, &connect_done), _delta(&connect_done, &read_done),
  180. _delta(&read_done, &write_done), _delta(&write_done, &close_done),
  181. _delta(&start, &close_done));
  182. #endif
  183. }
  184. done:
  185. if (data_buffer != NULL) {
  186. PR_Free (data_buffer);
  187. }
  188. if (sock) {
  189. PR_Close(sock);
  190. }
  191. _thread_exit(*id);
  192. return;
  193. }
  194. void
  195. _client_thread(void *arg_id)
  196. {
  197. int *id = (int *)arg_id;
  198. int index;
  199. PRNetAddr sa;
  200. PRFileDesc *sock_h;
  201. char *data_buffer = NULL;
  202. int data_buffer_size;
  203. int bytes;
  204. PRIntervalTime start,
  205. connect_done,
  206. read_done,
  207. write_done,
  208. close_done;
  209. PRStatus rv;
  210. #ifdef DEBUG
  211. fprintf(stdout, "client thread %d alive\n", *id);
  212. #endif
  213. data_buffer_size = (_client_data>_server_data?_client_data:_server_data);
  214. if ( (data_buffer = (char *)PR_Malloc(data_buffer_size * sizeof(char))) == NULL) {
  215. fprintf(stderr, "Error creating buffer in server thread %d\n", *id);
  216. goto done;
  217. }
  218. memset(&sa, 0, sizeof(sa));
  219. rv = PR_InitializeNetAddr(PR_IpAddrLoopback, PORT_BASE + *id, &sa);
  220. PR_ASSERT(PR_SUCCESS == rv);
  221. for (index = 0; index< _iterations; index++) {
  222. #ifdef DEBUG
  223. fprintf(stdout, "client thread %d loop %d\n", *id, index);
  224. #endif
  225. start = PR_IntervalNow();
  226. if ( (sock_h = PR_NewTCPSocket()) == NULL) {
  227. fprintf(stderr, "Error creating socket %d in client thread %d\n",
  228. index, *id);
  229. goto done;
  230. }
  231. #ifdef DEBUG
  232. fprintf(stdout, "client thread %d socket created %d\n", *id, sock_h);
  233. #endif
  234. if ( PR_Connect(sock_h, &sa,
  235. PR_INTERVAL_NO_TIMEOUT) < 0) {
  236. fprintf(stderr, "Error accepting connection %d in client thread %d\n",
  237. index, *id);
  238. goto done;
  239. }
  240. #ifdef DEBUG
  241. fprintf(stdout, "client thread %d socket connected %d\n", *id, sock_h);
  242. #endif
  243. connect_done = PR_IntervalNow();
  244. if ( PR_Send(sock_h, data_buffer, _client_data, 0,
  245. PR_INTERVAL_NO_TIMEOUT) < _client_data) {
  246. fprintf(stderr, "Error sending client data for iteration %d in client thread %d\n", index, *id );
  247. goto done;
  248. }
  249. #ifdef DEBUG
  250. fprintf(stdout, "client thread %d socket wrote %d\n", *id, _client_data);
  251. #endif
  252. write_done = PR_IntervalNow();
  253. if ( (bytes = _readn(sock_h, data_buffer, _server_data)) < _server_data) {
  254. fprintf(stderr, "Error reading server data for iteration %d in client thread %d (read %d bytes)\n", index, *id, bytes );
  255. goto done;
  256. }
  257. #ifdef DEBUG
  258. fprintf(stdout, "client thread %d socket read %d\n", *id, _server_data);
  259. #endif
  260. read_done = PR_IntervalNow();
  261. PR_Close(sock_h);
  262. close_done = PR_IntervalNow();
  263. timer_data[2*(*id)+1].d_connect += _delta(&start, &connect_done);
  264. timer_data[2*(*id)+1].d_cl_data += _delta(&connect_done, &write_done);
  265. timer_data[2*(*id)+1].d_sv_data += _delta(&write_done, &read_done);
  266. timer_data[2*(*id)+1].d_close += _delta(&read_done, &close_done);
  267. timer_data[2*(*id)+1].d_total += _delta(&start, &close_done);
  268. timer_data[2*(*id)+1].requests++;
  269. }
  270. done:
  271. if (data_buffer != NULL) {
  272. PR_Free (data_buffer);
  273. }
  274. _thread_exit(*id);
  275. return;
  276. }
  277. static
  278. void do_work(void)
  279. {
  280. int index;
  281. _thread_exit_count = _threads * 2;
  282. for (index=0; index<_threads; index++) {
  283. int *id = (int *)PR_Malloc(sizeof(int));
  284. *id = index;
  285. if ( PR_CreateThread(PR_USER_THREAD,
  286. _server_thread,
  287. id,
  288. PR_PRIORITY_NORMAL,
  289. scope1,
  290. PR_UNJOINABLE_THREAD,
  291. 0) == NULL) {
  292. fprintf(stderr, "Error creating server thread %d\n", index);
  293. }
  294. }
  295. PR_EnterMonitor(exit_cv);
  296. while (_thread_exit_count > 0) {
  297. PR_Wait(exit_cv, PR_INTERVAL_NO_TIMEOUT);
  298. }
  299. PR_ExitMonitor(exit_cv);
  300. fprintf(stdout, "TEST COMPLETE!\n");
  301. tally_results(verbose);
  302. }
  303. static void do_workUU(void)
  304. {
  305. scope1 = PR_LOCAL_THREAD;
  306. scope2 = PR_LOCAL_THREAD;
  307. do_work();
  308. }
  309. static void do_workUK(void)
  310. {
  311. scope1 = PR_LOCAL_THREAD;
  312. scope2 = PR_GLOBAL_THREAD;
  313. do_work();
  314. }
  315. static void do_workKU(void)
  316. {
  317. scope1 = PR_GLOBAL_THREAD;
  318. scope2 = PR_LOCAL_THREAD;
  319. do_work();
  320. }
  321. static void do_workKK(void)
  322. {
  323. scope1 = PR_GLOBAL_THREAD;
  324. scope2 = PR_GLOBAL_THREAD;
  325. do_work();
  326. }
  327. static void Measure(void (*func)(void), const char *msg)
  328. {
  329. PRIntervalTime start, stop;
  330. double d;
  331. start = PR_IntervalNow();
  332. (*func)();
  333. stop = PR_IntervalNow();
  334. d = (double)PR_IntervalToMicroseconds(stop - start);
  335. printf("%40s: %6.2f usec\n", msg, d / _iterations);
  336. }
  337. int main(int argc, char **argv)
  338. {
  339. #if defined(XP_UNIX) || defined(XP_OS2)
  340. int opt;
  341. PR_IMPORT_DATA(char *) optarg;
  342. #endif
  343. #if defined(XP_UNIX) || defined(XP_OS2)
  344. while ( (opt = getopt(argc, argv, "c:s:i:t:v")) != EOF) {
  345. switch(opt) {
  346. case 'i':
  347. _iterations = atoi(optarg);
  348. break;
  349. case 't':
  350. _threads_max = _threads = atoi(optarg);
  351. break;
  352. case 'c':
  353. _client_data = atoi(optarg);
  354. break;
  355. case 's':
  356. _server_data = atoi(optarg);
  357. break;
  358. case 'v':
  359. verbose = 1;
  360. break;
  361. default:
  362. break;
  363. }
  364. }
  365. #endif
  366. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  367. PR_STDIO_INIT();
  368. fprintf(stdout, "Running test for %d iterations with %d simultaneous threads.\n",
  369. _iterations, _threads);
  370. fprintf(stdout, "\tWill send %d bytes of client data and %d bytes of server data\n",
  371. _client_data, _server_data);
  372. if ( (exit_cv = PR_NewMonitor()) == NULL) {
  373. fprintf(stderr, "Error creating monitor for exit cv\n");
  374. }
  375. if ( (timer_data = (timer_slot_t *)PR_Malloc(2*_threads * sizeof(timer_slot_t))) == NULL) {
  376. fprintf(stderr, "error allocating thread time results array\n");
  377. }
  378. memset(timer_data, 0, 2*_threads*sizeof(timer_slot_t));
  379. Measure(do_workUU, "select loop user/user");
  380. Measure(do_workUK, "select loop user/kernel");
  381. Measure(do_workKU, "select loop kernel/user");
  382. Measure(do_workKK, "select loop kernel/kernel");
  383. return 0;
  384. }
  385. void
  386. tally_results(int verbose)
  387. {
  388. int index;
  389. unsigned long tot_connect = 0;
  390. unsigned long tot_cl_data = 0;
  391. unsigned long tot_sv_data = 0;
  392. unsigned long tot_close = 0;
  393. unsigned long tot_all = 0;
  394. unsigned long tot_requests = 0;
  395. fprintf(stdout, "Server results:\n\n");
  396. for (index=0; index<_threads_max*2; index+=2) {
  397. if (verbose)
  398. fprintf(stdout, "server thread %u\t%u\t%u\t%u\t%u\t%u\t%u\n",
  399. index, timer_data[index].requests, timer_data[index].d_connect,
  400. timer_data[index].d_cl_data, timer_data[index].d_sv_data,
  401. timer_data[index].d_close, timer_data[index].d_total);
  402. tot_connect += timer_data[index].d_connect / _threads;
  403. tot_cl_data += timer_data[index].d_cl_data / _threads;
  404. tot_sv_data += timer_data[index].d_sv_data / _threads;
  405. tot_close += timer_data[index].d_close / _threads;
  406. tot_all += timer_data[index].d_total / _threads;
  407. tot_requests += timer_data[index].requests / _threads;
  408. }
  409. fprintf(stdout, "----------\n");
  410. fprintf(stdout, "server per thread totals %u\t%u\t%u\t%u\t%u\n",
  411. tot_requests, tot_connect, tot_cl_data, tot_sv_data, tot_close);
  412. fprintf(stdout, "server per thread elapsed time %u\n", tot_all);
  413. fprintf(stdout, "----------\n");
  414. tot_connect = tot_cl_data = tot_sv_data = tot_close = tot_all = tot_requests = 0;
  415. fprintf(stdout, "Client results:\n\n");
  416. for (index=1; index<_threads_max*2; index+=2) {
  417. if (verbose)
  418. fprintf(stdout, "client thread %u\t%u\t%u\t%u\t%u\t%u\t%u\n",
  419. index, timer_data[index].requests, timer_data[index].d_connect,
  420. timer_data[index].d_cl_data, timer_data[index].d_sv_data,
  421. timer_data[index].d_close, timer_data[index].d_total);
  422. tot_connect += timer_data[index].d_connect / _threads;
  423. tot_cl_data += timer_data[index].d_cl_data / _threads;
  424. tot_sv_data += timer_data[index].d_sv_data / _threads;
  425. tot_close += timer_data[index].d_close / _threads;
  426. tot_all += timer_data[index].d_total / _threads;
  427. tot_requests += timer_data[index].requests / _threads;
  428. }
  429. fprintf(stdout, "----------\n");
  430. fprintf(stdout, "client per thread totals %u\t%u\t%u\t%u\t%u\n",
  431. tot_requests, tot_connect, tot_cl_data, tot_sv_data, tot_close);
  432. fprintf(stdout, "client per thread elapsed time %u\n", tot_all);
  433. }