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>
27 #include <vppinfra/clib.h>
28 #include <vppinfra/vec.h>
29 #include <vppinfra/hash.h>
36 char *time_format = "%.03d:%.02d:%.02d:%.03d:%.03d ";
37 static char version[] = "cpelstate 2.0h";
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)
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 */
49 u32 state_event_code = 1; /* default: XR thread-on-cpu */
50 int exclude_kernel_from_summary_stats=1;
51 int summary_stats_only;
61 sort_t sort_type = SORT_MAX_TIME;
63 int widest_name_format=5;
64 int widest_track_format=5;
66 typedef struct bound_event_ {
73 bound_event_t *bound_events;
75 typedef struct bound_track_ {
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;
84 bound_track_t *bound_tracks;
88 fprintf(stderr, "%s", s);
98 int (*pass1)(cpel_section_header_t *, int, FILE *);
99 int (*pass2)(cpel_section_header_t *, int, FILE *);
100 } section_processor_t;
102 int bad_section(cpel_section_header_t *sh, int verbose, FILE *ofp)
104 fprintf(ofp, "Bad (type 0) section, skipped...\n");
108 int noop_pass(cpel_section_header_t *sh, int verbose, FILE *ofp)
113 int strtab_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
116 u8 *strtab_data_area = (u8 *)(sh+1);
118 /* Multiple string tables with the same name are Bad... */
119 p = hash_get_mem(the_strtab_hash, strtab_data_area);
121 fprintf(ofp, "Duplicate string table name %s", strtab_data_area);
124 * Looks funny, but we really do want key = first string in the
125 * table, value = address(first string in the table)
127 hash_set_mem(the_strtab_hash, strtab_data_area, strtab_data_area);
129 fprintf(ofp, "String Table %s\n", strtab_data_area);
134 int evtdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
137 event_definition_section_header_t *edh;
138 event_definition_t *ep;
145 edh = (event_definition_section_header_t *)(sh+1);
146 nevents = ntohl(edh->number_of_event_definitions);
149 fprintf(ofp, "Event Definition Section: %d definitions\n",
153 p = hash_get_mem(the_strtab_hash, edh->string_table_name);
155 fprintf(ofp, "Fatal: couldn't find string table\n");
158 this_strtab = (u8 *)p[0];
160 ep = (event_definition_t *)(edh+1);
162 for (i = 0; i < nevents; i++) {
163 event_code = ntohl(ep->event);
164 p = hash_get(the_evtdef_hash, event_code);
166 fprintf(ofp, "Event %d redefined, retain first definition\n",
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 */
180 for (j = 0; j < strlen((char *)(bp->datum_str)); j++) {
181 if (bp->datum_str[j] == '%'){
185 if (seen_percent && bp->datum_str[j] == 's') {
186 bp->is_strtab_ref = 1;
191 hash_set(the_evtdef_hash, event_code, bp - bound_events);
193 thislen = strlen((char *)bp->event_str);
194 if (thislen > widest_name_format)
195 widest_name_format = thislen;
202 int trackdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
205 track_definition_section_header_t *tdh;
206 track_definition_t *tp;
213 tdh = (track_definition_section_header_t *)(sh+1);
214 nevents = ntohl(tdh->number_of_track_definitions);
217 fprintf(ofp, "Track Definition Section: %d definitions\n",
221 p = hash_get_mem(the_strtab_hash, tdh->string_table_name);
223 fprintf(ofp, "Fatal: couldn't find string table\n");
226 this_strtab = (u8 *)p[0];
228 tp = (track_definition_t *)(tdh+1);
230 for (i = 0; i < nevents; i++) {
231 track_code = ntohl(tp->track);
232 p = hash_get(the_trackdef_hash, track_code);
234 fprintf(ofp, "track %d redefined, retain first definition\n",
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);
243 thislen = strlen((char *)(btp->track_str));
244 if (thislen > widest_track_format)
245 widest_track_format = thislen;
251 int unsupported_pass (cpel_section_header_t *sh, int verbose, FILE *ofp)
254 fprintf(ofp, "Unsupported type %d section\n",
255 ntohl(sh->section_type));
260 int event_pass2(cpel_section_header_t *sh, int verbose, FILE *ofp)
262 event_section_header_t *eh;
270 bound_track_t generic_track;
272 u64 state_start_ticks=0;
274 bound_track_t *state_track=0;
276 generic_track.track_str = (u8 *) "%d";
277 last_track_code = 0xdeafbeef;
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;
284 fprintf(ofp, "%.3f ticks_per_us\n", ticks_per_us);
287 ep = (event_entry_t *)(eh+1);
289 p = hash_get_mem(the_strtab_hash, eh->string_table_name);
291 fprintf(ofp, "Fatal: couldn't find string table\n");
295 for (i = 0; i < nevents; i++) {
296 time0 = ntohl (ep->time[0]);
297 time1 = ntohl (ep->time[1]);
299 now = (((u64) time0)<<32) | time1;
301 /* Found the state-change event ? */
302 if (ntohl(ep->event_code) == state_event_code) {
304 * Add a ticks-in-state record, unless
305 * this is the "prime mover" event instance
308 ticks_in_state = now - state_start_ticks;
309 vec_add1(state_track->ticks_in_state, ticks_in_state);
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);
317 state_track = &bound_tracks[p[0]];
319 state_track = &generic_track;
321 last_track_code = track_code;
331 * Note: If necessary, add passes / columns to this table to
332 * handle section order dependencies.
335 section_processor_t processors[CPEL_NUM_SECTION_TYPES+1] =
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 */
346 int process_section(cpel_section_header_t *sh, int verbose, FILE *ofp,
350 type = ntohl(sh->section_type);
352 int (*fp)(cpel_section_header_t *, int, FILE *);
354 if (type > CPEL_NUM_SECTION_TYPES) {
355 fprintf(stderr, "Unknown section type %d\n", type);
360 fp = processors[type].pass1;
364 fp = processors[type].pass2;
368 fprintf(stderr, "Unknown pass %d\n", pass);
372 rv = (*fp)(sh, verbose, ofp);
377 int cpel_dump_file_header(cpel_file_header_t *fh, int verbose, FILE *ofp)
382 fprintf(ofp, "CPEL file: %s-endian, version %d\n",
383 ((fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) ?
385 fh->endian_version & CPEL_FILE_VERSION_MASK);
387 file_time = ntohl(fh->file_date);
389 fprintf(ofp, "File created %s", ctime(&file_time));
390 fprintf(ofp, "File has %d sections\n",
391 ntohs(fh->nsections));
398 int cpel_dump(u8 *cpel, int verbose, FILE *ofp)
400 cpel_file_header_t *fh;
401 cpel_section_header_t *sh;
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");
413 fprintf(stderr, "Unsupported file version 0x%x\n",
417 cpel_dump_file_header(fh, verbose, ofp);
418 nsections = ntohs(fh->nsections);
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.
425 sh = (cpel_section_header_t *)(fh+1);
426 for (i = 0; i < nsections; i++) {
427 section_size = ntohl(sh->data_length);
430 fprintf(ofp, "Section type %d, size %d\n", ntohl(sh->section_type),
434 if(process_section(sh, verbose, ofp, PASS1))
438 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
441 sh = (cpel_section_header_t *)(fh+1);
442 for (i = 0; i < nsections; i++) {
443 if(process_section(sh, verbose, ofp, PASS2))
445 section_size = ntohl(sh->data_length);
447 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
452 void compute_state_statistics(int verbose, FILE *ofp)
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];
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));
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);
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);
487 int track_compare_max (const void *arg1, const void *arg2)
489 bound_track_t *a1 = (bound_track_t *)arg1;
490 bound_track_t *a2 = (bound_track_t *)arg2;
493 v1 = a1->total_ticks_in_state;
494 v2 = a2->total_ticks_in_state;
503 int track_compare_occurrences (const void *arg1, const void *arg2)
505 bound_track_t *a1 = (bound_track_t *)arg1;
506 bound_track_t *a2 = (bound_track_t *)arg2;
509 v1 = (f64) vec_len(a1->ticks_in_state);
510 v2 = (f64) vec_len(a2->ticks_in_state);
519 int track_compare_name (const void *arg1, const void *arg2)
521 bound_track_t *a1 = (bound_track_t *)arg1;
522 bound_track_t *a2 = (bound_track_t *)arg2;
524 return (strcmp((char *)(a1->track_str), (char *)(a2->track_str)));
527 void sort_state_statistics(sort_t type, FILE *ofp)
529 int (*compare)(const void *, const void *)=0;
531 if (summary_stats_only)
536 fprintf(ofp, "Results sorted by max time in state.\n");
537 compare = track_compare_max;
540 case SORT_MAX_OCCURRENCES:
541 fprintf(ofp, "Results sorted by max occurrences of state.\n");
542 compare = track_compare_occurrences;
546 compare = track_compare_name;
547 fprintf(ofp, "Results sorted by process-id/name/thread ID\n");
551 fatal("sort type not set?");
554 qsort (bound_tracks, vec_len(bound_tracks),
555 sizeof (bound_track_t), compare);
558 void print_state_statistics(int verbose, FILE *ofp)
563 f64 total_time = 0.0;
564 f64 total_switches = 0.0;
566 trackpad = format(0, "%%-%ds ", widest_track_format);
567 vec_add1(trackpad, 0);
569 if (!summary_stats_only) {
570 fprintf(ofp, (char *)trackpad, "ProcThread");
571 fprintf(ofp, " Mean(us) Stdev(us) Total(us) N\n");
574 for (i = 0; i < vec_len(bound_tracks); i++) {
575 bp = &bound_tracks[i];
576 if (bp->mean_ticks_in_state == 0.0)
580 strncmp((char *)(bp->track_str), (char *)name_filter,
581 strlen((char *)name_filter)))
585 * Exclude kernel threads (e.g. idle thread) from
588 if (exclude_kernel_from_summary_stats &&
589 !strncmp((char *)(bp->track_str), "kernel ", 7))
592 total_switches += (f64) vec_len(bp->ticks_in_state);
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));
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);
612 total_time += bp->total_ticks_in_state;
615 if (!summary_stats_only)
617 fprintf(ofp, "Note: the following statistics %s kernel-thread activity.\n",
618 exclude_kernel_from_summary_stats ? "exclude" : "include");
621 "Note: only pid/proc/threads matching '%s' are included.\n",
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);
631 char *mapfile (char *file)
638 maphfile = open (file, O_RDONLY);
642 fprintf (stderr, "Couldn't read %s, skipping it...\n", file);
646 if (fstat (maphfile, &statb) < 0)
648 fprintf (stderr, "Couldn't get size of %s, skipping it...\n", file);
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);
658 mapfsize = statb.st_size;
662 fprintf (stderr, "%s zero-length, skipping it...\n", file);
667 rv = mmap (0, mapfsize, PROT_READ, MAP_SHARED, maphfile, 0);
671 fprintf (stderr, "%s problem mapping, I quit...\n", file);
681 int main (int argc, char **argv)
684 char *outputfile = 0;
690 while (curarg < argc) {
691 if (!strncmp(argv[curarg], "--input-file", 3)) {
694 cpel_file = argv[curarg];
698 fatal("Missing filename after --input-file\n");
700 if (!strncmp(argv[curarg], "--output-file", 3)) {
703 outputfile = argv[curarg];
707 fatal("Missing filename after --output-file\n");
709 if (!strncmp(argv[curarg], "--verbose", 3)) {
714 if (!strncmp(argv[curarg], "--scatterplot", 4)) {
720 if (!strncmp(argv[curarg], "--state-event", 4)) {
723 state_event_code = atol(argv[curarg]);
727 fatal("Missing integer after --state-event\n");
729 if (!strncmp(argv[curarg], "--max-time-sort", 7)) {
730 sort_type = SORT_MAX_TIME;
734 if (!strncmp(argv[curarg], "--max-occurrence-sort", 7)) {
735 sort_type = SORT_MAX_OCCURRENCES;
739 if (!strncmp(argv[curarg], "--name-sort", 3)) {
740 sort_type = SORT_NAME;
744 if (!strncmp(argv[curarg], "--kernel-included", 3)) {
745 exclude_kernel_from_summary_stats = 0;
749 if (!strncmp(argv[curarg], "--summary", 3)) {
750 summary_stats_only=1;
754 if (!strncmp(argv[curarg], "--filter", 3)) {
757 name_filter = (u8 *)argv[curarg];
761 fatal("Missing filter string after --filter\n");
767 "cpelstate --input-file <filename> [--output-file <filename>]\n");
769 " [--state-event <decimal>] [--verbose]\n");
771 " [--max-time-sort(default) | --max-occurrence-sort |\n");
774 " --name-sort-sort] [--kernel-included]\n");
777 " [--summary-stats-only] [--scatterplot]\n");
779 fprintf(stderr, "%s\n", version);
786 cpel = mapfile(cpel_file);
788 fprintf(stderr, "Couldn't map %s...\n", cpel_file);
793 ofp = fdopen(1, "w");
795 fprintf(stderr, "Couldn't fdopen(1)?\n");
799 ofp = fopen(outputfile, "w");
801 fprintf(stderr, "Couldn't create %s...\n", outputfile);
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));
810 if (cpel_dump((u8 *) cpel, verbose, ofp)) {
815 compute_state_statistics(verbose, ofp);
816 sort_state_statistics(sort_type, ofp);
817 print_state_statistics(verbose, ofp);