Back to index

lightning-sunbird  0.9+nobinonly
sel_spd.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Test the speed of select within NSPR
00040  *
00041  */
00042 
00043 #include "nspr.h"
00044 #include "prpriv.h"
00045 
00046 #include <stdlib.h>
00047 #include <stdio.h>
00048 #include <errno.h>
00049 #include <string.h>
00050 
00051 #ifdef XP_MAC
00052 #include "prlog.h"
00053 int fprintf(FILE *stream, const char *fmt, ...)
00054 {
00055 PR_LogPrint(fmt);
00056 return 0;
00057 }
00058 #define printf PR_LogPrint
00059 extern void SetupMacPrintfLog(char *logFile);
00060 #endif
00061 
00062 #define PORT_BASE 19000
00063 
00064 typedef struct timer_slot_t {
00065        unsigned long d_connect;
00066        unsigned long d_cl_data;
00067        unsigned long d_sv_data;
00068        unsigned long d_close;
00069        unsigned long d_total;
00070        unsigned long requests;
00071 } timer_slot_t;
00072 
00073 static long _iterations = 5;
00074 static long _client_data = 8192;
00075 
00076 #if defined(XP_MAC)
00077 /*
00078  * Mac does not scale well specially the requirement for thread stack
00079  * space and buffer allocation space.  It is easy to get into a fragmented
00080  * memory and not be able to allocate thread stack or client/server data
00081  * buffer.
00082 */
00083 static long _server_data = (8*1024);
00084 static long _threads_max = 10, _threads = 10;
00085 #else
00086 static long _server_data = (128*1024);
00087 static long _threads_max = 10, _threads = 10;
00088 #endif
00089 
00090 static int verbose=0;
00091 static PRMonitor *exit_cv;
00092 static long _thread_exit_count;
00093 static timer_slot_t *timer_data;
00094 static PRThreadScope scope1, scope2;
00095 
00096 void tally_results(int);
00097 
00098 /* return the diff in microseconds */
00099 unsigned long _delta(PRIntervalTime *start, PRIntervalTime *stop)
00100 {
00101        /*
00102         * Will C do the right thing with unsigned arithemtic?
00103         */
00104        return PR_IntervalToMicroseconds(*stop - *start);
00105 }
00106 
00107 int _readn(PRFileDesc *sock, char *buf, int len)
00108 {
00109        int rem;
00110        int bytes;
00111 
00112        for (rem=len; rem; rem -= bytes) {
00113               bytes = PR_Recv(sock, buf+len-rem, rem, 0, PR_INTERVAL_NO_TIMEOUT);
00114               if (bytes <= 0)
00115             return -1;
00116        }
00117        return len;
00118 }
00119 
00120 void
00121 _thread_exit(int id)
00122 {
00123        PR_EnterMonitor(exit_cv);
00124 #ifdef DEBUG
00125        fprintf(stdout, "Thread %d EXIT\n", id);
00126 #endif
00127 
00128        _thread_exit_count--;
00129        if (_thread_exit_count == 0) {
00130 #ifdef DEBUG
00131        fprintf(stdout, "Thread %d EXIT triggered notify\n", id);
00132 #endif
00133               PR_Notify(exit_cv);
00134        }
00135        PR_ExitMonitor(exit_cv);
00136 }
00137 
00138 void
00139 _server_thread(void *arg_id)
00140 {
00141        void _client_thread(void *);
00142        PRThread *thread;
00143        int *id =  (int *)arg_id;
00144        PRFileDesc *sock;
00145        PRSocketOptionData sockopt;
00146        PRNetAddr sa;
00147        PRFileDesc * newsock;
00148        char *data_buffer = NULL;
00149        int data_buffer_size;
00150        int index;
00151        PRIntervalTime start,
00152              connect_done,
00153              read_done,
00154              write_done,
00155              close_done;
00156        
00157 
00158 #ifdef DEBUG
00159        fprintf(stdout, "server thread %d alive\n", *id);
00160 #endif
00161 
00162        data_buffer_size = (_client_data>_server_data?_client_data:_server_data);
00163 
00164        if ( (data_buffer = (char *)PR_Malloc(data_buffer_size * sizeof(char))) == NULL ) {
00165               fprintf(stderr, "Error creating buffer in server thread %d\n", *id);
00166               goto done;
00167        }
00168 
00169 
00170        if ( (sock = PR_NewTCPSocket()) == NULL) {
00171               fprintf(stderr, "Error creating socket in server thread %d\n", *id);
00172               goto done;
00173        }
00174 
00175        sockopt.option = PR_SockOpt_Reuseaddr;
00176        sockopt.value.reuse_addr = PR_TRUE;
00177        if ( PR_SetSocketOption(sock, &sockopt) == PR_FAILURE) {
00178               fprintf(stderr, "Error setting socket option in server thread %d\n", *id);
00179               goto done;
00180        }
00181 
00182        memset(&sa, 0 , sizeof(sa));
00183        sa.inet.family = PR_AF_INET;
00184        sa.inet.port = PR_htons(PORT_BASE + *id);
00185        sa.inet.ip = PR_htonl(PR_INADDR_ANY);
00186 
00187        if ( PR_Bind(sock, &sa) < 0) {
00188               fprintf(stderr, "Error binding socket in server thread %d errno = %d\n", *id, errno);
00189               goto done;
00190        }
00191 
00192        if ( PR_Listen(sock, 32) < 0 ) {
00193               fprintf(stderr, "Error listening to socket in server thread %d\n", *id);
00194               goto done;
00195        }
00196 
00197        /* Tell the client to start */
00198        if ( (thread = PR_CreateThread(PR_USER_THREAD, 
00199                                       _client_thread, 
00200                                       id, 
00201                                       PR_PRIORITY_NORMAL, 
00202                                       scope2, 
00203                                       PR_UNJOINABLE_THREAD, 
00204                                       0)) == NULL)
00205               fprintf(stderr, "Error creating client thread %d\n", *id);
00206 
00207        for (index = 0; index< _iterations; index++) {
00208 
00209 #ifdef DEBUG
00210        fprintf(stdout, "server thread %d loop %d\n", *id, index);
00211 #endif
00212 
00213               start = PR_IntervalNow();
00214 
00215               if ( (newsock = PR_Accept(sock, &sa,
00216                                    PR_INTERVAL_NO_TIMEOUT)) == NULL) {
00217                      fprintf(stderr, "Error accepting connection %d in server thread %d\n",
00218                             index, *id);
00219                      goto done;
00220               }
00221 #ifdef DEBUG
00222        fprintf(stdout, "server thread %d got connection %d\n", *id, newsock);
00223 #endif
00224 
00225 
00226               connect_done = PR_IntervalNow();
00227               
00228               if ( _readn(newsock, data_buffer, _client_data) < _client_data) {
00229                      fprintf(stderr, "Error reading client data for iteration %d in server thread %d\n", index, *id );
00230                      goto done;
00231               }
00232 
00233 #ifdef DEBUG
00234        fprintf(stdout, "server thread %d read %d bytes\n", *id, _client_data);
00235 #endif
00236               read_done = PR_IntervalNow();
00237 
00238               if ( PR_Send(newsock, data_buffer, _server_data, 0,
00239                             PR_INTERVAL_NO_TIMEOUT) < _server_data) {
00240                      fprintf(stderr, "Error sending client data for iteration %d in server thread %d\n", index, *id );
00241                      goto done;
00242               }
00243 
00244 #ifdef DEBUG
00245        fprintf(stdout, "server thread %d write %d bytes\n", *id, _server_data);
00246 #endif
00247 
00248               write_done = PR_IntervalNow();
00249 
00250               PR_Close(newsock);
00251 
00252               close_done = PR_IntervalNow();
00253 
00254               timer_data[2*(*id)].d_connect += _delta(&start, &connect_done);
00255               timer_data[2*(*id)].d_cl_data += _delta(&connect_done, &read_done);
00256               timer_data[2*(*id)].d_sv_data += _delta(&read_done, &write_done);
00257               timer_data[2*(*id)].d_close += _delta(&write_done, &close_done);
00258               timer_data[2*(*id)].d_total += _delta(&start, &close_done);
00259               timer_data[2*(*id)].requests++;
00260 
00261 
00262 #ifdef DEBUG
00263               fprintf(stdout, "server: %d %d %d %d %d\n",
00264                       _delta(&start, &connect_done), _delta(&connect_done, &read_done),
00265                       _delta(&read_done, &write_done), _delta(&write_done, &close_done),
00266                      _delta(&start, &close_done));
00267 #endif
00268        }
00269 
00270 done:
00271        if (data_buffer != NULL) PR_Free (data_buffer);
00272        if (sock) PR_Close(sock);
00273        _thread_exit(*id);
00274        return;
00275 }
00276 
00277 void
00278 _client_thread(void *arg_id)
00279 {
00280        int *id =  (int *)arg_id;
00281        int index;
00282        PRNetAddr sa;
00283        PRFileDesc *sock_h;
00284        char *data_buffer = NULL;
00285        int data_buffer_size;
00286        int bytes;
00287        PRIntervalTime start,
00288              connect_done,
00289              read_done,
00290              write_done,
00291              close_done;
00292        PRStatus rv;
00293 
00294 #ifdef DEBUG
00295        fprintf(stdout, "client thread %d alive\n", *id);
00296 #endif
00297 
00298        data_buffer_size = (_client_data>_server_data?_client_data:_server_data);
00299 
00300        if ( (data_buffer = (char *)PR_Malloc(data_buffer_size * sizeof(char))) == NULL) {
00301               fprintf(stderr, "Error creating buffer in server thread %d\n", *id);
00302               goto done;
00303        }
00304 
00305        memset(&sa, 0 , sizeof(sa));
00306        rv = PR_InitializeNetAddr(PR_IpAddrLoopback, PORT_BASE + *id, &sa);
00307        PR_ASSERT(PR_SUCCESS == rv);
00308        
00309        for (index = 0; index< _iterations; index++) {
00310 
00311 #ifdef DEBUG
00312        fprintf(stdout, "client thread %d loop %d\n", *id, index);
00313 #endif
00314 
00315               start = PR_IntervalNow();
00316               if ( (sock_h = PR_NewTCPSocket()) == NULL) {
00317                      fprintf(stderr, "Error creating socket %d in client thread %d\n",
00318                             index, *id);
00319                      goto done;
00320               }
00321 
00322 #ifdef DEBUG
00323        fprintf(stdout, "client thread %d socket created %d\n", *id, sock_h);
00324 #endif
00325 
00326               if ( PR_Connect(sock_h, &sa,
00327                             PR_INTERVAL_NO_TIMEOUT) < 0) {
00328                      fprintf(stderr, "Error accepting connection %d in client thread %d\n",
00329                             index, *id);
00330                      goto done;
00331               }
00332 
00333 #ifdef DEBUG
00334        fprintf(stdout, "client thread %d socket connected %d\n", *id, sock_h);
00335 #endif
00336 
00337               connect_done = PR_IntervalNow();
00338               if ( PR_Send(sock_h, data_buffer, _client_data, 0,
00339                             PR_INTERVAL_NO_TIMEOUT) < _client_data) {
00340                      fprintf(stderr, "Error sending client data for iteration %d in client thread %d\n", index, *id );
00341                      goto done;
00342               }
00343 
00344 #ifdef DEBUG
00345        fprintf(stdout, "client thread %d socket wrote %d\n", *id, _client_data);
00346 #endif
00347 
00348               write_done = PR_IntervalNow();
00349               if ( (bytes = _readn(sock_h, data_buffer, _server_data)) < _server_data) {
00350                      fprintf(stderr, "Error reading server data for iteration %d in client thread %d (read %d bytes)\n", index, *id, bytes );
00351                      goto done;
00352               }
00353 
00354 #ifdef DEBUG
00355        fprintf(stdout, "client thread %d socket read %d\n", *id, _server_data);
00356 #endif
00357 
00358               read_done = PR_IntervalNow();
00359               PR_Close(sock_h);
00360               close_done = PR_IntervalNow();
00361 
00362               timer_data[2*(*id)+1].d_connect += _delta(&start, &connect_done);
00363               timer_data[2*(*id)+1].d_cl_data += _delta(&connect_done, &write_done);
00364               timer_data[2*(*id)+1].d_sv_data += _delta(&write_done, &read_done);
00365               timer_data[2*(*id)+1].d_close += _delta(&read_done, &close_done);
00366               timer_data[2*(*id)+1].d_total += _delta(&start, &close_done);
00367               timer_data[2*(*id)+1].requests++;
00368        }
00369 done:
00370        if (data_buffer != NULL) PR_Free (data_buffer);
00371        _thread_exit(*id);
00372 
00373        return;
00374 }
00375 
00376 static
00377 void do_work(void)
00378 {
00379     int index;
00380 
00381        _thread_exit_count = _threads * 2;
00382        for (index=0; index<_threads; index++) {
00383               PRThread *thread;
00384               int *id = (int *)PR_Malloc(sizeof(int));
00385 
00386               *id = index;
00387 
00388               if ( (thread = PR_CreateThread(PR_USER_THREAD, 
00389                                        _server_thread, 
00390                                        id, 
00391                                        PR_PRIORITY_NORMAL, 
00392                                        scope1, 
00393                                        PR_UNJOINABLE_THREAD, 
00394                                        0)) == NULL)
00395                      fprintf(stderr, "Error creating server thread %d\n", index);
00396        }
00397        
00398        PR_EnterMonitor(exit_cv);
00399        while (_thread_exit_count > 0)
00400               PR_Wait(exit_cv, PR_INTERVAL_NO_TIMEOUT);
00401        PR_ExitMonitor(exit_cv);
00402 
00403        fprintf(stdout, "TEST COMPLETE!\n");
00404 
00405        tally_results(verbose);
00406 
00407 }
00408 
00409 static void do_workUU(void)
00410 {
00411     scope1 = PR_LOCAL_THREAD;
00412     scope2 = PR_LOCAL_THREAD;
00413     do_work();
00414 }
00415 
00416 static void do_workUK(void)
00417 {
00418     scope1 = PR_LOCAL_THREAD;
00419     scope2 = PR_GLOBAL_THREAD;
00420     do_work();
00421 }
00422 
00423 static void do_workKU(void)
00424 {
00425     scope1 = PR_GLOBAL_THREAD;
00426     scope2 = PR_LOCAL_THREAD;
00427     do_work();
00428 }
00429 
00430 static void do_workKK(void)
00431 {
00432     scope1 = PR_GLOBAL_THREAD;
00433     scope2 = PR_GLOBAL_THREAD;
00434     do_work();
00435 }
00436 
00437 
00438 
00439 static void Measure(void (*func)(void), const char *msg)
00440 {
00441     PRIntervalTime start, stop;
00442     double d;
00443 
00444     start = PR_IntervalNow();
00445     (*func)();
00446     stop = PR_IntervalNow();
00447 
00448     d = (double)PR_IntervalToMicroseconds(stop - start);
00449 
00450     printf("%40s: %6.2f usec\n", msg, d / _iterations);
00451 }
00452 
00453 
00454 int main(int argc, char **argv)
00455 {
00456 #if defined(XP_UNIX) || defined(XP_OS2_EMX)
00457        int opt;
00458        PR_IMPORT_DATA(char *) optarg;
00459 #endif
00460 
00461 #if defined(XP_UNIX) || defined(XP_OS2_EMX)
00462        while ( (opt = getopt(argc, argv, "c:s:i:t:v")) != EOF) {
00463               switch(opt) {
00464                      case 'i':
00465                             _iterations = atoi(optarg);
00466                             break;
00467                      case 't':
00468                             _threads_max = _threads = atoi(optarg);
00469                             break;
00470                      case 'c':
00471                             _client_data = atoi(optarg);
00472                             break;
00473                      case 's':
00474                             _server_data = atoi(optarg);
00475                             break;
00476                      case 'v':
00477                             verbose = 1;
00478                             break;
00479                      default: 
00480                             break;
00481               }
00482        }
00483 #endif
00484 
00485        PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00486     PR_STDIO_INIT();
00487 
00488 #ifdef XP_MAC
00489        SetupMacPrintfLog("sel_spd.log");
00490 #endif
00491 
00492        fprintf(stdout, "Running test for %d iterations with %d simultaneous threads.\n", 
00493               _iterations, _threads);
00494        fprintf(stdout, "\tWill send %d bytes of client data and %d bytes of server data\n", 
00495               _client_data, _server_data);
00496 
00497        if ( (exit_cv = PR_NewMonitor()) == NULL) 
00498               fprintf(stderr, "Error creating monitor for exit cv\n");
00499        if ( (timer_data = (timer_slot_t *)PR_Malloc(2*_threads * sizeof(timer_slot_t))) == NULL) 
00500               fprintf(stderr, "error allocating thread time results array\n");
00501        memset(timer_data, 0 , 2*_threads*sizeof(timer_slot_t));
00502 
00503     Measure(do_workUU, "select loop user/user");
00504     Measure(do_workUK, "select loop user/kernel");
00505     Measure(do_workKU, "select loop kernel/user");
00506     Measure(do_workKK, "select loop kernel/kernel");
00507 
00508 
00509        return 0;
00510 }
00511 
00512 void
00513 tally_results(int verbose)
00514 {
00515        int index;
00516        unsigned long tot_connect = 0;
00517        unsigned long tot_cl_data = 0;
00518        unsigned long tot_sv_data = 0;
00519        unsigned long tot_close = 0;
00520        unsigned long tot_all = 0;
00521        unsigned long tot_requests = 0;
00522 
00523        fprintf(stdout, "Server results:\n\n");
00524        for (index=0; index<_threads_max*2; index+=2) {
00525 
00526               if (verbose)
00527                      fprintf(stdout, "server thread %u\t%u\t%u\t%u\t%u\t%u\t%u\n",
00528                             index, timer_data[index].requests, timer_data[index].d_connect,
00529                             timer_data[index].d_cl_data, timer_data[index].d_sv_data,
00530                             timer_data[index].d_close, timer_data[index].d_total);
00531 
00532               tot_connect += timer_data[index].d_connect / _threads;
00533               tot_cl_data += timer_data[index].d_cl_data / _threads;
00534               tot_sv_data += timer_data[index].d_sv_data / _threads;
00535               tot_close += timer_data[index].d_close / _threads;
00536               tot_all += timer_data[index].d_total / _threads;
00537               tot_requests += timer_data[index].requests / _threads;
00538        }
00539        fprintf(stdout, "----------\n");
00540        fprintf(stdout, "server per thread totals %u\t%u\t%u\t%u\t%u\n",
00541               tot_requests, tot_connect, tot_cl_data, tot_sv_data, tot_close);
00542        fprintf(stdout, "server per thread elapsed time %u\n", tot_all);
00543        fprintf(stdout, "----------\n");
00544 
00545        tot_connect = tot_cl_data = tot_sv_data = tot_close = tot_all = tot_requests = 0;
00546        fprintf(stdout, "Client results:\n\n");
00547        for (index=1; index<_threads_max*2; index+=2) {
00548 
00549               if (verbose)
00550                      fprintf(stdout, "client thread %u\t%u\t%u\t%u\t%u\t%u\t%u\n",
00551                             index, timer_data[index].requests, timer_data[index].d_connect,
00552                             timer_data[index].d_cl_data, timer_data[index].d_sv_data,
00553                             timer_data[index].d_close, timer_data[index].d_total);
00554 
00555               tot_connect += timer_data[index].d_connect / _threads;
00556               tot_cl_data += timer_data[index].d_cl_data / _threads;
00557               tot_sv_data += timer_data[index].d_sv_data / _threads;
00558               tot_close += timer_data[index].d_close / _threads;
00559               tot_all += timer_data[index].d_total / _threads;
00560               tot_requests += timer_data[index].requests / _threads;
00561        }
00562        fprintf(stdout, "----------\n");
00563        fprintf(stdout, "client per thread totals %u\t%u\t%u\t%u\t%u\n",
00564               tot_requests, tot_connect, tot_cl_data, tot_sv_data, tot_close);
00565        fprintf(stdout, "client per thread elapsed time %u\n", tot_all);
00566 }
00567