histogram* histogram_new_binsize(double zero, double maximum, double binsize) { int Nbins = (int)ceil((maximum - zero) / binsize) + 1; histogram* h = hist_new(Nbins); h->min = zero; h->binsize = binsize; return h; }
histogram* histogram_new_nbins(double zero, double maximum, int Nbins) { double binsize = (maximum - zero) / (double)(Nbins - 1); histogram* h = hist_new(Nbins); h->min = zero; h->binsize = binsize; return h; }
histogram2d* histogram2d_new_nbins(double minX, double maxX, int NbinsX, double minY, double maxY, int NbinsY) { double binsizeX = (maxX - minX) / (double)(NbinsX); double binsizeY = (maxY - minY) / (double)(NbinsY); histogram2d* h = hist_new(NbinsX, NbinsY); h->minx = minX; //h->maxx = maxX; h->miny = minY; //h->maxy = maxY; h->binsizex = binsizeX; h->binsizey = binsizeY; h->NX = NbinsX; h->NY = NbinsY; h->edgex = HIST2D_TRUNCATE; h->edgey = HIST2D_TRUNCATE; return h; }
int main(int argc, char **argv) { int i, nthreads=-1; char *server_string = NULL; char opt; // deal w/ args while ( (opt = getopt(argc, argv, "ml:t:n:u:r:s:d")) != (char)EOF) { switch ( opt ) { case 'm': mdwstyle = 1; break; case 'l': spec_load = atoi(optarg); if( spec_load <= 0 ) usage("Invalid load level"); break; case 't': total_reports = atoi(optarg); if( total_reports <= 0 ) usage("Invalid number of reports"); break; case 'n': nthreads = atoi(optarg); if( nthreads <= 0 ) usage("Invalid number of threads"); break; case 'u': server_string = optarg; break; case 'd': debug = 1; break; case 'r': nreqs = atoi(optarg); if( nreqs <= 0 ) usage("Invalid option for -n NUM_REQUESTS"); break; case 's': sleep_ms = atoi(optarg); if( sleep_ms < 0 ) usage("Invalid option for -s SLEEP_MS"); break; case '?': usage(NULL); default: usage("Invalid option"); } } if( nthreads == -1 ) usage("You must specify the number of threads."); if( server_string == NULL ) usage("You didn't specify the base URL(s)."); // set up the SPEC load generation setupDists(spec_load); // create some histograms connect_hist = hist_new(HIST_BUCKETS, 0); err_connect_hist = hist_new(HIST_BUCKETS, 0); request_hist = hist_new(HIST_BUCKETS, 0); response_hist = hist_new(HIST_BUCKETS, 0); close_hist = hist_new(HIST_BUCKETS, 0); close_unconnected_hist = hist_new(HIST_BUCKETS, 0); sleep_hist = hist_new(HIST_BUCKETS, 0); // set up addres, ports, etc. for the servers { struct hostent *hostelement; char *str = server_string; char *end, c; int i=0; bzero( servers, sizeof(servers) ); while( str != NULL && *str != '\0' ) { // skip past http:// if( strncasecmp(str,"http://",7) == 0 ) str += 7; // read the hostname servers[i].hostname = str; str += strcspn(str,",/:"); if( servers[i].hostname == str ) usage("Invalid URL, hostname not found"); c = *str; *str = '\0'; str++; // read the port if( c == ':' ) { servers[i].port = (int) strtol(str, &end, 0); if( servers[i].port == 0 || str == end ) usage("Invalid port in URL."); str = end; } else { servers[i].port = 80; } // read the base URL str += strspn(str,"/"); servers[i].url = str; // update the server structure servers[i].addr.sin_family = AF_INET; servers[i].addr.sin_port = htons((short)servers[i].port); hostelement = gethostbyname(servers[i].hostname); assert( hostelement ); memcpy(&servers[i].addr.sin_addr, hostelement->h_addr, hostelement->h_length); printf("server %d: host=%s port=%d url=%s\n", i, servers[i].hostname, servers[i].port, servers[i].url ); // increment the number i++; if( i >= MAX_SERVERS ) usage("Too many servers specified."); // end the URL string str += strcspn(str,","); if( *str == '\0' ) break; *str = '\0'; str++; } num_servers = i; } // spawn client threads { int rv; pthread_attr_t attr; pthread_t thread; pthread_attr_init( &attr ); rv = pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED ); assert (rv == 0); for( i=0; i<nthreads; i++ ) { int rv; rv = pthread_create(&thread, &attr, &client, (void*)i); assert( rv == 0); } } // periodically print stats { cpu_tick_t prevstart; cpu_tick_t start, end; struct timeval tv_start, tv_prev; int num_reports_done = 0; // initial delay usleep( (STATS_INTERVAL_SECS*1e6) ); GET_REAL_CPU_TICKS(prevstart); gettimeofday(&tv_prev, NULL); while( 1 ) { unsigned long long s_bytes_read = bytes_read; unsigned long long s_bytes_written = bytes_written; unsigned long long s_good_conn = good_conn; unsigned long long s_bad_conn = bad_conn; unsigned long long s_conn_timedout = conn_timedout; unsigned long long s_io_errors = io_errors; unsigned long long s_addrnotavail = addrnotavail; unsigned long long s_requests_completed = requests_completed; //unsigned long long s_bind_port_calls = bind_port_calls; //unsigned long long s_ports_checked = ports_checked; int connect_mean = hist_mean(connect_hist); int err_connect_mean = hist_mean(err_connect_hist); int request_mean = hist_mean(request_hist); int response_mean = hist_mean(response_hist); int close_mean = hist_mean(close_hist); int close_unconnected_mean = hist_mean(close_unconnected_hist); int sleep_mean = hist_mean(sleep_hist); hist_reset(connect_hist); hist_reset(err_connect_hist); hist_reset(request_hist); hist_reset(response_hist); hist_reset(close_hist); hist_reset(close_unconnected_hist); hist_reset(sleep_hist); GET_REAL_CPU_TICKS(start); gettimeofday(&tv_start, NULL); requests_completed = 0; addrnotavail = 0; bind_port_calls = 0; ports_checked = 0; io_errors = 0; bad_conn = 0; conn_timedout = 0; good_conn = 0; bytes_written = 0; bytes_read = 0; printf("(%.1f %.1f) Mb/s: %5.2fr %5.2fw conn/s: %5llu (%llu %4llu) io err/s: %llu (%4llu) -- idle: %d (%d) conn: %d (%d,%d) write: %d (%d) read %d (%d) close %d (%d,%d)\n", ((float)(start-prevstart)) / ticks_per_second, ((float)TVDIFF(tv_start,tv_prev)) / 1e6, ((float)s_bytes_read)*8/1024/1024 / STATS_INTERVAL_SECS, ((float)s_bytes_written)*8/1024/1024 / STATS_INTERVAL_SECS, s_good_conn/STATS_INTERVAL_SECS, s_bad_conn/STATS_INTERVAL_SECS, s_conn_timedout/STATS_INTERVAL_SECS, s_io_errors/STATS_INTERVAL_SECS, s_addrnotavail/STATS_INTERVAL_SECS, state_idle, sleep_mean, state_connecting, connect_mean, err_connect_mean, state_writing, request_mean, state_reading, response_mean, state_closing, close_mean, close_unconnected_mean ); //0 BT: 4047705 5.174 if( mdwstyle ) { printf("Connection Rate: %f connections/sec, %llu conns\n", ((float)s_good_conn)/STATS_INTERVAL_SECS, s_good_conn); printf("Overall rate: %f completions/sec\n", ((float)s_requests_completed)/STATS_INTERVAL_SECS); printf("Bandwidth: %f bytes/sec\n", ((float)s_bytes_read) / STATS_INTERVAL_SECS); // FIXME: add histograms, and fix these up //printf("Connect Time: %f ms, max %d ms\n", 0, 0); //printf("Response Time: %f ms, max %d ms\n", 0, 0); //printf("Combined Response Time: %f ms, max %d ms\n", 0, 0); } fflush(stdout); num_reports_done++; if(num_reports_done >= total_reports) { // kludgey way to force an exit kill(getpid(), SIGINT); kill(getpid(), SIGQUIT); kill(getpid(), SIGKILL); } GET_REAL_CPU_TICKS(end); usleep( (STATS_INTERVAL_SECS*1e6) - (end-start)/ticks_per_microsecond ); prevstart = start; tv_prev = tv_start; } } return 0; }