File: netpipe.c

package info (click to toggle)
netpipe 2.3-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 160 kB
  • ctags: 109
  • sloc: ansic: 979; makefile: 113
file content (472 lines) | stat: -rw-r--r-- 15,379 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*****************************************************************************/
/* "NetPIPE" -- Network Protocol Independent Performance Evaluator.          */
/* Copyright 1997, 1998 Iowa State University Research Foundation, Inc.      */
/*                                                                           */
/* This program is free software; you can redistribute it and/or modify      */
/* it under the terms of the GNU General Public License as published by      */
/* the Free Software Foundation.  You should have received a copy of the     */
/* GNU General Public License along with this program; if not, write to the  */
/* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   */
/*                                                                           */
/* Files needed for use:                                                     */
/*     * netpipe.c          ---- Driver source                               */
/*     * netpipe.h          ---- General include file                        */
/*     * TCP.c              ---- TCP calls source                            */
/*     * TCP.h              ---- Include file for TCP calls and data structs */
/*     * MPI.c              ---- MPI calls source                            */
/*     * MPI.h              ---- Include file for MPI calls and data structs */
/*     * PVM.c              ---- PVM calls source                            */
/*     * PVM.h              ---- Include file for PVM calls and data structs */
/*****************************************************************************/
#include "netpipe.h"

extern char *optarg;

#ifndef lint
static const char rcsid[] =
	"$Id: netpipe.c,v 1.8 1998/09/24 16:02:45 ghelmer Exp $";
#endif

main(int argc, char *argv[])
{
    FILE        *out;           /* Output data file                          */
    char        s[255];         /* Generic string                            */
    char        *memtmp;
    char        *memtmp1;

    int         c,              /* option index                              */
                i, j, k, n, nq, /* Loop indices                              */
		asyncReceive=0, /* Pre-post a receive buffer?                */
                bits,           /* Number of bits transmitted                */
                bufoffset=0,    /* Align buffer to this                      */
                bufalign=16*1024,/* Boundary to align buffer to              */
		errFlag,        /* Error occurred in inner testing loop      */
                nrepeat,        /* Number of time to do the transmission     */
                nzero=0,
                len,            /* Number of bytes to be transmitted         */
                inc=0,          /* Increment value                           */
                trans=-1,       /* Transmitter flag. 1 if transmitting.      */
                detailflag=0,   /* Set to examine the signature curve detail */
                bufszflag=0,    /* Set to change the TCP socket buffer size  */
                pert,           /* Perturbation value                        */
                start=1,        /* Starting value for signature curve        */
                end=MAXINT,     /* Ending value for signature curve          */
                streamopt=0,    /* Streaming mode flag                       */
                printopt=0;     /* Debug print statements flag               */
   
    ArgStruct   args;           /* Argumentsfor all the calls                */

    double      t, t0, t1, t2,  /* Time variables                            */
                tlast,          /* Time for the last transmission            */
                tzero=0,
                latency,        /* Network message latency                   */
                ackt,           /* Time for acknowlegement                   */
                bps,            /* Bits per second                           */
                bwtmp,          /* Temporary for calculating Net bps         */
                trepeat,
                expectedMbps,   /* Expected Mbps                             */
                netbps;         /* Net bandwidth                             */

    Data        bwdata[NSAMP];  /* Bandwidth curve data                      */

    short       port=DEFPORT;   /* Port number for connection                */

#ifdef MPI
    MPI_Init(&argc, &argv);
#endif

    strcpy(s, "NetPIPE.out");
#ifndef MPI
    if(argc < 2)
     PrintUsage();
#endif

    /* Parse the arguments. See Usage for description */
    while ((c = getopt(argc, argv, "Pstrh:p:o:A:O:l:u:i:b:a")) != -1)
    {
        switch(c)
        {
            case 'o': strcpy(s,optarg);
                      break;

            case 't': trans = 1;
                      break;
            
            case 'r': trans = 0;
                      break;

            case 's': streamopt = 1;
                      break;

            case 'l': /*detailflag = 1;*/
                      start = atoi(optarg);
                      if (start < 1)
                      {
                        fprintf(stderr,"Need a starting value >= 1\n");
                        exit(743);
                      }
                      break;

            case 'u': /*detailflag = 1;*/
                      end = atoi(optarg);
                      break;

            case 'i': detailflag = 1;
                      inc = atoi(optarg);
                      break;

            case 'b': bufszflag = 1;
#ifdef TCP
                      args.prot.rcvbufsz=atoi(optarg);
                      args.prot.sndbufsz=args.prot.rcvbufsz;
#endif
                      break;

            case 'P': printopt = 1;
                      break;
            
            case 'A': bufalign = atoi(optarg);
                      break;

            case 'O': bufoffset = atoi(optarg);
                      break;

            case 'p': port = atoi(optarg);
                      break;
            
            case 'h': if (trans == 1)
                      {
                          args.host = (char *)malloc(strlen(optarg)+1);
                          strcpy(args.host, optarg);
                      }
		      else
		      {
			  fprintf(stderr, "Error: -t must be specified before -h\n");
			  exit(-11);
		      }
                      break;

	    case 'a': asyncReceive = 1;
		      break;

            default:  PrintUsage(); 
                      exit(-12);
        }
    }
    if (start > end)
    {
        fprintf(stderr, "Start MUST be LESS than end\n");
        exit(420132);
    }
#if defined(TCP) || defined(PVM)
    /*
      It should be explicitly specified whether this is the transmitter
      or the receiver.
    */
    if (trans < 0)
    {
	fprintf(stderr, "Error: either -t or -r must be specified\n");
	exit(-11);
    }
#endif

    args.nbuff = TRIALS;
    args.tr = trans;
    args.port = port;

#if defined(TCP)
    if (!bufszflag)
    {
        args.prot.sndbufsz = 0;
        args.prot.rcvbufsz = 0;
    }
    else
        fprintf(stderr,"Send and Recv Buffers are %d bytes\n",
                                                   args.prot.sndbufsz);
#endif

    Setup(&args);
    Establish(&args);

    if (args.tr)
    {
        if ((out = fopen(s, "w")) == NULL)
        {
            fprintf(stderr,"Can't open %s for output\n", s);
            exit(1);
        }
    }
    else
        out = stdout;

    args.bufflen = 1;
    args.buff = (char *)malloc(args.bufflen);
    args.buff1 = (char *)malloc(args.bufflen);
    if (asyncReceive)
	PrepareToReceive(&args);
    Sync(&args);
    t0 = When();
    t0 = When();
    t0 = When();
    t0 = When();
    for (i = 0; i < LATENCYREPS; i++)
    {
        if (args.tr)
        {
            SendData(&args);
            RecvData(&args);
	    if (asyncReceive && (i < LATENCYREPS - 1))
	    {
		PrepareToReceive(&args);
	    }
        }
        else
        {
            RecvData(&args);
	    if (asyncReceive && (i < LATENCYREPS - 1))
	    {
		PrepareToReceive(&args);
	    }
            SendData(&args);
        }
    }
    latency = (When() - t0)/(2 * LATENCYREPS);
    free(args.buff);
    free(args.buff1);


    if (args.tr)
    {
        SendTime(&args, &latency);
    }
    else
    {
        RecvTime(&args, &latency);
    }
    if (args.tr && printopt)
    {
        fprintf(stderr,"Latency: %lf\n", latency);
        fprintf(stderr,"Now starting main loop\n");
    }
    tlast = latency;
    if (inc == 0)
    {
	/* Set a starting value for the message size increment. */
	inc = (start > 1) ? start / 2 : 1;
    }

    /* Main loop of benchmark */
    for (nq = n = 0, len = start, errFlag = 0; 
         n < NSAMP - 3 && tlast < STOPTM && len <= end && !errFlag; 
         len = len + inc, nq++ )
    {
        if (nq > 2 && !detailflag)
	  {
	    /*
	      This has the effect of exponentially increasing the block
	      size.  If detailflag is false, then the block size is
	      linearly increased (the increment is not adjusted).
	    */
            inc = ((nq % 2))? inc + inc: inc;
	  }
        
        /* This is a perturbation loop to test nearby values */
        for (pert = (!detailflag && inc > PERT+1)? -PERT: 0;
             pert <= PERT; 
             n++, pert += (!detailflag && inc > PERT+1)? PERT: PERT+1)
        {

            /* Calculate howmany times to repeat the experiment. */
            if (args.tr)
            {
                nrepeat = MAX((RUNTM / ((double)args.bufflen /
                                 (args.bufflen - inc + 1.0) * tlast)), TRIALS);
                SendRepeat(&args, nrepeat);
            }
            else
            {
                RecvRepeat(&args, &nrepeat);
            }

            /* Allocate the buffer */
            args.bufflen = len + pert;
            if((args.buff=(char *)malloc(args.bufflen+bufalign))==(char *)NULL)
            {
                fprintf(stderr,"Couldn't allocate memory\n");
		errFlag = -1;
                break;
            }
            if((args.buff1=(char *)malloc(args.bufflen+bufalign))==(char *)NULL)
            {
                fprintf(stderr,"Couldn't allocate memory\n");
		errFlag = -1;
                break;
            }
            /*
	      Possibly align the data buffer: make memtmp and memtmp1
	      point to the original blocks (so they can be freed later),
	      then adjust args.buff and args.buff1 if the user requested it.
	    */
            memtmp = args.buff;
            memtmp1 = args.buff1;
            if (bufalign != 0)
                args.buff +=(bufalign - 
                        ((int)args.buff % bufalign) + bufoffset) % bufalign;

            if (bufalign != 0)
                args.buff1 +=(bufalign - 
                        ((int)args.buff1 % bufalign) + bufoffset) % bufalign;


            if (args.tr && printopt)
                fprintf(stderr,"%3d: %9d bytes %4d times --> ",
                                 n,args.bufflen,nrepeat);

            /* Finally, we get to transmit or receive and time */
            if (args.tr)
            {
		/*
		   This is the transmitter: send the block TRIALS times, and
		   if we are not streaming, expect the receiver to return each
		   block.
		*/
                bwdata[n].t = LONGTIME;
                t2 = t1 = 0;
                for (i = 0; i < TRIALS; i++)
                {
                    Sync(&args);
                    t0 = When();
                    for (j = 0; j < nrepeat; j++)
                    {
			if (asyncReceive && !streamopt)
			{
			    PrepareToReceive(&args);
			}
                        SendData(&args);
                        if (!streamopt)
			{
                            RecvData(&args);
			}
                    }
                    t = (When() - t0)/((1 + !streamopt) * nrepeat);

                    if (!streamopt)
                    {
                        t2 += t*t;
                        t1 += t;
                        bwdata[n].t = MIN(bwdata[n].t, t);
                    }
                }
                if (!streamopt)
                    SendTime(&args, &bwdata[n].t);
                else
                    RecvTime(&args, &bwdata[n].t);

                if (!streamopt)
                    bwdata[n].variance = t2/TRIALS - t1/TRIALS * t1/TRIALS;

            }
            else
            {
		/*
		   This is the receiver: receive the block TRIALS times, and
		   if we are not streaming, send the block back to the
		   sender.
		*/
                bwdata[n].t = LONGTIME;
                t2 = t1 = 0;
                for (i = 0; i < TRIALS; i++)
                {
		    if (asyncReceive)
		    {
		    	PrepareToReceive(&args);
		    }
                    Sync(&args);
                    t0 = When();
                    for (j = 0; j < nrepeat; j++)
                    {
                        RecvData(&args);
			if (asyncReceive && (j < nrepeat - 1))
			{
			    PrepareToReceive(&args);
			}
                        if (!streamopt)
                            SendData(&args);
                    }
                    t = (When() - t0)/((1 + !streamopt) * nrepeat);

                    if (streamopt)
                    {
                        t2 += t*t;
                        t1 += t;
                        bwdata[n].t = MIN(bwdata[n].t, t);
                    }
                }
                if (streamopt)
                    SendTime(&args, &bwdata[n].t);
                else
                    RecvTime(&args, &bwdata[n].t);

                if (streamopt)
                    bwdata[n].variance = t2/TRIALS - t1/TRIALS * t1/TRIALS;

            }
            tlast = bwdata[n].t;
            bwdata[n].bits = args.bufflen * CHARSIZE;
            bwdata[n].bps = bwdata[n].bits / (bwdata[n].t * 1024 * 1024);
            bwdata[n].repeat = nrepeat;
            
            if (args.tr)
              fprintf(out,"%lf %lf %d %d %lf\n",bwdata[n].t,bwdata[n].bps,
                       bwdata[n].bits, bwdata[n].bits / 8, bwdata[n].variance);
            fflush(out);

            free(memtmp);
            free(memtmp1);

            if (args.tr && printopt)
              fprintf(stderr," %6.2lf Mbps in %lf sec\n", bwdata[n].bps,tlast);
        } /* End of perturbation loop */

    } /* End of main loop  */
        
    if (args.tr)
       fclose(out);
         
    CleanUp(&args);
}


/* Return the current time in seconds, using a double precision number.      */
double
When()
{
    struct timeval tp;
    gettimeofday(&tp, NULL);
    return ((double) tp.tv_sec + (double) tp.tv_usec * 1e-6);
}

PrintUsage()
{
	printf("\n NETPIPE USAGE \n\n");
	printf("A: specify buffers alignment e.g.: <-A 1024>\n");
	printf("a: asynchronous receive (a.k.a. preposted receive)\n");
#if defined(TCP)
	printf("b: specify send and receive buffer sizes e.g. <-b 32768>\n");
	printf("h: specify hostname <-h host>\n");
#endif
	printf("i: specify increment step size e.g. <-i 64>\n");
	printf("l: lower bound start value e.g. <-i 1>\n");
	printf("O: specify buffer offset e.g. <-O 127>\n");
	printf("o: specify output filename <-o fn>\n");
	printf("P: print on screen\n");
#if defined(TCP)
	printf("p: specify port e.g. <-p 5150>\n");
#endif
	printf("r: receiver\n");
	printf("s: stream option\n");
	printf("t: transmitter\n");
	printf("u: upper bound stop value e.g. <-u 1048576>\n");
	printf("\n");
	exit(-12);
}