dpdk: Add support for Mellanox ConnectX-4 devices
[vpp.git] / perftool / cpelstate.c
1 /* 
2  *------------------------------------------------------------------
3  * Copyright (c) 2006-2016 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <netinet/in.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <sys/fcntl.h>
25 #include <sys/mman.h>
26 #include <unistd.h>
27 #include <ctype.h>
28 #include <vppinfra/clib.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/hash.h>
31 #include <pwd.h>
32 #include <stdarg.h>
33 #include <time.h>
34 #include "cpel.h"
35 #include <math.h>
36
37 char *time_format = "%.03d:%.02d:%.02d:%.03d:%.03d ";
38 static char version[] = "cpelstate 2.0h";
39
40 #define USEC_PER_MS 1000LL
41 #define USEC_PER_SECOND (1000*USEC_PER_MS)
42 #define USEC_PER_MINUTE (60*USEC_PER_SECOND)
43 #define USEC_PER_HOUR (60*USEC_PER_MINUTE)
44
45 uword *the_strtab_hash; /* (name, base-VA) hash of all string tables */
46 uword *the_evtdef_hash; /* (event-id, event-definition) hash */
47 uword *the_trackdef_hash; /* (track-id, track-definition) hash */
48
49 f64 ticks_per_us;
50 u32 state_event_code = 1;       /* default: XR thread-on-cpu */
51 int exclude_kernel_from_summary_stats=1;
52 int summary_stats_only;
53 int scatterplot;
54 u8 *name_filter;
55
56 typedef enum {
57     SORT_MAX_TIME=1,
58     SORT_MAX_OCCURRENCES,
59     SORT_NAME,
60 } sort_t;
61
62 sort_t sort_type = SORT_MAX_TIME;
63
64 int widest_name_format=5;
65 int widest_track_format=5;
66
67 typedef struct bound_event_ {
68     u32 event_code;
69     u8  *event_str;
70     u8  *datum_str;
71     u32  is_strtab_ref;
72 } bound_event_t;
73
74 bound_event_t *bound_events;
75
76 typedef struct bound_track_ {
77     u32 track;
78     u8  *track_str;
79     u64 *ticks_in_state; /* vector of state occurrences */
80     f64  mean_ticks_in_state;
81     f64  variance_ticks_in_state;
82     f64  total_ticks_in_state;
83 } bound_track_t;
84
85 bound_track_t *bound_tracks;
86
87 void fatal(char *s)
88 {
89     fprintf(stderr, "%s", s);
90     exit(1);
91 }
92
93 typedef enum {
94     PASS1=1,
95     PASS2=2,
96 } pass_t;
97
98 typedef struct {
99     int (*pass1)(cpel_section_header_t *, int, FILE *);
100     int (*pass2)(cpel_section_header_t *, int, FILE *);
101 } section_processor_t;
102
103 int bad_section(cpel_section_header_t *sh, int verbose, FILE *ofp)
104 {
105     fprintf(ofp, "Bad (type 0) section, skipped...\n");
106     return(0);
107 }
108
109 int noop_pass(cpel_section_header_t *sh, int verbose, FILE *ofp)
110 {
111     return(0);
112 }
113
114 int strtab_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
115 {
116     uword *p;
117     u8 *strtab_data_area = (u8 *)(sh+1);
118     
119     /* Multiple string tables with the same name are Bad... */
120     p = hash_get_mem(the_strtab_hash, strtab_data_area);
121     if (p) {
122         fprintf(ofp, "Duplicate string table name %s", strtab_data_area);
123     }
124     /*
125      * Looks funny, but we really do want key = first string in the
126      * table, value = address(first string in the table) 
127      */
128     hash_set_mem(the_strtab_hash, strtab_data_area, strtab_data_area);
129     if (verbose) {
130         fprintf(ofp, "String Table %s\n", strtab_data_area);
131     }
132     return(0);
133 }
134
135 int evtdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
136 {
137     int i, nevents;
138     event_definition_section_header_t *edh;
139     event_definition_t *ep;
140     u8 *this_strtab;
141     u32 event_code;
142     uword *p;
143     bound_event_t *bp;
144     int thislen;
145
146     edh = (event_definition_section_header_t *)(sh+1);
147     nevents = ntohl(edh->number_of_event_definitions);
148     
149     if (verbose) {
150         fprintf(ofp, "Event Definition Section: %d definitions\n",
151                 nevents);
152     }
153
154     p = hash_get_mem(the_strtab_hash, edh->string_table_name);
155     if (!p) {
156         fprintf(ofp, "Fatal: couldn't find string table\n");
157         return(1);
158     }
159     this_strtab = (u8 *)p[0];
160
161     ep = (event_definition_t *)(edh+1);
162     
163     for (i = 0; i < nevents; i++) {
164         event_code = ntohl(ep->event);
165         p = hash_get(the_evtdef_hash, event_code);
166         if (p) {
167             fprintf(ofp, "Event %d redefined, retain first definition\n",
168                     event_code);
169             continue;
170         }
171         vec_add2(bound_events, bp, 1);
172         bp->event_code = event_code;
173         bp->event_str = this_strtab + ntohl(ep->event_format);
174         bp->datum_str = this_strtab + ntohl(ep->datum_format);
175         bp->is_strtab_ref = 0;
176         /* Decide if the datum format is a %s format => strtab reference */
177         {
178             int j;
179             int seen_percent=0;
180
181             for (j = 0; j < strlen((char *)(bp->datum_str)); j++) {
182                 if (bp->datum_str[j] == '%'){
183                     seen_percent=1;
184                     continue;
185                 }
186                 if (seen_percent && bp->datum_str[j] == 's') {
187                     bp->is_strtab_ref = 1;
188                 }
189             }
190         }
191         
192         hash_set(the_evtdef_hash, event_code, bp - bound_events);
193
194         thislen = strlen((char *)bp->event_str);
195         if (thislen > widest_name_format)
196             widest_name_format = thislen;
197
198         ep++;
199     }
200     return (0);
201 }
202
203 int trackdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
204 {
205     int i, nevents;
206     track_definition_section_header_t *tdh;
207     track_definition_t *tp;
208     u8 *this_strtab;
209     u32 track_code;
210     uword *p;
211     bound_track_t *btp;
212     int thislen;
213
214     tdh = (track_definition_section_header_t *)(sh+1);
215     nevents = ntohl(tdh->number_of_track_definitions);
216     
217     if (verbose) {
218         fprintf(ofp, "Track Definition Section: %d definitions\n",
219                 nevents);
220     }
221
222     p = hash_get_mem(the_strtab_hash, tdh->string_table_name);
223     if (!p) {
224         fprintf(ofp, "Fatal: couldn't find string table\n");
225         return(1);
226     }
227     this_strtab = (u8 *)p[0];
228
229     tp = (track_definition_t *)(tdh+1);
230     
231     for (i = 0; i < nevents; i++) {
232         track_code = ntohl(tp->track);
233         p = hash_get(the_trackdef_hash, track_code);
234         if (p) {
235             fprintf(ofp, "track %d redefined, retain first definition\n",
236                     track_code);
237             continue;
238         }
239         vec_add2(bound_tracks, btp, 1);
240         btp->track = track_code;
241         btp->track_str = this_strtab + ntohl(tp->track_format);
242         hash_set(the_trackdef_hash, track_code, btp - bound_tracks);
243
244         thislen = strlen((char *)(btp->track_str));
245         if (thislen > widest_track_format)
246             widest_track_format = thislen;
247         tp++;
248     }
249     return (0);
250 }
251
252 int unsupported_pass (cpel_section_header_t *sh, int verbose, FILE *ofp)
253 {
254     if (verbose) {
255         fprintf(ofp, "Unsupported type %d section\n",
256                 ntohl(sh->section_type));
257     }
258     return(0);
259 }
260
261 int event_pass2(cpel_section_header_t *sh, int verbose, FILE *ofp)
262 {
263     event_section_header_t *eh;
264     u32 track_code;
265     int nevents;
266     int i;
267     uword *p;
268     event_entry_t *ep;
269     u64 now;
270     u32 time0, time1;
271     bound_track_t generic_track;
272     u32 last_track_code;
273     u64 state_start_ticks=0;
274     u64 ticks_in_state;
275     bound_track_t *state_track=0;
276     int in_state=0;
277     generic_track.track_str = (u8 *) "%d";
278     last_track_code = 0xdeafbeef;
279
280     eh = (event_section_header_t *)(sh+1);
281     nevents = ntohl(eh->number_of_events);
282     ticks_per_us = ((double)ntohl(eh->clock_ticks_per_second))/1e6;
283
284     if (verbose) {
285         fprintf(ofp, "%.3f ticks_per_us\n", ticks_per_us);
286     }
287
288     ep = (event_entry_t *)(eh+1);
289
290     p = hash_get_mem(the_strtab_hash, eh->string_table_name);
291     if (!p) {
292         fprintf(ofp, "Fatal: couldn't find string table\n");
293         return(1);
294     }
295
296     for (i = 0; i < nevents; i++) {
297         time0 = ntohl (ep->time[0]);
298         time1 = ntohl (ep->time[1]);
299
300         now = (((u64) time0)<<32) | time1;
301
302         /* Found the state-change event ? */
303         if (ntohl(ep->event_code) == state_event_code) {
304             /*
305              * Add a ticks-in-state record, unless
306              * this is the "prime mover" event instance 
307              */
308             if (in_state) {
309                 ticks_in_state = now - state_start_ticks;
310                 vec_add1(state_track->ticks_in_state, ticks_in_state);
311             }
312             /* switch to now-current track */
313             state_start_ticks = now;
314             track_code = ntohl(ep->track);
315             if (track_code != last_track_code) {
316                 p = hash_get(the_trackdef_hash, track_code);
317                 if (p) {
318                     state_track = &bound_tracks[p[0]];
319                 } else {
320                     state_track = &generic_track;
321                 }
322                 last_track_code = track_code;
323             }
324             in_state = 1;
325         }
326         ep++;
327     }
328     return(0);
329 }
330
331 /* 
332  * Note: If necessary, add passes / columns to this table to 
333  * handle section order dependencies.
334  */
335
336 section_processor_t processors[CPEL_NUM_SECTION_TYPES+1] =
337 {
338     {bad_section,       noop_pass},             /* type 0 -- f**ked */
339     {strtab_pass1,      noop_pass},             /* type 1 -- STRTAB */
340     {unsupported_pass,  noop_pass},             /* type 2 -- SYMTAB */
341     {evtdef_pass1,      noop_pass},             /* type 3 -- EVTDEF */
342     {trackdef_pass1,    noop_pass},             /* type 4 -- TRACKDEF */
343     {noop_pass,         event_pass2},           /* type 5 -- EVENTS */
344 };
345
346
347 int process_section(cpel_section_header_t *sh, int verbose, FILE *ofp,
348                     pass_t pass)
349 {
350     u32 type;
351     type = ntohl(sh->section_type);
352     int rv;
353     int (*fp)(cpel_section_header_t *, int, FILE *);
354
355     if (type > CPEL_NUM_SECTION_TYPES) {
356         fprintf(stderr, "Unknown section type %d\n", type);
357         return(1);
358     }
359     switch(pass) {
360     case PASS1:
361         fp = processors[type].pass1;
362         break;
363
364     case PASS2:
365         fp = processors[type].pass2;
366         break;
367         
368     default:
369         fprintf(stderr, "Unknown pass %d\n", pass);
370         return(1);
371     }
372
373     rv = (*fp)(sh, verbose, ofp);
374
375     return(rv);
376 }
377
378 int cpel_dump_file_header(cpel_file_header_t *fh, int verbose, FILE *ofp)
379 {
380     time_t file_time;
381
382     if (verbose) {
383         fprintf(ofp, "CPEL file: %s-endian, version %d\n",
384                 ((fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) ? 
385                  "little" : "big"), 
386                 fh->endian_version & CPEL_FILE_VERSION_MASK);
387
388         file_time = ntohl(fh->file_date);
389         
390         fprintf(ofp, "File created %s", ctime(&file_time));
391         fprintf(ofp, "File has %d sections\n", 
392                 ntohs(fh->nsections));
393     }
394
395     return(0);
396 }
397
398
399 int cpel_dump(u8 *cpel, int verbose, FILE *ofp)
400 {
401     cpel_file_header_t *fh;
402     cpel_section_header_t *sh;
403     u16 nsections;
404     u32 section_size;
405     int i;
406
407     /* First, the file header */
408     fh = (cpel_file_header_t *)cpel;
409     if (fh->endian_version != CPEL_FILE_VERSION) {
410         if (fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) {
411             fprintf(stderr, "Little endian data format not supported\n");
412             return(1);
413         }
414         fprintf(stderr, "Unsupported file version 0x%x\n", 
415                 fh->endian_version);
416         return(1);
417     }
418     cpel_dump_file_header(fh, verbose, ofp);
419     nsections = ntohs(fh->nsections);
420
421     /*
422      * Take two passes through the file. PASS1 builds
423      * data structures, PASS2 actually dumps the file.
424      * Just in case the sections are in an unobvious order.
425      */
426     sh = (cpel_section_header_t *)(fh+1);
427     for (i = 0; i < nsections; i++) {
428         section_size = ntohl(sh->data_length);
429
430         if(verbose) {
431             fprintf(ofp, "Section type %d, size %d\n", ntohl(sh->section_type),
432                     section_size);
433         }
434
435         if(process_section(sh, verbose, ofp, PASS1))
436             return(1);
437
438         sh++;
439         sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
440     }
441
442     sh = (cpel_section_header_t *)(fh+1);
443     for (i = 0; i < nsections; i++) {
444         if(process_section(sh, verbose, ofp, PASS2))
445             return(1);
446         section_size = ntohl(sh->data_length);
447         sh++;
448         sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
449     }
450     return(0);
451 }
452
453 void compute_state_statistics(int verbose, FILE *ofp)
454 {
455     int i, j;
456     bound_track_t *bp;
457     f64 fticks;
458
459     /* Across the bound tracks */
460     for (i = 0; i < vec_len(bound_tracks); i++) {
461         bp = &bound_tracks[i];
462         bp->mean_ticks_in_state = 0.0;
463         bp->variance_ticks_in_state = 0.0;
464         bp->total_ticks_in_state = 0.0;
465         for (j = 0; j < vec_len(bp->ticks_in_state); j++) {
466             bp->total_ticks_in_state += (f64) bp->ticks_in_state[j];
467         }
468         /* Compute mean */
469         if (vec_len(bp->ticks_in_state)) {
470             bp->mean_ticks_in_state = bp->total_ticks_in_state / 
471                 ((f64) vec_len(bp->ticks_in_state));
472         }
473         /* Accumulate sum: (Xi-Xbar)**2 */
474         for (j = 0; j < vec_len(bp->ticks_in_state); j++) {
475             fticks = bp->ticks_in_state[j];
476             bp->variance_ticks_in_state += 
477                 (fticks - bp->mean_ticks_in_state)*
478                 (fticks - bp->mean_ticks_in_state);
479         }
480         /* Compute s**2, the unbiased estimator of sigma**2 */
481         if (vec_len(bp->ticks_in_state) > 1) {
482             bp->variance_ticks_in_state /= (f64) 
483                 (vec_len(bp->ticks_in_state)-1);
484         }
485     }
486 }
487
488 int track_compare_max (const void *arg1, const void *arg2)
489 {
490     bound_track_t *a1 = (bound_track_t *)arg1;
491     bound_track_t *a2 = (bound_track_t *)arg2;
492     f64 v1, v2;
493
494     v1 = a1->total_ticks_in_state;
495     v2 = a2->total_ticks_in_state;
496     
497     if (v1 < v2)
498         return (1);
499     else if (v1 == v2)
500         return (0);
501     else return (-1);
502 }
503
504 int track_compare_occurrences (const void *arg1, const void *arg2)
505 {
506     bound_track_t *a1 = (bound_track_t *)arg1;
507     bound_track_t *a2 = (bound_track_t *)arg2;
508     f64 v1, v2;
509
510     v1 = (f64) vec_len(a1->ticks_in_state);
511     v2 = (f64) vec_len(a2->ticks_in_state);
512     
513     if (v1 < v2)
514         return (1);
515     else if (v1 == v2)
516         return (0);
517     else return (-1);
518 }
519
520 int track_compare_name (const void *arg1, const void *arg2)
521 {
522     bound_track_t *a1 = (bound_track_t *)arg1;
523     bound_track_t *a2 = (bound_track_t *)arg2;
524
525     return (strcmp((char *)(a1->track_str), (char *)(a2->track_str)));
526 }
527
528 void sort_state_statistics(sort_t type, FILE *ofp)
529 {
530     int (*compare)(const void *, const void *)=0;
531
532     if (summary_stats_only)
533         return;
534
535     switch(type) {
536     case SORT_MAX_TIME:
537         fprintf(ofp, "Results sorted by max time in state.\n");
538         compare = track_compare_max;
539         break;
540
541     case SORT_MAX_OCCURRENCES:
542         fprintf(ofp, "Results sorted by max occurrences of state.\n");
543         compare = track_compare_occurrences;
544         break;
545
546     case SORT_NAME:
547         compare = track_compare_name;
548         fprintf(ofp, "Results sorted by process-id/name/thread ID\n");
549         break;
550
551     default:
552         fatal("sort type not set?");
553     }
554     
555     qsort (bound_tracks, vec_len(bound_tracks), 
556            sizeof (bound_track_t), compare);    
557 }
558
559 void print_state_statistics(int verbose, FILE *ofp)
560 {
561     int i,j;
562     u8 *trackpad;
563     bound_track_t *bp;
564     f64 total_time = 0.0;
565     f64 total_switches = 0.0;
566
567     trackpad = format(0, "%%-%ds ", widest_track_format);
568     vec_add1(trackpad, 0);
569
570     if (!summary_stats_only) {
571         fprintf(ofp, (char *)trackpad, "ProcThread");
572         fprintf(ofp, "  Mean(us)     Stdev(us)   Total(us)      N\n");
573     }
574         
575     for (i = 0; i < vec_len(bound_tracks); i++) {
576         bp = &bound_tracks[i];
577         if (bp->mean_ticks_in_state == 0.0)
578             continue;
579
580         if (name_filter &&
581             strncmp((char *)(bp->track_str), (char *)name_filter, 
582                     strlen((char *)name_filter)))
583             continue;
584
585         /*
586          * Exclude kernel threads (e.g. idle thread) from
587          * state statistics 
588          */
589         if (exclude_kernel_from_summary_stats && 
590             !strncmp((char *)(bp->track_str), "kernel ", 7))
591             continue;
592
593         total_switches += (f64) vec_len(bp->ticks_in_state);
594         
595         if (!summary_stats_only) {
596             fprintf(ofp, (char *) trackpad, bp->track_str);
597             fprintf(ofp, "%10.3f +- %10.3f", 
598                     bp->mean_ticks_in_state / ticks_per_us,
599                     sqrt(bp->variance_ticks_in_state) 
600                     / (f64) ticks_per_us);
601             fprintf(ofp, "%12.3f", 
602                     bp->total_ticks_in_state / ticks_per_us);
603             fprintf(ofp, "%8d\n", (int)vec_len(bp->ticks_in_state));
604         }
605
606         if (scatterplot) {
607             for (j = 0; j < vec_len(bp->ticks_in_state); j++) {
608                 fprintf(ofp, "%.3f\n", 
609                         (f64)bp->ticks_in_state[j] / ticks_per_us);
610             }
611         }
612
613         total_time += bp->total_ticks_in_state;
614     }
615     
616     if (!summary_stats_only)
617         fprintf(ofp, "\n");
618     fprintf(ofp, "Note: the following statistics %s kernel-thread activity.\n",
619             exclude_kernel_from_summary_stats ? "exclude" : "include");
620     if (name_filter)
621         fprintf(ofp, 
622                 "Note: only pid/proc/threads matching '%s' are included.\n",
623                 name_filter);
624
625     fprintf(ofp, 
626       "Total runtime: %10.3f (us), Total state switches: %.0f\n", 
627             total_time / ticks_per_us, total_switches);
628     fprintf(ofp, "Average time in state: %10.3f (us)\n",
629             (total_time / total_switches) / ticks_per_us);
630 }
631
632 char *mapfile (char *file)
633 {
634     struct stat statb;
635     char *rv;
636     int maphfile;
637     size_t mapfsize;
638     
639     maphfile = open (file, O_RDONLY);
640
641     if (maphfile < 0)
642     {
643         fprintf (stderr, "Couldn't read %s, skipping it...\n", file);
644         return (NULL);
645     }
646
647     if (fstat (maphfile, &statb) < 0)
648     {
649         fprintf (stderr, "Couldn't get size of %s, skipping it...\n", file);
650         return (NULL);
651     }
652
653     /* Don't try to mmap directories, FIFOs, semaphores, etc. */
654     if (! (statb.st_mode & S_IFREG)) {
655         fprintf (stderr, "%s is not a regular file, skipping it...\n", file);
656         return (NULL);
657     }
658
659     mapfsize = statb.st_size;
660
661     if (mapfsize < 3)
662     {
663         fprintf (stderr, "%s zero-length, skipping it...\n", file);
664         close (maphfile);
665         return (NULL);
666     }
667
668     rv = mmap (0, mapfsize, PROT_READ, MAP_SHARED, maphfile, 0);
669
670     if (rv == 0)
671     {
672         fprintf (stderr, "%s problem mapping, I quit...\n", file);
673         exit (-1);
674     }
675     close (maphfile);
676     return (rv);
677 }
678
679 /*
680  * main 
681  */
682 int main (int argc, char **argv)
683 {
684     char *cpel_file = 0;
685     char *outputfile = 0;
686     FILE *ofp;
687     char *cpel;
688     int verbose=0;
689     int curarg=1;
690
691     while (curarg < argc) {
692         if (!strncmp(argv[curarg], "--input-file", 3)) {
693             curarg++;
694             if (curarg < argc) {
695                 cpel_file = argv[curarg];
696                 curarg++;
697                 continue;
698             }
699             fatal("Missing filename after --input-file\n");
700         }
701         if (!strncmp(argv[curarg], "--output-file", 3)) {
702             curarg ++;
703             if (curarg < argc) {
704                 outputfile = argv[curarg];
705                 curarg ++;
706                 continue;
707             }
708             fatal("Missing filename after --output-file\n");
709         }
710         if (!strncmp(argv[curarg], "--verbose", 3)) {
711             curarg++;
712             verbose++;
713             continue;
714         }
715         if (!strncmp(argv[curarg], "--scatterplot", 4)) {
716             curarg++;
717             scatterplot=1;
718             continue;
719         }
720
721         if (!strncmp(argv[curarg], "--state-event", 4)) {
722             curarg++;
723             if (curarg < argc) {
724                 state_event_code = atol(argv[curarg]);
725                 curarg ++;
726                 continue;
727             }
728             fatal("Missing integer after --state-event\n");
729         }
730         if (!strncmp(argv[curarg], "--max-time-sort", 7)) {
731             sort_type = SORT_MAX_TIME;
732             curarg++;
733             continue;
734         }
735         if (!strncmp(argv[curarg], "--max-occurrence-sort", 7)) {
736             sort_type = SORT_MAX_OCCURRENCES;
737             curarg++;
738             continue;
739         }
740         if (!strncmp(argv[curarg], "--name-sort", 3)) {
741             sort_type = SORT_NAME;
742             curarg++;
743             continue;
744         }
745         if (!strncmp(argv[curarg], "--kernel-included", 3)) {
746             exclude_kernel_from_summary_stats = 0;
747             curarg++;
748             continue;
749         }
750         if (!strncmp(argv[curarg], "--summary", 3)) {
751             summary_stats_only=1;
752             curarg++;
753             continue;
754         }
755         if (!strncmp(argv[curarg], "--filter", 3)) {
756             curarg ++;
757             if (curarg < argc) {
758                 name_filter = (u8 *)argv[curarg];
759                 curarg ++;
760                 continue;
761             }
762             fatal("Missing filter string after --filter\n");
763         }
764         
765
766     usage:
767         fprintf(stderr, 
768           "cpelstate --input-file <filename> [--output-file <filename>]\n");
769         fprintf(stderr, 
770           "          [--state-event <decimal>] [--verbose]\n");
771         fprintf(stderr, 
772           "          [--max-time-sort(default) | --max-occurrence-sort |\n");
773
774         fprintf(stderr, 
775           "           --name-sort-sort] [--kernel-included]\n");
776
777         fprintf(stderr, 
778           "          [--summary-stats-only] [--scatterplot]\n");
779
780         fprintf(stderr, "%s\n", version);
781         exit(1);
782     }
783
784     if (cpel_file == 0)
785         goto usage;
786
787     cpel = mapfile(cpel_file);
788     if (cpel == 0) {
789         fprintf(stderr, "Couldn't map %s...\n", cpel_file);
790         exit(1);
791     }
792
793     if (!outputfile) {
794         ofp = fdopen(1, "w");
795         if (ofp == NULL) {
796             fprintf(stderr, "Couldn't fdopen(1)?\n");
797             exit(1);
798         }
799     } else {
800         ofp = fopen(outputfile, "w");
801         if (ofp == NULL) {
802             fprintf(stderr, "Couldn't create %s...\n", outputfile);
803             exit(1);
804         }
805     }
806
807     the_strtab_hash = hash_create_string (0, sizeof (uword));
808     the_evtdef_hash = hash_create (0, sizeof (uword));
809     the_trackdef_hash = hash_create (0, sizeof (uword));
810
811     if (cpel_dump((u8 *) cpel, verbose, ofp)) {
812         if (outputfile)
813             unlink(outputfile);
814     }
815
816     compute_state_statistics(verbose, ofp);
817     sort_state_statistics(sort_type, ofp);
818     print_state_statistics(verbose, ofp);
819
820     fclose(ofp);
821     return(0);
822 }