how did these ever work before?
[unix-history] / usr / src / sys / kern / subr_prof.c
index a8a5f05..63cb05e 100644 (file)
@@ -1,75 +1,87 @@
-/*     subr_prof.c     4.3     82/12/17        */
+/*     subr_prof.c     4.5     83/07/01        */
+
+/* last integrated from: gmon.c        4.10 (Berkeley) 1/14/83 */
 
 #ifdef GPROF
 
 #ifdef GPROF
-#include "../h/crt0.h"
+#include "../h/gprof.h"
 #include "../h/param.h"
 #include "../h/systm.h"
 
 /*
  * Froms is actually a bunch of unsigned shorts indexing tos
  */
 #include "../h/param.h"
 #include "../h/systm.h"
 
 /*
  * Froms is actually a bunch of unsigned shorts indexing tos
  */
-int    profiling = 3;
-u_short        *froms = 0;
-struct tostruct *tos = 0;
-u_short        tolimit = 0;
+int profiling = 3;
+u_short *froms;
+struct tostruct *tos = 0;
+long tolimit = 0;
 #ifdef vax
 char   *s_lowpc = (char *)0x80000000;
 #endif
 #ifdef vax
 char   *s_lowpc = (char *)0x80000000;
 #endif
-#ifdef sun
-char   *s_lowpc = (char *)0x4000;
-#endif
-extern char etext;
-char   *s_highpc = &etext;
+extern char etext;
+char *s_highpc = &etext;
 u_long s_textsize = 0;
 u_long s_textsize = 0;
-int    ssiz;
+int ssiz;
 u_short        *sbuf;
 u_short        *kcount;
 
 kmstartup()
 {
 u_short        *sbuf;
 u_short        *kcount;
 
 kmstartup()
 {
-       u_long  limit;
+       u_long  fromssize, tossize;
 
 
+       /*
+        *      round lowpc and highpc to multiples of the density we're using
+        *      so the rest of the scaling (here and in gprof) stays in ints.
+        */
+       s_lowpc = (char *)
+           ROUNDDOWN((unsigned)s_lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
+       s_highpc = (char *)
+           ROUNDUP((unsigned)s_highpc, HISTFRACTION*sizeof(HISTCOUNTER));
        s_textsize = s_highpc - s_lowpc;
        s_textsize = s_highpc - s_lowpc;
-       ssiz = s_textsize + sizeof(struct phdr);
        printf("Profiling kernel, s_textsize=%d [%x..%x]\n",
                s_textsize, s_lowpc, s_highpc);
        printf("Profiling kernel, s_textsize=%d [%x..%x]\n",
                s_textsize, s_lowpc, s_highpc);
+       ssiz = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
        sbuf = (u_short *)wmemall(memall, ssiz);
        if (sbuf == 0) {
                printf("No space for monitor buffer(s)\n");
                return;
        }
        blkclr((caddr_t)sbuf, ssiz);
        sbuf = (u_short *)wmemall(memall, ssiz);
        if (sbuf == 0) {
                printf("No space for monitor buffer(s)\n");
                return;
        }
        blkclr((caddr_t)sbuf, ssiz);
-       froms = (u_short *)wmemall(memall, s_textsize);
+       fromssize = s_textsize / HASHFRACTION;
+       froms = (u_short *)wmemall(memall, fromssize);
        if (froms == 0) {
                printf("No space for monitor buffer(s)\n");
                wmemfree(sbuf, ssiz);
                sbuf = 0;
                return;
        }
        if (froms == 0) {
                printf("No space for monitor buffer(s)\n");
                wmemfree(sbuf, ssiz);
                sbuf = 0;
                return;
        }
-       blkclr((caddr_t)froms, s_textsize);
-       tos = (struct tostruct *)wmemall(memall, s_textsize);
+       blkclr((caddr_t)froms, fromssize);
+       tolimit = s_textsize * ARCDENSITY / 100;
+       if (tolimit < MINARCS) {
+               tolimit = MINARCS;
+       } else if (tolimit > 65534) {
+               tolimit = 65534;
+       }
+       tossize = tolimit * sizeof(struct tostruct);
+       tos = (struct tostruct *)wmemall(memall, tossize);
        if (tos == 0) {
                printf("No space for monitor buffer(s)\n");
                wmemfree(sbuf, ssiz);
                sbuf = 0;
        if (tos == 0) {
                printf("No space for monitor buffer(s)\n");
                wmemfree(sbuf, ssiz);
                sbuf = 0;
-               wmemfree(froms, s_textsize);
+               wmemfree(froms, fromssize);
                froms = 0;
                return;
        }
                froms = 0;
                return;
        }
-       blkclr((caddr_t)tos, s_textsize);
+       blkclr((caddr_t)tos, tossize);
        tos[0].link = 0;
        tos[0].link = 0;
-       limit = s_textsize / sizeof(struct tostruct);
-       /*
-        * Tolimit is what mcount checks to see if
-        * all the data structures are ready!!!
-        * Make sure it won't overflow.
-        */
-       tolimit = limit > 65534 ? 65534 : limit;
        ((struct phdr *)sbuf)->lpc = s_lowpc;
        ((struct phdr *)sbuf)->hpc = s_highpc;
        ((struct phdr *)sbuf)->ncnt = ssiz;
        kcount = (u_short *)(((int)sbuf) + sizeof(struct phdr));
 #ifdef notdef
        ((struct phdr *)sbuf)->lpc = s_lowpc;
        ((struct phdr *)sbuf)->hpc = s_highpc;
        ((struct phdr *)sbuf)->ncnt = ssiz;
        kcount = (u_short *)(((int)sbuf) + sizeof(struct phdr));
 #ifdef notdef
+       /*
+        *      profiling is what mcount checks to see if
+        *      all the data structures are ready!!!
+        */
        profiling = 0;          /* patch by hand when you're ready */
 #endif
 }
        profiling = 0;          /* patch by hand when you're ready */
 #endif
 }
@@ -78,87 +90,130 @@ kmstartup()
 /*
  * This routine is massaged so that it may be jsb'ed to
  */
 /*
  * This routine is massaged so that it may be jsb'ed to
  */
-asm("#define _mcount mcount");
+asm(".text");
+asm("#the beginning of mcount()");
+asm(".data");
 mcount()
 {
 mcount()
 {
-       register char *selfpc;          /* r11 */
-       register u_short *frompcindex;  /* r10 */
-       register struct tostruct *top;  /* r9 */
+       register char                   *selfpc;        /* r11 => r5 */
+       register unsigned short         *frompcindex;   /* r10 => r4 */
+       register struct tostruct        *top;           /* r9  => r3 */
+       register struct tostruct        *prevtop;       /* r8  => r2 */
+       register long                   toindex;        /* r7  => r1 */
 
 
-       asm("   forgot to run ex script on gcrt0.s");
-       asm("#define r11 r5");
-       asm("#define r10 r4");
-       asm("#define r9 r3");
 #ifdef lint
 #ifdef lint
-       selfpc = (char *) 0;
+       selfpc = (char *)0;
        frompcindex = 0;
 #else not lint
        /*
        frompcindex = 0;
 #else not lint
        /*
-        * Find the return address for mcount,
-        * and the return address for mcount's caller.
+        *      find the return address for mcount,
+        *      and the return address for mcount's caller.
         */
         */
+       asm("   .text");                /* make sure we're in text space */
        asm("   movl (sp), r11");       /* selfpc = ... (jsb frame) */
        asm("   movl 16(fp), r10");     /* frompcindex =     (calls frame) */
 #endif not lint
        /*
        asm("   movl (sp), r11");       /* selfpc = ... (jsb frame) */
        asm("   movl 16(fp), r10");     /* frompcindex =     (calls frame) */
 #endif not lint
        /*
-        * Check that we are profiling
-        * and that we aren't recursively invoked.
+        *      check that we are profiling
+        *      and that we aren't recursively invoked.
         */
         */
-       if (tolimit == 0)
-               goto out;
-       if (profiling)
+       if (profiling) {
                goto out;
                goto out;
+       }
        profiling++;
        /*
        profiling++;
        /*
-        * Check that frompcindex is a reasonable pc value.
-        * For example: signal catchers get called from the stack,
-        *              not from text space.  too bad.
+        *      check that frompcindex is a reasonable pc value.
+        *      for example:    signal catchers get called from the stack,
+        *                      not from text space.  too bad.
         */
         */
-       frompcindex = (u_short *)((long)frompcindex - (long)s_lowpc);
-       if ((u_long)frompcindex > s_textsize)
+       frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
+       if ((unsigned long)frompcindex > s_textsize) {
                goto done;
                goto done;
-       frompcindex = &froms[((long)frompcindex) >> 1];
-       if (*frompcindex != 0)
-               top = &tos[*frompcindex];
-       else {
-               *frompcindex = ++tos[0].link;
-               if (*frompcindex >= tolimit)
+       }
+       frompcindex =
+           &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
+       toindex = *frompcindex;
+       if (toindex == 0) {
+               /*
+                *      first time traversing this arc
+                */
+               toindex = ++tos[0].link;
+               if (toindex >= tolimit) {
                        goto overflow;
                        goto overflow;
-               top = &tos[*frompcindex];
+               }
+               *frompcindex = toindex;
+               top = &tos[toindex];
                top->selfpc = selfpc;
                top->selfpc = selfpc;
-               top->count = 0;
+               top->count = 1;
                top->link = 0;
                top->link = 0;
+               goto done;
+       }
+       top = &tos[toindex];
+       if (top->selfpc == selfpc) {
+               /*
+                *      arc at front of chain; usual case.
+                */
+               top->count++;
+               goto done;
        }
        }
-       for (; /* break */; top = &tos[top->link]) {
+       /*
+        *      have to go looking down chain for it.
+        *      top points to what we are looking at,
+        *      prevtop points to previous top.
+        *      we know it is not at the head of the chain.
+        */
+       for (; /* goto done */; ) {
+               if (top->link == 0) {
+                       /*
+                        *      top is end of the chain and none of the chain
+                        *      had top->selfpc == selfpc.
+                        *      so we allocate a new tostruct
+                        *      and link it to the head of the chain.
+                        */
+                       toindex = ++tos[0].link;
+                       if (toindex >= tolimit) {
+                               goto overflow;
+                       }
+                       top = &tos[toindex];
+                       top->selfpc = selfpc;
+                       top->count = 1;
+                       top->link = *frompcindex;
+                       *frompcindex = toindex;
+                       goto done;
+               }
+               /*
+                *      otherwise, check the next arc on the chain.
+                */
+               prevtop = top;
+               top = &tos[top->link];
                if (top->selfpc == selfpc) {
                if (top->selfpc == selfpc) {
+                       /*
+                        *      there it is.
+                        *      increment its count
+                        *      move it to the head of the chain.
+                        */
                        top->count++;
                        top->count++;
-                       break;
+                       toindex = prevtop->link;
+                       prevtop->link = top->link;
+                       top->link = *frompcindex;
+                       *frompcindex = toindex;
+                       goto done;
                }
                }
-               if (top->link != 0)
-                       continue;
-               top->link = ++tos[0].link;
-               if (top->link >= tolimit)
-                       goto overflow;
-               top = &tos[top->link];
-               top->selfpc = selfpc;
-               top->count = 1;
-               top->link = 0;
-               break;
+
        }
 done:
        profiling--;
        /* and fall through */
 out:
        asm("   rsb");
        }
 done:
        profiling--;
        /* and fall through */
 out:
        asm("   rsb");
-       asm("#undef r11");
-       asm("#undef r10");
-       asm("#undef r9");
-       asm("#undef _mcount");
 
 overflow:
 
 overflow:
-       tolimit = 0;
+       profiling = 3;
        printf("mcount: tos overflow\n");
        goto out;
 }
        printf("mcount: tos overflow\n");
        goto out;
 }
-#endif
+asm(".text");
+asm("#the end of mcount()");
+asm(".data");
+#endif vax
 #endif GPROF
 #endif GPROF