togreport.c 15.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_INPUT 256
#define CHUNK_TOGGLE	    50000
#define CHUNK_STRING	    100000
#define HASH_TABLE_SIZE	    32768
#define MAX_TOGGLE_CHUNKS   100

#define NET_CMD		1
#define IGNORE_CMD	2
#define SUMMARY_CMD	3
#define COMMENT_CMD	4
#define QUIT_CMD	5
#define LIST_CMD	6
#define HELP_CMD	7
#define NULL_CMD	8
#define UNKNOWN_CMD	9
#define INPUT_CMD	10
#define OUTPUT_CMD	11
#define APPEND_CMD	12
#define THRESHOLD_CMD	13
#define TOGGLETO_CMD	14

#define LIST_FLAG	0x0001		/* set by net, cleared by ignore */

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

struct toggle {
    char *netname;
    long flags;
    long tog_to_0;
    long tog_to_1;
    long tog_sum;
};

struct hash_entry {
    struct toggle *tp;
    struct hash_entry *next;
};

struct hash_entry hash_table[HASH_TABLE_SIZE];

struct toggle *toggle_tbl[MAX_TOGGLE_CHUNKS];
enum wildtype {WILD_NONE, WILD_POUND, WILD_STAR, WILD_TILDE};
enum toggletype {SUM, TO_0, TO_1};


char line[MAX_INPUT];
int interactive;
FILE *output;
int threshold = 0;
enum toggletype toggletype = SUM;

/* prototypes */
int net(char *, int);
void summary(FILE *);
void list(FILE *);
void readtogfile(FILE *);
int parsecmd(char *, char **);
int iswild(char *);
int wildmatchname(char *, char *);
void readcommands(FILE *);
void addhash(struct toggle *);
struct toggle *locatehash(char *);
unsigned long calchash(char *);

main(int argc, char *argv[])
{
    extern char *optarg;
    extern int optind, opterr, optopt;
    int c;
    FILE *input, *togfile;
    char *input_name, *output_name, *model_name;
    
    /* parse arguments */
    interactive = 0;
    input_name = 0;
    output_name = 0;
    model_name = 0;
    input = stdin;
    output = stdout;
    
    while ((c = getopt(argc, argv, "ihm:d:o:")) != EOF) {
	switch (c) {
	case 'h':
	    fprintf(stdout, "Usage: %s <-i> -m model <-o output> <-d input>\n", argv[0]);
	    exit(0);
	    break;
	case 'i':
	    interactive = 1;
	    break;
	case 'm':
	    model_name = optarg;
	    break;
	case 'o':
	    output_name = optarg;
	    break;
	case 'd':
	    input_name = optarg;
	    break;
	}
    }
    
    /* open files */
    if (!model_name) {
	fprintf(stderr, "Need model name (-m option)\n");
	exit(1);
    }
    sprintf(line, "%s.tog", model_name);
    if ((togfile = fopen(line, "r")) == NULL) {
	fprintf(stderr, "Cannot open %s\n", line);
	exit(1);
    }
    
    if (!interactive && !input_name) {
	fprintf(stderr, "Need input file name or interactive mode (-d or -i)\n");
	exit(1);
	
    }
    if (input_name) {
	if ((input = fopen(input_name, "r")) == NULL) {
	    fprintf(stderr, "Cannot open %s\n", input_name);
	    exit(1);
	}
    }
    
    if (output_name) {
	if ((output = fopen(output_name, "w")) == NULL) {
	    fprintf(stderr, "Cannot open %s\n", output_name);
	    exit(1);
	}
    }
    
    fprintf(stdout, "togreport version 3.14159\nReading tog file, please wait\n");
    fflush(stdout); 
    
    
    /* initialize the toggle database */
    readtogfile(togfile);
    
    /* process commands */
    readcommands(input);
    exit(0);
}
  
void  
readcommands(FILE *input)
{
    FILE *newinput, *newoutput;
    char *argp;
    
    while (1) {
	if (interactive) {
	    fprintf(stdout, "togreport> ");
	    fflush(stdout);
	}
	if (fgets(line, MAX_INPUT, input) == NULL) {
	    return;
	}
	if (interactive && input != stdin) {
	    fputs(line, stdout);
	}
	switch (parsecmd(line, &argp)) {
	
	case NET_CMD:
	    if (net(argp, TRUE) == 0) {
		fprintf(output, "Could not find netname <%s>\n", argp);
	    }
	    break;
	    
	case IGNORE_CMD:
	    if (net(argp, FALSE) == 0) {
		fprintf(output, "Could not find netname <%s>\n", argp);
	    }
	    break;
	    
	case SUMMARY_CMD:
	    summary(output);
	    break;
	    
	case LIST_CMD:
	    list(output);
	    break;
	
	case QUIT_CMD:
	    return;
	    break;
	    
	case COMMENT_CMD:
	    fputs(line, output);
	    break;
	
	case HELP_CMD:
	    fprintf(output, "Commands are:\n");
	    fprintf(output, "help - print this message\n");
	    fprintf(output, "ignore netname - delete netname from list of considered nets\n");
	    fprintf(output, "list - print list of untoggled nets\n");
	    fprintf(output, "net netname - add netname to list of considered nets\n");
	    fprintf(output, "quit - bye bye\n");
	    fprintf(output, "summary - print summary of toggle coverage\n");
	    fprintf(output, "lookat sum or to_0 or to_1\n");
	    fprintf(output, "threshold n - set threshold count to n (default is 0)\n");
	    fprintf(output, "-- comment line\n");
	    fprintf(output, "< filename - get input commands from new file\n");
	    fprintf(output, "> filename - output to new file (\"\" is stdout)\n");
	    fprintf(output, ">> filename - append to file\n\n");
	    fprintf(output, "netname wild cards (from ATTEST):\n");
	    fprintf(output, "~ tilde matches any number of characters,\n");
	    fprintf(output, "\teven through hierarchy boundaries\n");
	    fprintf(output, "* asterisk matches any number of characters,\n");
	    fprintf(output, "\tup to a hierarchy boundary\n");
	    fprintf(output, "# pound matches one or more digits\n");
	    fprintf(output, "\n");
	    break;
	    
	case NULL_CMD:
	    break;
	    
	case UNKNOWN_CMD:
	    fprintf(output, "Unknown command\n");
	    break;
	    
	case INPUT_CMD:
	    if ((newinput = fopen(argp, "r")) == NULL) {
		fprintf(stderr, "Cannot open %s\n", argp);
	    } else {
		readcommands(newinput);
	    }
	    break;
	    
	case OUTPUT_CMD:
	    if (strcmp(argp, "\"\"") == 0) {
		output = stdout;
	    } else if ((newoutput = fopen(argp, "w")) == NULL) {
		fprintf(stderr, "Cannot open %s\n", argp);
	    } else {
		output = newoutput;
	    }
	    break;
	    
	case APPEND_CMD:
	    if ((newoutput = fopen(argp, "a")) == NULL) {
		fprintf(stderr, "Cannot open %s\n", argp);
	    } else {
		output = newoutput;
	    }
	    break;
	    
	case TOGGLETO_CMD:
	    if (strcmp(argp, "sum") == 0) {
		toggletype = SUM;
	    } else if (strcmp(argp, "to_0") == 0) {
		toggletype = TO_0;
	    } else if (strcmp(argp, "to_1") == 0) {
		toggletype = TO_1;
	    }
	    break;
	    
	case THRESHOLD_CMD:
	    threshold = atoi(argp);
	    break;
	}
    }
}

/*
 * Parse command for string passed in.  (string still has newline)
 *
 * Valid commands:
 *   net netname
 *   ignore netname
 *   summary
 *   list
 *   quit
 *   help
 */
int
parsecmd(char *l, char **p)
{
    int cmd;
    char *cp;
    
    cp = l;
    
    cmd = UNKNOWN_CMD;
    
    while (*cp && isspace(*cp)) {
	cp++;
    }
    
    /* have l point to first white space after *cp */
    l = cp;
    while (!isspace(*l))
	l++;
	
    switch (*cp) {
    
    case 'n':
	if ((strncmp(cp, "net", 3) == 0) && (l - cp == 3)) {
	    cmd = NET_CMD;
	}
	break;

    case 'h':
	if ((strncmp(cp, "help", 4) == 0) && (l - cp == 4)) {
	    cmd = HELP_CMD;
	}
	break;

    case 'i':
	if ((strncmp(cp, "ignore", 6) == 0) && (l - cp == 6)) {
	    cmd = IGNORE_CMD;
	}
	break;

    case 's':
	if ((strncmp(cp, "summary", 7) == 0) && (l - cp == 7)) {
	    cmd = SUMMARY_CMD;
	}
	break;

    case 't':
	if ((strncmp(cp, "threshold", 9) == 0) && (l - cp == 9)) {
	    cmd = THRESHOLD_CMD;
	}
	break;

    case 'l':
	if ((strncmp(cp, "list", 4) == 0) && (l - cp == 4)) {
	    cmd = LIST_CMD;
	}
	if ((strncmp(cp, "lookat", 6) == 0) && (l - cp == 6)) {
	    cmd = TOGGLETO_CMD;
	}
	break;

    case 'q':
	if ((strncmp(cp, "quit", 4) == 0) && (l - cp == 4)) {
	    cmd = QUIT_CMD;
	}
	break;
    
    case '-':
	if (strncmp(cp, "--", 2) == 0) {
	    cmd = COMMENT_CMD;
	}
	break;
	
    case '<':
	if (strncmp(cp, "<", 1) == 0 && (l - cp == 1)) {
	    cmd = INPUT_CMD;
	}
	break;
	
    case '>':
	if ((l - cp) == 1) {
	    cmd = OUTPUT_CMD;
	} else if ((l - cp) == 2 && *(cp+1) == '>') {
	    cmd = APPEND_CMD;
	}
	break;
	
    case '\0':
	cmd = NULL_CMD;
	break;
	
    }
    
    while (isspace(*l))
	l++;
    *p = l;
    while (!isspace(*l) && *l )
	l++;
    *l = '\0';
    
    return cmd;
}

void
readtogfile(FILE *tfile)
{
    int tableindex, i, stringbytes, namesize;
    struct toggle *tp;
    char *stringarea;
    long sum, t0, t1;
    char netname[256];
    
    /* jump start the table & string area */
    toggle_tbl[0] = (struct toggle *)calloc(CHUNK_TOGGLE, sizeof(struct toggle));
    tp = toggle_tbl[0];
    tableindex = 0;
    i = 0;
    stringarea = (char *)malloc(CHUNK_STRING);
    stringbytes = CHUNK_STRING;
        
    while (fgets(line, MAX_INPUT, tfile) != NULL) {
	if (sscanf(line, "%d %d %d %s\n", &sum, &t0, &t1, netname) == 4) {
	    /* add to toggle list */
	    namesize = strlen(netname) + 1;
	    /* check for string area overflow */
	    if (namesize > stringbytes) {
		stringarea = (char *)malloc(CHUNK_STRING);
		stringbytes = CHUNK_STRING;
	    }
	    strcpy(stringarea, netname);
	    tp->netname = stringarea;
	    stringarea += namesize;
	    stringbytes -= namesize;
	    tp->tog_to_0 = t0;
	    tp->tog_to_1 = t1;
	    tp->tog_sum = sum;
	    tp->flags = 0;
	    addhash(tp);
	    tp++;
	    i++;
	    
	    /* check for chunk overflow */
	    if (i == CHUNK_TOGGLE) {
		tableindex++;
		if (tableindex == MAX_TOGGLE_CHUNKS) {
		    fprintf(stderr, "oh S---,  huge number of nodes\n");
		    exit(1);
		}
		toggle_tbl[tableindex] = (struct toggle *)calloc(CHUNK_TOGGLE,
			sizeof(struct toggle));
		tp = toggle_tbl[tableindex];
		i = 0;
	    }
	}
    }
printf("done with readtogfile.  %d netnames\n", (tableindex*CHUNK_TOGGLE) + i);
}

int
net(char *match, int addflag)
{
    int i, nmatches;
    int tableindex;
    struct toggle *tp;

    nmatches = 0;
    if (iswild(match)) {
	tableindex = 0;
	i = 0;
	tp = toggle_tbl[tableindex];
	
	while (tp) {
	
	    if (tp->netname && wildmatchname(tp->netname, match)) {
		nmatches++;
		if (addflag) {
		    tp->flags |= LIST_FLAG;
		} else {
		    tp->flags &= ~LIST_FLAG;		    
		}
	    }
	    
	    i++;
	    if (i == CHUNK_TOGGLE) {
		tableindex++;
		tp = toggle_tbl[tableindex];
		i = 0;
	    } else {
		tp++;
	    }
	}
	
    } else {
	tp = locatehash(match);
	if (tp) {
	    nmatches++;
	    if (addflag) {
		tp->flags |= LIST_FLAG;
	    } else {
		tp->flags &= ~LIST_FLAG;		    
	    }
	}
    }
    
    return nmatches;
}

int
iswild(char *match)
{
    char c;
    
    c = *match;
    
    while ((c = *match)) {
	if (c == '*' || c == '#' || c == '~') {
	    return TRUE;
	} else {
	    match++;
	}
    }
    return FALSE;
}

/*
 * XXX rewrite this section.  Need to work on performance and make 
 * sure it works!!
 *
 * match a netname against a matchstring
 * special characters in match string are:
 *	* - match anything up to a .
 *	~ - match anything
 *	# - match one or more digits
 */
int
wildmatchname(char *netname, char *match)
{
    char *prefix_str, suffix_str[MAX_INPUT];
    int prefix_len, suffix_len;
    char *n, *m1, *m2, c;
    int len;
    enum wildtype wild;
    
    m1 = match;
    prefix_str = m1;
    prefix_len = 0;
    while (1) {
	c = *m1;
	if (c == '\0') {
	    wild = WILD_NONE;
	    break;
	} else if (c == '*') {
	    wild = WILD_STAR;
	    break;
	} else if (c == '#') {
	    wild = WILD_POUND;
	    break;
	} else if (c == '~') {
	    wild = WILD_TILDE;
	    break;
	} else {
	    m1++;
	    prefix_len++;
	}
    }
    /* XXX fix this restriction skip over wild card characters */
    while (1) {
	c = *m1;
	if (c == '#' || c == '~' || c == '*') {
	    m1++;
	} else {
	    break;
	}
    }
    m2 = m1;
    suffix_len = 0;
    while (1) {
	c = *m1;
	if (c == '\0' || c == '*' || c == '#' || c == '~') {
	    break;
	} else {
	    m1++;
	    suffix_len++;
	}
    }
    if (suffix_len) {
	strncpy(suffix_str, m2, suffix_len);
	*(suffix_str + suffix_len) = '\0';
	match = m2;
    } else {
	match = m1;
    }
	
    /* now match netname against prefix, wild card and suffix */
    if (prefix_len) {
	if (strncmp(netname, prefix_str, prefix_len) != 0) {
	    return FALSE;
	} else {
	    netname += prefix_len;
	} 
    }
    
    /* now match the wildcard with a possible suffix */
    if (wild != WILD_NONE) {
	if (wild == WILD_POUND) {
	    while (isdigit(*netname)) {
		netname++;
	    }
	} else if (wild == WILD_TILDE) {
	     if (suffix_len) {
		 m1 = strstr(netname, suffix_str);
		 if (m1 == NULL)
		    return FALSE;
		 else
		    netname = m1;
	     } else {
		 return TRUE;
	     }
	} else {    /* a WILD_STAR stop at suffix or at next '.' */
	    m2 = strchr(netname, '.');
	    if (suffix_len) {
		m1 = strstr(netname, suffix_str);
		if (m2 && m2 < m1)
		    m1 = m2;
		if (m1 == NULL)
		    return FALSE;
		else
		    netname = m1;
	    } else {
		if (m2) {
		    netname = m2;
		} else {
		    netname = netname + strlen(netname);
		}
	    }
	}
    }
    
    if (*netname == '\0' && *match == '\0') {
        return TRUE;
    } else if (*netname == '\0' || *match == '\0') {
        return FALSE;
    } else {
	return wildmatchname(netname, match);
    }
}

void
summary(FILE *out)
{
    int i;
    int tableindex;
    struct toggle *tp;
    long totalnets, subtotalnets, togglednets;
    char *s;
    
    tableindex = 0;
    i = 0;
    tp = toggle_tbl[tableindex];
    
    totalnets = 0;
    subtotalnets = 0;
    togglednets = 0;
    
    while (tp) {
    
	if (tp->netname) {
	    totalnets++;
	}
	
	if (tp->flags & LIST_FLAG) {
	    subtotalnets++;
	    
	    switch (toggletype) {
	    case SUM:
		if (tp->tog_sum > threshold) {
		    togglednets++;
		}
		break;
	    case TO_0:
		if (tp->tog_to_0 > threshold) {
		    togglednets++;
		}
		break;
	    case TO_1:
		if (tp->tog_to_1 > threshold) {
		    togglednets++;
		}
		break;
	    }
	}
	
	i++;
	if (i == CHUNK_TOGGLE) {
	    tableindex++;
	    tp = toggle_tbl[tableindex];
	    i = 0;
	} else {
	    tp++;
	}
    }
    switch (toggletype) {
    case SUM:
	s = "sum";
	break;
    case TO_0:
	s = "to_0";
	break;
    case TO_1:
	s = "to_1";
	break;
    }
    fprintf(out, "Toggle summary:\n");
    fprintf(out, "Total Nets %d\n\n", totalnets);
    fprintf(out, "Listed nets %d\n", subtotalnets);
    fprintf(out, "Looking at %s > %d\n", s, threshold); 
    if (subtotalnets) {
	fprintf(out, "toggled nets %d,  (%d%%)\n", togglednets, 
		(togglednets*100)/subtotalnets);
    } else {
	fprintf(out, "no toggled nets\n");
    }
}

void
list(FILE *out)
{
    int i;
    int tableindex;
    struct toggle *tp;
    
    tableindex = 0;
    i = 0;
    tp = toggle_tbl[tableindex];
    
    
    fprintf(out, "Untoggled List:\n");
    
    while (tp) {
    
	if (tp->flags & LIST_FLAG) {
	    if (tp->tog_sum == 0) {
		fprintf(out, "%s\n", tp->netname);
	    }
	}
	
	i++;
	if (i == CHUNK_TOGGLE) {
	    tableindex++;
	    tp = toggle_tbl[tableindex];
	    i = 0;
	} else {
	    tp++;
	}
    }
}

unsigned long
calchash(char *s)
{
    unsigned long hashvalue;
    unsigned char c;
    
    hashvalue = 0;
    while (1) {
	c = *s++;
	if (!c)
	    break;
	hashvalue <<= 1;
	hashvalue += c;
    }
    hashvalue %= HASH_TABLE_SIZE;
    
    return hashvalue;
}

void
addhash(struct toggle *tp)
{
    unsigned long hashvalue;
    struct hash_entry *hp;
    
    hashvalue = calchash(tp->netname);
    
    hp = &hash_table[hashvalue];
    if (hp->tp == 0) {
	/* need to add first entry */
	hash_table[hashvalue].tp = tp;
    } else {
	/* add as the front entry */
	hp = (struct hash_entry *)malloc(sizeof(struct hash_entry));
	hp->tp = hash_table[hashvalue].tp;
	hp->next = hash_table[hashvalue].next;
	hash_table[hashvalue].tp = tp;
	hash_table[hashvalue].next = hp;
    }
}

struct toggle *
locatehash(char *netname)
{
    unsigned long hashvalue;
    struct hash_entry *hp;
    
    hashvalue = calchash(netname);
    
    hp = &hash_table[hashvalue];
    
    while (hp && hp->tp) {
	if (strcmp(netname, hp->tp->netname) == 0) {
	    return hp->tp;
	} else {
	    hp = hp->next;
	}
    }
    return 0;
}