michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Test the speed of select within NSPR michael@0: * michael@0: */ michael@0: michael@0: #include "nspr.h" michael@0: #include "prpriv.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef SYMBIAN michael@0: #include michael@0: #endif michael@0: michael@0: #define PORT_BASE 19000 michael@0: michael@0: typedef struct timer_slot_t { michael@0: unsigned long d_connect; michael@0: unsigned long d_cl_data; michael@0: unsigned long d_sv_data; michael@0: unsigned long d_close; michael@0: unsigned long d_total; michael@0: unsigned long requests; michael@0: } timer_slot_t; michael@0: michael@0: static long _iterations = 5; michael@0: static long _client_data = 8192; michael@0: michael@0: #ifdef SYMBIAN michael@0: /* michael@0: * Symbian OS does not scale well specially the requirement for thread stack michael@0: * space and buffer allocation space. It is easy to get into a fragmented michael@0: * memory and not be able to allocate thread stack or client/server data michael@0: * buffer. michael@0: */ michael@0: static long _server_data = (8*1024); michael@0: static long _threads_max = 10, _threads = 10; michael@0: #else michael@0: static long _server_data = (128*1024); michael@0: static long _threads_max = 10, _threads = 10; michael@0: #endif michael@0: michael@0: static int verbose=0; michael@0: static PRMonitor *exit_cv; michael@0: static long _thread_exit_count; michael@0: static timer_slot_t *timer_data; michael@0: static PRThreadScope scope1, scope2; michael@0: michael@0: void tally_results(int); michael@0: michael@0: /* return the diff in microseconds */ michael@0: unsigned long _delta(PRIntervalTime *start, PRIntervalTime *stop) michael@0: { michael@0: /* michael@0: * Will C do the right thing with unsigned arithemtic? michael@0: */ michael@0: return PR_IntervalToMicroseconds(*stop - *start); michael@0: } michael@0: michael@0: int _readn(PRFileDesc *sock, char *buf, int len) michael@0: { michael@0: int rem; michael@0: int bytes; michael@0: michael@0: for (rem=len; rem; rem -= bytes) { michael@0: bytes = PR_Recv(sock, buf+len-rem, rem, 0, PR_INTERVAL_NO_TIMEOUT); michael@0: if (bytes <= 0) michael@0: return -1; michael@0: } michael@0: return len; michael@0: } michael@0: michael@0: void michael@0: _thread_exit(int id) michael@0: { michael@0: PR_EnterMonitor(exit_cv); michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "Thread %d EXIT\n", id); michael@0: #endif michael@0: michael@0: _thread_exit_count--; michael@0: if (_thread_exit_count == 0) { michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "Thread %d EXIT triggered notify\n", id); michael@0: #endif michael@0: PR_Notify(exit_cv); michael@0: } michael@0: PR_ExitMonitor(exit_cv); michael@0: } michael@0: michael@0: void michael@0: _server_thread(void *arg_id) michael@0: { michael@0: void _client_thread(void *); michael@0: PRThread *thread; michael@0: int *id = (int *)arg_id; michael@0: PRFileDesc *sock; michael@0: PRSocketOptionData sockopt; michael@0: PRNetAddr sa; michael@0: PRFileDesc * newsock; michael@0: char *data_buffer = NULL; michael@0: int data_buffer_size; michael@0: int index; michael@0: PRIntervalTime start, michael@0: connect_done, michael@0: read_done, michael@0: write_done, michael@0: close_done; michael@0: michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "server thread %d alive\n", *id); michael@0: #endif michael@0: michael@0: data_buffer_size = (_client_data>_server_data?_client_data:_server_data); michael@0: michael@0: if ( (data_buffer = (char *)PR_Malloc(data_buffer_size * sizeof(char))) == NULL ) { michael@0: fprintf(stderr, "Error creating buffer in server thread %d\n", *id); michael@0: goto done; michael@0: } michael@0: michael@0: michael@0: if ( (sock = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr, "Error creating socket in server thread %d\n", *id); michael@0: goto done; michael@0: } michael@0: michael@0: sockopt.option = PR_SockOpt_Reuseaddr; michael@0: sockopt.value.reuse_addr = PR_TRUE; michael@0: if ( PR_SetSocketOption(sock, &sockopt) == PR_FAILURE) { michael@0: fprintf(stderr, "Error setting socket option in server thread %d\n", *id); michael@0: goto done; michael@0: } michael@0: michael@0: memset(&sa, 0 , sizeof(sa)); michael@0: sa.inet.family = PR_AF_INET; michael@0: sa.inet.port = PR_htons(PORT_BASE + *id); michael@0: sa.inet.ip = PR_htonl(PR_INADDR_ANY); michael@0: michael@0: if ( PR_Bind(sock, &sa) < 0) { michael@0: fprintf(stderr, "Error binding socket in server thread %d errno = %d\n", *id, errno); michael@0: goto done; michael@0: } michael@0: michael@0: if ( PR_Listen(sock, 32) < 0 ) { michael@0: fprintf(stderr, "Error listening to socket in server thread %d\n", *id); michael@0: goto done; michael@0: } michael@0: michael@0: /* Tell the client to start */ michael@0: if ( (thread = PR_CreateThread(PR_USER_THREAD, michael@0: _client_thread, michael@0: id, michael@0: PR_PRIORITY_NORMAL, michael@0: scope2, michael@0: PR_UNJOINABLE_THREAD, michael@0: 0)) == NULL) michael@0: fprintf(stderr, "Error creating client thread %d\n", *id); michael@0: michael@0: for (index = 0; index< _iterations; index++) { michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "server thread %d loop %d\n", *id, index); michael@0: #endif michael@0: michael@0: start = PR_IntervalNow(); michael@0: michael@0: if ( (newsock = PR_Accept(sock, &sa, michael@0: PR_INTERVAL_NO_TIMEOUT)) == NULL) { michael@0: fprintf(stderr, "Error accepting connection %d in server thread %d\n", michael@0: index, *id); michael@0: goto done; michael@0: } michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "server thread %d got connection %d\n", *id, newsock); michael@0: #endif michael@0: michael@0: michael@0: connect_done = PR_IntervalNow(); michael@0: michael@0: if ( _readn(newsock, data_buffer, _client_data) < _client_data) { michael@0: fprintf(stderr, "Error reading client data for iteration %d in server thread %d\n", index, *id ); michael@0: goto done; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "server thread %d read %d bytes\n", *id, _client_data); michael@0: #endif michael@0: read_done = PR_IntervalNow(); michael@0: michael@0: if ( PR_Send(newsock, data_buffer, _server_data, 0, michael@0: PR_INTERVAL_NO_TIMEOUT) < _server_data) { michael@0: fprintf(stderr, "Error sending client data for iteration %d in server thread %d\n", index, *id ); michael@0: goto done; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "server thread %d write %d bytes\n", *id, _server_data); michael@0: #endif michael@0: michael@0: write_done = PR_IntervalNow(); michael@0: michael@0: PR_Close(newsock); michael@0: michael@0: close_done = PR_IntervalNow(); michael@0: michael@0: timer_data[2*(*id)].d_connect += _delta(&start, &connect_done); michael@0: timer_data[2*(*id)].d_cl_data += _delta(&connect_done, &read_done); michael@0: timer_data[2*(*id)].d_sv_data += _delta(&read_done, &write_done); michael@0: timer_data[2*(*id)].d_close += _delta(&write_done, &close_done); michael@0: timer_data[2*(*id)].d_total += _delta(&start, &close_done); michael@0: timer_data[2*(*id)].requests++; michael@0: michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "server: %d %d %d %d %d\n", michael@0: _delta(&start, &connect_done), _delta(&connect_done, &read_done), michael@0: _delta(&read_done, &write_done), _delta(&write_done, &close_done), michael@0: _delta(&start, &close_done)); michael@0: #endif michael@0: } michael@0: michael@0: done: michael@0: if (data_buffer != NULL) PR_Free (data_buffer); michael@0: if (sock) PR_Close(sock); michael@0: _thread_exit(*id); michael@0: return; michael@0: } michael@0: michael@0: void michael@0: _client_thread(void *arg_id) michael@0: { michael@0: int *id = (int *)arg_id; michael@0: int index; michael@0: PRNetAddr sa; michael@0: PRFileDesc *sock_h; michael@0: char *data_buffer = NULL; michael@0: int data_buffer_size; michael@0: int bytes; michael@0: PRIntervalTime start, michael@0: connect_done, michael@0: read_done, michael@0: write_done, michael@0: close_done; michael@0: PRStatus rv; michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "client thread %d alive\n", *id); michael@0: #endif michael@0: michael@0: data_buffer_size = (_client_data>_server_data?_client_data:_server_data); michael@0: michael@0: if ( (data_buffer = (char *)PR_Malloc(data_buffer_size * sizeof(char))) == NULL) { michael@0: fprintf(stderr, "Error creating buffer in server thread %d\n", *id); michael@0: goto done; michael@0: } michael@0: michael@0: memset(&sa, 0 , sizeof(sa)); michael@0: rv = PR_InitializeNetAddr(PR_IpAddrLoopback, PORT_BASE + *id, &sa); michael@0: PR_ASSERT(PR_SUCCESS == rv); michael@0: michael@0: for (index = 0; index< _iterations; index++) { michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "client thread %d loop %d\n", *id, index); michael@0: #endif michael@0: michael@0: start = PR_IntervalNow(); michael@0: if ( (sock_h = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr, "Error creating socket %d in client thread %d\n", michael@0: index, *id); michael@0: goto done; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "client thread %d socket created %d\n", *id, sock_h); michael@0: #endif michael@0: michael@0: if ( PR_Connect(sock_h, &sa, michael@0: PR_INTERVAL_NO_TIMEOUT) < 0) { michael@0: fprintf(stderr, "Error accepting connection %d in client thread %d\n", michael@0: index, *id); michael@0: goto done; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "client thread %d socket connected %d\n", *id, sock_h); michael@0: #endif michael@0: michael@0: connect_done = PR_IntervalNow(); michael@0: if ( PR_Send(sock_h, data_buffer, _client_data, 0, michael@0: PR_INTERVAL_NO_TIMEOUT) < _client_data) { michael@0: fprintf(stderr, "Error sending client data for iteration %d in client thread %d\n", index, *id ); michael@0: goto done; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "client thread %d socket wrote %d\n", *id, _client_data); michael@0: #endif michael@0: michael@0: write_done = PR_IntervalNow(); michael@0: if ( (bytes = _readn(sock_h, data_buffer, _server_data)) < _server_data) { michael@0: fprintf(stderr, "Error reading server data for iteration %d in client thread %d (read %d bytes)\n", index, *id, bytes ); michael@0: goto done; michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: fprintf(stdout, "client thread %d socket read %d\n", *id, _server_data); michael@0: #endif michael@0: michael@0: read_done = PR_IntervalNow(); michael@0: PR_Close(sock_h); michael@0: close_done = PR_IntervalNow(); michael@0: michael@0: timer_data[2*(*id)+1].d_connect += _delta(&start, &connect_done); michael@0: timer_data[2*(*id)+1].d_cl_data += _delta(&connect_done, &write_done); michael@0: timer_data[2*(*id)+1].d_sv_data += _delta(&write_done, &read_done); michael@0: timer_data[2*(*id)+1].d_close += _delta(&read_done, &close_done); michael@0: timer_data[2*(*id)+1].d_total += _delta(&start, &close_done); michael@0: timer_data[2*(*id)+1].requests++; michael@0: } michael@0: done: michael@0: if (data_buffer != NULL) PR_Free (data_buffer); michael@0: _thread_exit(*id); michael@0: michael@0: return; michael@0: } michael@0: michael@0: static michael@0: void do_work(void) michael@0: { michael@0: int index; michael@0: michael@0: _thread_exit_count = _threads * 2; michael@0: for (index=0; index<_threads; index++) { michael@0: PRThread *thread; michael@0: int *id = (int *)PR_Malloc(sizeof(int)); michael@0: michael@0: *id = index; michael@0: michael@0: if ( (thread = PR_CreateThread(PR_USER_THREAD, michael@0: _server_thread, michael@0: id, michael@0: PR_PRIORITY_NORMAL, michael@0: scope1, michael@0: PR_UNJOINABLE_THREAD, michael@0: 0)) == NULL) michael@0: fprintf(stderr, "Error creating server thread %d\n", index); michael@0: } michael@0: michael@0: PR_EnterMonitor(exit_cv); michael@0: while (_thread_exit_count > 0) michael@0: PR_Wait(exit_cv, PR_INTERVAL_NO_TIMEOUT); michael@0: PR_ExitMonitor(exit_cv); michael@0: michael@0: fprintf(stdout, "TEST COMPLETE!\n"); michael@0: michael@0: tally_results(verbose); michael@0: michael@0: } michael@0: michael@0: static void do_workUU(void) michael@0: { michael@0: scope1 = PR_LOCAL_THREAD; michael@0: scope2 = PR_LOCAL_THREAD; michael@0: do_work(); michael@0: } michael@0: michael@0: static void do_workUK(void) michael@0: { michael@0: scope1 = PR_LOCAL_THREAD; michael@0: scope2 = PR_GLOBAL_THREAD; michael@0: do_work(); michael@0: } michael@0: michael@0: static void do_workKU(void) michael@0: { michael@0: scope1 = PR_GLOBAL_THREAD; michael@0: scope2 = PR_LOCAL_THREAD; michael@0: do_work(); michael@0: } michael@0: michael@0: static void do_workKK(void) michael@0: { michael@0: scope1 = PR_GLOBAL_THREAD; michael@0: scope2 = PR_GLOBAL_THREAD; michael@0: do_work(); michael@0: } michael@0: michael@0: michael@0: michael@0: static void Measure(void (*func)(void), const char *msg) michael@0: { michael@0: PRIntervalTime start, stop; michael@0: double d; michael@0: michael@0: start = PR_IntervalNow(); michael@0: (*func)(); michael@0: stop = PR_IntervalNow(); michael@0: michael@0: d = (double)PR_IntervalToMicroseconds(stop - start); michael@0: michael@0: printf("%40s: %6.2f usec\n", msg, d / _iterations); michael@0: } michael@0: michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: #if defined(XP_UNIX) || defined(XP_OS2) michael@0: int opt; michael@0: PR_IMPORT_DATA(char *) optarg; michael@0: #endif michael@0: michael@0: #if defined(XP_UNIX) || defined(XP_OS2) michael@0: while ( (opt = getopt(argc, argv, "c:s:i:t:v")) != EOF) { michael@0: switch(opt) { michael@0: case 'i': michael@0: _iterations = atoi(optarg); michael@0: break; michael@0: case 't': michael@0: _threads_max = _threads = atoi(optarg); michael@0: break; michael@0: case 'c': michael@0: _client_data = atoi(optarg); michael@0: break; michael@0: case 's': michael@0: _server_data = atoi(optarg); michael@0: break; michael@0: case 'v': michael@0: verbose = 1; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: PR_STDIO_INIT(); michael@0: michael@0: fprintf(stdout, "Running test for %d iterations with %d simultaneous threads.\n", michael@0: _iterations, _threads); michael@0: fprintf(stdout, "\tWill send %d bytes of client data and %d bytes of server data\n", michael@0: _client_data, _server_data); michael@0: michael@0: if ( (exit_cv = PR_NewMonitor()) == NULL) michael@0: fprintf(stderr, "Error creating monitor for exit cv\n"); michael@0: if ( (timer_data = (timer_slot_t *)PR_Malloc(2*_threads * sizeof(timer_slot_t))) == NULL) michael@0: fprintf(stderr, "error allocating thread time results array\n"); michael@0: memset(timer_data, 0 , 2*_threads*sizeof(timer_slot_t)); michael@0: michael@0: Measure(do_workUU, "select loop user/user"); michael@0: Measure(do_workUK, "select loop user/kernel"); michael@0: Measure(do_workKU, "select loop kernel/user"); michael@0: Measure(do_workKK, "select loop kernel/kernel"); michael@0: michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: void michael@0: tally_results(int verbose) michael@0: { michael@0: int index; michael@0: unsigned long tot_connect = 0; michael@0: unsigned long tot_cl_data = 0; michael@0: unsigned long tot_sv_data = 0; michael@0: unsigned long tot_close = 0; michael@0: unsigned long tot_all = 0; michael@0: unsigned long tot_requests = 0; michael@0: michael@0: fprintf(stdout, "Server results:\n\n"); michael@0: for (index=0; index<_threads_max*2; index+=2) { michael@0: michael@0: if (verbose) michael@0: fprintf(stdout, "server thread %u\t%u\t%u\t%u\t%u\t%u\t%u\n", michael@0: index, timer_data[index].requests, timer_data[index].d_connect, michael@0: timer_data[index].d_cl_data, timer_data[index].d_sv_data, michael@0: timer_data[index].d_close, timer_data[index].d_total); michael@0: michael@0: tot_connect += timer_data[index].d_connect / _threads; michael@0: tot_cl_data += timer_data[index].d_cl_data / _threads; michael@0: tot_sv_data += timer_data[index].d_sv_data / _threads; michael@0: tot_close += timer_data[index].d_close / _threads; michael@0: tot_all += timer_data[index].d_total / _threads; michael@0: tot_requests += timer_data[index].requests / _threads; michael@0: } michael@0: fprintf(stdout, "----------\n"); michael@0: fprintf(stdout, "server per thread totals %u\t%u\t%u\t%u\t%u\n", michael@0: tot_requests, tot_connect, tot_cl_data, tot_sv_data, tot_close); michael@0: fprintf(stdout, "server per thread elapsed time %u\n", tot_all); michael@0: fprintf(stdout, "----------\n"); michael@0: michael@0: tot_connect = tot_cl_data = tot_sv_data = tot_close = tot_all = tot_requests = 0; michael@0: fprintf(stdout, "Client results:\n\n"); michael@0: for (index=1; index<_threads_max*2; index+=2) { michael@0: michael@0: if (verbose) michael@0: fprintf(stdout, "client thread %u\t%u\t%u\t%u\t%u\t%u\t%u\n", michael@0: index, timer_data[index].requests, timer_data[index].d_connect, michael@0: timer_data[index].d_cl_data, timer_data[index].d_sv_data, michael@0: timer_data[index].d_close, timer_data[index].d_total); michael@0: michael@0: tot_connect += timer_data[index].d_connect / _threads; michael@0: tot_cl_data += timer_data[index].d_cl_data / _threads; michael@0: tot_sv_data += timer_data[index].d_sv_data / _threads; michael@0: tot_close += timer_data[index].d_close / _threads; michael@0: tot_all += timer_data[index].d_total / _threads; michael@0: tot_requests += timer_data[index].requests / _threads; michael@0: } michael@0: fprintf(stdout, "----------\n"); michael@0: fprintf(stdout, "client per thread totals %u\t%u\t%u\t%u\t%u\n", michael@0: tot_requests, tot_connect, tot_cl_data, tot_sv_data, tot_close); michael@0: fprintf(stdout, "client per thread elapsed time %u\n", tot_all); michael@0: } michael@0: