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