Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 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> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 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[] = "cpelatency 2.0"; |
| 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 | uword *the_pidtid_hash; /* ("pid:xxx tid:yy", track-definition) hash */ |
| 48 | |
| 49 | f64 ticks_per_us; |
| 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; |
| 54 | int scatterplot; |
| 55 | u8 *name_filter; |
| 56 | int have_trackdefs; |
| 57 | |
| 58 | typedef enum { |
| 59 | SORT_MAX_TIME=1, |
| 60 | SORT_MAX_OCCURRENCES, |
| 61 | SORT_NAME, |
| 62 | } sort_t; |
| 63 | |
| 64 | sort_t sort_type = SORT_MAX_TIME; |
| 65 | |
| 66 | int widest_name_format=5; |
| 67 | int widest_track_format=20; |
| 68 | |
| 69 | typedef struct bound_event_ { |
| 70 | u32 event_code; |
| 71 | u8 *event_str; |
| 72 | u8 *datum_str; |
| 73 | u32 is_strtab_ref; |
| 74 | } bound_event_t; |
| 75 | |
| 76 | bound_event_t *bound_events; |
| 77 | |
| 78 | typedef struct bound_track_ { |
| 79 | u32 track; |
| 80 | u8 *track_str; |
| 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; |
| 86 | } bound_track_t; |
| 87 | |
| 88 | bound_track_t *bound_tracks; |
| 89 | |
| 90 | void fatal(char *s) |
| 91 | { |
| 92 | fprintf(stderr, "%s", s); |
| 93 | exit(1); |
| 94 | } |
| 95 | |
| 96 | typedef enum { |
| 97 | PASS1=1, |
| 98 | PASS2=2, |
| 99 | } pass_t; |
| 100 | |
| 101 | typedef struct { |
| 102 | int (*pass1)(cpel_section_header_t *, int, FILE *); |
| 103 | int (*pass2)(cpel_section_header_t *, int, FILE *); |
| 104 | } section_processor_t; |
| 105 | |
| 106 | int bad_section(cpel_section_header_t *sh, int verbose, FILE *ofp) |
| 107 | { |
| 108 | fprintf(ofp, "Bad (type 0) section, skipped...\n"); |
| 109 | return(0); |
| 110 | } |
| 111 | |
| 112 | int noop_pass(cpel_section_header_t *sh, int verbose, FILE *ofp) |
| 113 | { |
| 114 | return(0); |
| 115 | } |
| 116 | |
| 117 | int strtab_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp) |
| 118 | { |
| 119 | uword *p; |
| 120 | u8 *strtab_data_area = (u8 *)(sh+1); |
| 121 | |
| 122 | /* Multiple string tables with the same name are Bad... */ |
| 123 | p = hash_get_mem(the_strtab_hash, strtab_data_area); |
| 124 | if (p) { |
| 125 | fprintf(ofp, "Duplicate string table name %s", strtab_data_area); |
| 126 | } |
| 127 | /* |
| 128 | * Looks funny, but we really do want key = first string in the |
| 129 | * table, value = address(first string in the table) |
| 130 | */ |
| 131 | hash_set_mem(the_strtab_hash, strtab_data_area, strtab_data_area); |
| 132 | if (verbose) { |
| 133 | fprintf(ofp, "String Table %s\n", strtab_data_area); |
| 134 | } |
| 135 | return(0); |
| 136 | } |
| 137 | |
| 138 | int evtdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp) |
| 139 | { |
| 140 | int i, nevents; |
| 141 | event_definition_section_header_t *edh; |
| 142 | event_definition_t *ep; |
| 143 | u8 *this_strtab; |
| 144 | u32 event_code; |
| 145 | uword *p; |
| 146 | bound_event_t *bp; |
| 147 | int thislen; |
| 148 | |
| 149 | edh = (event_definition_section_header_t *)(sh+1); |
| 150 | nevents = ntohl(edh->number_of_event_definitions); |
| 151 | |
| 152 | if (verbose) { |
| 153 | fprintf(ofp, "Event Definition Section: %d definitions\n", |
| 154 | nevents); |
| 155 | } |
| 156 | |
| 157 | p = hash_get_mem(the_strtab_hash, edh->string_table_name); |
| 158 | if (!p) { |
| 159 | fprintf(ofp, "Fatal: couldn't find string table\n"); |
| 160 | return(1); |
| 161 | } |
| 162 | this_strtab = (u8 *)p[0]; |
| 163 | |
| 164 | ep = (event_definition_t *)(edh+1); |
| 165 | |
| 166 | for (i = 0; i < nevents; i++) { |
| 167 | event_code = ntohl(ep->event); |
| 168 | p = hash_get(the_evtdef_hash, event_code); |
| 169 | if (p) { |
| 170 | fprintf(ofp, "Event %d redefined, retain first definition\n", |
| 171 | event_code); |
| 172 | continue; |
| 173 | } |
| 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 */ |
| 180 | { |
| 181 | int j; |
| 182 | int seen_percent=0; |
| 183 | |
| 184 | for (j = 0; j < strlen((char *) bp->datum_str); j++) { |
| 185 | if (bp->datum_str[j] == '%'){ |
| 186 | seen_percent=1; |
| 187 | continue; |
| 188 | } |
| 189 | if (seen_percent && bp->datum_str[j] == 's') { |
| 190 | bp->is_strtab_ref = 1; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | hash_set(the_evtdef_hash, event_code, bp - bound_events); |
| 196 | |
| 197 | thislen = strlen((char *) bp->event_str); |
| 198 | if (thislen > widest_name_format) |
| 199 | widest_name_format = thislen; |
| 200 | |
| 201 | ep++; |
| 202 | } |
| 203 | return (0); |
| 204 | } |
| 205 | |
| 206 | int trackdef_pass1(cpel_section_header_t *sh, int verbose, FILE *ofp) |
| 207 | { |
| 208 | int i, nevents; |
| 209 | track_definition_section_header_t *tdh; |
| 210 | track_definition_t *tp; |
| 211 | u8 *this_strtab; |
| 212 | u32 track_code; |
| 213 | uword *p; |
| 214 | bound_track_t *btp; |
| 215 | int thislen; |
| 216 | u8 *pidstr; |
| 217 | u8 *pidtid_str; |
| 218 | u8 *cp; |
| 219 | int tid, pid; |
| 220 | |
| 221 | tdh = (track_definition_section_header_t *)(sh+1); |
| 222 | nevents = ntohl(tdh->number_of_track_definitions); |
| 223 | |
| 224 | if (verbose) { |
| 225 | fprintf(ofp, "Track Definition Section: %d definitions\n", |
| 226 | nevents); |
| 227 | } |
| 228 | |
| 229 | p = hash_get_mem(the_strtab_hash, tdh->string_table_name); |
| 230 | if (!p) { |
| 231 | fprintf(ofp, "Fatal: couldn't find string table\n"); |
| 232 | return(1); |
| 233 | } |
| 234 | this_strtab = (u8 *)p[0]; |
| 235 | |
| 236 | tp = (track_definition_t *)(tdh+1); |
| 237 | |
| 238 | for (i = 0; i < nevents; i++) { |
| 239 | track_code = ntohl(tp->track); |
| 240 | p = hash_get(the_trackdef_hash, track_code); |
| 241 | if (p) { |
| 242 | fprintf(stderr, "track %d redefined, retain first definition\n", |
| 243 | track_code); |
| 244 | continue; |
| 245 | } |
| 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); |
| 250 | |
| 251 | if (verbose) { |
| 252 | fprintf(stderr, "adding track '%s'\n", btp->track_str); |
| 253 | } |
| 254 | |
| 255 | thislen = strlen((char *) btp->track_str); |
| 256 | if (thislen > widest_track_format) |
| 257 | widest_track_format = thislen; |
| 258 | |
| 259 | /* convert track_str "eth_server t11(20498)" to "pid:20498 tid:11" */ |
| 260 | cp = btp->track_str; |
| 261 | while (*cp && *cp != '(') |
| 262 | cp++; |
| 263 | if (!*cp) { |
| 264 | fprintf(stderr, "error canonicalizing '%s'\n", btp->track_str); |
| 265 | goto out; |
| 266 | } |
| 267 | pidstr = cp+1; /* remember location of PID */ |
| 268 | |
| 269 | while (cp > btp->track_str && *cp != 't') |
| 270 | cp--; |
| 271 | |
| 272 | if (cp == btp->track_str) { |
| 273 | fprintf(stderr, "error canonicalizing '%s'\n", btp->track_str); |
| 274 | goto out; |
| 275 | } |
| 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); |
| 280 | |
| 281 | /* |
| 282 | * Note: duplicates are possible due to thread create / |
| 283 | * thread destroy operations. |
| 284 | */ |
| 285 | p = hash_get_mem(the_pidtid_hash, pidtid_str); |
| 286 | if (p) { |
| 287 | vec_free(pidtid_str); |
| 288 | goto out; |
| 289 | } |
| 290 | hash_set_mem(the_pidtid_hash, pidtid_str, btp - bound_tracks); |
| 291 | |
| 292 | out: |
| 293 | tp++; |
| 294 | } |
| 295 | have_trackdefs = 1; |
| 296 | return (0); |
| 297 | } |
| 298 | |
| 299 | int unsupported_pass (cpel_section_header_t *sh, int verbose, FILE *ofp) |
| 300 | { |
| 301 | if (verbose) { |
| 302 | fprintf(ofp, "Unsupported type %d section\n", |
| 303 | ntohl(sh->section_type)); |
| 304 | } |
| 305 | return(0); |
| 306 | } |
| 307 | |
| 308 | int event_pass2(cpel_section_header_t *sh, int verbose, FILE *ofp) |
| 309 | { |
| 310 | event_section_header_t *eh; |
| 311 | int nevents; |
| 312 | int i; |
| 313 | uword *p; |
| 314 | event_entry_t *ep; |
| 315 | u64 now; |
| 316 | u32 time0, time1; |
| 317 | u32 track_code; |
| 318 | u8 *this_strtab; |
| 319 | u64 ticks_in_state; |
| 320 | bound_track_t *btp; |
| 321 | bound_track_t *state_track=0; |
| 322 | u8 *pidtid_str; |
| 323 | u8 *pidtid_dup; |
| 324 | u8 *ecp; |
| 325 | u32 event_code; |
| 326 | |
| 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; |
| 330 | |
| 331 | if (verbose) { |
| 332 | fprintf(ofp, "%.3f ticks_per_us\n", ticks_per_us); |
| 333 | } |
| 334 | |
| 335 | ep = (event_entry_t *)(eh+1); |
| 336 | |
| 337 | p = hash_get_mem(the_strtab_hash, eh->string_table_name); |
| 338 | if (!p) { |
| 339 | fprintf(ofp, "Fatal: couldn't find string table\n"); |
| 340 | return(1); |
| 341 | } |
| 342 | this_strtab = (u8 *)p[0]; |
| 343 | |
| 344 | /* |
| 345 | * Some logger implementation that doesn't produce |
| 346 | * trackdef sections, synthesize the bound_tracks vector |
| 347 | */ |
| 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); |
| 354 | if (!p) { |
| 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); |
| 360 | } else { |
| 361 | vec_free(pidtid_dup); |
| 362 | } |
| 363 | ep++; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | ep = (event_entry_t *)(eh+1); |
| 368 | |
| 369 | for (i = 0; i < nevents; i++) { |
| 370 | time0 = ntohl (ep->time[0]); |
| 371 | time1 = ntohl (ep->time[1]); |
| 372 | |
| 373 | now = (((u64) time0)<<32) | time1; |
| 374 | |
| 375 | event_code = ntohl(ep->event_code); |
| 376 | |
| 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 == ' ') |
| 385 | *ecp = 0; |
| 386 | } else { |
| 387 | pidtid_dup = format(0, "%d", ntohl(ep->track)); |
| 388 | vec_add1(pidtid_dup, 0); |
| 389 | } |
| 390 | |
| 391 | p = hash_get_mem(the_pidtid_hash, pidtid_dup); |
| 392 | if (!p) { |
| 393 | fprintf(stderr, "warning: couldn't find '%s'\n", |
| 394 | pidtid_dup); |
| 395 | vec_free(pidtid_dup); |
| 396 | ep++; |
| 397 | continue; |
| 398 | } |
| 399 | state_track = &bound_tracks[p[0]]; |
| 400 | } |
| 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) { |
| 405 | /* |
| 406 | * Add a ticks-in-state record, unless |
| 407 | * e.g. the log started with the exit event |
| 408 | */ |
| 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; |
| 413 | } |
| 414 | /* Otherwise, nothing */ |
| 415 | } |
| 416 | ep++; |
| 417 | } |
| 418 | return(0); |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Note: If necessary, add passes / columns to this table to |
| 423 | * handle section order dependencies. |
| 424 | */ |
| 425 | |
| 426 | section_processor_t processors[CPEL_NUM_SECTION_TYPES+1] = |
| 427 | { |
| 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 */ |
| 434 | }; |
| 435 | |
| 436 | |
| 437 | int process_section(cpel_section_header_t *sh, int verbose, FILE *ofp, |
| 438 | pass_t pass) |
| 439 | { |
| 440 | u32 type; |
| 441 | type = ntohl(sh->section_type); |
| 442 | int rv; |
| 443 | int (*fp)(cpel_section_header_t *, int, FILE *); |
| 444 | |
| 445 | if (type > CPEL_NUM_SECTION_TYPES) { |
| 446 | fprintf(stderr, "Unknown section type %d\n", type); |
| 447 | return(1); |
| 448 | } |
| 449 | switch(pass) { |
| 450 | case PASS1: |
| 451 | fp = processors[type].pass1; |
| 452 | break; |
| 453 | |
| 454 | case PASS2: |
| 455 | fp = processors[type].pass2; |
| 456 | break; |
| 457 | |
| 458 | default: |
| 459 | fprintf(stderr, "Unknown pass %d\n", pass); |
| 460 | return(1); |
| 461 | } |
| 462 | |
| 463 | rv = (*fp)(sh, verbose, ofp); |
| 464 | |
| 465 | return(rv); |
| 466 | } |
| 467 | |
| 468 | int cpel_dump_file_header(cpel_file_header_t *fh, int verbose, FILE *ofp) |
| 469 | { |
| 470 | time_t file_time; |
| 471 | |
| 472 | if (verbose) { |
| 473 | fprintf(ofp, "CPEL file: %s-endian, version %d\n", |
| 474 | ((fh->endian_version & CPEL_FILE_LITTLE_ENDIAN) ? |
| 475 | "little" : "big"), |
| 476 | fh->endian_version & CPEL_FILE_VERSION_MASK); |
| 477 | |
| 478 | file_time = ntohl(fh->file_date); |
| 479 | |
| 480 | fprintf(ofp, "File created %s", ctime(&file_time)); |
| 481 | fprintf(ofp, "File has %d sections\n", |
| 482 | ntohs(fh->nsections)); |
| 483 | } |
| 484 | |
| 485 | return(0); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | int cpel_dump(u8 *cpel, int verbose, FILE *ofp) |
| 490 | { |
| 491 | cpel_file_header_t *fh; |
| 492 | cpel_section_header_t *sh; |
| 493 | u16 nsections; |
| 494 | u32 section_size; |
| 495 | int i; |
| 496 | |
| 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"); |
| 502 | return(1); |
| 503 | } |
| 504 | fprintf(stderr, "Unsupported file version 0x%x\n", |
| 505 | fh->endian_version); |
| 506 | return(1); |
| 507 | } |
| 508 | cpel_dump_file_header(fh, verbose, ofp); |
| 509 | nsections = ntohs(fh->nsections); |
| 510 | |
| 511 | /* |
| 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. |
| 515 | */ |
| 516 | sh = (cpel_section_header_t *)(fh+1); |
| 517 | for (i = 0; i < nsections; i++) { |
| 518 | section_size = ntohl(sh->data_length); |
| 519 | |
| 520 | if(verbose) { |
| 521 | fprintf(ofp, "Section type %d, size %d\n", ntohl(sh->section_type), |
| 522 | section_size); |
| 523 | } |
| 524 | |
| 525 | if(process_section(sh, verbose, ofp, PASS1)) |
| 526 | return(1); |
| 527 | |
| 528 | sh++; |
| 529 | sh = (cpel_section_header_t *)(((u8 *)sh)+section_size); |
| 530 | } |
| 531 | |
| 532 | sh = (cpel_section_header_t *)(fh+1); |
| 533 | for (i = 0; i < nsections; i++) { |
| 534 | if(process_section(sh, verbose, ofp, PASS2)) |
| 535 | return(1); |
| 536 | section_size = ntohl(sh->data_length); |
| 537 | sh++; |
| 538 | sh = (cpel_section_header_t *)(((u8 *)sh)+section_size); |
| 539 | } |
| 540 | return(0); |
| 541 | } |
| 542 | |
| 543 | void compute_state_statistics(int verbose, FILE *ofp) |
| 544 | { |
| 545 | int i, j; |
| 546 | bound_track_t *bp; |
| 547 | f64 fticks; |
| 548 | |
| 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]; |
| 557 | } |
| 558 | /* Compute mean */ |
| 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)); |
| 562 | } |
| 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); |
| 569 | } |
| 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); |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | int track_compare_max (const void *arg1, const void *arg2) |
| 579 | { |
| 580 | bound_track_t *a1 = (bound_track_t *)arg1; |
| 581 | bound_track_t *a2 = (bound_track_t *)arg2; |
| 582 | f64 v1, v2; |
| 583 | |
| 584 | v1 = a1->total_ticks_in_state; |
| 585 | v2 = a2->total_ticks_in_state; |
| 586 | |
| 587 | if (v1 < v2) |
| 588 | return (1); |
| 589 | else if (v1 == v2) |
| 590 | return (0); |
| 591 | else return (-1); |
| 592 | } |
| 593 | |
| 594 | int track_compare_occurrences (const void *arg1, const void *arg2) |
| 595 | { |
| 596 | bound_track_t *a1 = (bound_track_t *)arg1; |
| 597 | bound_track_t *a2 = (bound_track_t *)arg2; |
| 598 | f64 v1, v2; |
| 599 | |
| 600 | v1 = (f64) vec_len(a1->ticks_in_state); |
| 601 | v2 = (f64) vec_len(a2->ticks_in_state); |
| 602 | |
| 603 | if (v1 < v2) |
| 604 | return (1); |
| 605 | else if (v1 == v2) |
| 606 | return (0); |
| 607 | else return (-1); |
| 608 | } |
| 609 | |
| 610 | int track_compare_name (const void *arg1, const void *arg2) |
| 611 | { |
| 612 | bound_track_t *a1 = (bound_track_t *)arg1; |
| 613 | bound_track_t *a2 = (bound_track_t *)arg2; |
| 614 | |
| 615 | return (strcmp((char *)(a1->track_str), (char *)(a2->track_str))); |
| 616 | } |
| 617 | |
| 618 | void sort_state_statistics(sort_t type, FILE *ofp) |
| 619 | { |
| 620 | int (*compare)(const void *, const void *) = 0; |
| 621 | |
| 622 | if (summary_stats_only) |
| 623 | return; |
| 624 | |
| 625 | switch(type) { |
| 626 | case SORT_MAX_TIME: |
| 627 | fprintf(ofp, "Results sorted by max time in state.\n\n"); |
| 628 | compare = track_compare_max; |
| 629 | break; |
| 630 | |
| 631 | case SORT_MAX_OCCURRENCES: |
| 632 | fprintf(ofp, "Results sorted by max occurrences of state.\n\n"); |
| 633 | compare = track_compare_occurrences; |
| 634 | break; |
| 635 | |
| 636 | case SORT_NAME: |
| 637 | compare = track_compare_name; |
| 638 | fprintf(ofp, "Results sorted by process name, thread ID, PID\n\n"); |
| 639 | break; |
| 640 | |
| 641 | default: |
| 642 | fatal("sort type not set?"); |
| 643 | } |
| 644 | |
| 645 | qsort (bound_tracks, vec_len(bound_tracks), |
| 646 | sizeof (bound_track_t), compare); |
| 647 | } |
| 648 | |
| 649 | void print_state_statistics(int verbose, FILE *ofp) |
| 650 | { |
| 651 | int i,j; |
| 652 | u8 *trackpad; |
| 653 | bound_track_t *bp; |
| 654 | f64 total_time = 0.0; |
| 655 | f64 total_switches = 0.0; |
| 656 | |
| 657 | trackpad = format(0, "%%-%ds ", widest_track_format); |
| 658 | vec_add1(trackpad, 0); |
| 659 | |
| 660 | if (!summary_stats_only) { |
| 661 | fprintf(ofp, (char *)trackpad, "ProcName Thread(PID)"); |
| 662 | fprintf(ofp, " Mean(us) Stdev(us) Total(us) N\n"); |
| 663 | } |
| 664 | |
| 665 | for (i = 0; i < vec_len(bound_tracks); i++) { |
| 666 | bp = &bound_tracks[i]; |
| 667 | if (bp->mean_ticks_in_state == 0.0) |
| 668 | continue; |
| 669 | |
| 670 | if (name_filter && |
| 671 | strncmp((char *)bp->track_str, (char *)name_filter, |
| 672 | strlen((char *)name_filter))) |
| 673 | continue; |
| 674 | |
| 675 | /* |
| 676 | * Exclude kernel threads (e.g. idle thread) from |
| 677 | * state statistics |
| 678 | */ |
| 679 | if (exclude_kernel_from_summary_stats && |
| 680 | !strncmp((char *) bp->track_str, "kernel ", 7)) |
| 681 | continue; |
| 682 | |
| 683 | total_switches += (f64) vec_len(bp->ticks_in_state); |
| 684 | |
| 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) |
| 690 | / ticks_per_us); |
| 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)); |
| 694 | } |
| 695 | |
| 696 | if (scatterplot) { |
| 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); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | total_time += bp->total_ticks_in_state; |
| 704 | } |
| 705 | |
| 706 | if (!summary_stats_only) |
| 707 | fprintf(ofp, "\n"); |
| 708 | fprintf(ofp, "Note: the following statistics %s kernel-thread activity.\n", |
| 709 | exclude_kernel_from_summary_stats ? "exclude" : "include"); |
| 710 | if (name_filter) |
| 711 | fprintf(ofp, |
| 712 | "Note: only pid/proc/threads matching '%s' are included.\n", |
| 713 | name_filter); |
| 714 | |
| 715 | fprintf(ofp, |
| 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); |
| 722 | } |
| 723 | |
| 724 | char *mapfile (char *file) |
| 725 | { |
| 726 | struct stat statb; |
| 727 | char *rv; |
| 728 | int maphfile; |
| 729 | size_t mapfsize; |
| 730 | |
| 731 | maphfile = open (file, O_RDONLY); |
| 732 | |
| 733 | if (maphfile < 0) |
| 734 | { |
| 735 | fprintf (stderr, "Couldn't read %s, skipping it...\n", file); |
| 736 | return (NULL); |
| 737 | } |
| 738 | |
| 739 | if (fstat (maphfile, &statb) < 0) |
| 740 | { |
| 741 | fprintf (stderr, "Couldn't get size of %s, skipping it...\n", file); |
| 742 | return (NULL); |
| 743 | } |
| 744 | |
| 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); |
| 748 | return (NULL); |
| 749 | } |
| 750 | |
| 751 | mapfsize = statb.st_size; |
| 752 | |
| 753 | if (mapfsize < 3) |
| 754 | { |
| 755 | fprintf (stderr, "%s zero-length, skipping it...\n", file); |
| 756 | close (maphfile); |
| 757 | return (NULL); |
| 758 | } |
| 759 | |
| 760 | rv = mmap (0, mapfsize, PROT_READ, MAP_SHARED, maphfile, 0); |
| 761 | |
| 762 | if (rv == 0) |
| 763 | { |
| 764 | fprintf (stderr, "%s problem mapping, I quit...\n", file); |
| 765 | exit (-1); |
| 766 | } |
| 767 | close (maphfile); |
| 768 | return (rv); |
| 769 | } |
| 770 | |
| 771 | /* |
| 772 | * main |
| 773 | */ |
| 774 | int main (int argc, char **argv) |
| 775 | { |
| 776 | char *cpel_file = 0; |
| 777 | char *outputfile = 0; |
| 778 | FILE *ofp; |
| 779 | char *cpel; |
| 780 | int verbose=0; |
| 781 | int curarg=1; |
| 782 | |
| 783 | while (curarg < argc) { |
| 784 | if (!strncmp(argv[curarg], "--input-file", 3)) { |
| 785 | curarg++; |
| 786 | if (curarg < argc) { |
| 787 | cpel_file = argv[curarg]; |
| 788 | curarg++; |
| 789 | continue; |
| 790 | } |
| 791 | fatal("Missing filename after --input-file\n"); |
| 792 | } |
| 793 | if (!strncmp(argv[curarg], "--output-file", 3)) { |
| 794 | curarg ++; |
| 795 | if (curarg < argc) { |
| 796 | outputfile = argv[curarg]; |
| 797 | curarg ++; |
| 798 | continue; |
| 799 | } |
| 800 | fatal("Missing filename after --output-file\n"); |
| 801 | } |
| 802 | if (!strncmp(argv[curarg], "--verbose", 3)) { |
| 803 | curarg++; |
| 804 | verbose++; |
| 805 | continue; |
| 806 | } |
| 807 | if (!strncmp(argv[curarg], "--scatterplot", 4)) { |
| 808 | curarg++; |
| 809 | scatterplot=1; |
| 810 | continue; |
| 811 | } |
| 812 | |
| 813 | if (!strncmp(argv[curarg], "--start-event", 4)) { |
| 814 | curarg++; |
| 815 | if (curarg < argc) { |
| 816 | start_event_code = atol(argv[curarg]); |
| 817 | curarg ++; |
| 818 | continue; |
| 819 | } |
| 820 | fatal("Missing integer after --start-event\n"); |
| 821 | } |
| 822 | if (!strncmp(argv[curarg], "--end-event", 4)) { |
| 823 | curarg++; |
| 824 | if (curarg < argc) { |
| 825 | end_event_code = atol(argv[curarg]); |
| 826 | curarg ++; |
| 827 | continue; |
| 828 | } |
| 829 | fatal("Missing integer after --end-event\n"); |
| 830 | } |
| 831 | if (!strncmp(argv[curarg], "--max-time-sort", 7)) { |
| 832 | sort_type = SORT_MAX_TIME; |
| 833 | curarg++; |
| 834 | continue; |
| 835 | } |
| 836 | if (!strncmp(argv[curarg], "--max-occurrence-sort", 7)) { |
| 837 | sort_type = SORT_MAX_OCCURRENCES; |
| 838 | curarg++; |
| 839 | continue; |
| 840 | } |
| 841 | if (!strncmp(argv[curarg], "--name-sort", 3)) { |
| 842 | sort_type = SORT_NAME; |
| 843 | curarg++; |
| 844 | continue; |
| 845 | } |
| 846 | if (!strncmp(argv[curarg], "--kernel-included", 3)) { |
| 847 | exclude_kernel_from_summary_stats = 0; |
| 848 | curarg++; |
| 849 | continue; |
| 850 | } |
| 851 | if (!strncmp(argv[curarg], "--summary", 3)) { |
| 852 | summary_stats_only=1; |
| 853 | curarg++; |
| 854 | continue; |
| 855 | } |
| 856 | if (!strncmp(argv[curarg], "--filter", 3)) { |
| 857 | curarg ++; |
| 858 | if (curarg < argc) { |
| 859 | name_filter = (u8 *) argv[curarg]; |
| 860 | curarg ++; |
| 861 | continue; |
| 862 | } |
| 863 | fatal("Missing filter string after --filter\n"); |
| 864 | } |
| 865 | |
| 866 | |
| 867 | usage: |
| 868 | fprintf(stderr, |
| 869 | "cpelatency --input-file <filename> [--output-file <filename>]\n"); |
| 870 | fprintf(stderr, |
| 871 | " [--start-event <decimal>] [--verbose]\n"); |
| 872 | fprintf(stderr, |
| 873 | " [--end-event <decimal>]\n"); |
| 874 | fprintf(stderr, |
| 875 | " [--max-time-sort(default) | --max-occurrence-sort |\n"); |
| 876 | |
| 877 | fprintf(stderr, |
| 878 | " --name-sort-sort] [--kernel-included]\n"); |
| 879 | |
| 880 | fprintf(stderr, |
| 881 | " [--summary-stats-only] [--scatterplot]\n"); |
| 882 | |
| 883 | fprintf(stderr, "%s\n", version); |
| 884 | exit(1); |
| 885 | } |
| 886 | |
| 887 | if (cpel_file == 0) |
| 888 | goto usage; |
| 889 | |
| 890 | cpel = mapfile(cpel_file); |
| 891 | if (cpel == 0) { |
| 892 | fprintf(stderr, "Couldn't map %s...\n", cpel_file); |
| 893 | exit(1); |
| 894 | } |
| 895 | |
| 896 | if (!outputfile) { |
| 897 | ofp = fdopen(1, "w"); |
| 898 | if (ofp == NULL) { |
| 899 | fprintf(stderr, "Couldn't fdopen(1)?\n"); |
| 900 | exit(1); |
| 901 | } |
| 902 | } else { |
| 903 | ofp = fopen(outputfile, "w"); |
| 904 | if (ofp == NULL) { |
| 905 | fprintf(stderr, "Couldn't create %s...\n", outputfile); |
| 906 | exit(1); |
| 907 | } |
| 908 | } |
| 909 | |
| 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)); |
| 914 | |
| 915 | if (cpel_dump((u8 *)cpel, verbose, ofp)) { |
| 916 | if (outputfile) |
| 917 | unlink(outputfile); |
| 918 | } |
| 919 | |
| 920 | compute_state_statistics(verbose, ofp); |
| 921 | sort_state_statistics(sort_type, ofp); |
| 922 | print_state_statistics(verbose, ofp); |
| 923 | |
| 924 | fclose(ofp); |
| 925 | return(0); |
| 926 | } |