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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <netinet/in.h>
21 #include <sys/types.h>
24 #include <sys/fcntl.h>
28 #include <vppinfra/clib.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/hash.h>
37 char *time_format = "%.03d:%.02d:%.02d:%.03d:%.03d ";
38 static char version[] = "cpelstate 2.0h";
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)
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 */
50 u32 state_event_code = 1; /* default: XR thread-on-cpu */
51 int exclude_kernel_from_summary_stats=1;
52 int summary_stats_only;
62 sort_t sort_type = SORT_MAX_TIME;
64 int widest_name_format=5;
65 int widest_track_format=5;
67 typedef struct bound_event_ {
74 bound_event_t *bound_events;
76 typedef struct bound_track_ {
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;
85 bound_track_t *bound_tracks;
89 fprintf(stderr, "%s", s);
99 int (*pass1)(cpel_section_header_t *, int, FILE *);
100 int (*pass2)(cpel_section_header_t *, int, FILE *);
101 } section_processor_t;
103 int bad_section(cpel_section_header_t *sh, int verbose, FILE *ofp)
105 fprintf(ofp, "Bad (type 0) section, skipped...\n");
109 int noop_pass(cpel_section_header_t *sh, int verbose, FILE *ofp)
114 int strtab_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
117 u8 *strtab_data_area = (u8 *)(sh+1);
119 /* Multiple string tables with the same name are Bad... */
120 p = hash_get_mem(the_strtab_hash, strtab_data_area);
122 fprintf(ofp, "Duplicate string table name %s", strtab_data_area);
125 * Looks funny, but we really do want key = first string in the
126 * table, value = address(first string in the table)
128 hash_set_mem(the_strtab_hash, strtab_data_area, strtab_data_area);
130 fprintf(ofp, "String Table %s\n", strtab_data_area);
135 int evtdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
138 event_definition_section_header_t *edh;
139 event_definition_t *ep;
146 edh = (event_definition_section_header_t *)(sh+1);
147 nevents = ntohl(edh->number_of_event_definitions);
150 fprintf(ofp, "Event Definition Section: %d definitions\n",
154 p = hash_get_mem(the_strtab_hash, edh->string_table_name);
156 fprintf(ofp, "Fatal: couldn't find string table\n");
159 this_strtab = (u8 *)p[0];
161 ep = (event_definition_t *)(edh+1);
163 for (i = 0; i < nevents; i++) {
164 event_code = ntohl(ep->event);
165 p = hash_get(the_evtdef_hash, event_code);
167 fprintf(ofp, "Event %d redefined, retain first definition\n",
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 */
181 for (j = 0; j < strlen((char *)(bp->datum_str)); j++) {
182 if (bp->datum_str[j] == '%'){
186 if (seen_percent && bp->datum_str[j] == 's') {
187 bp->is_strtab_ref = 1;
192 hash_set(the_evtdef_hash, event_code, bp - bound_events);
194 thislen = strlen((char *)bp->event_str);
195 if (thislen > widest_name_format)
196 widest_name_format = thislen;
203 int trackdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
206 track_definition_section_header_t *tdh;
207 track_definition_t *tp;
214 tdh = (track_definition_section_header_t *)(sh+1);
215 nevents = ntohl(tdh->number_of_track_definitions);
218 fprintf(ofp, "Track Definition Section: %d definitions\n",
222 p = hash_get_mem(the_strtab_hash, tdh->string_table_name);
224 fprintf(ofp, "Fatal: couldn't find string table\n");
227 this_strtab = (u8 *)p[0];
229 tp = (track_definition_t *)(tdh+1);
231 for (i = 0; i < nevents; i++) {
232 track_code = ntohl(tp->track);
233 p = hash_get(the_trackdef_hash, track_code);
235 fprintf(ofp, "track %d redefined, retain first definition\n",
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);
244 thislen = strlen((char *)(btp->track_str));
245 if (thislen > widest_track_format)
246 widest_track_format = thislen;
252 int unsupported_pass (cpel_section_header_t *sh, int verbose, FILE *ofp)
255 fprintf(ofp, "Unsupported type %d section\n",
256 ntohl(sh->section_type));
261 int event_pass2(cpel_section_header_t *sh, int verbose, FILE *ofp)
263 event_section_header_t *eh;
271 bound_track_t generic_track;
273 u64 state_start_ticks=0;
275 bound_track_t *state_track=0;
277 generic_track.track_str = (u8 *) "%d";
278 last_track_code = 0xdeafbeef;
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;
285 fprintf(ofp, "%.3f ticks_per_us\n", ticks_per_us);
288 ep = (event_entry_t *)(eh+1);
290 p = hash_get_mem(the_strtab_hash, eh->string_table_name);
292 fprintf(ofp, "Fatal: couldn't find string table\n");
296 for (i = 0; i < nevents; i++) {
297 time0 = ntohl (ep->time[0]);
298 time1 = ntohl (ep->time[1]);
300 now = (((u64) time0)<<32) | time1;
302 /* Found the state-change event ? */
303 if (ntohl(ep->event_code) == state_event_code) {
305 * Add a ticks-in-state record, unless
306 * this is the "prime mover" event instance
309 ticks_in_state = now - state_start_ticks;
310 vec_add1(state_track->ticks_in_state, ticks_in_state);
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);
318 state_track = &bound_tracks[p[0]];
320 state_track = &generic_track;
322 last_track_code = track_code;
332 * Note: If necessary, add passes / columns to this table to
333 * handle section order dependencies.
336 section_processor_t processors[CPEL_NUM_SECTION_TYPES+1] =
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 */
347 int process_section(cpel_section_header_t *sh, int verbose, FILE *ofp,
351 type = ntohl(sh->section_type);
353 int (*fp)(cpel_section_header_t *, int, FILE *);
355 if (type > CPEL_NUM_SECTION_TYPES) {
356 fprintf(stderr, "Unknown section type %d\n", type);
361 fp = processors[type].pass1;
365 fp = processors[type].pass2;
369 fprintf(stderr, "Unknown pass %d\n", pass);
373 rv = (*fp)(sh, verbose, ofp);
378 int cpel_dump_file_header(cpel_file_header_t *fh, int verbose, FILE *ofp)
383 fprintf(ofp, "CPEL file: %s-endian, version %d\n",
384 ((fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) ?
386 fh->endian_version & CPEL_FILE_VERSION_MASK);
388 file_time = ntohl(fh->file_date);
390 fprintf(ofp, "File created %s", ctime(&file_time));
391 fprintf(ofp, "File has %d sections\n",
392 ntohs(fh->nsections));
399 int cpel_dump(u8 *cpel, int verbose, FILE *ofp)
401 cpel_file_header_t *fh;
402 cpel_section_header_t *sh;
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");
414 fprintf(stderr, "Unsupported file version 0x%x\n",
418 cpel_dump_file_header(fh, verbose, ofp);
419 nsections = ntohs(fh->nsections);
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.
426 sh = (cpel_section_header_t *)(fh+1);
427 for (i = 0; i < nsections; i++) {
428 section_size = ntohl(sh->data_length);
431 fprintf(ofp, "Section type %d, size %d\n", ntohl(sh->section_type),
435 if(process_section(sh, verbose, ofp, PASS1))
439 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
442 sh = (cpel_section_header_t *)(fh+1);
443 for (i = 0; i < nsections; i++) {
444 if(process_section(sh, verbose, ofp, PASS2))
446 section_size = ntohl(sh->data_length);
448 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
453 void compute_state_statistics(int verbose, FILE *ofp)
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];
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));
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);
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);
488 int track_compare_max (const void *arg1, const void *arg2)
490 bound_track_t *a1 = (bound_track_t *)arg1;
491 bound_track_t *a2 = (bound_track_t *)arg2;
494 v1 = a1->total_ticks_in_state;
495 v2 = a2->total_ticks_in_state;
504 int track_compare_occurrences (const void *arg1, const void *arg2)
506 bound_track_t *a1 = (bound_track_t *)arg1;
507 bound_track_t *a2 = (bound_track_t *)arg2;
510 v1 = (f64) vec_len(a1->ticks_in_state);
511 v2 = (f64) vec_len(a2->ticks_in_state);
520 int track_compare_name (const void *arg1, const void *arg2)
522 bound_track_t *a1 = (bound_track_t *)arg1;
523 bound_track_t *a2 = (bound_track_t *)arg2;
525 return (strcmp((char *)(a1->track_str), (char *)(a2->track_str)));
528 void sort_state_statistics(sort_t type, FILE *ofp)
530 int (*compare)(const void *, const void *)=0;
532 if (summary_stats_only)
537 fprintf(ofp, "Results sorted by max time in state.\n");
538 compare = track_compare_max;
541 case SORT_MAX_OCCURRENCES:
542 fprintf(ofp, "Results sorted by max occurrences of state.\n");
543 compare = track_compare_occurrences;
547 compare = track_compare_name;
548 fprintf(ofp, "Results sorted by process-id/name/thread ID\n");
552 fatal("sort type not set?");
555 qsort (bound_tracks, vec_len(bound_tracks),
556 sizeof (bound_track_t), compare);
559 void print_state_statistics(int verbose, FILE *ofp)
564 f64 total_time = 0.0;
565 f64 total_switches = 0.0;
567 trackpad = format(0, "%%-%ds ", widest_track_format);
568 vec_add1(trackpad, 0);
570 if (!summary_stats_only) {
571 fprintf(ofp, (char *)trackpad, "ProcThread");
572 fprintf(ofp, " Mean(us) Stdev(us) Total(us) N\n");
575 for (i = 0; i < vec_len(bound_tracks); i++) {
576 bp = &bound_tracks[i];
577 if (bp->mean_ticks_in_state == 0.0)
581 strncmp((char *)(bp->track_str), (char *)name_filter,
582 strlen((char *)name_filter)))
586 * Exclude kernel threads (e.g. idle thread) from
589 if (exclude_kernel_from_summary_stats &&
590 !strncmp((char *)(bp->track_str), "kernel ", 7))
593 total_switches += (f64) vec_len(bp->ticks_in_state);
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));
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);
613 total_time += bp->total_ticks_in_state;
616 if (!summary_stats_only)
618 fprintf(ofp, "Note: the following statistics %s kernel-thread activity.\n",
619 exclude_kernel_from_summary_stats ? "exclude" : "include");
622 "Note: only pid/proc/threads matching '%s' are included.\n",
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);
632 char *mapfile (char *file)
639 maphfile = open (file, O_RDONLY);
643 fprintf (stderr, "Couldn't read %s, skipping it...\n", file);
647 if (fstat (maphfile, &statb) < 0)
649 fprintf (stderr, "Couldn't get size of %s, skipping it...\n", file);
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);
659 mapfsize = statb.st_size;
663 fprintf (stderr, "%s zero-length, skipping it...\n", file);
668 rv = mmap (0, mapfsize, PROT_READ, MAP_SHARED, maphfile, 0);
672 fprintf (stderr, "%s problem mapping, I quit...\n", file);
682 int main (int argc, char **argv)
685 char *outputfile = 0;
691 while (curarg < argc) {
692 if (!strncmp(argv[curarg], "--input-file", 3)) {
695 cpel_file = argv[curarg];
699 fatal("Missing filename after --input-file\n");
701 if (!strncmp(argv[curarg], "--output-file", 3)) {
704 outputfile = argv[curarg];
708 fatal("Missing filename after --output-file\n");
710 if (!strncmp(argv[curarg], "--verbose", 3)) {
715 if (!strncmp(argv[curarg], "--scatterplot", 4)) {
721 if (!strncmp(argv[curarg], "--state-event", 4)) {
724 state_event_code = atol(argv[curarg]);
728 fatal("Missing integer after --state-event\n");
730 if (!strncmp(argv[curarg], "--max-time-sort", 7)) {
731 sort_type = SORT_MAX_TIME;
735 if (!strncmp(argv[curarg], "--max-occurrence-sort", 7)) {
736 sort_type = SORT_MAX_OCCURRENCES;
740 if (!strncmp(argv[curarg], "--name-sort", 3)) {
741 sort_type = SORT_NAME;
745 if (!strncmp(argv[curarg], "--kernel-included", 3)) {
746 exclude_kernel_from_summary_stats = 0;
750 if (!strncmp(argv[curarg], "--summary", 3)) {
751 summary_stats_only=1;
755 if (!strncmp(argv[curarg], "--filter", 3)) {
758 name_filter = (u8 *)argv[curarg];
762 fatal("Missing filter string after --filter\n");
768 "cpelstate --input-file <filename> [--output-file <filename>]\n");
770 " [--state-event <decimal>] [--verbose]\n");
772 " [--max-time-sort(default) | --max-occurrence-sort |\n");
775 " --name-sort-sort] [--kernel-included]\n");
778 " [--summary-stats-only] [--scatterplot]\n");
780 fprintf(stderr, "%s\n", version);
787 cpel = mapfile(cpel_file);
789 fprintf(stderr, "Couldn't map %s...\n", cpel_file);
794 ofp = fdopen(1, "w");
796 fprintf(stderr, "Couldn't fdopen(1)?\n");
800 ofp = fopen(outputfile, "w");
802 fprintf(stderr, "Couldn't create %s...\n", outputfile);
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));
811 if (cpel_dump((u8 *) cpel, verbose, ofp)) {
816 compute_state_statistics(verbose, ofp);
817 sort_state_statistics(sort_type, ofp);
818 print_state_statistics(verbose, ofp);