File: trace.cc

package info (click to toggle)
nam 1.15-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 29,240 kB
  • sloc: cpp: 17,338; tcl: 10,655; sh: 2,997; ansic: 1,252; makefile: 139; perl: 66
file content (596 lines) | stat: -rw-r--r-- 17,495 bytes parent folder | download | duplicates (5)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
 * Copyright (c) 1991,1993 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *  This product includes software developed by the Computer Systems
 *  Engineering Group at Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * @(#) $Header: /cvsroot/nsnam/nam-1/trace.cc,v 1.69 2002/05/03 01:16:39 buchheim Exp $ (LBL)
 */

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/file.h>
#endif
#include <sys/stat.h>
#include <ctype.h>

#include "trace.h"
#include "state.h"
#include "packet.h"
#include "address.h"
#include "nam_stream.h"

extern double time_atof(const char*);

class TraceClass : public TclClass {
 public:
  TraceClass() : TclClass("Trace") {}
  TclObject* create(int argc, const char*const* argv);
} trace_class;

TclObject* TraceClass::create(int argc, const char*const* argv) {
  if (argc != 6)
    return (0);
  /*
   * Open the trace file for reading and read its first line
   * to be able to determine needed values: trace start time,
   * trace duration, etc.
   */
  Trace* tr = new Trace(argv[4], argv[5]);
  if (!tr->valid()) {
    delete tr;
    tr = NULL;
  }
  return (tr);
}

//----------------------------------------------------------------------
// Trace::Trace(const char *fname, const char *animator) 
//   - Create a new tracefile parser and event handler for
//     file "fname" and connect it to the animator object
//----------------------------------------------------------------------
Trace::Trace(const char *fname, const char *animator) : 
  handlers_(0),
  nam_(0),
  skipahead_mode_(0),
  count_(0) {
  
  last_event_time_ = 0.0;

  // Connect to nam animator
  nam_ = (NetworkAnimator *)TclObject::lookup(animator);

  strncpy(fileName_, fname, sizeof(fileName_)-1);
  fileName_[sizeof(fileName_)-1] = 0;
  nam_stream_ = NamStream::open(fileName_);
  if (!nam_stream_->is_ok()) {
    delete nam_stream_;
    nam_stream_ = NULL;
    perror("nam: fdopen");
    // exit(1);
    return; // tr->valid() will check this
  }
  findLastLine();

  /*
   * Go to the beginning of the file, read the first line and
   * save its contents.
   */
  off_t pos = nam_stream_->seek(0, SEEK_SET);
  assert(pos == 0);

  /*
   * Minimum time on the nam time slider is the time of the first
   * trace event.
   */
  mintime_ = nextTime();
  now_ = mintime_;

  State::instance()->setTimes(mintime_, maxtime_);

  // Initialize ParseTable
  parse_table_ = new ParseTable(&pending_);
}

//----------------------------------------------------------------------
// Trace::~Trace()
//----------------------------------------------------------------------
Trace::~Trace() {
  delete parse_table_;
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
int
Trace::valid() {
  return (nam_stream_ && nam_stream_->is_ok());
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
int
Trace::ReadEvent(double now, TraceEvent& e) {
  /*
   * If specified time is after the next event's time, read
   * the next line and return 1. Otherwise, just return 0.
   */
  if (direction_ == FORWARDS) {
    if ((pending_.time != TIME_EOF) && (pending_.time < now)) {
      e = pending_;
      scan();
      return (1);
    }
  } else {
    /*going backwards!*/
    /*
     * two occasions: 1) pending_.time > now, 
     * 2) pending_.time = -1, which means go to the end of file
     */
    if ((pending_.time > now) || (pending_.time == TIME_EOF)) {
      e = pending_;
      scan();
      if (pending_.time == TIME_EOF)
        pending_.time = TIME_BOF;
      return (1);
    }
  }
  return (0);
}

//---------------------------------------------------------------------
// int
// Trace::nextLine()
//
// - This needs to be cleaned up and it may effect other parts of
//   the file parsing code.  It is not very efficient with the amount
//   of seeking going on.  
//---------------------------------------------------------------------
int
Trace::nextLine() {
  char * position;

  if (direction_ == FORWARDS) {
    // moving forward through the trace file
    // We need to read in one line at a time
    nam_stream_->gets(pending_.image, sizeof(pending_.image));

    ++lineno_;
    if (nam_stream_->eof()) {
      Tcl& tcl = Tcl::instance();
      tcl.evalf("%s stopmaxtime %f", nam()->name(), pending_.time);
      return 0;
    }

  } else {
    // moving backwards through the trace file 
    position = nam_stream_->rgets(pending_.image, sizeof(pending_.image));

    lineno_--;
    if (position == NULL ) {
      fprintf(stderr, "nam file reading error.\n");
      return -1; // signal reading error
    }

    if (nam_stream_->tell() == 0) {
      // Beginning of file reached so we need to stop
      return -1;
    }  
  }

  return(nam_stream_->tell());
}

//---------------------------------------------------------------------
// void
// Trace::scan()
//   - scan for the next valid nam event line in file
//   - Once everything becomes stable you can to get rid of all the
//     data type specific scan functions
//---------------------------------------------------------------------
void
Trace::scan() {
  char * time;
  /*
   * Read the next line in the trace file and store its contents
   * depending on line type.
   */
  while (1) {
    TraceEvent & p = pending_;
    time = p.image;

    p.offset = nextLine();

    if (p.offset <= 0) {
      // We reached the beginning of the file
      pending_.time = TIME_EOF;
      // Reset the last event time tracker
      last_event_time_ = 0.0;
      //fprintf(stderr, "EOF Reached.\n");
      return;
    }

    // -----------------------------------------------
    // This -t * line skipping is a hack to
    // enable the wireless code to work properly.
    // All the -t * lines are parsed in tcl files
    // before the animation starts and reparsing them
    // seems to screw up the wireless stuff.
    // ------------------------------------------------
    
    // Skip over -t * lines
    while (*time != '\0') {
      // Find the -t flag
      if (*time == '-') {
        time++;
        if (*time == 't') {
          // Eat the whitespace
          time++;
          while (*time == ' ' || *time == '\t') {
            time++;
          } 

          // We should be pointing to the
          // -t flag value now
          if (*time == '*') {
            break;
          }
        }
      } else {
        time++;
      }
    }
    
    if (*time == '*') {
      // Notify user if their tracefile has initialization events that
      // are mixed in with animation events
      switch (direction_) {
        case FORWARDS:
          // last_event_time_ is initialized to 0.0 since events cannot happen before this time.
          if (last_event_time_ != 0.0) {
            fprintf(stderr, "Warning: Initialization event is mixed in with animation events.\n");
            fprintf(stderr, "%s", p.image);
            fprintf(stderr, "The above event should occur before all -t <time> animation events.\n\n");
            fprintf(stderr, "last_time_event_ %f\n", last_event_time_);
          }
          break;
      }
      last_event_time_ = 0.0;

      if (p.offset <= 0) {
	      pending_.time = TIME_EOF;
	      //fprintf(stderr, "EOF Reached.\n");
	      return;
      }
      continue;
    } 

    if (parse_table_->parseLine(p.image)) {
      switch (direction_) {
        // Notify user if their tracefile has records that are out of order
        case FORWARDS:
          if (pending_.time < last_event_time_) {
            fprintf(stderr, "Warning: Tracefile events are not sorted by time.\n");
            fprintf(stderr, "%s", p.image);
            fprintf(stderr, "The above event should occur at or after -t %f.\n\n",
                            last_event_time_);
          }
          break;
        case BACKWARDS:
          if (pending_.time > last_event_time_ && last_event_time_ != 0.0) {
            fprintf(stderr, "Warning: Tracefile events are not sorted by time.\n");
            fprintf(stderr, "%s", p.image);
            fprintf(stderr, "The above event should occur at or before -t %f.\n\n",
                            last_event_time_);
          }
          break;
      }
      last_event_time_ = pending_.time;
      return;

    } else {
      fprintf(stderr, "Parsing error in event.\n");
    }
    
  } // end while
  /* NOTREACHED */
}



//----------------------------------------------------------------------
// void
// Trace::findLastLine()
//   - Go to the specified trace file's last line, verify that
//     the file isn't empty and that its last line is correctly
//     formatted. Compute the time interval for the entire trace
//     assuming it started at time 0.
//----------------------------------------------------------------------
void Trace::findLastLine() {
  off_t pos;

  // should initialize it
  direction_ = FORWARDS;
  pending_.time = 0.0;
  pending_.image[0] = 0;

  /*
   * Fake it?
   */
  if (!nam_stream_->seekable()) {
    maxtime_ = 0.0;
    return;
  }

  /*
   * If we can, find the end of time.
   */
  char buf[TRACE_LINE_MAXLEN+1];

  /* is there stuff in the file */
  nam_stream_->seek(0, SEEK_END);
  if (nam_stream_->tell() <= 1) {
    fprintf(stderr, "nam: empty trace file `%s'.", fileName_);
    exit(1);
  }

  /*
   * If the file is larger than 'buf', go to the end of the file
   * at the point just large enough to fit into 'buf'.
   */
  if (nam_stream_->tell() > (off_t)(sizeof(buf) - 1)) {
    pos = nam_stream_->seek(- (sizeof(buf) - 1), SEEK_CUR);
    assert(pos != -1);
  } else 
    /* If the file is smaller than 'buf', go to the head of file */
    nam_stream_->seek(0, SEEK_SET);

  int n = nam_stream_->read(buf, sizeof(buf) - 1);
  if (n < 0) {
    perror("read");
    exit(1);
  }
  buf[n] = 0;

  /*
   * This next check is bogus, but zlib fails it.
   * Go figure.
   * Fortunately compressed files should not be seekable.
   */
  pos = nam_stream_->tell();
  assert(pos != -1);

  /* Error if the last line doesn't have '\n' in it. */
  char *cp = strrchr(buf, '\n');
  if (cp == 0) {
    fprintf(stderr, "nam: error, missing newline on the last line of `%s'.",
      fileName_);
    exit(1);
  }

  /*
   * Look for the first '\n' and check if the line following
   * it has the correct type, i.e., it starts with any of the
   * characters in [h+-dv].
   */
  *cp = 0;
  cp = strrchr(buf, '\n');
  if (cp == 0 || strchr("h+-dvrRanlfDEGPAmVNXWT", *++cp) == 0) {
    fprintf(stderr, "nam: no dynamic animation events in `%s'.\n", fileName_);
    maxtime_ = 0.0;
    return;
    //fprintf(stderr, "     Unknown event %s\n", buf);
    //exit(1);
  }

  /*
   * Compute the time interval from the beginning of the trace
   * to the end.
   * XXX this should include the duration of the last event but
   *     since we changed the format of 'size' from 'time' to
   *     'bytes', we can no longer figure this out.
   */
  double time = 0.;
  (void)sscanf(++cp, " -t %lg", &time);
  maxtime_ = time;
}

/* Go to the beginning of the file, read the first line and process it. */
void Trace::rewind(long offset)
{
  nam_stream_->seek(offset, SEEK_SET);
  //lineno_ = 0;
  scan();
}

/*
 * Put the specified trace handler to the beginning of the trace 
 * handler list.
 */
void Trace::addHandler(TraceHandler* th)
{
  TraceHandlerList* p = new TraceHandlerList;
  p->th = th;
  p->next = handlers_;
  handlers_ = p;
}

//----------------------------------------------------------------------
// void
// Trace::settime(double now, int timeSliderClicked)
//   - Set the current trace time to 'now'.
//----------------------------------------------------------------------
void Trace::settime(double now, int timeSliderClicked) {
  if (now < now_) {
#ifdef OLDWAY
    for (TraceHandlerList* p = handlers_; p != 0; p = p->next)
      p->th->reset(now);
    StateInfo si;
    State::instance()->get(now, si);
    rewind(si.offset);
#endif
    direction_=BACKWARDS;
  } else {
    direction_=FORWARDS;
  }

  now_ = now;

  /*
   * Scan the trace file until the event occurring at time 'now'
   * is read. For each event scanned, pass it on to all trace
   * handlers in the trace handler list so they can handle
   * them as needed.
   */
  Tcl& tcl = Tcl::instance();
  Tcl_Interp* interp = tcl.interp();
  static char event[128];
  TraceEvent e;

  while (ReadEvent(now, e)) {
     //if (e.time == TIME_BOF) {
     if (pending_.time == TIME_BOF) {
       // Reached the beginning of file during backtracking
       // Rewind to the beginning
       direction_ = FORWARDS;
       rewind(0);
       continue;
     }

    if (skipahead_mode_) {
      count_ = 0;
      skipahead_mode_ = 0;
      tcl.evalf("%s normalspeed", nam()->name());
    }
     
    // Find Handler for this pending event?
    for (TraceHandlerList* p = handlers_; p != 0; p = p->next)
      p->th->handle(e, now, direction_);

    if (e.tt == 'h' && (e.pe.src == 0 || e.pe.dst == 0)) {
      sprintf(event, "%d %d %.6f %d/", e.pe.src, e.pe.dst, 
                                       e.time, e.pe.pkt.id);
      if (timeSliderClicked)
        tcl.result((const char *)event);
      else
        Tcl_AppendResult(interp, event, 0);
    }
  }

  for (TraceHandlerList* p = handlers_; p != 0; p = p->next)
    p->th->update(now);

  if (Packet::count() == 0 && ++count_ == 2) {
    count_ = 0;
    skipahead_mode_ = 1;
    tcl.evalf("%s speedup %g", nam()->name(), pending_.time);
  }

}

/*
 * Process 'nam trace' commands:
 *   mintime - return the trace start time
 *   maxtime - return the trace duration
 *   connect - add the specified trace handler to the trace handler list
 *   settime - reset current time to specified time and adjust the
 *             display as needed
 *   nxtevent - read next event in trace file and animate from that point
 *   reset    - reset nam tracefile reading
 */
int Trace::command(int argc, const char* const* argv)
{
  Tcl& tcl = Tcl::instance();

  if (argc == 2) {
    if (strcmp(argv[1], "mintime") == 0) {
      char* bp = tcl.buffer();
      sprintf(bp, "%g", mintime_);
      tcl.result(bp);
      return (TCL_OK);
    } else if (strcmp(argv[1], "maxtime") == 0) {
      char* bp = tcl.buffer();
      sprintf(bp, "%g", maxtime_);
      tcl.result(bp);
      return (TCL_OK);
    } else if (strcmp(argv[1], "nxtevent") == 0) {
      char* bp = tcl.buffer();
      sprintf(bp, "%g", pending_.time);
      tcl.result(bp);
      return (TCL_OK);
    } else if (strcmp(argv[1], "reset") == 0) {
      nam_stream_->close();
      nam_stream_ = NULL;
      nam_stream_ = NamStream::open(fileName_);
      if (nam_stream_->is_ok()) {
        delete nam_stream_;
        nam_stream_ = NULL;
        perror("nam: fdopen");
        // exit(1);
        return TCL_ERROR; // tr->valid() will check this
      }
                        return (TCL_OK);
    }
  }
  if (argc == 3) {
    if (strcmp(argv[1], "connect") == 0) {
      /*
       * Get the handler for the specified trace and
       * add it to the trace's list of handlers.
       */
      TraceHandler* th = (TraceHandler*)TclObject::lookup(argv[2]);
      if (th == 0) {
        tcl.resultf("nam connect: no such trace %s",
              argv[2]);
        return (TCL_ERROR);
      }
      addHandler(th);
      return (TCL_OK);
    }
  }
  if (argc == 4 ) {
    if (strcmp(argv[1], "settime") == 0) {
      /*
       * Set current time to the specified time and update
       * the display to reflect the new current time.
       */
      double now = atof(argv[2]);
      int timeSliderClicked = atoi(argv[3]);
      settime(now, timeSliderClicked);
      return (TCL_OK);
    }
  }
  return (TclObject::command(argc, argv));
}