Annotation of researchv8dc/cmd/lcomp/lprint.c, revision 1.1

1.1     ! root        1: #include "stdio.h"
        !             2: #include "ctype.h"
        !             3: typedef unsigned long ul;
        !             4: typedef struct {
        !             5:        char *fname;
        !             6:        int len;        /* how many counts have been seen */
        !             7:        int quot;       /* how much has been allocated for cnt */
        !             8:        unsigned long *cnt;
        !             9: } stab;
        !            10: stab *tab;
        !            11: int ntab, ltab;
        !            12: unsigned long N, B, L, V;      /* for summary */
        !            13: double A, U;
        !            14: FILE *fd, *sfd, *cfd;
        !            15: extern FILE *popen();
        !            16: char fname[512] = "/"; /* not checked for overflow */
        !            17: char buf[256];
        !            18: char curfunc[256];
        !            19: extern char *malloc();
        !            20: char flg[128];
        !            21: unsigned long val;
        !            22: 
        !            23: main(argc, argv)
        !            24: char **argv;
        !            25: {      int i, j;
        !            26:        for(i = 1; i < argc; i++)
        !            27:                if(argv[i][0] != '-')
        !            28:                        fprintf(stderr, "unknown arg %s\n", argv[i]);
        !            29:                else for(j = 1; argv[i][j]; j++)
        !            30:                        switch(argv[i][j]) {
        !            31:                        default:
        !            32:                                fprintf(stderr, "unk option %c\n", argv[i][j]);
        !            33:                                break;
        !            34:                        case 'b':       /* each bb */
        !            35:                                flg['b']++;
        !            36:                                break;
        !            37:                        case 'a':       /* intermediate everything */
        !            38:                                flg['a']++;
        !            39:                                break;
        !            40:                        case 'i':       /* count machine instrs */
        !            41:                                flg['i']++;
        !            42:                                break;
        !            43:                        case 'f':       /* counts by function */
        !            44:                                flg['f']++;
        !            45:                                flg['s']++;
        !            46:                                break;
        !            47:                        case 'p':       /* print, in addition */
        !            48:                                flg['p']++;
        !            49:                                break;
        !            50:                        case 's':       /* bb summary */
        !            51:                                flg['s']++;
        !            52:                                break;
        !            53:                        case 'c':       /* compress prof.out */
        !            54:                                flg['c']++;
        !            55:                                break;
        !            56:                        }
        !            57:        if(argc <= 1)
        !            58:                flg['p']++;
        !            59:        if((fd = fopen("prof.out", "r")) == 0) {
        !            60:                perror("prof.out");
        !            61:                exit(1);
        !            62:        }
        !            63:        readall();
        !            64:        fclose(fd);
        !            65:        if(flg['f']) {
        !            66:                for(i = 0; i < ntab; i++)
        !            67:                        fsum(tab + i);
        !            68:        }
        !            69:        if(flg['s']) {
        !            70:                for(i = 0; i < ntab; i++)
        !            71:                        summary(tab + i);
        !            72:                if(ntab > 1)
        !            73:                        printf("%.0fie %ui %uin %.0fbbe %ubb %ubbne total\n",
        !            74:                                A, N, B, U, L, V);
        !            75:        }
        !            76:        if(flg['c']) {
        !            77:                fd = fopen("prof.out", "w");
        !            78:                if(fd == 0) {
        !            79:                        perror("rewriting prof.out");
        !            80:                        exit(1);
        !            81:                }
        !            82:                for(i = 0; i < ntab; i++) {
        !            83:                        fprintf(fd, "%s\n", tab[i].fname);
        !            84:                        for(j = 0; j < tab[i].len; j++)
        !            85:                                fprintf(fd, "%u\n", tab[i].cnt[j]);
        !            86:                }
        !            87:                fclose(fd);
        !            88:        }
        !            89:        if(flg['p'] || flg['i'] || flg['a'] || flg['b'])
        !            90:                giantprint();
        !            91:        exit(0);
        !            92: }
        !            93: 
        !            94: readall()
        !            95: {      int c, i, index;
        !            96:        stab *curtab = 0;
        !            97: sawnl:
        !            98:        if((c = getc(fd)) == EOF)
        !            99:                return;
        !           100:        if(c == '\n')
        !           101:                goto sawnl;
        !           102:        if(c == '/') {
        !           103:                fscanf(fd, "%s", fname+1);
        !           104:                for(i = 0; i < ntab; i++)
        !           105:                        if(strcmp(fname, tab[i].fname) == 0)
        !           106:                                break;
        !           107:                if(i >= ntab) { /* new file */
        !           108:                        if(ltab == 0) {
        !           109:                                tab = (stab *)malloc(20 * sizeof(stab));
        !           110:                                ltab = 20;
        !           111:                        }
        !           112:                        else if(ntab >= ltab)
        !           113:                                tab = (stab *)realloc((char *)tab,
        !           114:                                        (ltab += 20) * sizeof(stab));
        !           115:                        tab[ntab].fname = malloc(sizeof(fname) + 1);
        !           116:                        strcpy(tab[ntab].fname, fname);
        !           117:                        tab[ntab].len = tab[ntab].quot = 0;
        !           118:                        ntab++;
        !           119:                }
        !           120:                curtab = tab + i;
        !           121:                index = 0;
        !           122:        }
        !           123:        else if(c < '0' || c > '9') {
        !           124:                fprintf(stderr, "prof.out has weird format\n");
        !           125:                abort();
        !           126:        }
        !           127:        else {
        !           128:                ungetc(c, fd);
        !           129:                fscanf(fd, "%d", &val);
        !           130:                if(curtab->len <= index) {
        !           131:                        if(curtab->quot == 0) {
        !           132:                                curtab->cnt = (ul *)malloc(100*sizeof(long));
        !           133:                                curtab->quot = 100;
        !           134:                                for(i = 0; i < 100; i++)
        !           135:                                        curtab->cnt[i] = 0;
        !           136:                        }
        !           137:                        else if(curtab->len >= curtab->quot) {
        !           138:                                curtab->cnt = (ul *)realloc(curtab->cnt,
        !           139:                                        (curtab->quot += 200) * sizeof(long));
        !           140:                                for(i = curtab->quot-200; i < curtab->quot; i++)
        !           141:                                        curtab->cnt[i] = 0;
        !           142:                        }
        !           143:                        curtab->len++;
        !           144:                }
        !           145:                curtab->cnt[index++] += val;
        !           146:        }
        !           147:        goto sawnl;
        !           148: }
        !           149: 
        !           150: summary(x)
        !           151: stab *x;
        !           152: {      unsigned long i, v, n, b;
        !           153:        double a, u;
        !           154:        for(i = u = v = 0; i < x->len; i++)
        !           155:                if(x->cnt[i])
        !           156:                        u += x->cnt[i];
        !           157:                else
        !           158:                        v++;
        !           159:        n = strlen(x->fname);
        !           160:        strcpy(buf, x->fname);
        !           161:        strcpy(buf + n - 2, ".sL");
        !           162:        if((fd = fopen(buf, "r")) == 0) {
        !           163:                perror(buf);
        !           164:                printf("%s %u bbs %.0f execs %u untouched\n",
        !           165:                        x->fname, x->len, u, v);
        !           166:                return;
        !           167:        }
        !           168:        for(n = a = b = 0; ;) {
        !           169:                (void) fgets(buf, sizeof(buf), fd);
        !           170:                if(feof(fd))
        !           171:                        break;
        !           172:                if(hascolon(buf))
        !           173:                        continue;
        !           174:                n++;
        !           175:                i = atoi(buf)/4 - 3;
        !           176:                if(x->cnt[i])
        !           177:                        a += x->cnt[i];
        !           178:                else
        !           179:                        b++;
        !           180:        }
        !           181:        printf("%.0fie %ui %uine %.0fbbe %ubb %ubbne %s\n", a, n, b, u, x->len,
        !           182:                v, x->fname);
        !           183:        fclose(fd);
        !           184:        A += a; N += n; B += b; U += u; L += x->len; V += v;
        !           185: }
        !           186: 
        !           187: fsum(x)
        !           188: stab *x;
        !           189: {      unsigned i, v, n, b, cnt;
        !           190:        double a, u;
        !           191:        char *p;
        !           192:        strcpy(buf, x->fname);
        !           193:        n = strlen(x->fname);
        !           194:        strcpy(buf + n - 2, ".sL");
        !           195:        if((fd = fopen(buf, "r")) == 0) {
        !           196:                perror(buf);
        !           197:                return;
        !           198:        }
        !           199:        curfunc[0] = 0;
        !           200:        for(cnt = v = n = b = a = u = 0;;) {
        !           201:                (void) fgets(buf, sizeof(buf), fd);
        !           202:                if(feof(fd))
        !           203:                        break;
        !           204:                if(hascolon(buf)) {
        !           205:                        /* 24 foo.c: 456        or
        !           206:                         * 24 foo.c: _funcjunk */
        !           207:                        for(p = buf; *p != ':'; p++)
        !           208:                                ;
        !           209:                        while(isspace(*++p))
        !           210:                                ;
        !           211:                        if(isdigit(*p))
        !           212:                                continue;
        !           213:                        if(curfunc[0] != 0)
        !           214:                                printf("%.0fie %dcalls %ui %uine %s\n",
        !           215:                                        a, v, n, b, curfunc);
        !           216:                        for(i = 0; *p && *p != '\n'; i++)
        !           217:                                curfunc[i] = *p++;
        !           218:                        curfunc[i] = 0;
        !           219:                        a = n = b = 0;
        !           220:                        i = atoi(buf)/4 - 3;
        !           221:                        v = x->cnt[i];
        !           222:                        continue;
        !           223:                }
        !           224:                n++;
        !           225:                i = atoi(buf)/4 - 3;
        !           226:                if(x->cnt[i])
        !           227:                        a += x->cnt[i];
        !           228:                else
        !           229:                        b++;
        !           230:        }
        !           231:        if(n > 0)
        !           232:                printf("%.0fie %dcalls %ui %uine %s\n", a, v, n, b, curfunc);
        !           233:        fclose(fd);
        !           234: }
        !           235: 
        !           236: giantprint()
        !           237: {      int i, n;
        !           238:        for(i = 0; i < ntab; i++) {
        !           239:        /* the fname file is the source, there should be a corresponding
        !           240:         * .sL file for correlation between basic blocks and source.
        !           241:         * If fname is a .s file, the .sL file is all there is */
        !           242:                n = strlen(tab[i].fname);
        !           243:                if(strcmp(".s", tab[i].fname + n - 2) == 0) {
        !           244:                        sfile(tab + i);
        !           245:                        continue;
        !           246:                }
        !           247:                sfd = fopen(tab[i].fname, "r");
        !           248:                if(sfd == 0 && !flg['a']) {
        !           249:                        perror(tab[i].fname);
        !           250:                        return;
        !           251:                }
        !           252:                strcpy(buf, tab[i].fname);
        !           253:                strcpy(buf + n - 1, "sL");
        !           254:                if((cfd = fopen(buf, "r")) == 0) {
        !           255:                        fprintf(stderr, "no intermediate listing file ");
        !           256:                        perror(buf);
        !           257:                        fclose(sfd);
        !           258:                        return;
        !           259:                }
        !           260:                sprintf(buf, "pr -h %s", tab[i].fname);
        !           261:                if(!flg['a'])
        !           262:                        fd = popen(buf, "w");
        !           263:                if(flg['b'])
        !           264:                        xlistit(tab + i);
        !           265:                else
        !           266:                        listit(tab + i);
        !           267:                if(fd)
        !           268:                        pclose(fd);
        !           269:                if(sfd)
        !           270:                        fclose(sfd);
        !           271:                fclose(cfd);
        !           272:        }
        !           273: }
        !           274: 
        !           275: sfile(x)
        !           276: stab *x;
        !           277: {      int i;
        !           278:        char *p;
        !           279:        strcpy(buf, x->fname);
        !           280:        strcat(buf, "L");
        !           281:        sfd = fopen(buf, "r");
        !           282:        if(sfd == 0) {
        !           283:                perror(buf);
        !           284:                return;
        !           285:        }
        !           286:        sprintf(buf, "pr -h %s", x->fname);
        !           287:        fd = popen(buf, "w");
        !           288:        if(fd == 0) {
        !           289:                perror(buf);
        !           290:                fclose(sfd);
        !           291:                return;
        !           292:        }
        !           293:        for(;;) {
        !           294:                (void) fgets(buf, sizeof(buf), sfd);
        !           295:                if(feof(sfd)) {
        !           296:                        pclose(fd);
        !           297:                        fclose(sfd);
        !           298:                        return;
        !           299:                }
        !           300:                for(i = 0, p = buf; *p >= '0' && *p <= '9'; p++)
        !           301:                        i = 10 * i + *p - '0';
        !           302:                i = i/4 - 3;
        !           303:                fprintf(fd, "%u\t%s", x->cnt[i], p);
        !           304:        }
        !           305: }
        !           306: 
        !           307: char *
        !           308: stripnum(s)
        !           309: char *s;
        !           310: {
        !           311:        while(*s && (isdigit(*s) || isspace(*s)))
        !           312:                s++;
        !           313:        return(s);
        !           314: }
        !           315: 
        !           316: listit(x)
        !           317: stab *x;
        !           318: {      int bb, lnum, done, i;
        !           319:        unsigned long count;
        !           320:        char *p;
        !           321:        count = done = 0;
        !           322:        for(;;) {
        !           323:                (void) fgets(buf, sizeof(buf), cfd);
        !           324:                if(feof(cfd))
        !           325:                        break;
        !           326:                for(p = buf; *p && *p != ':' ; p++)
        !           327:                        ;
        !           328:                if(*p++ != ':') {
        !           329:                        count += i = x->cnt[atoi(buf)/4 - 3];
        !           330:                        if(flg['a'])
        !           331:                                printf("%d\t%s", i, stripnum(buf));
        !           332:                        continue;
        !           333:                }
        !           334:                else if(flg['a']) {
        !           335:                        printf("%s", stripnum(buf));
        !           336:                        continue;
        !           337:                }
        !           338:                lnum = atoi(p); /* atoi("_...") == 0 */
        !           339:                i = atoi(buf);
        !           340:                for(; done < lnum - 1; done++) {
        !           341:                        (void) fgets(buf, sizeof(buf), sfd);
        !           342:                        if(flg['i'])
        !           343:                                fprintf(fd, "%ui\t", count);
        !           344:                        if(flg['p'])
        !           345:                                fprintf(fd, "%u\t", x->cnt[bb]);
        !           346:                        fprintf(fd, "%s", buf);
        !           347:                        count = 0;
        !           348:                }
        !           349:                bb = i/4 - 3;
        !           350:        }
        !           351:        if(flg['a'])
        !           352:                return;
        !           353:        (void) fgets(buf, sizeof(buf), sfd);
        !           354:        if(feof(sfd))
        !           355:                strcpy(buf, "unexpected eof\n");
        !           356:        if(flg['i'])
        !           357:                fprintf(fd, "%ui\t", count);
        !           358:        if(flg['p'])
        !           359:                fprintf(fd, "%u\t", x->cnt[bb]);
        !           360:        fprintf(fd, "%s", buf);
        !           361: }
        !           362: 
        !           363: xlistit(x)
        !           364: stab *x;
        !           365: {      int bb, lnum, i, j;
        !           366:        char *p;
        !           367:        bb = 0;
        !           368: loop:
        !           369:        if(feof(cfd))
        !           370:                return;
        !           371:        (void) fgets(buf, sizeof(buf), cfd);
        !           372:        for(p = buf; *p && *p != ':'; p++)
        !           373:                ;
        !           374:        if(*p++ != ':')
        !           375:                goto loop;
        !           376:        lnum = atoi(p);         /* hmm */
        !           377:        if(lnum <= 0)
        !           378:                goto loop;
        !           379:        i = atoi(buf)/4 - 3;
        !           380:        (void) fgets(buf, sizeof(buf), sfd);
        !           381:        buf[strlen(buf) - 1] = 0;
        !           382:        fprintf(fd, "%s", buf);
        !           383:        if(bb > i)
        !           384:                fprintf(fd, " %d:%u", i, x->cnt[i]);
        !           385:        for(j = bb; j <= i; j++)
        !           386:                fprintf(fd, " %d:%u", j, x->cnt[j]);
        !           387:        putc('\n', fd);
        !           388:        bb = j;
        !           389:        goto loop;
        !           390: }
        !           391: 
        !           392: hascolon(s)
        !           393: register char *s;
        !           394: {
        !           395:        for(; *s && *s != ':'; s++)
        !           396:                ;
        !           397:        if(*s == ':')
        !           398:                return(1);
        !           399:        else
        !           400:                return(0);
        !           401: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.