delete unneeded header files
[unix-history] / usr / src / sys / kern / subr_prof.c
index def3aff..f359fba 100644 (file)
@@ -1,18 +1,30 @@
-/*     subr_prof.c     4.2     82/11/13        */
+/*
+ * Copyright (c) 1982, 1986 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ *
+ *     @(#)subr_prof.c 7.4 (Berkeley) %G%
+ */
 
 #ifdef GPROF
 
 #ifdef GPROF
-#include "../h/crt0.h"
-#include "../h/param.h"
-#include "../h/systm.h"
+#include "gprof.h"
+#include "param.h"
+#include "systm.h"
+#include "malloc.h"
 
 /*
  * Froms is actually a bunch of unsigned shorts indexing tos
  */
 int    profiling = 3;
 
 /*
  * Froms is actually a bunch of unsigned shorts indexing tos
  */
 int    profiling = 3;
-u_short        *froms = 0;
+u_short        *froms;
 struct tostruct *tos = 0;
 struct tostruct *tos = 0;
-u_short        tolimit = 0;
+long   tolimit = 0;
+#if defined(vax)
 char   *s_lowpc = (char *)0x80000000;
 char   *s_lowpc = (char *)0x80000000;
+#endif
+#if defined(tahoe)
+char   *s_lowpc = (char *)0xc0000000;
+#endif
 extern char etext;
 char   *s_highpc = &etext;
 u_long s_textsize = 0;
 extern char etext;
 char   *s_highpc = &etext;
 u_long s_textsize = 0;
@@ -22,136 +34,199 @@ u_short   *kcount;
 
 kmstartup()
 {
 
 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);
-       sbuf = (u_short *)wmemall(memall, ssiz);
+       ssiz = (s_textsize / HISTFRACTION) + sizeof (struct phdr);
+       sbuf = (u_short *)malloc(ssiz, M_GPROF, M_WAITOK);
        if (sbuf == 0) {
                printf("No space for monitor buffer(s)\n");
                return;
        }
        if (sbuf == 0) {
                printf("No space for monitor buffer(s)\n");
                return;
        }
-       blkclr((caddr_t)sbuf, ssiz);
-       froms = (u_short *)wmemall(memall, s_textsize);
+       bzero(sbuf, ssiz);
+       fromssize = s_textsize / HASHFRACTION;
+       froms = (u_short *)malloc(fromssize, M_GPROF, M_WAITOK);
        if (froms == 0) {
                printf("No space for monitor buffer(s)\n");
        if (froms == 0) {
                printf("No space for monitor buffer(s)\n");
-               wmemfree(sbuf, ssiz);
+               free(sbuf, M_GPROF);
                sbuf = 0;
                return;
        }
                sbuf = 0;
                return;
        }
-       blkclr((caddr_t)froms, s_textsize);
-       tos = (struct tostruct *)wmemall(memall, s_textsize);
+       bzero(froms, fromsize);
+       tolimit = s_textsize * ARCDENSITY / 100;
+       if (tolimit < MINARCS)
+               tolimit = MINARCS;
+       else if (tolimit > (0xffff - 1))
+               tolimit = 0xffff - 1;
+       tossize = tolimit * sizeof (struct tostruct);
+       tos = (struct tostruct *)malloc(tossize, M_GPROF, M_WAITOK);
        if (tos == 0) {
                printf("No space for monitor buffer(s)\n");
        if (tos == 0) {
                printf("No space for monitor buffer(s)\n");
-               wmemfree(sbuf, ssiz);
-               sbuf = 0;
-               wmemfree(froms, s_textsize);
-               froms = 0;
+               free(sbuf, M_GPROF), sbuf = 0;
+               free(froms, M_GPROF), froms = 0;
                return;
        }
                return;
        }
-       blkclr((caddr_t)tos, s_textsize);
+       bzero(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;
        ((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));
+       kcount = (u_short *)(((int)sbuf) + sizeof (struct phdr));
 #ifdef notdef
 #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
 }
 
 /*
- * This routine is massaged so that it may be jsb'ed to
+ * This routine is massaged so that it may be jsb'ed to on vax.
  */
  */
-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 u_short *frompcindex;          /* r10 => r4 */
+       register struct tostruct *top;          /* r9  => r3 */
+       register struct tostruct *prevtop;      /* r8  => r2 */
+       register long toindex;                  /* r7  => r1 */
+       static int s;
 
 
-       asm("   forgot to run ex script on gcrt0.s");
-       asm("#define r11 r5");
-       asm("#define r10 r4");
-       asm("#define r9 r3");
-#ifdef lint
-       selfpc = (char *) 0;
-       frompcindex = 0;
-#else not lint
+       asm("   .text");                /* make sure we're in text space */
+       /*
+        * Check that we are profiling.
+        */
+       if (profiling)
+               goto out;
        /*
         * 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.
         */
+#ifdef lint
+       selfpc = (char *)0;
+       frompcindex = 0;
+#else
+#if defined(vax)
        asm("   movl (sp), r11");       /* selfpc = ... (jsb frame) */
        asm("   movl 16(fp), r10");     /* frompcindex =     (calls frame) */
        asm("   movl (sp), r11");       /* selfpc = ... (jsb frame) */
        asm("   movl 16(fp), r10");     /* frompcindex =     (calls frame) */
-#endif not lint
+#endif
+#if defined(tahoe)
+       ;                               /* avoid label botch */
+       asm("   movl -8(fp),r12");      /* selfpc = callf frame */
+       asm("   movl (fp),r11");
+       asm("   movl -8(r11),r11");     /* frompcindex = 1 callf frame back */
+#endif
+#endif
        /*
        /*
-        * Check that we are profiling
-        * and that we aren't recursively invoked.
+        * Insure that we cannot be recursively invoked.
+        * this requires that splhigh() and splx() below
+        * do NOT call mcount!
         */
         */
-       if (tolimit == 0)
-               goto out;
-       if (profiling)
-               goto out;
-       profiling++;
+       s = splhigh();
        /*
         * Check that frompcindex is a reasonable pc value.
         * For example: signal catchers get called from the stack,
        /*
         * Check that frompcindex is a reasonable pc value.
         * For example: signal catchers get called from the stack,
-        *              not from text space.  too bad.
+        *      not from text space.  too bad.
         */
        frompcindex = (u_short *)((long)frompcindex - (long)s_lowpc);
        if ((u_long)frompcindex > s_textsize)
                goto done;
         */
        frompcindex = (u_short *)((long)frompcindex - (long)s_lowpc);
        if ((u_long)frompcindex > s_textsize)
                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 and
+                        * 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:
        }
 done:
-       profiling--;
+       splx(s);
        /* and fall through */
 out:
        /* and fall through */
 out:
+#if defined(vax)
        asm("   rsb");
        asm("   rsb");
-       asm("#undef r11");
-       asm("#undef r10");
-       asm("#undef r9");
-       asm("#undef _mcount");
-
+#endif
+       return;
 overflow:
 overflow:
-       tolimit = 0;
+       profiling = 3;
        printf("mcount: tos overflow\n");
        goto out;
 }
        printf("mcount: tos overflow\n");
        goto out;
 }
-#endif GPROF
+asm(".text");
+asm("#the end of mcount()");
+asm(".data");
+#endif