keep track of pending selects
[unix-history] / usr / src / sys / kern / subr_prof.c
index cbfc9a8..cf6b88c 100644 (file)
@@ -1,18 +1,34 @@
-/*     subr_prof.c     4.1     82/06/28        */
+/*
+ * Copyright (c) 1982, 1986 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ *
+ *     @(#)subr_prof.c 7.8 (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
+#if defined(hp300)
+char   *s_lowpc = (char *)0x00000000;
+#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 +38,236 @@ 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(vmemall, 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(vmemall, 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(vmemall, s_textsize);
+       bzero(froms, fromssize);
+       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));
-#ifdef notdef
-       profiling = 0;          /* patch by hand when you're ready */
-#endif
+       kcount = (u_short *)(((int)sbuf) + sizeof (struct phdr));
 }
 
 /*
 }
 
 /*
- * This routine is massaged so that it may be jsb'ed to
+ * Special, non-profiled versions
  */
  */
-asm("#define _mcount mcount");
+#if defined(hp300) && !defined(__GNUC__)
+#define splhigh        _splhigh
+#define splx   _splx
+#endif
+
+/*
+ * This routine is massaged so that it may be jsb'ed to on vax.
+ */
+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
+       ;                               /* avoid label botch */
+#ifdef __GNUC__
+#if defined(vax)
+       Fix Me!!
+#endif
+#if defined(tahoe)
+       Fix Me!!
+#endif
+#if defined(hp300)
+       /*
+        * selfpc = pc pushed by mcount jsr,
+        * frompcindex = pc pushed by jsr into self.
+        * In GCC the caller's stack frame has already been built so we
+        * have to chase a6 to find caller's raddr.  This assumes that all
+        * routines we are profiling were built with GCC and that all
+        * profiled routines use link/unlk.
+        */
+       asm("movl a6@(4),%0" : "=r" (selfpc));
+       asm("movl a6@(0)@(4),%0" : "=r" (frompcindex));
+#endif
+#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)
+       asm("   movl -8(fp),r12");      /* selfpc = callf frame */
+       asm("   movl (fp),r11");
+       asm("   movl -8(r11),r11");     /* frompcindex = 1 callf frame back */
+#endif
+#if defined(hp300)
+       asm("   .text");                /* make sure we're in text space */
+       asm("   movl a6@(4),a5");       /* selfpc = pc pushed by mcount jsr */
+       asm("   movl a6@(8),a4");       /* frompcindex = pc pushed by jsr into
+                                          self, stack frame not yet built */
+#endif
+#endif /* not __GNUC__ */
+#endif /* not lint */
        /*
        /*
-        * 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++;
+#if defined(hp300) && defined(__GNUC__)
+       asm("movw       sr,%0" : "=g" (s));
+       asm("movw       #0x2700,sr");
+#else
+       s = splhigh();
+#endif
        /*
         * 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;
        }
        }
-       for (; /* break */; top = &tos[top->link]) {
+       top = &tos[toindex];
+       if (top->selfpc == selfpc) {
+               /*
+                * Arc at front of chain; usual case.
+                */
+               top->count++;
+               goto done;
+       }
+       /*
+        * 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--;
+#if defined(hp300) && defined(__GNUC__)
+       asm("movw       %0,sr" : : "g" (s));
+#else
+       splx(s);
+#endif
        /* 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