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[] = "cpelatency 2.0";
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 */
47 uword *the_pidtid_hash; /* ("pid:xxx tid:yy", track-definition) hash */
50 u32 start_event_code = 2; /* default: XR thread ready event */
51 u32 end_event_code = 1; /* default: XR thread running event */
52 int exclude_kernel_from_summary_stats=1;
53 int summary_stats_only;
64 sort_t sort_type = SORT_MAX_TIME;
66 int widest_name_format=5;
67 int widest_track_format=20;
69 typedef struct bound_event_ {
76 bound_event_t *bound_events;
78 typedef struct bound_track_ {
81 u64 state_start_ticks;
82 u64 *ticks_in_state; /* vector of state occurrences */
83 f64 mean_ticks_in_state;
84 f64 variance_ticks_in_state;
85 f64 total_ticks_in_state;
88 bound_track_t *bound_tracks;
92 fprintf(stderr, "%s", s);
102 int (*pass1)(cpel_section_header_t *, int, FILE *);
103 int (*pass2)(cpel_section_header_t *, int, FILE *);
104 } section_processor_t;
106 int bad_section(cpel_section_header_t *sh, int verbose, FILE *ofp)
108 fprintf(ofp, "Bad (type 0) section, skipped...\n");
112 int noop_pass(cpel_section_header_t *sh, int verbose, FILE *ofp)
117 int strtab_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
120 u8 *strtab_data_area = (u8 *)(sh+1);
122 /* Multiple string tables with the same name are Bad... */
123 p = hash_get_mem(the_strtab_hash, strtab_data_area);
125 fprintf(ofp, "Duplicate string table name %s", strtab_data_area);
128 * Looks funny, but we really do want key = first string in the
129 * table, value = address(first string in the table)
131 hash_set_mem(the_strtab_hash, strtab_data_area, strtab_data_area);
133 fprintf(ofp, "String Table %s\n", strtab_data_area);
138 int evtdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
141 event_definition_section_header_t *edh;
142 event_definition_t *ep;
149 edh = (event_definition_section_header_t *)(sh+1);
150 nevents = ntohl(edh->number_of_event_definitions);
153 fprintf(ofp, "Event Definition Section: %d definitions\n",
157 p = hash_get_mem(the_strtab_hash, edh->string_table_name);
159 fprintf(ofp, "Fatal: couldn't find string table\n");
162 this_strtab = (u8 *)p[0];
164 ep = (event_definition_t *)(edh+1);
166 for (i = 0; i < nevents; i++) {
167 event_code = ntohl(ep->event);
168 p = hash_get(the_evtdef_hash, event_code);
170 fprintf(ofp, "Event %d redefined, retain first definition\n",
174 vec_add2(bound_events, bp, 1);
175 bp->event_code = event_code;
176 bp->event_str = this_strtab + ntohl(ep->event_format);
177 bp->datum_str = this_strtab + ntohl(ep->datum_format);
178 bp->is_strtab_ref = 0;
179 /* Decide if the datum format is a %s format => strtab reference */
184 for (j = 0; j < strlen((char *) bp->datum_str); j++) {
185 if (bp->datum_str[j] == '%'){
189 if (seen_percent && bp->datum_str[j] == 's') {
190 bp->is_strtab_ref = 1;
195 hash_set(the_evtdef_hash, event_code, bp - bound_events);
197 thislen = strlen((char *) bp->event_str);
198 if (thislen > widest_name_format)
199 widest_name_format = thislen;
206 int trackdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp)
209 track_definition_section_header_t *tdh;
210 track_definition_t *tp;
221 tdh = (track_definition_section_header_t *)(sh+1);
222 nevents = ntohl(tdh->number_of_track_definitions);
225 fprintf(ofp, "Track Definition Section: %d definitions\n",
229 p = hash_get_mem(the_strtab_hash, tdh->string_table_name);
231 fprintf(ofp, "Fatal: couldn't find string table\n");
234 this_strtab = (u8 *)p[0];
236 tp = (track_definition_t *)(tdh+1);
238 for (i = 0; i < nevents; i++) {
239 track_code = ntohl(tp->track);
240 p = hash_get(the_trackdef_hash, track_code);
242 fprintf(stderr, "track %d redefined, retain first definition\n",
246 vec_add2(bound_tracks, btp, 1);
247 btp->track = track_code;
248 btp->track_str = this_strtab + ntohl(tp->track_format);
249 hash_set(the_trackdef_hash, track_code, btp - bound_tracks);
252 fprintf(stderr, "adding track '%s'\n", btp->track_str);
255 thislen = strlen((char *) btp->track_str);
256 if (thislen > widest_track_format)
257 widest_track_format = thislen;
259 /* convert track_str "eth_server t11(20498)" to "pid:20498 tid:11" */
261 while (*cp && *cp != '(')
264 fprintf(stderr, "error canonicalizing '%s'\n", btp->track_str);
267 pidstr = cp+1; /* remember location of PID */
269 while (cp > btp->track_str && *cp != 't')
272 if (cp == btp->track_str) {
273 fprintf(stderr, "error canonicalizing '%s'\n", btp->track_str);
276 tid = atol((char *)(cp+1));
277 pid = atol((char *) pidstr);
278 pidtid_str = format(0, "pid:%d tid:%d", pid, tid);
279 vec_add1(pidtid_str, 0);
282 * Note: duplicates are possible due to thread create /
283 * thread destroy operations.
285 p = hash_get_mem(the_pidtid_hash, pidtid_str);
287 vec_free(pidtid_str);
290 hash_set_mem(the_pidtid_hash, pidtid_str, btp - bound_tracks);
299 int unsupported_pass (cpel_section_header_t *sh, int verbose, FILE *ofp)
302 fprintf(ofp, "Unsupported type %d section\n",
303 ntohl(sh->section_type));
308 int event_pass2(cpel_section_header_t *sh, int verbose, FILE *ofp)
310 event_section_header_t *eh;
321 bound_track_t *state_track=0;
327 eh = (event_section_header_t *)(sh+1);
328 nevents = ntohl(eh->number_of_events);
329 ticks_per_us = ((double)ntohl(eh->clock_ticks_per_second)) / 1e6;
332 fprintf(ofp, "%.3f ticks_per_us\n", ticks_per_us);
335 ep = (event_entry_t *)(eh+1);
337 p = hash_get_mem(the_strtab_hash, eh->string_table_name);
339 fprintf(ofp, "Fatal: couldn't find string table\n");
342 this_strtab = (u8 *)p[0];
345 * Some logger implementation that doesn't produce
346 * trackdef sections, synthesize the bound_tracks vector
348 if (!have_trackdefs) {
349 for (i = 0; i < nevents; i++) {
350 track_code = ntohl(ep->track);
351 pidtid_dup = format(0, "%d", track_code);
352 vec_add1(pidtid_dup, 0);
353 p = hash_get_mem(the_pidtid_hash, pidtid_dup);
355 vec_add2(bound_tracks, btp, 1);
356 btp->track = track_code;
357 btp->track_str = pidtid_dup;
358 hash_set(the_trackdef_hash, track_code, btp - bound_tracks);
359 hash_set_mem(the_pidtid_hash, pidtid_dup, btp - bound_tracks);
361 vec_free(pidtid_dup);
367 ep = (event_entry_t *)(eh+1);
369 for (i = 0; i < nevents; i++) {
370 time0 = ntohl (ep->time[0]);
371 time1 = ntohl (ep->time[1]);
373 now = (((u64) time0)<<32) | time1;
375 event_code = ntohl(ep->event_code);
377 /* Find the corresponding track via the pidtid hash table */
378 if (event_code == start_event_code || event_code == end_event_code) {
379 if (have_trackdefs) {
380 pidtid_str = this_strtab + ntohl(ep->event_datum);
381 pidtid_dup = format(0, (char *) pidtid_str);
382 vec_add1(pidtid_dup, 0);
383 ecp = &pidtid_dup[vec_len(pidtid_dup)-1];
384 while (*--ecp == ' ')
387 pidtid_dup = format(0, "%d", ntohl(ep->track));
388 vec_add1(pidtid_dup, 0);
391 p = hash_get_mem(the_pidtid_hash, pidtid_dup);
393 fprintf(stderr, "warning: couldn't find '%s'\n",
395 vec_free(pidtid_dup);
399 state_track = &bound_tracks[p[0]];
401 /* Found the start-event code ? */
402 if (event_code == start_event_code) {
403 state_track->state_start_ticks = now;
404 } else if (event_code == end_event_code) {
406 * Add a ticks-in-state record, unless
407 * e.g. the log started with the exit event
409 if (state_track->state_start_ticks) {
410 ticks_in_state = now - state_track->state_start_ticks;
411 vec_add1(state_track->ticks_in_state, ticks_in_state);
412 state_track->state_start_ticks = 0;
414 /* Otherwise, nothing */
422 * Note: If necessary, add passes / columns to this table to
423 * handle section order dependencies.
426 section_processor_t processors[CPEL_NUM_SECTION_TYPES+1] =
428 {bad_section, noop_pass}, /* type 0 -- f**ked */
429 {strtab_pass1, noop_pass}, /* type 1 -- STRTAB */
430 {unsupported_pass, noop_pass}, /* type 2 -- SYMTAB */
431 {evtdef_pass1, noop_pass}, /* type 3 -- EVTDEF */
432 {trackdef_pass1, noop_pass}, /* type 4 -- TRACKDEF */
433 {noop_pass, event_pass2}, /* type 5 -- EVENTS */
437 int process_section(cpel_section_header_t *sh, int verbose, FILE *ofp,
441 type = ntohl(sh->section_type);
443 int (*fp)(cpel_section_header_t *, int, FILE *);
445 if (type > CPEL_NUM_SECTION_TYPES) {
446 fprintf(stderr, "Unknown section type %d\n", type);
451 fp = processors[type].pass1;
455 fp = processors[type].pass2;
459 fprintf(stderr, "Unknown pass %d\n", pass);
463 rv = (*fp)(sh, verbose, ofp);
468 int cpel_dump_file_header(cpel_file_header_t *fh, int verbose, FILE *ofp)
473 fprintf(ofp, "CPEL file: %s-endian, version %d\n",
474 ((fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) ?
476 fh->endian_version & CPEL_FILE_VERSION_MASK);
478 file_time = ntohl(fh->file_date);
480 fprintf(ofp, "File created %s", ctime(&file_time));
481 fprintf(ofp, "File has %d sections\n",
482 ntohs(fh->nsections));
489 int cpel_dump(u8 *cpel, int verbose, FILE *ofp)
491 cpel_file_header_t *fh;
492 cpel_section_header_t *sh;
497 /* First, the file header */
498 fh = (cpel_file_header_t *)cpel;
499 if (fh->endian_version != CPEL_FILE_VERSION) {
500 if (fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) {
501 fprintf(stderr, "Little endian data format not supported\n");
504 fprintf(stderr, "Unsupported file version 0x%x\n",
508 cpel_dump_file_header(fh, verbose, ofp);
509 nsections = ntohs(fh->nsections);
512 * Take two passes through the file. PASS1 builds
513 * data structures, PASS2 actually dumps the file.
514 * Just in case the sections are in an unobvious order.
516 sh = (cpel_section_header_t *)(fh+1);
517 for (i = 0; i < nsections; i++) {
518 section_size = ntohl(sh->data_length);
521 fprintf(ofp, "Section type %d, size %d\n", ntohl(sh->section_type),
525 if(process_section(sh, verbose, ofp, PASS1))
529 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
532 sh = (cpel_section_header_t *)(fh+1);
533 for (i = 0; i < nsections; i++) {
534 if(process_section(sh, verbose, ofp, PASS2))
536 section_size = ntohl(sh->data_length);
538 sh = (cpel_section_header_t *)(((u8 *)sh)+section_size);
543 void compute_state_statistics(int verbose, FILE *ofp)
549 /* Across the bound tracks */
550 for (i = 0; i < vec_len(bound_tracks); i++) {
551 bp = &bound_tracks[i];
552 bp->mean_ticks_in_state = 0.0;
553 bp->variance_ticks_in_state = 0.0;
554 bp->total_ticks_in_state = 0.0;
555 for (j = 0; j < vec_len(bp->ticks_in_state); j++) {
556 bp->total_ticks_in_state += (f64) bp->ticks_in_state[j];
559 if (vec_len(bp->ticks_in_state)) {
560 bp->mean_ticks_in_state = bp->total_ticks_in_state /
561 ((f64) vec_len(bp->ticks_in_state));
563 /* Accumulate sum: (Xi-Xbar)**2 */
564 for (j = 0; j < vec_len(bp->ticks_in_state); j++) {
565 fticks = bp->ticks_in_state[j];
566 bp->variance_ticks_in_state +=
567 (fticks - bp->mean_ticks_in_state)*
568 (fticks - bp->mean_ticks_in_state);
570 /* Compute s**2, the unbiased estimator of sigma**2 */
571 if (vec_len(bp->ticks_in_state) > 1) {
572 bp->variance_ticks_in_state /= (f64)
573 (vec_len(bp->ticks_in_state)-1);
578 int track_compare_max (const void *arg1, const void *arg2)
580 bound_track_t *a1 = (bound_track_t *)arg1;
581 bound_track_t *a2 = (bound_track_t *)arg2;
584 v1 = a1->total_ticks_in_state;
585 v2 = a2->total_ticks_in_state;
594 int track_compare_occurrences (const void *arg1, const void *arg2)
596 bound_track_t *a1 = (bound_track_t *)arg1;
597 bound_track_t *a2 = (bound_track_t *)arg2;
600 v1 = (f64) vec_len(a1->ticks_in_state);
601 v2 = (f64) vec_len(a2->ticks_in_state);
610 int track_compare_name (const void *arg1, const void *arg2)
612 bound_track_t *a1 = (bound_track_t *)arg1;
613 bound_track_t *a2 = (bound_track_t *)arg2;
615 return (strcmp((char *)(a1->track_str), (char *)(a2->track_str)));
618 void sort_state_statistics(sort_t type, FILE *ofp)
620 int (*compare)(const void *, const void *) = 0;
622 if (summary_stats_only)
627 fprintf(ofp, "Results sorted by max time in state.\n\n");
628 compare = track_compare_max;
631 case SORT_MAX_OCCURRENCES:
632 fprintf(ofp, "Results sorted by max occurrences of state.\n\n");
633 compare = track_compare_occurrences;
637 compare = track_compare_name;
638 fprintf(ofp, "Results sorted by process name, thread ID, PID\n\n");
642 fatal("sort type not set?");
645 qsort (bound_tracks, vec_len(bound_tracks),
646 sizeof (bound_track_t), compare);
649 void print_state_statistics(int verbose, FILE *ofp)
654 f64 total_time = 0.0;
655 f64 total_switches = 0.0;
657 trackpad = format(0, "%%-%ds ", widest_track_format);
658 vec_add1(trackpad, 0);
660 if (!summary_stats_only) {
661 fprintf(ofp, (char *)trackpad, "ProcName Thread(PID)");
662 fprintf(ofp, " Mean(us) Stdev(us) Total(us) N\n");
665 for (i = 0; i < vec_len(bound_tracks); i++) {
666 bp = &bound_tracks[i];
667 if (bp->mean_ticks_in_state == 0.0)
671 strncmp((char *)bp->track_str, (char *)name_filter,
672 strlen((char *)name_filter)))
676 * Exclude kernel threads (e.g. idle thread) from
679 if (exclude_kernel_from_summary_stats &&
680 !strncmp((char *) bp->track_str, "kernel ", 7))
683 total_switches += (f64) vec_len(bp->ticks_in_state);
685 if (!summary_stats_only) {
686 fprintf(ofp, (char *) trackpad, bp->track_str);
687 fprintf(ofp, "%10.3f +- %10.3f",
688 bp->mean_ticks_in_state / ticks_per_us,
689 sqrt(bp->variance_ticks_in_state)
691 fprintf(ofp, "%12.3f",
692 bp->total_ticks_in_state / ticks_per_us);
693 fprintf(ofp, "%8d\n", vec_len(bp->ticks_in_state));
697 for (j = 0; j < vec_len(bp->ticks_in_state); j++) {
698 fprintf(ofp, "%.3f\n",
699 (f64)bp->ticks_in_state[j] / ticks_per_us);
703 total_time += bp->total_ticks_in_state;
706 if (!summary_stats_only)
708 fprintf(ofp, "Note: the following statistics %s kernel-thread activity.\n",
709 exclude_kernel_from_summary_stats ? "exclude" : "include");
712 "Note: only pid/proc/threads matching '%s' are included.\n",
716 "Total time in state: %10.3f (us), Total state occurrences: %.0f\n",
717 total_time / ticks_per_us, total_switches);
718 fprintf(ofp, "Average time in state: %10.3f (us)\n",
719 (total_time / total_switches) / ticks_per_us);
720 fprintf(ofp, "State start event: %d, state end event: %d\n",
721 start_event_code, end_event_code);
724 char *mapfile (char *file)
731 maphfile = open (file, O_RDONLY);
735 fprintf (stderr, "Couldn't read %s, skipping it...\n", file);
739 if (fstat (maphfile, &statb) < 0)
741 fprintf (stderr, "Couldn't get size of %s, skipping it...\n", file);
745 /* Don't try to mmap directories, FIFOs, semaphores, etc. */
746 if (! (statb.st_mode & S_IFREG)) {
747 fprintf (stderr, "%s is not a regular file, skipping it...\n", file);
751 mapfsize = statb.st_size;
755 fprintf (stderr, "%s zero-length, skipping it...\n", file);
760 rv = mmap (0, mapfsize, PROT_READ, MAP_SHARED, maphfile, 0);
764 fprintf (stderr, "%s problem mapping, I quit...\n", file);
774 int main (int argc, char **argv)
777 char *outputfile = 0;
783 while (curarg < argc) {
784 if (!strncmp(argv[curarg], "--input-file", 3)) {
787 cpel_file = argv[curarg];
791 fatal("Missing filename after --input-file\n");
793 if (!strncmp(argv[curarg], "--output-file", 3)) {
796 outputfile = argv[curarg];
800 fatal("Missing filename after --output-file\n");
802 if (!strncmp(argv[curarg], "--verbose", 3)) {
807 if (!strncmp(argv[curarg], "--scatterplot", 4)) {
813 if (!strncmp(argv[curarg], "--start-event", 4)) {
816 start_event_code = atol(argv[curarg]);
820 fatal("Missing integer after --start-event\n");
822 if (!strncmp(argv[curarg], "--end-event", 4)) {
825 end_event_code = atol(argv[curarg]);
829 fatal("Missing integer after --end-event\n");
831 if (!strncmp(argv[curarg], "--max-time-sort", 7)) {
832 sort_type = SORT_MAX_TIME;
836 if (!strncmp(argv[curarg], "--max-occurrence-sort", 7)) {
837 sort_type = SORT_MAX_OCCURRENCES;
841 if (!strncmp(argv[curarg], "--name-sort", 3)) {
842 sort_type = SORT_NAME;
846 if (!strncmp(argv[curarg], "--kernel-included", 3)) {
847 exclude_kernel_from_summary_stats = 0;
851 if (!strncmp(argv[curarg], "--summary", 3)) {
852 summary_stats_only=1;
856 if (!strncmp(argv[curarg], "--filter", 3)) {
859 name_filter = (u8 *) argv[curarg];
863 fatal("Missing filter string after --filter\n");
869 "cpelatency --input-file <filename> [--output-file <filename>]\n");
871 " [--start-event <decimal>] [--verbose]\n");
873 " [--end-event <decimal>]\n");
875 " [--max-time-sort(default) | --max-occurrence-sort |\n");
878 " --name-sort-sort] [--kernel-included]\n");
881 " [--summary-stats-only] [--scatterplot]\n");
883 fprintf(stderr, "%s\n", version);
890 cpel = mapfile(cpel_file);
892 fprintf(stderr, "Couldn't map %s...\n", cpel_file);
897 ofp = fdopen(1, "w");
899 fprintf(stderr, "Couldn't fdopen(1)?\n");
903 ofp = fopen(outputfile, "w");
905 fprintf(stderr, "Couldn't create %s...\n", outputfile);
910 the_strtab_hash = hash_create_string (0, sizeof (uword));
911 the_evtdef_hash = hash_create (0, sizeof (uword));
912 the_trackdef_hash = hash_create (0, sizeof (uword));
913 the_pidtid_hash = hash_create_string (0, sizeof(uword));
915 if (cpel_dump((u8 *)cpel, verbose, ofp)) {
920 compute_state_statistics(verbose, ofp);
921 sort_state_statistics(sort_type, ofp);
922 print_state_statistics(verbose, ofp);